Skip to content

Commit b1676a3

Browse files
committed
test that unwrap gets us the right panic location
1 parent 71a7b9b commit b1676a3

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

rust-version

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

0 commit comments

Comments
 (0)