From 49576e0cbbcb3d342a8e43db052dbf7c8264c19f Mon Sep 17 00:00:00 2001 From: Georgy Bronnikov Date: Sat, 23 Apr 2022 20:07:06 +0300 Subject: [PATCH] Fir2Ir: handle nested class references in type parameter bounds --- .../kotlin/fir/backend/Fir2IrClassifierStorage.kt | 4 ++++ .../codegen/FirBlackBoxCodegenTestGenerated.java | 6 ++++++ .../compileKotlinAgainstKotlin/nestedClassAsBound.kt | 10 ++++++++++ .../runners/codegen/BlackBoxCodegenTestGenerated.java | 6 ++++++ .../codegen/IrBlackBoxCodegenTestGenerated.java | 6 ++++++ .../codegen/JvmIrAgainstOldBoxTestGenerated.java | 6 ++++++ .../codegen/JvmOldAgainstIrBoxTestGenerated.java | 6 ++++++ .../blackboxtest/NativeCodegenBoxTestGenerated.java | 6 ++++++ 8 files changed, 50 insertions(+) create mode 100644 compiler/testData/codegen/box/compileKotlinAgainstKotlin/nestedClassAsBound.kt diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrClassifierStorage.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrClassifierStorage.kt index 982100f3872..7d83c511c76 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrClassifierStorage.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrClassifierStorage.kt @@ -546,6 +546,10 @@ class Fir2IrClassifierStorage( val parentId = classId.outerClassId val parentClass = parentId?.let { session.symbolProvider.getClassLikeSymbolByClassId(it) } val irParent = declarationStorage.findIrParent(classId.packageFqName, parentClass?.toLookupTag(), firClassSymbol, firClass.origin)!! + + // firClass may be referenced by some parent's type parameters as a bound. In that case, getIrClassSymbol will be called recursively. + getCachedIrClass(firClass)?.let { return it.symbol } + val irClass = firClass.convertWithOffsets { startOffset, endOffset -> symbolTable.declareClass(signature, { symbol }) { Fir2IrLazyClass(components, startOffset, endOffset, firClass.irOrigin(firProvider), firClass, symbol).apply { 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 92946e270ea..74a3c021cd0 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 @@ -7355,6 +7355,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTest("compiler/testData/codegen/box/compileKotlinAgainstKotlin/nestedClass.kt"); } + @Test + @TestMetadata("nestedClassAsBound.kt") + public void testNestedClassAsBound() throws Exception { + runTest("compiler/testData/codegen/box/compileKotlinAgainstKotlin/nestedClassAsBound.kt"); + } + @Test @TestMetadata("nestedClassInAnnotationArgument.kt") public void testNestedClassInAnnotationArgument() throws Exception { diff --git a/compiler/testData/codegen/box/compileKotlinAgainstKotlin/nestedClassAsBound.kt b/compiler/testData/codegen/box/compileKotlinAgainstKotlin/nestedClassAsBound.kt new file mode 100644 index 00000000000..b73f282d11f --- /dev/null +++ b/compiler/testData/codegen/box/compileKotlinAgainstKotlin/nestedClassAsBound.kt @@ -0,0 +1,10 @@ +// MODULE: lib +// FILE: Q.kt +class Q { + open class S { + val ok = "OK" + } +} +// MODULE: main(lib) +// FILE: box.kt +fun box() = Q.S().ok \ No newline at end of file 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 4e9474e8981..0fd53fc7009 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 @@ -7241,6 +7241,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/compileKotlinAgainstKotlin/nestedClass.kt"); } + @Test + @TestMetadata("nestedClassAsBound.kt") + public void testNestedClassAsBound() throws Exception { + runTest("compiler/testData/codegen/box/compileKotlinAgainstKotlin/nestedClassAsBound.kt"); + } + @Test @TestMetadata("nestedClassInAnnotationArgument.kt") public void testNestedClassInAnnotationArgument() 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 7510823e2bb..e10fe278b87 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 @@ -7355,6 +7355,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/compileKotlinAgainstKotlin/nestedClass.kt"); } + @Test + @TestMetadata("nestedClassAsBound.kt") + public void testNestedClassAsBound() throws Exception { + runTest("compiler/testData/codegen/box/compileKotlinAgainstKotlin/nestedClassAsBound.kt"); + } + @Test @TestMetadata("nestedClassInAnnotationArgument.kt") public void testNestedClassInAnnotationArgument() throws Exception { diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/JvmIrAgainstOldBoxTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/JvmIrAgainstOldBoxTestGenerated.java index 183bce5cb36..9bc59dac3e3 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/JvmIrAgainstOldBoxTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/JvmIrAgainstOldBoxTestGenerated.java @@ -337,6 +337,12 @@ public class JvmIrAgainstOldBoxTestGenerated extends AbstractJvmIrAgainstOldBoxT runTest("compiler/testData/codegen/box/compileKotlinAgainstKotlin/nestedClass.kt"); } + @Test + @TestMetadata("nestedClassAsBound.kt") + public void testNestedClassAsBound() throws Exception { + runTest("compiler/testData/codegen/box/compileKotlinAgainstKotlin/nestedClassAsBound.kt"); + } + @Test @TestMetadata("nestedClassInAnnotationArgument.kt") public void testNestedClassInAnnotationArgument() throws Exception { diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/JvmOldAgainstIrBoxTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/JvmOldAgainstIrBoxTestGenerated.java index e0de8a0883a..26e2570ca13 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/JvmOldAgainstIrBoxTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/JvmOldAgainstIrBoxTestGenerated.java @@ -337,6 +337,12 @@ public class JvmOldAgainstIrBoxTestGenerated extends AbstractJvmOldAgainstIrBoxT runTest("compiler/testData/codegen/box/compileKotlinAgainstKotlin/nestedClass.kt"); } + @Test + @TestMetadata("nestedClassAsBound.kt") + public void testNestedClassAsBound() throws Exception { + runTest("compiler/testData/codegen/box/compileKotlinAgainstKotlin/nestedClassAsBound.kt"); + } + @Test @TestMetadata("nestedClassInAnnotationArgument.kt") public void testNestedClassInAnnotationArgument() throws Exception { diff --git a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/NativeCodegenBoxTestGenerated.java b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/NativeCodegenBoxTestGenerated.java index 403e303f64b..4a1af212771 100644 --- a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/NativeCodegenBoxTestGenerated.java +++ b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/NativeCodegenBoxTestGenerated.java @@ -5563,6 +5563,12 @@ public class NativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTest runTest("compiler/testData/codegen/box/compileKotlinAgainstKotlin/nestedClass.kt"); } + @Test + @TestMetadata("nestedClassAsBound.kt") + public void testNestedClassAsBound() throws Exception { + runTest("compiler/testData/codegen/box/compileKotlinAgainstKotlin/nestedClassAsBound.kt"); + } + @Test @TestMetadata("nestedClassInAnnotationArgument.kt") public void testNestedClassInAnnotationArgument() throws Exception {