Skip to content

Commit b186cd7

Browse files
committed
improve panic message on expect
This is something that has been bothering me for years, which (naively) seems simple to fix. The message passed to `.expect(msg)` is printed in (at least potentially) confusing tway to the user: ``` let x: Option<u32> = None; x.expect("valid x value"); // thread 'main' panicked at ...: // valid x value ``` IMO, it would be much better if it got reported as: ``` // thread 'main' panicked at ...: // expected: valid x value ``` Then, no matter how the message is formulated, it should be clear that it was the *expectation*, not the failing condition itself.
1 parent 2c12b4a commit b186cd7

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

library/core/src/option.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -955,7 +955,7 @@ impl<T> Option<T> {
955955
pub const fn expect(self, msg: &str) -> T {
956956
match self {
957957
Some(val) => val,
958-
None => expect_failed(msg),
958+
None => expect_failed("expected: ", msg),
959959
}
960960
}
961961

@@ -1783,7 +1783,11 @@ impl<T> Option<T> {
17831783
where
17841784
P: FnOnce(&mut T) -> bool,
17851785
{
1786-
if self.as_mut().map_or(false, predicate) { self.take() } else { None }
1786+
if self.as_mut().map_or(false, predicate) {
1787+
self.take()
1788+
} else {
1789+
None
1790+
}
17871791
}
17881792

17891793
/// Replaces the actual value in the option by the value given in parameter,
@@ -2045,8 +2049,8 @@ const fn unwrap_failed() -> ! {
20452049
#[cfg_attr(feature = "panic_immediate_abort", inline)]
20462050
#[cold]
20472051
#[track_caller]
2048-
const fn expect_failed(msg: &str) -> ! {
2049-
panic_display(&msg)
2052+
const fn expect_failed(prefix: &str, msg: &str) -> ! {
2053+
panic_fmt(const_format_args!("{prefix}{msg}"))
20502054
}
20512055

20522056
/////////////////////////////////////////////////////////////////////////////

library/core/src/result.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,7 +1086,7 @@ impl<T, E> Result<T, E> {
10861086
{
10871087
match self {
10881088
Ok(t) => t,
1089-
Err(e) => unwrap_failed(msg, &e),
1089+
Err(e) => unwrap_failed("expected: ", msg, &e),
10901090
}
10911091
}
10921092

@@ -1134,7 +1134,7 @@ impl<T, E> Result<T, E> {
11341134
{
11351135
match self {
11361136
Ok(t) => t,
1137-
Err(e) => unwrap_failed("called `Result::unwrap()` on an `Err` value", &e),
1137+
Err(e) => unwrap_failed("", "called `Result::unwrap()` on an `Err` value", &e),
11381138
}
11391139
}
11401140

@@ -1197,7 +1197,7 @@ impl<T, E> Result<T, E> {
11971197
T: fmt::Debug,
11981198
{
11991199
match self {
1200-
Ok(t) => unwrap_failed(msg, &t),
1200+
Ok(t) => unwrap_failed("expected error: ", msg, &t),
12011201
Err(e) => e,
12021202
}
12031203
}
@@ -1228,7 +1228,7 @@ impl<T, E> Result<T, E> {
12281228
T: fmt::Debug,
12291229
{
12301230
match self {
1231-
Ok(t) => unwrap_failed("called `Result::unwrap_err()` on an `Ok` value", &t),
1231+
Ok(t) => unwrap_failed("", "called `Result::unwrap_err()` on an `Ok` value", &t),
12321232
Err(e) => e,
12331233
}
12341234
}
@@ -1728,8 +1728,8 @@ impl<T, E> Result<Result<T, E>, E> {
17281728
#[inline(never)]
17291729
#[cold]
17301730
#[track_caller]
1731-
fn unwrap_failed(msg: &str, error: &dyn fmt::Debug) -> ! {
1732-
panic!("{msg}: {error:?}")
1731+
fn unwrap_failed(prefix: &str, msg: &str, error: &dyn fmt::Debug) -> ! {
1732+
panic!("{prefix}{msg}: {error:?}")
17331733
}
17341734

17351735
// This is a separate function to avoid constructing a `dyn Debug`
@@ -1740,7 +1740,7 @@ fn unwrap_failed(msg: &str, error: &dyn fmt::Debug) -> ! {
17401740
#[inline]
17411741
#[cold]
17421742
#[track_caller]
1743-
fn unwrap_failed<T>(_msg: &str, _error: &T) -> ! {
1743+
fn unwrap_failed<T>(_prefix: &str, _msg: &str, _error: &T) -> ! {
17441744
panic!()
17451745
}
17461746

0 commit comments

Comments
 (0)