Skip to content

Commit ba2d0d7

Browse files
authored
flambda-backend: Refactor modality logic (#2344)
1 parent 42d708b commit ba2d0d7

33 files changed

+627
-269
lines changed

.depend

+4-2
Original file line numberDiff line numberDiff line change
@@ -1002,6 +1002,7 @@ typing/includecore.cmi : \
10021002
typing/types.cmi \
10031003
typing/typedtree.cmi \
10041004
typing/path.cmi \
1005+
typing/mode.cmi \
10051006
parsing/location.cmi \
10061007
typing/jkind.cmi \
10071008
typing/ident.cmi \
@@ -1486,6 +1487,7 @@ typing/printtyp.cmi : \
14861487
typing/shape.cmi \
14871488
typing/path.cmi \
14881489
typing/outcometree.cmi \
1490+
typing/mode.cmi \
14891491
parsing/longident.cmi \
14901492
parsing/location.cmi \
14911493
typing/ident.cmi \
@@ -1943,6 +1945,7 @@ typing/typedecl.cmi : \
19431945
typing/shape.cmi \
19441946
typing/path.cmi \
19451947
parsing/parsetree.cmi \
1948+
typing/mode.cmi \
19461949
parsing/longident.cmi \
19471950
parsing/location.cmi \
19481951
utils/language_extension.cmi \
@@ -2185,21 +2188,20 @@ typing/typemod.cmi : \
21852188
utils/compilation_unit.cmi \
21862189
file_formats/cmi_format.cmi
21872190
typing/typemode.cmo : \
2188-
utils/warnings.cmi \
21892191
typing/mode.cmi \
21902192
parsing/location.cmi \
21912193
parsing/jane_syntax_parsing.cmi \
21922194
parsing/jane_syntax.cmi \
21932195
typing/typemode.cmi
21942196
typing/typemode.cmx : \
2195-
utils/warnings.cmx \
21962197
typing/mode.cmx \
21972198
parsing/location.cmx \
21982199
parsing/jane_syntax_parsing.cmx \
21992200
parsing/jane_syntax.cmx \
22002201
typing/typemode.cmi
22012202
typing/typemode.cmi : \
22022203
typing/mode.cmi \
2204+
parsing/location.cmi \
22032205
parsing/jane_syntax.cmi
22042206
typing/typeopt.cmo : \
22052207
typing/types.cmi \

ocamldoc/odoc_sig.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ module Analyser =
492492
let record comments
493493
{ Typedtree.ld_id; ld_mutable; ld_type; ld_loc; ld_attributes } =
494494
get_field env comments @@
495-
{Types.ld_id; ld_mutable; ld_global = Unrestricted;
495+
{Types.ld_id; ld_mutable; ld_modalities = Mode.Modality.Value.id;
496496
ld_jkind=Jkind.any ~why:Dummy_jkind (* ignored *);
497497
ld_type=ld_type.Typedtree.ctyp_type;
498498
ld_loc; ld_attributes; ld_uid=Types.Uid.internal_not_actually_unique} in

otherlibs/dynlink/dune

-5
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@
9191
solver
9292
mode_intf
9393
mode
94-
typemode
9594
jkind_intf
9695
jkind_types
9796
primitive
@@ -199,7 +198,6 @@
199198
(copy_files ../../typing/solver.ml)
200199
(copy_files ../../typing/shape_reduce.ml)
201200
(copy_files ../../typing/mode.ml)
202-
(copy_files ../../typing/typemode.ml)
203201
(copy_files ../../typing/types.ml)
204202
(copy_files ../../typing/btype.ml)
205203
(copy_files ../../typing/subst.ml)
@@ -269,7 +267,6 @@
269267
(copy_files ../../typing/solver.mli)
270268
(copy_files ../../typing/shape_reduce.mli)
271269
(copy_files ../../typing/mode.mli)
272-
(copy_files ../../typing/typemode.mli)
273270
(copy_files ../../typing/types.mli)
274271
(copy_files ../../typing/btype.mli)
275272
(copy_files ../../typing/subst.mli)
@@ -380,7 +377,6 @@
380377
.dynlink_compilerlibs.objs/byte/dynlink_compilerlibs__Ast_mapper.cmo
381378
.dynlink_compilerlibs.objs/byte/dynlink_compilerlibs__Solver.cmo
382379
.dynlink_compilerlibs.objs/byte/dynlink_compilerlibs__Mode.cmo
383-
.dynlink_compilerlibs.objs/byte/dynlink_compilerlibs__Typemode.cmo
384380
.dynlink_compilerlibs.objs/byte/dynlink_compilerlibs__Jkind_intf.cmo
385381
.dynlink_compilerlibs.objs/byte/dynlink_compilerlibs__Jkind_types.cmo
386382
.dynlink_compilerlibs.objs/byte/dynlink_compilerlibs__Types.cmo
@@ -464,7 +460,6 @@
464460
.dynlink_compilerlibs.objs/native/dynlink_compilerlibs__Ast_mapper.cmx
465461
.dynlink_compilerlibs.objs/native/dynlink_compilerlibs__Solver.cmx
466462
.dynlink_compilerlibs.objs/native/dynlink_compilerlibs__Mode.cmx
467-
.dynlink_compilerlibs.objs/native/dynlink_compilerlibs__Typemode.cmx
468463
.dynlink_compilerlibs.objs/native/dynlink_compilerlibs__Jkind_intf.cmx
469464
.dynlink_compilerlibs.objs/native/dynlink_compilerlibs__Jkind_types.cmx
470465
.dynlink_compilerlibs.objs/native/dynlink_compilerlibs__Types.cmx

testsuite/tests/typing-local/local.ml

+6-4
Original file line numberDiff line numberDiff line change
@@ -1672,7 +1672,7 @@ Error: Signature mismatch:
16721672
foo : string;
16731673
is not the same as:
16741674
global_ foo : string;
1675-
The second is global and the first is not.
1675+
The second is global_ and the first is not.
16761676
|}]
16771677
16781678
module M : sig
@@ -1698,7 +1698,7 @@ Error: Signature mismatch:
16981698
global_ foo : string;
16991699
is not the same as:
17001700
foo : string;
1701-
The first is global and the second is not.
1701+
The first is global_ and the second is not.
17021702
|}]
17031703
17041704
(* Special handling of tuples in matches and let bindings *)
@@ -2523,7 +2523,8 @@ Error: Signature mismatch:
25232523
Bar of int * string
25242524
is not the same as:
25252525
Bar of int * global_ string
2526-
Locality mismatch at argument position 2 : The second is global and the first is not.
2526+
Modality mismatch at argument position 2:
2527+
The second is global_ and the first is not.
25272528
|}]
25282529
25292530
@@ -2550,7 +2551,8 @@ Error: Signature mismatch:
25502551
Bar of int * global_ string
25512552
is not the same as:
25522553
Bar of int * string
2553-
Locality mismatch at argument position 2 : The first is global and the second is not.
2554+
Modality mismatch at argument position 2:
2555+
The first is global_ and the second is not.
25542556
|}]
25552557
25562558
(* global_ binds closer than star *)

testsuite/tests/typing-modes/modes.ml

+40-10
Original file line numberDiff line numberDiff line change
@@ -231,12 +231,8 @@ Error: Unrecognized modality foo.
231231
|}]
232232

233233
type t = Foo of global_ string @@ global
234+
(* CR reduced-modality: this should warn. *)
234235
[%%expect{|
235-
Line 1, characters 34-40:
236-
1 | type t = Foo of global_ string @@ global
237-
^^^^^^
238-
Warning 250 [redundant-modality]: This global modality is redundant.
239-
240236
type t = Foo of global_ string
241237
|}]
242238

@@ -260,15 +256,49 @@ Error: Unrecognized modality foo.
260256
type r = {
261257
global_ x : string @@ global
262258
}
259+
(* CR reduced-modality: this should warn. *)
263260
[%%expect{|
264-
Line 2, characters 24-30:
265-
2 | global_ x : string @@ global
266-
^^^^^^
267-
Warning 250 [redundant-modality]: This global modality is redundant.
268-
269261
type r = { global_ x : string; }
270262
|}]
271263

264+
(* Modalities don't imply each other; this will change as we add borrowing. *)
265+
type r = {
266+
global_ x : string @@ shared
267+
}
268+
[%%expect{|
269+
type r = { global_ x : string @@ shared; }
270+
|}]
271+
272+
type r = {
273+
x : string @@ shared global many
274+
}
275+
[%%expect{|
276+
type r = { global_ x : string @@ many shared; }
277+
|}]
278+
279+
type r = {
280+
x : string @@ shared global many shared
281+
}
282+
(* CR reduced-modality: this should warn. *)
283+
[%%expect{|
284+
type r = { global_ x : string @@ many shared; }
285+
|}]
286+
287+
type r = Foo of string @@ global shared many
288+
[%%expect{|
289+
type r = Foo of global_ string @@ many shared
290+
|}]
291+
292+
(* mutable implies global shared many. No warnings are given since we imagine
293+
that the coupling will be removed soon. *)
294+
type r = {
295+
mutable x : string @@ global shared many
296+
}
297+
[%%expect{|
298+
type r = { mutable x : string; }
299+
|}]
300+
301+
272302
(* patterns *)
273303

274304
let foo ?(local_ x @ unique once = 42) () = ()

testsuite/tests/typing-modes/mutable.ml

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
expect;
44
*)
55

6-
(* Since [mutable] implies [global] modality, which in turns implies [shared]
7-
and [many] modalities, the effect of mutable in isolation is not testable
8-
yet. *)
6+
(* This file tests the typing around mutable() logic. *)
7+
8+
(* For legacy compatibility, [mutable] implies [global] [shared] and [many].
9+
Therefore, the effect of mutable in isolation is not testable yet. *)
910

1011
(* CR zqian: add test for mutable when mutable is decoupled from modalities. *)
1112

testsuite/tests/typing-unique/unique.ml

+4-6
Original file line numberDiff line numberDiff line change
@@ -241,12 +241,11 @@ Error: This value is shared but expected to be unique.
241241
|}]
242242

243243

244-
(* global modality entails shared modality;
245-
this is crucial once we introduce borrowing whose scope is controlled
246-
by locality *)
247-
type 'a glob = { global_ glob: 'a } [@@unboxed]
244+
(* CR zqian: [global] should imply [shared]/[many], once we introduce borrowing whose
245+
scope is controlled by locality *)
246+
type 'a glob = { glob: 'a @@ shared many } [@@unboxed]
248247
[%%expect{|
249-
type 'a glob = { global_ glob : 'a; } [@@unboxed]
248+
type 'a glob = { glob : 'a @@ many shared; } [@@unboxed]
250249
|}]
251250
let dup (glob : 'a) : 'a glob * 'a glob = unique_ ({glob}, {glob})
252251
[%%expect{|
@@ -655,4 +654,3 @@ Line 2, characters 11-19:
655654
^^^^^^^^
656655
657656
|}]
658-

testsuite/tests/typing-unique/unique_analysis.ml

+12-12
Original file line numberDiff line numberDiff line change
@@ -612,9 +612,9 @@ val foo : unit -> unit = <fun>
612612
|}]
613613

614614
(* Testing modalities in records *)
615-
type r_global = {x : string; global_ y : string}
615+
type r_shared = {x : string; y : string @@ shared many}
616616
[%%expect{|
617-
type r_global = { x : string; global_ y : string; }
617+
type r_shared = { x : string; y : string @@ many shared; }
618618
|}]
619619

620620
let foo () =
@@ -697,14 +697,14 @@ Line 3, characters 19-20:
697697
|}]
698698

699699
(* testing modalities in constructors *)
700-
type r_global = R_global of string * global_ string
700+
type r_shared = R_shared of string * string @@ shared many
701701
[%%expect{|
702-
type r_global = R_global of string * global_ string
702+
type r_shared = R_shared of string * string @@ many shared
703703
|}]
704704

705705
let foo () =
706-
let r = R_global ("hello", "world") in
707-
let R_global (_, y) = r in
706+
let r = R_shared ("hello", "world") in
707+
let R_shared (_, y) = r in
708708
ignore (shared_id y);
709709
(* the following is allowed, because using r uniquely implies using r.x
710710
shared *)
@@ -715,17 +715,17 @@ val foo : unit -> unit = <fun>
715715

716716
(* Similarly for linearity *)
717717
let foo () =
718-
let r = once_ (R_global ("hello", "world")) in
719-
let R_global (_, y) = r in
718+
let r = once_ (R_shared ("hello", "world")) in
719+
let R_shared (_, y) = r in
720720
ignore_once y;
721721
ignore_once r;
722722
[%%expect{|
723723
val foo : unit -> unit = <fun>
724724
|}]
725725

726726
let foo () =
727-
let r = once_ (R_global ("hello", "world")) in
728-
let R_global (x, _) = r in
727+
let r = once_ (R_shared ("hello", "world")) in
728+
let R_shared (x, _) = r in
729729
ignore_once x;
730730
ignore_once r;
731731
[%%expect{|
@@ -741,8 +741,8 @@ Line 4, characters 14-15:
741741
|}]
742742

743743
let foo () =
744-
let r = R_global ("hello", "world") in
745-
let R_global (x, _) = r in
744+
let r = R_shared ("hello", "world") in
745+
let R_shared (x, _) = r in
746746
ignore (shared_id x);
747747
(* doesn't work for normal fields *)
748748
ignore (unique_id r)

typing/ctype.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3144,7 +3144,7 @@ and mcomp_record_description type_pairs env =
31443144
mcomp type_pairs env l1.ld_type l2.ld_type;
31453145
if Ident.name l1.ld_id = Ident.name l2.ld_id &&
31463146
l1.ld_mutable = l2.ld_mutable &&
3147-
l1.ld_global = l2.ld_global
3147+
l1.ld_modalities = l2.ld_modalities
31483148
then iter xs ys
31493149
else raise Incompatible
31503150
| [], [] -> ()

typing/ctype.mli

+1-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ type existential_treatment =
186186

187187
val instance_constructor: existential_treatment ->
188188
constructor_description ->
189-
(type_expr * Global_flag.t) list * type_expr * type_expr list
189+
(type_expr * Modality.Value.t) list * type_expr * type_expr list
190190
(* Same, for a constructor. Also returns existentials. *)
191191
val instance_parameterized_type:
192192
?keep_names:bool ->

typing/datarepr.ml

+3-3
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ let constructor_args ~current_unit priv cd_args cd_res path rep =
9595
}
9696
in
9797
existentials,
98-
[ newgenconstr path type_params, Global_flag.Unrestricted ],
98+
[ newgenconstr path type_params, Modality.Value.id ],
9999
Some tdecl
100100

101101
let constructor_descrs ~current_unit ty_path decl cstrs rep =
@@ -201,7 +201,7 @@ let none =
201201

202202
let dummy_label =
203203
{ lbl_name = ""; lbl_res = none; lbl_arg = none;
204-
lbl_mut = Immutable; lbl_global = Unrestricted;
204+
lbl_mut = Immutable; lbl_modalities = Modality.Value.id;
205205
lbl_jkind = Jkind.any ~why:Dummy_jkind;
206206
lbl_num = -1; lbl_pos = -1; lbl_all = [||];
207207
lbl_repres = Record_unboxed;
@@ -222,7 +222,7 @@ let label_descrs ty_res lbls repres priv =
222222
lbl_res = ty_res;
223223
lbl_arg = l.ld_type;
224224
lbl_mut = l.ld_mutable;
225-
lbl_global = l.ld_global;
225+
lbl_modalities = l.ld_modalities;
226226
lbl_jkind = l.ld_jkind;
227227
lbl_pos = if is_void then lbl_pos_void else pos;
228228
lbl_num = num;

0 commit comments

Comments
 (0)