Skip to content

Commit b9df8ac

Browse files
authored
Fix build error with Dynamic import of module in nested expressions (#6431)
* add test * transform module import * add more tests * transform module imports * changelog * update changelog
1 parent 13084df commit b9df8ac

File tree

5 files changed

+104
-30
lines changed

5 files changed

+104
-30
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
1313
# 11.0.0-rc.5 (Unreleased)
1414

15+
#### :bug: Bug Fix
16+
17+
- Fix issue with Dynamic import of module in nested expressions https://github.com/rescript-lang/rescript-compiler/pull/6431
18+
1519
# 11.0.0-rc.4
1620

1721
#### :rocket: New Feature

jscomp/frontend/bs_builtin_ppx.ml

+34-27
Original file line numberDiff line numberDiff line change
@@ -574,35 +574,42 @@ let rec structure_mapper ~await_context (self : mapper) (stru : Ast_structure.t)
574574
| Pstr_value (_, vbs) ->
575575
let item = self.structure_item self item in
576576
(* [ module __Belt_List__ = module type of Belt.List ] *)
577-
let module_type_decls =
578-
vbs
579-
|> List.filter_map (fun ({pvb_expr} : Parsetree.value_binding) ->
580-
match pvb_expr.pexp_desc with
581-
| Pexp_letmodule
582-
( _,
583-
({pmod_desc = Pmod_ident {txt; loc}; pmod_attributes} as
584-
me),
585-
_ )
586-
when Res_parsetree_viewer.hasAwaitAttribute pmod_attributes
587-
-> (
588-
let safe_module_type_name = local_module_type_name txt in
589-
let has_local_module_name =
590-
Hashtbl.find_opt !await_context safe_module_type_name
591-
in
577+
let rec spelunk_vbs acc vbs =
578+
match vbs with
579+
| [] -> acc
580+
| ({pvb_expr} : Parsetree.value_binding) :: tl ->
581+
let rec aux (expr : Parsetree.expression) =
582+
match expr.pexp_desc with
583+
| Pexp_letmodule
584+
( _,
585+
({pmod_desc = Pmod_ident {txt; loc}; pmod_attributes} as me),
586+
expr )
587+
when Res_parsetree_viewer.hasAwaitAttribute pmod_attributes -> (
588+
let safe_module_type_name = local_module_type_name txt in
589+
let has_local_module_name =
590+
Hashtbl.find_opt !await_context safe_module_type_name
591+
in
592592

593-
match has_local_module_name with
594-
| Some _ -> None
595-
| None ->
596-
Hashtbl.add !await_context safe_module_type_name
597-
safe_module_type_name;
598-
Some
599-
Ast_helper.(
600-
Str.modtype ~loc
601-
(Mtd.mk ~loc
602-
{txt = safe_module_type_name; loc}
603-
~typ:(Mty.typeof_ ~loc me))))
604-
| _ -> None)
593+
match has_local_module_name with
594+
| Some _ -> aux expr
595+
| None ->
596+
Hashtbl.add !await_context safe_module_type_name
597+
safe_module_type_name;
598+
Ast_helper.(
599+
Str.modtype ~loc
600+
(Mtd.mk ~loc
601+
{txt = safe_module_type_name; loc}
602+
~typ:(Mty.typeof_ ~loc me)))
603+
:: aux expr)
604+
| Pexp_let (_, vbs, expr) -> aux expr @ spelunk_vbs acc vbs
605+
| Pexp_ifthenelse (_, then_expr, Some else_expr) ->
606+
aux then_expr @ aux else_expr
607+
| Pexp_fun (_, _, _, expr) | Pexp_newtype (_, expr) -> aux expr
608+
| _ -> acc
609+
in
610+
aux pvb_expr @ spelunk_vbs acc tl
605611
in
612+
let module_type_decls = spelunk_vbs [] vbs in
606613

607614
module_type_decls @ (item :: structure_mapper ~await_context self rest)
608615
| _ ->

jscomp/runtime/release.ninja

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ o runtime/caml_bytes.cmj : cc_cmi runtime/caml_bytes.res | runtime/caml_bytes.cm
2121
o runtime/caml_bytes.cmi : cc runtime/caml_bytes.resi | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
2222
o runtime/caml_float.cmj : cc_cmi runtime/caml_float.res | runtime/caml_float.cmi runtime/caml_float_extern.cmj
2323
o runtime/caml_float.cmi : cc runtime/caml_float.resi | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
24-
o runtime/caml_format.cmj : cc_cmi runtime/caml_format.ml | runtime/caml_float.cmj runtime/caml_float_extern.cmj runtime/caml_format.cmi runtime/caml_int64.cmj runtime/caml_int64_extern.cmj runtime/caml_nativeint_extern.cmj runtime/caml_string_extern.cmj
24+
o runtime/caml_format.cmj : cc_cmi runtime/caml_format.ml | runtime/caml.cmj runtime/caml_float.cmj runtime/caml_float_extern.cmj runtime/caml_format.cmi runtime/caml_int64.cmj runtime/caml_int64_extern.cmj runtime/caml_nativeint_extern.cmj runtime/caml_string_extern.cmj
2525
o runtime/caml_format.cmi : cc runtime/caml_format.mli | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
2626
o runtime/caml_hash.cmj : cc_cmi runtime/caml_hash.res | runtime/caml_hash.cmi runtime/caml_hash_primitive.cmj runtime/caml_nativeint_extern.cmj runtime/js.cmj
2727
o runtime/caml_hash.cmi : cc runtime/caml_hash.resi | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
@@ -37,7 +37,7 @@ o runtime/caml_md5.cmj : cc_cmi runtime/caml_md5.res | runtime/caml_array_extern
3737
o runtime/caml_md5.cmi : cc runtime/caml_md5.resi | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
3838
o runtime/caml_module.cmj : cc_cmi runtime/caml_module.res | runtime/caml_array_extern.cmj runtime/caml_module.cmi runtime/caml_obj.cmj
3939
o runtime/caml_module.cmi : cc runtime/caml_module.resi | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
40-
o runtime/caml_obj.cmj : cc_cmi runtime/caml_obj.res | runtime/caml_array_extern.cmj runtime/caml_obj.cmi runtime/caml_option.cmj runtime/js.cmj
40+
o runtime/caml_obj.cmj : cc_cmi runtime/caml_obj.res | runtime/caml.cmj runtime/caml_array_extern.cmj runtime/caml_obj.cmi runtime/caml_option.cmj runtime/js.cmj
4141
o runtime/caml_obj.cmi : cc runtime/caml_obj.resi | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
4242
o runtime/caml_option.cmj : cc_cmi runtime/caml_option.res | runtime/caml_option.cmi runtime/caml_undefined_extern.cmj runtime/js.cmj
4343
o runtime/caml_option.cmi : cc runtime/caml_option.resi | runtime/bs_stdlib_mini.cmi runtime/caml_undefined_extern.cmj runtime/js.cmi runtime/js.cmj
@@ -54,7 +54,7 @@ o runtime/caml_exceptions.cmi runtime/caml_exceptions.cmj : cc runtime/caml_exce
5454
o runtime/caml_external_polyfill.cmi runtime/caml_external_polyfill.cmj : cc runtime/caml_external_polyfill.res | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
5555
o runtime/caml_float_extern.cmi runtime/caml_float_extern.cmj : cc runtime/caml_float_extern.res | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
5656
o runtime/caml_int64_extern.cmi runtime/caml_int64_extern.cmj : cc runtime/caml_int64_extern.res | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
57-
o runtime/caml_js_exceptions.cmi runtime/caml_js_exceptions.cmj : cc runtime/caml_js_exceptions.res | runtime/bs_stdlib_mini.cmi runtime/caml_exceptions.cmj runtime/js.cmi runtime/js.cmj
57+
o runtime/caml_js_exceptions.cmi runtime/caml_js_exceptions.cmj : cc runtime/caml_js_exceptions.res | runtime/bs_stdlib_mini.cmi runtime/caml_exceptions.cmj runtime/caml_option.cmj runtime/js.cmi runtime/js.cmj
5858
o runtime/caml_nativeint_extern.cmi runtime/caml_nativeint_extern.cmj : cc runtime/caml_nativeint_extern.res | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
5959
o runtime/caml_string_extern.cmi runtime/caml_string_extern.cmj : cc runtime/caml_string_extern.res | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
6060
o runtime/caml_undefined_extern.cmi runtime/caml_undefined_extern.cmj : cc runtime/caml_undefined_extern.res | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj

jscomp/test/Import.js

+32
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jscomp/test/Import.res

+31
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,34 @@ let f3 = async () => {
5959
module M4 = await Belt.List
6060
(M3.forEach, M4.forEach)
6161
}
62+
63+
let f4 = async () => {
64+
module A = await Belt.Array
65+
A.forEach
66+
}
67+
68+
let f5 = async () => {
69+
module A = await Belt.Array
70+
module O = await Belt.Option
71+
(A.forEach, O.forEach)
72+
}
73+
74+
let f6 = async () => {
75+
let a = 0
76+
and b = {
77+
module MS = await Belt.Map.String
78+
MS.forEach
79+
}
80+
module A = await Belt.Array
81+
(a, b, A.forEach)
82+
}
83+
84+
let f7 = async () => {
85+
if true {
86+
module MI = await Belt.Map.Int
87+
1
88+
} else {
89+
module MI = await Belt.Map.Dict
90+
0
91+
}
92+
}

0 commit comments

Comments
 (0)