Skip to content

Commit

Permalink
[cs] add cast in int division filter if one of operands isn't exactly…
Browse files Browse the repository at this point in the history
… Int (closes HaxeFoundation#3543)
  • Loading branch information
nadako committed Nov 6, 2014
1 parent b1e92a6 commit 800b8f8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
16 changes: 14 additions & 2 deletions gencommon.ml
Original file line number Diff line number Diff line change
Expand Up @@ -10494,6 +10494,10 @@ struct

let is_int = like_int

let is_exactly_int t = match follow t with
| TAbstract ({ a_path=[],"Int" }, []) -> true
| _ -> false

let default_implementation gen catch_int_div =
let basic = gen.gcon.basic in
let rec run e =
Expand All @@ -10504,10 +10508,18 @@ struct
{ eexpr = TField(_, FStatic({ cl_path = ([], "Std") }, { cf_name = "int" })) },
[ ({ eexpr = TBinop((Ast.OpDiv as op), e1, e2) } as ebinop ) ]
) when catch_int_div && is_int e1.etype && is_int e2.etype ->
{ ebinop with eexpr = TBinop(op, run e1, run e2); etype = basic.tint }
let e = { ebinop with eexpr = TBinop(op, run e1, run e2); etype = basic.tint } in
if not (is_exactly_int e1.etype && is_exactly_int e2.etype) then
mk_cast basic.tint e
else
e
| TCast( ({ eexpr = TBinop((Ast.OpDiv as op), e1, e2) } as ebinop ), _ )
| TCast( ({ eexpr = TBinop(( (Ast.OpAssignOp Ast.OpDiv) as op), e1, e2) } as ebinop ), _ ) when catch_int_div && is_int e1.etype && is_int e2.etype && is_int e.etype ->
{ ebinop with eexpr = TBinop(op, run e1, run e2); etype = basic.tint }
let e = { ebinop with eexpr = TBinop(op, run e1, run e2); etype = basic.tint } in
if not (is_exactly_int e1.etype && is_exactly_int e2.etype) then
mk_cast basic.tint e
else
e
| _ -> Type.map_expr run e
in
run
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/src/unit/issues/Issue3543.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package unit.issues;

class Issue3543 extends Test {
function test() {
var a = Std.int((3 : UInt) / 2);
eq(1, a);
}
}

0 comments on commit 800b8f8

Please sign in to comment.