From 0efe28a12a2430c1843059bb40984d820c4a9fa0 Mon Sep 17 00:00:00 2001 From: Denis Zharkov Date: Fri, 5 Feb 2016 12:33:57 +0300 Subject: [PATCH] Do not inline non-const vals #KT-10425 Fixed --- .../kotlin/codegen/ExpressionCodegen.java | 3 ++ .../constants/noInlineNonConst.kt | 28 +++++++++++++++++++ .../whenStringOptimization/nonInlinedConst.kt | 3 +- .../codegen/BytecodeTextTestGenerated.java | 6 ++++ 4 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 compiler/testData/codegen/bytecodeText/constants/noInlineNonConst.kt diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java index b780585cc26..f06791a0490 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java @@ -1246,6 +1246,9 @@ public class ExpressionCodegen extends KtVisitor impleme if (compileTimeValue == null) { return null; } + + if (compileTimeValue.getUsesNonConstValAsConstant()) return null; + KotlinType expectedType = bindingContext.getType(expression); return compileTimeValue.toConstantValue(expectedType); } diff --git a/compiler/testData/codegen/bytecodeText/constants/noInlineNonConst.kt b/compiler/testData/codegen/bytecodeText/constants/noInlineNonConst.kt new file mode 100644 index 00000000000..b505c039a64 --- /dev/null +++ b/compiler/testData/codegen/bytecodeText/constants/noInlineNonConst.kt @@ -0,0 +1,28 @@ +val x = 1 + +class A { + val b = 2 +} + +fun box(): Int { + when ("abc".length) { + x -> return 0 + else -> A() + } + + if (x == 0) return 1 + + val a = A() + when ("cde".length) { + a.b -> return 0 + else -> A() + } + + if (a.b == 0) return 1 + + return 2 +} + +/* 1 GETSTATIC is located within `x` public getter, other 2 are accesses that should not be inlined */ +// 3 GETSTATIC NoInlineNonConstKt\.x : I +// 2 INVOKEVIRTUAL A\.getB \(\)I diff --git a/compiler/testData/codegen/bytecodeText/whenStringOptimization/nonInlinedConst.kt b/compiler/testData/codegen/bytecodeText/whenStringOptimization/nonInlinedConst.kt index f5b8d524824..8563b273642 100644 --- a/compiler/testData/codegen/bytecodeText/whenStringOptimization/nonInlinedConst.kt +++ b/compiler/testData/codegen/bytecodeText/whenStringOptimization/nonInlinedConst.kt @@ -1,5 +1,6 @@ +const val y = "cde" + fun foo(x : String) : String { - val y = "cde" when (x) { "abc", "${y}" -> return "abc_cde" "e" + "fg", "ghi" -> return "efg_ghi" diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java index d10219e7db0..939d594f5f1 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java @@ -592,6 +592,12 @@ public class BytecodeTextTestGenerated extends AbstractBytecodeTextTest { doTest(fileName); } + @TestMetadata("noInlineNonConst.kt") + public void testNoInlineNonConst() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeText/constants/noInlineNonConst.kt"); + doTest(fileName); + } + @TestMetadata("nullableByteAndShort.kt") public void testNullableByteAndShort() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeText/constants/nullableByteAndShort.kt");