Skip to content

Commit f7a5b63

Browse files
committed
Auto merge of #67887 - anp:tracked-std-panics, r=<try>
`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 093241d + 7a6af7e commit f7a5b63

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

src/libcore/option.rs

+6
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

+5
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
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// run-pass
2+
3+
#![feature(option_expect_none, option_unwrap_none)]
4+
5+
//! Test that panic locations for `#[track_caller]` functions in std have the correct
6+
//! location reported.
7+
8+
fn main() {
9+
// inspect the `PanicInfo` we receive to ensure the right file is the source
10+
std::panic::set_hook(Box::new(|info| {
11+
let actual = info.location().unwrap();
12+
if actual.file() != file!(){
13+
eprintln!("expected a location in the test file, found {:?}", actual);
14+
panic!();
15+
}
16+
}));
17+
18+
fn assert_panicked(f: impl FnOnce() + std::panic::UnwindSafe) {
19+
std::panic::catch_unwind(f).unwrap_err();
20+
}
21+
22+
let nope: Option<()> = None;
23+
assert_panicked(|| nope.unwrap());
24+
assert_panicked(|| nope.expect(""));
25+
26+
let yep: Option<()> = Some(());
27+
assert_panicked(|| yep.unwrap_none());
28+
assert_panicked(|| yep.expect_none(""));
29+
30+
let oops: Result<(), ()> = Err(());
31+
assert_panicked(|| oops.unwrap());
32+
assert_panicked(|| oops.expect(""));
33+
34+
let fine: Result<(), ()> = Ok(());
35+
assert_panicked(|| fine.unwrap_err());
36+
assert_panicked(|| fine.expect_err(""));
37+
}

0 commit comments

Comments
 (0)