Skip to content

Commit 573dd37

Browse files
committed
fix functors
1 parent 04a5846 commit 573dd37

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

compiler/syntax/src/jsx_common.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ type jsx_config = {
1313
[module_expr]), which would otherwise see [nested_modules = []] and wipe
1414
hoisted bindings added for earlier structure items. *)
1515
mutable structure_depth: int;
16+
(* Inside [Pmod_functor] bodies, hoisting [File$M = M.make] at the file top
17+
would reference [M] as a functor (illegal). Skip hoists when > 0. *)
18+
mutable functor_depth: int;
1619
}
1720

1821
(* Helper method to look up the [@react.component] attribute *)

compiler/syntax/src/jsx_ppx.ml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,19 @@ let get_mapper ~config =
8585
| 4 -> module_binding4 mapper mb
8686
| _ -> default_mapper.module_binding mapper mb
8787
in
88+
let module_expr mapper me =
89+
match (config.version, me.pmod_desc) with
90+
| 4, Pmod_functor _ -> (
91+
config.functor_depth <- config.functor_depth + 1;
92+
try
93+
let m = default_mapper.module_expr mapper me in
94+
config.functor_depth <- config.functor_depth - 1;
95+
m
96+
with e ->
97+
config.functor_depth <- config.functor_depth - 1;
98+
raise e)
99+
| _ -> default_mapper.module_expr mapper me
100+
in
88101
let save_config () =
89102
{
90103
config with
@@ -142,7 +155,7 @@ let get_mapper ~config =
142155
result
143156
in
144157

145-
{default_mapper with expr; module_binding; signature; structure}
158+
{default_mapper with expr; module_binding; module_expr; signature; structure}
146159

147160
let rewrite_implementation ~jsx_version ~jsx_module (code : Parsetree.structure)
148161
: Parsetree.structure =
@@ -154,6 +167,7 @@ let rewrite_implementation ~jsx_version ~jsx_module (code : Parsetree.structure)
154167
has_component = false;
155168
hoisted_structure_items = [];
156169
structure_depth = 0;
170+
functor_depth = 0;
157171
}
158172
in
159173
let mapper = get_mapper ~config in
@@ -169,6 +183,7 @@ let rewrite_signature ~jsx_version ~jsx_module (code : Parsetree.signature) :
169183
has_component = false;
170184
hoisted_structure_items = [];
171185
structure_depth = 0;
186+
functor_depth = 0;
172187
}
173188
in
174189
let mapper = get_mapper ~config in

compiler/syntax/src/jsx_v4.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ let unnamespace_module_name file_name =
132132

133133
let maybe_hoist_nested_make_component ~(config : Jsx_common.jsx_config)
134134
~empty_loc ~full_module_name fn_name =
135-
match (fn_name, config.nested_modules) with
136-
| "make", _ :: _ ->
135+
match (fn_name, config.nested_modules, config.functor_depth) with
136+
| "make", _ :: _, 0 ->
137137
config.hoisted_structure_items <-
138138
make_hoisted_component_binding ~empty_loc ~full_module_name
139139
config.nested_modules

0 commit comments

Comments
 (0)