Skip to content

Commit c6bc5be

Browse files
cristianoccknitt
andauthored
Don't emit f(undefined) when passing unit (). (#6459)
* RFC: don't emit f(undefined) This changes code generation so it emits `f()` instead of `f(undefined)` when the function takes a single argument and its value is `undefined`. * Track when undefined is coming from `()`. * isUnit * Update CHANGELOG.md * Update jscomp/core/js_dump.ml Co-authored-by: Christoph Knittel <[email protected]> --------- Co-authored-by: Christoph Knittel <[email protected]>
1 parent e2da4ef commit c6bc5be

File tree

99 files changed

+326
-314
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

99 files changed

+326
-314
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
- Add [`Deno`](https://deno.land/api?s=Deno) to reserved names, so that modules named `Deno` don't clash with the globally exposed `Deno` object. https://github.com/rescript-lang/rescript-compiler/pull/6428
4242
- Disable ESLint/TSLint on gentype outputs properly. https://github.com/rescript-lang/rescript-compiler/pull/6442
4343
- Improve `rescript` CLI to use `stdout`/`stderr` appropriately for help command's message. https://github.com/rescript-lang/rescript-compiler/pull/6439
44+
- Generate `f()` instead of `f(undefined)` for `f()` https://github.com/rescript-lang/rescript-compiler/pull/6459
4445

4546
# 11.0.0-rc.4
4647

jscomp/core/j.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ and expression_desc =
159159
*)
160160
| Number of number
161161
| Object of property_map
162-
| Undefined
162+
| Undefined of {isUnit: bool}
163163
| Null
164164
| Await of expression
165165

jscomp/core/js_analyzer.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ let free_variables_of_expression st =
8282

8383
let rec no_side_effect_expression_desc (x : J.expression_desc) =
8484
match x with
85-
| Undefined | Null | Bool _ | Var _ -> true
85+
| Undefined _ | Null | Bool _ | Var _ -> true
8686
| Fun _ -> true
8787
| Number _ -> true (* Can be refined later *)
8888
| Static_index (obj, (_name : string), (_pos : int32 option)) ->
@@ -153,7 +153,7 @@ let rec eq_expression ({ expression_desc = x0 } : J.expression)
153153
({ expression_desc = y0 } : J.expression) =
154154
match x0 with
155155
| Null -> y0 = Null
156-
| Undefined -> y0 = Undefined
156+
| Undefined x -> y0 = Undefined x
157157
| Number (Int { i }) -> (
158158
match y0 with Number (Int { i = j }) -> i = j | _ -> false)
159159
| Number (Float _) -> false

jscomp/core/js_cmj_format.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ let get_result midVal =
113113
match midVal.persistent_closed_lambda with
114114
| Some
115115
(Lconst
116-
(Const_js_null | Const_js_undefined | Const_js_true | Const_js_false))
116+
(Const_js_null | Const_js_undefined _ | Const_js_true | Const_js_false))
117117
| None ->
118118
midVal
119119
| Some _ ->

jscomp/core/js_dump.ml

+16-9
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ let exp_need_paren (e : J.expression) =
160160
| Raw_js_code { code_info = Stmt _ }
161161
| Length _ | Call _ | Caml_block_tag _ | Seq _ | Static_index _ | Cond _
162162
| Bin _ | Is_null_or_undefined _ | String_index _ | Array_index _
163-
| String_append _ | Var _ | Undefined | Null | Str _ | Array _
163+
| String_append _ | Var _ | Undefined _ | Null | Str _ | Array _
164164
| Optional_block _ | Caml_block _ | FlatCall _ | Typeof _ | Number _
165165
| Js_not _ | Bool _ | New _ ->
166166
false
@@ -519,7 +519,7 @@ and expression_desc cxt ~(level : int) f x : cxt =
519519
| Null ->
520520
P.string f L.null;
521521
cxt
522-
| Undefined ->
522+
| Undefined _ ->
523523
P.string f L.undefined;
524524
cxt
525525
| Var v -> vident cxt f v
@@ -569,7 +569,14 @@ and expression_desc cxt ~(level : int) f x : cxt =
569569
pp_function ~is_method ~return_unit ~async cxt f
570570
~fn_state:(No_name { single_arg = true })
571571
params body env
572-
| _ -> arguments cxt f el)
572+
| _ ->
573+
let el = match el with
574+
| [e] when e.expression_desc = Undefined {isUnit = true} ->
575+
(* omit passing undefined when the call is f() *)
576+
[]
577+
| _ ->
578+
el in
579+
arguments cxt f el)
573580
| _, _ ->
574581
let len = List.length el in
575582
if 1 <= len && len <= 8 then (
@@ -736,7 +743,7 @@ and expression_desc cxt ~(level : int) f x : cxt =
736743
let fields =
737744
Ext_list.array_list_filter_map fields el (fun f x ->
738745
match x.expression_desc with
739-
| Undefined -> None
746+
| Undefined _ -> None
740747
| _ -> Some (Js_op.Lit f, x))
741748
in
742749
expression_desc cxt ~level f (Object fields))
@@ -774,7 +781,7 @@ and expression_desc cxt ~(level : int) f x : cxt =
774781
| _ ->
775782
Ext_list.filter_map tails (fun (f, x) ->
776783
match x.expression_desc with
777-
| Undefined when is_optional f -> None
784+
| Undefined _ when is_optional f -> None
778785
| _ -> Some (f, x))
779786
in
780787
if untagged then
@@ -1176,7 +1183,7 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt =
11761183
in
11771184
semi f;
11781185
cxt
1179-
| Undefined ->
1186+
| Undefined _ ->
11801187
return_sp f;
11811188
semi f;
11821189
cxt
@@ -1270,16 +1277,16 @@ and function_body (cxt : cxt) f ~return_unit (b : J.block) : unit =
12701277
| If
12711278
( bool,
12721279
then_,
1273-
[ { statement_desc = Return { expression_desc = Undefined } } ] ) ->
1280+
[ { statement_desc = Return { expression_desc = Undefined _ } } ] ) ->
12741281
ignore
12751282
(statement false cxt f
12761283
{ s with statement_desc = If (bool, then_, []) }
12771284
: cxt)
1278-
| Return { expression_desc = Undefined } -> ()
1285+
| Return { expression_desc = Undefined _ } -> ()
12791286
| Return exp when return_unit ->
12801287
ignore (statement false cxt f (S.exp exp) : cxt)
12811288
| _ -> ignore (statement false cxt f s : cxt))
1282-
| [ s; { statement_desc = Return { expression_desc = Undefined } } ] ->
1289+
| [ s; { statement_desc = Return { expression_desc = Undefined _ } } ] ->
12831290
ignore (statement false cxt f s : cxt)
12841291
| s :: r ->
12851292
let cxt = statement false cxt f s in

jscomp/core/js_exp_make.ml

+32-32
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ let var ?comment id : t = { expression_desc = Var (Id id); comment }
6060
Invariant: it should not call an external module .. *)
6161

6262
let js_global ?comment (v : string) = var ?comment (Ext_ident.create_js v)
63-
let undefined : t = { expression_desc = Undefined; comment = None }
63+
let undefined : t = { expression_desc = Undefined {isUnit = false}; comment = None }
6464
let nil : t = { expression_desc = Null; comment = None }
6565

6666
let call ?comment ~info e0 args : t =
@@ -183,7 +183,7 @@ let is_array (e0 : t) : t =
183183
let new_ ?comment e0 args : t =
184184
{ expression_desc = New (e0, Some args); comment }
185185

186-
let unit : t = { expression_desc = Undefined; comment = None }
186+
let unit : t = { expression_desc = Undefined {isUnit = true}; comment = None }
187187

188188
(* let math ?comment v args : t =
189189
{comment ; expression_desc = Math(v,args)} *)
@@ -256,17 +256,17 @@ let dummy_obj ?comment (info : Lam_tag_info.t) : t =
256256
*)
257257
let rec seq ?comment (e0 : t) (e1 : t) : t =
258258
match (e0.expression_desc, e1.expression_desc) with
259-
| ( ( Seq (a, { expression_desc = Number _ | Undefined })
260-
| Seq ({ expression_desc = Number _ | Undefined }, a) ),
259+
| ( ( Seq (a, { expression_desc = Number _ | Undefined _ })
260+
| Seq ({ expression_desc = Number _ | Undefined _ }, a) ),
261261
_ ) ->
262262
seq ?comment a e1
263-
| _, Seq ({ expression_desc = Number _ | Undefined }, a) ->
263+
| _, Seq ({ expression_desc = Number _ | Undefined _ }, a) ->
264264
(* Return value could not be changed*)
265265
seq ?comment e0 a
266-
| _, Seq (a, ({ expression_desc = Number _ | Undefined } as v)) ->
266+
| _, Seq (a, ({ expression_desc = Number _ | Undefined _ } as v)) ->
267267
(* Return value could not be changed*)
268268
seq ?comment (seq e0 a) v
269-
| (Number _ | Var _ | Undefined), _ -> e1
269+
| (Number _ | Var _ | Undefined _), _ -> e1
270270
| _ -> { expression_desc = Seq (e0, e1); comment }
271271

272272
let fuse_to_seq x xs = if xs = [] then x else Ext_list.fold_left xs x seq
@@ -567,22 +567,22 @@ let str_equal (txt0:string) (delim0:External_arg_spec.delim) txt1 delim1 =
567567

568568
let rec triple_equal ?comment (e0 : t) (e1 : t) : t =
569569
match (e0.expression_desc, e1.expression_desc) with
570-
| ( (Null | Undefined),
570+
| ( (Null | Undefined _),
571571
(Bool _ | Number _ | Typeof _ | Fun _ | Array _ | Caml_block _) )
572572
when no_side_effect e1 ->
573573
false_
574574
| ( (Bool _ | Number _ | Typeof _ | Fun _ | Array _ | Caml_block _),
575-
(Null | Undefined) )
575+
(Null | Undefined _) )
576576
when no_side_effect e0 ->
577577
false_
578578
| Number (Int { i = i0; _ }), Number (Int { i = i1; _ }) -> bool (i0 = i1)
579579
| Optional_block (a, _), Optional_block (b, _) -> triple_equal ?comment a b
580-
| Undefined, Optional_block _
581-
| Optional_block _, Undefined
582-
| Null, Undefined
583-
| Undefined, Null ->
580+
| Undefined _, Optional_block _
581+
| Optional_block _, Undefined _
582+
| Null, Undefined _
583+
| Undefined _, Null ->
584584
false_
585-
| Null, Null | Undefined, Undefined -> true_
585+
| Null, Null | Undefined _, Undefined _ -> true_
586586
| _ -> { expression_desc = Bin (EqEqEq, e0, e1); comment }
587587

588588
let bin ?comment (op : J.binop) (e0 : t) (e1 : t) : t =
@@ -654,7 +654,7 @@ let and_ ?comment (e1 : t) (e2 : t) : t =
654654
| Var i, Bin (And, l, ({ expression_desc = Var j; _ } as r))
655655
when Js_op_util.same_vident i j ->
656656
{ e2 with expression_desc = Bin (And, r, l) }
657-
| ( Bin (NotEqEq, { expression_desc = Var i }, { expression_desc = Undefined }),
657+
| ( Bin (NotEqEq, { expression_desc = Var i }, { expression_desc = Undefined _ }),
658658
Bin
659659
( EqEqEq,
660660
{ expression_desc = Var j },
@@ -702,7 +702,7 @@ let not (e : t) : t =
702702

703703
let not_empty_branch (x : t) =
704704
match x.expression_desc with
705-
| Number (Int { i = 0l }) | Undefined -> false
705+
| Number (Int { i = 0l }) | Undefined _ -> false
706706
| _ -> true
707707

708708
let rec econd ?comment (pred : t) (ifso : t) (ifnot : t) : t =
@@ -735,8 +735,8 @@ let rec econd ?comment (pred : t) (ifso : t) (ifnot : t) : t =
735735
econd (or_ pred (not pred1)) ifso ifso1
736736
| Js_not e, _, _ when not_empty_branch ifnot -> econd ?comment e ifnot ifso
737737
| ( _,
738-
Seq (a, { expression_desc = Undefined }),
739-
Seq (b, { expression_desc = Undefined }) ) ->
738+
Seq (a, { expression_desc = Undefined _ }),
739+
Seq (b, { expression_desc = Undefined _ }) ) ->
740740
seq (econd ?comment pred a b) undefined
741741
| _ ->
742742
if Js_analyzer.eq_expression ifso ifnot then
@@ -746,7 +746,7 @@ let rec econd ?comment (pred : t) (ifso : t) (ifnot : t) : t =
746746
let rec float_equal ?comment (e0 : t) (e1 : t) : t =
747747
match (e0.expression_desc, e1.expression_desc) with
748748
| Number (Int { i = i0; _ }), Number (Int { i = i1 }) -> bool (i0 = i1)
749-
| Undefined, Undefined -> true_
749+
| Undefined _, Undefined _ -> true_
750750
(* | (Bin(Bor,
751751
{expression_desc = Number(Int {i = 0l; _})},
752752
({expression_desc = Caml_block_tag _; _} as a ))
@@ -983,11 +983,11 @@ let rec int_comp (cmp : Lam_compat.comparison) ?comment (e0 : t) (e1 : t) =
983983
args,
984984
call_info );
985985
}
986-
| Ceq, Optional_block _, Undefined | Ceq, Undefined, Optional_block _ ->
986+
| Ceq, Optional_block _, Undefined _ | Ceq, Undefined _, Optional_block _ ->
987987
false_
988988
| Ceq, _, _ -> int_equal e0 e1
989-
| Cneq, Optional_block _, Undefined
990-
| Cneq, Undefined, Optional_block _
989+
| Cneq, Optional_block _, Undefined _
990+
| Cneq, Undefined _, Optional_block _
991991
| Cneq, Caml_block _, Number _
992992
| Cneq, Number _, Caml_block _ ->
993993
true_
@@ -1281,36 +1281,36 @@ let is_null ?comment (x : t) = triple_equal ?comment x nil
12811281
let is_undef ?comment x = triple_equal ?comment x undefined
12821282

12831283
let for_sure_js_null_undefined (x : t) =
1284-
match x.expression_desc with Null | Undefined -> true | _ -> false
1284+
match x.expression_desc with Null | Undefined _ -> true | _ -> false
12851285

12861286
let is_null_undefined ?comment (x : t) : t =
12871287
match x.expression_desc with
1288-
| Null | Undefined -> true_
1288+
| Null | Undefined _ -> true_
12891289
| Number _ | Array _ | Caml_block _ -> false_
12901290
| _ -> { comment; expression_desc = Is_null_or_undefined x }
12911291

12921292
let eq_null_undefined_boolean ?comment (a : t) (b : t) =
12931293
match (a.expression_desc, b.expression_desc) with
1294-
| ( (Null | Undefined),
1294+
| ( (Null | Undefined _),
12951295
(Bool _ | Number _ | Typeof _ | Fun _ | Array _ | Caml_block _) ) ->
12961296
false_
12971297
| ( (Bool _ | Number _ | Typeof _ | Fun _ | Array _ | Caml_block _),
1298-
(Null | Undefined) ) ->
1298+
(Null | Undefined _) ) ->
12991299
false_
1300-
| Null, Undefined | Undefined, Null -> false_
1301-
| Null, Null | Undefined, Undefined -> true_
1300+
| Null, Undefined _ | Undefined _, Null -> false_
1301+
| Null, Null | Undefined _, Undefined _ -> true_
13021302
| _ -> { expression_desc = Bin (EqEqEq, a, b); comment }
13031303

13041304
let neq_null_undefined_boolean ?comment (a : t) (b : t) =
13051305
match (a.expression_desc, b.expression_desc) with
1306-
| ( (Null | Undefined),
1306+
| ( (Null | Undefined _),
13071307
(Bool _ | Number _ | Typeof _ | Fun _ | Array _ | Caml_block _) ) ->
13081308
true_
13091309
| ( (Bool _ | Number _ | Typeof _ | Fun _ | Array _ | Caml_block _),
1310-
(Null | Undefined) ) ->
1310+
(Null | Undefined _) ) ->
13111311
true_
1312-
| Null, Null | Undefined, Undefined -> false_
1313-
| Null, Undefined | Undefined, Null -> true_
1312+
| Null, Null | Undefined _, Undefined _ -> false_
1313+
| Null, Undefined _ | Undefined _, Null -> true_
13141314
| _ -> { expression_desc = Bin (NotEqEq, a, b); comment }
13151315

13161316
(** TODO: in the future add a flag

jscomp/core/js_fold.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ class fold =
169169
| Object _x0 ->
170170
let _self = _self#property_map _x0 in
171171
_self
172-
| Undefined -> _self
172+
| Undefined _ -> _self
173173
| Null -> _self
174174
| Await _x0 ->
175175
let _self = _self#expression _x0 in

jscomp/core/js_of_lam_option.ml

+3-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ type option_unwrap_time = Static_unwrapped | Runtime_maybe_unwrapped
3636
*)
3737
let none : J.expression = E.undefined
3838

39-
let is_none_static (arg : J.expression_desc) = arg = Undefined
39+
let is_none_static (arg : J.expression_desc) = match arg with
40+
| Undefined _ -> true
41+
| _ -> false
4042

4143
let is_not_none (e : J.expression) : J.expression =
4244
let desc = e.expression_desc in

jscomp/core/js_pass_flatten_and_mark_dead.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ let subst_map (substitution : J.expression Hash_ident.t) =
192192
let _, e, bindings =
193193
Ext_list.fold_left ls (0, [], []) (fun (i, e, acc) x ->
194194
match x.expression_desc with
195-
| Var _ | Number _ | Str _ | J.Bool _ | Undefined ->
195+
| Var _ | Number _ | Str _ | J.Bool _ | Undefined _ ->
196196
(* TODO: check the optimization *)
197197
(i + 1, x :: e, acc)
198198
| _ ->
@@ -257,7 +257,7 @@ let subst_map (substitution : J.expression Hash_ident.t) =
257257
match Ext_list.nth_opt ls (Int32.to_int i) with
258258
| Some
259259
({
260-
expression_desc = J.Var _ | Number _ | Str _ | Undefined;
260+
expression_desc = J.Var _ | Number _ | Str _ | Undefined _;
261261
} as x) ->
262262
x
263263
| None | Some _ -> super.expression self x)

jscomp/core/js_record_fold.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ let expression_desc : 'a. ('a, expression_desc) fn =
175175
| Object _x0 ->
176176
let st = property_map _self st _x0 in
177177
st
178-
| Undefined -> st
178+
| Undefined _ -> st
179179
| Null -> st
180180
| Await _x0 ->
181181
let st = _self.expression _self st _x0 in

jscomp/core/js_record_iter.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ let expression_desc : expression_desc fn =
131131
| Caml_block_tag (_x0, _tag) -> _self.expression _self _x0
132132
| Number _ -> ()
133133
| Object _x0 -> property_map _self _x0
134-
| Undefined -> ()
134+
| Undefined _ -> ()
135135
| Null -> ()
136136
| Await _x0 -> _self.expression _self _x0
137137

jscomp/core/js_record_map.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ let expression_desc : expression_desc fn =
173173
| Object _x0 ->
174174
let _x0 = property_map _self _x0 in
175175
Object _x0
176-
| Undefined as v -> v
176+
| Undefined _ as v -> v
177177
| Null as v -> v
178178
| Await _x0 ->
179179
let _x0 = _self.expression _self _x0 in

jscomp/core/js_stmt_make.ml

+6-6
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ let rec block ?comment (b : J.block) : t =
4444
(* It's a statement, we can discard some values *)
4545
let rec exp ?comment (e : E.t) : t =
4646
match e.expression_desc with
47-
| Seq ({ expression_desc = Number _ | Undefined }, b)
48-
| Seq (b, { expression_desc = Number _ | Undefined }) ->
47+
| Seq ({ expression_desc = Number _ | Undefined _ }, b)
48+
| Seq (b, { expression_desc = Number _ | Undefined _ }) ->
4949
exp ?comment b
50-
| Number _ | Undefined -> block []
50+
| Number _ | Undefined _ -> block []
5151
(* TODO: we can do more *)
5252
(* | _ when is_pure e -> block [] *)
5353
| _ -> { statement_desc = Exp e; comment }
@@ -63,10 +63,10 @@ let declare_variable ?comment ?ident_info ~kind (ident : Ident.t) : t =
6363
}
6464

6565
let define_variable ?comment ?ident_info ~kind (v : Ident.t)
66-
(exp : J.expression) : t =
67-
if exp.expression_desc = Undefined then
66+
(exp : J.expression) : t = match exp.expression_desc with
67+
| Undefined _ ->
6868
declare_variable ?comment ?ident_info ~kind v
69-
else
69+
| _ ->
7070
let property : J.property = kind in
7171
let ident_info : J.ident_info =
7272
match ident_info with None -> { used_stats = NA } | Some x -> x

0 commit comments

Comments
 (0)