Skip to content

Commit a278540

Browse files
cknittclaude
andauthored
Export nested make values flat, derived from the signature (#8904)
* Export nested make values flat, derived from the signature Every `make` in a nested module of a file's final signature is also exported as `A$B$make`, and other modules read it through that name. React Server Components only pass a client module's top-level exports to the client, so `<Sidebar.Provider />` no longer reads `.make` from a client reference. Both sides decide from the signature alone, which replaces `@res.hoistedFunction` and its `.cmj` table (#8402); see #8901. Example (rescript): // Sidebar.res module Provider = { @react.component let make = (~children) => children } // Page.res let page = <Sidebar.Provider> {React.null} </Sidebar.Provider> Before (js): // Page.res.js JsxRuntime.jsx(Sidebar.Provider.make, {children: null}) After (js): // Sidebar.res.js let Provider$make = Sidebar$Provider; export { Provider, Provider$make } // Page.res.js JsxRuntime.jsx(Sidebar.Provider$make, {children: null}) Source: After from tests/build_tests/react_ppx/src/nested_component_*.res.js in this diff. Before is master's fallback for a nested read without a `.cmj` hoisting entry. Breaking: `@res.hoistedFunction` is gone; it now reports the unused-attribute warning 101. Nested functions other than `make` are no longer exported flat. Consumers read `Lib.A$make` instead of `Lib.A.make`, so test spies or mocks that replace `make` on the module object no longer intercept those reads. Review: Why can't a `.resi` edit leave consumers stale, as in #8886? The flat export set is a function of the signature, so it changes exactly when the `.cmi` does, and both build systems already rebuild dependents then. Changing only `Sidebar.res` behind an unchanged `.resi` (a module alias, an external, a functor application, `include`) kept the `.cmi` byte-identical and every flat export in a scratch build. Review: Do the aliases defeat tree-shaking? When the module field holds a top-level binding, the alias is `let A$B$make = make`. Only fields without one (a wrapped value such as `Some(make)`, a functor result, an awaited module) fall back to a property read. Review: What about name clashes? A flat name already used by a top-level binding is a compile error, as it was for `@res.hoistedFunction`. Area: lambda codegen frontend typechecker playground tests Output: changed programs with nested `make` values gain flat exports and read them from other modules; 26 committed outputs change, all additive apart from the rewritten reads, and 3 go with the removed tests Formatting: unchanged Formats: cmi PPX: unchanged Editor: unchanged Errors: none, because the new clash error is an inline raise_errorf, outside the catalog's scope Tests: +tests/build_tests/react_ppx/src/nested_component_producer.res +tests/build_tests/react_ppx/src/nested_component_consumer.res +tests/build_tests/super_errors/fixtures/flat_make_export_collision.res ~tests/tests/src/flat_make_export*.res (from hoisted_function_attr*.res) ~tests/tests/src/import2.res ~tests/build_tests/super_errors/fixtures/warning_101_bs_unused_attribute.res -tests/build_tests/super_errors/fixtures/hoisted_function_*.res Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013GUg1NbeoJMi6t6V89yBWh Signed-off-by: Christoph Knittel <ck@cca.io> * Check flat export names only against exported bindings A flat `A$B$make` export now only clashes with another export. A binding hidden by the interface may have the same JS name: the printer renames the local and exports the alias under the flat name. Example (rescript): // Lib.resi exposes A.B.make and both, but not \"A$B$make" module A = { module B = { let make = () => 1 } } let \"A$B$make" = () => Math.random() > 2. ? 0 : 2 let both = () => A.B.make() + \"A$B$make"() After (js): function A$B$make() { ... } let A$B$make$1 = make; export { A, both, A$B$make$1 as A$B$make } Source: tests/tests/src/flat_make_export_hidden.mjs in this diff; the previous commit rejected this file. Review: Codex finding on #8904, lam_compile_main.ml:186. Area: lambda tests Output: changed tests/tests/src/flat_make_export_*.mjs only, for the new test Formatting: unchanged Formats: none PPX: unchanged Editor: unchanged Tests: +tests/tests/src/flat_make_export_hidden.res ~tests/tests/src/flat_make_export_test.res ~tests/build_tests/super_errors/expected/flat_make_export_collision.res.expected PR: #8904 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013GUg1NbeoJMi6t6V89yBWh Signed-off-by: Christoph Knittel <ck@cca.io> * Carry flat make paths in Translmod.implementation The paths are a field of `Translmod.implementation`, built from the final signature by `transl_implementation`, and `Lam_compile_main.compile` takes the whole record. Each step of a path is a `{pos; name}` record instead of an `(int * string)` pair. Consumer reads and dynamic imports share `Translmod.flat_make_name`, and the aliases are plain `(ident, value)` pairs. Review: fhammerschmidt on #8904, lam_compile_main.mli:33, asked for a labeled argument or a named type instead of `(int * string) list list`. Area: lambda codegen playground tests Output: unchanged, node scripts/test.js -all rebuilds every test project with no generated file changing Formatting: unchanged Formats: none PPX: unchanged Editor: unchanged Tests: none, because the change is a refactor covered by the existing flat_make_export and nested_component tests PR: #8904 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013GUg1NbeoJMi6t6V89yBWh Signed-off-by: Christoph Knittel <ck@cca.io> * Read flat exports through re-exports and awaited modules A flat export of a module that re-exports another file's module, as in `module Provider = Sidebar.Provider`, now aliases that file's flat export instead of reading `make` from its module object. Reads below a local bound to an awaited module, `module D = await Sidebar`, use the flat names too. Both cases read `make` from a React Server Components client reference before. Example (rescript): // Reexport.res, with a .resi exposing Provider.make module Provider = Sidebar.Provider // Page.res let load = async () => { module D = await Sidebar <D.Provider> {React.null} </D.Provider> } Before (js): // Reexport.res.js let Provider$make = Provider.make; // Page.res.js let D = await import("./Sidebar.res.js"); return JsxRuntime.jsx(D.Provider.make, {children: null}); After (js): // Reexport.res.js let Provider$make = Sidebar.Provider$make; // Page.res.js let D = await import("./Sidebar.res.js"); return JsxRuntime.jsx(D.Provider$make, {children: null}); Source: bsc of the previous commit and of this one on a scratch copy of the two files; the same shapes are in tests/build_tests/react_ppx/src/nested_component_{reexport,consumer}.res.js. Review: fhammerschmidt's Next.js end-to-end run on #8904 failed prerendering for exactly these two cases (rescript-lang/end-to-end#16). Review: `module P = await Sidebar.Group` still reads `P.Provider.make`: `P` is Sidebar's `Group` export, so its flat names are out of reach without changing what `P` holds. Area: lambda codegen tests Output: changed tests/tests/src/Import.mjs, where an alias now reads the awaited module directly (`O.make` instead of `N1.O.make`), and the new test outputs Formatting: unchanged Formats: none PPX: unchanged Editor: unchanged Tests: +tests/build_tests/react_ppx/src/nested_component_reexport.res ~tests/build_tests/react_ppx/src/nested_component_consumer.res +tests/tests/src/flat_make_export_reexport.res ~tests/tests/src/flat_make_export_test.res PR: #8904 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013GUg1NbeoJMi6t6V89yBWh Signed-off-by: Christoph Knittel <ck@cca.io> --------- Signed-off-by: Christoph Knittel <ck@cca.io> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 2d0a9f0 commit a278540

95 files changed

Lines changed: 1129 additions & 779 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Export every nested `make` as a flat value such as `A$B$make` and read it that way from other modules, so nested JSX components work as React Server Components client references.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Remove `@res.hoistedFunction`, which nested `make` flat exports replace; other nested functions are no longer exported flat.

‎compiler/core/js_cmj_format.ml‎

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -45,26 +45,14 @@ type keyed_cmj_value = {
4545

4646
type keyed_cmj_values = keyed_cmj_value array
4747

48-
type hoisted_export = {
49-
path: string list; (** Exact source-level module path segments. *)
50-
export_name: string;
51-
(** Flattened compiler name for the export, before JS identifier
52-
conversion: the emitted export applies [Ext_ident.convert] to it
53-
(a hoisted [Operator.\"+"] is stored as [Operator$+] and exported
54-
as [Operator$$plus]), and so must any consumer that reconstructs
55-
the JS export name. *)
56-
}
57-
5848
type t = {
5949
values: keyed_cmj_values;
60-
hoisted_exports: hoisted_export array;
6150
pure: bool;
6251
package_spec: Js_packages_info.t;
6352
case: Ext_js_file_kind.case;
6453
}
6554

66-
let make ~(values : cmj_value Map_string.t) ~hoisted_exports ~effect_
67-
~package_spec ~case : t =
55+
let make ~(values : cmj_value Map_string.t) ~effect_ ~package_spec ~case : t =
6856
{
6957
values =
7058
Map_string.bindings values
@@ -75,7 +63,6 @@ let make ~(values : cmj_value Map_string.t) ~hoisted_exports ~effect_
7563
persistent_closed_lambda = v.persistent_closed_lambda;
7664
})
7765
|> Array.of_list;
78-
hoisted_exports = Array.of_list hoisted_exports;
7966
pure = effect_ = None;
8067
package_spec;
8168
case;
@@ -173,13 +160,6 @@ let query_by_name (cmj_table : t) name : keyed_cmj_value =
173160
let values = cmj_table.values in
174161
binary_search values name
175162

176-
let find_hoisted_export (cmj_table : t) path =
177-
Array.find_map
178-
(fun value ->
179-
if List.equal Ext_string.equal value.path path then Some value.export_name
180-
else None)
181-
cmj_table.hoisted_exports
182-
183163
type path = string
184164

185165
type cmj_load_info = {

‎compiler/core/js_cmj_format.mli‎

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,36 +61,22 @@ type keyed_cmj_value = {
6161
persistent_closed_lambda: Lambda.t option;
6262
}
6363

64-
type hoisted_export = {
65-
path: string list; (** Exact source-level module path segments. *)
66-
export_name: string;
67-
(** Flattened compiler name for the export, before JS identifier
68-
conversion: the emitted export applies [Ext_ident.convert] to it
69-
(a hoisted [Operator.\"+"] is stored as [Operator$+] and exported
70-
as [Operator$$plus]), and so must any consumer that reconstructs
71-
the JS export name. *)
72-
}
73-
7464
type t = {
7565
values: keyed_cmj_value array;
76-
hoisted_exports: hoisted_export array;
7766
pure: bool;
7867
package_spec: Js_packages_info.t;
7968
case: Ext_js_file_kind.case;
8069
}
8170

8271
val make :
8372
values:cmj_value Map_string.t ->
84-
hoisted_exports:hoisted_export list ->
8573
effect_:effect_ ->
8674
package_spec:Js_packages_info.t ->
8775
case:Ext_js_file_kind.case ->
8876
t
8977

9078
val query_by_name : t -> string -> keyed_cmj_value
9179

92-
val find_hoisted_export : t -> string list -> string option
93-
9480
val single_na : arity
9581

9682
val from_file : string -> t

‎compiler/core/js_implementation.ml‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ let after_parsing_impl ppf outputprefix (ast : Parsetree.structure) =
129129
Lam_compile_env.reset ();
130130
let env = Res_compmisc.initial_env ~modulename () in
131131
Env.set_unit_name modulename;
132-
let typedtree, coercion, _, _ =
132+
let typedtree, coercion, finalenv, signature =
133133
Typemod.type_implementation_more
134134
?check_exists:
135135
(if !((Js_config.current ()).force_cmi) then None else Some ())
@@ -141,13 +141,14 @@ let after_parsing_impl ppf outputprefix (ast : Parsetree.structure) =
141141
Printtyped.implementation_with_coercion typedtree_coercion;
142142
(if !((Js_config.current ()).cmi_only) then Warnings.check_fatal ()
143143
else
144-
let {Translmod.lambda; exports; hoisted_functions} =
145-
Translmod.transl_implementation modulename typedtree_coercion
144+
let implementation =
145+
Translmod.transl_implementation modulename finalenv signature
146+
typedtree_coercion
146147
in
148+
print_if ppf (Clflags.current ()).dump_rawlambda Printlambda.lambda
149+
implementation.lambda;
147150
let js_program =
148-
print_if_pipe ppf (Clflags.current ()).dump_rawlambda
149-
Printlambda.lambda lambda
150-
|> Lam_compile_main.compile outputprefix exports hoisted_functions
151+
Lam_compile_main.compile outputprefix implementation
151152
in
152153
if not !((Js_config.current ()).cmj_only) then
153154
Lam_compile_main.lambda_as_module js_program outputprefix);

‎compiler/core/lam_compile.ml‎

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -255,31 +255,31 @@ let compile output_prefix =
255255
256256
Pfield "make" (Pfield "B" (Pfield "A" (Lglobal_module Other)))
257257
258-
Normal compilation does not look up the full path. It only queries the
259-
first field, "A", and then emits the remaining fields as JS property
260-
access: Other.A.B.make. The "A" lookup may include Submodule arity data,
261-
but it does not say whether A.B.make has a separate root-level export.
262-
263-
Hoisted functions need that extra question. For them, query the separate
264-
hoisted-values table with an unambiguous key for source path A.B.make. If
265-
present, the table returns the root-level JS export name, for example
266-
A$B$make. Normal export metadata still lives in the regular .cmj values
267-
table. *)
268-
let rec extract_field_path segments primitive args =
258+
Every nested [make] is also exported under a flat name (see
259+
[Translmod.implementation]), so it is read as Other.A$B$make rather
260+
than through the module objects. The same holds below a local bound to
261+
the awaited module, [let d = await import(Other)]. Other reads query
262+
only the first field, "A", and emit the remaining fields as JS property
263+
access. *)
264+
let rec extract_field_path awaited_units segments primitive args =
269265
match (primitive, args) with
270266
| Lambda.Pfield (_, Fld_module {name}), [Lambda.Lprim {primitive; args; _}]
271267
->
272-
extract_field_path (name :: segments) primitive args
268+
extract_field_path awaited_units (name :: segments) primitive args
273269
| Lambda.Pfield (_, Fld_module {name}), [Lambda.Lglobal_module id] ->
274-
Some (id, name :: segments)
270+
Some (E.ml_var_dot id, name :: segments)
271+
| Lambda.Pfield (_, Fld_module {name}), [Lambda.Lvar id]
272+
when Set_ident.mem id awaited_units ->
273+
Some (E.dot (E.var id), name :: segments)
275274
| _ -> None
276275
in
277-
let hoisted_external_field_name primitive args =
278-
match extract_field_path [] primitive args with
279-
| Some (id, (_ :: _ :: _ as segments)) ->
280-
Lam_compile_env.find_hoisted_external_export id segments
281-
|> Option.map (fun name -> (id, name))
282-
| Some (_, ([] | [_])) | None -> None
276+
let flat_make_field (lambda_cxt : Lam_compile_context.t) primitive args =
277+
match
278+
extract_field_path lambda_cxt.meta.awaited_units [] primitive args
279+
with
280+
| Some (read, segments) ->
281+
Translmod.flat_make_name segments |> Option.map read
282+
| None -> None
283283
in
284284
let rec compile_external_field (* Like [List.empty]*)
285285
(lamba_cxt : Lam_compile_context.t) (id : Ident.t) name : Js_output.t =
@@ -1633,11 +1633,10 @@ let compile output_prefix =
16331633
in
16341634
match prim_info with
16351635
| {primitive = Pfield (_, Fld_module _); _} -> (
1636-
match hoisted_external_field_name prim_info.primitive prim_info.args with
1637-
| Some (id, hoisted_name) ->
1636+
match flat_make_field lambda_cxt prim_info.primitive prim_info.args with
1637+
| Some read ->
16381638
Js_output.output_of_expression lambda_cxt.continuation
1639-
~no_effects:no_effects_const
1640-
(E.ml_var_dot id hoisted_name)
1639+
~no_effects:no_effects_const read
16411640
| None -> (
16421641
match prim_info with
16431642
| {primitive = Pfield (_, fld_info); args = [Lglobal_module id]; _} -> (

‎compiler/core/lam_compile_env.ml‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,6 @@ let query_external_id_info ?(dynamic_import = false) (module_id : Ident.t)
106106
let cmj_table = cmj_table_of_module_id ~dynamic_import module_id in
107107
Js_cmj_format.query_by_name cmj_table name
108108

109-
let find_hoisted_external_export ?(dynamic_import = false) (module_id : Ident.t)
110-
(path : string list) : string option =
111-
let cmj_table = cmj_table_of_module_id ~dynamic_import module_id in
112-
Js_cmj_format.find_hoisted_export cmj_table path
113-
114109
let get_package_path_from_cmj (id : Lam_module_ident.t) :
115110
string * Js_packages_info.t * Ext_js_file_kind.case =
116111
let cmj_load_info =

‎compiler/core/lam_compile_env.mli‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,6 @@ val query_external_id_info :
7575
will raise if not found
7676
*)
7777

78-
val find_hoisted_external_export :
79-
?dynamic_import:bool -> Ident.t -> string list -> string option
80-
8178
val is_pure_module : Lam_module_ident.t -> bool
8279

8380
val get_package_path_from_cmj :

0 commit comments

Comments
 (0)