Skip to content

Add procedural macros for function contracts. - #1552

Merged
celinval merged 11 commits into
model-checking:features/function-contractsfrom
monal:contracts-macros
Nov 15, 2022
Merged

celinval merged 11 commits into
model-checking:features/function-contractsfrom
monal:contracts-macros

Conversation

@monal

@monal monal commented Aug 18, 2022

Copy link
Copy Markdown

Description of changes:

Add macros for requires, ensures, and modifies clause for function contracts.

Resolved issues:

Resolves #1503

Call-outs:

Testing:

  • How is this change tested?
    The change is tested locally for now. Will add test cases before merging into main.

  • Is this a refactor change?
    No.

Checklist

  • Each commit message has a non-empty body, explaining why the change was made
  • Methods or procedures are documented
  • Regression or unit tests are included, or existing tests cover the modified code
  • My PR is restricted to a single feature or bugfix

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.

@monal
monal requested a review from a team as a code owner August 18, 2022 20:53
@monal monal self-assigned this Aug 18, 2022
Comment thread library/kani_macros/Cargo.toml Resolved
Comment thread library/kani_macros/src/contract.rs Resolved
// SPDX-License-Identifier: Apache-2.0 OR MIT

// Utility functions for macro expansion of function contract clauses.
use proc_macro2::TokenStream as TokenStream2;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why 2?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because TokenStream exists in both proc_macro and proc_macro2 crate. I've redefined the type to TokenStream2 when it comes from proc_macro2. Most other crates that use proc_macro2 do something similar - they refer to the types from proc_macro2 using the suffix 2.

Comment thread library/kani_macros/src/contract.rs Resolved
Comment thread library/kani_macros/src/contract.rs Outdated
.collect()
}

/// Returns the list of function arguments from the function signature.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment that you are ignoring self.
Comment explaining why

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a comment. The function is returning a list of arguments for use in a function or method call.
If we have a function fn foo(self, x: i32, y:i32) -> ..., it extracts vec![x, y] for use in the foo(x, y) method call.

Comment thread library/kani_macros/src/contract.rs Resolved
Comment thread library/kani_macros/src/contract.rs Outdated
.filter_map(|a| {
let name = a.path.segments.last().unwrap().ident.to_string();
match name.as_str() {
"ensures" | "requires" => None,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will this only match kani::ensures

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I've changed it to match exactly.

Comment thread library/kani_macros/src/contract.rs Resolved
Comment on lines 12 to +13
use proc_macro::TokenStream;
use proc_macro2::TokenStream as TokenStream2;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two different proc macro crates?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both syn and quote use the proc_macro2 crate.

proc_macro2 is a wrapper around proc_macro, except it allows you to use the defined types outside a procedural macro.

I need proc_macro2::TokenStream because quote::quote!(...) expects inputs to be of that type.

@monal
monal requested a review from danielsn August 19, 2022 17:26
Comment thread library/kani_macros/src/contract.rs Outdated
attributes
.iter()
.filter_map(|a| {
let name = a.path.segments.last().unwrap().ident.to_string();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you only look at the last part, wuoldn't that match whatever::ensures too?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing it out. I've added a helper function to deal with this now.

@celinval

Copy link
Copy Markdown
Contributor

I'm merging this for future reference since it's being kept in a feature branch.

@celinval
celinval merged commit 26064cd into model-checking:features/function-contracts Nov 15, 2022
zhassan-aws pushed a commit to zhassan-aws/kani that referenced this pull request May 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants