Skip to content

Use extension node for encoding labeled tuples #2387

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions ocaml/parsing/jane_syntax.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1354,6 +1354,13 @@ module Labeled_tuples = struct
labeled_components, ptyp_attributes
| _ -> Desugaring_error.raise typ.ptyp_loc Malformed

(* We wrap labeled tuple expressions in an additional extension node
so that tools that inspect the OCaml syntax tree are less likely
to treat a labeled tuple as a regular tuple.
*)
let labeled_tuple_extension_node_name =
Embedded_name.of_feature feature [] |> Embedded_name.to_string

let expr_of ~loc el =
match check_for_any_label el with
| No_labels el -> Ast_helper.Exp.tuple ~loc el
Expand All @@ -1362,15 +1369,21 @@ module Labeled_tuples = struct
Expression.make_entire_jane_syntax ~loc feature (fun () ->
let names = List.map (fun (label, _) -> string_of_label label) el in
Expression.make_jane_syntax feature names
@@ Ast_helper.Exp.tuple (List.map snd el))
@@ Ast_helper.Exp.apply
(Ast_helper.Exp.extension
(Location.mknoloc labeled_tuple_extension_node_name, PStr []))
[Nolabel, Ast_helper.Exp.tuple (List.map snd el)])

(* Returns remaining unconsumed attributes *)
let of_expr expr =
let labels, pexp_attributes =
expand_labeled_tuple_extension expr.pexp_loc expr.pexp_attributes
in
match expr.pexp_desc with
| Pexp_tuple components ->
| Pexp_apply
( { pexp_desc = Pexp_extension (name, PStr []) },
[(Nolabel, { pexp_desc = Pexp_tuple components; _ })] )
when String.equal name.txt labeled_tuple_extension_node_name ->
if List.length labels <> List.length components
then Desugaring_error.raise expr.pexp_loc Malformed;
let labeled_components =
Expand Down