Skip to content

Commit f1fd470

Browse files
authored
Merge pull request github#10821 from smowton/smowton/fix/kotlin-property-ref-to-sam-interface
Kotlin SAM conversion: tolerate property refs used to implement a SAM interface
2 parents 2b3ab18 + 1efcf38 commit f1fd470

File tree

5 files changed

+233
-1
lines changed

5 files changed

+233
-1
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5045,7 +5045,10 @@ open class KotlinFileExtractor(
50455045
return
50465046
}
50475047

5048-
if (!st.isFunctionOrKFunction() && !st.isSuspendFunctionOrKFunction()) {
5048+
fun IrSimpleType.isKProperty() =
5049+
classFqName?.asString()?.startsWith("kotlin.reflect.KProperty") == true
5050+
5051+
if (!st.isFunctionOrKFunction() && !st.isSuspendFunctionOrKFunction() && !st.isKProperty()) {
50495052
logger.errorElement("Expected to find expression with function type in SAM conversion.", e)
50505053
return
50515054
}

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

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6432,6 +6432,120 @@ samConversion.kt:
64326432
# 59| -1: [VarAccess] i0
64336433
# 59| 0: [IntegerLiteral] 1
64346434
# 59| 1: [IntegerLiteral] 2
6435+
# 74| 6: [Method] propertyRefsTest
6436+
# 74| 3: [TypeAccess] Unit
6437+
#-----| 4: (Parameters)
6438+
# 74| 0: [Parameter] prt
6439+
# 74| 0: [TypeAccess] PropertyRefsTest
6440+
# 74| 5: [BlockStmt] { ... }
6441+
# 75| 0: [LocalVariableDeclStmt] var ...;
6442+
# 75| 1: [LocalVariableDeclExpr] test1
6443+
# 75| 0: [CastExpr] (...)...
6444+
# 75| 0: [TypeAccess] IntGetter
6445+
# 75| 1: [ClassInstanceExpr] new (...)
6446+
# 75| -4: [AnonymousClass] new IntGetter(...) { ... }
6447+
# 75| 1: [Constructor]
6448+
#-----| 4: (Parameters)
6449+
# 75| 0: [Parameter] <fn>
6450+
# 75| 5: [BlockStmt] { ... }
6451+
# 75| 0: [SuperConstructorInvocationStmt] super(...)
6452+
# 75| 1: [ExprStmt] <Expr>;
6453+
# 75| 0: [AssignExpr] ...=...
6454+
# 75| 0: [VarAccess] this.<fn>
6455+
# 75| -1: [ThisAccess] this
6456+
# 75| 1: [VarAccess] <fn>
6457+
# 75| 2: [FieldDeclaration] Function0<Integer> <fn>;
6458+
# 75| -1: [TypeAccess] Function0<Integer>
6459+
# 75| 0: [TypeAccess] Integer
6460+
# 75| 3: [Method] f
6461+
# 75| 3: [TypeAccess] int
6462+
# 75| 5: [BlockStmt] { ... }
6463+
# 75| 0: [ReturnStmt] return ...
6464+
# 75| 0: [MethodAccess] invoke(...)
6465+
# 75| -1: [VarAccess] <fn>
6466+
# 75| -3: [TypeAccess] IntGetter
6467+
# 75| 0: [PropertyRefExpr] ...::...
6468+
# 75| -4: [AnonymousClass] new KProperty0<Integer>(...) { ... }
6469+
# 75| 1: [Constructor]
6470+
#-----| 4: (Parameters)
6471+
# 75| 0: [Parameter] <dispatchReceiver>
6472+
# 75| 5: [BlockStmt] { ... }
6473+
# 75| 0: [SuperConstructorInvocationStmt] super(...)
6474+
# 75| 1: [ExprStmt] <Expr>;
6475+
# 75| 0: [AssignExpr] ...=...
6476+
# 75| 0: [VarAccess] this.<dispatchReceiver>
6477+
# 75| -1: [ThisAccess] this
6478+
# 75| 1: [VarAccess] <dispatchReceiver>
6479+
# 75| 2: [FieldDeclaration] PropertyRefsTest <dispatchReceiver>;
6480+
# 75| -1: [TypeAccess] PropertyRefsTest
6481+
# 75| 3: [Method] get
6482+
# 75| 5: [BlockStmt] { ... }
6483+
# 75| 0: [ReturnStmt] return ...
6484+
# 75| 0: [MethodAccess] getX(...)
6485+
# 75| -1: [VarAccess] this.<dispatchReceiver>
6486+
# 75| -1: [ThisAccess] this
6487+
# 75| 4: [Method] invoke
6488+
# 75| 5: [BlockStmt] { ... }
6489+
# 75| 0: [ReturnStmt] return ...
6490+
# 75| 0: [MethodAccess] get(...)
6491+
# 75| -1: [ThisAccess] this
6492+
# 75| -3: [TypeAccess] KProperty0<Integer>
6493+
# 75| 0: [TypeAccess] Integer
6494+
# 75| 0: [VarAccess] prt
6495+
# 76| 1: [LocalVariableDeclStmt] var ...;
6496+
# 76| 1: [LocalVariableDeclExpr] test2
6497+
# 76| 0: [CastExpr] (...)...
6498+
# 76| 0: [TypeAccess] PropertyRefsGetter
6499+
# 76| 1: [ClassInstanceExpr] new (...)
6500+
# 76| -4: [AnonymousClass] new PropertyRefsGetter(...) { ... }
6501+
# 76| 1: [Constructor]
6502+
#-----| 4: (Parameters)
6503+
# 76| 0: [Parameter] <fn>
6504+
# 76| 5: [BlockStmt] { ... }
6505+
# 76| 0: [SuperConstructorInvocationStmt] super(...)
6506+
# 76| 1: [ExprStmt] <Expr>;
6507+
# 76| 0: [AssignExpr] ...=...
6508+
# 76| 0: [VarAccess] this.<fn>
6509+
# 76| -1: [ThisAccess] this
6510+
# 76| 1: [VarAccess] <fn>
6511+
# 76| 2: [FieldDeclaration] Function1<PropertyRefsTest,Integer> <fn>;
6512+
# 76| -1: [TypeAccess] Function1<PropertyRefsTest,Integer>
6513+
# 76| 0: [TypeAccess] PropertyRefsTest
6514+
# 76| 1: [TypeAccess] Integer
6515+
# 76| 3: [Method] f
6516+
# 76| 3: [TypeAccess] int
6517+
#-----| 4: (Parameters)
6518+
# 76| 0: [Parameter] prt
6519+
# 76| 0: [TypeAccess] PropertyRefsTest
6520+
# 76| 5: [BlockStmt] { ... }
6521+
# 76| 0: [ReturnStmt] return ...
6522+
# 76| 0: [MethodAccess] invoke(...)
6523+
# 76| -1: [VarAccess] <fn>
6524+
# 76| 0: [VarAccess] prt
6525+
# 76| -3: [TypeAccess] PropertyRefsGetter
6526+
# 76| 0: [PropertyRefExpr] ...::...
6527+
# 76| -4: [AnonymousClass] new KProperty1<PropertyRefsTest,Integer>(...) { ... }
6528+
# 76| 1: [Constructor]
6529+
# 76| 5: [BlockStmt] { ... }
6530+
# 76| 0: [SuperConstructorInvocationStmt] super(...)
6531+
# 76| 2: [Method] get
6532+
#-----| 4: (Parameters)
6533+
# 76| 0: [Parameter] a0
6534+
# 76| 5: [BlockStmt] { ... }
6535+
# 76| 0: [ReturnStmt] return ...
6536+
# 76| 0: [MethodAccess] getX(...)
6537+
# 76| -1: [VarAccess] a0
6538+
# 76| 3: [Method] invoke
6539+
#-----| 4: (Parameters)
6540+
# 76| 0: [Parameter] a0
6541+
# 76| 5: [BlockStmt] { ... }
6542+
# 76| 0: [ReturnStmt] return ...
6543+
# 76| 0: [MethodAccess] get(...)
6544+
# 76| -1: [ThisAccess] this
6545+
# 76| 0: [VarAccess] a0
6546+
# 76| -3: [TypeAccess] KProperty1<PropertyRefsTest,Integer>
6547+
# 76| 0: [TypeAccess] PropertyRefsTest
6548+
# 76| 1: [TypeAccess] Integer
64356549
# 16| 2: [Interface] IntPredicate
64366550
# 17| 1: [Method] accept
64376551
# 17| 3: [TypeAccess] boolean
@@ -6520,6 +6634,32 @@ samConversion.kt:
65206634
# 54| 0: [TypeAccess] int
65216635
# 54| 1: [Parameter] j
65226636
# 54| 0: [TypeAccess] int
6637+
# 62| 8: [Class] PropertyRefsTest
6638+
# 62| 1: [Constructor] PropertyRefsTest
6639+
# 62| 5: [BlockStmt] { ... }
6640+
# 62| 0: [SuperConstructorInvocationStmt] super(...)
6641+
# 62| 1: [BlockStmt] { ... }
6642+
# 63| 0: [ExprStmt] <Expr>;
6643+
# 63| 0: [KtInitializerAssignExpr] ...=...
6644+
# 63| 0: [VarAccess] x
6645+
# 63| 2: [Method] getX
6646+
# 63| 3: [TypeAccess] int
6647+
# 63| 5: [BlockStmt] { ... }
6648+
# 63| 0: [ReturnStmt] return ...
6649+
# 63| 0: [VarAccess] this.x
6650+
# 63| -1: [ThisAccess] this
6651+
# 63| 3: [FieldDeclaration] int x;
6652+
# 63| -1: [TypeAccess] int
6653+
# 63| 0: [IntegerLiteral] 1
6654+
# 66| 9: [Interface] PropertyRefsGetter
6655+
# 67| 1: [Method] f
6656+
# 67| 3: [TypeAccess] int
6657+
#-----| 4: (Parameters)
6658+
# 67| 0: [Parameter] prt
6659+
# 67| 0: [TypeAccess] PropertyRefsTest
6660+
# 70| 10: [Interface] IntGetter
6661+
# 71| 1: [Method] f
6662+
# 71| 3: [TypeAccess] int
65236663
whenExpr.kt:
65246664
# 0| [CompilationUnit] whenExpr
65256665
# 0| 1: [Class] WhenExprKt

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

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4028,6 +4028,72 @@
40284028
| samConversion.kt:59:8:59:15 | fn1(...) | samConversion.kt:57:9:60:1 | test | MethodAccess |
40294029
| samConversion.kt:59:12:59:12 | 1 | samConversion.kt:57:9:60:1 | test | IntegerLiteral |
40304030
| samConversion.kt:59:14:59:14 | 2 | samConversion.kt:57:9:60:1 | test | IntegerLiteral |
4031+
| samConversion.kt:63:5:63:13 | ...=... | samConversion.kt:62:1:64:1 | PropertyRefsTest | KtInitializerAssignExpr |
4032+
| samConversion.kt:63:5:63:13 | int | file://:0:0:0:0 | <none> | TypeAccess |
4033+
| samConversion.kt:63:5:63:13 | int | file://:0:0:0:0 | <none> | TypeAccess |
4034+
| samConversion.kt:63:5:63:13 | this | samConversion.kt:63:5:63:13 | getX | ThisAccess |
4035+
| samConversion.kt:63:5:63:13 | this.x | samConversion.kt:63:5:63:13 | getX | VarAccess |
4036+
| samConversion.kt:63:5:63:13 | x | samConversion.kt:62:1:64:1 | PropertyRefsTest | VarAccess |
4037+
| samConversion.kt:63:13:63:13 | 1 | samConversion.kt:62:1:64:1 | PropertyRefsTest | IntegerLiteral |
4038+
| samConversion.kt:67:5:67:37 | int | file://:0:0:0:0 | <none> | TypeAccess |
4039+
| samConversion.kt:67:11:67:31 | PropertyRefsTest | file://:0:0:0:0 | <none> | TypeAccess |
4040+
| samConversion.kt:71:5:71:16 | int | file://:0:0:0:0 | <none> | TypeAccess |
4041+
| samConversion.kt:74:1:77:1 | Unit | file://:0:0:0:0 | <none> | TypeAccess |
4042+
| samConversion.kt:74:22:74:42 | PropertyRefsTest | file://:0:0:0:0 | <none> | TypeAccess |
4043+
| samConversion.kt:75:5:75:33 | test1 | samConversion.kt:74:1:77:1 | propertyRefsTest | LocalVariableDeclExpr |
4044+
| samConversion.kt:75:17:75:33 | (...)... | samConversion.kt:74:1:77:1 | propertyRefsTest | CastExpr |
4045+
| samConversion.kt:75:17:75:33 | ...=... | samConversion.kt:75:17:75:33 | | AssignExpr |
4046+
| samConversion.kt:75:17:75:33 | <fn> | samConversion.kt:75:17:75:33 | | VarAccess |
4047+
| samConversion.kt:75:17:75:33 | <fn> | samConversion.kt:75:17:75:33 | f | VarAccess |
4048+
| samConversion.kt:75:17:75:33 | Function0<Integer> | file://:0:0:0:0 | <none> | TypeAccess |
4049+
| samConversion.kt:75:17:75:33 | IntGetter | samConversion.kt:74:1:77:1 | propertyRefsTest | TypeAccess |
4050+
| samConversion.kt:75:17:75:33 | IntGetter | samConversion.kt:74:1:77:1 | propertyRefsTest | TypeAccess |
4051+
| samConversion.kt:75:17:75:33 | Integer | file://:0:0:0:0 | <none> | TypeAccess |
4052+
| samConversion.kt:75:17:75:33 | int | file://:0:0:0:0 | <none> | TypeAccess |
4053+
| samConversion.kt:75:17:75:33 | invoke(...) | samConversion.kt:75:17:75:33 | f | MethodAccess |
4054+
| samConversion.kt:75:17:75:33 | new (...) | samConversion.kt:74:1:77:1 | propertyRefsTest | ClassInstanceExpr |
4055+
| samConversion.kt:75:17:75:33 | this | samConversion.kt:75:17:75:33 | | ThisAccess |
4056+
| samConversion.kt:75:17:75:33 | this.<fn> | samConversion.kt:75:17:75:33 | | VarAccess |
4057+
| samConversion.kt:75:27:75:29 | prt | samConversion.kt:74:1:77:1 | propertyRefsTest | VarAccess |
4058+
| samConversion.kt:75:27:75:32 | ...::... | samConversion.kt:74:1:77:1 | propertyRefsTest | PropertyRefExpr |
4059+
| samConversion.kt:75:27:75:32 | ...=... | samConversion.kt:75:27:75:32 | | AssignExpr |
4060+
| samConversion.kt:75:27:75:32 | <dispatchReceiver> | samConversion.kt:75:27:75:32 | | VarAccess |
4061+
| samConversion.kt:75:27:75:32 | Integer | samConversion.kt:74:1:77:1 | propertyRefsTest | TypeAccess |
4062+
| samConversion.kt:75:27:75:32 | KProperty0<Integer> | samConversion.kt:74:1:77:1 | propertyRefsTest | TypeAccess |
4063+
| samConversion.kt:75:27:75:32 | PropertyRefsTest | file://:0:0:0:0 | <none> | TypeAccess |
4064+
| samConversion.kt:75:27:75:32 | get(...) | samConversion.kt:75:27:75:32 | invoke | MethodAccess |
4065+
| samConversion.kt:75:27:75:32 | getX(...) | samConversion.kt:75:27:75:32 | get | MethodAccess |
4066+
| samConversion.kt:75:27:75:32 | this | samConversion.kt:75:27:75:32 | | ThisAccess |
4067+
| samConversion.kt:75:27:75:32 | this | samConversion.kt:75:27:75:32 | get | ThisAccess |
4068+
| samConversion.kt:75:27:75:32 | this | samConversion.kt:75:27:75:32 | invoke | ThisAccess |
4069+
| samConversion.kt:75:27:75:32 | this.<dispatchReceiver> | samConversion.kt:75:27:75:32 | | VarAccess |
4070+
| samConversion.kt:75:27:75:32 | this.<dispatchReceiver> | samConversion.kt:75:27:75:32 | get | VarAccess |
4071+
| samConversion.kt:76:5:76:55 | test2 | samConversion.kt:74:1:77:1 | propertyRefsTest | LocalVariableDeclExpr |
4072+
| samConversion.kt:76:17:76:55 | (...)... | samConversion.kt:74:1:77:1 | propertyRefsTest | CastExpr |
4073+
| samConversion.kt:76:17:76:55 | ...=... | samConversion.kt:76:17:76:55 | | AssignExpr |
4074+
| samConversion.kt:76:17:76:55 | <fn> | samConversion.kt:76:17:76:55 | | VarAccess |
4075+
| samConversion.kt:76:17:76:55 | <fn> | samConversion.kt:76:17:76:55 | f | VarAccess |
4076+
| samConversion.kt:76:17:76:55 | Function1<PropertyRefsTest,Integer> | file://:0:0:0:0 | <none> | TypeAccess |
4077+
| samConversion.kt:76:17:76:55 | Integer | file://:0:0:0:0 | <none> | TypeAccess |
4078+
| samConversion.kt:76:17:76:55 | PropertyRefsGetter | samConversion.kt:74:1:77:1 | propertyRefsTest | TypeAccess |
4079+
| samConversion.kt:76:17:76:55 | PropertyRefsGetter | samConversion.kt:74:1:77:1 | propertyRefsTest | TypeAccess |
4080+
| samConversion.kt:76:17:76:55 | PropertyRefsTest | file://:0:0:0:0 | <none> | TypeAccess |
4081+
| samConversion.kt:76:17:76:55 | PropertyRefsTest | file://:0:0:0:0 | <none> | TypeAccess |
4082+
| samConversion.kt:76:17:76:55 | int | file://:0:0:0:0 | <none> | TypeAccess |
4083+
| samConversion.kt:76:17:76:55 | invoke(...) | samConversion.kt:76:17:76:55 | f | MethodAccess |
4084+
| samConversion.kt:76:17:76:55 | new (...) | samConversion.kt:74:1:77:1 | propertyRefsTest | ClassInstanceExpr |
4085+
| samConversion.kt:76:17:76:55 | prt | samConversion.kt:76:17:76:55 | f | VarAccess |
4086+
| samConversion.kt:76:17:76:55 | this | samConversion.kt:76:17:76:55 | | ThisAccess |
4087+
| samConversion.kt:76:17:76:55 | this.<fn> | samConversion.kt:76:17:76:55 | | VarAccess |
4088+
| samConversion.kt:76:36:76:54 | ...::... | samConversion.kt:74:1:77:1 | propertyRefsTest | PropertyRefExpr |
4089+
| samConversion.kt:76:36:76:54 | Integer | samConversion.kt:74:1:77:1 | propertyRefsTest | TypeAccess |
4090+
| samConversion.kt:76:36:76:54 | KProperty1<PropertyRefsTest,Integer> | samConversion.kt:74:1:77:1 | propertyRefsTest | TypeAccess |
4091+
| samConversion.kt:76:36:76:54 | PropertyRefsTest | samConversion.kt:74:1:77:1 | propertyRefsTest | TypeAccess |
4092+
| samConversion.kt:76:36:76:54 | a0 | samConversion.kt:76:36:76:54 | get | VarAccess |
4093+
| samConversion.kt:76:36:76:54 | a0 | samConversion.kt:76:36:76:54 | invoke | VarAccess |
4094+
| samConversion.kt:76:36:76:54 | get(...) | samConversion.kt:76:36:76:54 | invoke | MethodAccess |
4095+
| samConversion.kt:76:36:76:54 | getX(...) | samConversion.kt:76:36:76:54 | get | MethodAccess |
4096+
| samConversion.kt:76:36:76:54 | this | samConversion.kt:76:36:76:54 | invoke | ThisAccess |
40314097
| whenExpr.kt:1:1:9:1 | int | file://:0:0:0:0 | <none> | TypeAccess |
40324098
| whenExpr.kt:1:14:1:19 | int | file://:0:0:0:0 | <none> | TypeAccess |
40334099
| whenExpr.kt:2:10:8:3 | <Stmt> | whenExpr.kt:1:1:9:1 | testWhen | StmtExpr |

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,12 @@ anon_class_member_modifiers
241241
| samConversion.kt:46:32:46:44 | new Function1<Integer,Boolean>(...) { ... } | samConversion.kt:46:32:46:44 | invoke | override, public |
242242
| samConversion.kt:58:14:58:45 | new InterfaceFn1Sus(...) { ... } | samConversion.kt:58:14:58:45 | fn1 | override, public, suspend |
243243
| samConversion.kt:58:30:58:45 | new Function2<Integer,Integer,Unit>(...) { ... } | samConversion.kt:58:30:58:45 | invoke | override, public, suspend |
244+
| samConversion.kt:75:17:75:33 | new IntGetter(...) { ... } | samConversion.kt:75:17:75:33 | f | override, public |
245+
| samConversion.kt:75:27:75:32 | new KProperty0<Integer>(...) { ... } | samConversion.kt:75:27:75:32 | get | override, public |
246+
| samConversion.kt:75:27:75:32 | new KProperty0<Integer>(...) { ... } | samConversion.kt:75:27:75:32 | invoke | override, public |
247+
| samConversion.kt:76:17:76:55 | new PropertyRefsGetter(...) { ... } | samConversion.kt:76:17:76:55 | f | override, public |
248+
| samConversion.kt:76:36:76:54 | new KProperty1<PropertyRefsTest,Integer>(...) { ... } | samConversion.kt:76:36:76:54 | get | override, public |
249+
| samConversion.kt:76:36:76:54 | new KProperty1<PropertyRefsTest,Integer>(...) { ... } | samConversion.kt:76:36:76:54 | invoke | override, public |
244250
nonOverrideInvoke
245251
| funcExprs.kt:36:29:36:117 | ...->... | funcExprs.kt:36:29:36:117 | invoke | 23 |
246252
| funcExprs.kt:90:15:90:69 | ...->... | funcExprs.kt:90:15:90:69 | invoke | 23 |

java/ql/test/kotlin/library-tests/exprs/samConversion.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,20 @@ suspend fun test() {
5858
val i0 = InterfaceFn1Sus { a, b -> Unit }
5959
i0.fn1(1,2)
6060
}
61+
62+
class PropertyRefsTest {
63+
val x = 1
64+
}
65+
66+
fun interface PropertyRefsGetter {
67+
fun f(prt: PropertyRefsTest): Int
68+
}
69+
70+
fun interface IntGetter {
71+
fun f(): Int
72+
}
73+
74+
fun propertyRefsTest(prt: PropertyRefsTest) {
75+
val test1 = IntGetter(prt::x)
76+
val test2 = PropertyRefsGetter(PropertyRefsTest::x)
77+
}

0 commit comments

Comments
 (0)