about summary refs log tree commit diff
path: root/src/tools/miri/tests/pass/panic/std-panic-locations.rs
blob: 8781a371d51fa1d6641a7ef8a429e132edef9ab9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//! 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 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), 6);
}