From 5684e694b5e5519bb76acd33a1d52ce3b0199717 Mon Sep 17 00:00:00 2001 From: Georgy Bronnikov Date: Fri, 26 Jun 2020 15:34:16 +0300 Subject: [PATCH] JVM_IR: handle diamond inheritance for Java fields --- .../ir/FirBlackBoxCodegenTestGenerated.java | 5 +++++ .../src/org/jetbrains/kotlin/psi2ir/KotlinUtils.kt | 9 +++++---- compiler/testData/codegen/box/jvmField/diamond.kt | 14 ++++++++++++++ .../codegen/BlackBoxCodegenTestGenerated.java | 5 +++++ .../codegen/LightAnalysisModeTestGenerated.java | 5 +++++ .../codegen/ir/IrBlackBoxCodegenTestGenerated.java | 5 +++++ 6 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 compiler/testData/codegen/box/jvmField/diamond.kt diff --git a/compiler/fir/fir2ir/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java b/compiler/fir/fir2ir/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java index 2b21bbf2f85..999c2237bd8 100644 --- a/compiler/fir/fir2ir/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java +++ b/compiler/fir/fir2ir/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java @@ -16014,6 +16014,11 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTest("compiler/testData/codegen/box/jvmField/constructorProperty.kt"); } + @TestMetadata("diamond.kt") + public void testDiamond() throws Exception { + runTest("compiler/testData/codegen/box/jvmField/diamond.kt"); + } + @TestMetadata("fileOrder.kt") public void testFileOrder() throws Exception { runTest("compiler/testData/codegen/box/jvmField/fileOrder.kt"); diff --git a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/KotlinUtils.kt b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/KotlinUtils.kt index 37e3d6f4c70..402f0dd7fdd 100644 --- a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/KotlinUtils.kt +++ b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/KotlinUtils.kt @@ -79,12 +79,13 @@ val PropertyDescriptor.unwrappedSetMethod: FunctionDescriptor? // Only works for descriptors of Java fields. internal fun PropertyDescriptor.resolveFakeOverride(): PropertyDescriptor { - assert(getter == null) - // Fields can only be inherited from objects, so there will be at most one overridden descriptor at each step. + assert(getter == null) { "resolveFakeOverride should only be called for Java fields, got $this"} var current = this while (current.kind == CallableMemberDescriptor.Kind.FAKE_OVERRIDE) { - current = current.overriddenDescriptors.singleOrNull() - ?: error("Descriptor of Java field $current should have exactly one overridden descriptor, have ${current.overriddenDescriptors}") + current = current.overriddenDescriptors.singleOrNull { + (it.containingDeclaration as ClassDescriptor).kind != ClassKind.INTERFACE + } ?: current.overriddenDescriptors.firstOrNull() + ?: error("Fake override descriptor of Java field $current should has no overridden descriptors") } return current } diff --git a/compiler/testData/codegen/box/jvmField/diamond.kt b/compiler/testData/codegen/box/jvmField/diamond.kt new file mode 100644 index 00000000000..f53f3e274c4 --- /dev/null +++ b/compiler/testData/codegen/box/jvmField/diamond.kt @@ -0,0 +1,14 @@ +// TARGET_BACKEND: JVM +// IGNORE_BACKEND_FIR: JVM_IR +// FILE: A.java +public interface A { public String ok = "OK"; } +// FILE: B.java +public class B implements A {} +// FILE: C.java +public class C extends B implements A {} +// FILE: test.kt +class D: C() { + fun okay() = ok +} + +fun box() = D().okay() diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index 49330942b2f..f2611ad2bed 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -17239,6 +17239,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/jvmField/constructorProperty.kt"); } + @TestMetadata("diamond.kt") + public void testDiamond() throws Exception { + runTest("compiler/testData/codegen/box/jvmField/diamond.kt"); + } + @TestMetadata("fileOrder.kt") public void testFileOrder() throws Exception { runTest("compiler/testData/codegen/box/jvmField/fileOrder.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index d1e66cc9b0c..1587e7dc1c1 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -17239,6 +17239,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/jvmField/constructorProperty.kt"); } + @TestMetadata("diamond.kt") + public void testDiamond() throws Exception { + runTest("compiler/testData/codegen/box/jvmField/diamond.kt"); + } + @TestMetadata("fileOrder.kt") public void testFileOrder() throws Exception { runTest("compiler/testData/codegen/box/jvmField/fileOrder.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java index d8e562b4456..e60bf68d696 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -16014,6 +16014,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/jvmField/constructorProperty.kt"); } + @TestMetadata("diamond.kt") + public void testDiamond() throws Exception { + runTest("compiler/testData/codegen/box/jvmField/diamond.kt"); + } + @TestMetadata("fileOrder.kt") public void testFileOrder() throws Exception { runTest("compiler/testData/codegen/box/jvmField/fileOrder.kt");