|
492 | 492 |
|
493 | 493 | #![stable(feature = "rust1", since = "1.0.0")]
|
494 | 494 |
|
| 495 | +use safety::requires; |
495 | 496 | use crate::iter::{self, FusedIterator, TrustedLen};
|
496 | 497 | use crate::ops::{self, ControlFlow, Deref, DerefMut};
|
497 | 498 | use crate::{convert, fmt, hint};
|
498 | 499 |
|
| 500 | +#[cfg(kani)] |
| 501 | +use crate::kani; |
| 502 | + |
499 | 503 | /// `Result` is a type that represents either success ([`Ok`]) or failure ([`Err`]).
|
500 | 504 | ///
|
501 | 505 | /// See the [module documentation](self) for details.
|
@@ -1459,6 +1463,7 @@ impl<T, E> Result<T, E> {
|
1459 | 1463 | #[inline]
|
1460 | 1464 | #[track_caller]
|
1461 | 1465 | #[stable(feature = "option_result_unwrap_unchecked", since = "1.58.0")]
|
| 1466 | + #[requires(self.is_ok())] |
1462 | 1467 | pub unsafe fn unwrap_unchecked(self) -> T {
|
1463 | 1468 | debug_assert!(self.is_ok());
|
1464 | 1469 | match self {
|
@@ -1491,6 +1496,7 @@ impl<T, E> Result<T, E> {
|
1491 | 1496 | #[inline]
|
1492 | 1497 | #[track_caller]
|
1493 | 1498 | #[stable(feature = "option_result_unwrap_unchecked", since = "1.58.0")]
|
| 1499 | + #[requires(self.is_err())] |
1494 | 1500 | pub unsafe fn unwrap_err_unchecked(self) -> E {
|
1495 | 1501 | debug_assert!(self.is_err());
|
1496 | 1502 | match self {
|
@@ -1982,3 +1988,17 @@ impl<T, E, F: From<E>> ops::FromResidual<ops::Yeet<E>> for Result<T, F> {
|
1982 | 1988 | impl<T, E> ops::Residual<T> for Result<convert::Infallible, E> {
|
1983 | 1989 | type TryType = Result<T, E>;
|
1984 | 1990 | }
|
| 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