Browse Source

preserve type semantics of overloaded == and != if second type is a monomorph (closes #2871)

Simon Krajewski 11 years ago
parent
commit
7b6aa167b0
2 changed files with 8 additions and 2 deletions
  1. 0 2
      tests/unit/issues/Issue2871.hx
  2. 8 0
      typer.ml

+ 0 - 2
tests/unit/issues/Issue2871.hx

@@ -1,7 +1,6 @@
 package unit.issues;
 
 class Issue2871 extends Test {
-	#if !java
     function call(myUInt:Null<UInt> = null):Int {
         return myUInt == null ? 0 : myUInt;
     }
@@ -10,5 +9,4 @@ class Issue2871 extends Test {
 		eq(0, call(null));
 		eq(1, call((1:UInt)));
 	}
-	#end
 }

+ 8 - 0
typer.ml

@@ -1824,6 +1824,14 @@ let rec type_binop ctx op e1 e2 is_assign_op p =
 								else
 									type_eq EqStrict (TAbstract(a,pl)) t1;
 							end;
+							(* special case for == and !=: if the second type is a monomorph, assume that we want to unify
+							   it with the first type to preserve comparison semantics. *)
+							begin match op,follow t with
+								| (OpEq | OpNotEq),TMono _ ->
+									Type.unify (if left then e1.etype else e2.etype) t
+								| _ ->
+									()
+							end;
 							Type.unify t t2;
 							check_constraints ctx "" cf.cf_params monos (apply_params a.a_types pl) false cf.cf_pos;
 							cf,t2,r,o = OpAssignOp(op),Meta.has Meta.Commutative cf.cf_meta