소스 검색

prefer casting rhs to lhs when unifying equality operators

Simon Krajewski 3 달 전
부모
커밋
c025f769c1
1개의 변경된 파일6개의 추가작업 그리고 1개의 파일을 삭제
  1. 6 1
      src/typing/operators.ml

+ 6 - 1
src/typing/operators.ml

@@ -335,9 +335,14 @@ let make_binop ctx op e1 e2 is_assign_op p =
 		let e1,e2 = try
 		let e1,e2 = try
 			(* we only have to check one type here, because unification fails if one is Void and the other is not *)
 			(* 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"},_) -> raise_typing_error "Cannot compare Void" p | _ -> ());
 			(match follow e2.etype with TAbstract({a_path=[],"Void"},_) -> raise_typing_error "Cannot compare Void" p | _ -> ());
+			(* Try casting the rhs to the lhs first *)
+			e1,AbstractCast.cast_or_unify_raise ctx e1.etype e2 p
+		with Error ({ err_message = Unify _ } as err) -> try
 			AbstractCast.cast_or_unify_raise ctx e2.etype e1 p,e2
 			AbstractCast.cast_or_unify_raise ctx e2.etype e1 p,e2
 		with Error { err_message = Unify _ } ->
 		with Error { err_message = Unify _ } ->
-			e1,AbstractCast.cast_or_unify ctx e1.etype e2 p
+			(* If both fail we want to report the first one because that's more natural *)
+			raise_or_display_error ctx err;
+			e1,e2
 		in
 		in
 		begin match e1.eexpr, e2.eexpr with
 		begin match e1.eexpr, e2.eexpr with
 		| TConst TNull , _ | _ , TConst TNull -> ()
 		| TConst TNull , _ | _ , TConst TNull -> ()