Skip to content

Commit 9491fd9

Browse files
committed
Turn off transformation for closures inside loops.
The scope of `var` is per-function, requiring a transformation for closures inside loops when capturing loop variables. This PR turns off the transformation. This PR should go after #6102
1 parent 3c9c6a1 commit 9491fd9

28 files changed

+154
-403
lines changed

jscomp/core/j.ml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,15 +253,14 @@ and statement_desc =
253253
(* Function declaration and Variable declaration *)
254254
| Exp of expression
255255
| If of expression * block * block
256-
| While of label option * expression * block * Js_closure.t
256+
| While of label option * expression * block
257257
(* check if it contains loop mutable values, happens in nested loop *)
258258
| ForRange of
259259
for_ident_expression option
260260
* finish_ident_expression
261261
* for_ident
262262
* for_direction
263263
* block
264-
* Js_closure.t
265264
| Continue of label
266265
| Break (* only used when inline a fucntion *)
267266
| Return of expression

jscomp/core/js_closure.ml

Lines changed: 0 additions & 31 deletions
This file was deleted.

jscomp/core/js_closure.mli

Lines changed: 0 additions & 35 deletions
This file was deleted.

jscomp/core/js_dump.ml

Lines changed: 8 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -166,14 +166,6 @@ let exp_need_paren (e : J.expression) =
166166
false
167167
| Await _ -> false
168168

169-
let comma_idents (cxt : cxt) f ls = iter_lst cxt f ls Ext_pp_scope.ident comma
170-
171-
let pp_paren_params (inner_cxt : cxt) (f : Ext_pp.t) (lexical : Ident.t list) :
172-
unit =
173-
P.string f L.lparen;
174-
let (_ : cxt) = comma_idents inner_cxt f lexical in
175-
P.string f L.rparen
176-
177169
(** Print as underscore for unused vars, may not be
178170
needed in the future *)
179171
(* let ipp_ident cxt f id (un_used : bool) =
@@ -383,10 +375,9 @@ and pp_function ~return_unit ~async ~is_method cxt (f : P.t) ~fn_state
383375
P.space f;
384376
P.brace_vgroup f 1 (fun _ -> function_body ~return_unit cxt f b)
385377
in
386-
let lexical : Set_ident.t = Js_fun_env.get_lexical_scope env in
387-
let enclose lexical =
388-
let handle lexical =
389-
if Set_ident.is_empty lexical then (
378+
let enclose () =
379+
let handle () =
380+
(
390381
match fn_state with
391382
| Is_return ->
392383
return_sp f;
@@ -410,46 +401,10 @@ and pp_function ~return_unit ~async ~is_method cxt (f : P.t) ~fn_state
410401
P.space f;
411402
ignore (Ext_pp_scope.ident inner_cxt f x : cxt);
412403
param_body ())
413-
else
414-
(* print our closure as
415-
{[(function(x,y){ return function(..){...}} (x,y))]}
416-
Maybe changed to `let` in the future
417-
*)
418-
let lexical = Set_ident.elements lexical in
419-
(match fn_state with
420-
| Is_return -> return_sp f
421-
| No_name _ -> ()
422-
| Name_non_top name | Name_top name ->
423-
ignore (pp_var_assign inner_cxt f name : cxt));
424-
if async then P.string f L.await;
425-
P.string f L.lparen;
426-
P.string f (L.function_async ~async);
427-
pp_paren_params inner_cxt f lexical;
428-
P.brace_vgroup f 0 (fun _ ->
429-
return_sp f;
430-
P.string f (L.function_async ~async);
431-
P.space f;
432-
(match fn_state with
433-
| Is_return | No_name _ -> ()
434-
| Name_non_top x | Name_top x ->
435-
ignore (Ext_pp_scope.ident inner_cxt f x));
436-
param_body ());
437-
pp_paren_params inner_cxt f lexical;
438-
P.string f L.rparen;
439-
match fn_state with
440-
| Is_return | No_name _ -> () (* expression *)
441-
| _ -> semi f
442-
(* has binding, a statement *)
443404
in
444-
handle
445-
(match fn_state with
446-
| (Name_top name | Name_non_top name) when Set_ident.mem lexical name
447-
->
448-
(*TODO: when calculating lexical we should not include itself *)
449-
Set_ident.remove lexical name
450-
| _ -> lexical)
405+
handle ()
451406
in
452-
enclose lexical;
407+
enclose ();
453408
outer_cxt
454409

455410
(* Assume the cond would not change the context,
@@ -1048,7 +1003,7 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt =
10481003
P.string f L.else_;
10491004
P.space f;
10501005
brace_block cxt f s2)
1051-
| While (label, e, s, _env) ->
1006+
| While (label, e, s) ->
10521007
(* FIXME: print scope as well *)
10531008
(match label with
10541009
| Some i ->
@@ -1076,7 +1031,7 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt =
10761031
let cxt = brace_block cxt f s in
10771032
semi f;
10781033
cxt
1079-
| ForRange (for_ident_expression, finish, id, direction, s, env) ->
1034+
| ForRange (for_ident_expression, finish, id, direction, s) ->
10801035
let action cxt =
10811036
P.vgroup f 0 (fun _ ->
10821037
let cxt =
@@ -1147,24 +1102,7 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt =
11471102
in
11481103
brace_block cxt f s)
11491104
in
1150-
let lexical = Js_closure.get_lexical_scope env in
1151-
if Set_ident.is_empty lexical then action cxt
1152-
else
1153-
(* unlike function,
1154-
[print for loop] has side effect,
1155-
we should take it out
1156-
*)
1157-
let inner_cxt = Ext_pp_scope.merge cxt lexical in
1158-
let lexical = Set_ident.elements lexical in
1159-
P.vgroup f 0 (fun _ ->
1160-
P.string f L.lparen;
1161-
P.string f L.function_;
1162-
pp_paren_params inner_cxt f lexical;
1163-
let cxt = P.brace_vgroup f 0 (fun _ -> action inner_cxt) in
1164-
pp_paren_params inner_cxt f lexical;
1165-
P.string f L.rparen;
1166-
semi f;
1167-
cxt)
1105+
action cxt
11681106
| Continue s ->
11691107
continue f s;
11701108
cxt

jscomp/core/js_fold.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,12 +212,12 @@ class fold =
212212
let _self = _self#block _x1 in
213213
let _self = _self#block _x2 in
214214
_self
215-
| While (_x0, _x1, _x2, _x3) ->
215+
| While (_x0, _x1, _x2) ->
216216
let _self = option (fun _self -> _self#label) _self _x0 in
217217
let _self = _self#expression _x1 in
218218
let _self = _self#block _x2 in
219219
_self
220-
| ForRange (_x0, _x1, _x2, _x3, _x4, _x5) ->
220+
| ForRange (_x0, _x1, _x2, _x3, _x4) ->
221221
let _self =
222222
option (fun _self -> _self#for_ident_expression) _self _x0
223223
in

jscomp/core/js_fun_env.ml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ type immutable_mask =
4444

4545
type t = {
4646
mutable unbounded : Set_ident.t;
47-
mutable bound_loop_mutable_values : Set_ident.t;
4847
used_mask : bool array;
4948
immutable_mask : immutable_mask;
5049
}
@@ -58,7 +57,6 @@ let make ?immutable_mask n =
5857
(match immutable_mask with
5958
| Some x -> Immutable_mask x
6059
| None -> All_immutable_and_no_tail_call);
61-
bound_loop_mutable_values = Set_ident.empty;
6260
}
6361

6462
let no_tailcall x =
@@ -92,13 +90,3 @@ let set_unbounded env v =
9290
(* if Set_ident.is_empty env.bound then *)
9391
env.unbounded <- v
9492
(* else assert false *)
95-
96-
let set_lexical_scope env bound_loop_mutable_values =
97-
env.bound_loop_mutable_values <- bound_loop_mutable_values
98-
99-
let get_lexical_scope env = env.bound_loop_mutable_values
100-
101-
(* TODO: can be refined if it
102-
only enclose toplevel variables
103-
*)
104-
(* let is_empty t = Set_ident.is_empty t.unbounded *)

jscomp/core/js_fun_env.mli

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,6 @@ val no_tailcall : t -> bool list
3636

3737
val set_unbounded : t -> Set_ident.t -> unit
3838

39-
val set_lexical_scope : t -> Set_ident.t -> unit
40-
41-
val get_lexical_scope : t -> Set_ident.t
42-
4339
(* val to_string : t -> string *)
4440

4541
val mark_unused : t -> int -> unit

jscomp/core/js_pass_scope.ml

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,6 @@ let record_scope_pass =
173173
due to the recursive thing
174174
*)
175175
Js_fun_env.set_unbounded env closured_idents';
176-
let lexical_scopes =
177-
Set_ident.(inter closured_idents' state.loop_mutable_values)
178-
in
179-
Js_fun_env.set_lexical_scope env lexical_scopes;
180176
(* tailcall , note that these varibles are used in another pass *)
181177
{
182178
state with
@@ -242,7 +238,7 @@ let record_scope_pass =
242238
statement =
243239
(fun self state x ->
244240
match x.statement_desc with
245-
| ForRange (_, _, loop_id, _, _, a_env) ->
241+
| ForRange (_, _, loop_id, _, _) ->
246242
(* TODO: simplify definition of For *)
247243
let {
248244
defined_idents = defined_idents';
@@ -274,8 +270,6 @@ let record_scope_pass =
274270
(diff closured_idents' defined_idents')
275271
state.loop_mutable_values)
276272
in
277-
let () = Js_closure.set_lexical_scope a_env lexical_scope in
278-
(* set scope *)
279273
{
280274
state with
281275
used_idents = Set_ident.union state.used_idents used_idents';
@@ -293,7 +287,7 @@ let record_scope_pass =
293287
closured_idents =
294288
Set_ident.union state.closured_idents lexical_scope;
295289
}
296-
| While (_label, pred, body, _env) ->
290+
| While (_label, pred, body) ->
297291
with_in_loop
298292
(self.block self
299293
(with_in_loop (self.expression self state pred) true)

jscomp/core/js_record_fold.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,12 +218,12 @@ let statement_desc : 'a. ('a, statement_desc) fn =
218218
let st = _self.block _self st _x1 in
219219
let st = _self.block _self st _x2 in
220220
st
221-
| While (_x0, _x1, _x2, _x3) ->
221+
| While (_x0, _x1, _x2) ->
222222
let st = option label _self st _x0 in
223223
let st = _self.expression _self st _x1 in
224224
let st = _self.block _self st _x2 in
225225
st
226-
| ForRange (_x0, _x1, _x2, _x3, _x4, _x5) ->
226+
| ForRange (_x0, _x1, _x2, _x3, _x4) ->
227227
let st = option for_ident_expression _self st _x0 in
228228
let st = finish_ident_expression _self st _x1 in
229229
let st = _self.for_ident _self st _x2 in

jscomp/core/js_record_iter.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,11 @@ let statement_desc : statement_desc fn =
159159
_self.expression _self _x0;
160160
_self.block _self _x1;
161161
_self.block _self _x2
162-
| While (_x0, _x1, _x2, _x3) ->
162+
| While (_x0, _x1, _x2) ->
163163
option label _self _x0;
164164
_self.expression _self _x1;
165165
_self.block _self _x2
166-
| ForRange (_x0, _x1, _x2, _x3, _x4, _x5) ->
166+
| ForRange (_x0, _x1, _x2, _x3, _x4) ->
167167
option for_ident_expression _self _x0;
168168
finish_ident_expression _self _x1;
169169
_self.for_ident _self _x2;

jscomp/core/js_record_map.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,18 +216,18 @@ let statement_desc : statement_desc fn =
216216
let _x1 = _self.block _self _x1 in
217217
let _x2 = _self.block _self _x2 in
218218
If (_x0, _x1, _x2)
219-
| While (_x0, _x1, _x2, _x3) ->
219+
| While (_x0, _x1, _x2) ->
220220
let _x0 = option label _self _x0 in
221221
let _x1 = _self.expression _self _x1 in
222222
let _x2 = _self.block _self _x2 in
223-
While (_x0, _x1, _x2, _x3)
224-
| ForRange (_x0, _x1, _x2, _x3, _x4, _x5) ->
223+
While (_x0, _x1, _x2)
224+
| ForRange (_x0, _x1, _x2, _x3, _x4) ->
225225
let _x0 = option for_ident_expression _self _x0 in
226226
let _x1 = finish_ident_expression _self _x1 in
227227
let _x2 = _self.for_ident _self _x2 in
228228
let _x3 = for_direction _self _x3 in
229229
let _x4 = _self.block _self _x4 in
230-
ForRange (_x0, _x1, _x2, _x3, _x4, _x5)
230+
ForRange (_x0, _x1, _x2, _x3, _x4)
231231
| Continue _x0 ->
232232
let _x0 = label _self _x0 in
233233
Continue _x0

jscomp/core/js_stmt_make.ml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -318,17 +318,15 @@ let if_ ?comment ?declaration ?else_ (e : J.expression) (then_ : J.block) : t =
318318
let assign ?comment id e : t =
319319
{ statement_desc = J.Exp (E.assign (E.var id) e); comment }
320320

321-
let while_ ?comment ?label ?env (e : E.t) (st : J.block) : t =
322-
let env = match env with None -> Js_closure.empty () | Some x -> x in
323-
{ statement_desc = While (label, e, st, env); comment }
321+
let while_ ?comment ?label (e : E.t) (st : J.block) : t =
322+
{ statement_desc = While (label, e, st); comment }
324323

325-
let for_ ?comment ?env for_ident_expression finish_ident_expression id direction
324+
let for_ ?comment for_ident_expression finish_ident_expression id direction
326325
(b : J.block) : t =
327-
let env = match env with None -> Js_closure.empty () | Some x -> x in
328326
{
329327
statement_desc =
330328
ForRange
331-
(for_ident_expression, finish_ident_expression, id, direction, b, env);
329+
(for_ident_expression, finish_ident_expression, id, direction, b);
332330
comment;
333331
}
334332

jscomp/core/js_stmt_make.mli

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,12 @@ val assign : ?comment:string -> J.ident -> J.expression -> t
133133
val while_ :
134134
?comment:string ->
135135
?label:J.label ->
136-
?env:Js_closure.t ->
137136
J.expression ->
138137
J.block ->
139138
t
140139

141140
val for_ :
142141
?comment:string ->
143-
?env:Js_closure.t ->
144142
J.for_ident_expression option ->
145143
J.finish_ident_expression ->
146144
J.for_ident ->

0 commit comments

Comments
 (0)