diff --git a/changes/toplevel-module-await-constraint.fix.md b/changes/toplevel-module-await-constraint.fix.md new file mode 100644 index 0000000000..c2b8719a88 --- /dev/null +++ b/changes/toplevel-module-await-constraint.fix.md @@ -0,0 +1 @@ +- Fix `module M = await (P: S)` at the top level of a module compiling to a static import of `P`. https://github.com/rescript-lang/rescript/pull/8848 diff --git a/compiler/frontend/bs_builtin_ppx.ml b/compiler/frontend/bs_builtin_ppx.ml index 21af6c8899..38d5696e46 100644 --- a/compiler/frontend/bs_builtin_ppx.ml +++ b/compiler/frontend/bs_builtin_ppx.ml @@ -789,7 +789,22 @@ let rec structure_mapper ~await_context (self : mapper) (stru : Ast_structure.t) }; } :: structure_mapper ~await_context self rest - | _ -> + | Some (_, imported, Some {pmty_desc = Pmty_ident module_type_lid}) -> + (* module M = await (List: ListType) *) + let item = self.structure_item self item in + { + item with + pstr_desc = + Pstr_module + { + mb with + pmb_expr = + Ast_await.create_await_module_expression ~module_type_lid + mb.pmb_expr imported; + }; + } + :: structure_mapper ~await_context self rest + | Some (_, _, Some _) | None -> self.structure_item self item :: structure_mapper ~await_context self rest) | Pstr_value (_, vbs) -> diff --git a/tests/tests/src/Import.mjs b/tests/tests/src/Import.mjs index 2597d653fb..1655f29d9c 100644 --- a/tests/tests/src/Import.mjs +++ b/tests/tests/src/Import.mjs @@ -34,12 +34,15 @@ let N1 = { let N2 = await import("@rescript/runtime/lib/es6/Stdlib_List.mjs"); -let N_each = N2.forEach; +let N3 = await import("@rescript/runtime/lib/es6/Stdlib_List.mjs"); + +let N_each = N3.forEach; let N = { N0: N0, N1: N1, N2: N2, + N3: N3, each: N_each }; @@ -47,6 +50,8 @@ let M0 = await import("@rescript/runtime/lib/es6/Stdlib_List.mjs"); let M1 = await import("@rescript/runtime/lib/es6/Stdlib_List.mjs"); +let MConstrained = await import("@rescript/runtime/lib/es6/Stdlib_List.mjs"); + async function f() { return (await import("@rescript/runtime/lib/es6/Stdlib_List.mjs")).forEach; } @@ -123,7 +128,7 @@ async function f9(value) { } } -let each = M1.forEach; +let each = MConstrained.forEach; let M2; @@ -137,6 +142,7 @@ export { N, M0, M1, + MConstrained, each, M2, each2, diff --git a/tests/tests/src/Import.res b/tests/tests/src/Import.res index 3dcb159d33..5f7c25b84f 100644 --- a/tests/tests/src/Import.res +++ b/tests/tests/src/Import.res @@ -27,6 +27,9 @@ module N = { module N2 = await List let each = N2.forEach + + module N3 = await (List: ListType) + let each = N3.forEach } module M0 = await List @@ -35,6 +38,9 @@ let each = M0.forEach module M1 = await List let each = M1.forEach +module MConstrained = await (List: ListType) +let each = MConstrained.forEach + module M2 = N.N1.O let each2 = M2.forEach