Skip to content

Commit a1821b1

Browse files
committed
[1523] Respond to comments, update core_type_desc
1 parent 18e55a5 commit a1821b1

14 files changed

+65
-23
lines changed

ocaml/parsing/ast_mapper.ml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,9 @@ module T = struct
160160
| Ptyp_var s -> var ~loc ~attrs s
161161
| Ptyp_arrow (lab, t1, t2) ->
162162
arrow ~loc ~attrs lab (sub.typ sub t1) (sub.typ sub t2)
163-
| Ptyp_tuple tyl -> tuple ~loc ~attrs (List.map (fun (label,typ) -> label, sub.typ sub typ) tyl)
163+
| Ptyp_tuple tyl ->
164+
tuple ~loc ~attrs
165+
(List.map (fun (label,typ) -> label, sub.typ sub typ) tyl)
164166
| Ptyp_constr (lid, tl) ->
165167
constr ~loc ~attrs (map_loc sub lid) (List.map (sub.typ sub) tl)
166168
| Ptyp_object (l, o) ->
@@ -637,7 +639,8 @@ module P = struct
637639
| Ppat_constant c -> constant ~loc ~attrs (sub.constant sub c)
638640
| Ppat_interval (c1, c2) ->
639641
interval ~loc ~attrs (sub.constant sub c1) (sub.constant sub c2)
640-
| Ppat_tuple pl -> tuple ~loc ~attrs (List.map (fun (label, p) -> label, sub.pat sub p) pl)
642+
| Ppat_tuple pl ->
643+
tuple ~loc ~attrs (List.map (fun (label, p) -> label, sub.pat sub p) pl)
641644
| Ppat_construct (l, p) ->
642645
construct ~loc ~attrs (map_loc sub l)
643646
(map_opt

ocaml/parsing/parser.mly

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3132,7 +3132,8 @@ pattern_no_exn:
31323132

31333133
%inline pattern_(self):
31343134
| self COLONCOLON pattern
3135-
{ mkpat_cons ~loc:$sloc $loc($2) (ghpat ~loc:$sloc (Ppat_tuple[None,$1;None,$3])) }
3135+
{ mkpat_cons ~loc:$sloc $loc($2)
3136+
(ghpat ~loc:$sloc (Ppat_tuple[None,$1;None,$3])) }
31363137
| self attribute
31373138
{ Pat.attr $1 $2 }
31383139
| pattern_gen
@@ -3145,7 +3146,7 @@ pattern_no_exn:
31453146
(* CR labeled tuples: merge the below two cases *)
31463147
| pattern_comma_list(self) %prec below_COMMA
31473148
{ Ppat_tuple(List.rev_map (fun p -> None, p) $1) }
3148-
| TILDETILDELPAREN labeled_pattern_comma_list(self) RPAREN // %prec below_COMMA
3149+
| TILDETILDELPAREN labeled_pattern_comma_list(self) RPAREN
31493150
{ Ppat_tuple(List.rev $2) }
31503151
| self COLONCOLON error
31513152
{ expecting $loc($3) "pattern" }
@@ -3869,7 +3870,7 @@ tuple_type:
38693870
| TILDETILDELPAREN mktyp(
38703871
labeled_tys = separated_nontrivial_llist(STAR, labeled_atomic_type)
38713872
{ Ptyp_tuple labeled_tys }
3872-
) RPAREN
3873+
) RPAREN
38733874
{ $2 }
38743875
;
38753876

ocaml/parsing/parsetree.mli

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -356,9 +356,12 @@ and expression_desc =
356356
(** [try E0 with P1 -> E1 | ... | Pn -> En] *)
357357
| Pexp_tuple of (string option * expression) list
358358
(** [Pexp_tuple(el)] represents
359-
- [(E1, ..., En)] when [el] is [(None, E1);...;(None, En)],
360-
- [(L1:E1, ..., Ln:En)] when [el] is [(Some L1, E1);...;(Some Ln, En)],
361-
- Any mix, e.g. [(L1: E1, E2)] when [el] is [(Some L1, E1); (None, E2)]
359+
- [(E1, ..., En)]
360+
when [el] is [(None, E1);...;(None, En)]
361+
- [(~L1:E1, ..., ~Ln:En)]
362+
when [el] is [(Some L1, E1);...;(Some Ln, En)]
363+
- Any mix, e.g.:
364+
[(~L1:E1, E2)] when [el] is [(Some L1, E1); (None, E2)]
362365
363366
Invariant: [n >= 2]
364367
*)

ocaml/testsuite/tests/typing-labeled-tuples/labeledtuples.ml

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,8 @@ Line 1, characters 4-23:
205205
Error: Labeled tuple patterns are not yet supported
206206
|}]
207207

208-
(* Labeled tuple pattern in constructor pattern *)
208+
(* Labeled tuple pattern in constructor pattern, with the same arity as the
209+
constructor. This is intentionally disallowed. *)
209210
let f = function
210211
| Pair (~~(~x=5; 2)) -> true
211212
| _ -> false
@@ -216,7 +217,10 @@ Line 2, characters 2-20:
216217
Error: Constructors cannot have labeled arguments. Consider using an inline record instead.
217218
|}]
218219

219-
(* Labeled tuple pattern in constructor pattern *)
220+
(* Labeled tuple patterns in constructor patterns with that can union with the
221+
constructor pattern type.
222+
223+
CR labeled tuples: these should eventually work. *)
220224
let f = function
221225
| Some (~~(~x=5; 2)) -> true
222226
| _ -> false
@@ -227,6 +231,19 @@ Line 2, characters 7-20:
227231
Error: Labeled tuple patterns are not yet supported
228232
|}]
229233

234+
235+
type t = Foo of (~~(x:int * int))
236+
let f = function
237+
| Foo (~~(~x=5; 2)) -> true
238+
| _ -> false
239+
[%%expect{|
240+
type t = Foo of (x:int * int)
241+
Line 3, characters 6-19:
242+
3 | | Foo (~~(~x=5; 2)) -> true
243+
^^^^^^^^^^^^^
244+
Error: Labeled tuple patterns are not yet supported
245+
|}]
246+
230247
(* CR labeled tuples: test constructor special cases thoroughly once patterns
231248
are typed. *)
232249

ocaml/testsuite/tests/typing-labeled-tuples/labeledtuples_dsource.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@ Line 1, characters 4-32:
3131
1 | let (~~(~x=x0; ~s; ~(y:int); _)) : ~~(x:int * s:string * y:int * string) =
3232
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3333
Error: Labeled tuple patterns are not yet supported
34-
|}]
34+
|}]

ocaml/typing/printtyped.ml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ let rec core_type i ppf x =
215215
core_type i ppf ct2;
216216
| Ttyp_tuple l ->
217217
line i ppf "Ttyp_tuple\n";
218-
list i core_type ppf l;
218+
list i labeled_core_type ppf l;
219219
| Ttyp_constr (li, _, l) ->
220220
line i ppf "Ttyp_constr %a\n" fmt_path li;
221221
list i core_type ppf l;
@@ -250,6 +250,10 @@ let rec core_type i ppf x =
250250
line i ppf "Ttyp_package %a\n" fmt_path s;
251251
list i package_with ppf l;
252252

253+
and labeled_core_type i ppf (l, t) =
254+
tuple_component_label i ppf l;
255+
core_type i ppf t
256+
253257
and package_with i ppf (s, t) =
254258
line i ppf "with type %a\n" fmt_longident s;
255259
core_type i ppf t

ocaml/typing/tast_iterator.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ let typ sub {ctyp_desc; ctyp_env; _} =
462462
| Ttyp_arrow (_, ct1, ct2) ->
463463
sub.typ sub ct1;
464464
sub.typ sub ct2
465-
| Ttyp_tuple list -> List.iter (sub.typ sub) list
465+
| Ttyp_tuple list -> List.iter (fun (_, t) -> sub.typ sub t) list
466466
| Ttyp_constr (_, _, list) -> List.iter (sub.typ sub) list
467467
| Ttyp_object (list, _) -> List.iter (sub.object_field sub) list
468468
| Ttyp_class (_, _, list) -> List.iter (sub.typ sub) list

ocaml/typing/tast_mapper.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,8 @@ let typ sub x =
692692
| Ttyp_var _ as d -> d
693693
| Ttyp_arrow (label, ct1, ct2) ->
694694
Ttyp_arrow (label, sub.typ sub ct1, sub.typ sub ct2)
695-
| Ttyp_tuple list -> Ttyp_tuple (List.map (sub.typ sub) list)
695+
| Ttyp_tuple list ->
696+
Ttyp_tuple (List.map (fun (label, t) -> label, sub.typ sub t) list)
696697
| Ttyp_constr (path, lid, list) ->
697698
Ttyp_constr (path, lid, List.map (sub.typ sub) list)
698699
| Ttyp_object (list, closed) ->

ocaml/typing/typecore.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2415,6 +2415,9 @@ and type_pat_aux
24152415
List.map
24162416
(fun (label, p) ->
24172417
if Option.is_some label then
2418+
(* CR labeled tuples: is this the best error to give?
2419+
It's a bit unexpected because a constructor can take a
2420+
labeled tuple for a single arg. *)
24182421
raise(Error(loc, !env, Constructor_labeled_arg));
24192422
p)
24202423
spl

ocaml/typing/typedtree.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ and core_type_desc =
525525
Ttyp_any
526526
| Ttyp_var of string
527527
| Ttyp_arrow of arg_label * core_type * core_type
528-
| Ttyp_tuple of core_type list
528+
| Ttyp_tuple of (string option * core_type) list
529529
| Ttyp_constr of Path.t * Longident.t loc * core_type list
530530
| Ttyp_object of object_field list * closed_flag
531531
| Ttyp_class of Path.t * Longident.t loc * core_type list

ocaml/typing/typedtree.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,7 @@ and core_type_desc =
726726
Ttyp_any
727727
| Ttyp_var of string
728728
| Ttyp_arrow of arg_label * core_type * core_type
729-
| Ttyp_tuple of core_type list
729+
| Ttyp_tuple of (string option * core_type) list
730730
| Ttyp_constr of Path.t * Longident.t loc * core_type list
731731
| Ttyp_object of object_field list * closed_flag
732732
| Ttyp_class of Path.t * Longident.t loc * core_type list

ocaml/typing/types.mli

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ and type_desc =
7979

8080
| Ttuple of (string option * type_expr) list
8181
(** [Ttuple [None, t1; ...; None, tn]] ==> [t1 * ... * tn]
82-
[Ttuple [Some "l1", t1; ...; Some "ln", tn]] ==> [l1: t1 * ... * ln: tn]
82+
[Ttuple [Some "l1", t1; ...; Some "ln", tn]] ==> [l1:t1 * ... * ln:tn]
8383
8484
Any mix of labeled and unlabeled components also works:
85-
[Ttuple [Some "l1", t1; None, t2; Some "l3", t3]] ==> [l1: t1 * t2 * l3: t3]
85+
[Ttuple [Some "l1", t1; None, t2; Some "l3", t3]] ==> [l1:t1 * t2 * l3:t3]
8686
*)
8787

8888
| Tconstr of Path.t * type_expr list * abbrev_memo ref

ocaml/typing/typetexp.ml

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,11 @@ and transl_type_aux env policy mode styp =
514514
loop mode args
515515
| Ptyp_tuple stl ->
516516
assert (List.length stl >= 2);
517-
let labeled_ctys = List.map (fun (label, t) -> label, transl_type env policy Alloc_mode.Global t) stl in
517+
let labeled_ctys =
518+
List.map
519+
(fun (label, t) -> label, transl_type env policy Alloc_mode.Global t)
520+
stl
521+
in
518522
List.iter (fun (_, {ctyp_type; ctyp_loc}) ->
519523
(* CR layouts v5: remove value requirement *)
520524
match
@@ -527,8 +531,12 @@ and transl_type_aux env policy mode styp =
527531
Non_value {vloc = Tuple; err = e; typ = ctyp_type})))
528532
labeled_ctys;
529533
(* CR labeled tuples: handle labeled tuple type expressions *)
530-
let ty = newty (Ttuple (List.map (fun (label, ctyp) -> label, ctyp.ctyp_type) labeled_ctys)) in
531-
ctyp (Ttyp_tuple (List.map snd labeled_ctys)) ty
534+
let ty =
535+
newty
536+
(Ttuple
537+
(List.map (fun (label, ctyp) -> label, ctyp.ctyp_type) labeled_ctys))
538+
in
539+
ctyp (Ttyp_tuple labeled_ctys) ty
532540
| Ptyp_constr(lid, stl) ->
533541
let (path, decl) = Env.lookup_type ~loc:lid.loc lid.txt env in
534542
let stl =

ocaml/typing/untypeast.ml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ let pattern : type k . _ -> k T.general_pattern -> _ = fun sub pat ->
349349
| Tpat_constant cst -> Ppat_constant (constant cst)
350350
| Tpat_tuple list ->
351351
(* CR labeled tuples: update once Tpat_tuple has labels *)
352-
Ppat_tuple (List.map (fun p -> None,(sub.pat sub p)) list)
352+
Ppat_tuple (List.map (fun p -> None, (sub.pat sub p)) list)
353353
| Tpat_construct (lid, _, args, vto) ->
354354
let tyo =
355355
match vto with
@@ -899,7 +899,9 @@ let core_type sub ct =
899899
| Ttyp_arrow (label, ct1, ct2) ->
900900
Ptyp_arrow (label, sub.typ sub ct1, sub.typ sub ct2)
901901
(* CR labeled tuples: update when Ttyp_tuple has labels *)
902-
| Ttyp_tuple list -> Ptyp_tuple (List.map (fun typ -> None, sub.typ sub typ) list)
902+
| Ttyp_tuple list ->
903+
Ptyp_tuple
904+
(List.map (fun (label, typ) -> label, sub.typ sub typ) list)
903905
| Ttyp_constr (_path, lid, list) ->
904906
Ptyp_constr (map_loc sub lid,
905907
List.map (sub.typ sub) list)

0 commit comments

Comments
 (0)