diff --git a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java index 0a36258a65b..2d1c1787691 100644 --- a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java +++ b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java @@ -17951,12 +17951,42 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTest("compiler/testData/codegen/box/fieldRename/genericPropertyWithItself.kt"); } + @Test + @TestMetadata("javaCloseFieldAndKotlinProperty.kt") + public void testJavaCloseFieldAndKotlinProperty() throws Exception { + runTest("compiler/testData/codegen/box/fieldRename/javaCloseFieldAndKotlinProperty.kt"); + } + @Test @TestMetadata("javaFieldAndCompanionProperty.kt") public void testJavaFieldAndCompanionProperty() throws Exception { runTest("compiler/testData/codegen/box/fieldRename/javaFieldAndCompanionProperty.kt"); } + @Test + @TestMetadata("javaFieldAndKotlinInvisibleProperty.kt") + public void testJavaFieldAndKotlinInvisibleProperty() throws Exception { + runTest("compiler/testData/codegen/box/fieldRename/javaFieldAndKotlinInvisibleProperty.kt"); + } + + @Test + @TestMetadata("javaFieldAndKotlinInvisiblePropertyViaTypeParameter.kt") + public void testJavaFieldAndKotlinInvisiblePropertyViaTypeParameter() throws Exception { + runTest("compiler/testData/codegen/box/fieldRename/javaFieldAndKotlinInvisiblePropertyViaTypeParameter.kt"); + } + + @Test + @TestMetadata("javaFieldAndKotlinInvisiblePropertyWithGetter.kt") + public void testJavaFieldAndKotlinInvisiblePropertyWithGetter() throws Exception { + runTest("compiler/testData/codegen/box/fieldRename/javaFieldAndKotlinInvisiblePropertyWithGetter.kt"); + } + + @Test + @TestMetadata("javaFieldAndKotlinLateinitProperty.kt") + public void testJavaFieldAndKotlinLateinitProperty() throws Exception { + runTest("compiler/testData/codegen/box/fieldRename/javaFieldAndKotlinLateinitProperty.kt"); + } + @Test @TestMetadata("javaFieldAndKotlinPrivateProperty.kt") public void testJavaFieldAndKotlinPrivateProperty() throws Exception { @@ -17969,6 +17999,90 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTest("compiler/testData/codegen/box/fieldRename/javaFieldAndKotlinProperty.kt"); } + @Test + @TestMetadata("javaFieldAndKotlinPropertyReference.kt") + public void testJavaFieldAndKotlinPropertyReference() throws Exception { + runTest("compiler/testData/codegen/box/fieldRename/javaFieldAndKotlinPropertyReference.kt"); + } + + @Test + @TestMetadata("javaFieldAndKotlinPropertyReferenceFromInner.kt") + public void testJavaFieldAndKotlinPropertyReferenceFromInner() throws Exception { + runTest("compiler/testData/codegen/box/fieldRename/javaFieldAndKotlinPropertyReferenceFromInner.kt"); + } + + @Test + @TestMetadata("javaFieldAndKotlinPropertySuperAccess.kt") + public void testJavaFieldAndKotlinPropertySuperAccess() throws Exception { + runTest("compiler/testData/codegen/box/fieldRename/javaFieldAndKotlinPropertySuperAccess.kt"); + } + + @Test + @TestMetadata("javaFieldAndKotlinPropertyWithComplexReceiver.kt") + public void testJavaFieldAndKotlinPropertyWithComplexReceiver() throws Exception { + runTest("compiler/testData/codegen/box/fieldRename/javaFieldAndKotlinPropertyWithComplexReceiver.kt"); + } + + @Test + @TestMetadata("javaFieldAndKotlinPropertyWithSmartcast.kt") + public void testJavaFieldAndKotlinPropertyWithSmartcast() throws Exception { + runTest("compiler/testData/codegen/box/fieldRename/javaFieldAndKotlinPropertyWithSmartcast.kt"); + } + + @Test + @TestMetadata("javaFieldKotlinPropertyJavaField.kt") + public void testJavaFieldKotlinPropertyJavaField() throws Exception { + runTest("compiler/testData/codegen/box/fieldRename/javaFieldKotlinPropertyJavaField.kt"); + } + + @Test + @TestMetadata("javaFieldKotlinPropertyJavaPackagePrivate.kt") + public void testJavaFieldKotlinPropertyJavaPackagePrivate() throws Exception { + runTest("compiler/testData/codegen/box/fieldRename/javaFieldKotlinPropertyJavaPackagePrivate.kt"); + } + + @Test + @TestMetadata("javaInvisibleFieldAndKotlinProperty.kt") + public void testJavaInvisibleFieldAndKotlinProperty() throws Exception { + runTest("compiler/testData/codegen/box/fieldRename/javaInvisibleFieldAndKotlinProperty.kt"); + } + + @Test + @TestMetadata("javaInvisibleFieldAndKotlinPropertyReference.kt") + public void testJavaInvisibleFieldAndKotlinPropertyReference() throws Exception { + runTest("compiler/testData/codegen/box/fieldRename/javaInvisibleFieldAndKotlinPropertyReference.kt"); + } + + @Test + @TestMetadata("javaProtectedFieldAndKotlinInvisibleProperty.kt") + public void testJavaProtectedFieldAndKotlinInvisibleProperty() throws Exception { + runTest("compiler/testData/codegen/box/fieldRename/javaProtectedFieldAndKotlinInvisibleProperty.kt"); + } + + @Test + @TestMetadata("javaProtectedFieldAndKotlinInvisiblePropertyReference.kt") + public void testJavaProtectedFieldAndKotlinInvisiblePropertyReference() throws Exception { + runTest("compiler/testData/codegen/box/fieldRename/javaProtectedFieldAndKotlinInvisiblePropertyReference.kt"); + } + + @Test + @TestMetadata("javaProtectedFieldAndKotlinInvisiblePropertyWrite.kt") + public void testJavaProtectedFieldAndKotlinInvisiblePropertyWrite() throws Exception { + runTest("compiler/testData/codegen/box/fieldRename/javaProtectedFieldAndKotlinInvisiblePropertyWrite.kt"); + } + + @Test + @TestMetadata("javaProtectedFieldAndKotlinPropertyReference.kt") + public void testJavaProtectedFieldAndKotlinPropertyReference() throws Exception { + runTest("compiler/testData/codegen/box/fieldRename/javaProtectedFieldAndKotlinPropertyReference.kt"); + } + + @Test + @TestMetadata("javaProtectedFieldAnotherPackageReference.kt") + public void testJavaProtectedFieldAnotherPackageReference() throws Exception { + runTest("compiler/testData/codegen/box/fieldRename/javaProtectedFieldAnotherPackageReference.kt"); + } + @Test @TestMetadata("jvmFieldNoClash1.kt") public void testJvmFieldNoClash1() throws Exception { diff --git a/compiler/testData/codegen/box/fieldRename/javaCloseFieldAndKotlinProperty.kt b/compiler/testData/codegen/box/fieldRename/javaCloseFieldAndKotlinProperty.kt new file mode 100644 index 00000000000..3935aa91430 --- /dev/null +++ b/compiler/testData/codegen/box/fieldRename/javaCloseFieldAndKotlinProperty.kt @@ -0,0 +1,30 @@ +// TARGET_BACKEND: JVM_IR +// Field VS property: case 3.1 +// See KT-54393 for details + +// FILE: VeryBase.kt +open class VeryBase { + val some = "FAIL" +} + +// FILE: Base.java +public class Base extends VeryBase { + public String some = "OK"; + + public String foo() { + return some; + } +} + +// FILE: Test.kt +class Derived : Base() + +fun box(): String { + val first = Derived().some + if (first != "OK") return first + val d = Derived() + if (d::some.get() != "OK") return d::some.get() + d.some = "12" + if (d.foo() != "12") return "Error writing: ${d.foo()}" + return "OK" +} diff --git a/compiler/testData/codegen/box/fieldRename/javaFieldAndCompanionProperty.kt b/compiler/testData/codegen/box/fieldRename/javaFieldAndCompanionProperty.kt index b1261613250..198cde701d0 100644 --- a/compiler/testData/codegen/box/fieldRename/javaFieldAndCompanionProperty.kt +++ b/compiler/testData/codegen/box/fieldRename/javaFieldAndCompanionProperty.kt @@ -6,6 +6,10 @@ // FILE: Base.java public class Base { protected String TAG = "OK"; + + public String foo() { + return TAG; + } } // FILE: Sub.kt @@ -16,6 +20,18 @@ class Sub : Base() { } fun log() = TAG + + fun logReference() = this::TAG.get() + + fun logAssignment(): String { + TAG = "12" + if (foo() != "12") return "Error writing: ${foo()}" + return "OK" + } } -fun box() = Sub().log() +fun box(): String { + if (Sub().log() != "OK") return Sub().log() + if (Sub().logReference() != "OK") return Sub().logReference() + return Sub().logAssignment() +} diff --git a/compiler/testData/codegen/box/fieldRename/javaFieldAndKotlinInvisibleProperty.kt b/compiler/testData/codegen/box/fieldRename/javaFieldAndKotlinInvisibleProperty.kt new file mode 100644 index 00000000000..d17336f801d --- /dev/null +++ b/compiler/testData/codegen/box/fieldRename/javaFieldAndKotlinInvisibleProperty.kt @@ -0,0 +1,28 @@ +// 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 { + public String a = "OK"; + + public String foo() { + return a; + } +} + +// FILE: Derived.kt +class Derived : BaseJava() { + private val a = "FAIL" +} + +fun box(): String { + val first = Derived().a + if (first != "OK") return first + val d = Derived() + if (d::a.get() != "OK") return d::a.get() + d.a = "12" + if (d.foo() != "12") return "Error writing: ${d.foo()}" + return "OK" +} diff --git a/compiler/testData/codegen/box/fieldRename/javaFieldAndKotlinInvisiblePropertyViaTypeParameter.kt b/compiler/testData/codegen/box/fieldRename/javaFieldAndKotlinInvisiblePropertyViaTypeParameter.kt new file mode 100644 index 00000000000..95e7868512f --- /dev/null +++ b/compiler/testData/codegen/box/fieldRename/javaFieldAndKotlinInvisiblePropertyViaTypeParameter.kt @@ -0,0 +1,29 @@ +// 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 { + public String a = "OK"; + + public String foo() { + return a; + } +} + +// FILE: Derived.kt +open class Derived : BaseJava() { + private val a = "FAIL" +} + +fun test(t: T): String { + val first = t.a + if (first != "OK") return first + if (t::a.get() != "OK") return t::a.get() + t.a = "12" + if (t.foo() != "12") return "Error writing: ${t.foo()}" + return "OK" +} + +fun box(): String = test(Derived()) diff --git a/compiler/testData/codegen/box/fieldRename/javaFieldAndKotlinInvisiblePropertyWithGetter.kt b/compiler/testData/codegen/box/fieldRename/javaFieldAndKotlinInvisiblePropertyWithGetter.kt new file mode 100644 index 00000000000..b784c5bd6ee --- /dev/null +++ b/compiler/testData/codegen/box/fieldRename/javaFieldAndKotlinInvisiblePropertyWithGetter.kt @@ -0,0 +1,27 @@ +// TARGET_BACKEND: JVM_IR +// Field VS property: case 1.2 +// See KT-54393 for details + +// FILE: BaseJava.java +public class BaseJava { + public String a = "OK"; + + public String foo() { + return a; + } +} + +// FILE: Derived.kt +class Derived : BaseJava() { + private val a get() = "FAIL" +} + +fun box(): String { + val first = Derived().a + if (first != "OK") return first + val d = Derived() + if (d::a.get() != "OK") return d::a.get() + d.a = "12" + if (d.foo() != "12") return "Error writing: ${d.foo()}" + return "OK" +} diff --git a/compiler/testData/codegen/box/fieldRename/javaFieldAndKotlinLateinitProperty.kt b/compiler/testData/codegen/box/fieldRename/javaFieldAndKotlinLateinitProperty.kt new file mode 100644 index 00000000000..0489be9948f --- /dev/null +++ b/compiler/testData/codegen/box/fieldRename/javaFieldAndKotlinLateinitProperty.kt @@ -0,0 +1,24 @@ +// TARGET_BACKEND: JVM_IR +// Field VS property: case "lateinit" + +// FILE: BaseJava.java +public class BaseJava { + public String a = "FAIL"; + + public String fieldValue() { + return a; + } +} + +// FILE: Derived.kt +class Derived : BaseJava() { + lateinit var a: String +} + +fun box(): String { + val d = Derived() + d.a = "OK" + if ((d as BaseJava).a == "OK") return "FAIL (accidental shadowed field access #1)" + if (d.fieldValue() == "OK") return "FAIL (accidental shadowed field access #2)" + return d.a +} diff --git a/compiler/testData/codegen/box/fieldRename/javaFieldAndKotlinProperty.fir.ir.txt b/compiler/testData/codegen/box/fieldRename/javaFieldAndKotlinProperty.fir.ir.txt new file mode 100644 index 00000000000..4d5b9392a24 --- /dev/null +++ b/compiler/testData/codegen/box/fieldRename/javaFieldAndKotlinProperty.fir.ir.txt @@ -0,0 +1,83 @@ +FILE fqName: fileName:/Derived.kt + CLASS CLASS name:Derived modality:FINAL visibility:public superTypes:[.BaseJava] + $this: VALUE_PARAMETER INSTANCE_RECEIVER name: type:.Derived + CONSTRUCTOR visibility:public <> () returnType:.Derived [primary] + BLOCK_BODY + DELEGATING_CONSTRUCTOR_CALL 'public constructor () [primary] declared in .BaseJava' + INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:Derived modality:FINAL visibility:public superTypes:[.BaseJava]' + PROPERTY name:a visibility:public modality:FINAL [var] + FIELD PROPERTY_BACKING_FIELD name:a type:kotlin.String visibility:private + EXPRESSION_BODY + CONST String type=kotlin.String value="OK" + FUN DEFAULT_PROPERTY_ACCESSOR name: visibility:public modality:FINAL <> ($this:.Derived) returnType:kotlin.String + correspondingProperty: PROPERTY name:a visibility:public modality:FINAL [var] + $this: VALUE_PARAMETER name: type:.Derived + BLOCK_BODY + RETURN type=kotlin.Nothing from='public final fun (): kotlin.String declared in .Derived' + GET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:a type:kotlin.String visibility:private' type=kotlin.String origin=null + receiver: GET_VAR ': .Derived declared in .Derived.' type=.Derived origin=null + FUN DEFAULT_PROPERTY_ACCESSOR name: visibility:public modality:FINAL <> ($this:.Derived, :kotlin.String) returnType:kotlin.Unit + correspondingProperty: PROPERTY name:a visibility:public modality:FINAL [var] + $this: VALUE_PARAMETER name: type:.Derived + VALUE_PARAMETER name: index:0 type:kotlin.String + BLOCK_BODY + SET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:a type:kotlin.String visibility:private' type=kotlin.Unit origin=null + receiver: GET_VAR ': .Derived declared in .Derived.' type=.Derived origin=null + value: GET_VAR ': kotlin.String declared in .Derived.' type=kotlin.String 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 .BaseJava + $this: VALUE_PARAMETER name: 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 .BaseJava + $this: VALUE_PARAMETER name: 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 .BaseJava + $this: VALUE_PARAMETER name: type:kotlin.Any + FUN name:box visibility:public modality:FINAL <> () returnType:kotlin.String + BLOCK_BODY + VAR name:first type:kotlin.String [val] + CALL 'public final fun (): kotlin.String declared in .Derived' type=kotlin.String origin=GET_PROPERTY + $this: CONSTRUCTOR_CALL 'public constructor () [primary] declared in .Derived' type=.Derived origin=null + WHEN type=kotlin.Unit origin=IF + BRANCH + if: CALL 'public final fun not (): kotlin.Boolean [operator] declared in kotlin.Boolean' type=kotlin.Boolean origin=EXCLEQ + $this: CALL 'public final fun EQEQ (arg0: kotlin.Any?, arg1: kotlin.Any?): kotlin.Boolean declared in kotlin.internal.ir' type=kotlin.Boolean origin=EXCLEQ + arg0: GET_VAR 'val first: kotlin.String [val] declared in .box' type=kotlin.String origin=null + arg1: CONST String type=kotlin.String value="OK" + then: RETURN type=kotlin.Nothing from='public final fun box (): kotlin.String declared in ' + GET_VAR 'val first: kotlin.String [val] declared in .box' type=kotlin.String origin=null + VAR name:d type:.Derived [val] + CONSTRUCTOR_CALL 'public constructor () [primary] declared in .Derived' type=.Derived origin=null + WHEN type=kotlin.Unit origin=IF + BRANCH + if: CALL 'public final fun not (): kotlin.Boolean [operator] declared in kotlin.Boolean' type=kotlin.Boolean origin=EXCLEQ + $this: CALL 'public final fun EQEQ (arg0: kotlin.Any?, arg1: kotlin.Any?): kotlin.Boolean declared in kotlin.internal.ir' type=kotlin.Boolean origin=EXCLEQ + arg0: CALL 'public abstract fun get (): V of kotlin.reflect.KMutableProperty0 [expect,fake_override] declared in kotlin.reflect.KMutableProperty0' type=kotlin.String origin=null + $this: PROPERTY_REFERENCE 'public final a: kotlin.String [var]' field=null getter='public final fun (): kotlin.String declared in .Derived' setter='public final fun (: kotlin.String): kotlin.Unit declared in .Derived' type=kotlin.reflect.KMutableProperty0 origin=null + $this: GET_VAR 'val d: .Derived [val] declared in .box' type=.Derived origin=null + arg1: CONST String type=kotlin.String value="OK" + then: RETURN type=kotlin.Nothing from='public final fun box (): kotlin.String declared in ' + CALL 'public abstract fun get (): V of kotlin.reflect.KMutableProperty0 [expect,fake_override] declared in kotlin.reflect.KMutableProperty0' type=kotlin.String origin=null + $this: PROPERTY_REFERENCE 'public final a: kotlin.String [var]' field=null getter='public final fun (): kotlin.String declared in .Derived' setter='public final fun (: kotlin.String): kotlin.Unit declared in .Derived' type=kotlin.reflect.KMutableProperty0 origin=null + $this: GET_VAR 'val d: .Derived [val] declared in .box' type=.Derived origin=null + CALL 'public final fun (: kotlin.String): kotlin.Unit declared in .Derived' type=kotlin.Unit origin=EQ + $this: GET_VAR 'val d: .Derived [val] declared in .box' type=.Derived origin=null + : CONST String type=kotlin.String value="12" + WHEN type=kotlin.Unit origin=IF + BRANCH + if: CALL 'public final fun not (): kotlin.Boolean [operator] declared in kotlin.Boolean' type=kotlin.Boolean origin=EXCLEQ + $this: CALL 'public final fun EQEQ (arg0: kotlin.Any?, arg1: kotlin.Any?): kotlin.Boolean declared in kotlin.internal.ir' type=kotlin.Boolean origin=EXCLEQ + arg0: CALL 'public final fun (): kotlin.String declared in .Derived' type=kotlin.String origin=GET_PROPERTY + $this: GET_VAR 'val d: .Derived [val] declared in .box' type=.Derived origin=null + arg1: CONST String type=kotlin.String value="12" + then: RETURN type=kotlin.Nothing from='public final fun box (): kotlin.String declared in ' + STRING_CONCATENATION type=kotlin.String + CONST String type=kotlin.String value="Error writing: " + CALL 'public final fun (): kotlin.String declared in .Derived' type=kotlin.String origin=GET_PROPERTY + $this: GET_VAR 'val d: .Derived [val] declared in .box' type=.Derived origin=null + RETURN type=kotlin.Nothing from='public final fun box (): kotlin.String declared in ' + CONST String type=kotlin.String value="OK" diff --git a/compiler/testData/codegen/box/fieldRename/javaFieldAndKotlinProperty.ir.txt b/compiler/testData/codegen/box/fieldRename/javaFieldAndKotlinProperty.ir.txt new file mode 100644 index 00000000000..fcc84304203 --- /dev/null +++ b/compiler/testData/codegen/box/fieldRename/javaFieldAndKotlinProperty.ir.txt @@ -0,0 +1,88 @@ +FILE fqName: fileName:/Derived.kt + CLASS CLASS name:Derived modality:FINAL visibility:public superTypes:[.BaseJava] + $this: VALUE_PARAMETER INSTANCE_RECEIVER name: type:.Derived + CONSTRUCTOR visibility:public <> () returnType:.Derived [primary] + BLOCK_BODY + DELEGATING_CONSTRUCTOR_CALL 'public constructor () [primary] declared in .BaseJava' + INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:Derived modality:FINAL visibility:public superTypes:[.BaseJava]' + PROPERTY name:a visibility:public modality:FINAL [var] + FIELD PROPERTY_BACKING_FIELD name:a type:kotlin.String visibility:private + EXPRESSION_BODY + CONST String type=kotlin.String value="OK" + FUN DEFAULT_PROPERTY_ACCESSOR name: visibility:public modality:FINAL <> ($this:.Derived) returnType:kotlin.String + correspondingProperty: PROPERTY name:a visibility:public modality:FINAL [var] + $this: VALUE_PARAMETER name: type:.Derived + BLOCK_BODY + RETURN type=kotlin.Nothing from='public final fun (): kotlin.String declared in .Derived' + GET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:a type:kotlin.String visibility:private' type=kotlin.String origin=null + receiver: GET_VAR ': .Derived declared in .Derived.' type=.Derived origin=null + FUN DEFAULT_PROPERTY_ACCESSOR name: visibility:public modality:FINAL <> ($this:.Derived, :kotlin.String) returnType:kotlin.Unit + correspondingProperty: PROPERTY name:a visibility:public modality:FINAL [var] + $this: VALUE_PARAMETER name: type:.Derived + VALUE_PARAMETER name: index:0 type:kotlin.String + BLOCK_BODY + SET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:a type:kotlin.String visibility:private' type=kotlin.Unit origin=null + receiver: GET_VAR ': .Derived declared in .Derived.' type=.Derived origin=null + value: GET_VAR ': kotlin.String declared in .Derived.' type=kotlin.String 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 .BaseJava + $this: VALUE_PARAMETER name: 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 .BaseJava + $this: VALUE_PARAMETER name: 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 .BaseJava + $this: VALUE_PARAMETER name: type:kotlin.Any + FUN name:box visibility:public modality:FINAL <> () returnType:kotlin.String + BLOCK_BODY + VAR name:first type:@[FlexibleNullability] kotlin.String? [val] + 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 () [primary] declared in .Derived' type=.Derived origin=null + WHEN type=kotlin.Unit origin=IF + BRANCH + if: CALL 'public final fun not (): kotlin.Boolean [operator] declared in kotlin.Boolean' type=kotlin.Boolean origin=EXCLEQ + $this: CALL 'public final fun EQEQ (arg0: kotlin.Any?, arg1: kotlin.Any?): kotlin.Boolean declared in kotlin.internal.ir' type=kotlin.Boolean origin=EXCLEQ + arg0: GET_VAR 'val first: @[FlexibleNullability] kotlin.String? [val] declared in .box' type=@[FlexibleNullability] kotlin.String? origin=null + arg1: CONST String type=kotlin.String value="OK" + then: RETURN type=kotlin.Nothing from='public final fun box (): kotlin.String declared in ' + TYPE_OP type=kotlin.String origin=IMPLICIT_NOTNULL typeOperand=kotlin.String + GET_VAR 'val first: @[FlexibleNullability] kotlin.String? [val] declared in .box' type=@[FlexibleNullability] kotlin.String? origin=null + VAR name:d type:.Derived [val] + CONSTRUCTOR_CALL 'public constructor () [primary] declared in .Derived' type=.Derived origin=null + WHEN type=kotlin.Unit origin=IF + BRANCH + if: CALL 'public final fun not (): kotlin.Boolean [operator] declared in kotlin.Boolean' type=kotlin.Boolean origin=EXCLEQ + $this: CALL 'public final fun EQEQ (arg0: kotlin.Any?, arg1: kotlin.Any?): kotlin.Boolean declared in kotlin.internal.ir' type=kotlin.Boolean origin=EXCLEQ + arg0: CALL 'public abstract fun get (): V of kotlin.reflect.KMutableProperty0 [expect,fake_override] declared in kotlin.reflect.KMutableProperty0' type=@[FlexibleNullability] kotlin.String? origin=null + $this: PROPERTY_REFERENCE 'public final a [fake_override,var]' field='FIELD IR_EXTERNAL_JAVA_DECLARATION_STUB name:a type:@[FlexibleNullability] kotlin.String? visibility:public' getter=null setter=null type=kotlin.reflect.KMutableProperty0<@[FlexibleNullability] kotlin.String?> origin=null + $this: GET_VAR 'val d: .Derived [val] declared in .box' type=.Derived origin=null + arg1: CONST String type=kotlin.String value="OK" + then: RETURN type=kotlin.Nothing from='public final fun box (): kotlin.String declared in ' + TYPE_OP type=kotlin.String origin=IMPLICIT_NOTNULL typeOperand=kotlin.String + CALL 'public abstract fun get (): V of kotlin.reflect.KMutableProperty0 [expect,fake_override] declared in kotlin.reflect.KMutableProperty0' type=@[FlexibleNullability] kotlin.String? origin=null + $this: PROPERTY_REFERENCE 'public final a [fake_override,var]' field='FIELD IR_EXTERNAL_JAVA_DECLARATION_STUB name:a type:@[FlexibleNullability] kotlin.String? visibility:public' getter=null setter=null type=kotlin.reflect.KMutableProperty0<@[FlexibleNullability] kotlin.String?> origin=null + $this: GET_VAR 'val d: .Derived [val] declared in .box' type=.Derived origin=null + SET_FIELD 'FIELD IR_EXTERNAL_JAVA_DECLARATION_STUB name:a type:@[FlexibleNullability] kotlin.String? visibility:public' type=kotlin.Unit origin=EQ + receiver: GET_VAR 'val d: .Derived [val] declared in .box' type=.Derived origin=null + value: CONST String type=kotlin.String value="12" + WHEN type=kotlin.Unit origin=IF + BRANCH + if: CALL 'public final fun not (): kotlin.Boolean [operator] declared in kotlin.Boolean' type=kotlin.Boolean origin=EXCLEQ + $this: CALL 'public final fun EQEQ (arg0: kotlin.Any?, arg1: kotlin.Any?): kotlin.Boolean declared in kotlin.internal.ir' type=kotlin.Boolean origin=EXCLEQ + arg0: GET_FIELD 'FIELD IR_EXTERNAL_JAVA_DECLARATION_STUB name:a type:@[FlexibleNullability] kotlin.String? visibility:public' type=@[FlexibleNullability] kotlin.String? origin=GET_PROPERTY + receiver: GET_VAR 'val d: .Derived [val] declared in .box' type=.Derived origin=null + arg1: CONST String type=kotlin.String value="12" + then: RETURN type=kotlin.Nothing from='public final fun box (): kotlin.String declared in ' + STRING_CONCATENATION type=kotlin.String + CONST String type=kotlin.String value="Error writing: " + GET_FIELD 'FIELD IR_EXTERNAL_JAVA_DECLARATION_STUB name:a type:@[FlexibleNullability] kotlin.String? visibility:public' type=@[FlexibleNullability] kotlin.String? origin=GET_PROPERTY + receiver: GET_VAR 'val d: .Derived [val] declared in .box' type=.Derived origin=null + RETURN type=kotlin.Nothing from='public final fun box (): kotlin.String declared in ' + CONST String type=kotlin.String value="OK" diff --git a/compiler/testData/codegen/box/fieldRename/javaFieldAndKotlinProperty.kt b/compiler/testData/codegen/box/fieldRename/javaFieldAndKotlinProperty.kt index 645cbbd93b0..e73230d2f34 100644 --- a/compiler/testData/codegen/box/fieldRename/javaFieldAndKotlinProperty.kt +++ b/compiler/testData/codegen/box/fieldRename/javaFieldAndKotlinProperty.kt @@ -1,18 +1,25 @@ // TARGET_BACKEND: JVM_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) +// IGNORE_BACKEND_K1: JVM_IR // Field VS property: case 4.1 -// See KT-54393 for details +// See KT-50082 +// DUMP_IR // FILE: BaseJava.java public class BaseJava { - public String a = "OK"; + public String a = "FAIL"; } // FILE: Derived.kt class Derived : BaseJava() { - val a = "FAIL" + var a = "OK" } -fun box() = Derived().a \ No newline at end of file +fun box(): String { + val first = Derived().a + if (first != "OK") return first + val d = Derived() + if (d::a.get() != "OK") return d::a.get() + d.a = "12" + if (d.a != "12") return "Error writing: ${d.a}" + return "OK" +} diff --git a/compiler/testData/codegen/box/fieldRename/javaFieldAndKotlinPropertyReference.fir.ir.txt b/compiler/testData/codegen/box/fieldRename/javaFieldAndKotlinPropertyReference.fir.ir.txt new file mode 100644 index 00000000000..e87bd157910 --- /dev/null +++ b/compiler/testData/codegen/box/fieldRename/javaFieldAndKotlinPropertyReference.fir.ir.txt @@ -0,0 +1,58 @@ +FILE fqName: fileName:/Derived.kt + CLASS CLASS name:Derived modality:FINAL visibility:public superTypes:[.BaseJava] + $this: VALUE_PARAMETER INSTANCE_RECEIVER name: type:.Derived + CONSTRUCTOR visibility:public <> () returnType:.Derived [primary] + BLOCK_BODY + DELEGATING_CONSTRUCTOR_CALL 'public constructor () [primary] declared in .BaseJava' + INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:Derived modality:FINAL visibility:public superTypes:[.BaseJava]' + PROPERTY name:a visibility:private 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: visibility:private modality:FINAL <> ($this:.Derived) returnType:kotlin.String + correspondingProperty: PROPERTY name:a visibility:private modality:FINAL [val] + $this: VALUE_PARAMETER name: type:.Derived + BLOCK_BODY + RETURN type=kotlin.Nothing from='private final fun (): kotlin.String declared in .Derived' + GET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:a type:kotlin.String visibility:private [final]' type=kotlin.String origin=null + receiver: GET_VAR ': .Derived declared in .Derived.' type=.Derived origin=null + PROPERTY name:b visibility:private modality:FINAL [val] + FIELD PROPERTY_BACKING_FIELD name:b type:kotlin.String visibility:private [final] + EXPRESSION_BODY + CONST String type=kotlin.String value="FAIL" + FUN DEFAULT_PROPERTY_ACCESSOR name: visibility:private modality:FINAL <> ($this:.Derived) returnType:kotlin.String + correspondingProperty: PROPERTY name:b visibility:private modality:FINAL [val] + $this: VALUE_PARAMETER name: type:.Derived + BLOCK_BODY + RETURN type=kotlin.Nothing from='private final fun (): kotlin.String declared in .Derived' + GET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:b type:kotlin.String visibility:private [final]' type=kotlin.String origin=null + receiver: GET_VAR ': .Derived declared in .Derived.' type=.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 .BaseJava + $this: VALUE_PARAMETER name: 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 .BaseJava + $this: VALUE_PARAMETER name: 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 .BaseJava + $this: VALUE_PARAMETER name: type:kotlin.Any + FUN name:box visibility:public modality:FINAL <> () returnType:kotlin.String + BLOCK_BODY + VAR name:d type:.Derived [val] + CONSTRUCTOR_CALL 'public constructor () [primary] declared in .Derived' type=.Derived origin=null + RETURN type=kotlin.Nothing from='public final fun box (): kotlin.String declared in ' + CALL 'public final fun plus (other: kotlin.Any?): kotlin.String [operator] declared in kotlin.String' type=kotlin.String origin=PLUS + $this: CALL 'public abstract fun get (): V of kotlin.reflect.KMutableProperty0 [expect,fake_override] declared in kotlin.reflect.KMutableProperty0' type=@[FlexibleNullability] kotlin.String? origin=null + $this: PROPERTY_REFERENCE 'public open a: @[FlexibleNullability] kotlin.String? [var]' field='FIELD IR_EXTERNAL_JAVA_DECLARATION_STUB name:a type:@[FlexibleNullability] kotlin.String? visibility:public' getter='public open fun (): @[FlexibleNullability] kotlin.String? declared in .BaseJava' setter='public open fun (: @[FlexibleNullability] kotlin.String?): kotlin.Unit declared in .BaseJava' type=kotlin.reflect.KMutableProperty0<@[FlexibleNullability] kotlin.String?> origin=null + <1>: + $this: TYPE_OP type=.BaseJava origin=IMPLICIT_CAST typeOperand=.BaseJava + GET_VAR 'val d: .Derived [val] declared in .box' type=.Derived origin=null + other: CALL 'public abstract fun get (): V of kotlin.reflect.KMutableProperty0 [expect,fake_override] declared in kotlin.reflect.KMutableProperty0' type=@[FlexibleNullability] kotlin.String? origin=null + $this: PROPERTY_REFERENCE 'public/*package*/ open b: @[FlexibleNullability] kotlin.String? [var]' field='FIELD IR_EXTERNAL_JAVA_DECLARATION_STUB name:b type:@[FlexibleNullability] kotlin.String? visibility:public/*package*/' getter='public/*package*/ open fun (): @[FlexibleNullability] kotlin.String? declared in .BaseJava' setter='public/*package*/ open fun (: @[FlexibleNullability] kotlin.String?): kotlin.Unit declared in .BaseJava' type=kotlin.reflect.KMutableProperty0<@[FlexibleNullability] kotlin.String?> origin=null + <1>: + $this: TYPE_OP type=.BaseJava origin=IMPLICIT_CAST typeOperand=.BaseJava + GET_VAR 'val d: .Derived [val] declared in .box' type=.Derived origin=null diff --git a/compiler/testData/codegen/box/fieldRename/javaFieldAndKotlinPropertyReference.ir.txt b/compiler/testData/codegen/box/fieldRename/javaFieldAndKotlinPropertyReference.ir.txt new file mode 100644 index 00000000000..02b5baa3090 --- /dev/null +++ b/compiler/testData/codegen/box/fieldRename/javaFieldAndKotlinPropertyReference.ir.txt @@ -0,0 +1,61 @@ +FILE fqName: fileName:/Derived.kt + CLASS CLASS name:Derived modality:FINAL visibility:public superTypes:[.BaseJava] + $this: VALUE_PARAMETER INSTANCE_RECEIVER name: type:.Derived + CONSTRUCTOR visibility:public <> () returnType:.Derived [primary] + BLOCK_BODY + DELEGATING_CONSTRUCTOR_CALL 'public constructor () [primary] declared in .BaseJava' + INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:Derived modality:FINAL visibility:public superTypes:[.BaseJava]' + PROPERTY name:a visibility:private 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: visibility:private modality:FINAL <> ($this:.Derived) returnType:kotlin.String + correspondingProperty: PROPERTY name:a visibility:private modality:FINAL [val] + $this: VALUE_PARAMETER name: type:.Derived + BLOCK_BODY + RETURN type=kotlin.Nothing from='private final fun (): kotlin.String declared in .Derived' + GET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:a type:kotlin.String visibility:private [final]' type=kotlin.String origin=null + receiver: GET_VAR ': .Derived declared in .Derived.' type=.Derived origin=null + PROPERTY name:b visibility:private modality:FINAL [val] + FIELD PROPERTY_BACKING_FIELD name:b type:kotlin.String visibility:private [final] + EXPRESSION_BODY + CONST String type=kotlin.String value="FAIL" + FUN DEFAULT_PROPERTY_ACCESSOR name: visibility:private modality:FINAL <> ($this:.Derived) returnType:kotlin.String + correspondingProperty: PROPERTY name:b visibility:private modality:FINAL [val] + $this: VALUE_PARAMETER name: type:.Derived + BLOCK_BODY + RETURN type=kotlin.Nothing from='private final fun (): kotlin.String declared in .Derived' + GET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:b type:kotlin.String visibility:private [final]' type=kotlin.String origin=null + receiver: GET_VAR ': .Derived declared in .Derived.' type=.Derived origin=null + PROPERTY FAKE_OVERRIDE name:a visibility:public modality:FINAL [fake_override,var] + overridden: + public final a: @[FlexibleNullability] kotlin.String? [var] + PROPERTY FAKE_OVERRIDE name:b visibility:public/*package*/ modality:FINAL [fake_override,var] + overridden: + public/*package*/ final b: @[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 .BaseJava + $this: VALUE_PARAMETER name: 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 .BaseJava + $this: VALUE_PARAMETER name: 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 .BaseJava + $this: VALUE_PARAMETER name: type:kotlin.Any + FUN name:box visibility:public modality:FINAL <> () returnType:kotlin.String + BLOCK_BODY + VAR name:d type:.Derived [val] + CONSTRUCTOR_CALL 'public constructor () [primary] declared in .Derived' type=.Derived origin=null + RETURN type=kotlin.Nothing from='public final fun box (): kotlin.String declared in ' + CALL 'public final fun plus (other: kotlin.Any?): kotlin.String [operator] declared in kotlin.String' type=kotlin.String origin=PLUS + $this: TYPE_OP type=kotlin.String origin=IMPLICIT_NOTNULL typeOperand=kotlin.String + CALL 'public abstract fun get (): V of kotlin.reflect.KMutableProperty0 [expect,fake_override] declared in kotlin.reflect.KMutableProperty0' type=@[FlexibleNullability] kotlin.String? origin=null + $this: PROPERTY_REFERENCE 'public final a [fake_override,var]' field='FIELD IR_EXTERNAL_JAVA_DECLARATION_STUB name:a type:@[FlexibleNullability] kotlin.String? visibility:public' getter=null setter=null type=kotlin.reflect.KMutableProperty0<@[FlexibleNullability] kotlin.String?> origin=null + $this: GET_VAR 'val d: .Derived [val] declared in .box' type=.Derived origin=null + other: CALL 'public abstract fun get (): V of kotlin.reflect.KMutableProperty0 [expect,fake_override] declared in kotlin.reflect.KMutableProperty0' type=@[FlexibleNullability] kotlin.String? origin=null + $this: PROPERTY_REFERENCE 'public/*package*/ final b [fake_override,var]' field='FIELD IR_EXTERNAL_JAVA_DECLARATION_STUB name:b type:@[FlexibleNullability] kotlin.String? visibility:public/*package*/' getter=null setter=null type=kotlin.reflect.KMutableProperty0<@[FlexibleNullability] kotlin.String?> origin=null + $this: GET_VAR 'val d: .Derived [val] declared in .box' type=.Derived origin=null diff --git a/compiler/testData/codegen/box/fieldRename/javaFieldAndKotlinPropertyReference.kt b/compiler/testData/codegen/box/fieldRename/javaFieldAndKotlinPropertyReference.kt new file mode 100644 index 00000000000..dda60526777 --- /dev/null +++ b/compiler/testData/codegen/box/fieldRename/javaFieldAndKotlinPropertyReference.kt @@ -0,0 +1,20 @@ +// 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 +public class BaseJava { + public String a = "OK"; +} + +// FILE: Derived.kt +class Derived : BaseJava() { + private val a = "FAIL" +} + +fun box(): String { + val d = Derived() + return d::a.get() +} \ No newline at end of file diff --git a/compiler/testData/codegen/box/fieldRename/javaFieldAndKotlinPropertyReferenceFromInner.kt b/compiler/testData/codegen/box/fieldRename/javaFieldAndKotlinPropertyReferenceFromInner.kt new file mode 100644 index 00000000000..0babf6396ad --- /dev/null +++ b/compiler/testData/codegen/box/fieldRename/javaFieldAndKotlinPropertyReferenceFromInner.kt @@ -0,0 +1,25 @@ +// TARGET_BACKEND: JVM_IR +// FILE: Jaba.java + +package base; + +public class Jaba { + protected String a = "FAIL"; +} + +// FILE: test.kt + +import base.Jaba + +fun box(): String { + val x = object : Jaba() { + private val a: String = "OK" + inner class S { + fun foo() = ::a.get() + } + + fun bar() = S().foo() + } + + return x.bar() +} diff --git a/compiler/testData/codegen/box/fieldRename/javaFieldAndKotlinPropertySuperAccess.kt b/compiler/testData/codegen/box/fieldRename/javaFieldAndKotlinPropertySuperAccess.kt new file mode 100644 index 00000000000..9706e718c24 --- /dev/null +++ b/compiler/testData/codegen/box/fieldRename/javaFieldAndKotlinPropertySuperAccess.kt @@ -0,0 +1,26 @@ +// 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 + +public class Jaba { + public String a = "O"; + public String b = ""; +} + +// FILE: test.kt +open class My : Jaba() { + private val a: String = "FAIL" + private val b: String = "FAIL" +} + +class Some : My() { + fun soo(): String { + super.b = "K" + return super.a + super.b + } +} + +fun box(): String = Some().soo() diff --git a/compiler/testData/codegen/box/fieldRename/javaFieldAndKotlinPropertyWithComplexReceiver.kt b/compiler/testData/codegen/box/fieldRename/javaFieldAndKotlinPropertyWithComplexReceiver.kt new file mode 100644 index 00000000000..1bf3a2c8caa --- /dev/null +++ b/compiler/testData/codegen/box/fieldRename/javaFieldAndKotlinPropertyWithComplexReceiver.kt @@ -0,0 +1,23 @@ +// 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 + +public class Jaba { + public String a = "OK"; +} + +// FILE: test.kt +class My : Jaba() { + private val a: String = "FAIL" + + operator fun plus(my: My) = my +} + +fun create(): My? = My() + +fun box(): String { + return (create() ?: My()).a +} diff --git a/compiler/testData/codegen/box/fieldRename/javaFieldAndKotlinPropertyWithSmartcast.kt b/compiler/testData/codegen/box/fieldRename/javaFieldAndKotlinPropertyWithSmartcast.kt new file mode 100644 index 00000000000..f4ea77586dc --- /dev/null +++ b/compiler/testData/codegen/box/fieldRename/javaFieldAndKotlinPropertyWithSmartcast.kt @@ -0,0 +1,27 @@ +// 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 + +public class Jaba { + public String a = "O"; + public String b = ""; +} + +// FILE: test.kt +class My : Jaba() { + private val a: String = "FAIL" + private val b: String = "FAIL" +} + +fun test(j: Any): String { + if (j is My) { + j.b = "K" + return j.a + j.b + } + return "NO SMARTCAST" +} + +fun box(): String = test(My()) diff --git a/compiler/testData/codegen/box/fieldRename/javaFieldKotlinPropertyJavaField.kt b/compiler/testData/codegen/box/fieldRename/javaFieldKotlinPropertyJavaField.kt new file mode 100644 index 00000000000..4bd706caa14 --- /dev/null +++ b/compiler/testData/codegen/box/fieldRename/javaFieldKotlinPropertyJavaField.kt @@ -0,0 +1,26 @@ +// TARGET_BACKEND: JVM_IR +// IGNORE_BACKEND_K1: JVM_IR +// IGNORE_BACKEND_K2: JVM_IR +// tried to access field B.f from class TestKt + +// FILE: A.java + +public class A { + String f = "OK"; +} + +// FILE: B.kt + +open class B : A() { + private val f = "FAIL" +} + +// FILE: C.java + +public class C extends B {} + +// FILE: test.kt + +fun box(): String { + return C().f +} diff --git a/compiler/testData/codegen/box/fieldRename/javaFieldKotlinPropertyJavaPackagePrivate.kt b/compiler/testData/codegen/box/fieldRename/javaFieldKotlinPropertyJavaPackagePrivate.kt new file mode 100644 index 00000000000..a1fd63f5737 --- /dev/null +++ b/compiler/testData/codegen/box/fieldRename/javaFieldKotlinPropertyJavaPackagePrivate.kt @@ -0,0 +1,38 @@ +// TARGET_BACKEND: JVM_IR +// IGNORE_BACKEND_K1: JVM_IR +// IGNORE_BACKEND_K2: JVM_IR +// tried to access field base.B.f from class TestKt + +// FILE: Y.java + +package base; + +class Y { + public String f = "OK"; +} + +// FILE: A.java + +package base; + +public class A extends Y {} + +// FILE: B.kt + +package base + +open class B : A() { + private val f = "FAIL" +} + +// FILE: C.java + +import base.B; + +public class C extends B {} + +// FILE: test.kt + +fun box(): String { + return C().f +} diff --git a/compiler/testData/codegen/box/fieldRename/javaInvisibleFieldAndKotlinProperty.kt b/compiler/testData/codegen/box/fieldRename/javaInvisibleFieldAndKotlinProperty.kt new file mode 100644 index 00000000000..b351703142e --- /dev/null +++ b/compiler/testData/codegen/box/fieldRename/javaInvisibleFieldAndKotlinProperty.kt @@ -0,0 +1,23 @@ +// TARGET_BACKEND: JVM_IR +// Field VS property: case 2.1 +// See KT-54393 for details + +// FILE: BaseJava.java +public class BaseJava { + private String a = "FAIL"; +} + +// FILE: Derived.kt +class Derived : BaseJava() { + var a = "OK" +} + +fun box(): String { + val first = Derived().a + if (first != "OK") return first + val d = Derived() + if (d::a.get() != "OK") return d::a.get() + d.a = "12" + if (d.a != "12") return "Error writing: ${d.a}" + return "OK" +} diff --git a/compiler/testData/codegen/box/fieldRename/javaInvisibleFieldAndKotlinPropertyReference.kt b/compiler/testData/codegen/box/fieldRename/javaInvisibleFieldAndKotlinPropertyReference.kt new file mode 100644 index 00000000000..dc52056cc43 --- /dev/null +++ b/compiler/testData/codegen/box/fieldRename/javaInvisibleFieldAndKotlinPropertyReference.kt @@ -0,0 +1,25 @@ +// TARGET_BACKEND: JVM_IR +// Field VS property: case "reference", field is invisible + +// FILE: BaseJava.java + +package base; + +public class BaseJava { + String a = "FAIL"; +} + +// FILE: Derived.kt + +package derived + +import base.BaseJava + +class Derived : BaseJava() { + val a = "OK" +} + +fun box(): String { + val d = Derived() + return d::a.get() +} diff --git a/compiler/testData/codegen/box/fieldRename/javaProtectedFieldAndKotlinInvisibleProperty.kt b/compiler/testData/codegen/box/fieldRename/javaProtectedFieldAndKotlinInvisibleProperty.kt new file mode 100644 index 00000000000..cb09445955a --- /dev/null +++ b/compiler/testData/codegen/box/fieldRename/javaProtectedFieldAndKotlinInvisibleProperty.kt @@ -0,0 +1,32 @@ +// 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 = "OK"; +} + +// FILE: Derived.kt + +package derived + +import base.BaseJava + +open class Intermediate : BaseJava() { + private val a = "FAIL" +} + +class Derived : Intermediate() { + fun foo() = a +} + +fun box(): String { + val d = Derived() + return d.foo() +} diff --git a/compiler/testData/codegen/box/fieldRename/javaProtectedFieldAndKotlinInvisiblePropertyReference.kt b/compiler/testData/codegen/box/fieldRename/javaProtectedFieldAndKotlinInvisiblePropertyReference.kt new file mode 100644 index 00000000000..bdde63b42ec --- /dev/null +++ b/compiler/testData/codegen/box/fieldRename/javaProtectedFieldAndKotlinInvisiblePropertyReference.kt @@ -0,0 +1,31 @@ +// 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 + +package base; + +public class BaseJava { + protected String a = "OK"; +} + +// FILE: Derived.kt + +package base +// Note: this test should report an error when we are in different package + +open class Intermediate : BaseJava() { + private val a = "FAIL" +} + +class Derived : Intermediate() { + fun foo() = this::a.get() +} + +fun box(): String { + val d = Derived() + return d.foo() +} diff --git a/compiler/testData/codegen/box/fieldRename/javaProtectedFieldAndKotlinInvisiblePropertyWrite.kt b/compiler/testData/codegen/box/fieldRename/javaProtectedFieldAndKotlinInvisiblePropertyWrite.kt new file mode 100644 index 00000000000..14e6ef94629 --- /dev/null +++ b/compiler/testData/codegen/box/fieldRename/javaProtectedFieldAndKotlinInvisiblePropertyWrite.kt @@ -0,0 +1,37 @@ +// 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() +} diff --git a/compiler/testData/codegen/box/fieldRename/javaProtectedFieldAndKotlinPropertyReference.kt b/compiler/testData/codegen/box/fieldRename/javaProtectedFieldAndKotlinPropertyReference.kt new file mode 100644 index 00000000000..d96a81ab3ba --- /dev/null +++ b/compiler/testData/codegen/box/fieldRename/javaProtectedFieldAndKotlinPropertyReference.kt @@ -0,0 +1,20 @@ +// TARGET_BACKEND: JVM_IR +// IGNORE_BACKEND_K1: JVM_IR +// Field VS property: case "reference", protected field, visible property + +// FILE: BaseJava.java +public class BaseJava { + protected String a = "FAIL"; +} + +// FILE: Derived.kt +class Derived : BaseJava() { + private val a = "OK" + + fun foo() = this::a.get() +} + +fun box(): String { + val d = Derived() + return d.foo() +} diff --git a/compiler/testData/codegen/box/fieldRename/javaProtectedFieldAnotherPackageReference.kt b/compiler/testData/codegen/box/fieldRename/javaProtectedFieldAnotherPackageReference.kt new file mode 100644 index 00000000000..120bc26a538 --- /dev/null +++ b/compiler/testData/codegen/box/fieldRename/javaProtectedFieldAnotherPackageReference.kt @@ -0,0 +1,24 @@ +// TARGET_BACKEND: JVM_IR + +// FILE: BaseJava.java + +package base; + +public class BaseJava { + protected String a = "OK"; +} + +// FILE: Derived.kt + +package derived + +import base.BaseJava + +class Derived : BaseJava() { + fun foo() = ::a.get() +} + +fun box(): String { + val d = Derived() + return d.foo() +} diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java index c878a1c434a..87a0e291b73 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java @@ -17951,12 +17951,42 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/fieldRename/genericPropertyWithItself.kt"); } + @Test + @TestMetadata("javaCloseFieldAndKotlinProperty.kt") + public void testJavaCloseFieldAndKotlinProperty() throws Exception { + runTest("compiler/testData/codegen/box/fieldRename/javaCloseFieldAndKotlinProperty.kt"); + } + @Test @TestMetadata("javaFieldAndCompanionProperty.kt") public void testJavaFieldAndCompanionProperty() throws Exception { runTest("compiler/testData/codegen/box/fieldRename/javaFieldAndCompanionProperty.kt"); } + @Test + @TestMetadata("javaFieldAndKotlinInvisibleProperty.kt") + public void testJavaFieldAndKotlinInvisibleProperty() throws Exception { + runTest("compiler/testData/codegen/box/fieldRename/javaFieldAndKotlinInvisibleProperty.kt"); + } + + @Test + @TestMetadata("javaFieldAndKotlinInvisiblePropertyViaTypeParameter.kt") + public void testJavaFieldAndKotlinInvisiblePropertyViaTypeParameter() throws Exception { + runTest("compiler/testData/codegen/box/fieldRename/javaFieldAndKotlinInvisiblePropertyViaTypeParameter.kt"); + } + + @Test + @TestMetadata("javaFieldAndKotlinInvisiblePropertyWithGetter.kt") + public void testJavaFieldAndKotlinInvisiblePropertyWithGetter() throws Exception { + runTest("compiler/testData/codegen/box/fieldRename/javaFieldAndKotlinInvisiblePropertyWithGetter.kt"); + } + + @Test + @TestMetadata("javaFieldAndKotlinLateinitProperty.kt") + public void testJavaFieldAndKotlinLateinitProperty() throws Exception { + runTest("compiler/testData/codegen/box/fieldRename/javaFieldAndKotlinLateinitProperty.kt"); + } + @Test @TestMetadata("javaFieldAndKotlinPrivateProperty.kt") public void testJavaFieldAndKotlinPrivateProperty() throws Exception { @@ -17969,6 +17999,90 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/fieldRename/javaFieldAndKotlinProperty.kt"); } + @Test + @TestMetadata("javaFieldAndKotlinPropertyReference.kt") + public void testJavaFieldAndKotlinPropertyReference() throws Exception { + runTest("compiler/testData/codegen/box/fieldRename/javaFieldAndKotlinPropertyReference.kt"); + } + + @Test + @TestMetadata("javaFieldAndKotlinPropertyReferenceFromInner.kt") + public void testJavaFieldAndKotlinPropertyReferenceFromInner() throws Exception { + runTest("compiler/testData/codegen/box/fieldRename/javaFieldAndKotlinPropertyReferenceFromInner.kt"); + } + + @Test + @TestMetadata("javaFieldAndKotlinPropertySuperAccess.kt") + public void testJavaFieldAndKotlinPropertySuperAccess() throws Exception { + runTest("compiler/testData/codegen/box/fieldRename/javaFieldAndKotlinPropertySuperAccess.kt"); + } + + @Test + @TestMetadata("javaFieldAndKotlinPropertyWithComplexReceiver.kt") + public void testJavaFieldAndKotlinPropertyWithComplexReceiver() throws Exception { + runTest("compiler/testData/codegen/box/fieldRename/javaFieldAndKotlinPropertyWithComplexReceiver.kt"); + } + + @Test + @TestMetadata("javaFieldAndKotlinPropertyWithSmartcast.kt") + public void testJavaFieldAndKotlinPropertyWithSmartcast() throws Exception { + runTest("compiler/testData/codegen/box/fieldRename/javaFieldAndKotlinPropertyWithSmartcast.kt"); + } + + @Test + @TestMetadata("javaFieldKotlinPropertyJavaField.kt") + public void testJavaFieldKotlinPropertyJavaField() throws Exception { + runTest("compiler/testData/codegen/box/fieldRename/javaFieldKotlinPropertyJavaField.kt"); + } + + @Test + @TestMetadata("javaFieldKotlinPropertyJavaPackagePrivate.kt") + public void testJavaFieldKotlinPropertyJavaPackagePrivate() throws Exception { + runTest("compiler/testData/codegen/box/fieldRename/javaFieldKotlinPropertyJavaPackagePrivate.kt"); + } + + @Test + @TestMetadata("javaInvisibleFieldAndKotlinProperty.kt") + public void testJavaInvisibleFieldAndKotlinProperty() throws Exception { + runTest("compiler/testData/codegen/box/fieldRename/javaInvisibleFieldAndKotlinProperty.kt"); + } + + @Test + @TestMetadata("javaInvisibleFieldAndKotlinPropertyReference.kt") + public void testJavaInvisibleFieldAndKotlinPropertyReference() throws Exception { + runTest("compiler/testData/codegen/box/fieldRename/javaInvisibleFieldAndKotlinPropertyReference.kt"); + } + + @Test + @TestMetadata("javaProtectedFieldAndKotlinInvisibleProperty.kt") + public void testJavaProtectedFieldAndKotlinInvisibleProperty() throws Exception { + runTest("compiler/testData/codegen/box/fieldRename/javaProtectedFieldAndKotlinInvisibleProperty.kt"); + } + + @Test + @TestMetadata("javaProtectedFieldAndKotlinInvisiblePropertyReference.kt") + public void testJavaProtectedFieldAndKotlinInvisiblePropertyReference() throws Exception { + runTest("compiler/testData/codegen/box/fieldRename/javaProtectedFieldAndKotlinInvisiblePropertyReference.kt"); + } + + @Test + @TestMetadata("javaProtectedFieldAndKotlinInvisiblePropertyWrite.kt") + public void testJavaProtectedFieldAndKotlinInvisiblePropertyWrite() throws Exception { + runTest("compiler/testData/codegen/box/fieldRename/javaProtectedFieldAndKotlinInvisiblePropertyWrite.kt"); + } + + @Test + @TestMetadata("javaProtectedFieldAndKotlinPropertyReference.kt") + public void testJavaProtectedFieldAndKotlinPropertyReference() throws Exception { + runTest("compiler/testData/codegen/box/fieldRename/javaProtectedFieldAndKotlinPropertyReference.kt"); + } + + @Test + @TestMetadata("javaProtectedFieldAnotherPackageReference.kt") + public void testJavaProtectedFieldAnotherPackageReference() throws Exception { + runTest("compiler/testData/codegen/box/fieldRename/javaProtectedFieldAnotherPackageReference.kt"); + } + @Test @TestMetadata("jvmFieldNoClash1.kt") public void testJvmFieldNoClash1() throws Exception {