-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Matias Scharager
committed
Jul 17, 2024
1 parent
76a9c54
commit 7ca72aa
Showing
4 changed files
with
67 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
tests/expected/function-contract/interior-mutability/oncecell.expected
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
47
tests/expected/function-contract/interior-mutability/oncecell.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters