Skip to content

Commit 52ca529

Browse files
cknittclaude
andcommitted
Keep await locations across v0 and attributes in dynamic imports
The v0 bridge folds an await node and its operand into one node, marked with `res.await`. The marker was written without a location, so after a round trip through an external PPX the await node took its operand's location and lost the span of the `await` keyword. Store the await node's location on the marker, like `res.braces` does, and restore it, falling back to the node's location for markers without one. This also applies to `Pexp_await`, which had the same problem. The dynamic import rewrite replaced an await node with its operand and dropped the await node's attributes, e.g. a `@warning` in `(@warning("-3") (await List): ListT)` as a PPX may produce it. Keep them on the imported module, as when await was an attribute. Signed-off-by: Christoph Knittel <christoph@knittel.cc> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 34fef8f commit 52ca529

4 files changed

Lines changed: 174 additions & 12 deletions

File tree

‎compiler/ml/ast_await.ml‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ let create_await_module_expression ~module_type_lid (e : Parsetree.module_expr)
4444
let open Ast_helper in
4545
let rec remove_await (m : Parsetree.module_expr) =
4646
match m.pmod_desc with
47-
| Pmod_await inner -> inner
47+
(* Its attributes, e.g. [@warning], stay on the imported module *)
48+
| Pmod_await inner ->
49+
{inner with pmod_attributes = m.pmod_attributes @ inner.pmod_attributes}
4850
| Pmod_constraint (inner, mty) ->
4951
{m with pmod_desc = Pmod_constraint (remove_await inner, mty)}
5052
| _ -> m

‎compiler/ml/ast_mapper_from0.ml‎

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ let extract_internal_loc_attr attr_name attrs =
3838
in
3939
loop [] attrs
4040

41+
(* The location of an await node, which [Ast_mapper_to0] stores on the
42+
[res.await] marker. A marker created without one, e.g. by a PPX, falls back
43+
to the location of the node it is on. *)
44+
let await_marker_loc ~node_loc (marker_loc : Location.t) =
45+
if marker_loc = Location.none then node_loc else marker_loc
46+
4147
let extract_ternary_attr (attrs : Pt.attributes) =
4248
let rec loop rev_acc = function
4349
| [] -> (false, List.rev rev_acc)
@@ -471,16 +477,18 @@ module M = struct
471477
({pmod_loc = loc; pmod_desc = desc; pmod_attributes = attrs} as m) =
472478
let open Mod in
473479
let rec split_await acc = function
474-
| ({Location.txt = "res.await"}, _) :: rest -> Some (List.rev acc, rest)
480+
| ({Location.txt = "res.await"; loc = await_loc}, _) :: rest ->
481+
Some (List.rev acc, await_loc, rest)
475482
| a :: rest -> split_await (a :: acc) rest
476483
| [] -> None
477484
in
478485
match split_await [] attrs with
479-
| Some (await_attrs0, inner_attrs0) ->
486+
| Some (await_attrs0, await_loc, inner_attrs0) ->
480487
(* [Ast_mapper_to0] puts the await node's attributes before the
481488
[res.await] marker and the inner module's attributes after it *)
482489
let inner = map sub {m with pmod_attributes = inner_attrs0} in
483-
await_ ~loc:(sub.location sub loc)
490+
await_
491+
~loc:(sub.location sub (await_marker_loc ~node_loc:loc await_loc))
484492
~attrs:(sub.attributes sub await_attrs0)
485493
inner
486494
| None -> (
@@ -683,16 +691,21 @@ module E = struct
683691
expression's attributes into the one v0 slot, with [res.await] as
684692
the boundary: await-node attributes before it, inner attributes
685693
after it. *)
686-
let await_attrs0, inner_attrs0 =
694+
let await_attrs0, await_loc, inner_attrs0 =
687695
let rec split acc = function
688-
| ({Location.txt = "res.await"}, _) :: rest -> (List.rev acc, rest)
696+
| ({Location.txt = "res.await"; loc = await_loc}, _) :: rest ->
697+
(List.rev acc, await_loc, rest)
689698
| a :: rest -> split (a :: acc) rest
690-
| [] -> (List.rev acc, [])
699+
| [] -> (List.rev acc, Location.none, [])
691700
in
692701
split [] e.pexp_attributes
693702
in
694703
let inner = sub.expr sub {e with pexp_attributes = inner_attrs0} in
695-
await ~loc ~attrs:(sub.attributes sub await_attrs0) inner
704+
await
705+
~loc:
706+
(sub.location sub (await_marker_loc ~node_loc:e.pexp_loc await_loc))
707+
~attrs:(sub.attributes sub await_attrs0)
708+
inner
696709
| Pexp_ident x -> ident ~loc ~attrs (map_loc sub x)
697710
| Pexp_constant (Pconst_string (text, delimiter))
698711
when has_template_attr attrs && delimiter <> Some "json" ->

‎compiler/ml/ast_mapper_to0.ml‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -352,13 +352,14 @@ module M = struct
352352
| Pmod_await m ->
353353
(* Single v0 attribute slot for two nodes, as for [Pexp_await]: the
354354
await node's own attributes go in front of the [res.await] marker,
355-
the inner module's attributes after it. *)
355+
the inner module's attributes after it. The marker carries the await
356+
node's location. *)
356357
let m = sub.module_expr sub m in
357358
{
358359
m with
359360
pmod_attributes =
360361
attrs
361-
@ ((Location.mknoloc "res.await", Pt.PStr []) :: m.pmod_attributes);
362+
@ ((Location.mkloc "res.await" loc, Pt.PStr []) :: m.pmod_attributes);
362363
}
363364

364365
let map_structure_item sub {pstr_loc = loc; pstr_desc = desc} =
@@ -777,13 +778,13 @@ module E = struct
777778
(* Single v0 attribute slot for two nodes: the await node's own
778779
attributes go in front of the [res.await] marker, the inner
779780
expression's attributes after it, so [Ast_mapper_from0] can split
780-
them again. *)
781+
them again. The marker carries the await node's location. *)
781782
let e = sub.expr sub e in
782783
{
783784
e with
784785
pexp_attributes =
785786
attrs
786-
@ ((Location.mknoloc "res.await", Pt.PStr []) :: e.pexp_attributes);
787+
@ ((Location.mkloc "res.await" loc, Pt.PStr []) :: e.pexp_attributes);
787788
}
788789
| Pexp_jsx_element
789790
(Jsx_fragment

‎tests/ounit_tests/ounit_ast_mapper0_tests.ml‎

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
311449
let 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

Comments
 (0)