Skip to content

Commit 93962f9

Browse files
cknittclaude
andcommitted
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>
1 parent 5e3cc82 commit 93962f9

83 files changed

Lines changed: 802 additions & 758 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: 4 additions & 3 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} =
144+
let {Translmod.lambda; exports} =
145145
Translmod.transl_implementation modulename typedtree_coercion
146146
in
147+
let flat_make_paths = Translmod.flat_make_paths finalenv signature in
147148
let js_program =
148149
print_if_pipe ppf (Clflags.current ()).dump_rawlambda
149150
Printlambda.lambda lambda
150-
|> Lam_compile_main.compile outputprefix exports hoisted_functions
151+
|> Lam_compile_main.compile outputprefix exports flat_make_paths
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: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -255,16 +255,10 @@ 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. *)
258+
Every nested [make] is also exported under a flat name (see
259+
[Translmod.flat_make_paths]), so it is read as Other.A$B$make rather
260+
than through the module objects. Other reads query only the first field,
261+
"A", and emit the remaining fields as JS property access. *)
268262
let rec extract_field_path segments primitive args =
269263
match (primitive, args) with
270264
| Lambda.Pfield (_, Fld_module {name}), [Lambda.Lprim {primitive; args; _}]
@@ -274,12 +268,12 @@ let compile output_prefix =
274268
Some (id, name :: segments)
275269
| _ -> None
276270
in
277-
let hoisted_external_field_name primitive args =
271+
let flat_make_field_name primitive args =
278272
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
273+
| Some (id, (_ :: _ :: _ as segments)) when Ext_list.last segments = "make"
274+
->
275+
Some (id, Translmod.flat_export_name segments)
276+
| Some _ | None -> None
283277
in
284278
let rec compile_external_field (* Like [List.empty]*)
285279
(lamba_cxt : Lam_compile_context.t) (id : Ident.t) name : Js_output.t =
@@ -1633,11 +1627,11 @@ let compile output_prefix =
16331627
in
16341628
match prim_info with
16351629
| {primitive = Pfield (_, Fld_module _); _} -> (
1636-
match hoisted_external_field_name prim_info.primitive prim_info.args with
1637-
| Some (id, hoisted_name) ->
1630+
match flat_make_field_name prim_info.primitive prim_info.args with
1631+
| Some (id, flat_name) ->
16381632
Js_output.output_of_expression lambda_cxt.continuation
16391633
~no_effects:no_effects_const
1640-
(E.ml_var_dot id hoisted_name)
1634+
(E.ml_var_dot id flat_name)
16411635
| None -> (
16421636
match prim_info with
16431637
| {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)