Skip to content

Commit 2e9d573

Browse files
committed
Option's panics are all #[track_caller].
Also includes a simple test with a custom panic hook to ensure we don't regress.
1 parent 760ce94 commit 2e9d573

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-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
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
}

0 commit comments

Comments
 (0)