Skip to content

Commit

Permalink
Allow Single div without cast to Float (#12039)
Browse files Browse the repository at this point in the history
* [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)
  • Loading branch information
yuxiaomao authored Mar 6, 2025
1 parent 98b4326 commit b6b67ae
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/typing/operators.ml
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,13 @@ let make_binop ctx op e1 e2 is_assign_op p =
| KFloat, KFloat ->
result := tfloat
| 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 _ ->
result := tfloat
| KNumParam t, KInt | KInt, KNumParam t ->
Expand Down
2 changes: 1 addition & 1 deletion tests/misc/hl/projects/Issue11196/compile.hxml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
-m Issue11196
-hl out.hl
-hl out/main.hl
-dce no
17 changes: 17 additions & 0 deletions tests/misc/hl/projects/Issue12039/Main.hx
Original file line number Diff line number Diff line change
@@ -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 changes: 2 additions & 0 deletions tests/misc/hl/projects/Issue12039/compile.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--main Main
--hl out/main.hl
9 changes: 9 additions & 0 deletions tests/misc/hl/projects/Issue12039/compile.hxml.stderr
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit b6b67ae

Please sign in to comment.