From 19a95f2fb4b6653b267deb15396275004452a167 Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Thu, 26 Oct 2023 17:01:03 +0200 Subject: [PATCH] K2: swap heuristics in AnnotationLoader (related to KT-62598) Before this commit, we first tried to guess a type of array literal in deserialized annotation (it's a strange heuristics which can lead to SOE and maybe not needed at all, see KT-62598, KT-62929), and then, if unsuccessful, took the type from the first literal element, if any. Since the second heuristic is much more clear, safe, and understandable, in this commit they were swapped. This commit does not fix KT-62598 in general case, but decreases a set of cases when it can occur to empty array literals only. See the commented line at the end of the added test. --- ...LightTreeBlackBoxCodegenTestGenerated.java | 6 ++ ...hIrFakeOverrideGeneratorTestGenerated.java | 6 ++ .../FirPsiBlackBoxCodegenTestGenerated.java | 6 ++ .../java/deserialization/AnnotationsLoader.kt | 7 ++- .../fir/StackOverflowInAnnotationLoader.kt | 58 +++++++++++++++++++ .../IrBlackBoxCodegenTestGenerated.java | 6 ++ ...kBoxCodegenWithIrInlinerTestGenerated.java | 6 ++ .../LightAnalysisModeTestGenerated.java | 5 ++ 8 files changed, 97 insertions(+), 3 deletions(-) create mode 100644 compiler/testData/codegen/box/fir/StackOverflowInAnnotationLoader.kt diff --git a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirLightTreeBlackBoxCodegenTestGenerated.java b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirLightTreeBlackBoxCodegenTestGenerated.java index dbfd42e696e..2ebc34439a1 100644 --- a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirLightTreeBlackBoxCodegenTestGenerated.java +++ b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirLightTreeBlackBoxCodegenTestGenerated.java @@ -19239,6 +19239,12 @@ public class FirLightTreeBlackBoxCodegenTestGenerated extends AbstractFirLightTr runTest("compiler/testData/codegen/box/fir/selectingLambdas.kt"); } + @Test + @TestMetadata("StackOverflowInAnnotationLoader.kt") + public void testStackOverflowInAnnotationLoader() throws Exception { + runTest("compiler/testData/codegen/box/fir/StackOverflowInAnnotationLoader.kt"); + } + @Test @TestMetadata("staticImportFromEnum.kt") public void testStaticImportFromEnum() throws Exception { diff --git a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirLightTreeBlackBoxCodegenWithIrFakeOverrideGeneratorTestGenerated.java b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirLightTreeBlackBoxCodegenWithIrFakeOverrideGeneratorTestGenerated.java index 5775a32f9b6..7dfadd62224 100644 --- a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirLightTreeBlackBoxCodegenWithIrFakeOverrideGeneratorTestGenerated.java +++ b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirLightTreeBlackBoxCodegenWithIrFakeOverrideGeneratorTestGenerated.java @@ -19239,6 +19239,12 @@ public class FirLightTreeBlackBoxCodegenWithIrFakeOverrideGeneratorTestGenerated runTest("compiler/testData/codegen/box/fir/selectingLambdas.kt"); } + @Test + @TestMetadata("StackOverflowInAnnotationLoader.kt") + public void testStackOverflowInAnnotationLoader() throws Exception { + runTest("compiler/testData/codegen/box/fir/StackOverflowInAnnotationLoader.kt"); + } + @Test @TestMetadata("staticImportFromEnum.kt") public void testStaticImportFromEnum() throws Exception { diff --git a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirPsiBlackBoxCodegenTestGenerated.java b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirPsiBlackBoxCodegenTestGenerated.java index 6a51247950c..0523fa4c5a2 100644 --- a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirPsiBlackBoxCodegenTestGenerated.java +++ b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirPsiBlackBoxCodegenTestGenerated.java @@ -19239,6 +19239,12 @@ public class FirPsiBlackBoxCodegenTestGenerated extends AbstractFirPsiBlackBoxCo runTest("compiler/testData/codegen/box/fir/selectingLambdas.kt"); } + @Test + @TestMetadata("StackOverflowInAnnotationLoader.kt") + public void testStackOverflowInAnnotationLoader() throws Exception { + runTest("compiler/testData/codegen/box/fir/StackOverflowInAnnotationLoader.kt"); + } + @Test @TestMetadata("staticImportFromEnum.kt") public void testStaticImportFromEnum() throws Exception { diff --git a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/deserialization/AnnotationsLoader.kt b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/deserialization/AnnotationsLoader.kt index 95a09513dfa..b6eee699ce9 100644 --- a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/deserialization/AnnotationsLoader.kt +++ b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/deserialization/AnnotationsLoader.kt @@ -98,10 +98,11 @@ internal class AnnotationsLoader(private val session: FirSession, private val ko override fun visitEnd() { visitExpression(name, buildArrayLiteral { - guessArrayTypeIfNeeded(name, elements)?.let { - coneTypeOrNull = it.coneTypeOrNull - } ?: elements.firstOrNull()?.resolvedType?.createOutArrayType()?.let { + @OptIn(UnresolvedExpressionTypeAccess::class) + elements.firstOrNull()?.coneTypeOrNull?.createOutArrayType()?.let { coneTypeOrNull = it + } ?: guessArrayTypeIfNeeded(name, elements)?.let { + coneTypeOrNull = it.coneTypeOrNull } argumentList = buildArgumentList { arguments += elements diff --git a/compiler/testData/codegen/box/fir/StackOverflowInAnnotationLoader.kt b/compiler/testData/codegen/box/fir/StackOverflowInAnnotationLoader.kt new file mode 100644 index 00000000000..707cebc0861 --- /dev/null +++ b/compiler/testData/codegen/box/fir/StackOverflowInAnnotationLoader.kt @@ -0,0 +1,58 @@ +// TARGET_BACKEND: JVM_IR +// ISSUE: KT-62598 + +// MODULE: m1 +// FILE: m1.kt +interface Holder { + interface Entry { + @Annotation(value = [""]) + fun f() + } + + annotation class Annotation( + val value: Array, + ) +} + +interface ByteHolder { + interface Entry { + @Annotation(value = [1]) + fun f() + } + + annotation class Annotation( + val value: ByteArray, + ) +} + +interface HolderWithDefault { + interface Entry { + @Annotation + fun f() + } + + annotation class Annotation( + val value: Array = [""], + ) +} + +interface HolderWithEmpty { + interface Entry { + @Annotation(value = []) + fun f() + } + + annotation class Annotation( + val value: Array, + ) +} + +// MODULE: m2(m1) +// FILE: m2.kt +import Holder +import ByteHolder +import HolderWithDefault +// This line still provokes SOE in K2 +//import HolderWithEmpty + +fun box() = "OK" 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 66c5d677b50..259880650f6 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 @@ -19239,6 +19239,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/fir/selectingLambdas.kt"); } + @Test + @TestMetadata("StackOverflowInAnnotationLoader.kt") + public void testStackOverflowInAnnotationLoader() throws Exception { + runTest("compiler/testData/codegen/box/fir/StackOverflowInAnnotationLoader.kt"); + } + @Test @TestMetadata("staticImportFromEnum.kt") public void testStaticImportFromEnum() throws Exception { diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenWithIrInlinerTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenWithIrInlinerTestGenerated.java index fd9492f134f..b84279f147c 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenWithIrInlinerTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenWithIrInlinerTestGenerated.java @@ -19239,6 +19239,12 @@ public class IrBlackBoxCodegenWithIrInlinerTestGenerated extends AbstractIrBlack runTest("compiler/testData/codegen/box/fir/selectingLambdas.kt"); } + @Test + @TestMetadata("StackOverflowInAnnotationLoader.kt") + public void testStackOverflowInAnnotationLoader() throws Exception { + runTest("compiler/testData/codegen/box/fir/StackOverflowInAnnotationLoader.kt"); + } + @Test @TestMetadata("staticImportFromEnum.kt") public void testStaticImportFromEnum() 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 6096349cf8c..9022fff780c 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -16030,6 +16030,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/fir/SamWithReceiverMavenProjectImportHandler.kt"); } + @TestMetadata("StackOverflowInAnnotationLoader.kt") + public void testStackOverflowInAnnotationLoader() throws Exception { + runTest("compiler/testData/codegen/box/fir/StackOverflowInAnnotationLoader.kt"); + } + @TestMetadata("staticImportFromEnum.kt") public void testStaticImportFromEnum() throws Exception { runTest("compiler/testData/codegen/box/fir/staticImportFromEnum.kt");