K2 JVM IR: fix access to Java field shadowed by Kotlin property

#KT-56386 Fixed

Co-authored-by: Mikhail Glukhikh <Mikhail.Glukhikh@jetbrains.com>
This commit is contained in:
Alexander Udalov
2023-02-09 00:57:53 +01:00
parent 641d636c45
commit d73d3c46e2
28 changed files with 363 additions and 97 deletions
@@ -1,7 +1,5 @@
// 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)
// IGNORE_BACKEND_K1: JVM_IR
// FILE: Base.java
public class Base {
@@ -1,7 +1,5 @@
// TARGET_BACKEND: JVM_IR
// IGNORE_BACKEND_K1: JVM_IR
// IGNORE_BACKEND_K2: JVM_IR
// tried to access field Derived.a from class DerivedKt
// FILE: BaseJava.java
public class BaseJava {
@@ -1,7 +1,5 @@
// TARGET_BACKEND: JVM_IR
// IGNORE_BACKEND_K1: JVM_IR
// IGNORE_BACKEND_K2: JVM_IR
// tried to access field Derived.a from class DerivedKt
// FILE: BaseJava.java
public class BaseJava {
@@ -1,7 +1,5 @@
// TARGET_BACKEND: JVM_IR
// IGNORE_BACKEND_K1: JVM_IR
// IGNORE_BACKEND_K2: JVM_IR
// FIR status: fails because of incorrect receiver in bytecode
// Field VS property: case "reference"
// FILE: BaseJava.java
@@ -1,7 +1,5 @@
// TARGET_BACKEND: JVM_IR
// IGNORE_BACKEND_K1: JVM_IR
// IGNORE_BACKEND_K2: JVM_IR
// tried to access field My.b from class Some
// FILE: Jaba.java
@@ -1,7 +1,5 @@
// TARGET_BACKEND: JVM_IR
// IGNORE_BACKEND_K1: JVM_IR
// IGNORE_BACKEND_K2: JVM_IR
// tried to access field My.a from class TestKt
// FILE: Jaba.java
@@ -1,7 +1,5 @@
// TARGET_BACKEND: JVM_IR
// IGNORE_BACKEND_K1: JVM_IR
// IGNORE_BACKEND_K2: JVM_IR
// tried to access field My.b from class TestKt
// FILE: Jaba.java
@@ -1,7 +1,6 @@
// TARGET_BACKEND: JVM_IR
// IGNORE_BACKEND_K1: JVM_IR
// IGNORE_BACKEND_K2: JVM_IR
// tried to access field B.f from class TestKt
// CHECK_BYTECODE_TEXT
// FILE: A.java
@@ -24,3 +23,5 @@ public class C extends B {}
fun box(): String {
return C().f
}
// 1 GETFIELD A.f
@@ -1,7 +1,6 @@
// TARGET_BACKEND: JVM_IR
// IGNORE_BACKEND_K1: JVM_IR
// IGNORE_BACKEND_K2: JVM_IR
// tried to access field base.B.f from class TestKt
// CHECK_BYTECODE_TEXT
// FILE: Y.java
@@ -36,3 +35,5 @@ public class C extends B {}
fun box(): String {
return C().f
}
// 1 GETFIELD base/A.f
@@ -0,0 +1,37 @@
// TARGET_BACKEND: JVM_IR
// IGNORE_BACKEND_K1: JVM_IR
// CHECK_BYTECODE_TEXT
// FILE: a/VeryBase.java
package a;
class VeryBase {
public String foo = "OK";
}
// FILE: a/Base.java
package a;
public class Base extends VeryBase {
}
// FILE: b/Intermediate.java
package b;
class Intermediate extends a.Base {
}
// FILE: box.kt
package b
private class Final : Intermediate() {
private val foo = "FAIL"
}
fun box(): String =
Final().foo
// 1 GETFIELD b/Intermediate.foo
@@ -0,0 +1,36 @@
// TARGET_BACKEND: JVM_IR
// IGNORE_BACKEND_K1: JVM_IR
// CHECK_BYTECODE_TEXT
// FILE: p/PackagePrivateJavaClass.java
package p;
class PackagePrivateJavaClass {
public String foo = "OK";
}
// FILE: p/JavaWrapper.java
package p;
public class JavaWrapper {
protected static class JavaDerived extends PackagePrivateJavaClass {}
}
// FILE: test.kt
import p.JavaWrapper
class KotlinWrapper : JavaWrapper() {
protected class KotlinDerived : JavaDerived() {
private val foo = "FAIL"
}
fun bar() = KotlinDerived().foo
}
fun box(): String {
return KotlinWrapper().bar()
}
// 1 GETFIELD p/JavaWrapper\$JavaDerived.foo
@@ -1,7 +1,5 @@
// TARGET_BACKEND: JVM_IR
// IGNORE_BACKEND_K1: JVM_IR
// IGNORE_BACKEND_K2: JVM_IR
// tried to access field derived.Intermediate.a from class derived.Derived
// Field VS property: case "reference", protected field, invisible property
// FILE: BaseJava.java
@@ -9,7 +7,7 @@
package base;
public class BaseJava {
protected String a = "OK";
protected String a = "";
}
// FILE: Derived.kt
@@ -24,9 +22,14 @@ open class Intermediate : BaseJava() {
class Derived : Intermediate() {
fun foo() = a
fun bar() {
a = "OK"
}
}
fun box(): String {
val d = Derived()
d.bar()
return d.foo()
}
@@ -1,7 +1,5 @@
// TARGET_BACKEND: JVM_IR
// IGNORE_BACKEND_K1: JVM_IR
// IGNORE_BACKEND_K2: JVM_IR
// tried to access field base.Intermediate.a from class base.Derived$foo$1
// Field VS property: case "reference", protected field in the same package, invisible property
// FILE: BaseJava.java
@@ -9,23 +7,31 @@
package base;
public class BaseJava {
protected String a = "OK";
protected String a = "";
}
// FILE: Derived.kt
package base
// Note: this test should report an error when we are in different package
package derived
import base.BaseJava
open class Intermediate : BaseJava() {
private val a = "FAIL"
}
// TODO: remove suppress after dropping the relevant diagnostic
@Suppress("JAVA_SHADOWED_PROTECTED_FIELD_REFERENCE")
class Derived : Intermediate() {
fun foo() = this::a.get()
fun bar() {
Derived::a.set(this, "OK")
}
}
fun box(): String {
val d = Derived()
d.bar()
return d.foo()
}
@@ -1,37 +0,0 @@
// TARGET_BACKEND: JVM_IR
// IGNORE_BACKEND_K1: JVM_IR
// IGNORE_BACKEND_K2: JVM_IR
// tried to access field derived.Intermediate.a from class derived.Derived
// Field VS property: case "reference", protected field, invisible property
// FILE: BaseJava.java
package base;
public class BaseJava {
protected String a = "";
}
// FILE: Derived.kt
package derived
import base.BaseJava
open class Intermediate : BaseJava() {
private val a = "FAIL"
}
class Derived : Intermediate() {
fun foo() = a
fun bar() {
a = "OK"
}
}
fun box(): String {
val d = Derived()
d.bar()
return d.foo()
}