Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple Stubbing with Contracts #2746

Merged
merged 29 commits into from
Sep 27, 2023
Merged
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
e6db205
Replacement basics
JustusAdam Sep 7, 2023
ff344b6
Fixing errors and formatting
JustusAdam Sep 7, 2023
16d6e50
Documentation and general fixes
JustusAdam Sep 7, 2023
6ab4834
More docs, more fixes
JustusAdam Sep 7, 2023
3347515
Mostly expanding the test case conditions
JustusAdam Sep 7, 2023
a1f12be
Stupid whitespace
JustusAdam Sep 7, 2023
2c6abd6
Turns out I did need the copies.
JustusAdam Sep 8, 2023
4f3c633
Fix test cases
JustusAdam Sep 8, 2023
a847af2
Added nicer errors + tests for missing contracts
JustusAdam Sep 10, 2023
29b128a
Incorporating suggestions
JustusAdam Sep 15, 2023
5651146
Apply suggestions from code review
JustusAdam Sep 15, 2023
cf9e4a5
Format
JustusAdam Sep 18, 2023
9429f47
Merge branch 'main' into simple-contract-replacement
JustusAdam Sep 18, 2023
a6dc282
Switch the notes
JustusAdam Sep 19, 2023
05abe4d
Added type annotation
JustusAdam Sep 19, 2023
05071f1
Sketch for module-level contracts documentation.
JustusAdam Sep 19, 2023
0777d0d
Formatting
JustusAdam Sep 19, 2023
272326a
Change code structure
JustusAdam Sep 21, 2023
686b626
Adding code review suggestions
JustusAdam Sep 21, 2023
5dc470b
Make the macro emit an `Arbitrary` bound for return types.
JustusAdam Sep 21, 2023
9ae06e4
Hehe
JustusAdam Sep 21, 2023
82121c5
Remove unused shadowing statement.
JustusAdam Sep 26, 2023
54bdfee
Expand the doc even more
JustusAdam Sep 26, 2023
415a0f5
Apply suggestions from code review
JustusAdam Sep 26, 2023
f2a93de
Formatting
JustusAdam Sep 26, 2023
d987640
Merge branch 'simple-contract-replacement' of github.com:JustusAdam/k…
JustusAdam Sep 26, 2023
85f59c7
Committed wrong file
JustusAdam Sep 26, 2023
d32e651
Update library/kani_macros/src/sysroot/contracts.rs
JustusAdam Sep 26, 2023
a9b36a3
Merge branch 'main' into simple-contract-replacement
JustusAdam Sep 26, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Turns out I did need the copies.
  • Loading branch information
JustusAdam committed Sep 8, 2023
commit 2c6abd6eccd3674988371da21d27f81ee887eb1c
21 changes: 16 additions & 5 deletions library/kani_macros/src/sysroot/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,8 @@ struct ContractConditionsHandler {
attr: Expr,
/// Body of the function this attribute was found on.
body: Block,
/// An unparsed, unmodified copy of `attr`, used in the error messages.
attr_copy: TokenStream2,
}

/// Information needed for generating check and replace handlers for different
Expand Down Expand Up @@ -391,21 +393,28 @@ impl ContractConditionsType {
impl ContractConditionsHandler {
/// Initialize the handler. Constructs the required
/// [`ContractConditionsType`] depending on `is_requires`.
fn new(is_requires: bool, mut attr: Expr, fn_sig: &Signature, fn_body: Block) -> Self {
fn new(
is_requires: bool,
mut attr: Expr,
fn_sig: &Signature,
fn_body: Block,
attr_copy: TokenStream2,
) -> Self {
let condition_type = if is_requires {
ContractConditionsType::Requires
} else {
ContractConditionsType::new_ensures(fn_sig, &mut attr)
};

Self { condition_type, attr, body: fn_body }
Self { condition_type, attr, body: fn_body, attr_copy }
}

/// Create the body of a check function.
///
/// Wraps the conditions from this attribute around `self.body`.
fn make_check_body(&self) -> TokenStream2 {
let attr = &self.attr;
let attr_copy = &self.attr_copy;
let call_to_prior = &self.body;
match &self.condition_type {
ContractConditionsType::Requires => quote!(
Expand All @@ -419,7 +428,7 @@ impl ContractConditionsHandler {
// The code that enforces the postconditions and cleans up the shallow
// argument copies (with `mem::forget`).
let exec_postconditions = quote!(
kani::assert(#attr, stringify!(#attr));
kani::assert(#attr, stringify!(#attr_copy));
#copy_clean
);

Expand Down Expand Up @@ -448,11 +457,12 @@ impl ContractConditionsHandler {
/// otherwise `self.body`.
fn make_replace_body(&self, use_dummy_fn_call: bool) -> TokenStream2 {
let attr = &self.attr;
let attr_copy = &self.attr_copy;
let call_to_prior =
if use_dummy_fn_call { quote!(kani::any()) } else { self.body.to_token_stream() };
JustusAdam marked this conversation as resolved.
Show resolved Hide resolved
match &self.condition_type {
ContractConditionsType::Requires => quote!(
kani::assert(#attr, stringify!(#attr));
kani::assert(#attr, stringify!(#attr_copy));
#call_to_prior
),
ContractConditionsType::Ensures { argument_names } => {
Expand Down Expand Up @@ -577,6 +587,7 @@ fn make_unsafe_argument_copies(
/// }
/// ```
fn requires_ensures_alt(attr: TokenStream, item: TokenStream, is_requires: bool) -> TokenStream {
JustusAdam marked this conversation as resolved.
Show resolved Hide resolved
let attr_copy = TokenStream2::from(attr.clone());
let attr = parse_macro_input!(attr as Expr);

let mut output = proc_macro2::TokenStream::new();
Expand Down Expand Up @@ -604,7 +615,7 @@ fn requires_ensures_alt(attr: TokenStream, item: TokenStream, is_requires: bool)
};

let ItemFn { attrs, vis: _, mut sig, block } = item_fn;
let handler = ContractConditionsHandler::new(is_requires, attr, &sig, *block);
let handler = ContractConditionsHandler::new(is_requires, attr, &sig, *block, attr_copy);
let emit_common_header = |output: &mut TokenStream2| {
if function_state.emit_tag_attr() {
output.extend(quote!(
Expand Down