JS backend: fixed wrong behavior when using nullable type in string template.
(cherry picked from commit 3a16e95)
This commit is contained in:
+31
-19
@@ -23,16 +23,23 @@ import com.google.dart.compiler.backend.js.ast.JsNumberLiteral;
|
|||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.jetbrains.jet.lang.psi.*;
|
import org.jetbrains.jet.lang.psi.*;
|
||||||
|
import org.jetbrains.jet.lang.resolve.BindingContext;
|
||||||
import org.jetbrains.jet.lang.resolve.name.Name;
|
import org.jetbrains.jet.lang.resolve.name.Name;
|
||||||
|
import org.jetbrains.jet.lang.types.JetType;
|
||||||
import org.jetbrains.k2js.translate.context.TranslationContext;
|
import org.jetbrains.k2js.translate.context.TranslationContext;
|
||||||
import org.jetbrains.k2js.translate.general.AbstractTranslator;
|
import org.jetbrains.k2js.translate.general.AbstractTranslator;
|
||||||
import org.jetbrains.k2js.translate.general.Translation;
|
import org.jetbrains.k2js.translate.general.Translation;
|
||||||
|
import org.jetbrains.k2js.translate.intrinsic.functions.factories.TopLevelFIF;
|
||||||
|
import org.jetbrains.k2js.translate.intrinsic.functions.patterns.NamePredicate;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
import static org.jetbrains.k2js.translate.utils.JsAstUtils.sum;
|
import static org.jetbrains.k2js.translate.utils.JsAstUtils.sum;
|
||||||
import static org.jetbrains.k2js.translate.utils.JsDescriptorUtils.getNameIfStandardType;
|
import static org.jetbrains.k2js.translate.utils.JsDescriptorUtils.getNameIfStandardType;
|
||||||
|
|
||||||
|
|
||||||
public final class StringTemplateTranslator extends AbstractTranslator {
|
public final class StringTemplateTranslator extends AbstractTranslator {
|
||||||
|
private final JetStringTemplateEntry[] expressionEntries;
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public static JsExpression translate(@NotNull JetStringTemplateExpression expression,
|
public static JsExpression translate(@NotNull JetStringTemplateExpression expression,
|
||||||
@@ -40,20 +47,20 @@ public final class StringTemplateTranslator extends AbstractTranslator {
|
|||||||
return (new StringTemplateTranslator(expression, context).translate());
|
return (new StringTemplateTranslator(expression, context).translate());
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
|
||||||
private final JetStringTemplateExpression expression;
|
|
||||||
|
|
||||||
private StringTemplateTranslator(@NotNull JetStringTemplateExpression expression,
|
private StringTemplateTranslator(@NotNull JetStringTemplateExpression expression,
|
||||||
@NotNull TranslationContext context) {
|
@NotNull TranslationContext context) {
|
||||||
super(context);
|
super(context);
|
||||||
this.expression = expression;
|
|
||||||
|
expressionEntries = expression.getEntries();
|
||||||
|
assert expressionEntries.length != 0 : "String template must have one or more entries.";
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private JsExpression translate() {
|
private JsExpression translate() {
|
||||||
assert expression.getEntries().length != 0 : "String template must have one or more entries.";
|
|
||||||
EntryVisitor entryVisitor = new EntryVisitor();
|
EntryVisitor entryVisitor = new EntryVisitor();
|
||||||
for (JetStringTemplateEntry entry : expression.getEntries()) {
|
for (JetStringTemplateEntry entry : expressionEntries) {
|
||||||
entry.accept(entryVisitor);
|
entry.accept(entryVisitor);
|
||||||
}
|
}
|
||||||
return entryVisitor.getResultingExpression();
|
return entryVisitor.getResultingExpression();
|
||||||
@@ -83,26 +90,31 @@ public final class StringTemplateTranslator extends AbstractTranslator {
|
|||||||
append(context().program().getStringLiteral(translatedExpression.toString()));
|
append(context().program().getStringLiteral(translatedExpression.toString()));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (mustCallToString(entryExpression)) {
|
|
||||||
|
JetType type = context().bindingContext().get(BindingContext.EXPRESSION_TYPE, entryExpression);
|
||||||
|
if (type == null || type.isNullable()) {
|
||||||
|
append(TopLevelFIF.TO_STRING.apply((JsExpression) null, Collections.singletonList(translatedExpression), context()));
|
||||||
|
}
|
||||||
|
else if (mustCallToString(type)) {
|
||||||
append(new JsInvocation(new JsNameRef("toString", translatedExpression)));
|
append(new JsInvocation(new JsNameRef("toString", translatedExpression)));
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
append(translatedExpression);
|
append(translatedExpression);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean mustCallToString(@NotNull JetExpression entryExpression) {
|
private boolean mustCallToString(@NotNull JetType type) {
|
||||||
Name typeName = getNameIfStandardType(entryExpression, context());
|
Name typeName = getNameIfStandardType(type);
|
||||||
if (typeName == null) {
|
if (typeName != null) {
|
||||||
return true;
|
//TODO: this is a hacky optimization, should use some generic approach
|
||||||
|
if (NamePredicate.STRING.apply(typeName)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
else if (NamePredicate.PRIMITIVE_NUMBERS.apply(typeName)) {
|
||||||
|
return resultingExpression == null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
//TODO: this is a hacky optimization, should use some generic approach
|
return expressionEntries.length == 1;
|
||||||
if (typeName.asString().equals("String")) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (typeName.asString().equals("Int") && resultingExpression != null) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
+4
-1
@@ -123,11 +123,14 @@ public final class TopLevelFIF extends CompositeFIF {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public static final KotlinFunctionIntrinsic TO_STRING = new KotlinFunctionIntrinsic("toString");
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public static final FunctionIntrinsicFactory INSTANCE = new TopLevelFIF();
|
public static final FunctionIntrinsicFactory INSTANCE = new TopLevelFIF();
|
||||||
|
|
||||||
private TopLevelFIF() {
|
private TopLevelFIF() {
|
||||||
add(pattern("jet", "toString").receiverExists(), new KotlinFunctionIntrinsic("toString"));
|
add(pattern("jet", "toString").receiverExists(), TO_STRING);
|
||||||
add(pattern("jet", "equals").receiverExists(), EQUALS);
|
add(pattern("jet", "equals").receiverExists(), EQUALS);
|
||||||
add(pattern("jet", "identityEquals").receiverExists(), IDENTITY_EQUALS);
|
add(pattern("jet", "identityEquals").receiverExists(), IDENTITY_EQUALS);
|
||||||
add(pattern(NamePredicate.PRIMITIVE_NUMBERS, "equals"), EQUALS);
|
add(pattern(NamePredicate.PRIMITIVE_NUMBERS, "equals"), EQUALS);
|
||||||
|
|||||||
+3
@@ -40,6 +40,9 @@ public final class NamePredicate implements Predicate<Name> {
|
|||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public static final NamePredicate STRING = new NamePredicate("String");
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private final List<Name> validNames = Lists.newArrayList();
|
private final List<Name> validNames = Lists.newArrayList();
|
||||||
|
|
||||||
|
|||||||
@@ -7,5 +7,8 @@ fun box(): Boolean {
|
|||||||
|
|
||||||
if (message != "a = abc, b = def") return false
|
if (message != "a = abc, b = def") return false
|
||||||
|
|
||||||
|
val v1 = null
|
||||||
|
if ("returns null null" != "returns $v1 ${null}") return false
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user