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).
This commit is contained in:
bors 2020-01-09 10:42:02 +00:00
commit 85ab34826a
2 changed files with 46 additions and 1 deletions

View file

@ -1 +1 @@
ebbb2bf37aedaaa64dfaa52ba337ca6efb6b9093
adc65725004c8aac16392fe4052c3e347181157d

View file

@ -0,0 +1,45 @@
// ignore-windows: Unwind panicking does not currently work on Windows
#![feature(option_expect_none, option_unwrap_none)]
//! Test that panic locations for `#[track_caller]` functions in std have the correct
//! location reported.
use std::sync::atomic::{AtomicUsize, Ordering};
static HOOK_COUNT: AtomicUsize = AtomicUsize::new(0);
fn main() {
// inspect the `PanicInfo` we receive to ensure the right file is the source
std::panic::set_hook(Box::new(|info| {
HOOK_COUNT.fetch_add(1, Ordering::Relaxed);
let actual = info.location().unwrap();
if actual.file() != file!() {
eprintln!("expected a location in the test file, found {:?}", actual);
panic!();
}
}));
fn assert_panicked(f: impl FnOnce() + std::panic::UnwindSafe) {
std::panic::catch_unwind(f).unwrap_err();
}
let nope: Option<()> = None;
assert_panicked(|| nope.unwrap());
assert_panicked(|| nope.expect(""));
let yep: Option<()> = Some(());
assert_panicked(|| yep.unwrap_none());
assert_panicked(|| yep.expect_none(""));
let oops: Result<(), ()> = Err(());
assert_panicked(|| oops.unwrap());
assert_panicked(|| oops.expect(""));
let fine: Result<(), ()> = Ok(());
assert_panicked(|| fine.unwrap_err());
assert_panicked(|| fine.expect_err(""));
// Cleanup: reset to default hook.
drop(std::panic::take_hook());
assert_eq!(HOOK_COUNT.load(Ordering::Relaxed), 8);
}