-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Collect specifications of foreign function declarations.
- Loading branch information
Showing
10 changed files
with
182 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
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
37 changes: 37 additions & 0 deletions
37
prusti-contracts/prusti-specs/src/extern_spec_rewriter/foreign_mods.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,37 @@ | ||
//! Process external specifications in Rust foreign modules marked with | ||
//! the #[extern_spec] attribute. | ||
use super::common::generate_extern_spec_foreign_function_stub; | ||
use crate::{ | ||
extract_prusti_attributes, generate_spec_and_assertions, specifications::untyped::AnyFnItem, | ||
ExternSpecKind, | ||
}; | ||
use proc_macro2::TokenStream; | ||
use quote::{quote, ToTokens}; | ||
|
||
pub fn rewrite_extern_spec(item_foreign_mod: &mut syn::ItemForeignMod) -> syn::Result<TokenStream> { | ||
let mut specs = vec![]; | ||
for item in item_foreign_mod.items.iter_mut() { | ||
if let syn::ForeignItem::Fn(item_fn) = item { | ||
specs.extend(rewrite_fn(item_fn)?); | ||
} | ||
// eventually: handle specs for foreign variables | ||
} | ||
|
||
let mut res = TokenStream::new(); | ||
res.extend(specs.iter().map(syn::Item::to_token_stream)); | ||
res.extend(quote!(#item_foreign_mod)); | ||
Ok(res) | ||
} | ||
|
||
fn rewrite_fn(item_fn: &mut syn::ForeignItemFn) -> Result<Vec<syn::Item>, syn::Error> { | ||
let stub_fn: syn::ForeignItemFn = | ||
generate_extern_spec_foreign_function_stub(item_fn, ExternSpecKind::Method); | ||
let mut stub_fn = AnyFnItem::ForeignFn(stub_fn); | ||
let prusti_attributes = extract_prusti_attributes(&mut stub_fn); | ||
let (spec_items, generated_attributes) = | ||
generate_spec_and_assertions(prusti_attributes, &stub_fn)?; | ||
stub_fn.attrs_mut().extend(generated_attributes); | ||
*item_fn = stub_fn.expect_foreign_item_fn(); | ||
Ok(spec_items) | ||
} |
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
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,24 @@ | ||
use prusti_contracts::*; | ||
|
||
#[extern_spec] | ||
extern "C" { | ||
#[ensures(a >= b ==> result == a)] | ||
#[ensures(b >= a ==> result == b)] | ||
fn max(a: i32, b: i32) -> i32; | ||
|
||
fn unannotated(); | ||
|
||
#[ensures(a < 0 ==> result == -a)] | ||
#[ensures(a >= 0 ==> result == a)] | ||
fn abs(a: i32) -> i32; | ||
} | ||
|
||
fn main() { | ||
assert!(unsafe { max(1, 2) } == 2); | ||
assert!(unsafe { max(2, 1) } == 2); | ||
unsafe { | ||
unannotated(); | ||
}; | ||
assert!(unsafe { abs(-1) } == 1); | ||
assert!(unsafe { abs(1) == 1 }); | ||
} |
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,16 @@ | ||
use prusti_contracts::*; | ||
|
||
#[extern_spec] | ||
extern "C" { | ||
#[ensures(a >= b ==> result = a)] | ||
#[ensures(b >= a ==> result == b)] | ||
fn max(a: i32, b: i32) -> i32; | ||
|
||
fn unannotated(); | ||
|
||
#[ensures(a < 0 ==> result == -a)] | ||
#[ensures(a >= 0 ==> result = a)] | ||
fn abs(a: i32) -> i32; | ||
} | ||
|
||
fn main() {} |
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,37 @@ | ||
error[E0308]: mismatched types | ||
--> $DIR/foreign_mods.rs:5:26 | ||
| | ||
5 | #[ensures(a >= b ==> result = a)] | ||
| ^^^^^^ expected `bool`, found `i32` | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/foreign_mods.rs:5:22 | ||
| | ||
5 | #[ensures(a >= b ==> result = a)] | ||
| ^^^^^^^^^^^^^^ expected `bool`, found `()` | ||
| | ||
help: you might have meant to compare for equality | ||
| | ||
5 | #[ensures(a >= b ==> result == a)] | ||
| + | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/foreign_mods.rs:12:26 | ||
| | ||
12 | #[ensures(a >= 0 ==> result = a)] | ||
| ^^^^^^ expected `bool`, found `i32` | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/foreign_mods.rs:12:22 | ||
| | ||
12 | #[ensures(a >= 0 ==> result = a)] | ||
| ^^^^^^^^^^^^^^ expected `bool`, found `()` | ||
| | ||
help: you might have meant to compare for equality | ||
| | ||
12 | #[ensures(a >= 0 ==> result == a)] | ||
| + | ||
|
||
error: aborting due to 4 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0308`. |