Skip to content

Commit 37d03a9

Browse files
authored
flambda-backend: Implement mutable() logic for record and array (#2369)
* implement mutable() logic for record and array * fix chamleon * address some comments * move the coupling of mutable and global * better printing * fix array modalities * remove irrelavent test * address comments * make depend * bootstrap
1 parent f7cc47a commit 37d03a9

33 files changed

+289
-168
lines changed

.depend

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,7 @@ parsing/parse.cmi : \
592592
parsing/parser.cmo : \
593593
parsing/syntaxerr.cmi \
594594
parsing/parsetree.cmi \
595+
utils/misc.cmi \
595596
parsing/longident.cmi \
596597
parsing/location.cmi \
597598
utils/language_extension.cmi \
@@ -608,6 +609,7 @@ parsing/parser.cmo : \
608609
parsing/parser.cmx : \
609610
parsing/syntaxerr.cmx \
610611
parsing/parsetree.cmi \
612+
utils/misc.cmx \
611613
parsing/longident.cmx \
612614
parsing/location.cmx \
613615
utils/language_extension.cmx \
@@ -1354,6 +1356,7 @@ typing/primitive.cmo : \
13541356
typing/outcometree.cmi \
13551357
utils/misc.cmi \
13561358
parsing/location.cmi \
1359+
utils/language_extension.cmi \
13571360
typing/jkind.cmi \
13581361
parsing/attr_helper.cmi \
13591362
typing/primitive.cmi
@@ -1362,6 +1365,7 @@ typing/primitive.cmx : \
13621365
typing/outcometree.cmi \
13631366
utils/misc.cmx \
13641367
parsing/location.cmx \
1368+
utils/language_extension.cmx \
13651369
typing/jkind.cmx \
13661370
parsing/attr_helper.cmx \
13671371
typing/primitive.cmi
@@ -1736,6 +1740,7 @@ typing/typecore.cmo : \
17361740
typing/typedtree.cmi \
17371741
typing/typedecl.cmi \
17381742
typing/subst.cmi \
1743+
typing/solver.cmi \
17391744
typing/shape.cmi \
17401745
typing/rec_check.cmi \
17411746
typing/printtyp.cmi \
@@ -1777,6 +1782,7 @@ typing/typecore.cmx : \
17771782
typing/typedtree.cmx \
17781783
typing/typedecl.cmx \
17791784
typing/subst.cmx \
1785+
typing/solver.cmx \
17801786
typing/shape.cmx \
17811787
typing/rec_check.cmx \
17821788
typing/printtyp.cmx \
@@ -1843,6 +1849,7 @@ typing/typedecl.cmo : \
18431849
utils/misc.cmi \
18441850
parsing/longident.cmi \
18451851
parsing/location.cmi \
1852+
utils/language_extension.cmi \
18461853
typing/jkind.cmi \
18471854
parsing/jane_syntax.cmi \
18481855
typing/includecore.cmi \
@@ -1879,6 +1886,7 @@ typing/typedecl.cmx : \
18791886
utils/misc.cmx \
18801887
parsing/longident.cmx \
18811888
parsing/location.cmx \
1889+
utils/language_extension.cmx \
18821890
typing/jkind.cmx \
18831891
parsing/jane_syntax.cmx \
18841892
typing/includecore.cmx \
@@ -2167,7 +2175,6 @@ typing/typeopt.cmo : \
21672175
typing/ctype.cmi \
21682176
utils/config.cmi \
21692177
utils/clflags.cmi \
2170-
parsing/asttypes.cmi \
21712178
typing/typeopt.cmi
21722179
typing/typeopt.cmx : \
21732180
typing/types.cmx \
@@ -2186,7 +2193,6 @@ typing/typeopt.cmx : \
21862193
typing/ctype.cmx \
21872194
utils/config.cmx \
21882195
utils/clflags.cmx \
2189-
parsing/asttypes.cmi \
21902196
typing/typeopt.cmi
21912197
typing/typeopt.cmi : \
21922198
typing/types.cmi \
@@ -2332,9 +2338,11 @@ typing/uniqueness_analysis.cmx : \
23322338
typing/uniqueness_analysis.cmi : \
23332339
typing/typedtree.cmi
23342340
typing/untypeast.cmo : \
2341+
typing/types.cmi \
23352342
typing/typedtree.cmi \
23362343
typing/path.cmi \
23372344
parsing/parsetree.cmi \
2345+
typing/mode.cmi \
23382346
utils/misc.cmi \
23392347
parsing/longident.cmi \
23402348
parsing/location.cmi \
@@ -2345,9 +2353,11 @@ typing/untypeast.cmo : \
23452353
parsing/ast_helper.cmi \
23462354
typing/untypeast.cmi
23472355
typing/untypeast.cmx : \
2356+
typing/types.cmx \
23482357
typing/typedtree.cmx \
23492358
typing/path.cmx \
23502359
parsing/parsetree.cmi \
2360+
typing/mode.cmx \
23512361
utils/misc.cmx \
23522362
parsing/longident.cmx \
23532363
parsing/location.cmx \

boot/ocamlc

2.92 KB
Binary file not shown.

lambda/matching.ml

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2145,9 +2145,7 @@ let get_expr_args_record ~scopes head (arg, _mut, sort, layout) rem =
21452145
let lbl_sort = Jkind.sort_of_jkind lbl.lbl_jkind in
21462146
let lbl_layout = Typeopt.layout_of_sort lbl.lbl_loc lbl_sort in
21472147
let sem =
2148-
match lbl.lbl_mut with
2149-
| Immutable -> Reads_agree
2150-
| Mutable -> Reads_vary
2148+
if Types.is_mutable lbl.lbl_mut then Reads_vary else Reads_agree
21512149
in
21522150
let access, sort, layout =
21532151
match lbl.lbl_repres with
@@ -2170,11 +2168,7 @@ let get_expr_args_record ~scopes head (arg, _mut, sort, layout) rem =
21702168
Lprim (Pfield (lbl.lbl_pos + 1, ptr, sem), [ arg ], loc),
21712169
lbl_sort, lbl_layout
21722170
in
2173-
let str =
2174-
match lbl.lbl_mut with
2175-
| Immutable -> Alias
2176-
| Mutable -> StrictOpt
2177-
in
2171+
let str = if Types.is_mutable lbl.lbl_mut then StrictOpt else Alias in
21782172
(access, str, sort, layout) :: make_args (pos + 1)
21792173
in
21802174
make_args 0
@@ -2222,9 +2216,7 @@ let get_expr_args_array ~scopes kind head (arg, _mut, _sort, _layout) rem =
22222216
(Parrayrefu (ref_kind, Ptagged_int_index),
22232217
[ arg; Lconst (Const_base (Const_int pos)) ],
22242218
loc),
2225-
(match am with
2226-
| Mutable -> StrictOpt
2227-
| Immutable -> Alias),
2219+
(if Types.is_mutable am then StrictOpt else Alias),
22282220
arg_sort,
22292221
result_layout)
22302222
:: make_args (pos + 1)
@@ -3640,10 +3632,7 @@ let is_record_with_mutable_field p =
36403632
match p.pat_desc with
36413633
| Tpat_record (lps, _) ->
36423634
List.exists
3643-
(fun (_, lbl, _) ->
3644-
match lbl.Types.lbl_mut with
3645-
| Mutable -> true
3646-
| Immutable -> false)
3635+
(fun (_, lbl, _) -> Types.is_mutable lbl.lbl_mut)
36473636
lps
36483637
| Tpat_alias _
36493638
| Tpat_variant _

lambda/translcore.ml

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -554,9 +554,7 @@ and transl_exp0 ~in_new_scope ~scopes sort e =
554554
| Texp_field(arg, id, lbl, float) ->
555555
let targ = transl_exp ~scopes Jkind.Sort.for_record arg in
556556
let sem =
557-
match lbl.lbl_mut with
558-
| Immutable -> Reads_agree
559-
| Mutable -> Reads_vary
557+
if Types.is_mutable lbl.lbl_mut then Reads_vary else Reads_agree
560558
in
561559
let lbl_sort = Jkind.sort_of_jkind lbl.lbl_jkind in
562560
check_record_field_sort id.loc lbl_sort lbl.lbl_repres;
@@ -622,16 +620,14 @@ and transl_exp0 ~in_new_scope ~scopes sort e =
622620
in
623621
let imm_array = makearray Immutable in
624622
let lambda_arr_mut : Lambda.mutable_flag =
625-
match (amut : Asttypes.mutable_flag) with
626-
| Mutable -> Mutable
627-
| Immutable -> Immutable
623+
if Types.is_mutable amut then Mutable else Immutable
628624
in
629625
begin try
630626
(* For native code the decision as to which compilation strategy to
631627
use is made later. This enables the Flambda passes to lift certain
632628
kinds of array definitions to symbols. *)
633629
(* Deactivate constant optimization if array is small enough *)
634-
if amut = Asttypes.Mutable &&
630+
if Types.is_mutable amut &&
635631
List.length ll <= use_dup_for_constant_mutable_arrays_bigger_than
636632
then begin
637633
raise Not_constant
@@ -640,7 +636,7 @@ and transl_exp0 ~in_new_scope ~scopes sort e =
640636
if is_local_mode mode then raise Not_constant;
641637
begin match List.map extract_constant ll with
642638
| exception Not_constant
643-
when kind = Pfloatarray && amut = Asttypes.Mutable ->
639+
when kind = Pfloatarray && Types.is_mutable amut ->
644640
(* We cannot currently lift mutable [Pintarray] arrays safely in
645641
Flambda because [caml_modify] might be called upon them
646642
(e.g. from code operating on polymorphic arrays, or functions
@@ -669,9 +665,7 @@ and transl_exp0 ~in_new_scope ~scopes sort e =
669665
| Punboxedfloatarray _ | Punboxedintarray _ ->
670666
Misc.fatal_error "Use flambda2 for unboxed arrays"
671667
in
672-
match amut with
673-
| Mutable -> duparray_to_mutable const
674-
| Immutable -> const
668+
if Types.is_mutable amut then duparray_to_mutable const else const
675669
end
676670
with Not_constant ->
677671
makearray lambda_arr_mut
@@ -1663,9 +1657,7 @@ and transl_record ~scopes loc env mode fields repres opt_init_expr =
16631657
record_field_kind (layout env lbl.lbl_loc lbl_sort typ)
16641658
in
16651659
let sem =
1666-
match mut with
1667-
| Immutable -> Reads_agree
1668-
| Mutable -> Reads_vary
1660+
if Types.is_mutable mut then Reads_vary else Reads_agree
16691661
in
16701662
let access =
16711663
match repres with
@@ -1691,7 +1683,7 @@ and transl_record ~scopes loc env mode fields repres opt_init_expr =
16911683
in
16921684
let ll, shape = List.split (Array.to_list lv) in
16931685
let mut : Lambda.mutable_flag =
1694-
if Array.exists (fun (lbl, _) -> lbl.lbl_mut = Asttypes.Mutable) fields
1686+
if Array.exists (fun (lbl, _) -> Types.is_mutable lbl.lbl_mut) fields
16951687
then Mutable
16961688
else Immutable in
16971689
let lam =

ocamldoc/odoc_sig.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ module Analyser =
440440
let comment_opt = analyze_alerts comment_opt ld_attributes in
441441
{
442442
rf_name = field_name ;
443-
rf_mutable = mutable_flag = Mutable ;
443+
rf_mutable = Types.is_mutable mutable_flag;
444444
rf_type = Odoc_env.subst_type env type_expr ;
445445
rf_text = comment_opt
446446
}

testsuite/tests/typing-core-bugs/const_int_hint.ml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,12 +187,12 @@ Error: This pattern matches values of type int
187187
but a pattern was expected which matches values of type int32
188188
Hint: Did you mean `0b1000_1101l'?
189189
|}]
190-
type t1 = {f1: int32};; let _ = fun x -> x.f1 <- 1_000n;;
190+
type t1 = {mutable f1: int32};; let _ = fun x -> x.f1 <- 1_000n;;
191191
[%%expect{|
192-
type t1 = { f1 : int32; }
193-
Line 1, characters 49-55:
194-
1 | type t1 = {f1: int32};; let _ = fun x -> x.f1 <- 1_000n;;
195-
^^^^^^
192+
type t1 = { mutable f1 : int32; }
193+
Line 1, characters 57-63:
194+
1 | type t1 = {mutable f1: int32};; let _ = fun x -> x.f1 <- 1_000n;;
195+
^^^^^^
196196
Error: This expression has type nativeint
197197
but an expression was expected of type int32
198198
Hint: Did you mean `1_000l'?
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
(* TEST
2+
* expect
3+
flags = "-extension unique"
4+
*)
5+
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. *)
9+
10+
(* CR zqian: add test for mutable when mutable is decoupled from modalities. *)

typing/ctype.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4318,7 +4318,7 @@ type add_instance_variable_failure =
43184318

43194319
exception Add_instance_variable_failed of add_instance_variable_failure
43204320

4321-
let check_mutability mut mut' =
4321+
let check_mutability (mut : mutable_flag) (mut' : mutable_flag) =
43224322
match mut, mut' with
43234323
| Mutable, Mutable -> ()
43244324
| Immutable, Immutable -> ()
@@ -5295,10 +5295,10 @@ let match_class_sig_shape ~strict sign1 sign2 =
52955295
in
52965296
let errors =
52975297
Vars.fold
5298-
(fun lab (mut, vr, _) err ->
5298+
(fun lab ((mut:Asttypes.mutable_flag), vr, _) err ->
52995299
match Vars.find lab sign1.csig_vars with
53005300
| exception Not_found -> CM_Missing_value lab::err
5301-
| (mut', vr', _) ->
5301+
| ((mut':Asttypes.mutable_flag), vr', _) ->
53025302
match mut', mut with
53035303
| Immutable, Mutable -> CM_Non_mutable_value lab::err
53045304
| _, _ ->

typing/env.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,13 @@ let label_usage_complaint priv mut lu
123123
| Asttypes.Private, _ ->
124124
if lu.lu_projection then None
125125
else Some Unused
126-
| Asttypes.Public, Asttypes.Immutable -> begin
126+
| Asttypes.Public, Types.Immutable -> begin
127127
match lu.lu_projection, lu.lu_construct with
128128
| true, _ -> None
129129
| false, false -> Some Unused
130130
| false, true -> Some Not_read
131131
end
132-
| Asttypes.Public, Asttypes.Mutable -> begin
132+
| Asttypes.Public, Types.Mutable _ -> begin
133133
match lu.lu_projection, lu.lu_mutation, lu.lu_construct with
134134
| true, true, _ -> None
135135
| false, false, false -> Some Unused

typing/includecore.ml

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -543,11 +543,22 @@ module Record_diffing = struct
543543
let compare_labels env params1 params2
544544
(ld1 : Types.label_declaration)
545545
(ld2 : Types.label_declaration) =
546-
if ld1.ld_mutable <> ld2.ld_mutable
547-
then
548-
let ord = if ld1.ld_mutable = Asttypes.Mutable then First else Second in
549-
Some (Mutability ord)
550-
else begin
546+
let mut =
547+
match ld1.ld_mutable, ld2.ld_mutable with
548+
| Immutable, Immutable -> None
549+
| Mutable _, Immutable -> Some First
550+
| Immutable, Mutable _ -> Some Second
551+
| Mutable m1, Mutable m2 ->
552+
let open Mode.Alloc.Comonadic.Const in
553+
(if not (eq m1 legacy) then
554+
Misc.fatal_errorf "Unexpected mutable(%a)" print m1);
555+
(if not (eq m2 legacy) then
556+
Misc.fatal_errorf "Unexpected mutable(%a)" print m2);
557+
None
558+
in
559+
begin match mut with
560+
| Some mut -> Some (Mutability mut)
561+
| None ->
551562
match compare_global_flags ld1.ld_global ld2.ld_global with
552563
| None ->
553564
let tl1 = params1 @ [ld1.ld_type] in

typing/mode.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1412,6 +1412,8 @@ end
14121412
module Comonadic_with_locality = struct
14131413
module Const = struct
14141414
include C.Comonadic_with_locality
1415+
1416+
let eq a b = le a b && le b a
14151417
end
14161418

14171419
module Obj = struct

typing/mode_intf.mli

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,11 +331,18 @@ module type S = sig
331331
end
332332

333333
module Comonadic : sig
334+
module Const : sig
335+
include Lattice
336+
337+
val eq : t -> t -> bool
338+
end
339+
334340
include
335341
Common
336342
with type error =
337343
[ `Locality of Locality.error
338344
| `Linearity of Linearity.error ]
345+
and module Const := Const
339346

340347
val meet_with : Const.t -> ('l * 'r) t -> ('l * disallowed) t
341348
end
@@ -354,6 +361,8 @@ module type S = sig
354361

355362
val split : t -> (Monadic.Const.t, Comonadic.Const.t) monadic_comonadic
356363

364+
val merge : (Monadic.Const.t, Comonadic.Const.t) monadic_comonadic -> t
365+
357366
module Option : sig
358367
type some = t
359368

typing/oprint.ml

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,10 @@ let is_initially_labeled_tuple ty =
441441
| Otyp_tuple ((Some _, _) :: _) -> true
442442
| _ -> false
443443

444+
let string_of_gbl_space = function
445+
| Ogf_global -> "global_ "
446+
| Ogf_unrestricted -> ""
447+
444448
let rec print_out_type_0 mode ppf =
445449
function
446450
| Otyp_alias {non_gen; aliased; alias } ->
@@ -626,15 +630,19 @@ and print_typargs ppf =
626630
pp_print_char ppf ')';
627631
pp_close_box ppf ();
628632
pp_print_space ppf ()
629-
and print_out_label ppf (name, mut_or_gbl, arg) =
633+
and print_out_label ppf (name, mut, arg, gbl) =
630634
(* See the notes [NON-LEGACY MODES] *)
631-
let flag =
632-
match mut_or_gbl with
633-
| Ogom_mutable -> "mutable "
634-
| Ogom_global -> "global_ "
635-
| Ogom_immutable -> ""
635+
let mut =
636+
match mut with
637+
| Om_immutable -> ""
638+
| Om_mutable None -> "mutable "
639+
| Om_mutable (Some s) -> "mutable(" ^ s ^ ") "
636640
in
637-
fprintf ppf "@[<2>%s%s :@ %a@];" flag name print_out_type arg
641+
fprintf ppf "@[<2>%s%s%s :@ %a@];"
642+
mut
643+
(string_of_gbl_space gbl)
644+
name
645+
print_out_type arg
638646

639647
let out_label = ref print_out_label
640648

0 commit comments

Comments
 (0)