Skip to content

Commit 8cc9caf

Browse files
committed
Add accessors for type nodes
The type checker reads and writes the fields of `type_expr` directly in about 1,000 places, which ties it to the boxed mutable record. Upstream OCaml (4.14 and later) made `type_expr` abstract behind `get_desc`, `get_level`, `get_id` and `Transient_expr`; this adds the same layer while the record stays visible, so modules can be converted one at a time and `type_expr` made abstract once none reads the fields. `get_desc` returns the node's own descriptor and does not call `repr`, so callers keep calling `repr` first and path compression stays where it is. `get_desc`, `get_level` and the raw setters are field primitives, which compile to the same loads and stores as field access, also in dune's dev profile, which builds with `-opaque`. Review: Does this change the representation or any output? No. The record and its field order are unchanged; only the way code reaches the fields is new. Signed-off-by: Jono Prest <jjprest@gmail.com>
1 parent c1927e6 commit 8cc9caf

2 files changed

Lines changed: 72 additions & 2 deletions

File tree

‎compiler/ml/types.ml‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,32 @@ and field_mutability =
6868
| Mutability_value of Asttypes.mutable_flag
6969
| Mutability_link of field_mutability ref
7070

71+
(* The primitives below index the fields of [type_expr] by position: [desc] is
72+
field 0 and [level] field 1. Reordering the record would make them read the
73+
wrong field without a type error, so the check below fails as soon as the
74+
compiler starts instead. *)
75+
external get_desc : type_expr -> type_desc = "%field0"
76+
external get_level : type_expr -> int = "%field1"
77+
let get_id t = t.id
78+
79+
let () =
80+
let t = {desc = Tnil; level = 12345; id = 0} in
81+
if not (get_desc t == Tnil && get_level t = 12345) then
82+
failwith "Types: get_desc and get_level don't match the type_expr fields"
83+
84+
module Transient_expr = struct
85+
let create desc ~level ~id = {desc; level; id}
86+
87+
external set_desc : type_expr -> type_desc -> unit = "%setfield0"
88+
89+
(* OCaml 5.0 has no [%setfield1]. *)
90+
let set_level t level = t.level <- level
91+
end
92+
7193
module Type_ops = struct
7294
type t = type_expr
73-
let compare t1 t2 = t1.id - t2.id
74-
let hash t = t.id
95+
let compare t1 t2 = get_id t1 - get_id t2
96+
let hash t = get_id t
7597
let equal t1 t2 = t1 == t2
7698
end
7799

‎compiler/ml/types.mli‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,54 @@ and field_mutability =
179179
| Mutability_value of Asttypes.mutable_flag
180180
| Mutability_link of field_mutability ref
181181

182+
(** {1 Access to type nodes}
183+
184+
Code reads the fields of a [type_expr] through these accessors and changes
185+
them through the logged setters in [Btype] ([link_type], [set_level],
186+
[log_type]). This follows upstream OCaml (4.14 and later), where
187+
[type_expr] is abstract, and it separates the type checker from the
188+
representation of type nodes, so that the representation can change
189+
later, for example to an index into a per-request store.
190+
191+
The record above stays visible for now, so modules which still read its
192+
fields directly keep compiling while they are converted. Making
193+
[type_expr] abstract is the end state of this refactoring.
194+
195+
Unlike upstream's [get_desc], [get_desc] does not call [Btype.repr]: it
196+
returns the node's own descriptor, which can be [Tlink]. Callers keep
197+
calling [repr] first, so path compression and its trail entries happen
198+
exactly where they did before.
199+
200+
[get_desc] and [get_level] are primitives, so they compile to the same
201+
field loads as [ty.desc] and [ty.level], also in builds without
202+
cross-module inlining. *)
203+
204+
external get_desc : type_expr -> type_desc = "%field0"
205+
(** The node's descriptor, without following [Tlink]. *)
206+
207+
external get_level : type_expr -> int = "%field1"
208+
209+
val get_id : type_expr -> int
210+
(** The node's unique identifier, allocated by [Btype.newty2]. *)
211+
212+
(** Creation and unlogged updates of type nodes. The setters change a field
213+
without recording its old value on the backtracking trail. Their users are
214+
[Btype] (node creation, the trail itself, copy sessions and marking), and
215+
the parts of [Ctype] which either restore their own temporary changes
216+
(copy marks, level marks, temporary links during unification) or call
217+
[Btype.log_type] first, which records the old descriptor before a
218+
persistent change. Other code uses the logged setters of [Btype].
219+
220+
Upstream OCaml's setters take a separate [transient_expr], obtained with
221+
[Transient_expr.repr], so that they bypass the [repr] built into its
222+
[get_desc]. There is no implicit [repr] here, so they take a [type_expr]. *)
223+
module Transient_expr : sig
224+
val create : type_desc -> level:int -> id:int -> type_expr
225+
226+
external set_desc : type_expr -> type_desc -> unit = "%setfield0"
227+
val set_level : type_expr -> int -> unit
228+
end
229+
182230
module Type_ops : sig
183231
type t = type_expr
184232
val compare : t -> t -> int

0 commit comments

Comments
 (0)