Generate constant "" for effectively empty string
This commit is contained in:
@@ -831,24 +831,29 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
|
|||||||
public StackValue visitStringTemplateExpression(@NotNull KtStringTemplateExpression expression, StackValue receiver) {
|
public StackValue visitStringTemplateExpression(@NotNull KtStringTemplateExpression expression, StackValue receiver) {
|
||||||
List<StringTemplateEntry> entries = preprocessStringTemplate(expression);
|
List<StringTemplateEntry> 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);
|
StringTemplateEntry entry = entries.get(0);
|
||||||
if (entry instanceof StringTemplateEntry.Expression) {
|
if (entry instanceof StringTemplateEntry.Expression) {
|
||||||
KtExpression expr = ((StringTemplateEntry.Expression) entry).expression;
|
KtExpression expr = ((StringTemplateEntry.Expression) entry).expression;
|
||||||
return genToString(gen(expr), expressionType(expr));
|
return genToString(gen(expr), type);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Type type = expressionType(expression);
|
|
||||||
return StackValue.constant(((StringTemplateEntry.Constant) entry).value, type);
|
return StackValue.constant(((StringTemplateEntry.Constant) entry).value, type);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
return StackValue.operation(JAVA_STRING_TYPE, v -> {
|
return StackValue.operation(type, v -> {
|
||||||
genStringBuilderConstructor(v);
|
genStringBuilderConstructor(v);
|
||||||
invokeAppendForEntries(v, entries);
|
invokeAppendForEntries(v, entries);
|
||||||
v.invokevirtual("java/lang/StringBuilder", "toString", "()Ljava/lang/String;", false);
|
v.invokevirtual("java/lang/StringBuilder", "toString", "()Ljava/lang/String;", false);
|
||||||
return Unit.INSTANCE;
|
return Unit.INSTANCE;
|
||||||
});
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void invokeAppendForEntries(InstructionAdapter v, List<StringTemplateEntry> entries) {
|
private void invokeAppendForEntries(InstructionAdapter v, List<StringTemplateEntry> entries) {
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
const val empty = ""
|
||||||
|
|
||||||
|
val test1 = ""
|
||||||
|
val test2 = "abc$empty"
|
||||||
|
val test3 = "$empty$empty$empty"
|
||||||
|
|
||||||
|
// 0 StringBuilder
|
||||||
@@ -2090,6 +2090,12 @@ public class BytecodeTextTestGenerated extends AbstractBytecodeTextTest {
|
|||||||
doTest(fileName);
|
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")
|
@TestMetadata("nestedConcat.kt")
|
||||||
public void testNestedConcat() throws Exception {
|
public void testNestedConcat() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeText/stringOperations/nestedConcat.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeText/stringOperations/nestedConcat.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user