Skip to content

Commit d4f246c

Browse files
authored
Use extension node for encoding labeled tuples (#2387)
* Use extension node for encoding labeled tuples rather than merely an attribute * Add comment
1 parent 14984dd commit d4f246c

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

ocaml/parsing/jane_syntax.ml

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,6 +1354,13 @@ module Labeled_tuples = struct
13541354
labeled_components, ptyp_attributes
13551355
| _ -> Desugaring_error.raise typ.ptyp_loc Malformed
13561356

1357+
(* We wrap labeled tuple expressions in an additional extension node
1358+
so that tools that inspect the OCaml syntax tree are less likely
1359+
to treat a labeled tuple as a regular tuple.
1360+
*)
1361+
let labeled_tuple_extension_node_name =
1362+
Embedded_name.of_feature feature [] |> Embedded_name.to_string
1363+
13571364
let expr_of ~loc el =
13581365
match check_for_any_label el with
13591366
| No_labels el -> Ast_helper.Exp.tuple ~loc el
@@ -1362,15 +1369,21 @@ module Labeled_tuples = struct
13621369
Expression.make_entire_jane_syntax ~loc feature (fun () ->
13631370
let names = List.map (fun (label, _) -> string_of_label label) el in
13641371
Expression.make_jane_syntax feature names
1365-
@@ Ast_helper.Exp.tuple (List.map snd el))
1372+
@@ Ast_helper.Exp.apply
1373+
(Ast_helper.Exp.extension
1374+
(Location.mknoloc labeled_tuple_extension_node_name, PStr []))
1375+
[Nolabel, Ast_helper.Exp.tuple (List.map snd el)])
13661376

13671377
(* Returns remaining unconsumed attributes *)
13681378
let of_expr expr =
13691379
let labels, pexp_attributes =
13701380
expand_labeled_tuple_extension expr.pexp_loc expr.pexp_attributes
13711381
in
13721382
match expr.pexp_desc with
1373-
| Pexp_tuple components ->
1383+
| Pexp_apply
1384+
( { pexp_desc = Pexp_extension (name, PStr []) },
1385+
[(Nolabel, { pexp_desc = Pexp_tuple components; _ })] )
1386+
when String.equal name.txt labeled_tuple_extension_node_name ->
13741387
if List.length labels <> List.length components
13751388
then Desugaring_error.raise expr.pexp_loc Malformed;
13761389
let labeled_components =

0 commit comments

Comments
 (0)