Skip to content

Commit cb3ac37

Browse files
authored
API for constructing a labeled tuple AST is more permissive (#2146)
* Labeled tuple creation is more flexible * promote-menhir * promote test output I forgot to promote earlier
1 parent 78768ca commit cb3ac37

File tree

15 files changed

+9167
-9223
lines changed

15 files changed

+9167
-9223
lines changed

ocaml/boot/menhir/parser.ml

Lines changed: 9053 additions & 9073 deletions
Large diffs are not rendered by default.

ocaml/parsing/ast_invariants.ml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ let iterator =
6969
let jtyp _self loc (jtyp : Jane_syntax.Core_type.t) =
7070
match jtyp with
7171
| Jtyp_layout (Ltyp_var _ | Ltyp_poly _ | Ltyp_alias _) -> ()
72-
| Jtyp_tuple (Lttyp_tuple ([] | [_])) -> invalid_tuple loc
73-
| Jtyp_tuple (Lttyp_tuple l) ->
72+
| Jtyp_tuple ([] | [_]) -> invalid_tuple loc
73+
| Jtyp_tuple l ->
7474
if labeled_tuple_without_label l then unlabeled_labeled_tuple_typ loc
7575
in
7676
let typ self ty =
@@ -91,12 +91,12 @@ let iterator =
9191
| Jpat_layout (Lpat_constant _) -> ()
9292
| Jpat_tuple lt -> begin
9393
match lt with
94-
| Ltpat_tuple ([], Open) -> empty_open_labeled_tuple_pat loc
95-
| Ltpat_tuple (([] | [_]), Closed) ->
94+
| ([], Open) -> empty_open_labeled_tuple_pat loc
95+
| (([] | [_]), Closed) ->
9696
short_closed_labeled_tuple_pat loc
97-
| Ltpat_tuple (l, Closed) ->
97+
| (l, Closed) ->
9898
if labeled_tuple_without_label l then unlabeled_labeled_tuple_pat loc
99-
| Ltpat_tuple (_ :: _, Open) -> ()
99+
| (_ :: _, Open) -> ()
100100
end
101101
in
102102
let pat self pat =
@@ -143,8 +143,8 @@ let iterator =
143143
empty_comprehension loc
144144
| Jexp_tuple lt -> begin
145145
match lt with
146-
| Ltexp_tuple ([] | [_]) -> invalid_tuple loc
147-
| Ltexp_tuple l ->
146+
| [] | [_] -> invalid_tuple loc
147+
| l ->
148148
if labeled_tuple_without_label l then unlabeled_labeled_tuple_exp loc
149149
end
150150
| Jexp_comprehension _

ocaml/parsing/ast_iterator.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ module T = struct
140140
iter_loc_txt sub sub.jkind_annotation jkind
141141

142142
let iter_jst_labeled_tuple sub : LT.core_type -> _ = function
143-
| Lttyp_tuple tl -> List.iter (iter_snd (sub.typ sub)) tl
143+
| tl -> List.iter (iter_snd (sub.typ sub)) tl
144144

145145
let iter_jst sub : Jane_syntax.Core_type.t -> _ = function
146146
| Jtyp_layout typ -> iter_jst_layout sub typ
@@ -526,7 +526,7 @@ module E = struct
526526
iter_function_body sub body
527527

528528
let iter_labeled_tuple sub : LT.expression -> _ = function
529-
| Ltexp_tuple el -> List.iter (iter_snd (sub.expr sub)) el
529+
| el -> List.iter (iter_snd (sub.expr sub)) el
530530

531531
let iter_jst sub : Jane_syntax.Expression.t -> _ = function
532532
| Jexp_comprehension comp_exp -> iter_comp_exp sub comp_exp
@@ -636,7 +636,7 @@ module P = struct
636636
List.iter (sub.pat sub) elts
637637

638638
let iter_labeled_tuple sub : LT.pattern -> _ = function
639-
| Ltpat_tuple (pl, _) ->
639+
| (pl, _) ->
640640
List.iter (iter_snd (sub.pat sub)) pl
641641

642642
let iter_jst sub : Jane_syntax.Pattern.t -> _ = function

ocaml/parsing/ast_mapper.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ module T = struct
175175

176176
let map_jst_labeled_tuple sub : LT.core_type -> LT.core_type = function
177177
(* CR labeled tuples: Eventually mappers may want to see the labels. *)
178-
| Lttyp_tuple tl -> Lttyp_tuple (List.map (map_snd (sub.typ sub)) tl)
178+
| tl -> List.map (map_snd (sub.typ sub)) tl
179179

180180
let map_jst sub : Jane_syntax.Core_type.t -> Jane_syntax.Core_type.t =
181181
function
@@ -627,7 +627,7 @@ module E = struct
627627

628628
let map_ltexp sub : LT.expression -> LT.expression = function
629629
(* CR labeled tuples: Eventually mappers may want to see the labels. *)
630-
| Ltexp_tuple el -> Ltexp_tuple (List.map (map_snd (sub.expr sub)) el)
630+
| el -> List.map (map_snd (sub.expr sub)) el
631631

632632
let map_jst sub : Jane_syntax.Expression.t -> Jane_syntax.Expression.t =
633633
function
@@ -755,8 +755,8 @@ module P = struct
755755

756756
let map_ltpat sub : LT.pattern -> LT.pattern = function
757757
(* CR labeled tuples: Eventually mappers may want to see the labels. *)
758-
| Ltpat_tuple (pl, closed) ->
759-
Ltpat_tuple (List.map (map_snd (sub.pat sub)) pl, closed)
758+
| (pl, closed) ->
759+
(List.map (map_snd (sub.pat sub)) pl, closed)
760760

761761
let map_jst sub : Jane_syntax.Pattern.t -> Jane_syntax.Pattern.t = function
762762
| Jpat_immutable_array x -> Jpat_immutable_array (map_iapat sub x)

ocaml/parsing/depend.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ and add_type_jst_layouts bv : Jane_syntax.Layouts.core_type -> _ = function
146146
add_jkind bv jkind
147147

148148
and add_type_jst_labeled_tuple bv : Jane_syntax.Labeled_tuples.core_type -> _ =
149-
function Lttyp_tuple tl -> List.iter (fun (_, ty) -> add_type bv ty) tl
149+
fun tl -> List.iter (fun (_, ty) -> add_type bv ty) tl
150150

151151
and add_package_type bv (lid, l) =
152152
add bv lid;
@@ -242,7 +242,7 @@ and add_pattern_jane_syntax bv : Jane_syntax.Pattern.t -> _ = function
242242
| Jpat_immutable_array (Iapat_immutable_array pl) ->
243243
List.iter (add_pattern bv) pl
244244
| Jpat_layout (Lpat_constant _) -> add_constant
245-
| Jpat_tuple (Ltpat_tuple (labeled_pl, _)) ->
245+
| Jpat_tuple (labeled_pl, _) ->
246246
List.iter (fun (_, p) -> add_pattern bv p) labeled_pl
247247

248248
let add_pattern bv pat =
@@ -410,7 +410,7 @@ and add_function_constraint bv
410410
add_type bv ty2
411411

412412
and add_labeled_tuple_expr bv : Jane_syntax.Labeled_tuples.expression -> _ =
413-
function Ltexp_tuple el -> List.iter (add_expr bv) (List.map snd el)
413+
function el -> List.iter (add_expr bv) (List.map snd el)
414414

415415
and add_cases bv cases =
416416
List.iter (add_case bv) cases

ocaml/parsing/jane_syntax.ml

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,12 +1169,11 @@ module Labeled_tuples = struct
11691169
module Of_ast = Of_ast (Ext)
11701170
include Ext
11711171

1172-
type nonrec core_type = Lttyp_tuple of (string option * core_type) list
1172+
type nonrec core_type = (string option * core_type) list
11731173

1174-
type nonrec expression = Ltexp_tuple of (string option * expression) list
1174+
type nonrec expression = (string option * expression) list
11751175

1176-
type nonrec pattern =
1177-
| Ltpat_tuple of (string option * pattern) list * closed_flag
1176+
type nonrec pattern = (string option * pattern) list * closed_flag
11781177

11791178
let string_of_label = function None -> "" | Some lbl -> lbl
11801179

@@ -1218,8 +1217,19 @@ module Labeled_tuples = struct
12181217
| PStr [] -> names, attrs
12191218
| _ -> Desugaring_error.raise loc (Has_payload payload)
12201219

1221-
let typ_of ~loc = function
1222-
| Lttyp_tuple tl ->
1220+
type 'a label_check_result =
1221+
| No_labels of 'a list
1222+
| At_least_one_label of (string option * 'a) list
1223+
1224+
let check_for_any_label xs =
1225+
if List.for_all (fun (lbl, _x) -> Option.is_none lbl) xs
1226+
then No_labels (List.map snd xs)
1227+
else At_least_one_label xs
1228+
1229+
let typ_of ~loc tl =
1230+
match check_for_any_label tl with
1231+
| No_labels tl -> Ast_helper.Typ.tuple ~loc tl
1232+
| At_least_one_label tl ->
12231233
(* See Note [Wrapping with make_entire_jane_syntax] *)
12241234
Core_type.make_entire_jane_syntax ~loc feature (fun () ->
12251235
let names = List.map (fun (label, _) -> string_of_label label) tl in
@@ -1238,11 +1248,13 @@ module Labeled_tuples = struct
12381248
let labeled_components =
12391249
List.map2 (fun s t -> label_of_string s, t) labels components
12401250
in
1241-
Lttyp_tuple labeled_components, ptyp_attributes
1251+
labeled_components, ptyp_attributes
12421252
| _ -> Desugaring_error.raise typ.ptyp_loc Malformed
12431253

1244-
let expr_of ~loc = function
1245-
| Ltexp_tuple el ->
1254+
let expr_of ~loc el =
1255+
match check_for_any_label el with
1256+
| No_labels el -> Ast_helper.Exp.tuple ~loc el
1257+
| At_least_one_label el ->
12461258
(* See Note [Wrapping with make_entire_jane_syntax] *)
12471259
Expression.make_entire_jane_syntax ~loc feature (fun () ->
12481260
let names = List.map (fun (label, _) -> string_of_label label) el in
@@ -1261,17 +1273,25 @@ module Labeled_tuples = struct
12611273
let labeled_components =
12621274
List.map2 (fun s e -> label_of_string s, e) labels components
12631275
in
1264-
Ltexp_tuple labeled_components, pexp_attributes
1276+
labeled_components, pexp_attributes
12651277
| _ -> Desugaring_error.raise expr.pexp_loc Malformed
12661278

1267-
let pat_of ~loc = function
1268-
| Ltpat_tuple (pl, closed) ->
1279+
let pat_of =
1280+
let make_jane_syntax ~loc pl closed =
12691281
(* See Note [Wrapping with make_entire_jane_syntax] *)
12701282
Pattern.make_entire_jane_syntax ~loc feature (fun () ->
12711283
let names = List.map (fun (label, _) -> string_of_label label) pl in
12721284
Pattern.make_jane_syntax feature
12731285
(string_of_closed_flag closed :: names)
12741286
@@ Ast_helper.Pat.tuple (List.map snd pl))
1287+
in
1288+
fun ~loc (pl, closed) ->
1289+
match closed with
1290+
| Open -> make_jane_syntax ~loc pl closed
1291+
| Closed -> (
1292+
match check_for_any_label pl with
1293+
| No_labels pl -> Ast_helper.Pat.tuple ~loc pl
1294+
| At_least_one_label pl -> make_jane_syntax ~loc pl closed)
12751295

12761296
(* Returns remaining unconsumed attributes *)
12771297
let of_pat pat =
@@ -1286,7 +1306,7 @@ module Labeled_tuples = struct
12861306
let labeled_components =
12871307
List.map2 (fun s e -> label_of_string s, e) labels components
12881308
in
1289-
Ltpat_tuple (labeled_components, closed), ppat_attributes
1309+
(labeled_components, closed), ppat_attributes
12901310
| _ -> Desugaring_error.raise pat.ppat_loc Malformed
12911311
end
12921312

ocaml/parsing/jane_syntax.mli

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -202,48 +202,53 @@ end
202202
(** The ASTs for labeled tuples. When we merge this upstream, we'll replace
203203
existing [P{typ,exp,pat}_tuple] constructors with these. *)
204204
module Labeled_tuples : sig
205-
type core_type =
206-
| Lttyp_tuple of (string option * Parsetree.core_type) list
207-
(** [Lttyp_tuple(tl)] represents a product type:
205+
(** [tl] represents a product type:
208206
- [T1 * ... * Tn] when [tl] is [(None,T1);...;(None,Tn)]
209207
- [L1:T1 * ... * Ln:Tn] when [tl] is [(Some L1,T1);...;(Some Ln,Tn)]
210208
- A mix, e.g. [L1:T1,T2] when [tl] is [(Some L1,T1);(None,T2)]
211209
212-
Invariant: [n >= 2] and there is at least one label.
210+
Invariant: [n >= 2].
213211
*)
212+
type core_type = (string option * Parsetree.core_type) list
214213

215-
type expression =
216-
| Ltexp_tuple of (string option * Parsetree.expression) list
217-
(** [Ltexp_tuple(el)] represents
214+
(** [el] represents
218215
- [(E1, ..., En)]
219216
when [el] is [(None, E1);...;(None, En)]
220217
- [(~L1:E1, ..., ~Ln:En)]
221218
when [el] is [(Some L1, E1);...;(Some Ln, En)]
222219
- A mix, e.g.:
223220
[(~L1:E1, E2)] when [el] is [(Some L1, E1); (None, E2)]
224221
225-
Invariant: [n >= 2] and there is at least one label.
222+
Invariant: [n >= 2].
226223
*)
224+
type expression = (string option * Parsetree.expression) list
227225

228-
type pattern =
229-
| Ltpat_tuple of
230-
(string option * Parsetree.pattern) list * Asttypes.closed_flag
231-
(** [Ltpat_tuple(pl, Closed)] represents
226+
(** [(pl, Closed)] represents
232227
- [(P1, ..., Pn)] when [pl] is [(None, P1);...;(None, Pn)]
233228
- [(L1:P1, ..., Ln:Pn)] when [pl] is
234229
[(Some L1, P1);...;(Some Ln, Pn)]
235230
- A mix, e.g. [(L1:P1, P2)] when [pl] is [(Some L1, P1);(None, P2)]
236231
- If pattern is open, then it also ends in a [..]
237232
238233
Invariant:
239-
- If Closed, [n >= 2] and there is at least one label.
240-
- If Open, [n >= 1]
234+
- If Closed, [n >= 2].
235+
- If Open, [n >= 1].
241236
*)
237+
type pattern = (string option * Parsetree.pattern) list * Asttypes.closed_flag
242238

239+
(** Embeds the core type in Jane Syntax only if there are any labels.
240+
Otherwise, returns a normal [Ptyp_tuple].
241+
*)
243242
val typ_of : loc:Location.t -> core_type -> Parsetree.core_type
244243

244+
(** Embeds the expression in Jane Syntax only if there are any labels.
245+
Otherwise, returns a normal [Pexp_tuple].
246+
*)
245247
val expr_of : loc:Location.t -> expression -> Parsetree.expression
246248

249+
(** Embeds the pattern in Jane Syntax only if there are any labels or
250+
if the pattern is open. Otherwise, returns a normal [Ppat_tuple].
251+
*)
247252
val pat_of : loc:Location.t -> pattern -> Parsetree.pattern
248253
end
249254

ocaml/parsing/parser.mly

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -443,26 +443,20 @@ let expecting (loc : Lexing.position * Lexing.position) nonterm =
443443
let removed_string_set loc =
444444
raise(Syntaxerr.Error(Syntaxerr.Removed_string_set(make_loc loc)))
445445

446-
let ppat_lttuple loc elts closed =
446+
let ppat_ltuple loc elts closed =
447447
Jane_syntax.Labeled_tuples.pat_of
448448
~loc:(make_loc loc)
449-
(Ltpat_tuple (elts, closed))
449+
(elts, closed)
450450

451-
let ptyp_lttuple loc tl =
451+
let ptyp_ltuple loc tl =
452452
Jane_syntax.Labeled_tuples.typ_of
453453
~loc:(make_loc loc)
454-
(Lttyp_tuple tl)
454+
tl
455455

456-
let mktyp_tuple loc ltys =
457-
if List.for_all (fun (lbl, _) -> Option.is_none lbl) ltys then
458-
mktyp ~loc (Ptyp_tuple (List.map snd ltys))
459-
else
460-
ptyp_lttuple loc ltys
461-
462-
let pexp_lttuple loc args =
456+
let pexp_ltuple loc args =
463457
Jane_syntax.Labeled_tuples.expr_of
464458
~loc:(make_loc loc)
465-
(Ltexp_tuple args)
459+
args
466460

467461
(* Using the function [not_expecting] in a semantic action means that this
468462
syntactic form is recognized by the parser but is in fact incorrect. This
@@ -2873,11 +2867,7 @@ fun_expr:
28732867
| simple_expr nonempty_llist(labeled_simple_expr)
28742868
{ mkexp ~loc:$sloc (Pexp_apply($1, $2)) }
28752869
| labeled_tuple %prec below_COMMA
2876-
{ if List.for_all (fun (l,_) -> Option.is_none l) $1 then
2877-
mkexp ~loc:$sloc (Pexp_tuple (List.map snd $1))
2878-
else
2879-
pexp_lttuple $sloc $1
2880-
}
2870+
{ pexp_ltuple $sloc $1 }
28812871
| mkrhs(constr_longident) simple_expr %prec below_HASH
28822872
{ mkexp ~loc:$sloc (Pexp_construct($1, Some $2)) }
28832873
| name_tag simple_expr %prec below_HASH
@@ -3530,12 +3520,7 @@ pattern_no_exn:
35303520
) { $1 }
35313521
| reversed_labeled_tuple_pattern(self)
35323522
{ let closed, pats = $1 in
3533-
if closed = Closed
3534-
&& List.for_all (fun (l,_) -> Option.is_none l) pats
3535-
then
3536-
mkpat ~loc:$sloc (Ppat_tuple(List.rev_map snd pats))
3537-
else
3538-
ppat_lttuple $sloc (List.rev pats) closed
3523+
ppat_ltuple $sloc (List.rev pats) closed
35393524
}
35403525
;
35413526

@@ -4307,7 +4292,7 @@ strict_function_or_labeled_tuple_type:
43074292
{
43084293
let ty, ltys = tuple in
43094294
let label = Labelled label in
4310-
let domain = mktyp_tuple $loc(tuple) ((None, ty) :: ltys) in
4295+
let domain = ptyp_ltuple $loc(tuple) ((None, ty) :: ltys) in
43114296
let domain = extra_rhs_core_type domain ~pos:$endpos(tuple) in
43124297
Ptyp_arrow(label, mktyp_with_modes unique_local domain , codomain) }
43134298
)
@@ -4321,7 +4306,7 @@ strict_function_or_labeled_tuple_type:
43214306
codomain = tuple_type
43224307
{ let ty, ltys = tuple in
43234308
let label = Labelled label in
4324-
let domain = mktyp_tuple $loc(tuple) ((None, ty) :: ltys) in
4309+
let domain = ptyp_ltuple $loc(tuple) ((None, ty) :: ltys) in
43254310
let domain = extra_rhs_core_type domain ~pos:$endpos(tuple) in
43264311
Ptyp_arrow(label,
43274312
mktyp_with_modes arg_unique_local domain ,
@@ -4331,7 +4316,7 @@ strict_function_or_labeled_tuple_type:
43314316
{ $1 }
43324317
| label = LIDENT COLON proper_tuple_type %prec MINUSGREATER
43334318
{ let ty, ltys = $3 in
4334-
ptyp_lttuple $sloc ((Some label, ty) :: ltys)
4319+
ptyp_ltuple $sloc ((Some label, ty) :: ltys)
43354320
}
43364321
;
43374322

@@ -4388,7 +4373,7 @@ tuple_type:
43884373
{ ty }
43894374
| proper_tuple_type %prec below_FUNCTOR
43904375
{ let ty, ltys = $1 in
4391-
mktyp_tuple $sloc ((None, ty) :: ltys)
4376+
ptyp_ltuple $sloc ((None, ty) :: ltys)
43924377
}
43934378
;
43944379

0 commit comments

Comments
 (0)