Skip to content

Commit 33e226b

Browse files
Add kani::stub attribute (rust-lang#1814)
1 parent 3396936 commit 33e226b

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

kani-compiler/src/codegen_cprover_gotoc/codegen/function.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,10 @@ impl<'tcx> GotocCtx<'tcx> {
375375
let mut harness = self.default_kanitool_proof();
376376
for attr in other_attributes.iter() {
377377
match attr.0.as_str() {
378+
"stub" => self
379+
.tcx
380+
.sess
381+
.span_warn(attr.1.span, "Attribute `kani::stub` is currently ignored by Kani"),
378382
"unwind" => self.handle_kanitool_unwind(attr.1, &mut harness),
379383
_ => {
380384
self.tcx.sess.span_err(

library/kani_macros/src/lib.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,30 @@ pub fn unwind(attr: TokenStream, item: TokenStream) -> TokenStream {
118118
result.extend(item);
119119
result
120120
}
121+
122+
#[cfg(not(kani))]
123+
#[proc_macro_attribute]
124+
pub fn stub(_attr: TokenStream, item: TokenStream) -> TokenStream {
125+
// When the config is not kani, we should leave the function alone
126+
item
127+
}
128+
129+
/// Specify a function/method stub pair to use for proof harness
130+
///
131+
/// The attribute `#[kani::stub(original, replacement)]` can only be used alongside `#[kani::proof]`.
132+
///
133+
/// # Arguments
134+
/// * `original` - The function or method to replace, specified as a path.
135+
/// * `replacement` - The function or method to use as a replacement, specified as a path.
136+
#[cfg(kani)]
137+
#[proc_macro_attribute]
138+
pub fn stub(attr: TokenStream, item: TokenStream) -> TokenStream {
139+
let mut result = TokenStream::new();
140+
141+
// Translate #[kani::stub(original, replacement)] to #[kanitool::stub(original, replacement)]
142+
let insert_string = "#[kanitool::stub(".to_owned() + &attr.to_string() + ")]";
143+
result.extend(insert_string.parse::<TokenStream>().unwrap());
144+
145+
result.extend(item);
146+
result
147+
}

tests/ui/stub-attribute/attribute.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright Kani Contributors
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
//
4+
//! Checks that the `kani::stub` attribute is accepted
5+
6+
fn foo() {}
7+
8+
fn bar() {}
9+
10+
#[kani::proof]
11+
#[kani::stub(foo, bar)]
12+
fn main() {}

tests/ui/stub-attribute/expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
warning: Attribute `kani::stub` is currently ignored by Kani

0 commit comments

Comments
 (0)