PSI/FIR->IR translators: return to 1.7.10 behavior for field references

In 1.7.20 we used the nearest Java-based receiver for such field
references in backend. Now we are using use-site receiver again,
it can lead to accidental usage of derived class property backing field.

This is effectively a revert of KT-49507 fix, see commits:
- fa914f20
- b0a6508d

#KT-54393 Fixed
#KT-49507 Planned
#KT-52338 Planned
This commit is contained in:
Mikhail Glukhikh
2022-10-21 16:08:56 +02:00
committed by Space Team
parent 14213ddad2
commit c0789b5207
20 changed files with 62 additions and 157 deletions
@@ -1,4 +1,7 @@
// TARGET_BACKEND: JVM_IR
// IGNORE_BACKEND: JVM_IR
// IGNORE_BACKEND_FIR: JVM_IR
// FIR_STATUS: accesses companion property backing field statically and fails (does not work in K1/JVM too)
// FILE: Base.java
public class Base {
@@ -0,0 +1,18 @@
// TARGET_BACKEND: JVM_IR
// Note: works accidentally via backing field access
// Field VS property: case 4.2
// More or less duplicates the case in KT-34943/KT-54393
// FILE: BaseJava.java
public class BaseJava {
public String a = "FAIL";
}
// FILE: Derived.kt
class Derived : BaseJava() {
private val a = "OK"
fun x() = a
}
fun box() = Derived().x()
@@ -1,36 +0,0 @@
FILE fqName:<root> fileName:/Derived.kt
CLASS CLASS name:Derived modality:FINAL visibility:public superTypes:[<root>.BaseJava]
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.Derived
CONSTRUCTOR visibility:public <> () returnType:<root>.Derived [primary]
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'public constructor <init> () [primary] declared in <root>.BaseJava'
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:Derived modality:FINAL visibility:public superTypes:[<root>.BaseJava]'
PROPERTY name:a visibility:public modality:FINAL [val]
FIELD PROPERTY_BACKING_FIELD name:a type:kotlin.String visibility:private [final]
EXPRESSION_BODY
CONST String type=kotlin.String value="FAIL"
FUN DEFAULT_PROPERTY_ACCESSOR name:<get-a> visibility:public modality:FINAL <> ($this:<root>.Derived) returnType:kotlin.String
correspondingProperty: PROPERTY name:a visibility:public modality:FINAL [val]
$this: VALUE_PARAMETER name:<this> type:<root>.Derived
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun <get-a> (): kotlin.String declared in <root>.Derived'
GET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:a type:kotlin.String visibility:private [final]' type=kotlin.String origin=null
receiver: GET_VAR '<this>: <root>.Derived declared in <root>.Derived.<get-a>' type=<root>.Derived origin=null
FUN FAKE_OVERRIDE name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:kotlin.Boolean [fake_override,operator]
overridden:
public open fun equals (other: kotlin.Any?): kotlin.Boolean [fake_override,operator] declared in <root>.BaseJava
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
VALUE_PARAMETER name:other index:0 type:kotlin.Any?
FUN FAKE_OVERRIDE name:hashCode visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.Int [fake_override]
overridden:
public open fun hashCode (): kotlin.Int [fake_override] declared in <root>.BaseJava
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
FUN FAKE_OVERRIDE name:toString visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.String [fake_override]
overridden:
public open fun toString (): kotlin.String [fake_override] declared in <root>.BaseJava
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
FUN name:box visibility:public modality:FINAL <> () returnType:@[FlexibleNullability] kotlin.String?
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun box (): @[FlexibleNullability] kotlin.String? declared in <root>'
GET_FIELD 'FIELD IR_EXTERNAL_JAVA_DECLARATION_STUB name:a type:@[FlexibleNullability] kotlin.String? visibility:public' type=@[FlexibleNullability] kotlin.String? origin=GET_PROPERTY
receiver: CONSTRUCTOR_CALL 'public constructor <init> () [primary] declared in <root>.Derived' type=<root>.Derived origin=null
@@ -1,39 +0,0 @@
FILE fqName:<root> fileName:/Derived.kt
CLASS CLASS name:Derived modality:FINAL visibility:public superTypes:[<root>.BaseJava]
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.Derived
CONSTRUCTOR visibility:public <> () returnType:<root>.Derived [primary]
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'public constructor <init> () [primary] declared in <root>.BaseJava'
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:Derived modality:FINAL visibility:public superTypes:[<root>.BaseJava]'
PROPERTY name:a visibility:public modality:FINAL [val]
FIELD PROPERTY_BACKING_FIELD name:a type:kotlin.String visibility:private [final]
EXPRESSION_BODY
CONST String type=kotlin.String value="FAIL"
FUN DEFAULT_PROPERTY_ACCESSOR name:<get-a> visibility:public modality:FINAL <> ($this:<root>.Derived) returnType:kotlin.String
correspondingProperty: PROPERTY name:a visibility:public modality:FINAL [val]
$this: VALUE_PARAMETER name:<this> type:<root>.Derived
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun <get-a> (): kotlin.String declared in <root>.Derived'
GET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:a type:kotlin.String visibility:private [final]' type=kotlin.String origin=null
receiver: GET_VAR '<this>: <root>.Derived declared in <root>.Derived.<get-a>' type=<root>.Derived origin=null
PROPERTY FAKE_OVERRIDE name:a visibility:public modality:FINAL [fake_override,var]
overridden:
public final a: @[FlexibleNullability] kotlin.String? [var]
FUN FAKE_OVERRIDE name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:kotlin.Boolean [fake_override,operator]
overridden:
public open fun equals (other: kotlin.Any?): kotlin.Boolean [fake_override,operator] declared in <root>.BaseJava
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
VALUE_PARAMETER name:other index:0 type:kotlin.Any?
FUN FAKE_OVERRIDE name:hashCode visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.Int [fake_override]
overridden:
public open fun hashCode (): kotlin.Int [fake_override] declared in <root>.BaseJava
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
FUN FAKE_OVERRIDE name:toString visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.String [fake_override]
overridden:
public open fun toString (): kotlin.String [fake_override] declared in <root>.BaseJava
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
FUN name:box visibility:public modality:FINAL <> () returnType:@[FlexibleNullability] kotlin.String?
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun box (): @[FlexibleNullability] kotlin.String? declared in <root>'
GET_FIELD 'FIELD IR_EXTERNAL_JAVA_DECLARATION_STUB name:a type:@[FlexibleNullability] kotlin.String? visibility:public' type=@[FlexibleNullability] kotlin.String? origin=GET_PROPERTY
receiver: CONSTRUCTOR_CALL 'public constructor <init> () [primary] declared in <root>.Derived' type=<root>.Derived origin=null
@@ -1,5 +1,9 @@
// TARGET_BACKEND: JVM_IR
// DUMP_IR
// IGNORE_BACKEND: JVM_IR
// IGNORE_BACKEND_FIR: JVM_IR
// FIR_STATUS: accesses property backing field accidentally and fails with exception (does not work in K1/JVM too)
// Field VS property: case 4.1
// See KT-54393 for details
// FILE: BaseJava.java
public class BaseJava {
+1 -11
View File
@@ -16,18 +16,8 @@ open class Kotlin2 : Java2() {
fun test1(j: Kotlin2) = j.f
// JVM_IR_TEMPLATES
// @Kotlin2.class:
// 2 GETFIELD Java2.f : I
// JVM_IR_TEMPLATES
// @TestKt.class:
// 2 GETFIELD Java2.f : I
// JVM_TEMPLATES
// @Kotlin2.class:
// 1 GETFIELD Java2.f : I
// JVM_TEMPLATES
// @TestKt.class:
// 1 GETFIELD Java2.f : I
// 1 GETFIELD Kotlin2.f : I
@@ -31,13 +31,7 @@ fun test(b : Boolean) {
}
// @TestKt.class:
// 1 PUTFIELD JFieldOwner.f : I
// 1 PUTFIELD Mid.f : I
// JVM_IR_TEMPLATES
// 2 GETFIELD JFieldOwner.f : I
// 0 GETFIELD Mid.f : I
// JVM_TEMPLATES
// 1 GETFIELD JFieldOwner.f : I
// 1 PUTFIELD JFieldOwner.f : I
// 1 GETFIELD Mid.f : I
// 1 PUTFIELD Mid.f : I
@@ -13,14 +13,14 @@ class C : B {
fun f(): @FlexibleNullability String? {
return eval<@FlexibleNullability String?>(f = local fun <anonymous>(): @FlexibleNullability String? {
return <this>(super<B>).#x
return <this>.#x
}
)
}
fun g(): @FlexibleNullability String? {
return eval<@FlexibleNullability String?>(f = local fun <anonymous>(): @FlexibleNullability String? {
return <this>(super<B>).#y
return <this>.#y
}
)
}
@@ -13,14 +13,14 @@ class C : B {
fun f(): @FlexibleNullability String? {
return eval<@FlexibleNullability String?>(f = local fun <anonymous>(): @FlexibleNullability String? {
return <this>(super<B>).#x
return <this>(super<C>).#x
}
)
}
fun g(): @FlexibleNullability String? {
return eval<@FlexibleNullability String?>(f = local fun <anonymous>(): @FlexibleNullability String? {
return <this>(super<B>).#y
return <this>(super<C>).#y
}
)
}
@@ -55,7 +55,7 @@ fun test(b: Boolean) {
else -> d2
}
k.#f = 42
k(super<JFieldOwner>).#f /*~> Unit */
k.#f /*~> Unit */
val md1: DerivedThroughMid1 = DerivedThroughMid1()
val md2: DerivedThroughMid2 = DerivedThroughMid2()
val mk: Mid = when {
@@ -63,6 +63,6 @@ fun test(b: Boolean) {
else -> md2
}
mk.#f = 44
mk(super<JFieldOwner>).#f /*~> Unit */
mk.#f /*~> Unit */
}
@@ -63,6 +63,6 @@ fun test(b: Boolean) {
else -> md2
}
mk /*as Mid */(super<Mid>).#f = 44
mk /*as Mid */(super<JFieldOwner>).#f /*~> Unit */
mk /*as Mid */(super<Mid>).#f /*~> Unit */
}
@@ -10,7 +10,7 @@ class Derived : Base {
}
fun getValue(): Int {
return <this>(super<Base>).#value
return <this>.#value
}
fun setValue(value: Int) {
@@ -10,7 +10,7 @@ class Derived : Base {
}
fun getValue(): Int {
return <this>(super<Base>).#value
return <this>(super<Derived>).#value
}
fun setValue(value: Int) {
+1 -1
View File
@@ -13,7 +13,7 @@ class Box<out T : Foo> {
get(): String {
var foo: Foo = <this>.<get-foo>()
when {
foo is Buz -> return foo /*as Buz */(super<Buz>).#str /*!! String */
foo is Buz -> return foo /*as Buz */.#str /*!! String */
}
return ""
}
@@ -1,6 +1,6 @@
fun foo(movedPaths: MutableList<Couple<FilePath>>) {
movedPaths.forEach<Couple<FilePath>>(action = local fun <anonymous>(it: Couple<FilePath>) {
it(super<Couple>).#second.getName() /*~> Unit */
it.#second.getName() /*~> Unit */
}
)
}
@@ -6,6 +6,6 @@ fun testSetField(a: Any, b: Any) {
fun testGetField(a: Any): String {
a as JCell<String> /*~> Unit */
return a /*as JCell<String> */(super<JCell>).#value /*!! String */
return a /*as JCell<String> */.#value /*!! String */
}