Skip to content

Commit 85ab348

Browse files
committed
Auto merge of #1144 - RalfJung:panic-location, r=RalfJung
test that unwrap gets us the right panic location Make sure this stuff works in Miri as well -- basically, an integration test for `track_caller` and the panic machinery (we already have more focused tests as well).
2 parents 71a7b9b + 833816d commit 85ab348

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-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: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// ignore-windows: Unwind panicking does not currently work on Windows
2+
#![feature(option_expect_none, option_unwrap_none)]
3+
//! Test that panic locations for `#[track_caller]` functions in std have the correct
4+
//! location reported.
5+
6+
use std::sync::atomic::{AtomicUsize, Ordering};
7+
8+
static HOOK_COUNT: AtomicUsize = AtomicUsize::new(0);
9+
10+
fn main() {
11+
// inspect the `PanicInfo` we receive to ensure the right file is the source
12+
std::panic::set_hook(Box::new(|info| {
13+
HOOK_COUNT.fetch_add(1, Ordering::Relaxed);
14+
let actual = info.location().unwrap();
15+
if actual.file() != file!() {
16+
eprintln!("expected a location in the test file, found {:?}", actual);
17+
panic!();
18+
}
19+
}));
20+
21+
fn assert_panicked(f: impl FnOnce() + std::panic::UnwindSafe) {
22+
std::panic::catch_unwind(f).unwrap_err();
23+
}
24+
25+
let nope: Option<()> = None;
26+
assert_panicked(|| nope.unwrap());
27+
assert_panicked(|| nope.expect(""));
28+
29+
let yep: Option<()> = Some(());
30+
assert_panicked(|| yep.unwrap_none());
31+
assert_panicked(|| yep.expect_none(""));
32+
33+
let oops: Result<(), ()> = Err(());
34+
assert_panicked(|| oops.unwrap());
35+
assert_panicked(|| oops.expect(""));
36+
37+
let fine: Result<(), ()> = Ok(());
38+
assert_panicked(|| fine.unwrap_err());
39+
assert_panicked(|| fine.expect_err(""));
40+
41+
// Cleanup: reset to default hook.
42+
drop(std::panic::take_hook());
43+
44+
assert_eq!(HOOK_COUNT.load(Ordering::Relaxed), 8);
45+
}

0 commit comments

Comments
 (0)