Skip to content

Commit d48d577

Browse files
cknittclaude
andauthored
Drop a pure condition when both branches of an if are empty (#9024)
A switch whose remaining cases all fall through to the same code left their test behind as a bare statement, such as `x === null;`, and the empty block it left in an enclosing if blocked merging the conditions. JS modified (switch with cases that fall through): Example: type rank = Uninitialized | Visited | Ranked(int) let f3 = (lhs: rank, rhs: rank) => switch (lhs, rhs) { | (Ranked(x), Ranked(y)) => Pervasives.compare(x, y) | _ => assert(false) } Before (js): tests/tests/src/fun_pattern_match.res if (typeof lhs !== "object") { lhs === "Uninitialized"; } else { if (typeof rhs === "object") { return Primitive_int.compare(lhs._0, rhs._0); } rhs === "Uninitialized"; } throw { After (js): tests/tests/src/fun_pattern_match.res if (typeof lhs === "object" && typeof rhs === "object") { return Primitive_int.compare(lhs._0, rhs._0); } throw { Review: Is a condition with side effects still evaluated? Yes, only conditions that no_side_effect_expression accepts are dropped. Claude-Session: https://claude.ai/code/session_015yE4gCmTmJKVecrVt1PU8C Signed-off-by: Christoph Knittel <ck@cca.io> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 20b31e4 commit d48d577

21 files changed

Lines changed: 231 additions & 89 deletions
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Stop emitting bare conditions like `x === null;` and empty `if`/`else` branches.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Fix negated float comparisons with NaN: `!(x < 0.0)` compiled to `x >= 0.0`, which is `false` for NaN.

‎compiler/core/js_exp_make.ml‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1555,8 +1555,15 @@ let bool_comp (cmp : Lambda.comparison) ?comment (e0 : t) (e1 : t) =
15551555
bin ?comment (Lam_compile_util.jsop_of_comp cmp) e0 e1)
15561556
| _, _ -> bin ?comment (Lam_compile_util.jsop_of_comp cmp) e0 e1
15571557

1558+
let float_comp (cmp : Lambda.comparison) ?comment (e0 : t) (e1 : t) =
1559+
match cmp with
1560+
| Ceq | Cneq -> int_comp cmp ?comment e0 e1
1561+
| Clt | Cgt | Cle | Cge ->
1562+
bin ?comment (Lam_compile_util.jsop_of_float_comp cmp) e0 e1
1563+
1564+
(* The operands may be of any type, so they may be NaN *)
15581565
let js_comp cmp ?comment e0 e1 =
1559-
bin ?comment (Lam_compile_util.jsop_of_comp cmp) e0 e1
1566+
bin ?comment (Lam_compile_util.jsop_of_float_comp cmp) e0 e1
15601567

15611568
let rec int32_lsr ?comment (e1 : J.expression) (e2 : J.expression) :
15621569
J.expression =

‎compiler/core/js_exp_make.mli‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,8 @@ val bigint_div : checked:bool -> ?comment:string -> t -> t -> t
244244

245245
val bigint_mod : checked:bool -> ?comment:string -> t -> t -> t
246246

247+
val float_comp : Lambda.comparison -> ?comment:string -> t -> t -> t
248+
247249
val js_comp : Lambda.comparison -> ?comment:string -> t -> t -> t
248250

249251
val not : t -> t

‎compiler/core/js_op.ml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ type binop =
3838
| Le
3939
| Gt
4040
| Ge
41+
| Float_lt
42+
| Float_le
43+
| Float_gt
44+
| Float_ge
45+
(* Printed as [<], [<=], [>], [>=], for operands that may be NaN, so
46+
that negating them can't swap the operator: [!(a < b)] is not
47+
[a >= b] when [a] or [b] is NaN *)
4148
| Bor
4249
| Bxor
4350
| Band

‎compiler/core/js_op_util.ml‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ let op_prec (op : Js_op.binop) =
3434
| Or -> (3, 3, 3)
3535
| And -> (4, 4, 4)
3636
| EqEqEq | NotEqEq -> (8, 8, 9)
37-
| Gt | Ge | Lt | Le | InstanceOf -> (9, 9, 10)
37+
| Gt | Ge | Lt | Le | Float_lt | Float_le | Float_gt | Float_ge | InstanceOf
38+
->
39+
(9, 9, 10)
3840
| Bor -> (5, 5, 5)
3941
| Bxor -> (6, 6, 6)
4042
| Band -> (7, 7, 7)
@@ -62,10 +64,10 @@ let op_str (op : Js_op.binop) =
6264
| And -> "&&"
6365
| EqEqEq -> "==="
6466
| NotEqEq -> "!=="
65-
| Lt -> "<"
66-
| Le -> "<="
67-
| Gt -> ">"
68-
| Ge -> ">="
67+
| Lt | Float_lt -> "<"
68+
| Le | Float_le -> "<="
69+
| Gt | Float_gt -> ">"
70+
| Ge | Float_ge -> ">="
6971
| InstanceOf -> "instanceof"
7072

7173
let update_used_stats (ident_info : J.ident_info) used_stats =

‎compiler/core/js_pass_flatten.ml‎

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,20 @@ let flatten_map =
4949
S.block
5050
(Ext_list.map args (fun arg -> self.statement self (S.exp arg)))
5151
| Exp {expression_desc = Cond (a, b, c); comment; source_loc} ->
52-
{
53-
statement_desc =
54-
If
55-
( a,
56-
[self.statement self (S.exp b)],
57-
[self.statement self (S.exp c)] );
58-
comment;
59-
source_loc;
60-
}
52+
(* A branch whose value is pure leaves nothing to run *)
53+
let branch e =
54+
if Js_analyzer.no_side_effect_expression e then []
55+
else [self.statement self (S.exp e)]
56+
in
57+
let statement_desc : J.statement_desc =
58+
match (branch b, branch c) with
59+
| [], [] ->
60+
if Js_analyzer.no_side_effect_expression a then Block []
61+
else Exp a
62+
| [], ifnot -> If (E.not a, ifnot, [])
63+
| ifso, ifnot -> If (a, ifso, ifnot)
64+
in
65+
{statement_desc; comment; source_loc}
6166
| Exp
6267
{
6368
expression_desc = Bin (Eq, a, ({expression_desc = Seq _; _} as v));

‎compiler/core/js_stmt_make.ml‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,12 +246,24 @@ let if_ ?comment ?declaration ?else_ (e : J.expression) (then_ : J.block) : t =
246246
let declared = ref false in
247247
let rec aux ?comment (e : J.expression) (ifso : J.block) (ifnot : J.block) : t
248248
=
249+
(* An empty block, such as a dropped condition of a nested [if], leaves
250+
its branch empty *)
251+
let is_not_empty (s : t) =
252+
match s.statement_desc with
253+
| Block [] -> false
254+
| _ -> true
255+
in
256+
let ifso = List.filter is_not_empty ifso in
257+
let ifnot = List.filter is_not_empty ifnot in
249258
match (e.expression_desc, ifnot) with
250259
| Bool boolean, _ -> block (if boolean then ifso else ifnot)
251260
| Js_not pred_not, _ :: _ -> aux ?comment pred_not ifnot ifso
252261
| _ -> (
253262
match (ifso, ifnot) with
254-
| [], [] -> exp e
263+
| [], [] ->
264+
(* Both branches are empty: only a condition with side effects
265+
needs to be kept *)
266+
if Js_analyzer.no_side_effect_expression e then empty_stmt else exp e
255267
| [], _ -> aux ?comment (E.not e) ifnot [] (*Make sure no infinite loop*)
256268
| ( [{statement_desc = Return ret_ifso; _}],
257269
[{statement_desc = Return ret_ifnot; _}] ) ->

‎compiler/core/lam_compile.ml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1512,7 +1512,7 @@ let compile output_prefix =
15121512
( Js_exp_make.remove_pure_sub_exp out1,
15131513
Js_exp_make.remove_pure_sub_exp out2 )
15141514
with
1515-
| None, None -> Js_output.make (Ext_list.append_one b (S.exp e))
1515+
| None, None -> Js_output.make (Ext_list.append_one b (S.if_ e []))
15161516
(* FIX #1762 *)
15171517
| Some out1, Some out2 ->
15181518
Js_output.make b ~value:(E.econd e out1 out2)

‎compiler/core/lam_compile_primitive.ml‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,13 +337,17 @@ let translate output_prefix loc (cxt : Lam_compile_context.t)
337337
match args with
338338
| [e1; e2] -> E.bool_comp cmp e1 e2
339339
| _ -> assert false)
340-
| Pfloatcomp cmp | Pintcomp cmp -> (
340+
| Pintcomp cmp -> (
341341
(* Global Builtin Exception is an int, like
342342
[Not_found] or [Invalid_argument] ?
343343
*)
344344
match args with
345345
| [e1; e2] -> E.int_comp cmp e1 e2
346346
| _ -> assert false)
347+
| Pfloatcomp cmp -> (
348+
match args with
349+
| [e1; e2] -> E.float_comp cmp e1 e2
350+
| _ -> assert false)
347351
| Pbigintcomp cmp -> (
348352
match args with
349353
| [e1; e2] -> E.bigint_comp cmp e1 e2

0 commit comments

Comments
 (0)