]> git.proxmox.com Git - rustc.git/blame - src/test/ui/rfc-2091-track-caller/std-panic-locations.rs
Merge tag 'debian/1.52.1+dfsg1-1_exp2' into proxmox/buster
[rustc.git] / src / test / ui / rfc-2091-track-caller / std-panic-locations.rs
CommitLineData
dfeec247
XL
1// run-pass
2// ignore-wasm32-bare compiled with panic=abort by default
29967ef6 3// revisions: default mir-opt
6a06907d 4//[mir-opt] compile-flags: -Zmir-opt-level=4
dfeec247
XL
5
6#![feature(option_expect_none, option_unwrap_none)]
ba9703b0 7#![allow(unconditional_panic)]
dfeec247
XL
8
9//! Test that panic locations for `#[track_caller]` functions in std have the correct
10//! location reported.
11
3dfed10e 12use std::cell::RefCell;
ba9703b0
XL
13use std::collections::{BTreeMap, HashMap, VecDeque};
14use std::ops::{Index, IndexMut};
3dfed10e 15use std::panic::{AssertUnwindSafe, UnwindSafe};
ba9703b0 16
dfeec247
XL
17fn 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
3dfed10e 27 fn assert_panicked(f: impl FnOnce() + UnwindSafe) {
dfeec247
XL
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 yep: Option<()> = Some(());
36 assert_panicked(|| yep.unwrap_none());
37 assert_panicked(|| yep.expect_none(""));
38
39 let oops: Result<(), ()> = Err(());
40 assert_panicked(|| oops.unwrap());
41 assert_panicked(|| oops.expect(""));
42
43 let fine: Result<(), ()> = Ok(());
44 assert_panicked(|| fine.unwrap_err());
45 assert_panicked(|| fine.expect_err(""));
ba9703b0
XL
46
47 let mut small = [0]; // the implementation backing str, vec, etc
48 assert_panicked(move || { small.index(1); });
49 assert_panicked(move || { small[1]; });
50 assert_panicked(move || { small.index_mut(1); });
51 assert_panicked(move || { small[1] += 1; });
52
53 let sorted: BTreeMap<bool, bool> = Default::default();
54 assert_panicked(|| { sorted.index(&false); });
55 assert_panicked(|| { sorted[&false]; });
56
57 let unsorted: HashMap<bool, bool> = Default::default();
58 assert_panicked(|| { unsorted.index(&false); });
59 assert_panicked(|| { unsorted[&false]; });
60
61 let weirdo: VecDeque<()> = Default::default();
62 assert_panicked(|| { weirdo.index(1); });
63 assert_panicked(|| { weirdo[1]; });
3dfed10e
XL
64
65 let refcell: RefCell<()> = Default::default();
66 let _conflicting = refcell.borrow_mut();
67 assert_panicked(AssertUnwindSafe(|| { refcell.borrow(); }));
68 assert_panicked(AssertUnwindSafe(|| { refcell.borrow_mut(); }));
dfeec247 69}