Jelajahi Sumber

[php] fix checking floats for equality (fixes #4260)

Alexander Kuzmenko 8 tahun lalu
induk
melakukan
b80f2ab9b0

+ 1 - 0
extra/CHANGES.txt

@@ -26,6 +26,7 @@
 	php : fixed typed cast in assign operations (#5135)
 	php : fixed exception thrown by `Reflect.fields(o)` when `o` is `Class<T>` (#5608)
 	php : fixed json encoding of empty objects (#5015)
+	php : fixed checking floats for equality (#4260)
 
 2016-11-29: 3.4.0-RC1
 

+ 6 - 0
src/generators/genphp.ml

@@ -71,6 +71,11 @@ type context = {
 
 let follow = Abstract.follow_with_abstracts
 
+(**
+	Check if specified expression is of `Float` type
+*)
+let is_float expr = match follow expr.etype with TAbstract ({ a_path = ([], "Float") }, _) -> true | _ -> false
+
 let join_class_path path separator =
 	let result = match fst path, snd path with
 	| [], s -> s
@@ -1212,6 +1217,7 @@ and gen_expr ctx e =
 			end else if
 				   ((se1 = "Int" || se1 = "Null<Int>") && (se2 = "Int" || se2 = "Null<Int>"))
 				|| ((se1 = "Float" || se1 = "Null<Float>") && (se2 = "Float" || se2 = "Null<Float>"))
+				&& not (is_float e1 && is_float e2)
 			then begin
 				gen_field_op ctx e1;
 				spr ctx s_phop;

+ 1 - 1
src/generators/genphp7.ml

@@ -296,7 +296,7 @@ let is_unknown_type (target:Type.t) = is_dynamic_type target || is_generic_param
 *)
 let need_boot_equal expr1 expr2 =
 	(is_int expr1 && (is_float expr2 || is_unknown_type expr2.etype))
-	|| (is_float expr1 && (is_int expr2 || is_unknown_type expr2.etype))
+	|| (is_float expr1 && (is_float expr2 || is_int expr2 || is_unknown_type expr2.etype))
 	|| (is_unknown_type expr1.etype && (is_int expr2 || is_float expr2))
 	|| (is_unknown_type expr1.etype && is_unknown_type expr2.etype)
 

+ 13 - 0
tests/unit/src/unit/issues/Issue4260.hx

@@ -0,0 +1,13 @@
+package unit.issues;
+
+class Issue4260 extends unit.Test {
+	var floatValue:Float = 0.0;
+	var intValue:Float = 0;
+	var nullValue:Float;
+
+	function test() {
+        t(intValue == floatValue);
+		f(intValue == nullValue);
+		f(floatValue == nullValue);
+	}
+}