Skip to content

Commit 2129967

Browse files
authored
Undo sort changes during snapshot backtrack (#1885)
* record and undo sort changes * add test and cr * format and comment * remove test that's no longer relevant
1 parent 14f25b7 commit 2129967

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

ocaml/typing/jkind.ml

+20-6
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ module Sort = struct
3131

3232
and var = t option ref
3333

34+
(* To record changes to sorts, for use with `Types.{snapshot, backtrack}` *)
35+
type change = var * t option
36+
37+
let change_log : (change -> unit) ref = ref (fun _ -> ())
38+
39+
let log_change change = !change_log change
40+
41+
let undo_change (v, t_op) = v := t_op
42+
3443
let var_name : var -> string =
3544
let next_id = ref 1 in
3645
let named = ref [] in
@@ -58,6 +67,11 @@ module Sort = struct
5867

5968
let new_var () = Var (ref None)
6069

70+
let set : var -> t option -> unit =
71+
fun v t_op ->
72+
log_change (v, t_op);
73+
v := t_op
74+
6175
(* Post-condition: If the result is a [Var v], then [!v] is [None]. *)
6276
let rec get : t -> t = function
6377
| Const _ as t -> t
@@ -66,7 +80,7 @@ module Sort = struct
6680
| None -> t
6781
| Some s ->
6882
let result = get s in
69-
if result != s then r := Some result;
83+
if result != s then set r (Some result);
7084
(* path compression *)
7185
result)
7286

@@ -86,11 +100,11 @@ module Sort = struct
86100
| Var r -> (
87101
match !r with
88102
| None ->
89-
r := default_value;
103+
set r default_value;
90104
Value
91105
| Some s ->
92106
let result = get_default_value s in
93-
r := default result;
107+
set r (default result);
94108
(* path compression *)
95109
result)
96110

@@ -119,7 +133,7 @@ module Sort = struct
119133
match !v1 with
120134
| Some s1 -> equate_sort_const s1 c2
121135
| None ->
122-
v1 := Some (of_const c2);
136+
set v1 (Some (of_const c2));
123137
Equal_mutated_first
124138

125139
and equate_var v1 s2 =
@@ -135,7 +149,7 @@ module Sort = struct
135149
| Some s1, _ -> swap_equate_result (equate_var v2 s1)
136150
| _, Some s2 -> equate_var v1 s2
137151
| None, None ->
138-
v1 := Some (of_var v2);
152+
set v1 (Some (of_var v2));
139153
Equal_mutated_first
140154

141155
and equate_sort_const s1 c2 =
@@ -167,7 +181,7 @@ module Sort = struct
167181
match !v with
168182
(* CR layouts v5: this should probably default to void now *)
169183
| None ->
170-
v := some_value;
184+
set v some_value;
171185
false
172186
| Some s -> is_void_defaulting s)
173187
| Const Value -> false

ocaml/typing/jkind.mli

+7
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,13 @@ module Sort : sig
8282
it is set to [value] first. *)
8383
val get_default_value : t -> const
8484

85+
(** To record changes to sorts, for use with `Types.{snapshot, backtrack}` *)
86+
type change
87+
88+
val change_log : (change -> unit) ref
89+
90+
val undo_change : change -> unit
91+
8592
module Debug_printers : sig
8693
val t : Format.formatter -> t -> unit
8794

ocaml/typing/types.ml

+4-3
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,7 @@ type change =
609609
| Ccommu : [`var] commutable_gen -> change
610610
| Cuniv : type_expr option ref * type_expr option -> change
611611
| Cmodes : Mode.changes -> change
612+
| Csort : Jkind.Sort.change -> change
612613

613614
type changes =
614615
Change of change * changes ref
@@ -623,7 +624,8 @@ let log_change ch =
623624
trail := r'
624625

625626
let () =
626-
Mode.change_log := (fun changes -> log_change (Cmodes changes))
627+
Mode.change_log := (fun changes -> log_change (Cmodes changes));
628+
Jkind.Sort.change_log := (fun change -> log_change (Csort change))
627629

628630
(* constructor and accessors for [field_kind] *)
629631

@@ -873,6 +875,7 @@ let undo_change = function
873875
| Ccommu (Cvar r) -> r.commu <- Cunknown
874876
| Cuniv (r, v) -> r := v
875877
| Cmodes ms -> Mode.undo_changes ms
878+
| Csort change -> Jkind.Sort.undo_change change
876879

877880
type snapshot = changes ref * int
878881
let last_snapshot = Local_store.s_ref 0
@@ -1030,5 +1033,3 @@ let undo_compress (changes, _old) =
10301033
Transient_expr.set_desc ty desc; r := !next
10311034
| _ -> ())
10321035
log
1033-
1034-

0 commit comments

Comments
 (0)