From a7859e0332dc8347041e604e368aa418c6b44c5b Mon Sep 17 00:00:00 2001 From: "Denis.Zharkov" Date: Wed, 10 Nov 2021 18:15:22 +0300 Subject: [PATCH] FIR: Fix wrong ACCIDENTAL_OVERRIDE on fake override property with JvmName --- .../fir/backend/Fir2IrDeclarationStorage.kt | 27 ++++++++++++------- .../FirBlackBoxCodegenTestGenerated.java | 6 +++++ .../box/jvmName/fakeOverrideOfProperty.kt | 23 ++++++++++++++++ .../codegen/BlackBoxCodegenTestGenerated.java | 6 +++++ .../IrBlackBoxCodegenTestGenerated.java | 6 +++++ .../LightAnalysisModeTestGenerated.java | 5 ++++ .../NativeExtBlackBoxTestGenerated.java | 6 +++++ 7 files changed, 69 insertions(+), 10 deletions(-) create mode 100644 compiler/testData/codegen/box/jvmName/fakeOverrideOfProperty.kt diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrDeclarationStorage.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrDeclarationStorage.kt index b77bc524504..3d900cbe4f5 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrDeclarationStorage.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrDeclarationStorage.kt @@ -504,7 +504,7 @@ class Fir2IrDeclarationStorage( containerSource = simpleFunction?.containerSource, ).apply { metadata = FirMetadataSource.Function(function) - convertAnnotationsFromLibrary(function) + convertAnnotationsForNonDeclaredMembers(function, origin) enterScope(this) bindAndDeclareParameters( function, irParent, @@ -626,6 +626,7 @@ class Fir2IrDeclarationStorage( endOffset: Int, isLocal: Boolean = false, containingClass: ConeClassLikeLookupTag? = null, + propertyAccessorForAnnotations: FirPropertyAccessor? = propertyAccessor, ): IrSimpleFunction = convertCatching(propertyAccessor ?: property) { val prefix = if (isSetter) "set" else "get" val signature = if (isLocal) null else signatureComposer.composeAccessorSignature(property, isSetter, containingClass) @@ -654,7 +655,11 @@ class Fir2IrDeclarationStorage( if (propertyAccessor != null) { metadata = FirMetadataSource.Function(propertyAccessor) // Note that deserialized annotations are stored in the accessor, not the property. - convertAnnotationsFromLibrary(propertyAccessor) + convertAnnotationsForNonDeclaredMembers(propertyAccessor, origin) + } + + if (propertyAccessorForAnnotations != null) { + convertAnnotationsForNonDeclaredMembers(propertyAccessorForAnnotations, origin) } with(classifierStorage) { setTypeParameters( @@ -710,7 +715,7 @@ class Fir2IrDeclarationStorage( it.correspondingPropertySymbol = this@createBackingField.symbol }.apply { metadata = FirMetadataSource.Property(property) - convertAnnotationsFromLibrary(property) + convertAnnotationsForNonDeclaredMembers(property, origin) } } } @@ -812,7 +817,7 @@ class Fir2IrDeclarationStorage( containerSource = property.containerSource, ).apply { metadata = FirMetadataSource.Property(property) - convertAnnotationsFromLibrary(property) + convertAnnotationsForNonDeclaredMembers(property, origin) enterScope(this) if (irParent != null) { parent = irParent @@ -860,7 +865,8 @@ class Fir2IrDeclarationStorage( getter is FirDefaultPropertyGetter -> IrDeclarationOrigin.DEFAULT_PROPERTY_ACCESSOR else -> origin }, - startOffset, endOffset, isLocal, containingClass + startOffset, endOffset, isLocal, containingClass, + property.unwrapFakeOverrides().getter, ) if (property.isVar) { this.setter = createIrPropertyAccessor( @@ -870,7 +876,8 @@ class Fir2IrDeclarationStorage( setter is FirDefaultPropertySetter -> IrDeclarationOrigin.DEFAULT_PROPERTY_ACCESSOR else -> origin }, - startOffset, endOffset, isLocal, containingClass + startOffset, endOffset, isLocal, containingClass, + property.unwrapFakeOverrides().setter, ) } leaveScope(this) @@ -1403,10 +1410,10 @@ class Fir2IrDeclarationStorage( } } - private fun IrMutableAnnotationContainer.convertAnnotationsFromLibrary(firAnnotationContainer: FirAnnotationContainer) { - if ((firAnnotationContainer as? FirDeclaration)?.isFromLibrary == true || - (firAnnotationContainer is FirCallableDeclaration && firAnnotationContainer.isSubstitutionOrIntersectionOverride) - ) { + private fun IrMutableAnnotationContainer.convertAnnotationsForNonDeclaredMembers( + firAnnotationContainer: FirAnnotationContainer, origin: IrDeclarationOrigin, + ) { + if ((firAnnotationContainer as? FirDeclaration)?.isFromLibrary == true || origin == IrDeclarationOrigin.FAKE_OVERRIDE) { annotationGenerator.generate(this, firAnnotationContainer) } } 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 383a9155f4a..743a440ef49 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 @@ -25804,6 +25804,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTest("compiler/testData/codegen/box/jvmName/fakeJvmNameInJava.kt"); } + @Test + @TestMetadata("fakeOverrideOfProperty.kt") + public void testFakeOverrideOfProperty() throws Exception { + runTest("compiler/testData/codegen/box/jvmName/fakeOverrideOfProperty.kt"); + } + @Test @TestMetadata("functionName.kt") public void testFunctionName() throws Exception { diff --git a/compiler/testData/codegen/box/jvmName/fakeOverrideOfProperty.kt b/compiler/testData/codegen/box/jvmName/fakeOverrideOfProperty.kt new file mode 100644 index 00000000000..ad5f7100044 --- /dev/null +++ b/compiler/testData/codegen/box/jvmName/fakeOverrideOfProperty.kt @@ -0,0 +1,23 @@ +// TARGET_BACKEND: JVM +// WITH_RUNTIME + +abstract class Base { + protected abstract fun getChart(context: CharSequence): String + + @get:JvmName("getChartHelper") + public val CharSequence.chart get() = getChart(this) +} + +abstract class Derived1 : Base() + +class Derived2 : Derived1() { + override fun getChart(context: CharSequence): String { + return context.toString() + } +} + +fun box(): String { + return with(Derived2()) { + "OK".chart + } +} diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java index 6923029c7cd..538e3b0a749 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java @@ -25642,6 +25642,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/jvmName/fakeJvmNameInJava.kt"); } + @Test + @TestMetadata("fakeOverrideOfProperty.kt") + public void testFakeOverrideOfProperty() throws Exception { + runTest("compiler/testData/codegen/box/jvmName/fakeOverrideOfProperty.kt"); + } + @Test @TestMetadata("functionName.kt") public void testFunctionName() throws Exception { 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 5408bac70b5..23663b4117f 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 @@ -25804,6 +25804,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/jvmName/fakeJvmNameInJava.kt"); } + @Test + @TestMetadata("fakeOverrideOfProperty.kt") + public void testFakeOverrideOfProperty() throws Exception { + runTest("compiler/testData/codegen/box/jvmName/fakeOverrideOfProperty.kt"); + } + @Test @TestMetadata("functionName.kt") public void testFunctionName() throws Exception { diff --git a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index fc4d989fa9a..49271e25bcc 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -21628,6 +21628,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/jvmName/fakeJvmNameInJava.kt"); } + @TestMetadata("fakeOverrideOfProperty.kt") + public void testFakeOverrideOfProperty() throws Exception { + runTest("compiler/testData/codegen/box/jvmName/fakeOverrideOfProperty.kt"); + } + @TestMetadata("functionName.kt") public void testFunctionName() throws Exception { runTest("compiler/testData/codegen/box/jvmName/functionName.kt"); diff --git a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/NativeExtBlackBoxTestGenerated.java b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/NativeExtBlackBoxTestGenerated.java index c5d37cfc4f7..312b5ba4d09 100644 --- a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/NativeExtBlackBoxTestGenerated.java +++ b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/NativeExtBlackBoxTestGenerated.java @@ -26115,6 +26115,12 @@ public class NativeExtBlackBoxTestGenerated extends AbstractNativeBlackBoxTest { runTest("compiler/testData/codegen/box/jvmName/fakeJvmNameInJava.kt"); } + @Test + @TestMetadata("fakeOverrideOfProperty.kt") + public void testFakeOverrideOfProperty() throws Exception { + runTest("compiler/testData/codegen/box/jvmName/fakeOverrideOfProperty.kt"); + } + @Test @TestMetadata("functionName.kt") public void testFunctionName() throws Exception {