Skip to content

Commit 8fd824f

Browse files
committed
move the logic of legacy mode to printtyp.ml
1 parent d9baaaa commit 8fd824f

File tree

3 files changed

+40
-16
lines changed

3 files changed

+40
-16
lines changed

ocaml/typing/oprint.ml

+17-10
Original file line numberDiff line numberDiff line change
@@ -338,14 +338,17 @@ let pr_var_jkinds =
338338
disallowed in parsing of this file, but non-legacy modes might still pop
339339
up. For example, the current file might cite values from other files that
340340
mention non-legacy modes *)
341-
let print_out_legacy_axis f ppf = function
342-
| None -> ()
343-
| Some m -> Format.fprintf ppf "%a_ " f m
341+
let print_out_mode ppf = function
342+
| Omd_local -> fprintf ppf "local_"
343+
| Omd_unique -> fprintf ppf "unique_"
344+
| Omd_once -> fprintf ppf "once_"
345+
346+
let print_out_mode_space ppf m =
347+
print_out_mode ppf m;
348+
pp_print_space ppf ()
344349

345-
let print_out_modes ppf (m : Mode.Alloc.Const.Option.t) =
346-
print_out_legacy_axis Mode.Locality.Const.print ppf m.locality;
347-
print_out_legacy_axis Mode.Linearity.Const.print ppf m.linearity;
348-
print_out_legacy_axis Mode.Uniqueness.Const.print ppf m.uniqueness
350+
let print_out_modes ppf l =
351+
pp_print_list print_out_mode_space ppf l
349352

350353
(* Labeled tuples with the first element labeled sometimes require parens. *)
351354
let is_initially_labeled_tuple ty =
@@ -377,12 +380,16 @@ let rec print_out_type_0 ppf =
377380
- Or, there is at least one mode to print.
378381
*)
379382
and print_out_type_mode ~arg mode ppf ty =
380-
let mode = Format.asprintf "%a" print_out_modes mode in
383+
let has_modes =
384+
match mode with
385+
| [] -> false
386+
| _ -> true
387+
in
381388
let parens =
382389
is_initially_labeled_tuple ty
383-
&& (arg || String.length mode > 0)
390+
&& (arg || has_modes)
384391
in
385-
pp_print_string ppf mode;
392+
print_out_modes ppf mode;
386393
if parens then
387394
pp_print_char ppf '(';
388395
print_out_type_2 ppf ty;

ocaml/typing/outcometree.mli

+8-3
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,19 @@ type arg_label =
8484
| Optional of string
8585
| Position of string
8686

87-
type out_arg_mode = Mode.Alloc.Const.Option.t
87+
type out_mode =
88+
| Omd_local
89+
| Omd_unique
90+
| Omd_once
91+
92+
type out_arg_mode = out_mode list
8893

8994
type out_ret_mode =
90-
| Orm_not_arrow of Mode.Alloc.Const.Option.t
95+
| Orm_not_arrow of out_mode list
9196
(** The ret type is not arrow, with modes annotating. *)
9297
| Orm_no_parens
9398
(** The ret type is arrow, and no need to print parens around the arrow *)
94-
| Orm_parens of Mode.Alloc.Const.Option.t
99+
| Orm_parens of out_mode list
95100
(** The ret type is arrow, and need to print parens around the arrow, with
96101
modes annotating. *)
97102

ocaml/typing/printtyp.ml

+15-3
Original file line numberDiff line numberDiff line change
@@ -1257,6 +1257,18 @@ let outcome_label : Types.arg_label -> Outcometree.arg_label = function
12571257
| Optional l -> Optional l
12581258
| Position l -> Position l
12591259

1260+
let tree_of_mode mode l =
1261+
match mode with
1262+
| None -> []
1263+
(* should never raise *)
1264+
| Some x -> [List.assoc x l]
1265+
1266+
let tree_of_modes modes =
1267+
let diff = Mode.Alloc.Const.diff modes Mode.Alloc.Const.legacy in
1268+
tree_of_mode diff.locality [Mode.Locality.Const.Local, Omd_local] @
1269+
tree_of_mode diff.linearity [Mode.Linearity.Const.Once, Omd_once] @
1270+
tree_of_mode diff.uniqueness [Mode.Uniqueness.Const.Unique, Omd_unique]
1271+
12601272
(* [alloc_mode] is only used if [ty] is arrow-shaped. *)
12611273
let rec tree_of_typexp mode alloc_mode ty =
12621274
let px = proxy ty in
@@ -1294,7 +1306,7 @@ let rec tree_of_typexp mode alloc_mode ty =
12941306
in
12951307
let acc_mode = curry_mode alloc_mode am in
12961308
let (rm, t2) = tree_of_ret_typ mode acc_mode (mret, ty2) in
1297-
Otyp_arrow (lab, Alloc.Const.diff am Alloc.Const.legacy, t1, rm, t2)
1309+
Otyp_arrow (lab, tree_of_modes am, t1, rm, t2)
12981310
| Ttuple labeled_tyl ->
12991311
Otyp_tuple (tree_of_labeled_typlist mode labeled_tyl)
13001312
| Tconstr(p, tyl, _abbrev) ->
@@ -1436,12 +1448,12 @@ and tree_of_ret_typ mode acc_mode (m, ty) =
14361448
axes and we adopt a similar logic to the [marg] above. *)
14371449
let m = Alloc.zap_to_legacy m in
14381450
let ty = tree_of_typexp mode m ty in
1439-
(Orm_parens (Alloc.Const.diff m Alloc.Const.legacy), ty)
1451+
(Orm_parens (tree_of_modes m), ty)
14401452
end
14411453
| _ ->
14421454
let m = Alloc.zap_to_legacy m in
14431455
let ty = tree_of_typexp mode m ty in
1444-
(Orm_not_arrow (Alloc.Const.diff m Alloc.Const.legacy), ty)
1456+
(Orm_not_arrow (tree_of_modes m), ty)
14451457

14461458
and tree_of_typobject mode fi nm =
14471459
begin match nm with

0 commit comments

Comments
 (0)