From 245bc7c8e2d235aea8fbaefcad19174d9856ad68 Mon Sep 17 00:00:00 2001 From: Dmitriy Novozhilov Date: Mon, 1 Mar 2021 15:28:24 +0300 Subject: [PATCH] [FIR2IR] Fix interpreting constants from const properties from other module --- .../FirBlackBoxCodegenTestGenerated.java | 6 +++++ .../ir/interpreter/IrCompileTimeChecker.kt | 7 ++++-- .../constValFromAnotherModuleInConsVal.kt | 25 +++++++++++++++++++ .../codegen/BlackBoxCodegenTestGenerated.java | 6 +++++ .../IrBlackBoxCodegenTestGenerated.java | 6 +++++ .../LightAnalysisModeTestGenerated.java | 5 ++++ .../IrJsCodegenBoxES6TestGenerated.java | 5 ++++ .../IrJsCodegenBoxTestGenerated.java | 5 ++++ .../semantics/JsCodegenBoxTestGenerated.java | 5 ++++ .../IrCodegenBoxWasmTestGenerated.java | 5 ++++ 10 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 compiler/testData/codegen/box/constants/constValFromAnotherModuleInConsVal.kt 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 52805e46853..0e5acc9195d 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 @@ -7026,6 +7026,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTest("compiler/testData/codegen/box/constants/comparisonTrue.kt"); } + @Test + @TestMetadata("constValFromAnotherModuleInConsVal.kt") + public void testConstValFromAnotherModuleInConsVal() throws Exception { + runTest("compiler/testData/codegen/box/constants/constValFromAnotherModuleInConsVal.kt"); + } + @Test @TestMetadata("constantsInWhen.kt") public void testConstantsInWhen() throws Exception { diff --git a/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/IrCompileTimeChecker.kt b/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/IrCompileTimeChecker.kt index 6da69340c90..be488bb57c7 100644 --- a/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/IrCompileTimeChecker.kt +++ b/compiler/ir/ir.interpreter/src/org/jetbrains/kotlin/ir/interpreter/IrCompileTimeChecker.kt @@ -76,7 +76,10 @@ class IrCompileTimeChecker( override fun visitElement(element: IrElement, data: Nothing?) = false private fun visitStatements(statements: List, data: Nothing?): Boolean { - if (mode == EvaluationMode.ONLY_BUILTINS) return false + if (mode == EvaluationMode.ONLY_BUILTINS) { + val statement = statements.singleOrNull() ?: return false + return statement is IrConst<*> + } return statements.all { it.accept(this, data) } } @@ -263,4 +266,4 @@ class IrCompileTimeChecker( override fun visitThrow(expression: IrThrow, data: Nothing?): Boolean { return expression.value.accept(this, data) } -} \ No newline at end of file +} diff --git a/compiler/testData/codegen/box/constants/constValFromAnotherModuleInConsVal.kt b/compiler/testData/codegen/box/constants/constValFromAnotherModuleInConsVal.kt new file mode 100644 index 00000000000..7f3bcaaf64e --- /dev/null +++ b/compiler/testData/codegen/box/constants/constValFromAnotherModuleInConsVal.kt @@ -0,0 +1,25 @@ +// MODULE: lib +// FILE: Typography.kt +object Typography { + const val ellipsis: Char = 'O' +} + +// MODULE: main(lib) +// FILE: main.kt +class A { + private companion object { + fun String.orEllipsis(): String { + return ellipsis + } + + const val ellipsis = "${Typography.ellipsis}" + } + + object B { + fun box() = "".orEllipsis() + } +} + +fun box(): String { + return A.B.box() + "K" +} 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 bd9fd13d05d..d27a6017dc3 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 @@ -7026,6 +7026,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/constants/comparisonTrue.kt"); } + @Test + @TestMetadata("constValFromAnotherModuleInConsVal.kt") + public void testConstValFromAnotherModuleInConsVal() throws Exception { + runTest("compiler/testData/codegen/box/constants/constValFromAnotherModuleInConsVal.kt"); + } + @Test @TestMetadata("constantsInWhen.kt") public void testConstantsInWhen() 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 6208023c355..6109fcb788c 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 @@ -7026,6 +7026,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/constants/comparisonTrue.kt"); } + @Test + @TestMetadata("constValFromAnotherModuleInConsVal.kt") + public void testConstValFromAnotherModuleInConsVal() throws Exception { + runTest("compiler/testData/codegen/box/constants/constValFromAnotherModuleInConsVal.kt"); + } + @Test @TestMetadata("constantsInWhen.kt") public void testConstantsInWhen() 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 acb9990ac99..4b6fff7f9d9 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -5355,6 +5355,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/constants/comparisonTrue.kt"); } + @TestMetadata("constValFromAnotherModuleInConsVal.kt") + public void testConstValFromAnotherModuleInConsVal() throws Exception { + runTest("compiler/testData/codegen/box/constants/constValFromAnotherModuleInConsVal.kt"); + } + @TestMetadata("constantsInWhen.kt") public void testConstantsInWhen() throws Exception { runTest("compiler/testData/codegen/box/constants/constantsInWhen.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 6ea02ceed1d..a498cd42b68 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 @@ -4738,6 +4738,11 @@ public class IrJsCodegenBoxES6TestGenerated extends AbstractIrJsCodegenBoxES6Tes runTest("compiler/testData/codegen/box/constants/comparisonTrue.kt"); } + @TestMetadata("constValFromAnotherModuleInConsVal.kt") + public void testConstValFromAnotherModuleInConsVal() throws Exception { + runTest("compiler/testData/codegen/box/constants/constValFromAnotherModuleInConsVal.kt"); + } + @TestMetadata("constantsInWhen.kt") public void testConstantsInWhen() throws Exception { runTest("compiler/testData/codegen/box/constants/constantsInWhen.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 2d958a7d0f8..7a299e52f78 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 @@ -4195,6 +4195,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { runTest("compiler/testData/codegen/box/constants/comparisonTrue.kt"); } + @TestMetadata("constValFromAnotherModuleInConsVal.kt") + public void testConstValFromAnotherModuleInConsVal() throws Exception { + runTest("compiler/testData/codegen/box/constants/constValFromAnotherModuleInConsVal.kt"); + } + @TestMetadata("constantsInWhen.kt") public void testConstantsInWhen() throws Exception { runTest("compiler/testData/codegen/box/constants/constantsInWhen.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 4254f8bc012..22993aa10d8 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 @@ -4195,6 +4195,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { runTest("compiler/testData/codegen/box/constants/comparisonTrue.kt"); } + @TestMetadata("constValFromAnotherModuleInConsVal.kt") + public void testConstValFromAnotherModuleInConsVal() throws Exception { + runTest("compiler/testData/codegen/box/constants/constValFromAnotherModuleInConsVal.kt"); + } + @TestMetadata("constantsInWhen.kt") public void testConstantsInWhen() throws Exception { runTest("compiler/testData/codegen/box/constants/constantsInWhen.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 21e007c79d9..f1d8f3394d4 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 @@ -3021,6 +3021,11 @@ public class IrCodegenBoxWasmTestGenerated extends AbstractIrCodegenBoxWasmTest runTest("compiler/testData/codegen/box/constants/comparisonFalse.kt"); } + @TestMetadata("constValFromAnotherModuleInConsVal.kt") + public void testConstValFromAnotherModuleInConsVal() throws Exception { + runTest("compiler/testData/codegen/box/constants/constValFromAnotherModuleInConsVal.kt"); + } + @TestMetadata("constantsInWhen.kt") public void testConstantsInWhen() throws Exception { runTest("compiler/testData/codegen/box/constants/constantsInWhen.kt");