Skip to content

Commit

Permalink
OnceCell
Browse files Browse the repository at this point in the history
  • Loading branch information
Matias Scharager committed Jul 17, 2024
1 parent 76a9c54 commit 7ca72aa
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 2 deletions.
8 changes: 7 additions & 1 deletion tests/expected/function-contract/interior-mutability/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0 OR MIT
// kani-flags: -Zfunction-contracts

use std::cell::{Cell, UnsafeCell};
use std::cell::{Cell, OnceCell, UnsafeCell};
use std::mem::transmute;

trait ToHack<T: ?Sized> {
Expand All @@ -21,6 +21,12 @@ impl<T: ?Sized> ToHack<T> for Cell<T> {
}
}

impl<T> ToHack<Option<T>> for OnceCell<T> {
unsafe fn hack(&self) -> &Option<T> {
transmute(self)
}
}

// ---------------------------------------------------

struct InteriorMutability {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
assertion\
- Status: SUCCESS\
- Description: "|_| unsafe{ x.x.hack() }.is_some()"\
in function modify

VERIFICATION:- SUCCESSFUL
47 changes: 47 additions & 0 deletions tests/expected/function-contract/interior-mutability/oncecell.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright Kani Contributors
// SPDX-License-Identifier: Apache-2.0 OR MIT
// kani-flags: -Zfunction-contracts

use std::cell::{Cell, OnceCell, UnsafeCell};
use std::mem::transmute;

trait ToHack<T: ?Sized> {
unsafe fn hack(&self) -> &T;
}

impl<T: ?Sized> ToHack<T> for UnsafeCell<T> {
unsafe fn hack(&self) -> &T {
transmute(self)
}
}

impl<T: ?Sized> ToHack<T> for Cell<T> {
unsafe fn hack(&self) -> &T {
transmute(self)
}
}

impl<T> ToHack<Option<T>> for OnceCell<T> {
unsafe fn hack(&self) -> &Option<T> {
transmute(self)
}
}

// ---------------------------------------------------

struct InteriorMutability {
x: OnceCell<u32>,
}

#[kani::requires(unsafe{x.x.hack()}.is_none())]
#[kani::modifies(x.x.hack())]
#[kani::ensures(|_| unsafe{x.x.hack()}.is_some())]
fn modify(x: &InteriorMutability) {
x.x.set(5).expect("")
}

#[kani::proof_for_contract(modify)]
fn main() {
let x: InteriorMutability = InteriorMutability { x: OnceCell::new() };
modify(&x)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0 OR MIT
// kani-flags: -Zfunction-contracts

use std::cell::{Cell, UnsafeCell};
use std::cell::{Cell, OnceCell, UnsafeCell};
use std::mem::transmute;

trait ToHack<T: ?Sized> {
Expand All @@ -21,6 +21,12 @@ impl<T: ?Sized> ToHack<T> for Cell<T> {
}
}

impl<T> ToHack<Option<T>> for OnceCell<T> {
unsafe fn hack(&self) -> &Option<T> {
transmute(self)
}
}

// ---------------------------------------------------

struct InteriorMutability {
Expand Down

0 comments on commit 7ca72aa

Please sign in to comment.