|
| 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