JVM IR: Fix compilation of inline functions in anonymous objects...

...in class members. The corresponding classes end up nested in the
class initializer of the surrounding class and we need to take this into
account when creating instances of ClassCodegen.

This fixes KT-40332 on the JVM IR backend.
This commit is contained in:
Steven Schäfer
2020-08-19 18:42:47 +02:00
committed by Alexander Udalov
parent ce02982226
commit ea98062241
9 changed files with 56 additions and 1 deletions
@@ -4025,6 +4025,11 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
runTest("compiler/testData/codegen/box/classes/kt3546.kt");
}
@TestMetadata("kt40332.kt")
public void testKt40332() throws Exception {
runTest("compiler/testData/codegen/box/classes/kt40332.kt");
}
@TestMetadata("kt454.kt")
public void testKt454() throws Exception {
runTest("compiler/testData/codegen/box/classes/kt454.kt");
@@ -226,7 +226,16 @@ abstract class ClassCodegen protected constructor(
fun getOrCreate(
irClass: IrClass,
context: JvmBackendContext,
parentFunction: IrFunction? = null,
// The `parentFunction` is only set for classes nested inside of functions. This is usually safe, since there is no
// way to refer to (inline) members of such a class from outside of the function unless the function in question is
// itself declared as inline. In that case, the function will be compiled before we can refer to the nested class.
//
// The one exception to this rule are anonymous objects defined as members of a class. These are nested inside of the
// class initializer, but can be referred to from anywhere within the scope of the class. That's why we have to ensure
// that all references to classes inside of <clinit> have a non-null `parentFunction`.
parentFunction: IrFunction? = irClass.parent.safeAs<IrFunction>()?.takeIf {
it.origin == JvmLoweredDeclarationOrigin.CLASS_STATIC_INITIALIZER
},
): ClassCodegen =
context.classCodegens.getOrPut(irClass) {
context.createCodegen(irClass, context, parentFunction) ?: DescriptorBasedClassCodegen(irClass, context, parentFunction)
+11
View File
@@ -0,0 +1,11 @@
// IGNORE_BACKEND: JVM
object A {
private val s = object {
inline operator fun invoke(): String = "OK"
}
fun value() = s()
}
fun box(): String = A.value()
@@ -4045,6 +4045,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/classes/kt3546.kt");
}
@TestMetadata("kt40332.kt")
public void testKt40332() throws Exception {
runTest("compiler/testData/codegen/box/classes/kt40332.kt");
}
@TestMetadata("kt454.kt")
public void testKt454() throws Exception {
runTest("compiler/testData/codegen/box/classes/kt454.kt");
@@ -3642,6 +3642,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Classes extends AbstractLightAnalysisModeTest {
@TestMetadata("kt40332.kt")
public void ignoreKt40332() throws Exception {
runTest("compiler/testData/codegen/box/classes/kt40332.kt");
}
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.JVM, testDataFilePath);
}
@@ -4025,6 +4025,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/classes/kt3546.kt");
}
@TestMetadata("kt40332.kt")
public void testKt40332() throws Exception {
runTest("compiler/testData/codegen/box/classes/kt40332.kt");
}
@TestMetadata("kt454.kt")
public void testKt454() throws Exception {
runTest("compiler/testData/codegen/box/classes/kt454.kt");
@@ -3205,6 +3205,11 @@ public class IrJsCodegenBoxES6TestGenerated extends AbstractIrJsCodegenBoxES6Tes
runTest("compiler/testData/codegen/box/classes/kt3546.kt");
}
@TestMetadata("kt40332.kt")
public void testKt40332() throws Exception {
runTest("compiler/testData/codegen/box/classes/kt40332.kt");
}
@TestMetadata("kt454.kt")
public void testKt454() throws Exception {
runTest("compiler/testData/codegen/box/classes/kt454.kt");
@@ -3205,6 +3205,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/classes/kt3546.kt");
}
@TestMetadata("kt40332.kt")
public void testKt40332() throws Exception {
runTest("compiler/testData/codegen/box/classes/kt40332.kt");
}
@TestMetadata("kt454.kt")
public void testKt454() throws Exception {
runTest("compiler/testData/codegen/box/classes/kt454.kt");
@@ -3205,6 +3205,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/classes/kt3546.kt");
}
@TestMetadata("kt40332.kt")
public void testKt40332() throws Exception {
runTest("compiler/testData/codegen/box/classes/kt40332.kt");
}
@TestMetadata("kt454.kt")
public void testKt454() throws Exception {
runTest("compiler/testData/codegen/box/classes/kt454.kt");