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