Add property reference tests

This commit is contained in:
Tamas Vajk
2022-02-22 16:24:52 +01:00
committed by Ian Lynagh
parent f3b92e7549
commit d057530584
4 changed files with 279 additions and 17 deletions

View File

@@ -1,14 +1,35 @@
reflection.kt:
# 0| [CompilationUnit] reflection
# 3| 1: [Class] Reflection
# 0| 1: [Class] ReflectionKt
# 45| 1: [ExtensionMethod] getLastChar
#-----| 4: (Parameters)
# 44| 0: [Parameter] <this>
# 45| 5: [BlockStmt] { ... }
# 45| 0: [ReturnStmt] return ...
# 45| 0: [MethodAccess] get(...)
# 45| -1: [ThisAccess] this
# 45| 0: [SubExpr] ... - ...
# 45| 0: [MethodAccess] getLength(...)
# 45| -1: [ThisAccess] this
# 45| 1: [IntegerLiteral] 1
# 47| 2: [Method] fn2
# 47| 5: [BlockStmt] { ... }
# 48| 0: [ExprStmt] <Expr>;
# 48| 0: [MethodAccess] println(...)
# 48| -1: [TypeAccess] ConsoleKt
# 48| 0: [MethodAccess] get(...)
# 48| 0: [StringLiteral] abc
# 49| 1: [ExprStmt] <Expr>;
# 49| 0: [MethodAccess] println(...)
# 49| -1: [TypeAccess] ConsoleKt
# 49| 0: [MethodAccess] get(...)
# 3| 2: [Class] Reflection
# 3| 1: [Constructor] Reflection
# 3| 5: [BlockStmt] { ... }
# 3| 0: [SuperConstructorInvocationStmt] super(...)
# 3| 1: [ExprStmt] <Expr>;
# 3| 0: [MethodAccess] <obinit>(...)
# 4| 3: [Method] fn
#-----| 4: (Parameters)
# 4| 0: [Parameter] boo
# 4| 5: [BlockStmt] { ... }
# 5| 0: [LocalVariableDeclStmt] var ...;
# 5| 1: [LocalVariableDeclExpr] ref
@@ -32,15 +53,194 @@ reflection.kt:
# 6| -1: [TypeAccess] ConsoleKt
# 6| 0: [MethodAccess] getName(...)
# 6| -1: [VarAccess] ref
# 9| 4: [Class] Ccc
# 9| 1: [Constructor] Ccc
# 9| 5: [BlockStmt] { ... }
# 9| 0: [SuperConstructorInvocationStmt] super(...)
# 9| 1: [ExprStmt] <Expr>;
# 9| 0: [MethodAccess] <obinit>(...)
# 10| 3: [Method] m
# 8| 2: [LocalVariableDeclStmt] var ...;
# 8| 1: [LocalVariableDeclExpr] x0
# 9| 3: [LocalVariableDeclStmt] var ...;
# 9| 1: [LocalVariableDeclExpr] x1
# 9| 0: [MethodAccess] get(...)
# 9| -1: [VarAccess] x0
# 9| 0: [ClassInstanceExpr] new C(...)
# 9| -3: [TypeAccess] C
# 10| 4: [LocalVariableDeclStmt] var ...;
# 10| 1: [LocalVariableDeclExpr] x2
# 10| 0: [MethodAccess] getName(...)
# 10| -1: [VarAccess] x0
# 11| 5: [LocalVariableDeclStmt] var ...;
# 11| 1: [LocalVariableDeclExpr] x3
# 11| 0: [MethodAccess] getGetter(...)
# 11| -1: [VarAccess] x0
# 12| 6: [LocalVariableDeclStmt] var ...;
# 12| 1: [LocalVariableDeclExpr] x4
# 12| 0: [MemberRefExpr] ...::...
# 12| -4: [AnonymousClass] new Function1<T,V>(...) { ... }
# 12| 1: [Constructor]
#-----| 4: (Parameters)
# 12| 0: [Parameter] <dispatchReceiver>
# 12| 5: [BlockStmt] { ... }
# 12| 0: [SuperConstructorInvocationStmt] super(...)
# 12| 1: [ExprStmt] <Expr>;
# 12| 0: [AssignExpr] ...=...
# 12| 0: [VarAccess] this.<dispatchReceiver>
# 12| -1: [ThisAccess] this
# 12| 1: [VarAccess] <dispatchReceiver>
# 12| 1: [FieldDeclaration] KProperty1<C,Integer> <dispatchReceiver>;
# 12| -1: [TypeAccess] KProperty1<C,Integer>
# 12| 1: [Method] invoke
#-----| 4: (Parameters)
# 12| 0: [Parameter] a0
# 12| 5: [BlockStmt] { ... }
# 12| 0: [ReturnStmt] return ...
# 12| 0: [MethodAccess] get(...)
# 12| -1: [VarAccess] this.<dispatchReceiver>
# 12| -1: [ThisAccess] this
# 12| 0: [VarAccess] a0
# 12| -3: [TypeAccess] Function1<T,V>
# 12| 0: [VarAccess] x0
# 13| 7: [LocalVariableDeclStmt] var ...;
# 13| 1: [LocalVariableDeclExpr] x5
# 15| 8: [LocalVariableDeclStmt] var ...;
# 15| 1: [LocalVariableDeclExpr] y0
# 16| 9: [LocalVariableDeclStmt] var ...;
# 16| 1: [LocalVariableDeclExpr] y1
# 16| 0: [MethodAccess] set(...)
# 16| -1: [VarAccess] y0
# 16| 0: [ClassInstanceExpr] new C(...)
# 16| -3: [TypeAccess] C
# 16| 1: [IntegerLiteral] 5
# 17| 10: [LocalVariableDeclStmt] var ...;
# 17| 1: [LocalVariableDeclExpr] y2
# 17| 0: [MethodAccess] getName(...)
# 17| -1: [VarAccess] y0
# 18| 11: [LocalVariableDeclStmt] var ...;
# 18| 1: [LocalVariableDeclExpr] y3
# 18| 0: [MethodAccess] getSetter(...)
# 18| -1: [VarAccess] y0
# 19| 12: [LocalVariableDeclStmt] var ...;
# 19| 1: [LocalVariableDeclExpr] y4
# 19| 0: [MemberRefExpr] ...::...
# 19| -4: [AnonymousClass] new Function2<T,V,Unit>(...) { ... }
# 19| 1: [Constructor]
#-----| 4: (Parameters)
# 19| 0: [Parameter] <dispatchReceiver>
# 19| 5: [BlockStmt] { ... }
# 19| 0: [SuperConstructorInvocationStmt] super(...)
# 19| 1: [ExprStmt] <Expr>;
# 19| 0: [AssignExpr] ...=...
# 19| 0: [VarAccess] this.<dispatchReceiver>
# 19| -1: [ThisAccess] this
# 19| 1: [VarAccess] <dispatchReceiver>
# 19| 1: [FieldDeclaration] KMutableProperty1<C,Integer> <dispatchReceiver>;
# 19| -1: [TypeAccess] KMutableProperty1<C,Integer>
# 19| 1: [Method] invoke
#-----| 4: (Parameters)
# 19| 0: [Parameter] a0
# 19| 1: [Parameter] a1
# 19| 5: [BlockStmt] { ... }
# 19| 0: [ReturnStmt] return ...
# 19| 0: [MethodAccess] set(...)
# 19| -1: [VarAccess] this.<dispatchReceiver>
# 19| -1: [ThisAccess] this
# 19| 0: [VarAccess] a0
# 19| 1: [VarAccess] a1
# 19| -3: [TypeAccess] Function2<T,V,Unit>
# 19| 0: [VarAccess] y0
# 20| 13: [LocalVariableDeclStmt] var ...;
# 20| 1: [LocalVariableDeclExpr] y5
# 22| 14: [LocalVariableDeclStmt] var ...;
# 22| 1: [LocalVariableDeclExpr] prop
# 22| 0: [CastExpr] (...)...
# 22| 0: [TypeAccess] KProperty2<C,Integer,Integer>
# 22| 1: [MethodAccess] single(...)
# 22| -2: [TypeAccess] KCallable<?>
# 22| -1: [TypeAccess] CollectionsKt
# 22| 0: [MethodAccess] getMembers(...)
# 22| -1: [TypeLiteral] C.class
# 22| 0: [TypeAccess] C
# 22| 1: [LambdaExpr] ...->...
# 22| -4: [AnonymousClass] new Function1<KCallable<?>,Boolean>(...) { ... }
# 22| 1: [Constructor]
# 22| 5: [BlockStmt] { ... }
# 22| 0: [SuperConstructorInvocationStmt] super(...)
# 22| 1: [Method] invoke
#-----| 4: (Parameters)
# 22| 0: [Parameter] it
# 22| 5: [BlockStmt] { ... }
# 22| 0: [ReturnStmt] return ...
# 22| 0: [ValueEQExpr] ... (value equals) ...
# 22| 0: [MethodAccess] getName(...)
# 22| -1: [VarAccess] it
# 22| 1: [StringLiteral] p3
# 22| -3: [TypeAccess] Function1<KCallable<?>,Boolean>
# 23| 15: [LocalVariableDeclStmt] var ...;
# 23| 1: [LocalVariableDeclExpr] z0
# 23| 0: [MethodAccess] get(...)
# 23| -1: [VarAccess] prop
# 23| 0: [ClassInstanceExpr] new C(...)
# 23| -3: [TypeAccess] C
# 23| 1: [IntegerLiteral] 5
# 26| 4: [Class] Ccc
# 26| 1: [Constructor] Ccc
# 26| 5: [BlockStmt] { ... }
# 26| 0: [SuperConstructorInvocationStmt] super(...)
# 26| 1: [ExprStmt] <Expr>;
# 26| 0: [MethodAccess] <obinit>(...)
# 27| 3: [Method] m
#-----| 4: (Parameters)
# 10| 0: [Parameter] i
# 10| 5: [BlockStmt] { ... }
# 10| 0: [ReturnStmt] return ...
# 10| 0: [DoubleLiteral] 5.0
# 27| 0: [Parameter] i
# 27| 5: [BlockStmt] { ... }
# 27| 0: [ReturnStmt] return ...
# 27| 0: [DoubleLiteral] 5.0
# 30| 5: [Class] C
# 30| 1: [Constructor] C
# 30| 5: [BlockStmt] { ... }
# 30| 0: [SuperConstructorInvocationStmt] super(...)
# 30| 1: [ExprStmt] <Expr>;
# 30| 0: [MethodAccess] <obinit>(...)
# 31| 3: [Method] getP0
# 31| 5: [BlockStmt] { ... }
# 31| 0: [ReturnStmt] return ...
# 31| 0: [VarAccess] this.p0
# 31| -1: [ThisAccess] this
# 31| 3: [FieldDeclaration] int p0;
# 31| -1: [TypeAccess] int
# 31| 0: [IntegerLiteral] 1
# 32| 5: [Method] getP1
# 32| 5: [BlockStmt] { ... }
# 32| 0: [ReturnStmt] return ...
# 32| 0: [VarAccess] this.p1
# 32| -1: [ThisAccess] this
# 32| 5: [Method] setP1
#-----| 4: (Parameters)
# 32| 0: [Parameter] <set-?>
# 32| 5: [BlockStmt] { ... }
# 32| 0: [ExprStmt] <Expr>;
# 32| 0: [AssignExpr] ...=...
# 32| 0: [VarAccess] this.p1
# 32| -1: [ThisAccess] this
# 32| 1: [VarAccess] <set-?>
# 32| 5: [FieldDeclaration] int p1;
# 32| -1: [TypeAccess] int
# 32| 0: [IntegerLiteral] 2
# 34| 8: [Method] getP2
# 34| 5: [BlockStmt] { ... }
# 34| 0: [ReturnStmt] return ...
# 34| 0: [IntegerLiteral] 1
# 35| 9: [Method] setP2
#-----| 4: (Parameters)
# 35| 0: [Parameter] value
# 35| 5: [BlockStmt] { ... }
# 35| 0: [ReturnStmt] return ...
# 35| 0: [VarAccess] INSTANCE
# 38| 10: [ExtensionMethod] getP3
#-----| 4: (Parameters)
# 37| 0: [Parameter] <this>
# 38| 5: [BlockStmt] { ... }
# 38| 0: [ReturnStmt] return ...
# 38| 0: [IntegerLiteral] 1
# 39| 11: [ExtensionMethod] setP3
#-----| 4: (Parameters)
# 37| 0: [Parameter] <this>
# 39| 1: [Parameter] value
# 39| 5: [BlockStmt] { ... }
# 39| 0: [ReturnStmt] return ...
# 39| 0: [VarAccess] INSTANCE

View File

@@ -1,5 +1,28 @@
variableInitializerType
| reflection.kt:5:9:5:54 | KFunction<Double> ref | file://<external>/KFunction.class:0:0:0:0 | KFunction<Double> | reflection.kt:5:49:5:54 | new Function2<Ccc,Integer,Double>(...) { ... } | file://<external>/Function2.class:0:0:0:0 | Function2<Ccc,Integer,Double> | true |
| reflection.kt:5:9:5:54 | KFunction<Double> ref | file://<external>/KFunction.class:0:0:0:0 | KFunction<Double> | reflection.kt:5:49:5:54 | new Function2<Ccc,Integer,Double>(...) { ... } | file://<external>/FunctionReference.class:0:0:0:0 | FunctionReference | true |
| reflection.kt:11:9:11:53 | Getter<C,Integer> x3 | file://<external>/KProperty1$Getter.class:0:0:0:0 | Getter<C,Integer> | file://<external>/KProperty1$Getter.class:0:0:0:0 | Getter<C,Integer> | file://<external>/Function1.class:0:0:0:0 | Function1<C,Integer> | true |
| reflection.kt:11:9:11:53 | Getter<C,Integer> x3 | file://<external>/KProperty1$Getter.class:0:0:0:0 | Getter<C,Integer> | file://<external>/KProperty1$Getter.class:0:0:0:0 | Getter<C,Integer> | file://<external>/KProperty$Getter.class:0:0:0:0 | Getter<Integer> | true |
| reflection.kt:12:9:12:44 | KFunction<Integer> x4 | file://<external>/KFunction.class:0:0:0:0 | KFunction<Integer> | reflection.kt:12:38:12:44 | new Function1<T,V>(...) { ... } | file://<external>/Function1.class:0:0:0:0 | Function1<T,V> | true |
| reflection.kt:12:9:12:44 | KFunction<Integer> x4 | file://<external>/KFunction.class:0:0:0:0 | KFunction<Integer> | reflection.kt:12:38:12:44 | new Function1<T,V>(...) { ... } | file://<external>/FunctionReference.class:0:0:0:0 | FunctionReference | true |
| reflection.kt:18:9:18:60 | Setter<C,Integer> y3 | file://<external>/KMutableProperty1$Setter.class:0:0:0:0 | Setter<C,Integer> | file://<external>/KMutableProperty1$Setter.class:0:0:0:0 | Setter<C,Integer> | file://<external>/Function2.class:0:0:0:0 | Function2<C,Integer,Unit> | true |
| reflection.kt:18:9:18:60 | Setter<C,Integer> y3 | file://<external>/KMutableProperty1$Setter.class:0:0:0:0 | Setter<C,Integer> | file://<external>/KMutableProperty1$Setter.class:0:0:0:0 | Setter<C,Integer> | file://<external>/KMutableProperty$Setter.class:0:0:0:0 | Setter<Integer> | true |
| reflection.kt:19:9:19:50 | KFunction<Unit> y4 | file://<external>/KFunction.class:0:0:0:0 | KFunction<Unit> | reflection.kt:19:44:19:50 | new Function2<T,V,Unit>(...) { ... } | file://<external>/Function2.class:0:0:0:0 | Function2<T,V,Unit> | true |
| reflection.kt:19:9:19:50 | KFunction<Unit> y4 | file://<external>/KFunction.class:0:0:0:0 | KFunction<Unit> | reflection.kt:19:44:19:50 | new Function2<T,V,Unit>(...) { ... } | file://<external>/FunctionReference.class:0:0:0:0 | FunctionReference | true |
| reflection.kt:22:9:22:91 | KProperty2<C,Integer,Integer> prop | file://<external>/KProperty2.class:0:0:0:0 | KProperty2<C,Integer,Integer> | file://<external>/KProperty2.class:0:0:0:0 | KProperty2<C,Integer,Integer> | file://<external>/Function2.class:0:0:0:0 | Function2<C,Integer,Integer> | true |
| reflection.kt:22:9:22:91 | KProperty2<C,Integer,Integer> prop | file://<external>/KProperty2.class:0:0:0:0 | KProperty2<C,Integer,Integer> | file://<external>/KProperty2.class:0:0:0:0 | KProperty2<C,Integer,Integer> | file://<external>/KProperty.class:0:0:0:0 | KProperty<Integer> | true |
invocation
| reflection.kt:6:21:6:24 | getName(...) | file://<external>/KCallable.class:0:0:0:0 | getName |
| reflection.kt:9:26:9:33 | get(...) | file://<external>/KProperty1.class:0:0:0:0 | get |
| reflection.kt:10:29:10:32 | getName(...) | file://<external>/KCallable.class:0:0:0:0 | getName |
| reflection.kt:11:48:11:53 | getGetter(...) | file://<external>/KProperty1.class:0:0:0:0 | getGetter |
| reflection.kt:12:38:12:44 | get(...) | file://<external>/KProperty1.class:0:0:0:0 | get |
| reflection.kt:16:27:16:37 | set(...) | file://<external>/KMutableProperty1.class:0:0:0:0 | set |
| reflection.kt:17:29:17:32 | getName(...) | file://<external>/KCallable.class:0:0:0:0 | getName |
| reflection.kt:18:55:18:60 | getSetter(...) | file://<external>/KMutableProperty1.class:0:0:0:0 | getSetter |
| reflection.kt:19:44:19:50 | set(...) | file://<external>/KMutableProperty1.class:0:0:0:0 | set |
| reflection.kt:22:31:22:37 | getMembers(...) | file://<external>/KClass.class:0:0:0:0 | getMembers |
| reflection.kt:22:51:22:54 | getName(...) | file://<external>/KCallable.class:0:0:0:0 | getName |
| reflection.kt:23:23:23:33 | get(...) | file://<external>/KProperty2.class:0:0:0:0 | get |
| reflection.kt:48:30:48:39 | get(...) | file://<external>/KProperty1.class:0:0:0:0 | get |
| reflection.kt:49:30:49:34 | get(...) | file://<external>/KProperty0.class:0:0:0:0 | get |

View File

@@ -1,12 +1,50 @@
import kotlin.reflect.KFunction2
import kotlin.reflect.*
class Reflection {
fun fn(boo: Boolean) {
fun fn() {
val ref: KFunction2<Ccc, Int, Double> = Ccc::m
println(ref.name)
val x0: KProperty1<C, Int> = C::p0
val x1: Int = x0.get(C())
val x2: String = x0.name
val x3: KProperty1.Getter<C, Int> = x0.getter
val x4: KFunction1<C, Int> = x0::get
val x5: KProperty0<Int> = C()::p0
val y0: KMutableProperty1<C, Int> = C::p1
val y1: Unit = y0.set(C(), 5)
val y2: String = y0.name
val y3: KMutableProperty1.Setter<C, Int> = y0.setter
val y4: KFunction2<C, Int, Unit> = y0::set
val y5: KMutableProperty0<Int> = C()::p1
val prop = (C::class).members.single { it.name == "p3" } as KProperty2<C, Int, Int>
val z0 = prop.get(C(), 5)
}
class Ccc {
fun m(i:Int):Double = 5.0
}
}
class C {
val p0: Int = 1
var p1: Int = 2
var p2: Int
get() = 1
set(value) = Unit
var Int.p3: Int // this can't be referenced through a property reference
get() = 1
set(value) = Unit
}
}
val String.lastChar: Char
get() = this[length - 1]
fun fn2() {
println(String::lastChar.get("abc"))
println("abcd"::lastChar.get())
}

View File

@@ -26,6 +26,7 @@ query predicate variableInitializerType(
LocalVariableDecl decl, RefType t1, RefType t2, RefType t3, boolean compatible
) {
decl.getType() = t1 and
t1.getPackage().getName() = "kotlin.reflect" and
decl.getInitializer().getType() = t2 and
t2.extendsOrImplements(t3) and
(