diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/LocalDeclarationsLowering.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/LocalDeclarationsLowering.kt index e9fe958d02a..9adde42a6e0 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/LocalDeclarationsLowering.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/LocalDeclarationsLowering.kt @@ -953,6 +953,8 @@ class LocalDeclarationsLowering( super.visitConstructor(declaration, data) if (!isScriptMode && !declaration.constructedClass.isLocalNotInner()) return + // Inner classes in scripts are handled properly in the InnerClassesLowering + if (isScriptMode && declaration.constructedClass.isInner) return // LDL doesn't work on the enums because local enums are not allowed, so skipping them in scripts too if (isScriptMode && declaration.constructedClass.kind == ClassKind.ENUM_CLASS) return @@ -964,6 +966,8 @@ class LocalDeclarationsLowering( super.visitClass(declaration, data.withCurrentClass(declaration)) if (!isScriptMode && !declaration.isLocalNotInner()) return + // Inner classes in scripts are handled properly in the InnerClassesLowering + if (isScriptMode && declaration.isInner) return // LDL doesn't work on the enums because local enums are not allowed, so skipping them in scripts too if (isScriptMode && declaration.kind == ClassKind.ENUM_CLASS) return diff --git a/compiler/testData/codegen/script/innerClass.kts b/compiler/testData/codegen/script/innerClass.kts new file mode 100644 index 00000000000..287b34c463a --- /dev/null +++ b/compiler/testData/codegen/script/innerClass.kts @@ -0,0 +1,29 @@ + +val b = "K" + +inner class InnerClass(val s: String) { + fun test1() = s + b + + inner class C1 { + fun bar(c: String) = s + b + c + } + + fun test2() = C1().bar("!") + + inner class C2 { + fun bar(c: String) = s + b + c + + fun test(): String { + var c = "?" + return object { + fun run() = s + b + c + }.run() + } + } + + fun test3() = C2().test() +} + +val rv = InnerClass("O").test1() + InnerClass("_O").test2() + InnerClass("__O").test3() + +// expected: rv: OK_OK!__OK? diff --git a/compiler/tests-gen/org/jetbrains/kotlin/codegen/ScriptCodegenTestGenerated.java b/compiler/tests-gen/org/jetbrains/kotlin/codegen/ScriptCodegenTestGenerated.java index 946e6f41e4e..d66f2965798 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/codegen/ScriptCodegenTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/codegen/ScriptCodegenTestGenerated.java @@ -64,6 +64,11 @@ public class ScriptCodegenTestGenerated extends AbstractScriptCodegenTest { runTest("compiler/testData/codegen/script/inline.kts"); } + @TestMetadata("innerClass.kts") + public void testInnerClass() throws Exception { + runTest("compiler/testData/codegen/script/innerClass.kts"); + } + @TestMetadata("kt20707.kts") public void testKt20707() throws Exception { runTest("compiler/testData/codegen/script/kt20707.kts"); diff --git a/compiler/tests-gen/org/jetbrains/kotlin/codegen/ir/IrScriptCodegenTestGenerated.java b/compiler/tests-gen/org/jetbrains/kotlin/codegen/ir/IrScriptCodegenTestGenerated.java index c2b72d7b38a..f6c182292c8 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/codegen/ir/IrScriptCodegenTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/codegen/ir/IrScriptCodegenTestGenerated.java @@ -65,6 +65,11 @@ public class IrScriptCodegenTestGenerated extends AbstractIrScriptCodegenTest { runTest("compiler/testData/codegen/script/inline.kts"); } + @TestMetadata("innerClass.kts") + public void testInnerClass() throws Exception { + runTest("compiler/testData/codegen/script/innerClass.kts"); + } + @TestMetadata("kt20707.kts") public void testKt20707() throws Exception { runTest("compiler/testData/codegen/script/kt20707.kts");