Skip to content

Commit

Permalink
Revert "Implement Boxed annotation"
Browse files Browse the repository at this point in the history
This reverts commit b09dddf.

Implementing boxing in terms of annotations is conceptually correct, but
interacts with an existing bug in the definition of `pre_hash` (see
mirage#39) that makes this change
breaking inside Irmin.

Pending a solution to mirage#39, we
should avoid changing the behaviour of `pre_hash` in this way. This
restores compatibility with Irmin `master`.
  • Loading branch information
craigfe committed Jun 11, 2021
1 parent 46bf94e commit eb83002
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/repr/type.ml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ let array ?(len = `Int) v = Array { v; len }
let pair a b = Tuple (Pair (a, b))
let triple a b c = Tuple (Triple (a, b, c))
let option a = Option a
let boxed t = annotate ~add:Type_binary.Boxed.add ~data:() t
let boxed t = Boxed t

let abstract ~pp ~of_string ~json ~bin ?unboxed_bin ~equal ~compare ~short_hash
~pre_hash () =
Expand Down
16 changes: 6 additions & 10 deletions src/repr/type_binary.ml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ open Type_core
open Staging
open Utils

module Boxed = Attribute.Make1 (struct
type _ t = unit

let name = "boxed"
end)

module Encode = struct
let chars =
Array.init 256 (fun i -> Bytes.unsafe_to_string (Bytes.make 1 (Char.chr i)))
Expand Down Expand Up @@ -135,6 +129,7 @@ module Encode = struct
| Custom c -> c.encode_bin
| Map b -> map ~boxed:true b
| Prim t -> prim ~boxed:true t
| Boxed b -> t b
| Attributes { attr_type = x; _ } -> t x
| List l -> list (t l.v) l.len
| Array a -> array (t a.v) a.len
Expand All @@ -149,8 +144,8 @@ module Encode = struct
| Custom c -> c.unboxed_encode_bin
| Map b -> map ~boxed:false b
| Prim t -> prim ~boxed:false t
| Attributes { attr_type = x; attrs } -> (
match Boxed.find attrs with Some () -> t x | None -> unboxed x)
| Boxed b -> t b
| Attributes { attr_type = x; _ } -> unboxed x
| List l -> list (t l.v) l.len
| Array a -> array (t a.v) a.len
| Tuple t -> tuple t
Expand Down Expand Up @@ -339,6 +334,7 @@ module Decode = struct
| Custom c -> c.decode_bin
| Map b -> map ~boxed:true b
| Prim t -> prim ~boxed:true t
| Boxed b -> t b
| Attributes { attr_type = x; _ } -> t x
| List l -> list (t l.v) l.len
| Array a -> array (t a.v) a.len
Expand All @@ -353,8 +349,8 @@ module Decode = struct
| Custom c -> c.unboxed_decode_bin
| Map b -> map ~boxed:false b
| Prim t -> prim ~boxed:false t
| Attributes { attr_type = x; attrs } -> (
match Boxed.find attrs with Some () -> t x | None -> unboxed x)
| Boxed b -> t b
| Attributes { attr_type = x; _ } -> unboxed x
| List l -> list (t l.v) l.len
| Array a -> array (t a.v) a.len
| Tuple t -> tuple t
Expand Down
1 change: 0 additions & 1 deletion src/repr/type_binary.mli
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

open Type_core
open Staging
module Boxed : Attribute.S1 with type _ t = unit

val encode_bin : 'a t -> 'a encode_bin
val decode_bin : 'a t -> 'a decode_bin
Expand Down
1 change: 1 addition & 0 deletions src/repr/type_core_intf.ml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ module Types = struct
| Option : 'a t -> 'a option t
| Record : 'a record -> 'a t
| Variant : 'a variant -> 'a t
| Boxed : 'a t -> 'a t

and 'a len_v = { len : len; v : 'a t }

Expand Down
2 changes: 2 additions & 0 deletions src/repr/type_json.ml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ module Encode = struct
| Self s -> t s.self_fix
| Custom _ -> failwith "Unimplemented operation: encode_json"
| Map b -> map b
| Boxed x -> t x
| Attributes { attr_type = x; attrs } -> (
match Encode_json.find attrs with None -> t x | Some t -> t)
| Prim t -> prim t
Expand Down Expand Up @@ -301,6 +302,7 @@ module Decode = struct
| Self s -> t s.self_fix
| Map b -> map b
| Prim t -> prim t
| Boxed x -> t x
| Attributes { attr_type = x; attrs } -> (
match Decode_json.find attrs with None -> t x | Some f -> f)
| List l -> list (t l.v)
Expand Down
4 changes: 4 additions & 0 deletions src/repr/type_ordered.ml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ module Refl = struct
| Variant a, Variant b -> Witness.eq a.vwit b.vwit
| Var v, _ | _, Var v -> raise (Unbound_type_variable v)
| Attributes a, Attributes b -> t a.attr_type b.attr_type
| Boxed a, b -> t a b
| a, Boxed b -> t a b
| ( ( Map _ | Custom _ | Prim _ | Array _ | List _ | Tuple _ | Option _
| Record _ | Variant _ | Attributes _ ),
_ ) ->
Expand Down Expand Up @@ -135,6 +137,7 @@ module Equal = struct
| Custom c -> c.equal
| Map m -> map m
| Attributes { attr_type = x; _ } -> t x
| Boxed x -> t x
| Prim p -> prim p
| List l -> list (t l.v)
| Array x -> array (t x.v)
Expand Down Expand Up @@ -284,6 +287,7 @@ module Compare = struct
| Self s -> self s
| Custom c -> c.compare
| Map m -> map m
| Boxed x -> t x
| Attributes { attr_type = x; _ } -> t x
| Prim p -> (prim [@inlined]) p
| List l -> list (t l.v)
Expand Down
2 changes: 2 additions & 0 deletions src/repr/type_pp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ let dump t =
| Tuple t -> tuple t ppf x
| Record r -> record r ppf x
| Variant v -> variant v ppf x
| Boxed t -> aux t ppf x
| Attributes { attrs; attr_type = t } -> (
match Attr.find attrs with None -> aux t ppf x | Some pp -> pp ppf x)
and map : type a b. (a, b) map -> b pp = fun l ppf x -> aux l.x ppf (l.g x)
Expand Down Expand Up @@ -166,6 +167,7 @@ let ty : type a. a t Fmt.t =
Fmt.pf ppf "@[Attributes<%a> (%a)@]"
Fmt.(list ~sep:semi string)
names ty t
| Boxed b -> Fmt.pf ppf "@[Boxed (%a)@]" ty b
| Map m -> Fmt.pf ppf "@[Map (%a)@]" ty m.x
| Prim p -> Fmt.pf ppf "@[%a@]" prim p
| List l -> Fmt.pf ppf "@[%a list%a@]" ty l.v len l.len
Expand Down
4 changes: 2 additions & 2 deletions src/repr/type_random.ml
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ let rec t : type a. a t -> a random = function
| Option x -> option x
| Record x -> record x
| Variant x -> variant x
| Attributes { attr_type; attrs } -> (
match Attr.find attrs with None -> t attr_type | Some f -> stage f)
| Attributes { attr_type; _ } -> t attr_type
| Boxed x -> t x
| Self x -> stage (fun s -> (* improperly staged *) unstage (t x.self_fix) s)
| Custom _ -> failwith "Cannot generate random instance of Custom type"
| Var v -> raise (Unbound_type_variable v)
Expand Down
2 changes: 2 additions & 0 deletions src/repr/type_size.ml
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ let rec t : type a. a t -> a size_of = function
| Map b -> map ~boxed:true b
| Prim t -> prim ~boxed:true t
| Attributes { attr_type; _ } -> t attr_type
| Boxed b -> t b
| List l -> list (t l.v) l.len
| Array a -> array (t a.v) a.len
| Tuple t -> tuple t
Expand All @@ -121,6 +122,7 @@ and unboxed : type a. a t -> a size_of = function
| Map b -> map ~boxed:false b
| Prim t -> prim ~boxed:false t
| Attributes { attr_type; _ } -> t attr_type
| Boxed b -> t b
| List l -> list (t l.v) l.len
| Array a -> array (t a.v) a.len
| Tuple t -> tuple t
Expand Down

0 comments on commit eb83002

Please sign in to comment.