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 5af8c18ae4c..7977f9116d2 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 @@ -14066,6 +14066,18 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTest("compiler/testData/codegen/box/enum/kt38996.kt"); } + @Test + @TestMetadata("kt44744.kt") + public void testKt44744() throws Exception { + runTest("compiler/testData/codegen/box/enum/kt44744.kt"); + } + + @Test + @TestMetadata("kt44744_innerClass.kt") + public void testKt44744_innerClass() throws Exception { + runTest("compiler/testData/codegen/box/enum/kt44744_innerClass.kt"); + } + @Test @TestMetadata("kt7257.kt") public void testKt7257() throws Exception { diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/SingletonReferencesLowering.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/SingletonReferencesLowering.kt index 155eaa07db6..717350a4a8c 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/SingletonReferencesLowering.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/SingletonReferencesLowering.kt @@ -5,14 +5,22 @@ package org.jetbrains.kotlin.backend.jvm.lower -import org.jetbrains.kotlin.backend.common.* +import org.jetbrains.kotlin.backend.common.FileLoweringPass +import org.jetbrains.kotlin.backend.common.IrElementTransformerVoidWithContext import org.jetbrains.kotlin.backend.common.phaser.makeIrFilePhase +import org.jetbrains.kotlin.backend.common.pop +import org.jetbrains.kotlin.backend.common.push import org.jetbrains.kotlin.backend.jvm.JvmBackendContext +import org.jetbrains.kotlin.descriptors.DescriptorVisibilities import org.jetbrains.kotlin.ir.declarations.* -import org.jetbrains.kotlin.ir.expressions.* +import org.jetbrains.kotlin.ir.expressions.IrEnumConstructorCall +import org.jetbrains.kotlin.ir.expressions.IrExpression +import org.jetbrains.kotlin.ir.expressions.IrGetEnumValue +import org.jetbrains.kotlin.ir.expressions.IrGetObjectValue import org.jetbrains.kotlin.ir.expressions.impl.IrGetFieldImpl import org.jetbrains.kotlin.ir.expressions.impl.IrGetValueImpl import org.jetbrains.kotlin.ir.types.classOrNull +import org.jetbrains.kotlin.ir.util.isAnonymousObject import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid internal val singletonReferencesPhase = makeIrFilePhase( @@ -38,7 +46,7 @@ private class SingletonReferencesLowering(val context: JvmBackendContext) : File override fun visitGetEnumValue(expression: IrGetEnumValue): IrExpression { val candidate = expression.symbol.owner.correspondingClass val appropriateThis = thisOfClass(candidate) - return if (candidate != null && appropriateThis != null && !isVisitingSuperConstructor(candidate)) { + return if (candidate != null && appropriateThis != null && isThisAccessible(candidate) && !isVisitingSuperConstructor(candidate)) { // Replace `SomeEnumClass.SomeEnumEntry` with `this`, if possible. // // SomeEnumEntry is a singleton, which is assigned (SETFIELD) to SomeEnumClass after the construction of the singleton is done. @@ -51,6 +59,25 @@ private class SingletonReferencesLowering(val context: JvmBackendContext) : File } } + private fun isThisAccessible(irClass: IrClass): Boolean { + for (scope in allScopes.asReversed()) { + when (val irScopeElement = scope.irElement) { + irClass -> + return true + is IrClass -> + if (!irScopeElement.isInner && !irScopeElement.isAnonymousObject) + return false + is IrField -> + if (irScopeElement.isStatic) + return false + is IrFunction -> + if (irScopeElement.dispatchReceiverParameter == null && irScopeElement.visibility != DescriptorVisibilities.LOCAL) + return false + } + } + return false + } + override fun visitGetObjectValue(expression: IrGetObjectValue): IrExpression { val instanceField = if (allScopes.any { it.irElement == expression.symbol.owner }) context.cachedDeclarations.getPrivateFieldForObjectInstance(expression.symbol.owner) // Constructor or static method. diff --git a/compiler/testData/codegen/box/enum/kt44744.kt b/compiler/testData/codegen/box/enum/kt44744.kt new file mode 100644 index 00000000000..71f2ddb5a0f --- /dev/null +++ b/compiler/testData/codegen/box/enum/kt44744.kt @@ -0,0 +1,22 @@ +enum class ContentType { + + PLAIN_TEXT { + override fun convert(text: String, targetType: ContentType): String { + return text + } + }, + + MARKDOWN { + override fun convert(text: String, targetType: ContentType): String { + return when (targetType) { + MARKDOWN -> text + PLAIN_TEXT -> "" + } + } + }; + + abstract fun convert(text: String, targetType: ContentType): String +} + +fun box() = + ContentType.PLAIN_TEXT.convert("OK", ContentType.PLAIN_TEXT) diff --git a/compiler/testData/codegen/box/enum/kt44744_innerClass.kt b/compiler/testData/codegen/box/enum/kt44744_innerClass.kt new file mode 100644 index 00000000000..63fd4b49873 --- /dev/null +++ b/compiler/testData/codegen/box/enum/kt44744_innerClass.kt @@ -0,0 +1,27 @@ +interface IFoo { + fun foo(e: En): String +} + +enum class En { + TEST { + inner class Nested : IFoo { + private val ee = TEST + + override fun foo(e: En): String { + return if (e == ee) e.ok else "Failed" + } + } + + override val ok: String get() = "OK" + override fun foo(): IFoo = Nested() + }, + OTHER { + override val ok: String get() = throw AssertionError() + override fun foo(): IFoo = throw AssertionError() + }; + + abstract val ok: String + abstract fun foo(): IFoo +} + +fun box() = En.TEST.foo().foo(En.TEST) 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 4f6e3666dba..448e0973027 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 @@ -14066,6 +14066,18 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/enum/kt38996.kt"); } + @Test + @TestMetadata("kt44744.kt") + public void testKt44744() throws Exception { + runTest("compiler/testData/codegen/box/enum/kt44744.kt"); + } + + @Test + @TestMetadata("kt44744_innerClass.kt") + public void testKt44744_innerClass() throws Exception { + runTest("compiler/testData/codegen/box/enum/kt44744_innerClass.kt"); + } + @Test @TestMetadata("kt7257.kt") public void testKt7257() 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 f69a36d0ea9..edbaaae964c 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 @@ -14066,6 +14066,18 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/enum/kt38996.kt"); } + @Test + @TestMetadata("kt44744.kt") + public void testKt44744() throws Exception { + runTest("compiler/testData/codegen/box/enum/kt44744.kt"); + } + + @Test + @TestMetadata("kt44744_innerClass.kt") + public void testKt44744_innerClass() throws Exception { + runTest("compiler/testData/codegen/box/enum/kt44744_innerClass.kt"); + } + @Test @TestMetadata("kt7257.kt") public void testKt7257() 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 f7df1910994..6207429a062 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -11553,6 +11553,16 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/enum/kt38996.kt"); } + @TestMetadata("kt44744.kt") + public void testKt44744() throws Exception { + runTest("compiler/testData/codegen/box/enum/kt44744.kt"); + } + + @TestMetadata("kt44744_innerClass.kt") + public void testKt44744_innerClass() throws Exception { + runTest("compiler/testData/codegen/box/enum/kt44744_innerClass.kt"); + } + @TestMetadata("kt7257.kt") public void testKt7257() throws Exception { runTest("compiler/testData/codegen/box/enum/kt7257.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 8e617730727..85b09dbe886 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 @@ -10321,6 +10321,16 @@ public class IrJsCodegenBoxES6TestGenerated extends AbstractIrJsCodegenBoxES6Tes runTest("compiler/testData/codegen/box/enum/kt38996.kt"); } + @TestMetadata("kt44744.kt") + public void testKt44744() throws Exception { + runTest("compiler/testData/codegen/box/enum/kt44744.kt"); + } + + @TestMetadata("kt44744_innerClass.kt") + public void testKt44744_innerClass() throws Exception { + runTest("compiler/testData/codegen/box/enum/kt44744_innerClass.kt"); + } + @TestMetadata("kt7257.kt") public void testKt7257() throws Exception { runTest("compiler/testData/codegen/box/enum/kt7257.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 c1f8b450f72..d2f2bec4e59 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 @@ -9778,6 +9778,16 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { runTest("compiler/testData/codegen/box/enum/kt38996.kt"); } + @TestMetadata("kt44744.kt") + public void testKt44744() throws Exception { + runTest("compiler/testData/codegen/box/enum/kt44744.kt"); + } + + @TestMetadata("kt44744_innerClass.kt") + public void testKt44744_innerClass() throws Exception { + runTest("compiler/testData/codegen/box/enum/kt44744_innerClass.kt"); + } + @TestMetadata("kt7257.kt") public void testKt7257() throws Exception { runTest("compiler/testData/codegen/box/enum/kt7257.kt"); diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java index 631ee91a3be..3dfad450998 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java @@ -9778,6 +9778,16 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { runTest("compiler/testData/codegen/box/enum/kt38996.kt"); } + @TestMetadata("kt44744.kt") + public void testKt44744() throws Exception { + runTest("compiler/testData/codegen/box/enum/kt44744.kt"); + } + + @TestMetadata("kt44744_innerClass.kt") + public void testKt44744_innerClass() throws Exception { + runTest("compiler/testData/codegen/box/enum/kt44744_innerClass.kt"); + } + @TestMetadata("kt7257.kt") public void testKt7257() throws Exception { runTest("compiler/testData/codegen/box/enum/kt7257.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 ec4fc854be0..27d6248d0d7 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 @@ -4797,6 +4797,16 @@ public class IrCodegenBoxWasmTestGenerated extends AbstractIrCodegenBoxWasmTest runTest("compiler/testData/codegen/box/enum/kt38996.kt"); } + @TestMetadata("kt44744.kt") + public void testKt44744() throws Exception { + runTest("compiler/testData/codegen/box/enum/kt44744.kt"); + } + + @TestMetadata("kt44744_innerClass.kt") + public void testKt44744_innerClass() throws Exception { + runTest("compiler/testData/codegen/box/enum/kt44744_innerClass.kt"); + } + @TestMetadata("kt7257.kt") public void testKt7257() throws Exception { runTest("compiler/testData/codegen/box/enum/kt7257.kt");