Skip to content

Commit 584cb01

Browse files
ccasinrtjoancik-roberts
authored
flambda-backend: Labeled Tuples (#2009)
* Labeled tuples * A couple minor error message improvements * Turn on labeled tuples by default * Delete some CRs in ocamldoc - we don't care about this tool * Move source test to correct location * Final nits * Add test with attribute * Add more attributes in tests * Address review feedback about jane syntax attributes * An additional test for reordering --------- Co-authored-by: Ryan Tjoa <[email protected]> Co-authored-by: Nick Roberts <[email protected]>
1 parent 6d4afb3 commit 584cb01

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+30477
-20915
lines changed

boot/menhir/parser.ml

Lines changed: 27714 additions & 20635 deletions
Large diffs are not rendered by default.

boot/ocamlc

85.7 KB
Binary file not shown.

debugger/eval.ml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,10 @@ let rec expression event env = function
120120
Ttuple ty_list ->
121121
if n < 1 || n > List.length ty_list
122122
then raise(Error(Tuple_index(ty, List.length ty_list, n)))
123-
else (Debugcom.Remote_value.field v (n-1), List.nth ty_list (n-1))
123+
(* CR labeled tuples: handle labels in debugger (also see "E_field"
124+
case) *)
125+
else (Debugcom.Remote_value.field v (n-1),
126+
snd (List.nth ty_list (n-1)))
124127
| Tconstr(path, [ty_arg], _) when Path.same path Predef.path_array ->
125128
let size = Debugcom.Remote_value.size v in
126129
if n >= size

lambda/matching.ml

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,8 @@ end = struct
309309
match p.pat_desc with
310310
| `Any -> `Any
311311
| `Constant cst -> `Constant cst
312-
| `Tuple ps -> `Tuple (List.map (alpha_pat env) ps)
312+
| `Tuple ps ->
313+
`Tuple (List.map (fun (label, p) -> label, alpha_pat env p) ps)
313314
| `Construct (cstr, cst_descr, args) ->
314315
`Construct (cstr, cst_descr, List.map (alpha_pat env) args)
315316
| `Variant (cstr, argo, row_desc) ->
@@ -642,7 +643,7 @@ end
642643
let rec flatten_pat_line size p k =
643644
match p.pat_desc with
644645
| Tpat_any | Tpat_var _ -> Patterns.omegas size :: k
645-
| Tpat_tuple args -> args :: k
646+
| Tpat_tuple args -> (List.map snd args) :: k
646647
| Tpat_or (p1, p2, _) ->
647648
flatten_pat_line size p1 (flatten_pat_line size p2 k)
648649
| Tpat_alias (p, _, _, _, _) ->
@@ -2211,7 +2212,7 @@ let divide_lazy ~scopes head ctx pm =
22112212
let get_pat_args_tuple arity p rem =
22122213
match p with
22132214
| { pat_desc = Tpat_any } -> Patterns.omegas arity @ rem
2214-
| { pat_desc = Tpat_tuple args } -> args @ rem
2215+
| { pat_desc = Tpat_tuple args } -> (List.map snd args) @ rem
22152216
| _ -> assert false
22162217

22172218
let get_expr_args_tuple ~scopes head (arg, _mut, _sort, _layout) rem =
@@ -3985,10 +3986,13 @@ let assign_pat ~scopes body_layout opt nraise catch_ids loc pat pat_sort lam =
39853986
match (pat.pat_desc, lam) with
39863987
| Tpat_tuple patl, Lprim (Pmakeblock _, lams, _) ->
39873988
opt := true;
3988-
List.fold_left2 (collect Jkind.Sort.for_tuple_element) acc patl lams
3989+
List.fold_left2
3990+
(fun acc (_, pat) lam ->
3991+
collect Jkind.Sort.for_tuple_element acc pat lam)
3992+
acc patl lams
39893993
| Tpat_tuple patl, Lconst (Const_block (_, scl)) ->
39903994
opt := true;
3991-
let collect_const acc pat sc =
3995+
let collect_const acc (_, pat) sc =
39923996
collect Jkind.Sort.for_tuple_element acc pat (Lconst sc)
39933997
in
39943998
List.fold_left2 collect_const acc patl scl
@@ -4070,13 +4074,13 @@ let for_tupled_function ~scopes ~return_layout loc paraml pats_act_list partial
40704074

40714075
let flatten_pattern size p =
40724076
match p.pat_desc with
4073-
| Tpat_tuple args -> args
4077+
| Tpat_tuple args -> List.map snd args
40744078
| Tpat_any -> Patterns.omegas size
40754079
| _ -> raise Cannot_flatten
40764080

40774081
let flatten_simple_pattern size (p : Simple.pattern) =
40784082
match p.pat_desc with
4079-
| `Tuple args -> args
4083+
| `Tuple args -> (List.map snd args)
40804084
| `Any -> Patterns.omegas size
40814085
| `Array _
40824086
| `Variant _

lambda/translcore.ml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ let rec trivial_pat pat =
226226
| Tpat_construct (_, cd, [], _) ->
227227
not cd.cstr_generalized && cd.cstr_consts = 1 && cd.cstr_nonconsts = 0
228228
| Tpat_tuple patl ->
229-
List.for_all trivial_pat patl
229+
List.for_all (fun (_, p) -> trivial_pat p) patl
230230
| _ -> false
231231

232232
let rec push_defaults loc bindings use_lhs arg_mode arg_sort cases
@@ -461,7 +461,7 @@ and transl_exp0 ~in_new_scope ~scopes sort e =
461461
| Texp_tuple (el, alloc_mode) ->
462462
let ll, shape =
463463
transl_list_with_shape ~scopes
464-
(List.map (fun a -> (a, Jkind.Sort.for_tuple_element)) el)
464+
(List.map (fun (_, a) -> (a, Jkind.Sort.for_tuple_element)) el)
465465
in
466466
begin try
467467
Lconst(Const_block(0, List.map extract_constant ll))
@@ -1700,11 +1700,15 @@ and transl_match ~scopes ~arg_sort ~return_sort e arg pat_expr_list partial =
17001700
| {exp_desc = Texp_tuple (argl, alloc_mode)}, [] ->
17011701
assert (static_handlers = []);
17021702
let mode = transl_alloc_mode alloc_mode in
1703-
let argl = List.map (fun a -> (a, Jkind.Sort.for_tuple_element)) argl in
1703+
let argl =
1704+
List.map (fun (_, a) -> (a, Jkind.Sort.for_tuple_element)) argl
1705+
in
17041706
Matching.for_multiple_match ~scopes ~return_layout e.exp_loc
17051707
(transl_list_with_layout ~scopes argl) mode val_cases partial
17061708
| {exp_desc = Texp_tuple (argl, alloc_mode)}, _ :: _ ->
1707-
let argl = List.map (fun a -> (a, Jkind.Sort.for_tuple_element)) argl in
1709+
let argl =
1710+
List.map (fun (_, a) -> (a, Jkind.Sort.for_tuple_element)) argl
1711+
in
17081712
let val_ids, lvars =
17091713
List.map
17101714
(fun (arg,s) ->

ocamldoc/odoc_ast.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ module Analyser =
262262

263263
| Typedtree.Tpat_tuple patlist ->
264264
Tuple
265-
(List.map iter_pattern patlist,
265+
(List.map (fun (_, p) -> iter_pattern p) patlist,
266266
Odoc_env.subst_type env pat.pat_type)
267267

268268
| Typedtree.Tpat_construct (_, cons_desc, _, _) when

ocamldoc/odoc_value.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ let dummy_parameter_list typ =
9191
let open Asttypes in
9292
if label = Nolabel then
9393
Odoc_parameter.Tuple
94-
(List.map (fun t2 -> iter (Nolabel, t2)) l, t)
94+
(List.map (fun t2 -> iter (Nolabel, t2)) (List.map snd l), t)
9595
else
9696
(* if there is a label, then we don't want to decompose the tuple *)
9797
Odoc_parameter.Simple_name

parsing/ast_invariants.ml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@ let err = Syntaxerr.ill_formed_ast
2020

2121
let empty_record loc = err loc "Records cannot be empty."
2222
let invalid_tuple loc = err loc "Tuples must have at least 2 components."
23+
let unlabeled_labeled_tuple_typ loc =
24+
err loc "Labeled tuple types must have at least one labeled component."
25+
let unlabeled_labeled_tuple_exp loc =
26+
err loc "Labeled tuples must have at least one labeled component."
27+
let unlabeled_labeled_tuple_pat loc =
28+
err loc
29+
"Closed labeled tuple patterns must have at least one labeled component."
30+
let empty_open_labeled_tuple_pat loc =
31+
err loc "Open labeled tuple patterns must have at least one component."
32+
let short_closed_labeled_tuple_pat loc =
33+
err loc "Closed labeled tuple patterns must have at least 2 components."
2334
let no_args loc = err loc "Function application with no argument."
2435
let empty_let loc = err loc "Let with no bindings."
2536
let empty_type loc = err loc "Type declarations cannot be empty."
@@ -43,6 +54,9 @@ let simple_longident id =
4354
in
4455
if not (is_simple id.txt) then complex_id id.loc
4556

57+
let labeled_tuple_without_label lt =
58+
List.for_all (fun (lbl,_) -> Option.is_none lbl) lt
59+
4660
let iterator =
4761
let super = Ast_iterator.default_iterator in
4862
let type_declaration self td =
@@ -52,15 +66,39 @@ let iterator =
5266
| Ptype_record [] -> empty_record loc
5367
| _ -> ()
5468
in
69+
let jtyp _self loc (jtyp : Jane_syntax.Core_type.t) =
70+
match jtyp with
71+
| Jtyp_layout (Ltyp_var _ | Ltyp_poly _ | Ltyp_alias _) -> ()
72+
| Jtyp_tuple (Lttyp_tuple ([] | [_])) -> invalid_tuple loc
73+
| Jtyp_tuple (Lttyp_tuple l) ->
74+
if labeled_tuple_without_label l then unlabeled_labeled_tuple_typ loc
75+
in
5576
let typ self ty =
5677
super.typ self ty;
5778
let loc = ty.ptyp_loc in
79+
match Jane_syntax.Core_type.of_ast ty with
80+
| Some (jtyp_, _attrs) -> jtyp self ty.ptyp_loc jtyp_
81+
| None ->
5882
match ty.ptyp_desc with
5983
| Ptyp_tuple ([] | [_]) -> invalid_tuple loc
6084
| Ptyp_package (_, cstrs) ->
6185
List.iter (fun (id, _) -> simple_longident id) cstrs
6286
| _ -> ()
6387
in
88+
let jpat _self loc (jpat : Jane_syntax.Pattern.t) =
89+
match jpat with
90+
| Jpat_immutable_array (Iapat_immutable_array _)-> ()
91+
| Jpat_layout (Lpat_constant _) -> ()
92+
| Jpat_tuple lt -> begin
93+
match lt with
94+
| Ltpat_tuple ([], Open) -> empty_open_labeled_tuple_pat loc
95+
| Ltpat_tuple (([] | [_]), Closed) ->
96+
short_closed_labeled_tuple_pat loc
97+
| Ltpat_tuple (l, Closed) ->
98+
if labeled_tuple_without_label l then unlabeled_labeled_tuple_pat loc
99+
| Ltpat_tuple (_ :: _, Open) -> ()
100+
end
101+
in
64102
let pat self pat =
65103
begin match pat.ppat_desc with
66104
| Ppat_construct (_, Some (_, ({ppat_desc = Ppat_tuple _} as p)))
@@ -70,6 +108,9 @@ let iterator =
70108
super.pat self pat
71109
end;
72110
let loc = pat.ppat_loc in
111+
match Jane_syntax.Pattern.of_ast pat with
112+
| Some (jpat_, _attrs) -> jpat self pat.ppat_loc jpat_
113+
| None ->
73114
match pat.ppat_desc with
74115
| Ppat_tuple ([] | [_]) -> invalid_tuple loc
75116
| Ppat_record ([], _) -> empty_record loc
@@ -100,6 +141,12 @@ let iterator =
100141
| Cexp_array_comprehension (_, {clauses = []; body = _}) )
101142
->
102143
empty_comprehension loc
144+
| Jexp_tuple lt -> begin
145+
match lt with
146+
| Ltexp_tuple ([] | [_]) -> invalid_tuple loc
147+
| Ltexp_tuple l ->
148+
if labeled_tuple_without_label l then unlabeled_labeled_tuple_exp loc
149+
end
103150
| Jexp_comprehension _
104151
| Jexp_immutable_array _
105152
| Jexp_layout _

parsing/ast_iterator.ml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ let iter_loc_txt sub f { loc; txt } =
9898
module T = struct
9999
(* Type expressions for the core language *)
100100

101+
module LT = Jane_syntax.Labeled_tuples
102+
101103
let row_field sub {
102104
prf_desc;
103105
prf_loc;
@@ -137,8 +139,12 @@ module T = struct
137139
sub.typ sub aliased_type;
138140
iter_loc_txt sub sub.jkind_annotation jkind
139141

142+
let iter_jst_labeled_tuple sub : LT.core_type -> _ = function
143+
| Lttyp_tuple tl -> List.iter (iter_snd (sub.typ sub)) tl
144+
140145
let iter_jst sub : Jane_syntax.Core_type.t -> _ = function
141146
| Jtyp_layout typ -> iter_jst_layout sub typ
147+
| Jtyp_tuple lt_typ -> iter_jst_labeled_tuple sub lt_typ
142148

143149
let iter sub ({ptyp_desc = desc; ptyp_loc = loc; ptyp_attributes = attrs}
144150
as typ) =
@@ -438,6 +444,7 @@ module E = struct
438444
module IA = Jane_syntax.Immutable_arrays
439445
module L = Jane_syntax.Layouts
440446
module N_ary = Jane_syntax.N_ary_functions
447+
module LT = Jane_syntax.Labeled_tuples
441448

442449
let iter_iterator sub : C.iterator -> _ = function
443450
| Range { start; stop; direction = _ } ->
@@ -511,11 +518,15 @@ module E = struct
511518
Option.iter (iter_function_constraint sub) constraint_;
512519
iter_function_body sub body
513520

521+
let iter_labeled_tuple sub : LT.expression -> _ = function
522+
| Ltexp_tuple el -> List.iter (iter_snd (sub.expr sub)) el
523+
514524
let iter_jst sub : Jane_syntax.Expression.t -> _ = function
515525
| Jexp_comprehension comp_exp -> iter_comp_exp sub comp_exp
516526
| Jexp_immutable_array iarr_exp -> iter_iarr_exp sub iarr_exp
517527
| Jexp_layout layout_exp -> iter_layout_exp sub layout_exp
518528
| Jexp_n_ary_function n_ary_exp -> iter_n_ary_function sub n_ary_exp
529+
| Jexp_tuple lt_exp -> iter_labeled_tuple sub lt_exp
519530

520531
let iter sub
521532
({pexp_loc = loc; pexp_desc = desc; pexp_attributes = attrs} as expr)=
@@ -611,14 +622,20 @@ module P = struct
611622
(* Patterns *)
612623

613624
module IA = Jane_syntax.Immutable_arrays
625+
module LT = Jane_syntax.Labeled_tuples
614626

615627
let iter_iapat sub : IA.pattern -> _ = function
616628
| Iapat_immutable_array elts ->
617629
List.iter (sub.pat sub) elts
618630

631+
let iter_labeled_tuple sub : LT.pattern -> _ = function
632+
| Ltpat_tuple (pl, _) ->
633+
List.iter (iter_snd (sub.pat sub)) pl
634+
619635
let iter_jst sub : Jane_syntax.Pattern.t -> _ = function
620636
| Jpat_immutable_array iapat -> iter_iapat sub iapat
621637
| Jpat_layout (Lpat_constant _) -> iter_constant
638+
| Jpat_tuple ltpat -> iter_labeled_tuple sub ltpat
622639

623640
let iter sub
624641
({ppat_desc = desc; ppat_loc = loc; ppat_attributes = attrs} as pat) =

parsing/ast_mapper.ml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ end
121121
module T = struct
122122
(* Type expressions for the core language *)
123123

124+
module LT = Jane_syntax.Labeled_tuples
125+
124126
let row_field sub {
125127
prf_desc;
126128
prf_loc;
@@ -171,9 +173,14 @@ module T = struct
171173
let jkind = map_loc_txt sub sub.jkind_annotation jkind in
172174
Ltyp_alias { aliased_type; name; jkind }
173175

176+
let map_jst_labeled_tuple sub : LT.core_type -> LT.core_type = function
177+
(* 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)
179+
174180
let map_jst sub : Jane_syntax.Core_type.t -> Jane_syntax.Core_type.t =
175181
function
176182
| Jtyp_layout typ -> Jtyp_layout (map_jst_layouts sub typ)
183+
| Jtyp_tuple x -> Jtyp_tuple (map_jst_labeled_tuple sub x)
177184

178185
let map sub ({ptyp_desc = desc; ptyp_loc = loc; ptyp_attributes = attrs}
179186
as typ) =
@@ -518,6 +525,7 @@ module E = struct
518525
module IA = Jane_syntax.Immutable_arrays
519526
module L = Jane_syntax.Layouts
520527
module N_ary = Jane_syntax.N_ary_functions
528+
module LT = Jane_syntax.Labeled_tuples
521529

522530
let map_iterator sub : C.iterator -> C.iterator = function
523531
| Range { start; stop; direction } ->
@@ -607,12 +615,17 @@ module E = struct
607615
let body = map_function_body sub body in
608616
params, constraint_, body
609617

618+
let map_ltexp sub : LT.expression -> LT.expression = function
619+
(* CR labeled tuples: Eventually mappers may want to see the labels. *)
620+
| Ltexp_tuple el -> Ltexp_tuple (List.map (map_snd (sub.expr sub)) el)
621+
610622
let map_jst sub : Jane_syntax.Expression.t -> Jane_syntax.Expression.t =
611623
function
612624
| Jexp_comprehension x -> Jexp_comprehension (map_cexp sub x)
613625
| Jexp_immutable_array x -> Jexp_immutable_array (map_iaexp sub x)
614626
| Jexp_layout x -> Jexp_layout (map_layout_exp sub x)
615627
| Jexp_n_ary_function x -> Jexp_n_ary_function (map_n_ary_exp sub x)
628+
| Jexp_tuple ltexp -> Jexp_tuple (map_ltexp sub ltexp)
616629

617630
let map sub
618631
({pexp_loc = loc; pexp_desc = desc; pexp_attributes = attrs} as exp) =
@@ -718,6 +731,7 @@ module P = struct
718731

719732
module IA = Jane_syntax.Immutable_arrays
720733
module L = Jane_syntax.Layouts
734+
module LT = Jane_syntax.Labeled_tuples
721735

722736
let map_iapat sub : IA.pattern -> IA.pattern = function
723737
| Iapat_immutable_array elts ->
@@ -729,10 +743,16 @@ module P = struct
729743
*)
730744
| Float _ | Integer _ as x -> x
731745

746+
let map_ltpat sub : LT.pattern -> LT.pattern = function
747+
(* CR labeled tuples: Eventually mappers may want to see the labels. *)
748+
| Ltpat_tuple (pl, closed) ->
749+
Ltpat_tuple (List.map (map_snd (sub.pat sub)) pl, closed)
750+
732751
let map_jst sub : Jane_syntax.Pattern.t -> Jane_syntax.Pattern.t = function
733752
| Jpat_immutable_array x -> Jpat_immutable_array (map_iapat sub x)
734753
| Jpat_layout (Lpat_constant x) ->
735754
Jpat_layout (Lpat_constant (map_unboxed_constant_pat sub x))
755+
| Jpat_tuple ltpat -> Jpat_tuple (map_ltpat sub ltpat)
736756

737757
let map sub
738758
({ppat_desc = desc; ppat_loc = loc; ppat_attributes = attrs} as pat) =

0 commit comments

Comments
 (0)