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

Skip to content

Commit a70ade2

Browse files
tamasvajkigfoo
authored andcommitted
Fix (generic) type access extraction for lambdas
1 parent a8c94c5 commit a70ade2

7 files changed

Lines changed: 193 additions & 2 deletions

File tree

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2478,7 +2478,8 @@ open class KotlinFileExtractor(
24782478
e.function, // We're adding this function as a member, and changing its name to `invoke` to implement `kotlin.FunctionX<,,,>.invoke(,,)`
24792479
listOf(pluginContext.irBuiltIns.anyType, fnInterfaceType))
24802480

2481-
if (types.size > BuiltInFunctionArity.BIG_ARITY) {
2481+
val isBigArity = types.size > BuiltInFunctionArity.BIG_ARITY
2482+
if (isBigArity) {
24822483
implementFunctionNInvoke(e.function, ids, locId, parameters)
24832484
}
24842485

@@ -2491,7 +2492,9 @@ open class KotlinFileExtractor(
24912492
tw.writeStatementEnclosingExpr(idLambdaExpr, exprParent.enclosingStmt)
24922493
tw.writeCallableBinding(idLambdaExpr, ids.constructor)
24932494

2494-
extractTypeAccess(fnInterfaceType, callable, idLambdaExpr, -3, e, exprParent.enclosingStmt)
2495+
val typeAccessId = extractTypeAccess(fnInterfaceType, callable, idLambdaExpr, -3, e, exprParent.enclosingStmt)
2496+
val typeAccessArguments = if (isBigArity) listOf(types.last()) else types
2497+
extractTypeArguments(typeAccessArguments, e, typeAccessId, callable, exprParent.enclosingStmt)
24952498

24962499
// todo: fix hard coded block body of lambda
24972500
tw.writeLambdaKind(idLambdaExpr, 1)

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,9 @@ local_anonymous.kt:
441441
# 16| 0: [VarAccess] a
442442
# 16| 1: [VarAccess] b
443443
# 16| -3: [TypeAccess] Function2<Integer,Integer,Integer>
444+
# 16| 0: [TypeAccess] Integer
445+
# 16| 1: [TypeAccess] Integer
446+
# 16| 2: [TypeAccess] Integer
444447
# 17| 1: [LocalVariableDeclStmt] var ...;
445448
# 17| 1: [LocalVariableDeclExpr] lambda2
446449
# 17| 0: [LambdaExpr] ...->...
@@ -458,6 +461,9 @@ local_anonymous.kt:
458461
# 17| 0: [VarAccess] a
459462
# 17| 1: [VarAccess] b
460463
# 17| -3: [TypeAccess] Function2<Integer,Integer,Integer>
464+
# 17| 0: [TypeAccess] Integer
465+
# 17| 1: [TypeAccess] Integer
466+
# 17| 2: [TypeAccess] Integer
461467
# 20| 5: [Method] fn4
462468
# 20| 5: [BlockStmt] { ... }
463469
# 21| 0: [LocalVariableDeclStmt] var ...;

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

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,6 +1163,7 @@ funcExprs.kt:
11631163
# 16| 0: [ReturnStmt] return ...
11641164
# 16| 0: [IntegerLiteral] 5
11651165
# 16| -3: [TypeAccess] Function0<Integer>
1166+
# 16| 0: [TypeAccess] Integer
11661167
# 17| 1: [ExprStmt] <Expr>;
11671168
# 17| 0: [MethodAccess] functionExpression0b(...)
11681169
# 17| 0: [LambdaExpr] ...->...
@@ -1175,6 +1176,7 @@ funcExprs.kt:
11751176
# 17| 0: [ReturnStmt] return ...
11761177
# 17| 0: [IntegerLiteral] 5
11771178
# 17| -3: [TypeAccess] Function0<Object>
1179+
# 17| 0: [TypeAccess] Object
11781180
# 18| 2: [ExprStmt] <Expr>;
11791181
# 18| 0: [MethodAccess] functionExpression0c(...)
11801182
# 18| 0: [LambdaExpr] ...->...
@@ -1187,6 +1189,7 @@ funcExprs.kt:
11871189
# 18| 0: [ReturnStmt] return ...
11881190
# 18| 0: [IntegerLiteral] 5
11891191
# 18| -3: [TypeAccess] Function0<Object>
1192+
# 18| 0: [TypeAccess] Object
11901193
# 19| 3: [ExprStmt] <Expr>;
11911194
# 19| 0: [MethodAccess] functionExpression1a(...)
11921195
# 19| 0: [IntegerLiteral] 5
@@ -1202,6 +1205,8 @@ funcExprs.kt:
12021205
# 19| 0: [ReturnStmt] return ...
12031206
# 19| 0: [IntegerLiteral] 5
12041207
# 19| -3: [TypeAccess] Function1<Integer,Integer>
1208+
# 19| 0: [TypeAccess] Integer
1209+
# 19| 1: [TypeAccess] Integer
12051210
# 20| 4: [ExprStmt] <Expr>;
12061211
# 20| 0: [MethodAccess] functionExpression1a(...)
12071212
# 20| 0: [IntegerLiteral] 5
@@ -1217,6 +1222,8 @@ funcExprs.kt:
12171222
# 20| 0: [ReturnStmt] return ...
12181223
# 20| 0: [VarAccess] it
12191224
# 20| -3: [TypeAccess] Function1<Integer,Integer>
1225+
# 20| 0: [TypeAccess] Integer
1226+
# 20| 1: [TypeAccess] Integer
12201227
# 21| 5: [ExprStmt] <Expr>;
12211228
# 21| 0: [MethodAccess] functionExpression1a(...)
12221229
# 21| 0: [IntegerLiteral] 5
@@ -1232,6 +1239,8 @@ funcExprs.kt:
12321239
# 21| 0: [ReturnStmt] return ...
12331240
# 21| 0: [IntegerLiteral] 5
12341241
# 21| -3: [TypeAccess] Function1<Integer,Integer>
1242+
# 21| 0: [TypeAccess] Integer
1243+
# 21| 1: [TypeAccess] Integer
12351244
# 22| 6: [ExprStmt] <Expr>;
12361245
# 22| 0: [MethodAccess] functionExpression1a(...)
12371246
# 22| 0: [IntegerLiteral] 5
@@ -1252,6 +1261,8 @@ funcExprs.kt:
12521261
# 23| 0: [ReturnStmt] return ...
12531262
# 23| 0: [VarAccess] a
12541263
# 23| -3: [TypeAccess] Function1<Object,Object>
1264+
# 23| 0: [TypeAccess] Object
1265+
# 23| 1: [TypeAccess] Object
12551266
# 24| 8: [ExprStmt] <Expr>;
12561267
# 24| 0: [MethodAccess] functionExpression2(...)
12571268
# 24| 0: [IntegerLiteral] 5
@@ -1268,6 +1279,9 @@ funcExprs.kt:
12681279
# 24| 0: [ReturnStmt] return ...
12691280
# 24| 0: [IntegerLiteral] 5
12701281
# 24| -3: [TypeAccess] Function2<Integer,Integer,Integer>
1282+
# 24| 0: [TypeAccess] Integer
1283+
# 24| 1: [TypeAccess] Integer
1284+
# 24| 2: [TypeAccess] Integer
12711285
# 25| 9: [ExprStmt] <Expr>;
12721286
# 25| 0: [MethodAccess] functionExpression2(...)
12731287
# 25| 0: [IntegerLiteral] 5
@@ -1284,6 +1298,9 @@ funcExprs.kt:
12841298
# 25| 0: [ReturnStmt] return ...
12851299
# 25| 0: [IntegerLiteral] 5
12861300
# 25| -3: [TypeAccess] Function2<Integer,Integer,Integer>
1301+
# 25| 0: [TypeAccess] Integer
1302+
# 25| 1: [TypeAccess] Integer
1303+
# 25| 2: [TypeAccess] Integer
12871304
# 26| 10: [ExprStmt] <Expr>;
12881305
# 26| 0: [MethodAccess] functionExpression3(...)
12891306
# 26| 0: [IntegerLiteral] 5
@@ -1302,6 +1319,9 @@ funcExprs.kt:
13021319
# 26| 0: [ThisAccess] this
13031320
# 26| 1: [VarAccess] a
13041321
# 26| -3: [TypeAccess] Function2<Integer,Integer,Integer>
1322+
# 26| 0: [TypeAccess] Integer
1323+
# 26| 1: [TypeAccess] Integer
1324+
# 26| 2: [TypeAccess] Integer
13051325
# 27| 11: [ExprStmt] <Expr>;
13061326
# 27| 0: [MethodAccess] functionExpression4(...)
13071327
# 27| 0: [IntegerLiteral] 5
@@ -1327,7 +1347,13 @@ funcExprs.kt:
13271347
# 27| 0: [ReturnStmt] return ...
13281348
# 27| 0: [DoubleLiteral] 5.0
13291349
# 27| -3: [TypeAccess] Function1<Integer,Double>
1350+
# 27| 0: [TypeAccess] Integer
1351+
# 27| 1: [TypeAccess] Double
13301352
# 27| -3: [TypeAccess] Function1<Integer,Function1<Integer,Double>>
1353+
# 27| 0: [TypeAccess] Integer
1354+
# 27| 1: [TypeAccess] Function1<Integer,Double>
1355+
# 27| 0: [TypeAccess] Integer
1356+
# 27| 1: [TypeAccess] Double
13311357
# 29| 12: [ExprStmt] <Expr>;
13321358
# 29| 0: [MethodAccess] functionExpression22(...)
13331359
# 29| 0: [IntegerLiteral] 5
@@ -1366,6 +1392,29 @@ funcExprs.kt:
13661392
# 29| 0: [TypeAccess] Unit
13671393
# 29| 1: [IntegerLiteral] 5
13681394
# 29| -3: [TypeAccess] Function22<Integer,Integer,Integer,Integer,Integer,Integer,Integer,Integer,Integer,Integer,Integer,Integer,Integer,Integer,Integer,Integer,Integer,Integer,Integer,Integer,Integer,Integer,Unit>
1395+
# 29| 0: [TypeAccess] Integer
1396+
# 29| 1: [TypeAccess] Integer
1397+
# 29| 2: [TypeAccess] Integer
1398+
# 29| 3: [TypeAccess] Integer
1399+
# 29| 4: [TypeAccess] Integer
1400+
# 29| 5: [TypeAccess] Integer
1401+
# 29| 6: [TypeAccess] Integer
1402+
# 29| 7: [TypeAccess] Integer
1403+
# 29| 8: [TypeAccess] Integer
1404+
# 29| 9: [TypeAccess] Integer
1405+
# 29| 10: [TypeAccess] Integer
1406+
# 29| 11: [TypeAccess] Integer
1407+
# 29| 12: [TypeAccess] Integer
1408+
# 29| 13: [TypeAccess] Integer
1409+
# 29| 14: [TypeAccess] Integer
1410+
# 29| 15: [TypeAccess] Integer
1411+
# 29| 16: [TypeAccess] Integer
1412+
# 29| 17: [TypeAccess] Integer
1413+
# 29| 18: [TypeAccess] Integer
1414+
# 29| 19: [TypeAccess] Integer
1415+
# 29| 20: [TypeAccess] Integer
1416+
# 29| 21: [TypeAccess] Integer
1417+
# 29| 22: [TypeAccess] Unit
13691418
# 30| 13: [ExprStmt] <Expr>;
13701419
# 30| 0: [MethodAccess] functionExpression23(...)
13711420
# 30| 0: [IntegerLiteral] 5
@@ -1525,6 +1574,7 @@ funcExprs.kt:
15251574
# 30| 0: [VarAccess] a0
15261575
# 30| 1: [IntegerLiteral] 22
15271576
# 30| -3: [TypeAccess] FunctionN<String>
1577+
# 30| 0: [TypeAccess] String
15281578
# 32| 14: [ExprStmt] <Expr>;
15291579
# 32| 0: [MethodAccess] functionExpression0a(...)
15301580
# 32| 0: [MemberRefExpr] ...::...
@@ -2188,6 +2238,41 @@ funcExprs.kt:
21882238
# 64| 5: [BlockStmt] { ... }
21892239
# 64| 0: [ReturnStmt] return ...
21902240
# 64| 0: [StringLiteral]
2241+
# 67| 4: [Class] Class3
2242+
# 67| 1: [Constructor] Class3
2243+
# 67| 5: [BlockStmt] { ... }
2244+
# 67| 0: [SuperConstructorInvocationStmt] super(...)
2245+
# 68| 2: [Method] call
2246+
# 68| 5: [BlockStmt] { ... }
2247+
# 69| 0: [ExprStmt] <Expr>;
2248+
# 69| 0: [MethodAccess] fn(...)
2249+
# 69| -1: [ThisAccess] this
2250+
# 69| 0: [LambdaExpr] ...->...
2251+
# 69| -4: [AnonymousClass] new Function1<Generic<Generic<Integer>>,String>(...) { ... }
2252+
# 69| 1: [Constructor]
2253+
# 69| 5: [BlockStmt] { ... }
2254+
# 69| 0: [SuperConstructorInvocationStmt] super(...)
2255+
# 69| 1: [Method] invoke
2256+
#-----| 4: (Parameters)
2257+
# 69| 0: [Parameter] a
2258+
# 69| 5: [BlockStmt] { ... }
2259+
# 69| 0: [ReturnStmt] return ...
2260+
# 69| 0: [StringLiteral] a
2261+
# 69| -3: [TypeAccess] Function1<Generic<Generic<Integer>>,String>
2262+
# 69| 0: [TypeAccess] Generic<Generic<Integer>>
2263+
# 69| 0: [TypeAccess] Generic<Integer>
2264+
# 69| 0: [TypeAccess] Integer
2265+
# 69| 1: [TypeAccess] String
2266+
# 71| 3: [Method] fn
2267+
#-----| 4: (Parameters)
2268+
# 71| 0: [Parameter] f
2269+
# 71| 5: [BlockStmt] { ... }
2270+
# 73| 4: [Class,GenericType,ParameterizedType] Generic
2271+
#-----| -2: (Generic Parameters)
2272+
# 73| 0: [TypeVariable] T
2273+
# 73| 1: [Constructor] Generic
2274+
# 73| 5: [BlockStmt] { ... }
2275+
# 73| 0: [SuperConstructorInvocationStmt] super(...)
21912276
localFunctionCalls.kt:
21922277
# 0| [CompilationUnit] localFunctionCalls
21932278
# 0| 1: [Class] LocalFunctionCallsKt
@@ -2308,6 +2393,8 @@ samConversion.kt:
23082393
# 2| 1: [IntegerLiteral] 2
23092394
# 2| 1: [IntegerLiteral] 0
23102395
# 2| -3: [TypeAccess] Function1<Integer,Boolean>
2396+
# 2| 0: [TypeAccess] Integer
2397+
# 2| 1: [TypeAccess] Boolean
23112398
# 4| 1: [LocalVariableDeclStmt] var ...;
23122399
# 4| 1: [LocalVariableDeclExpr] i0
23132400
# 4| 0: [CastExpr] (...)...
@@ -2350,6 +2437,9 @@ samConversion.kt:
23502437
# 4| 0: [ExprStmt] <Expr>;
23512438
# 4| 0: [VarAccess] INSTANCE
23522439
# 4| -3: [TypeAccess] Function2<Integer,Integer,Unit>
2440+
# 4| 0: [TypeAccess] Integer
2441+
# 4| 1: [TypeAccess] Integer
2442+
# 4| 2: [TypeAccess] Unit
23532443
# 5| 2: [LocalVariableDeclStmt] var ...;
23542444
# 5| 1: [LocalVariableDeclExpr] i1
23552445
# 5| 0: [CastExpr] (...)...
@@ -2441,6 +2531,9 @@ samConversion.kt:
24412531
# 7| 0: [ThisAccess] this
24422532
# 7| 1: [StringLiteral]
24432533
# 7| -3: [TypeAccess] Function2<String,Integer,Boolean>
2534+
# 7| 0: [TypeAccess] String
2535+
# 7| 1: [TypeAccess] Integer
2536+
# 7| 2: [TypeAccess] Boolean
24442537
# 9| 4: [LocalVariableDeclStmt] var ...;
24452538
# 9| 1: [LocalVariableDeclExpr] x
24462539
# 9| 0: [CastExpr] (...)...
@@ -2488,6 +2581,8 @@ samConversion.kt:
24882581
# 10| 1: [IntegerLiteral] 2
24892582
# 10| 1: [IntegerLiteral] 0
24902583
# 9| -3: [TypeAccess] Function1<Integer,Boolean>
2584+
# 9| 0: [TypeAccess] Integer
2585+
# 9| 1: [TypeAccess] Boolean
24912586
#-----| 1: (branch)
24922587
# 9| 0: [BooleanLiteral] true
24932588
# 11| 1: [ExprStmt] <Expr>;
@@ -2507,6 +2602,8 @@ samConversion.kt:
25072602
# 12| 1: [IntegerLiteral] 2
25082603
# 12| 1: [IntegerLiteral] 1
25092604
# 11| -3: [TypeAccess] Function1<Integer,Boolean>
2605+
# 11| 0: [TypeAccess] Integer
2606+
# 11| 1: [TypeAccess] Boolean
25102607
# 20| 2: [Method] fn2
25112608
#-----| 4: (Parameters)
25122609
# 20| 0: [Parameter] i
@@ -2981,6 +3078,7 @@ samConversion.kt:
29813078
# 43| 0: [VarAccess] a0
29823079
# 43| 1: [IntegerLiteral] 22
29833080
# 43| -3: [TypeAccess] FunctionN<Boolean>
3081+
# 43| 0: [TypeAccess] Boolean
29843082
# 16| 2: [Interface] IntPredicate
29853083
# 17| 1: [Method] accept
29863084
#-----| 4: (Parameters)

0 commit comments

Comments
 (0)