summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--compiler/ExtractBuiltin.ml8
-rw-r--r--compiler/PureMicroPasses.ml12
-rw-r--r--tests/lean/NoNestedBorrows.lean134
3 files changed, 79 insertions, 75 deletions
diff --git a/compiler/ExtractBuiltin.ml b/compiler/ExtractBuiltin.ml
index 2e46b120..9cc7c226 100644
--- a/compiler/ExtractBuiltin.ml
+++ b/compiler/ExtractBuiltin.ml
@@ -29,6 +29,7 @@ module SimpleNameOrd = struct
end
module SimpleNameMap = Collections.MakeMap (SimpleNameOrd)
+module SimpleNameSet = Collections.MakeSet (SimpleNameOrd)
(** Small utility to memoize some computations *)
let mk_memoized (f : unit -> 'a) : unit -> 'a =
@@ -374,6 +375,13 @@ let mk_builtin_funs_map () =
let builtin_funs_map = mk_memoized mk_builtin_funs_map
+let builtin_non_fallible_funs =
+ [ "alloc::boxed::Box::deref"; "alloc::boxed::Box::deref_mut" ]
+
+let builtin_non_fallible_funs_set =
+ SimpleNameSet.of_list
+ (List.map string_to_simple_name builtin_non_fallible_funs)
+
type builtin_trait_decl_info = {
rust_name : string;
extract_name : string;
diff --git a/compiler/PureMicroPasses.ml b/compiler/PureMicroPasses.ml
index a326d19e..f3e6cbe2 100644
--- a/compiler/PureMicroPasses.ml
+++ b/compiler/PureMicroPasses.ml
@@ -1544,20 +1544,22 @@ let eliminate_box_functions (ctx : trans_ctx) (def : fun_decl) : fun_decl =
| Fun (FromLlbc (FunId (Regular fid), _lp_id, rg_id)) -> (
(* Lookup the function name *)
let def = FunDeclId.Map.find fid ctx.fun_ctx.fun_decls in
- match (Names.name_to_string def.name, rg_id) with
- | "alloc::box::Boxed::deref", None ->
+ match
+ (Names.name_no_disambiguators_to_string def.name, rg_id)
+ with
+ | "alloc::boxed::Box::deref", None ->
(* [Box::deref] forward is the identity *)
let arg, args = Collections.List.pop args in
mk_apps arg args
- | "alloc::box::Boxed::deref", Some _ ->
+ | "alloc::boxed::Box::deref", Some _ ->
(* [Box::deref] backward is [()] (doesn't give back anything) *)
assert (args = []);
mk_unit_rvalue
- | "alloc::box::Boxed::deref_mut", None ->
+ | "alloc::boxed::Box::deref_mut", None ->
(* [Box::deref_mut] forward is the identity *)
let arg, args = Collections.List.pop args in
mk_apps arg args
- | "alloc::box::Boxed::deref_mut", Some _ ->
+ | "alloc::boxed::Box::deref_mut", Some _ ->
(* [Box::deref_mut] back is almost the identity:
* let box_deref_mut (x_init : t) (x_back : t) : t = x_back
* *)
diff --git a/tests/lean/NoNestedBorrows.lean b/tests/lean/NoNestedBorrows.lean
index 884e62c4..a90d6ea2 100644
--- a/tests/lean/NoNestedBorrows.lean
+++ b/tests/lean/NoNestedBorrows.lean
@@ -54,7 +54,7 @@ def div_test (x : U32) (y : U32) : Result U32 :=
/- [no_nested_borrows::div_test1]: forward function -/
def div_test1 (x : U32) : Result U32 :=
- x / (U32.ofInt 2)
+ x / 2#u32
/- [no_nested_borrows::rem_test]: forward function -/
def rem_test (x : U32) (y : U32) : Result U32 :=
@@ -67,7 +67,7 @@ def cast_test (x : U32) : Result I32 :=
/- [no_nested_borrows::test2]: forward function -/
def test2 : Result Unit :=
do
- let _ ← (U32.ofInt 23) + (U32.ofInt 44)
+ let _ ← 23#u32 + 44#u32
Result.ret ()
/- Unit test for [no_nested_borrows::test2] -/
@@ -82,10 +82,10 @@ def get_max (x : U32) (y : U32) : Result U32 :=
/- [no_nested_borrows::test3]: forward function -/
def test3 : Result Unit :=
do
- let x ← get_max (U32.ofInt 4) (U32.ofInt 3)
- let y ← get_max (U32.ofInt 10) (U32.ofInt 11)
+ let x ← get_max 4#u32 3#u32
+ let y ← get_max 10#u32 11#u32
let z ← x + y
- if not (z = (U32.ofInt 15))
+ if not (z = 15#u32)
then Result.fail Error.panic
else Result.ret ()
@@ -95,8 +95,8 @@ def test3 : Result Unit :=
/- [no_nested_borrows::test_neg1]: forward function -/
def test_neg1 : Result Unit :=
do
- let y ← - (I32.ofInt 3)
- if not (y = (I32.ofInt (-(3:Int))))
+ let y ← - 3#i32
+ if not (y = (-(3:Int))#i32)
then Result.fail Error.panic
else Result.ret ()
@@ -105,7 +105,7 @@ def test_neg1 : Result Unit :=
/- [no_nested_borrows::refs_test1]: forward function -/
def refs_test1 : Result Unit :=
- if not ((I32.ofInt 1) = (I32.ofInt 1))
+ if not (1#i32 = 1#i32)
then Result.fail Error.panic
else Result.ret ()
@@ -114,16 +114,16 @@ def refs_test1 : Result Unit :=
/- [no_nested_borrows::refs_test2]: forward function -/
def refs_test2 : Result Unit :=
- if not ((I32.ofInt 2) = (I32.ofInt 2))
+ if not (2#i32 = 2#i32)
then Result.fail Error.panic
else
- if not ((I32.ofInt 0) = (I32.ofInt 0))
+ if not (0#i32 = 0#i32)
then Result.fail Error.panic
else
- if not ((I32.ofInt 2) = (I32.ofInt 2))
+ if not (2#i32 = 2#i32)
then Result.fail Error.panic
else
- if not ((I32.ofInt 2) = (I32.ofInt 2))
+ if not (2#i32 = 2#i32)
then Result.fail Error.panic
else Result.ret ()
@@ -167,8 +167,8 @@ def test_panic (b : Bool) : Result Unit :=
/- [no_nested_borrows::test_copy_int]: forward function -/
def test_copy_int : Result Unit :=
do
- let y ← copy_int (I32.ofInt 0)
- if not ((I32.ofInt 0) = y)
+ let y ← copy_int 0#i32
+ if not (0#i32 = y)
then Result.fail Error.panic
else Result.ret ()
@@ -185,7 +185,7 @@ def is_cons (T : Type) (l : List T) : Result Bool :=
def test_is_cons : Result Unit :=
do
let l := List.Nil
- let b ← is_cons I32 (List.Cons (I32.ofInt 0) l)
+ let b ← is_cons I32 (List.Cons 0#i32 l)
if not b
then Result.fail Error.panic
else Result.ret ()
@@ -203,9 +203,9 @@ def split_list (T : Type) (l : List T) : Result (T × (List T)) :=
def test_split_list : Result Unit :=
do
let l := List.Nil
- let p ← split_list I32 (List.Cons (I32.ofInt 0) l)
+ let p ← split_list I32 (List.Cons 0#i32 l)
let (hd, _) := p
- if not (hd = (I32.ofInt 0))
+ if not (hd = 0#i32)
then Result.fail Error.panic
else Result.ret ()
@@ -228,19 +228,18 @@ def choose_back
/- [no_nested_borrows::choose_test]: forward function -/
def choose_test : Result Unit :=
do
- let z ← choose I32 true (I32.ofInt 0) (I32.ofInt 0)
- let z0 ← z + (I32.ofInt 1)
- if not (z0 = (I32.ofInt 1))
+ let z ← choose I32 true 0#i32 0#i32
+ let z0 ← z + 1#i32
+ if not (z0 = 1#i32)
then Result.fail Error.panic
else
do
- let (x, y) ← choose_back I32 true (I32.ofInt 0) (I32.ofInt 0) z0
- if not (x = (I32.ofInt 1))
+ let (x, y) ← choose_back I32 true 0#i32 0#i32 z0
+ if not (x = 1#i32)
then Result.fail Error.panic
- else
- if not (y = (I32.ofInt 0))
- then Result.fail Error.panic
- else Result.ret ()
+ else if not (y = 0#i32)
+ then Result.fail Error.panic
+ else Result.ret ()
/- Unit test for [no_nested_borrows::choose_test] -/
#assert (choose_test == .ret ())
@@ -268,17 +267,17 @@ divergent def list_length (T : Type) (l : List T) : Result U32 :=
match l with
| List.Cons t l1 => do
let i ← list_length T l1
- (U32.ofInt 1) + i
- | List.Nil => Result.ret (U32.ofInt 0)
+ 1#u32 + i
+ | List.Nil => Result.ret 0#u32
/- [no_nested_borrows::list_nth_shared]: forward function -/
divergent def list_nth_shared (T : Type) (l : List T) (i : U32) : Result T :=
match l with
| List.Cons x tl =>
- if i = (U32.ofInt 0)
+ if i = 0#u32
then Result.ret x
else do
- let i0 ← i - (U32.ofInt 1)
+ let i0 ← i - 1#u32
list_nth_shared T tl i0
| List.Nil => Result.fail Error.panic
@@ -286,10 +285,10 @@ divergent def list_nth_shared (T : Type) (l : List T) (i : U32) : Result T :=
divergent def list_nth_mut (T : Type) (l : List T) (i : U32) : Result T :=
match l with
| List.Cons x tl =>
- if i = (U32.ofInt 0)
+ if i = 0#u32
then Result.ret x
else do
- let i0 ← i - (U32.ofInt 1)
+ let i0 ← i - 1#u32
list_nth_mut T tl i0
| List.Nil => Result.fail Error.panic
@@ -298,11 +297,11 @@ divergent def list_nth_mut_back
(T : Type) (l : List T) (i : U32) (ret0 : T) : Result (List T) :=
match l with
| List.Cons x tl =>
- if i = (U32.ofInt 0)
+ if i = 0#u32
then Result.ret (List.Cons ret0 tl)
else
do
- let i0 ← i - (U32.ofInt 1)
+ let i0 ← i - 1#u32
let tl0 ← list_nth_mut_back T tl i0 ret0
Result.ret (List.Cons x tl0)
| List.Nil => Result.fail Error.panic
@@ -324,47 +323,42 @@ def list_rev (T : Type) (l : List T) : Result (List T) :=
def test_list_functions : Result Unit :=
do
let l := List.Nil
- let l0 := List.Cons (I32.ofInt 2) l
- let l1 := List.Cons (I32.ofInt 1) l0
- let i ← list_length I32 (List.Cons (I32.ofInt 0) l1)
- if not (i = (U32.ofInt 3))
+ let l0 := List.Cons 2#i32 l
+ let l1 := List.Cons 1#i32 l0
+ let i ← list_length I32 (List.Cons 0#i32 l1)
+ if not (i = 3#u32)
then Result.fail Error.panic
else
do
- let i0 ←
- list_nth_shared I32 (List.Cons (I32.ofInt 0) l1) (U32.ofInt 0)
- if not (i0 = (I32.ofInt 0))
+ let i0 ← list_nth_shared I32 (List.Cons 0#i32 l1) 0#u32
+ if not (i0 = 0#i32)
then Result.fail Error.panic
else
do
- let i1 ←
- list_nth_shared I32 (List.Cons (I32.ofInt 0) l1) (U32.ofInt 1)
- if not (i1 = (I32.ofInt 1))
+ let i1 ← list_nth_shared I32 (List.Cons 0#i32 l1) 1#u32
+ if not (i1 = 1#i32)
then Result.fail Error.panic
else
do
- let i2 ←
- list_nth_shared I32 (List.Cons (I32.ofInt 0) l1)
- (U32.ofInt 2)
- if not (i2 = (I32.ofInt 2))
+ let i2 ← list_nth_shared I32 (List.Cons 0#i32 l1) 2#u32
+ if not (i2 = 2#i32)
then Result.fail Error.panic
else
do
let ls ←
- list_nth_mut_back I32 (List.Cons (I32.ofInt 0) l1)
- (U32.ofInt 1) (I32.ofInt 3)
- let i3 ← list_nth_shared I32 ls (U32.ofInt 0)
- if not (i3 = (I32.ofInt 0))
+ list_nth_mut_back I32 (List.Cons 0#i32 l1) 1#u32 3#i32
+ let i3 ← list_nth_shared I32 ls 0#u32
+ if not (i3 = 0#i32)
then Result.fail Error.panic
else
do
- let i4 ← list_nth_shared I32 ls (U32.ofInt 1)
- if not (i4 = (I32.ofInt 3))
+ let i4 ← list_nth_shared I32 ls 1#u32
+ if not (i4 = 3#i32)
then Result.fail Error.panic
else
do
- let i5 ← list_nth_shared I32 ls (U32.ofInt 2)
- if not (i5 = (I32.ofInt 2))
+ let i5 ← list_nth_shared I32 ls 2#u32
+ if not (i5 = 2#i32)
then Result.fail Error.panic
else Result.ret ()
@@ -427,15 +421,15 @@ structure StructWithTuple (T1 T2 : Type) where
/- [no_nested_borrows::new_tuple1]: forward function -/
def new_tuple1 : Result (StructWithTuple U32 U32) :=
- Result.ret { p := ((U32.ofInt 1), (U32.ofInt 2)) }
+ Result.ret { p := (1#u32, 2#u32) }
/- [no_nested_borrows::new_tuple2]: forward function -/
def new_tuple2 : Result (StructWithTuple I16 I16) :=
- Result.ret { p := ((I16.ofInt 1), (I16.ofInt 2)) }
+ Result.ret { p := (1#i16, 2#i16) }
/- [no_nested_borrows::new_tuple3]: forward function -/
def new_tuple3 : Result (StructWithTuple U64 I64) :=
- Result.ret { p := ((U64.ofInt 1), (I64.ofInt 2)) }
+ Result.ret { p := (1#u64, 2#i64) }
/- [no_nested_borrows::StructWithPair] -/
structure StructWithPair (T1 T2 : Type) where
@@ -443,31 +437,31 @@ structure StructWithPair (T1 T2 : Type) where
/- [no_nested_borrows::new_pair1]: forward function -/
def new_pair1 : Result (StructWithPair U32 U32) :=
- Result.ret { p := { x := (U32.ofInt 1), y := (U32.ofInt 2) } }
+ Result.ret { p := { x := 1#u32, y := 2#u32 } }
/- [no_nested_borrows::test_constants]: forward function -/
def test_constants : Result Unit :=
do
let swt ← new_tuple1
let (i, _) := swt.p
- if not (i = (U32.ofInt 1))
+ if not (i = 1#u32)
then Result.fail Error.panic
else
do
let swt0 ← new_tuple2
let (i0, _) := swt0.p
- if not (i0 = (I16.ofInt 1))
+ if not (i0 = 1#i16)
then Result.fail Error.panic
else
do
let swt1 ← new_tuple3
let (i1, _) := swt1.p
- if not (i1 = (U64.ofInt 1))
+ if not (i1 = 1#u64)
then Result.fail Error.panic
else
do
let swp ← new_pair1
- if not (swp.p.x = (U32.ofInt 1))
+ if not (swp.p.x = 1#u32)
then Result.fail Error.panic
else Result.ret ()
@@ -492,21 +486,21 @@ def test_mem_replace (px : U32) : Result U32 :=
/- [no_nested_borrows::test_shared_borrow_bool1]: forward function -/
def test_shared_borrow_bool1 (b : Bool) : Result U32 :=
if b
- then Result.ret (U32.ofInt 0)
- else Result.ret (U32.ofInt 1)
+ then Result.ret 0#u32
+ else Result.ret 1#u32
/- [no_nested_borrows::test_shared_borrow_bool2]: forward function -/
def test_shared_borrow_bool2 : Result U32 :=
- Result.ret (U32.ofInt 0)
+ Result.ret 0#u32
/- [no_nested_borrows::test_shared_borrow_enum1]: forward function -/
def test_shared_borrow_enum1 (l : List U32) : Result U32 :=
match l with
- | List.Cons i l0 => Result.ret (U32.ofInt 1)
- | List.Nil => Result.ret (U32.ofInt 0)
+ | List.Cons i l0 => Result.ret 1#u32
+ | List.Nil => Result.ret 0#u32
/- [no_nested_borrows::test_shared_borrow_enum2]: forward function -/
def test_shared_borrow_enum2 : Result U32 :=
- Result.ret (U32.ofInt 0)
+ Result.ret 0#u32
end no_nested_borrows