Skip to content

Commit 468e4b8

Browse files
authored
Import compiler changes from PR 5398 (#218)
* Backport changes * Fix spacing
1 parent 3f451fb commit 468e4b8

File tree

4 files changed

+141
-21
lines changed

4 files changed

+141
-21
lines changed

src/ocaml/typing/printtyp.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1352,7 +1352,7 @@ let outcome_label : Types.arg_label -> Outcometree.arg_label = function
13521352
accordingly. *)
13531353
let tree_of_modalities mut t =
13541354
t
1355-
|> Typemode.least_modalities_implying mut
1355+
|> Typemode.least_modalities ~include_implied:false ~mut
13561356
|> Typemode.sort_dedup_modalities
13571357
|> List.map (fun (Atom (ax, m) : Modality.atom) ->
13581358
Format.asprintf "%a" (Modality.Per_axis.print ax) m)

src/ocaml/typing/typemode.ml

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ let idx_expected_modalities ~(mut : bool) =
520520
"Typemode.idx_expected_modalities: mismatch with mutable implied \
521521
modalities"
522522

523-
let least_modalities_implying mut (t : Modality.Const.t) =
523+
let least_modalities ~include_implied ~mut (t : Modality.Const.t) =
524524
let baseline =
525525
mutable_implied_modalities ~for_mutable_variable:false
526526
(Types.is_mutable mut)
@@ -534,7 +534,7 @@ let least_modalities_implying mut (t : Modality.Const.t) =
534534
List.filter_map
535535
(fun (Modality.Atom (ax, m_implied)) ->
536536
let m_projected = Modality.Const.proj ax t in
537-
if m_projected <> m_implied
537+
if m_projected <> m_implied || include_implied
538538
then Some (Modality.Atom (ax, m_projected))
539539
else None)
540540
implied
@@ -545,27 +545,59 @@ let untransl_mod_bounds ?(verbose = false) (bounds : Jkind.Mod_bounds.t) :
545545
Parsetree.modes =
546546
let crossing = Jkind.Mod_bounds.crossing bounds in
547547
let modality = Crossing.to_modality crossing in
548+
let least_modalities =
549+
least_modalities ~include_implied:verbose ~mut:Immutable modality
550+
in
548551
let modality_annots =
549-
least_modalities_implying Types.Immutable modality
550-
|> List.map (fun (Atom (ax, m) : Modality.atom) ->
552+
List.map
553+
(fun (Atom (ax, m) : Modality.atom) ->
551554
let s = Format.asprintf "%a" (Modality.Per_axis.print ax) m in
552555
{ Location.txt = Parsetree.Mode s; loc = Location.none })
556+
least_modalities
553557
in
554-
let nonmodal_annots =
558+
(* These mod-bounds are top ones, which are redundant to print. But we include
559+
them when printing verbosely. *)
560+
let top_modality_annots () =
561+
List.filter_map
562+
(fun ax ->
563+
let (P ax) = Modality.Axis.of_value ax in
564+
let included_in_nonverbose =
565+
List.exists
566+
(fun (Atom (ax2, _) : Modality.atom) ->
567+
Modality.Axis.P ax = Modality.Axis.P ax2)
568+
least_modalities
569+
in
570+
match included_in_nonverbose with
571+
| true -> None
572+
| false ->
573+
let s =
574+
Format.asprintf "%a"
575+
(Modality.Per_axis.print ax)
576+
(Modality.Const.proj ax modality)
577+
in
578+
Some { Location.txt = Parsetree.Mode s; loc = Location.none })
579+
Value.Axis.all
580+
in
581+
let nonmodal_annots, top_nonmodal_annots =
555582
let open Jkind.Mod_bounds in
556-
let mk_annot default print value =
557-
if (not verbose) && value = default
558-
then None
559-
else
560-
let s = Format.asprintf "%a" print value in
561-
Some { Location.txt = Parsetree.Mode s; loc = Location.none }
583+
let mk_annot top print value =
584+
let only_when_verbose = value = top in
585+
let s = Format.asprintf "%a" print value in
586+
( { Location.txt = Parsetree.Mode s; loc = Location.none },
587+
only_when_verbose )
562588
in
563589
[ mk_annot Externality.max Externality.print (externality bounds);
564590
mk_annot Nullability.max Nullability.print (nullability bounds);
565591
mk_annot Separability.max Separability.print (separability bounds) ]
566-
|> List.filter_map Fun.id
592+
|> List.partition_map (fun (annot, only_when_verbose) ->
593+
match only_when_verbose with false -> Left annot | true -> Right annot)
594+
in
595+
let verbose_annots =
596+
match verbose with
597+
| true -> top_modality_annots () @ top_nonmodal_annots
598+
| false -> []
567599
in
568-
modality_annots @ nonmodal_annots
600+
modality_annots @ nonmodal_annots @ verbose_annots
569601

570602
let sort_dedup_modalities ~warn l =
571603
let open Modality in

src/ocaml/typing/typemode.mli

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,14 @@ val transl_modalities :
3333
Parsetree.modalities ->
3434
modalities
3535

36-
val least_modalities_implying :
37-
Types.mutability -> Mode.Modality.Const.t -> Mode.Modality.atom list
36+
(** Find the minimum modality annots a user must write to express the given
37+
modality. If [include_implied] is [false], modalities implied by other
38+
written modalities are included, even if not necessary. *)
39+
val least_modalities :
40+
include_implied:bool ->
41+
mut:Types.mutability ->
42+
Mode.Modality.Const.t ->
43+
Mode.Modality.atom list
3844

3945
val sort_dedup_modalities : Mode.Modality.atom list -> Mode.Modality.atom list
4046

tests/test-dirs/kind-enclosing/verbosity.t

Lines changed: 87 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,20 @@
1414
> EOF
1515
Verbosity 0: immediate
1616
Verbosity 1: value mod global many stateless immutable external_ non_float
17-
Verbosity 2: value mod global many stateless immutable external_ non_null non_float
17+
Verbosity 2: value
18+
mod global
19+
many
20+
stateless
21+
immutable
22+
forkable
23+
unyielding
24+
aliased
25+
portable
26+
contended
27+
external_
28+
non_null
29+
non_float
30+
static
1831

1932
$ run 1:17 <<EOF
2033
> type 'a t = 'a option
@@ -27,9 +40,14 @@
2740
many
2841
stateless
2942
immutable
30-
internal
43+
portable
44+
contended
3145
non_null
3246
non_float
47+
local
48+
unique
49+
static
50+
internal
3351
with 'a
3452
3553
$ run 2:6 <<EOF
@@ -44,14 +62,78 @@
4462
many
4563
stateless
4664
immutable
47-
internal
65+
portable
66+
contended
4867
non_null
4968
non_float
69+
local
70+
unique
71+
static
72+
internal
5073
with int t1
5174

5275
$ run 1:5 <<EOF
5376
> type t : value mod portable
5477
> EOF
5578
Verbosity 0: value mod portable
56-
Verbosity 1: value mod portable internal non_null separable
57-
Verbosity 2: value mod portable internal non_null separable
79+
Verbosity 1: value
80+
mod portable
81+
non_null
82+
separable
83+
local
84+
unforkable
85+
yielding
86+
once
87+
stateful
88+
unique
89+
read_write
90+
uncontended
91+
static
92+
internal
93+
Verbosity 2: value
94+
mod portable
95+
non_null
96+
separable
97+
local
98+
unforkable
99+
yielding
100+
once
101+
stateful
102+
unique
103+
read_write
104+
uncontended
105+
static
106+
internal
107+
108+
$ run 1:5 <<EOF
109+
> type t : value mod stateless
110+
> EOF
111+
Verbosity 0: value mod stateless
112+
Verbosity 1: value
113+
mod stateless
114+
portable
115+
non_null
116+
separable
117+
local
118+
unforkable
119+
yielding
120+
once
121+
unique
122+
read_write
123+
uncontended
124+
static
125+
internal
126+
Verbosity 2: value
127+
mod stateless
128+
portable
129+
non_null
130+
separable
131+
local
132+
unforkable
133+
yielding
134+
once
135+
unique
136+
read_write
137+
uncontended
138+
static
139+
internal

0 commit comments

Comments
 (0)