Skip to content

Commit

Permalink
Merge pull request #399 from Niols/get-rid-of-option
Browse files Browse the repository at this point in the history
Get rid of the compatibility `Option` module
  • Loading branch information
msprotz authored Dec 11, 2023
2 parents ed6d972 + dab250e commit e649495
Show file tree
Hide file tree
Showing 13 changed files with 27 additions and 47 deletions.
10 changes: 5 additions & 5 deletions lib/AstToCFlat.ml
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ let align array_size pos =

(* Helper *)
let fields_of_struct =
List.map (fun (name, (t, _mut)) -> Option.must name, t)
List.map (fun (name, (t, _mut)) -> Option.get name, t)

(* A TQualified can either be a struct or an enum. Same for TAnonymous. *)
let is_enum env t =
Expand Down Expand Up @@ -537,9 +537,9 @@ let write_static (env: env) (lid: lident) (e: expr): string * CFlat.expr list =
write_le dst ofs e.typ (Z.of_int (LidMap.find lid env.enums));
[]
| EFlat fields ->
let layout = Option.must (layout_of env e) in
let layout = Option.get (layout_of env e) in
KList.map_flatten (fun (fname, e) ->
let fname = Option.must fname in
let fname = Option.get fname in
let fofs = fst (List.assoc fname layout.fields) in
write_scalar dst (ofs + fofs) e
) fields
Expand Down Expand Up @@ -607,7 +607,7 @@ let rec write_at (env: env)
(* KPrint.bprintf " >>> write_at: literal\n"; *)
let locals, writes =
fold (fun locals (fname, e) ->
let fname = Option.must fname in
let fname = Option.get fname in
let fofs = fst (List.assoc fname layout.fields) in
(* Recursively write each field of the struct at its offset. *)
write_at locals (ofs + fofs, e)
Expand Down Expand Up @@ -800,7 +800,7 @@ and mk_expr (env: env) (locals: locals) (e: expr): locals * CF.expr =

| EAddrOf ({ node = EField (e1, f); _ }) ->
(* This is the "address-of" operation from the paper. *)
let ofs = fst (List.assoc f (Option.must (layout_of env e1)).fields) in
let ofs = fst (List.assoc f (Option.get (layout_of env e1)).fields) in
let locals, e1 = mk_expr env locals (with_type (TBuf (e1.typ, true)) (EAddrOf e1)) in
locals, mk_add32 e1 (mk_uint32 ofs)

Expand Down
4 changes: 2 additions & 2 deletions lib/AstToCStar.ml
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ and mk_expr env in_stmt e =
CStar.Cast (mk_expr env e, mk_type env t)
| EAbort (t, s) ->
let t = match t with Some t -> t | None -> e.typ in
CStar.EAbort (mk_type env t, Option.or_empty s)
CStar.EAbort (mk_type env t, Option.value ~default:"" s)
| EUnit ->
CStar.Cast (zero, CStar.Pointer CStar.Void)
| EAny ->
Expand Down Expand Up @@ -627,7 +627,7 @@ and mk_stmts env e ret_type =
fatal_error "[AstToCStar.collect EMatch]: not implemented"

| EAbort (_, s) ->
env, CStar.Abort (Option.or_empty s) :: acc
env, CStar.Abort (Option.value ~default:"" s) :: acc

| ESwitch (e, branches) ->
let default, branches =
Expand Down
2 changes: 1 addition & 1 deletion lib/AstToMiniRust.ml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ module H = struct
| _ -> None

let wrapping_operator o =
Option.must (wrapping_operator_opt o)
Option.get (wrapping_operator_opt o)

let is_wrapping_operator o =
wrapping_operator_opt o <> None
Expand Down
2 changes: 1 addition & 1 deletion lib/Bundles.ml
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ let direct_dependencies file_of file =
let prepend lid =
match file_of lid with
| Some f when f <> fst file ->
let dep = (Option.must !current_decl, fst file, lid, f) in
let dep = (Option.get !current_decl, fst file, lid, f) in
Hashtbl.replace deps f dep
| _ ->
()
Expand Down
4 changes: 2 additions & 2 deletions lib/CFlatToWasm.ml
Original file line number Diff line number Diff line change
Expand Up @@ -625,10 +625,10 @@ let rec mk_callop2 env (w, o) e1 e2 =
mk_expr env e2 @
if is_binop (w, o) then
mk_binop_conversion (w, o) @
[ dummy_phrase (W.Ast.Binary (mk_value size (Option.must (mk_binop (w, o))))) ] @
[ dummy_phrase (W.Ast.Binary (mk_value size (Option.get (mk_binop (w, o))))) ] @
mk_mask w
else if is_cmpop (w, o) then
[ dummy_phrase (W.Ast.Compare (mk_value size (Option.must (mk_cmpop (w, o))))) ] @
[ dummy_phrase (W.Ast.Compare (mk_value size (Option.get (mk_cmpop (w, o))))) ] @
mk_mask w
else
failwith "todo mk_callop2"
Expand Down
4 changes: 2 additions & 2 deletions lib/CStarToC11.ml
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ and mk_alloc_cast m t e =
and mk_malloc m t s =
match t with
| Qualified lid when Helpers.is_aligned_type lid ->
let sz = Option.must (mk_alignment m t) in
let sz = Option.get (mk_alignment m t) in
mk_alloc_cast m t (C.Call (C.Name "KRML_ALIGNED_MALLOC", [ sz; mk_sizeof_mul m t s ]))
| _ ->
mk_alloc_cast m t (C.Call (C.Name "KRML_HOST_MALLOC", [ mk_sizeof_mul m t s ]))
Expand Down Expand Up @@ -979,7 +979,7 @@ and mk_expr m (e: expr): C.expr =
| Struct (typ, fields) ->
if typ = None then
failwith ("Expected a type annotation for: \n" ^ show_expr e);
let typ = Option.must typ in
let typ = Option.get typ in
mk_compound_literal m typ fields

| Field (BufRead (e, Constant (_, "0")), field) ->
Expand Down
6 changes: 3 additions & 3 deletions lib/Checker.ml
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ and check_fields_opt env fieldexprs fieldtyps =
if List.length fieldexprs > List.length fieldtyps then
checker_error env "some fields are superfluous (expr) in %a" pexpr (with_unit (EFlat fieldexprs));
List.iter (fun (field, expr) ->
let field = Option.must field in
let field = Option.get field in
let t, _ = KList.assoc_opt field fieldtyps in
check env t expr
) fieldexprs
Expand Down Expand Up @@ -794,7 +794,7 @@ and infer_and_check_eq: 'a. env -> ('a -> typ) -> 'a list -> typ =
| Some t_base -> check_eqtype env t_base t
| None -> t_base := Some t
) l;
Option.must !t_base
Option.get !t_base

and find_field env t field =
match expand_abbrev env t with
Expand Down Expand Up @@ -863,7 +863,7 @@ and check_valid_path env e =
match e.node with
| EField (e, f) ->
let t1 = check_valid_path env e in
fst (Option.must (find_field env t1 f))
fst (Option.get (find_field env t1 f))

| EBufRead _
| EBound _ ->
Expand Down
2 changes: 1 addition & 1 deletion lib/Diagnostics.ml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ let types_depth files =
| Abbrev t ->
depth_of_type p t
| Flat fields ->
incr (KList.max (List.map (fun (f, (t, _)) -> depth_of_type (Option.must f :: p) t) fields))
incr (KList.max (List.map (fun (f, (t, _)) -> depth_of_type (Option.get f :: p) t) fields))
| Forward _
| Variant _ ->
failwith "impossible"
Expand Down
2 changes: 1 addition & 1 deletion lib/Monomorphization.ml
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ let equalities files =
(* Either a conjunction of equalities, or a disjunction of inequalities. *)
let def () =
let sub_equalities = List.map (fun (f, (t_field, _)) ->
let f = Option.must f in
let f = Option.get f in
(* __eq__ x.f y.f *)
mk_rec_equality t_field
(EField (with_type t (EBound 0), f))
Expand Down
2 changes: 1 addition & 1 deletion lib/PrintAst.ml
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ and print_fields_t fields =

and print_fields_opt_t fields =
separate_map (semi ^^ break1) (fun (ident, (typ, mut)) ->
let ident = if ident = None then empty else string (Option.must ident) in
let ident = if ident = None then empty else string (Option.get ident) in
let mut = if mut then string "mutable " else empty in
group (group (mut ^^ ident ^^ colon) ^/^ print_typ typ)
) fields
Expand Down
10 changes: 5 additions & 5 deletions lib/Simplify.ml
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ let functional_updates = object (self)
) fields in
if List.length the_field = 1 then
let the_field, the_expr = List.hd the_field in
let the_field = Option.must the_field in
let the_field = Option.get the_field in
let the_expr = self#visit_expr env (snd (open_binder b the_expr)) in
make_mut <- (assert_tlid e1.typ, the_field) :: make_mut;
k (EAssign (with_type the_expr.typ (EField (e1, the_field)), the_expr))
Expand Down Expand Up @@ -654,7 +654,7 @@ let misc_cosmetic = object (self)
(* if true then Warn.fatal_error "MATCHED: %a" pexpr (with_unit (ELet (b, e1, e2))); *)

let f, { typ = x_typ; _ } = List.find (fun (_, x) -> x.node = EBound 1) fields in
let f = Option.must f in
let f = Option.get f in

let e3 = snd (DeBruijn.open_binder b e3) in
let e4 = snd (DeBruijn.open_binder b (snd (DeBruijn.open_binder b' e4))) in
Expand All @@ -670,7 +670,7 @@ let misc_cosmetic = object (self)
with_type (TBuf (x_typ, false)) (EAddrOf (
with_type x_typ (EField (e4, f))))])) ::
List.filter_map (fun (f', e) ->
let f' = Option.must f' in
let f' = Option.get f' in
if f = f' then
None
else
Expand Down Expand Up @@ -700,7 +700,7 @@ let misc_cosmetic = object (self)
(* if true then Warn.fatal_error "MATCHED: %a" pexpr (with_unit (ELet (b, e1, e2))); *)

let f, { typ = x_typ; _ } = List.find (fun (_, x) -> x.node = EBound 1) fields in
let f = Option.must f in
let f = Option.get f in

let e3 = snd (DeBruijn.open_binder b e3) in
let e4 = snd (DeBruijn.open_binder b (snd (DeBruijn.open_binder b' e4))) in
Expand All @@ -718,7 +718,7 @@ let misc_cosmetic = object (self)
with_type (TBuf (x_typ, false)) (EAddrOf (
with_type x_typ (EField (e4, f))))])) ::
List.filter_map (fun (f', e) ->
let f' = Option.must f' in
let f' = Option.get f' in
if f = f' then
None
else
Expand Down
6 changes: 3 additions & 3 deletions lib/Structs.ml
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,9 @@ let pass_by_ref (should_rewrite: _ -> policy) = object (self)
if ret_is_struct then
let assign_into_ret e =
if ret_is_array then
with_type TUnit (EAssign (Option.must ret_atom, e))
with_type TUnit (EAssign (Option.get ret_atom, e))
else
with_type TUnit (EBufWrite (Option.must ret_atom, Helpers.zerou32, e))
with_type TUnit (EBufWrite (Option.get ret_atom, Helpers.zerou32, e))
in
(* Step 4.1: early-returns `return e` become `dst := e; return` *)
let body = (object
Expand Down Expand Up @@ -747,7 +747,7 @@ let remove_literals = object (self)
match e.node with
| EFlat fields ->
List.fold_left (fun acc (f, e) ->
let f = Option.must f in
let f = Option.get f in
self#explode acc (f :: path) e dst
) acc fields
| _ ->
Expand Down
20 changes: 0 additions & 20 deletions lib/lib/Option.ml

This file was deleted.

0 comments on commit e649495

Please sign in to comment.