@@ -308,6 +308,144 @@ let test_braces_roundtrip_through_ast0 _ =
308308 | _ ->
309309 assert_failure " Expected two structural brace nodes after ast0 roundtrip"
310310
311+ let attr_names attrs = List. map (fun ({Location. txt} , _ ) -> txt) attrs
312+
313+ let to_mod0 m =
314+ Ast_mapper_to0. default_mapper.module_expr Ast_mapper_to0. default_mapper m
315+
316+ let map_mod0 m =
317+ Ast_mapper_from0. default_mapper.module_expr Ast_mapper_from0. default_mapper m
318+
319+ let test_module_await_roundtrips_through_ast0 _ =
320+ let await_loc = source_loc 11 22 in
321+ let inner_loc = source_loc 17 22 in
322+ let inner =
323+ Ast_helper.Mod. ident ~loc: inner_loc
324+ ~attrs: [attr " inner" (Parsetree. PStr [] )]
325+ (located_string ~loc: inner_loc (Longident. Lident " List" ))
326+ in
327+ let wire =
328+ to_mod0
329+ (Ast_helper.Mod. await_ ~loc: await_loc
330+ ~attrs: [attr " outer" (Parsetree. PStr [] )]
331+ inner)
332+ in
333+ OUnit. assert_equal ~printer: (String. concat " , " )
334+ [" outer" ; " res.await" ; " inner" ]
335+ (attr_names wire.pmod_attributes);
336+ match map_mod0 wire with
337+ | {
338+ pmod_desc =
339+ Pmod_await
340+ {pmod_desc = Pmod_ident _; pmod_loc = mapped_inner_loc; pmod_attributes};
341+ pmod_loc = mapped_await_loc;
342+ pmod_attributes = await_attributes;
343+ } ->
344+ OUnit. assert_equal ~msg: " await location" await_loc mapped_await_loc;
345+ OUnit. assert_equal ~msg: " inner location" inner_loc mapped_inner_loc;
346+ OUnit. assert_equal [" outer" ] (attr_names await_attributes);
347+ OUnit. assert_equal [" inner" ] (attr_names pmod_attributes)
348+ | _ -> assert_failure " Expected Pmod_await after the ast0 roundtrip"
349+
350+ let test_expression_await_location_through_ast0 _ =
351+ let await_loc = source_loc 0 9 in
352+ let inner_loc = source_loc 6 9 in
353+ let inner =
354+ Ast_helper.Exp. ident ~loc: inner_loc
355+ ~attrs: [attr " inner" (Parsetree. PStr [] )]
356+ (located_string ~loc: inner_loc (Longident. Lident " p" ))
357+ in
358+ let wire =
359+ to_expr0
360+ (Ast_helper.Exp. await ~loc: await_loc
361+ ~attrs: [attr " outer" (Parsetree. PStr [] )]
362+ inner)
363+ in
364+ match map_expr0 wire with
365+ | {
366+ pexp_desc =
367+ Pexp_await
368+ {pexp_desc = Pexp_ident _; pexp_loc = mapped_inner_loc; pexp_attributes};
369+ pexp_loc = mapped_await_loc;
370+ pexp_attributes = await_attributes;
371+ } ->
372+ OUnit. assert_equal ~msg: " await location" await_loc mapped_await_loc;
373+ OUnit. assert_equal ~msg: " inner location" inner_loc mapped_inner_loc;
374+ OUnit. assert_equal [" outer" ] (attr_names await_attributes);
375+ OUnit. assert_equal [" inner" ] (attr_names pexp_attributes)
376+ | _ -> assert_failure " Expected Pexp_await after the ast0 roundtrip"
377+
378+ let test_v0_await_marker_without_location _ =
379+ let node_loc = source_loc 4 8 in
380+ let wire =
381+ Ast_helper0.Mod. ident ~loc: node_loc
382+ ~attrs: [attr " res.await" (Parsetree0. PStr [] )]
383+ (located_string ~loc: node_loc (Longident. Lident " List" ))
384+ in
385+ match map_mod0 wire with
386+ | {pmod_desc = Pmod_await {pmod_desc = Pmod_ident _ } ; pmod_loc} ->
387+ OUnit. assert_equal ~msg: " falls back to the node location" node_loc pmod_loc
388+ | _ -> assert_failure " Expected Pmod_await from a v0 res.await marker"
389+
390+ (* [(@warning("-3") (await List): ListT)] can come from a PPX through the v0
391+ bridge; the imported module must keep the await node's attributes *)
392+ let test_dynamic_import_keeps_await_attributes _ =
393+ let module_expr =
394+ Ast_helper.Mod. constraint_ ~loc
395+ (Ast_helper.Mod. await_ ~loc
396+ ~attrs: [attr " warning" (Parsetree. PStr [] )]
397+ (Ast_helper.Mod. ident ~loc (located_string (Longident. Lident " List" ))))
398+ (Ast_helper.Mty. ident ~loc (located_string (Longident. Lident " ListT" )))
399+ in
400+ let imported =
401+ Ast_await. create_await_module_expression
402+ ~module_type_lid: (located_string (Longident. Lident " ListT" ))
403+ module_expr
404+ in
405+ match imported.pmod_desc with
406+ | Pmod_unpack
407+ {
408+ pexp_desc =
409+ Pexp_apply
410+ {
411+ args =
412+ [
413+ ( _,
414+ {
415+ pexp_desc =
416+ Pexp_apply
417+ {
418+ args =
419+ [
420+ ( _,
421+ {
422+ pexp_desc =
423+ Pexp_constraint
424+ ( {
425+ pexp_desc =
426+ Pexp_pack
427+ {
428+ pmod_desc =
429+ Pmod_constraint
430+ ( {
431+ pmod_desc =
432+ Pmod_ident _;
433+ pmod_attributes;
434+ },
435+ _ );
436+ };
437+ },
438+ _ );
439+ } );
440+ ];
441+ };
442+ } );
443+ ];
444+ };
445+ } ->
446+ OUnit. assert_equal [" warning" ] (attr_names pmod_attributes)
447+ | _ -> assert_failure " Expected unpack(await import(module(List: ListT)))"
448+
311449let test_inline_record_definition_roundtrips_through_ast0 _ =
312450 let name = located_string " person.details" in
313451 let field =
@@ -1719,6 +1857,14 @@ let suites =
17191857 " v0_if_without_alternate_stays_if"
17201858 > :: test_v0_if_without_alternate_stays_if;
17211859 " braces_roundtrip_through_ast0" > :: test_braces_roundtrip_through_ast0;
1860+ " module_await_roundtrips_through_ast0"
1861+ > :: test_module_await_roundtrips_through_ast0;
1862+ " expression_await_location_through_ast0"
1863+ > :: test_expression_await_location_through_ast0;
1864+ " v0_await_marker_without_location"
1865+ > :: test_v0_await_marker_without_location;
1866+ " dynamic_import_keeps_await_attributes"
1867+ > :: test_dynamic_import_keeps_await_attributes;
17221868 " inline_record_definition_roundtrips_through_ast0"
17231869 > :: test_inline_record_definition_roundtrips_through_ast0;
17241870 " this_on_braced_function_reaches_builtin_ppx"
0 commit comments