Browse Source

[jvm] cast to float/double if comparing Null<Float> with Int (#8577)

* cast to float if comparing Null<Float> with Int

* minor
Aleksandr Kuzmenko 6 years ago
parent
commit
096b447dee
2 changed files with 29 additions and 4 deletions
  1. 20 4
      src/generators/genjvm.ml
  2. 9 0
      tests/unit/src/unit/issues/Issue8538.hx

+ 20 - 4
src/generators/genjvm.ml

@@ -1030,14 +1030,30 @@ class texpr_to_jvm gctx (jc : JvmClass.builder) (jm : JvmMethod.builder) (return
 						jm#get_code#bconst (op = CmpNe)
 					)
 					(fun () ->
-						jm#cast ~not_null:true sig2;
-						self#texpr rvalue_any e2;
+						(match (get_unboxed_type sig1), sig2 with
+						| (TFloat | TDouble as unboxed_sig1), TInt ->
+							jm#cast ~not_null:true unboxed_sig1;
+							self#texpr rvalue_any e2;
+							jm#cast ~not_null:true unboxed_sig1
+						| _ ->
+							jm#cast ~not_null:true sig2;
+							self#texpr rvalue_any e2
+						);
 						self#boolop (self#do_compare op)
 					);
 				CmpNormal(CmpEq,TBool)
 			| true,false ->
 				self#texpr rvalue_any e1;
-				self#texpr rvalue_any e2;
+				let cast =
+					match sig1, (get_unboxed_type sig2) with
+					| TInt, (TFloat | TDouble as unboxed_sig2) ->
+						jm#cast ~not_null:true unboxed_sig2;
+						self#texpr rvalue_any e2;
+						(fun() -> jm#cast ~not_null:true unboxed_sig2)
+					| _ ->
+						self#texpr rvalue_any e2;
+						(fun() -> jm#cast ~not_null:true sig1)
+				in
 				jm#get_code#dup;
 				jm#if_then_else
 					(self#if_not_null sig2)
@@ -1047,7 +1063,7 @@ class texpr_to_jvm gctx (jc : JvmClass.builder) (jm : JvmMethod.builder) (return
 						jm#get_code#bconst (op = CmpNe);
 					)
 					(fun () ->
-						jm#cast ~not_null:true sig1;
+						cast();
 						self#boolop (self#do_compare op)
 					);
 				CmpNormal(CmpEq,TBool)

+ 9 - 0
tests/unit/src/unit/issues/Issue8538.hx

@@ -0,0 +1,9 @@
+package unit.issues;
+
+class Issue8538 extends unit.Test {
+	function test() {
+		var a:Null<Float> = 0;
+		t(a == 0);
+		t(0 == a);
+	}
+}