From 8c7352e6686381f59101408fd1dce71a5f71f39b Mon Sep 17 00:00:00 2001 From: Dmitry Petrov Date: Fri, 14 Jul 2017 18:27:31 +0300 Subject: [PATCH] Generate constant "" for effectively empty string --- .../kotlin/codegen/ExpressionCodegen.java | 25 +++++++++++-------- .../bytecodeText/stringOperations/kt19037.kt | 7 ++++++ .../codegen/BytecodeTextTestGenerated.java | 6 +++++ 3 files changed, 28 insertions(+), 10 deletions(-) create mode 100644 compiler/testData/codegen/bytecodeText/stringOperations/kt19037.kt diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java index b4a19cf0953..91faa53d558 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java @@ -831,24 +831,29 @@ public class ExpressionCodegen extends KtVisitor impleme public StackValue visitStringTemplateExpression(@NotNull KtStringTemplateExpression expression, StackValue receiver) { List entries = preprocessStringTemplate(expression); - if (entries.size() == 1) { + Type type = expressionType(expression); + + if (entries.size() == 0) { + return StackValue.constant("", type); + } + else if (entries.size() == 1) { StringTemplateEntry entry = entries.get(0); if (entry instanceof StringTemplateEntry.Expression) { KtExpression expr = ((StringTemplateEntry.Expression) entry).expression; - return genToString(gen(expr), expressionType(expr)); + return genToString(gen(expr), type); } else { - Type type = expressionType(expression); return StackValue.constant(((StringTemplateEntry.Constant) entry).value, type); } } - - return StackValue.operation(JAVA_STRING_TYPE, v -> { - genStringBuilderConstructor(v); - invokeAppendForEntries(v, entries); - v.invokevirtual("java/lang/StringBuilder", "toString", "()Ljava/lang/String;", false); - return Unit.INSTANCE; - }); + else { + return StackValue.operation(type, v -> { + genStringBuilderConstructor(v); + invokeAppendForEntries(v, entries); + v.invokevirtual("java/lang/StringBuilder", "toString", "()Ljava/lang/String;", false); + return Unit.INSTANCE; + }); + } } private void invokeAppendForEntries(InstructionAdapter v, List entries) { diff --git a/compiler/testData/codegen/bytecodeText/stringOperations/kt19037.kt b/compiler/testData/codegen/bytecodeText/stringOperations/kt19037.kt new file mode 100644 index 00000000000..76b370bdc93 --- /dev/null +++ b/compiler/testData/codegen/bytecodeText/stringOperations/kt19037.kt @@ -0,0 +1,7 @@ +const val empty = "" + +val test1 = "" +val test2 = "abc$empty" +val test3 = "$empty$empty$empty" + +// 0 StringBuilder \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java index a263a49029c..e0d7161dd8f 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java @@ -2090,6 +2090,12 @@ public class BytecodeTextTestGenerated extends AbstractBytecodeTextTest { doTest(fileName); } + @TestMetadata("kt19037.kt") + public void testKt19037() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeText/stringOperations/kt19037.kt"); + doTest(fileName); + } + @TestMetadata("nestedConcat.kt") public void testNestedConcat() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeText/stringOperations/nestedConcat.kt");