Skip to content

Commit 1c9b803

Browse files
authored
Rollup merge of rust-lang#67887 - anp:tracked-std-panics, r=nagisa
`Option::{expect,unwrap}` and `Result::{expect, expect_err, unwrap, unwrap_err}` have `#[track_caller]` The annotated functions now produce panic messages pointing to the location where they were called, rather than `core`'s internals.
2 parents 03fe834 + 3acd346 commit 1c9b803

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

src/libcore/option.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ impl<T> Option<T> {
341341
/// x.expect("the world is ending"); // panics with `the world is ending`
342342
/// ```
343343
#[inline]
344+
#[track_caller]
344345
#[stable(feature = "rust1", since = "1.0.0")]
345346
pub fn expect(self, msg: &str) -> T {
346347
match self {
@@ -374,6 +375,7 @@ impl<T> Option<T> {
374375
/// assert_eq!(x.unwrap(), "air"); // fails
375376
/// ```
376377
#[inline]
378+
#[track_caller]
377379
#[stable(feature = "rust1", since = "1.0.0")]
378380
pub fn unwrap(self) -> T {
379381
match self {
@@ -1015,6 +1017,7 @@ impl<T: fmt::Debug> Option<T> {
10151017
/// }
10161018
/// ```
10171019
#[inline]
1020+
#[track_caller]
10181021
#[unstable(feature = "option_expect_none", reason = "newly added", issue = "62633")]
10191022
pub fn expect_none(self, msg: &str) {
10201023
if let Some(val) = self {
@@ -1057,6 +1060,7 @@ impl<T: fmt::Debug> Option<T> {
10571060
/// }
10581061
/// ```
10591062
#[inline]
1063+
#[track_caller]
10601064
#[unstable(feature = "option_unwrap_none", reason = "newly added", issue = "62633")]
10611065
pub fn unwrap_none(self) {
10621066
if let Some(val) = self {
@@ -1184,13 +1188,15 @@ impl<T, E> Option<Result<T, E>> {
11841188
// This is a separate function to reduce the code size of .expect() itself.
11851189
#[inline(never)]
11861190
#[cold]
1191+
#[track_caller]
11871192
fn expect_failed(msg: &str) -> ! {
11881193
panic!("{}", msg)
11891194
}
11901195

11911196
// This is a separate function to reduce the code size of .expect_none() itself.
11921197
#[inline(never)]
11931198
#[cold]
1199+
#[track_caller]
11941200
fn expect_none_failed(msg: &str, value: &dyn fmt::Debug) -> ! {
11951201
panic!("{}: {:?}", msg, value)
11961202
}

src/libcore/result.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,7 @@ impl<T, E: fmt::Debug> Result<T, E> {
957957
/// x.unwrap(); // panics with `emergency failure`
958958
/// ```
959959
#[inline]
960+
#[track_caller]
960961
#[stable(feature = "rust1", since = "1.0.0")]
961962
pub fn unwrap(self) -> T {
962963
match self {
@@ -984,6 +985,7 @@ impl<T, E: fmt::Debug> Result<T, E> {
984985
/// x.expect("Testing expect"); // panics with `Testing expect: emergency failure`
985986
/// ```
986987
#[inline]
988+
#[track_caller]
987989
#[stable(feature = "result_expect", since = "1.4.0")]
988990
pub fn expect(self, msg: &str) -> T {
989991
match self {
@@ -1017,6 +1019,7 @@ impl<T: fmt::Debug, E> Result<T, E> {
10171019
/// assert_eq!(x.unwrap_err(), "emergency failure");
10181020
/// ```
10191021
#[inline]
1022+
#[track_caller]
10201023
#[stable(feature = "rust1", since = "1.0.0")]
10211024
pub fn unwrap_err(self) -> E {
10221025
match self {
@@ -1044,6 +1047,7 @@ impl<T: fmt::Debug, E> Result<T, E> {
10441047
/// x.expect_err("Testing expect_err"); // panics with `Testing expect_err: 10`
10451048
/// ```
10461049
#[inline]
1050+
#[track_caller]
10471051
#[stable(feature = "result_expect_err", since = "1.17.0")]
10481052
pub fn expect_err(self, msg: &str) -> E {
10491053
match self {
@@ -1188,6 +1192,7 @@ impl<T, E> Result<Option<T>, E> {
11881192
// This is a separate function to reduce the code size of the methods
11891193
#[inline(never)]
11901194
#[cold]
1195+
#[track_caller]
11911196
fn unwrap_failed(msg: &str, error: &dyn fmt::Debug) -> ! {
11921197
panic!("{}: {:?}", msg, error)
11931198
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// run-pass
2+
// ignore-wasm32-bare compiled with panic=abort by default
3+
4+
#![feature(option_expect_none, option_unwrap_none)]
5+
6+
//! Test that panic locations for `#[track_caller]` functions in std have the correct
7+
//! location reported.
8+
9+
fn main() {
10+
// inspect the `PanicInfo` we receive to ensure the right file is the source
11+
std::panic::set_hook(Box::new(|info| {
12+
let actual = info.location().unwrap();
13+
if actual.file() != file!() {
14+
eprintln!("expected a location in the test file, found {:?}", actual);
15+
panic!();
16+
}
17+
}));
18+
19+
fn assert_panicked(f: impl FnOnce() + std::panic::UnwindSafe) {
20+
std::panic::catch_unwind(f).unwrap_err();
21+
}
22+
23+
let nope: Option<()> = None;
24+
assert_panicked(|| nope.unwrap());
25+
assert_panicked(|| nope.expect(""));
26+
27+
let yep: Option<()> = Some(());
28+
assert_panicked(|| yep.unwrap_none());
29+
assert_panicked(|| yep.expect_none(""));
30+
31+
let oops: Result<(), ()> = Err(());
32+
assert_panicked(|| oops.unwrap());
33+
assert_panicked(|| oops.expect(""));
34+
35+
let fine: Result<(), ()> = Ok(());
36+
assert_panicked(|| fine.unwrap_err());
37+
assert_panicked(|| fine.expect_err(""));
38+
}

0 commit comments

Comments
 (0)