-
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.
Raise an error when postconditions of pure functions contain old() ex…
…pressions (#1474) * Raise an error if old() appears in postcondition of pure functions * rustfmt, commit more files * Add a test * Clippy * Remove unnecessary debug * Fix test * More tests
- Loading branch information
Showing
8 changed files
with
212 additions
and
66 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
prusti-tests/tests/verify/fail/incorrect/old_in_pure_postcondition.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,18 @@ | ||
use prusti_contracts::*; | ||
|
||
struct MyWrapper(u32); | ||
|
||
impl MyWrapper { | ||
#[pure] | ||
#[ensures(old(self.0) == self.0)] | ||
fn unwrap(&self) -> u32 { //~ ERROR old expressions should not appear in the postconditions of pure functions | ||
self.0 | ||
} | ||
} | ||
|
||
fn test(x: &MyWrapper) -> u32 { | ||
// Following error is due to stub encoding of invalid spec for function `unwrap()` | ||
x.unwrap() //~ ERROR precondition of pure function call might not hold | ||
} | ||
|
||
fn main() { } |
22 changes: 22 additions & 0 deletions
22
prusti-tests/tests/verify/fail/incorrect/old_in_pure_postcondition_extern.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,22 @@ | ||
use prusti_contracts::*; | ||
|
||
#[extern_spec] | ||
impl<T> std::option::Option<T> { | ||
#[pure] // <=== Error triggered by this | ||
#[requires(self.is_some())] | ||
#[ensures(old(self) === Some(result))] | ||
pub fn unwrap(self) -> T; //~ ERROR old expressions should not appear in the postconditions of pure functions | ||
|
||
#[pure] | ||
#[ensures(result == matches!(self, Some(_)))] | ||
pub const fn is_some(&self) -> bool; | ||
} | ||
|
||
#[pure] | ||
#[requires(x.is_some())] | ||
fn test(x: Option<i32>) -> i32 { | ||
// Following error is due to stub encoding of invalid external spec for function `unwrap()` | ||
x.unwrap() //~ ERROR precondition of pure function call might not hold | ||
} | ||
|
||
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,46 @@ | ||
use crate::encoder::{ | ||
errors::{SpannedEncodingResult, WithSpan}, | ||
snapshot::interface::SnapshotEncoderInterface, | ||
Encoder, | ||
}; | ||
|
||
use prusti_rustc_interface::{ | ||
middle::{mir, ty, ty::Binder}, | ||
span::Span, | ||
}; | ||
|
||
use vir_crate::polymorphic as vir_poly; | ||
|
||
pub(crate) trait PureFunctionFormalArgsEncoderInterface<'p, 'v: 'p, 'tcx: 'v> { | ||
fn encoder(&self) -> &'p Encoder<'v, 'tcx>; | ||
|
||
fn check_type( | ||
&self, | ||
var_span: Span, | ||
ty: Binder<'tcx, ty::Ty<'tcx>>, | ||
) -> SpannedEncodingResult<()>; | ||
|
||
fn get_span(&self, local: mir::Local) -> Span; | ||
|
||
fn encode_formal_args( | ||
&self, | ||
sig: ty::PolyFnSig<'tcx>, | ||
) -> SpannedEncodingResult<Vec<vir_poly::LocalVar>> { | ||
let mut formal_args = vec![]; | ||
for local_idx in 0..sig.skip_binder().inputs().len() { | ||
let local_ty = sig.input(local_idx); | ||
let local = mir::Local::from_usize(local_idx + 1); | ||
let var_name = format!("{local:?}"); | ||
let var_span = self.get_span(local); | ||
|
||
self.check_type(var_span, local_ty)?; | ||
|
||
let var_type = self | ||
.encoder() | ||
.encode_snapshot_type(local_ty.skip_binder()) | ||
.with_span(var_span)?; | ||
formal_args.push(vir_poly::LocalVar::new(var_name, var_type)) | ||
} | ||
Ok(formal_args) | ||
} | ||
} |
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
Oops, something went wrong.