2
0
Эх сурвалжийг харах

Allow Single div without cast to Float (#12039)

* [hl] Allow F32 div without cast to Float

* unsigned_op is not needed in f32 div

* Use Single in div result type when e1 and e2 are both Single

* Only do is_single check if OpDiv

* [tests] add Single type test and fix a out path

* [tests] do not use -fail in hxml(.stderr)
Yuxiao Mao 4 сар өмнө
parent
commit
b6b67ae072

+ 7 - 1
src/typing/operators.ml

@@ -305,7 +305,13 @@ let make_binop ctx op e1 e2 is_assign_op p =
 		| KFloat, KFloat ->
 		| KFloat, KFloat ->
 			result := tfloat
 			result := tfloat
 		| KNumParam t1, KNumParam t2 when Type.type_iseq t1 t2 ->
 		| KNumParam t1, KNumParam t2 when Type.type_iseq t1 t2 ->
-			if op <> OpDiv then result := t1
+			(match op with
+			| OpDiv ->
+				let is_single = (match follow e1.etype with TAbstract({a_path=[],"Single"},_) -> true | _ -> false) in
+				if is_single then result := t1
+			| _ ->
+				result := t1
+			)
 		| KNumParam _, KNumParam _ ->
 		| KNumParam _, KNumParam _ ->
 			result := tfloat
 			result := tfloat
 		| KNumParam t, KInt | KInt, KNumParam t ->
 		| KNumParam t, KInt | KInt, KNumParam t ->

+ 1 - 1
tests/misc/hl/projects/Issue11196/compile.hxml

@@ -1,3 +1,3 @@
 -m Issue11196
 -m Issue11196
--hl out.hl
+-hl out/main.hl
 -dce no
 -dce no

+ 17 - 0
tests/misc/hl/projects/Issue12039/Main.hx

@@ -0,0 +1,17 @@
+class Main {
+	static function main() {
+		var s1 : Single = 10.0;
+		var s2 : Single = 0.3;
+		var f1 : Float = 10.0;
+		var f2 : Float = 0.3;
+		$type(s1 + s2);
+		$type(s1 - s2);
+		$type(s1 * s2);
+		$type(s1 / s2);
+		$type(s1 / (f2 : Single));
+		$type((f1 : Single) / s2);
+		$type((f1 : Single) / (f2 : Single));
+		$type(f1 / s2);
+		$type(s1 / f2);
+	}
+}

+ 2 - 0
tests/misc/hl/projects/Issue12039/compile.hxml

@@ -0,0 +1,2 @@
+--main Main
+--hl out/main.hl

+ 9 - 0
tests/misc/hl/projects/Issue12039/compile.hxml.stderr

@@ -0,0 +1,9 @@
+Main.hx:7: characters 9-16 : Warning : Single
+Main.hx:8: characters 9-16 : Warning : Single
+Main.hx:9: characters 9-16 : Warning : Single
+Main.hx:10: characters 9-16 : Warning : Single
+Main.hx:11: characters 9-27 : Warning : Single
+Main.hx:12: characters 9-27 : Warning : Single
+Main.hx:13: characters 9-38 : Warning : Single
+Main.hx:14: characters 9-16 : Warning : Float
+Main.hx:15: characters 9-16 : Warning : Float