Skip to content

Commit e351be9

Browse files
committed
Add first proof
1 parent 5d8ee62 commit e351be9

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

library/core/src/result.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,10 +492,14 @@
492492
493493
#![stable(feature = "rust1", since = "1.0.0")]
494494

495+
use safety::requires;
495496
use crate::iter::{self, FusedIterator, TrustedLen};
496497
use crate::ops::{self, ControlFlow, Deref, DerefMut};
497498
use crate::{convert, fmt, hint};
498499

500+
#[cfg(kani)]
501+
use crate::kani;
502+
499503
/// `Result` is a type that represents either success ([`Ok`]) or failure ([`Err`]).
500504
///
501505
/// See the [module documentation](self) for details.
@@ -1459,6 +1463,7 @@ impl<T, E> Result<T, E> {
14591463
#[inline]
14601464
#[track_caller]
14611465
#[stable(feature = "option_result_unwrap_unchecked", since = "1.58.0")]
1466+
#[requires(self.is_ok())]
14621467
pub unsafe fn unwrap_unchecked(self) -> T {
14631468
debug_assert!(self.is_ok());
14641469
match self {
@@ -1491,6 +1496,7 @@ impl<T, E> Result<T, E> {
14911496
#[inline]
14921497
#[track_caller]
14931498
#[stable(feature = "option_result_unwrap_unchecked", since = "1.58.0")]
1499+
#[requires(self.is_err())]
14941500
pub unsafe fn unwrap_err_unchecked(self) -> E {
14951501
debug_assert!(self.is_err());
14961502
match self {
@@ -1982,3 +1988,17 @@ impl<T, E, F: From<E>> ops::FromResidual<ops::Yeet<E>> for Result<T, F> {
19821988
impl<T, E> ops::Residual<T> for Result<convert::Infallible, E> {
19831989
type TryType = Result<T, E>;
19841990
}
1991+
1992+
#[cfg(kani)]
1993+
#[unstable(feature="kani", issue="none")]
1994+
mod verify {
1995+
use super::*;
1996+
1997+
#[kani::proof_for_contract(Result::unwrap_unchecked)]
1998+
pub fn check_unwrap_unchecked() {
1999+
let val: Result<u32, u64> = kani::any();
2000+
let ok_variant: Result<u32, u64> = Ok(0);
2001+
let copy = unsafe { ok_variant.unwrap_unchecked() };
2002+
assert_eq!(val, Result::Ok(copy));
2003+
}
2004+
}

0 commit comments

Comments
 (0)