Repository navigation
Evaluate the source of a record spread whose fields are all overridden #9427
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
117aee3
316c515
b24282c
e7e5d3e
c8b0a31
3b332e5
c68d7d3
c4907ef
37bb2f0
eba91e0
311b970
ea85907
ef8c7b9
eb6b9e3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| - Fix a `let rec` record that refers to itself and is built after other expressions, as in `let rec r = {...f(), next: r}` or `let rec r = {log(); {next: r}}`, reading itself before it is initialized, and a record of a `let rec` group being read by a record built before it, as `a` in `let rec b = {next: a, v: 1} and a = {f: () => b, w: 2}`, before it is declared (#9466). |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| - The source of a record spread whose fields are all overridden is now checked in a `let rec` like any other expression, since it is evaluated. `let rec callback = {...wrap(callback), run: () => 1}`, which compiled with warning 23 and never called `wrap`, is now an error: remove the spread, or build the source without reading the binding. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| - Fix the source of a record spread (or JSX props spread) not being evaluated when every field is overridden, as in `{...f(), a: 1, b: 2}`. Warning 23 on such a spread now says that its source is still evaluated, instead of suggesting to remove the spread, unless the source is a variable. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -84,6 +84,130 @@ let with_block_source_loc lam block = | |
| | stmt :: rest -> | ||
| with_statement_source_loc (source_map_loc_of_lam lam) stmt :: rest | ||
|
|
||
| let rec const_block_tag (c : Lambda.structured_constant) = | ||
| match c with | ||
| | Const_block (tag_info, _) -> Some tag_info | ||
| | Const_some inner -> const_block_tag inner | ||
| | _ -> None | ||
|
|
||
| let rec is_dups_alias dups (lam : Lambda.t) = | ||
| match lam with | ||
| | Lvar src -> Set_ident.mem src dups | ||
| | Lprim {primitive = Psome | Psome_not_nest; args = [inner]} -> | ||
| is_dups_alias dups inner | ||
| | _ -> false | ||
|
|
||
| (* The tag of the block that [lam] builds at the end of a chain of sequences, | ||
| lets, recursive lets and [Psome] / [Psome_not_nest] option wrappers, with | ||
| the expressions evaluated to build it: those before it and its fields. | ||
| Such a chain is the one [Lam_pass_deep_flatten] leaves when it hoists the | ||
| bindings of a spread source out of a record. Both option wrappers preserve | ||
| the block's JS representation when their payload is a known block. The | ||
| block is built by [Pmakeblock], is a constant when its fields are all | ||
| constants (as [transl_record] makes [{...s, next: None}]), or is a shallow | ||
| copy built by [Pduprecord] followed by field assignments and ending in the | ||
| copy's variable (as [transl_record] makes for a partial spread of a record | ||
| with optional fields). *) | ||
| let tail_block (lam : Lambda.t) = | ||
| let rec find ~before ~dups (lam : Lambda.t) = | ||
| match lam with | ||
| | Lprim {primitive = Psome | Psome_not_nest; args = [body]} -> | ||
| find ~before ~dups body | ||
| | Llet (_, id, Lprim {primitive = Pduprecord; args}, body) -> | ||
| find ~before:(args @ before) ~dups:(Set_ident.add id dups) body | ||
| | Llet (_, id, rhs, body) when is_dups_alias dups rhs -> | ||
| find ~before ~dups:(Set_ident.add id dups) body | ||
| | Lsequence (Lprim {primitive = Psetfield _; args = [Lvar id; value]}, body) | ||
| when Set_ident.mem id dups -> | ||
| find ~before:(value :: before) ~dups body | ||
| | Lsequence (prefix, body) | Llet (_, _, prefix, body) -> | ||
| find ~before:(prefix :: before) ~dups body | ||
| | Lletrec (bindings, body) -> | ||
| find ~before:(List.map snd bindings @ before) ~dups body | ||
| | Lprim {primitive = Pmakeblock tag_info; args} -> | ||
| Some (tag_info, args @ before) | ||
| | Lconst c -> ( | ||
| match const_block_tag c with | ||
| | Some tag_info -> Some (tag_info, before) | ||
| | None -> None) | ||
| | Lvar id when Set_ident.mem id dups -> | ||
| Some (Lambda.blk_record [||] Immutable ~array:false, before) | ||
| | _ -> None | ||
| in | ||
| find ~before:[] ~dups:Set_ident.empty lam | ||
|
|
||
| (* Whether evaluating the expressions [evaluated] may read a binding of | ||
| [group]. A function reads none of them when it is built, whatever it refers | ||
| to; any other expression reads one when it refers to it. *) | ||
| let reads_any group (evaluated : Lambda.t list) = | ||
| List.exists | ||
| (fun (arg : Lambda.t) -> | ||
| match arg with | ||
| | Lfunction _ -> false | ||
| | _ -> Lam_hit.hit_variables group arg) | ||
| evaluated | ||
|
|
||
| (* The tag of the block that [lam] builds after evaluating other expressions, | ||
| when the block or the expressions before it may read one of the recursive | ||
| [all_bindings], as in [let rec r = {...f(), next: r}], whose spread source | ||
| [f()] is evaluated for its side effects first, or in | ||
| [let rec r = {...{let rec s = {next: r}; s}, next: base}], whose source | ||
| reads [r] while it is built. Such a binding is declared as a dummy before | ||
| those expressions run, and the block is copied into it. A bare block is | ||
| compiled into a dummy by the caller already. *) | ||
| let block_after_effects ~(all_bindings : (Ident.t * Lambda.t) list) | ||
| (lam : Lambda.t) = | ||
| match tail_block lam with | ||
| | Some (tag_info, evaluated) | ||
| when reads_any (Set_ident.of_list (List.map fst all_bindings)) evaluated -> | ||
| Some tag_info | ||
| | Some _ | None -> None | ||
|
|
||
| (* The expressions evaluated to build [lam] that may read a variable while | ||
| they are evaluated: those evaluated to build its block or, for any other | ||
| value, [lam] itself. A function reads none when it is built, so a group of | ||
| functions, the common [let rec], has none to look at. *) | ||
| let evaluated_reads (lam : Lambda.t) = | ||
| let evaluated = | ||
| match tail_block lam with | ||
| | Some (_, evaluated) -> evaluated | ||
| | None -> [lam] | ||
| in | ||
| List.filter | ||
| (fun (arg : Lambda.t) -> | ||
| match arg with | ||
| | Lfunction _ -> false | ||
| | _ -> true) | ||
| evaluated | ||
|
|
||
| (* The bindings of the recursive group [id_args], listed in the order they are | ||
| built, that a binding built before them reads, as [a] in | ||
| [let rec b = {next: Some(a), f: () => b} and a = {next: None, f: () => b}]. | ||
| Such a binding must be declared before that read, so it is built into a | ||
| dummy even when it could be declared directly where it is built. Only the | ||
| bindings built after a binding are looked for in what it reads, and only | ||
| when it evaluates something other than functions, so a group of functions | ||
| costs one walk over its bindings. *) | ||
| let read_before_built (id_args : (Ident.t * Lambda.t) list) = | ||
| let rec mark read_before (id_args : (Ident.t * Lambda.t) list) = | ||
| match id_args with | ||
| | [] -> read_before | ||
| | (_, arg) :: later -> ( | ||
| match evaluated_reads arg with | ||
| | [] -> mark read_before later | ||
| | evaluated -> | ||
| let read_before = | ||
| List.fold_left | ||
| (fun read_before (id, _) -> | ||
| if List.exists (Lam_hit.hit_variable id) evaluated then | ||
| Set_ident.add id read_before | ||
| else read_before) | ||
| read_before later | ||
| in | ||
| mark read_before later) | ||
| in | ||
| mark Set_ident.empty id_args | ||
|
|
||
| let args_either_function_or_const (args : Lambda.t list) = | ||
| List.for_all | ||
| (fun (x : Lambda.t) -> | ||
|
|
@@ -755,26 +879,13 @@ let compile output_prefix = | |
| | _ -> assert false))) | ||
| ls), | ||
| [] ) | ||
| | Lprim {primitive = Pmakeblock tag_info} -> ( | ||
| | Lprim {primitive = Pmakeblock tag_info} -> | ||
| (* Lconst should not appear here if we do [scc] | ||
| optimization, since it's faked recursive value, | ||
| however it would affect scope issues, we have to declare it first | ||
| *) | ||
| match compile_lambda {cxt with continuation = NeedValue Not_tail} arg with | ||
| | {block = b; value = Some v} -> | ||
| (* TODO: check recursive value .. | ||
| could be improved for simple cases | ||
| *) | ||
| ( Js_output.make | ||
| (List.append b | ||
| [ | ||
| S.exp | ||
| (E.runtime_call Primitive_modules.object_ "updateDummy" | ||
| [E.var id; v]); | ||
| ]), | ||
| [S.define_variable ~kind:Variable id (E.dummy_obj tag_info)] ) | ||
| | _ -> assert false) | ||
| | _ -> | ||
| compile_into_dummy cxt id tag_info arg | ||
| | _ -> ( | ||
| (* pathological case: | ||
| fail to capture taill call? | ||
| {[ let rec a = | ||
|
|
@@ -796,15 +907,42 @@ let compile output_prefix = | |
| fun _-> print_endline "hey"; v () | ||
| ]} | ||
| *) | ||
| (compile_lambda {cxt with continuation = Declare (Alias, id)} arg, []) | ||
| match block_after_effects ~all_bindings arg with | ||
| | Some tag_info -> compile_into_dummy cxt id tag_info arg | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For AGENTS.md reference: compiler/AGENTS.md:L160-L160 Useful? React with 👍 / 👎.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This case is no longer reachable on |
||
| | None -> | ||
| (compile_lambda {cxt with continuation = Declare (Alias, id)} arg, [])) | ||
| (* [id] is declared as a dummy object, which the block [arg] evaluates to is | ||
| copied into *) | ||
| and compile_into_dummy cxt id tag_info arg = | ||
| match compile_lambda {cxt with continuation = NeedValue Not_tail} arg with | ||
| | {block = b; value = Some v} -> | ||
| (* TODO: check recursive value .. | ||
| could be improved for simple cases | ||
| *) | ||
| ( Js_output.make | ||
| (List.append b | ||
| [ | ||
| S.exp | ||
| (E.runtime_call Primitive_modules.object_ "updateDummy" | ||
| [E.var id; v]); | ||
| ]), | ||
| [S.define_variable ~kind:Variable id (E.dummy_obj tag_info)] ) | ||
| | _ -> assert false | ||
| and compile_recursive_lets_aux cxt (id_args : (Ident.t * Lambda.t) list) : | ||
| Js_output.t = | ||
| (* #1716 *) | ||
| let read_before = read_before_built id_args in | ||
| let output_code, ids = | ||
| List.fold_right | ||
| (fun (ident, arg) (acc, ids) -> | ||
| (* A binding read before it is built that builds no block, such as | ||
| an array, cannot be a dummy and is declared where it is built *) | ||
| let code, declare_ids = | ||
| compile_recursive_let ~all_bindings:id_args cxt ident arg | ||
| match tail_block arg with | ||
| | Some (tag_info, _) when Set_ident.mem ident read_before -> | ||
| compile_into_dummy cxt ident tag_info arg | ||
| | Some _ | None -> | ||
| compile_recursive_let ~all_bindings:id_args cxt ident arg | ||
|
Comment on lines
+944
to
+945
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When a later recursive binding partially spreads a record with optional fields, AGENTS.md reference: compiler/AGENTS.md:L160-L160 Useful? React with 👍 / 👎.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Real. When a later binding in a Fixed in 5d51c88 (after rebasing onto |
||
| in | ||
| (Js_output.append_output code acc, List.append declare_ids ids)) | ||
| id_args (Js_output.dummy, []) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For
type rec node = {left: option<node>, right: option<node>, value: int}and an effectfultick, the accepted initializerlet rec n = Some({...{let rec tmp = {left: Some(tmp), right: n, value: tick()}; tmp}, left: None, right: None, value: 1})has an outerPsome_not_nest. This fallback therefore hides both the terminal record and its eager prefix read fromtail_block;compile_recursive_letdeclaresnnormally, and constructingtmpreads it before initialization, throwingReferenceError. Previously the fully overridden spread source was dropped, leaving a constant initializer without that read. Traverse the allocation-free option wrapper when locating the block, and cover this with and without optimization.AGENTS.md reference: compiler/AGENTS.md:L160-L160
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Real. Wrapping the recursive record in a non-nesting
Some(...)produced an outerPsome_not_nest, sotail_blockreturnedNoneand declarednaftertmpread it, throwingReferenceError: Cannot access 'n' before initialization.Fixed in 5d51c88:
tail_blocknow traversesPsome_not_nestwrappers when locating the terminal block and its prefix reads, sonis declared as a dummy beforetmpis constructed and filled viaupdateDummy. Regression tests:optionalPrefixRead(module level) andlocalOptionalPrefixRead(local to a function) inrecord_spread_source_effect_test.res.