3 // ignore-wasm32-bare compiled with panic=abort by default
4 // revisions: default mir-opt
5 //[mir-opt] compile-flags: -Zmir-opt-level=4
7 #![allow(unconditional_panic)]
9 //! Test that panic locations for `#[track_caller]` functions in std have the correct
10 //! location reported.
12 use std::cell::RefCell;
13 use std::collections::{BTreeMap, HashMap, VecDeque};
14 use std::ops::{Index, IndexMut};
15 use std::panic::{AssertUnwindSafe, UnwindSafe};
18 // inspect the `PanicInfo` we receive to ensure the right file is the source
19 std::panic::set_hook(Box::new(|info| {
20 let actual = info.location().unwrap();
21 if actual.file() != file!() {
22 eprintln!("expected a location in the test file, found {:?}", actual);
27 fn assert_panicked(f: impl FnOnce() + UnwindSafe) {
28 std::panic::catch_unwind(f).unwrap_err();
31 let nope: Option<()> = None;
32 assert_panicked(|| nope.unwrap());
33 assert_panicked(|| nope.expect(""));
35 let oops: Result<(), ()> = Err(());
36 assert_panicked(|| oops.unwrap());
37 assert_panicked(|| oops.expect(""));
39 let fine: Result<(), ()> = Ok(());
40 assert_panicked(|| fine.unwrap_err());
41 assert_panicked(|| fine.expect_err(""));
43 let mut small = [0]; // the implementation backing str, vec, etc
44 assert_panicked(move || { small.index(1); });
45 assert_panicked(move || { small[1]; });
46 assert_panicked(move || { small.index_mut(1); });
47 assert_panicked(move || { small[1] += 1; });
49 let sorted: BTreeMap<bool, bool> = Default::default();
50 assert_panicked(|| { sorted.index(&false); });
51 assert_panicked(|| { sorted[&false]; });
53 let unsorted: HashMap<bool, bool> = Default::default();
54 assert_panicked(|| { unsorted.index(&false); });
55 assert_panicked(|| { unsorted[&false]; });
57 let weirdo: VecDeque<()> = Default::default();
58 assert_panicked(|| { weirdo.index(1); });
59 assert_panicked(|| { weirdo[1]; });
61 let refcell: RefCell<()> = Default::default();
62 let _conflicting = refcell.borrow_mut();
63 assert_panicked(AssertUnwindSafe(|| { refcell.borrow(); }));
64 assert_panicked(AssertUnwindSafe(|| { refcell.borrow_mut(); }));