]> git.lizzy.rs Git - rust.git/blob - src/test/ui/rfc-2091-track-caller/std-panic-locations.rs
Rollup merge of #94577 - RalfJung:simd-miri, r=scottmcm
[rust.git] / src / test / ui / rfc-2091-track-caller / std-panic-locations.rs
1 // run-pass
2 // needs-unwind
3 // ignore-wasm32-bare compiled with panic=abort by default
4 // revisions: default mir-opt
5 //[mir-opt] compile-flags: -Zmir-opt-level=4
6
7 #![allow(unconditional_panic)]
8
9 //! Test that panic locations for `#[track_caller]` functions in std have the correct
10 //! location reported.
11
12 use std::cell::RefCell;
13 use std::collections::{BTreeMap, HashMap, VecDeque};
14 use std::ops::{Index, IndexMut};
15 use std::panic::{AssertUnwindSafe, UnwindSafe};
16
17 fn main() {
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);
23             panic!();
24         }
25     }));
26
27     fn assert_panicked(f: impl FnOnce() + UnwindSafe) {
28         std::panic::catch_unwind(f).unwrap_err();
29     }
30
31     let nope: Option<()> = None;
32     assert_panicked(|| nope.unwrap());
33     assert_panicked(|| nope.expect(""));
34
35     let oops: Result<(), ()> = Err(());
36     assert_panicked(|| oops.unwrap());
37     assert_panicked(|| oops.expect(""));
38
39     let fine: Result<(), ()> = Ok(());
40     assert_panicked(|| fine.unwrap_err());
41     assert_panicked(|| fine.expect_err(""));
42
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; });
48
49     let sorted: BTreeMap<bool, bool> = Default::default();
50     assert_panicked(|| { sorted.index(&false); });
51     assert_panicked(|| { sorted[&false]; });
52
53     let unsorted: HashMap<bool, bool> = Default::default();
54     assert_panicked(|| { unsorted.index(&false); });
55     assert_panicked(|| { unsorted[&false]; });
56
57     let weirdo: VecDeque<()> = Default::default();
58     assert_panicked(|| { weirdo.index(1); });
59     assert_panicked(|| { weirdo[1]; });
60
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(); }));
65 }