Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7b2b40c

Browse files
tamasvajkigfoo
authored andcommitted
Fix type access extraction of nested generic constructor references
1 parent 46af856 commit 7b2b40c

2 files changed

Lines changed: 23 additions & 6 deletions

File tree

java/kotlin-extractor/src/main/kotlin/KotlinFileExtractor.kt

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2614,7 +2614,12 @@ open class KotlinFileExtractor(
26142614
val callId = tw.getFreshIdLabel<DbNewexpr>()
26152615
tw.writeExprs_newexpr(callId, callType.javaResult.id, retId, 0)
26162616
tw.writeExprsKotlinType(callId, callType.kotlinResult.id)
2617-
extractTypeAccessRecursive(expressionTypeArgs.first(), locId, callId, -3, labels.methodId, retId)
2617+
2618+
val typeAccessId = extractTypeAccess(callType, locId, callId, -3, labels.methodId, retId)
2619+
if (returnType is IrSimpleType) {
2620+
// Only extract type arguments relating to the constructed type, not the constructor itself:
2621+
extractTypeArguments(returnType.arguments.take(target.owner.parentAsClass.typeParameters.size).filterIsInstance<IrType>(), locId, typeAccessId, labels.methodId, retId)
2622+
}
26182623
callId
26192624
} else {
26202625
var callId = tw.getFreshIdLabel<DbMethodaccess>()
@@ -2949,7 +2954,23 @@ open class KotlinFileExtractor(
29492954
tw.writeStatementEnclosingExpr(idMemberRef, exprParent.enclosingStmt)
29502955
tw.writeCallableBinding(idMemberRef, ids.constructor)
29512956

2952-
extractTypeAccessRecursive(fnInterfaceType, locId, idMemberRef, -3, callable, exprParent.enclosingStmt)
2957+
var typeAccessArguments = if (isBigArity) listOf(parameterTypes.last()) else parameterTypes
2958+
if (target is IrConstructorSymbol) {
2959+
val returnType = typeAccessArguments.last()
2960+
2961+
val typeAccessId = extractTypeAccess(useType(fnInterfaceType, TypeContext.OTHER), locId, idMemberRef, -3, callable, exprParent.enclosingStmt)
2962+
typeAccessArguments.dropLast(1).forEachIndexed { argIdx, arg ->
2963+
extractTypeAccessRecursive(arg, locId, typeAccessId, argIdx, callable, exprParent.enclosingStmt, TypeContext.GENERIC_ARGUMENT)
2964+
}
2965+
2966+
val returnTypeAccessId = extractTypeAccess(useType(returnType), locId, typeAccessId, typeAccessArguments.count() - 1, callable, exprParent.enclosingStmt)
2967+
if (returnType is IrSimpleType) {
2968+
// Only extract type arguments relating to the constructed type, not the constructor itself:
2969+
extractTypeArguments(returnType.arguments.take(target.owner.parentAsClass.typeParameters.size).filterIsInstance<IrType>(), locId, returnTypeAccessId, callable, exprParent.enclosingStmt)
2970+
}
2971+
} else {
2972+
extractTypeAccessRecursive(fnInterfaceType, locId, idMemberRef, -3, callable, exprParent.enclosingStmt)
2973+
}
29532974

29542975
tw.writeMemberRefBinding(idMemberRef, targetCallableId)
29552976

java/ql/test/kotlin/library-tests/reflection/PrintAst.expected

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,6 @@ reflection.kt:
157157
# 93| 0: [ClassInstanceExpr] new Inner<String>(...)
158158
# 93| -3: [TypeAccess] Inner<String>
159159
# 93| 0: [TypeAccess] String
160-
# 93| 1: [TypeAccess] Integer
161160
# 93| -2: [VarAccess] this.<dispatchReceiver>
162161
# 93| -1: [ThisAccess] this
163162
# 93| 0: [VarAccess] a0
@@ -168,7 +167,6 @@ reflection.kt:
168167
# 93| 0: [TypeAccess] String
169168
# 93| 1: [TypeAccess] Inner<String>
170169
# 93| 0: [TypeAccess] String
171-
# 93| 1: [TypeAccess] Integer
172170
# 93| 0: [ClassInstanceExpr] new Class2<Integer>(...)
173171
# 93| -3: [TypeAccess] Class2<Integer>
174172
# 93| 0: [TypeAccess] Integer
@@ -829,7 +827,6 @@ reflection.kt:
829827
# 84| 0: [ClassInstanceExpr] new Inner<String>(...)
830828
# 84| -3: [TypeAccess] Inner<String>
831829
# 84| 0: [TypeAccess] String
832-
# 84| 1: [TypeAccess] T
833830
# 84| -2: [VarAccess] this.<dispatchReceiver>
834831
# 84| -1: [ThisAccess] this
835832
# 84| 0: [VarAccess] a0
@@ -840,5 +837,4 @@ reflection.kt:
840837
# 84| 0: [TypeAccess] String
841838
# 84| 1: [TypeAccess] Inner<String>
842839
# 84| 0: [TypeAccess] String
843-
# 84| 1: [TypeAccess] T
844840
# 84| 0: [ThisAccess] this

0 commit comments

Comments
 (0)