提交 22b6f286 编写于 作者: A Alex Tkachman

fix for KT-152

上级 58765f23
......@@ -1202,6 +1202,14 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
private StackValue generateEquals(JetExpression left, JetExpression right, IElementType opToken) {
Type leftType = expressionType(left);
Type rightType = expressionType(right);
if(leftType == JetTypeMapper.TYPE_NOTHING) {
return genCmpWithNull(right, rightType, opToken);
}
if(rightType == JetTypeMapper.TYPE_NOTHING) {
return genCmpWithNull(left, leftType, opToken);
}
if(JetTypeMapper.isPrimitive(leftType) != JetTypeMapper.isPrimitive(rightType)) {
gen(left, leftType);
v.valueOf(leftType);
......@@ -1217,6 +1225,28 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
return generateEqualsForExpressionsOnStack(opToken, leftType, rightType);
}
private StackValue genCmpWithNull(JetExpression exp, Type expType, IElementType opToken) {
gen(exp, typeMapper.boxType(expType));
Label fail = new Label(), end = new Label();
if(JetTokens.EQEQ == opToken || JetTokens.EQEQEQ == opToken) {
v.ifnonnull(fail);
v.iconst(1);
v.goTo(end);
v.mark(fail);
v.iconst(0);
v.mark(end);
}
else {
v.ifnull(fail);
v.iconst(1);
v.goTo(end);
v.mark(fail);
v.iconst(0);
v.mark(end);
}
return StackValue.onStack(Type.BOOLEAN_TYPE);
}
private StackValue generateEqualsForExpressionsOnStack(IElementType opToken, Type leftType, Type rightType) {
if (isNumberPrimitive(leftType) && leftType == rightType) {
return compareExpressionsOnStack(opToken, leftType);
......
......@@ -25,6 +25,7 @@ public class JetTypeMapper {
public static final Type TYPE_TYPEINFO = Type.getType(TypeInfo.class);
public static final Type TYPE_JET_OBJECT = Type.getType(JetObject.class);
public static final Type TYPE_CLASS = Type.getType(Class.class);
public static final Type TYPE_NOTHING = Type.getObjectType("jet/Nothing");
private final JetStandardLibrary standardLibrary;
private final BindingContext bindingContext;
......@@ -289,6 +290,9 @@ public class JetTypeMapper {
}
public Type mapType(@NotNull final JetType jetType, OwnerKind kind) {
if (jetType.equals(JetStandardClasses.getNothingType()) || jetType.equals(JetStandardClasses.getNullableNothingType())) {
return TYPE_NOTHING;
}
if (jetType.equals(standardLibrary.getIntType())) {
return Type.INT_TYPE;
}
......
......@@ -282,7 +282,7 @@ public abstract class StackValue {
@Override
public void condJump(Label label, boolean jumpIfFalse, InstructionAdapter v) {
if (value instanceof Boolean) {
boolean boolValue = ((Boolean) value).booleanValue();
boolean boolValue = (Boolean) value;
if (boolValue ^ jumpIfFalse) {
v.goTo(label);
}
......
......@@ -148,4 +148,14 @@ public class ControlStructuresTest extends CodegenTestCase {
public void testKt237() throws Exception {
blackBoxFile("regressions/kt237.jet");
}
public void testCompareToNull() throws Exception {
loadText("fun foo(a: String?, b: String?): Boolean = a == null && b !== null");
String text = generateToText();
assertTrue(!text.contains("java/lang/Object.equals"));
System.out.println(text);
final Method main = generateFunction();
assertEquals(true, main.invoke(null, null, "lala"));
assertEquals(false, main.invoke(null, null, null));
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册