Skip to content

Commit 4c63237

Browse files
committed
Add test checking argument <-> parameter matching, and fix superconstructor calls that were missing their argument.
1 parent f1fd470 commit 4c63237

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4496,7 +4496,10 @@ open class KotlinFileExtractor(
44964496
val baseClass = pluginContext.referenceClass(FqName("kotlin.jvm.internal.FunctionReference"))?.owner?.typeWith()
44974497
?: pluginContext.irBuiltIns.anyType
44984498

4499-
val classId = extractGeneratedClass(ids, listOf(baseClass, fnInterfaceType), locId, functionReferenceExpr, declarationParent)
4499+
val classId = extractGeneratedClass(ids, listOf(baseClass, fnInterfaceType), locId, functionReferenceExpr, declarationParent, { it.valueParameters.size == 1 }) {
4500+
// The argument to FunctionReference's constructor is the function arity.
4501+
extractConstantInteger(type.arguments.size - 1, locId, it, 0, ids.constructor, it)
4502+
}
45004503

45014504
helper.extractReceiverField()
45024505

@@ -5236,7 +5239,9 @@ open class KotlinFileExtractor(
52365239
superTypes: List<IrType>,
52375240
locId: Label<DbLocation>,
52385241
elementToReportOn: IrElement,
5239-
declarationParent: IrDeclarationParent
5242+
declarationParent: IrDeclarationParent,
5243+
superConstructorSelector: (IrFunction) -> Boolean = { it.valueParameters.isEmpty() },
5244+
extractSuperconstructorArgs: (Label<DbSuperconstructorinvocationstmt>) -> Unit = {}
52405245
): Label<out DbClass> {
52415246
// Write class
52425247
val id = ids.type.javaResult.id.cast<DbClass>()
@@ -5261,7 +5266,7 @@ open class KotlinFileExtractor(
52615266
if (baseClass == null) {
52625267
logger.warnElement("Cannot find base class", elementToReportOn)
52635268
} else {
5264-
val baseConstructor = baseClass.owner.declarations.findSubType<IrFunction> { it.symbol is IrConstructorSymbol }
5269+
val baseConstructor = baseClass.owner.declarations.findSubType<IrFunction> { it.symbol is IrConstructorSymbol && superConstructorSelector(it) }
52655270
if (baseConstructor == null) {
52665271
logger.warnElement("Cannot find base constructor", elementToReportOn)
52675272
} else {
@@ -5272,6 +5277,7 @@ open class KotlinFileExtractor(
52725277

52735278
tw.writeHasLocation(superCallId, locId)
52745279
tw.writeCallableBinding(superCallId.cast<DbCaller>(), baseConstructorId)
5280+
extractSuperconstructorArgs(superCallId)
52755281
}
52765282
}
52775283

@@ -5285,7 +5291,7 @@ open class KotlinFileExtractor(
52855291
}
52865292

52875293
/**
5288-
* Extracts the class around a local function or a lambda.
5294+
* Extracts the class around a local function or a lambda. The superclass must have a no-arg constructor.
52895295
*/
52905296
private fun extractGeneratedClass(localFunction: IrFunction, superTypes: List<IrType>) : Label<out DbClass> {
52915297
with("generated class", localFunction) {

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ reflection.kt:
124124
# 97| 1: [Constructor]
125125
# 97| 5: [BlockStmt] { ... }
126126
# 97| 0: [SuperConstructorInvocationStmt] super(...)
127+
# 97| 0: [IntegerLiteral] 1
127128
# 97| 2: [Method] invoke
128129
#-----| 4: (Parameters)
129130
# 97| 0: [Parameter] a0
@@ -148,6 +149,7 @@ reflection.kt:
148149
# 98| 1: [Constructor]
149150
# 98| 5: [BlockStmt] { ... }
150151
# 98| 0: [SuperConstructorInvocationStmt] super(...)
152+
# 98| 0: [IntegerLiteral] 1
151153
# 98| 2: [Method] invoke
152154
#-----| 4: (Parameters)
153155
# 98| 0: [Parameter] a0
@@ -175,6 +177,7 @@ reflection.kt:
175177
# 99| 0: [Parameter] <dispatchReceiver>
176178
# 99| 5: [BlockStmt] { ... }
177179
# 99| 0: [SuperConstructorInvocationStmt] super(...)
180+
# 99| 0: [IntegerLiteral] 1
178181
# 99| 1: [ExprStmt] <Expr>;
179182
# 99| 0: [AssignExpr] ...=...
180183
# 99| 0: [VarAccess] this.<dispatchReceiver>
@@ -274,6 +277,7 @@ reflection.kt:
274277
# 126| 1: [Constructor]
275278
# 126| 5: [BlockStmt] { ... }
276279
# 126| 0: [SuperConstructorInvocationStmt] super(...)
280+
# 126| 0: [IntegerLiteral] 0
277281
# 126| 2: [Method] invoke
278282
# 126| 5: [BlockStmt] { ... }
279283
# 126| 0: [ReturnStmt] return ...
@@ -296,6 +300,7 @@ reflection.kt:
296300
# 7| 1: [Constructor]
297301
# 7| 5: [BlockStmt] { ... }
298302
# 7| 0: [SuperConstructorInvocationStmt] super(...)
303+
# 7| 0: [IntegerLiteral] 2
299304
# 7| 2: [Method] invoke
300305
#-----| 4: (Parameters)
301306
# 7| 0: [Parameter] a0
@@ -362,6 +367,7 @@ reflection.kt:
362367
# 14| 0: [Parameter] <dispatchReceiver>
363368
# 14| 5: [BlockStmt] { ... }
364369
# 14| 0: [SuperConstructorInvocationStmt] super(...)
370+
# 14| 0: [IntegerLiteral] 1
365371
# 14| 1: [ExprStmt] <Expr>;
366372
# 14| 0: [AssignExpr] ...=...
367373
# 14| 0: [VarAccess] this.<dispatchReceiver>
@@ -473,6 +479,7 @@ reflection.kt:
473479
# 21| 0: [Parameter] <dispatchReceiver>
474480
# 21| 5: [BlockStmt] { ... }
475481
# 21| 0: [SuperConstructorInvocationStmt] super(...)
482+
# 21| 0: [IntegerLiteral] 2
476483
# 21| 1: [ExprStmt] <Expr>;
477484
# 21| 0: [AssignExpr] ...=...
478485
# 21| 0: [VarAccess] this.<dispatchReceiver>
@@ -678,6 +685,7 @@ reflection.kt:
678685
# 60| 1: [Constructor]
679686
# 60| 5: [BlockStmt] { ... }
680687
# 60| 0: [SuperConstructorInvocationStmt] super(...)
688+
# 60| 0: [IntegerLiteral] 2
681689
# 60| 2: [Method] invoke
682690
#-----| 4: (Parameters)
683691
# 60| 0: [Parameter] a0
@@ -702,6 +710,7 @@ reflection.kt:
702710
# 61| 0: [Parameter] <dispatchReceiver>
703711
# 61| 5: [BlockStmt] { ... }
704712
# 61| 0: [SuperConstructorInvocationStmt] super(...)
713+
# 61| 0: [IntegerLiteral] 1
705714
# 61| 1: [ExprStmt] <Expr>;
706715
# 61| 0: [AssignExpr] ...=...
707716
# 61| 0: [VarAccess] this.<dispatchReceiver>
@@ -733,6 +742,7 @@ reflection.kt:
733742
# 62| 1: [Constructor]
734743
# 62| 5: [BlockStmt] { ... }
735744
# 62| 0: [SuperConstructorInvocationStmt] super(...)
745+
# 62| 0: [IntegerLiteral] 1
736746
# 62| 2: [Method] invoke
737747
#-----| 4: (Parameters)
738748
# 62| 0: [Parameter] a0
@@ -756,6 +766,7 @@ reflection.kt:
756766
# 63| 0: [Parameter] <extensionReceiver>
757767
# 63| 5: [BlockStmt] { ... }
758768
# 63| 0: [SuperConstructorInvocationStmt] super(...)
769+
# 63| 0: [IntegerLiteral] 0
759770
# 63| 1: [ExprStmt] <Expr>;
760771
# 63| 0: [AssignExpr] ...=...
761772
# 63| 0: [VarAccess] this.<extensionReceiver>
@@ -785,6 +796,7 @@ reflection.kt:
785796
# 64| 1: [Constructor]
786797
# 64| 5: [BlockStmt] { ... }
787798
# 64| 0: [SuperConstructorInvocationStmt] super(...)
799+
# 64| 0: [IntegerLiteral] 1
788800
# 64| 2: [Method] invoke
789801
#-----| 4: (Parameters)
790802
# 64| 0: [Parameter] a0
@@ -807,6 +819,7 @@ reflection.kt:
807819
# 65| 0: [Parameter] <extensionReceiver>
808820
# 65| 5: [BlockStmt] { ... }
809821
# 65| 0: [SuperConstructorInvocationStmt] super(...)
822+
# 65| 0: [IntegerLiteral] 0
810823
# 65| 1: [ExprStmt] <Expr>;
811824
# 65| 0: [AssignExpr] ...=...
812825
# 65| 0: [VarAccess] this.<extensionReceiver>
@@ -1077,6 +1090,7 @@ reflection.kt:
10771090
# 90| 0: [Parameter] <dispatchReceiver>
10781091
# 90| 5: [BlockStmt] { ... }
10791092
# 90| 0: [SuperConstructorInvocationStmt] super(...)
1093+
# 90| 0: [IntegerLiteral] 1
10801094
# 90| 1: [ExprStmt] <Expr>;
10811095
# 90| 0: [AssignExpr] ...=...
10821096
# 90| 0: [VarAccess] this.<dispatchReceiver>
@@ -1210,6 +1224,7 @@ reflection.kt:
12101224
# 116| 1: [Constructor]
12111225
# 116| 5: [BlockStmt] { ... }
12121226
# 116| 0: [SuperConstructorInvocationStmt] super(...)
1227+
# 116| 0: [IntegerLiteral] 1
12131228
# 116| 2: [Method] invoke
12141229
#-----| 4: (Parameters)
12151230
# 116| 0: [Parameter] a0

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

Whitespace-only changes.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import java
2+
3+
from Call call, Callable callable, int argCount, int paramCount
4+
where
5+
call.getCallee() = callable and
6+
argCount = count(call.getAnArgument()) and
7+
paramCount = count(callable.getAParameter()) and
8+
argCount != paramCount
9+
select "Call should have " + paramCount + " arguments but actually has " + argCount, call, callable

0 commit comments

Comments
 (0)