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 e7ce70fcedc..b6ff5922c46 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 @@ -42640,6 +42640,18 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTest("compiler/testData/codegen/box/syntheticAccessors/kt48954.kt"); } + @Test + @TestMetadata("kt49316.kt") + public void testKt49316() throws Exception { + runTest("compiler/testData/codegen/box/syntheticAccessors/kt49316.kt"); + } + + @Test + @TestMetadata("kt49316a.kt") + public void testKt49316a() throws Exception { + runTest("compiler/testData/codegen/box/syntheticAccessors/kt49316a.kt"); + } + @Test @TestMetadata("kt9717.kt") public void testKt9717() throws Exception { diff --git a/compiler/ir/backend.jvm/lower/src/org/jetbrains/kotlin/backend/jvm/lower/SyntheticAccessorLowering.kt b/compiler/ir/backend.jvm/lower/src/org/jetbrains/kotlin/backend/jvm/lower/SyntheticAccessorLowering.kt index 937d48e4ca1..33f4b4527be 100644 --- a/compiler/ir/backend.jvm/lower/src/org/jetbrains/kotlin/backend/jvm/lower/SyntheticAccessorLowering.kt +++ b/compiler/ir/backend.jvm/lower/src/org/jetbrains/kotlin/backend/jvm/lower/SyntheticAccessorLowering.kt @@ -58,6 +58,7 @@ private class SyntheticAccessorTransformer( val inlineScopeResolver: IrInlineScopeResolver, val pendingAccessorsToAdd: MutableList ) : IrElementTransformerVoidWithContext() { + private data class FieldKey(val fieldSymbol: IrFieldSymbol, val parent: IrDeclarationParent, val superQualifierSymbol: IrClassSymbol?) private data class FunctionKey( @@ -245,21 +246,30 @@ private class SyntheticAccessorTransformer( } override fun visitSetField(expression: IrSetField): IrExpression { + // FE accepts code that assigns to a val of this or other class if it happens in unreachable code (KT-35565). + // Sometimes this can cause internal error in the BE (see KT-49316). + // Assume that 'val' property with a backing field can never be initialized from a context that requires synthetic accessor. + val correspondingProperty = expression.symbol.owner.correspondingPropertySymbol?.owner + if (correspondingProperty != null && !correspondingProperty.isVar) { + return super.visitExpression(expression) + } + val dispatchReceiverType = expression.receiver?.type val dispatchReceiverClassSymbol = dispatchReceiverType?.classifierOrNull as? IrClassSymbol + if (expression.symbol.isAccessible(false, dispatchReceiverClassSymbol)) { + return super.visitExpression(expression) + } + + val symbol = expression.symbol + val parent = symbol.owner.accessorParent(dispatchReceiverClassSymbol?.owner ?: symbol.owner.parent) as IrClass + return super.visitExpression( - if (!expression.symbol.isAccessible(false, dispatchReceiverClassSymbol)) { - val symbol = expression.symbol - val parent = symbol.owner.accessorParent(dispatchReceiverClassSymbol?.owner ?: symbol.owner.parent) as IrClass - modifySetterExpression( - expression, - setterMap.getOrPut(FieldKey(symbol, parent, expression.superQualifierSymbol)) { - makeSetterAccessorSymbol(symbol, parent, expression.superQualifierSymbol) - } - ) - } else { - expression - } + modifySetterExpression( + expression, + setterMap.getOrPut(FieldKey(symbol, parent, expression.superQualifierSymbol)) { + makeSetterAccessorSymbol(symbol, parent, expression.superQualifierSymbol) + } + ) ) } diff --git a/compiler/testData/codegen/box/syntheticAccessors/kt49316.kt b/compiler/testData/codegen/box/syntheticAccessors/kt49316.kt new file mode 100644 index 00000000000..d713211e725 --- /dev/null +++ b/compiler/testData/codegen/box/syntheticAccessors/kt49316.kt @@ -0,0 +1,27 @@ +// IGNORE_BACKEND: JS +// java.lang.AssertionError +// at org.jetbrains.kotlin.js.translate.context.TranslationContext.getDispatchReceiver(TranslationContext.java:590) +// at org.jetbrains.kotlin.js.translate.utils.TranslationUtils.backingFieldReference(TranslationUtils.java:237) +// at org.jetbrains.kotlin.js.translate.utils.TranslationUtils.assignmentToBackingField(TranslationUtils.java:250) +// at org.jetbrains.kotlin.js.translate.reference.BackingFieldAccessTranslator.translateAsSet(BackingFieldAccessTranslator.java:60) +// ... + +// FILE: kt49316.kt +import a.* + +// This test should become irrelevant after KT-35565 is fixed. + +fun test(foo: Foo): String { + return foo.s + + // VAL_REASSIGNMENT not reported in unreachable code. + // Make sure there's no BE internal error here. + foo.s = "oops" +} + +fun box() = test(Foo("OK")) + +// FILE: Foo.kt +package a + +class Foo(val s: String) diff --git a/compiler/testData/codegen/box/syntheticAccessors/kt49316a.kt b/compiler/testData/codegen/box/syntheticAccessors/kt49316a.kt new file mode 100644 index 00000000000..338a688fe66 --- /dev/null +++ b/compiler/testData/codegen/box/syntheticAccessors/kt49316a.kt @@ -0,0 +1,17 @@ +// TARGET_BACKEND: JVM + +// This test should become irrelevant after KT-35565 is fixed. + +fun test(foo: Foo): String { + return foo.s + + // VAL_REASSIGNMENT not reported in unreachable code. + // Make sure there's no BE internal error here. + foo.s = "oops" +} + +// CHECK_BYTECODE_LISTING +// - there should be no synthetic accessor generated in 'Foo' +class Foo(val s: String) + +fun box() = test(Foo("OK")) diff --git a/compiler/testData/codegen/box/syntheticAccessors/kt49316a.txt b/compiler/testData/codegen/box/syntheticAccessors/kt49316a.txt new file mode 100644 index 00000000000..2b1a4e39c5c --- /dev/null +++ b/compiler/testData/codegen/box/syntheticAccessors/kt49316a.txt @@ -0,0 +1,14 @@ +@kotlin.Metadata +public final class Foo { + // source: 'kt49316a.kt' + private final @org.jetbrains.annotations.NotNull field s: java.lang.String + public method (@org.jetbrains.annotations.NotNull p0: java.lang.String): void + public final @org.jetbrains.annotations.NotNull method getS(): java.lang.String +} + +@kotlin.Metadata +public final class Kt49316aKt { + // source: 'kt49316a.kt' + public final static @org.jetbrains.annotations.NotNull method box(): java.lang.String + public final static @org.jetbrains.annotations.NotNull method test(@org.jetbrains.annotations.NotNull p0: Foo): java.lang.String +} 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 636e6a17753..d5f211199e0 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 @@ -42472,6 +42472,18 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/syntheticAccessors/kt48954.kt"); } + @Test + @TestMetadata("kt49316.kt") + public void testKt49316() throws Exception { + runTest("compiler/testData/codegen/box/syntheticAccessors/kt49316.kt"); + } + + @Test + @TestMetadata("kt49316a.kt") + public void testKt49316a() throws Exception { + runTest("compiler/testData/codegen/box/syntheticAccessors/kt49316a.kt"); + } + @Test @TestMetadata("kt9717.kt") public void testKt9717() 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 d1bd575bb85..21a5f397f2c 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 @@ -42640,6 +42640,18 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/syntheticAccessors/kt48954.kt"); } + @Test + @TestMetadata("kt49316.kt") + public void testKt49316() throws Exception { + runTest("compiler/testData/codegen/box/syntheticAccessors/kt49316.kt"); + } + + @Test + @TestMetadata("kt49316a.kt") + public void testKt49316a() throws Exception { + runTest("compiler/testData/codegen/box/syntheticAccessors/kt49316a.kt"); + } + @Test @TestMetadata("kt9717.kt") public void testKt9717() 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 7e1d97f860b..b045735fcb5 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -34207,6 +34207,16 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/syntheticAccessors/kt48954.kt"); } + @TestMetadata("kt49316.kt") + public void testKt49316() throws Exception { + runTest("compiler/testData/codegen/box/syntheticAccessors/kt49316.kt"); + } + + @TestMetadata("kt49316a.kt") + public void testKt49316a() throws Exception { + runTest("compiler/testData/codegen/box/syntheticAccessors/kt49316a.kt"); + } + @TestMetadata("kt9717.kt") public void testKt9717() throws Exception { runTest("compiler/testData/codegen/box/syntheticAccessors/kt9717.kt"); diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java index 572584b8c91..0adca1bcfdd 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java @@ -28382,6 +28382,11 @@ public class IrJsCodegenBoxES6TestGenerated extends AbstractIrJsCodegenBoxES6Tes runTest("compiler/testData/codegen/box/syntheticAccessors/kt21258_simple.kt"); } + @TestMetadata("kt49316.kt") + public void testKt49316() throws Exception { + runTest("compiler/testData/codegen/box/syntheticAccessors/kt49316.kt"); + } + @TestMetadata("kt9717.kt") public void testKt9717() throws Exception { runTest("compiler/testData/codegen/box/syntheticAccessors/kt9717.kt"); diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java index f8b7b17d4f5..7d36defa427 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java @@ -27788,6 +27788,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { runTest("compiler/testData/codegen/box/syntheticAccessors/kt21258_simple.kt"); } + @TestMetadata("kt49316.kt") + public void testKt49316() throws Exception { + runTest("compiler/testData/codegen/box/syntheticAccessors/kt49316.kt"); + } + @TestMetadata("kt9717.kt") public void testKt9717() throws Exception { runTest("compiler/testData/codegen/box/syntheticAccessors/kt9717.kt"); diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/wasm/semantics/IrCodegenBoxWasmTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/wasm/semantics/IrCodegenBoxWasmTestGenerated.java index dc5b9da3430..a4f4ad140fe 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/wasm/semantics/IrCodegenBoxWasmTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/wasm/semantics/IrCodegenBoxWasmTestGenerated.java @@ -24142,6 +24142,11 @@ public class IrCodegenBoxWasmTestGenerated extends AbstractIrCodegenBoxWasmTest runTest("compiler/testData/codegen/box/syntheticAccessors/kt21258_simple.kt"); } + @TestMetadata("kt49316.kt") + public void testKt49316() throws Exception { + runTest("compiler/testData/codegen/box/syntheticAccessors/kt49316.kt"); + } + @TestMetadata("kt9717.kt") public void testKt9717() throws Exception { runTest("compiler/testData/codegen/box/syntheticAccessors/kt9717.kt"); diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/testNew/JsCodegenBoxTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/testNew/JsCodegenBoxTestGenerated.java index b5825de059f..840af4df2c4 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/testNew/JsCodegenBoxTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/testNew/JsCodegenBoxTestGenerated.java @@ -30864,6 +30864,12 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { runTest("compiler/testData/codegen/box/syntheticAccessors/kt21258_simple.kt"); } + @Test + @TestMetadata("kt49316.kt") + public void testKt49316() throws Exception { + runTest("compiler/testData/codegen/box/syntheticAccessors/kt49316.kt"); + } + @Test @TestMetadata("kt9717.kt") public void testKt9717() throws Exception {