瀏覽代碼

check abstract cast when unifying OpEq/OpNotEq (closes #2698)

Simon Krajewski 11 年之前
父節點
當前提交
44c2cfe1b0
共有 2 個文件被更改,包括 43 次插入4 次删除
  1. 36 0
      tests/unit/issues/Issue2698.hx
  2. 7 4
      typer.ml

+ 36 - 0
tests/unit/issues/Issue2698.hx

@@ -0,0 +1,36 @@
+package unit.issues;
+import unit.Test;
+
+private abstract IntString(String) {
+    @:from static function fromInt(i:Int):IntString return cast Std.string(i);
+    @:to function toInt():Int return Std.parseInt(this);
+}
+
+class Issue2698 extends Test {
+	function test() {
+		var a:IntString = 1;
+		var b:Int = 1;
+		var c:IntString = 2;
+		var d:Int = 2;
+		
+		t(a == a);
+		t(a == b);
+		f(a == c);
+		f(a == d);
+		
+		t(b == a);
+		t(b == b);
+		f(b == c);
+		f(b == d);
+		
+		f(c == a);
+		f(c == b);
+		t(c == c);
+		t(c == d);
+		
+		f(d == a);
+		f(d == b);
+		t(d == c);
+		t(d == d);
+	}
+}

+ 7 - 4
typer.ml

@@ -1726,12 +1726,15 @@ let rec type_binop ctx op e1 e2 is_assign_op p =
 		mk_op e1 e2 !result
 	| OpEq
 	| OpNotEq ->
-		(try
+		let e1,e2 = try
 			unify_raise ctx e1.etype e2.etype p;
 			(* we only have to check one type here, because unification fails if one is Void and the other is not *)
-			(match follow e2.etype with TAbstract({a_path=[],"Void"},_) -> error "Cannot compare Void" p | _ -> ())
-		with
-			Error (Unify _,_) -> unify ctx e2.etype e1.etype p);
+			(match follow e2.etype with TAbstract({a_path=[],"Void"},_) -> error "Cannot compare Void" p | _ -> ());
+			Codegen.Abstract.check_cast ctx e2.etype e1 p,e2
+		with Error (Unify _,_) ->
+			unify ctx e2.etype e1.etype p;
+			e1,Codegen.Abstract.check_cast ctx e1.etype e2 p
+		in
 		mk_op e1 e2 ctx.t.tbool
 	| OpGt
 	| OpGte