forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add simple ensures, requires, safety predicates (#15)
I also added one proof-of-concept harness to ensure Kani can verify it.
- Loading branch information
Showing
12 changed files
with
173 additions
and
1 deletion.
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
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
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 |
---|---|---|
|
@@ -26,6 +26,7 @@ Session.vim | |
*.rlib | ||
*.rmeta | ||
*.mir | ||
Cargo.lock | ||
|
||
## Temporary files | ||
*~ | ||
|
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,17 @@ | ||
# Copyright Kani Contributors | ||
# SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
||
[package] | ||
name = "safety" | ||
version = "0.1.0" | ||
edition = "2021" | ||
license = "MIT OR Apache-2.0" | ||
|
||
[lib] | ||
proc-macro = true | ||
|
||
[dependencies] | ||
proc-macro2 = "1.0" | ||
proc-macro-error = "1.0.4" | ||
quote = "1.0.20" | ||
syn = { version = "2.0.18", features = ["full"] } |
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,4 @@ | ||
fn main() { | ||
// We add the configurations here to be checked. | ||
println!("cargo:rustc-check-cfg=cfg(kani_host)"); | ||
} |
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,21 @@ | ||
use proc_macro::{TokenStream}; | ||
use quote::{quote, format_ident}; | ||
use syn::{ItemFn, parse_macro_input}; | ||
|
||
pub(crate) fn requires(attr: TokenStream, item: TokenStream) -> TokenStream { | ||
rewrite_attr(attr, item, "requires") | ||
} | ||
|
||
pub(crate) fn ensures(attr: TokenStream, item: TokenStream) -> TokenStream { | ||
rewrite_attr(attr, item, "ensures") | ||
} | ||
|
||
fn rewrite_attr(attr: TokenStream, item: TokenStream, name: &str) -> TokenStream { | ||
let args = proc_macro2::TokenStream::from(attr); | ||
let fn_item = parse_macro_input!(item as ItemFn); | ||
let attribute = format_ident!("{}", name); | ||
quote!( | ||
#[kani_core::#attribute(#args)] | ||
#fn_item | ||
).into() | ||
} |
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,25 @@ | ||
//! Implement a few placeholders for contract attributes until they get implemented upstream. | ||
//! Each tool should implement their own version in a separate module of this crate. | ||
|
||
use proc_macro::TokenStream; | ||
use proc_macro_error::proc_macro_error; | ||
|
||
#[cfg(kani_host)] | ||
#[path = "kani.rs"] | ||
mod tool; | ||
|
||
#[cfg(not(kani_host))] | ||
#[path = "runtime.rs"] | ||
mod tool; | ||
|
||
#[proc_macro_error] | ||
#[proc_macro_attribute] | ||
pub fn requires(attr: TokenStream, item: TokenStream) -> TokenStream { | ||
tool::requires(attr, item) | ||
} | ||
|
||
#[proc_macro_error] | ||
#[proc_macro_attribute] | ||
pub fn ensures(attr: TokenStream, item: TokenStream) -> TokenStream { | ||
tool::ensures(attr, item) | ||
} |
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,15 @@ | ||
use proc_macro::TokenStream; | ||
|
||
/// For now, runtime requires is a no-op. | ||
/// | ||
/// TODO: At runtime the `requires` should become an assert unsafe precondition. | ||
pub(crate) fn requires(_attr: TokenStream, item: TokenStream) -> TokenStream { | ||
item | ||
} | ||
|
||
/// For now, runtime requires is a no-op. | ||
/// | ||
/// TODO: At runtime the `ensures` should become an assert as well. | ||
pub(crate) fn ensures(_attr: TokenStream, item: TokenStream) -> TokenStream { | ||
item | ||
} |
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
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
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
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