2020-01-04 21:30:44 -06:00
|
|
|
// run-pass
|
2021-12-03 09:32:51 -06:00
|
|
|
// needs-unwind
|
2020-02-08 15:46:57 -06:00
|
|
|
// revisions: default mir-opt
|
2021-03-04 07:21:13 -06:00
|
|
|
//[mir-opt] compile-flags: -Zmir-opt-level=4
|
2020-01-04 21:30:44 -06:00
|
|
|
|
2020-03-22 20:21:09 -05:00
|
|
|
#![allow(unconditional_panic)]
|
2020-01-04 21:30:44 -06:00
|
|
|
|
|
|
|
//! Test that panic locations for `#[track_caller]` functions in std have the correct
|
|
|
|
//! location reported.
|
|
|
|
|
2020-07-19 13:07:21 -05:00
|
|
|
use std::cell::RefCell;
|
2020-03-22 20:21:09 -05:00
|
|
|
use std::collections::{BTreeMap, HashMap, VecDeque};
|
|
|
|
use std::ops::{Index, IndexMut};
|
2020-07-19 13:07:21 -05:00
|
|
|
use std::panic::{AssertUnwindSafe, UnwindSafe};
|
2020-03-22 20:21:09 -05:00
|
|
|
|
2020-01-04 21:30:44 -06:00
|
|
|
fn main() {
|
|
|
|
// inspect the `PanicInfo` we receive to ensure the right file is the source
|
|
|
|
std::panic::set_hook(Box::new(|info| {
|
|
|
|
let actual = info.location().unwrap();
|
2020-01-05 00:54:09 -06:00
|
|
|
if actual.file() != file!() {
|
2020-01-04 21:30:44 -06:00
|
|
|
eprintln!("expected a location in the test file, found {:?}", actual);
|
|
|
|
panic!();
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
2020-07-19 13:07:21 -05:00
|
|
|
fn assert_panicked(f: impl FnOnce() + UnwindSafe) {
|
2020-01-04 21:30:44 -06:00
|
|
|
std::panic::catch_unwind(f).unwrap_err();
|
|
|
|
}
|
|
|
|
|
|
|
|
let nope: Option<()> = None;
|
|
|
|
assert_panicked(|| nope.unwrap());
|
|
|
|
assert_panicked(|| nope.expect(""));
|
|
|
|
|
2020-01-04 21:42:21 -06:00
|
|
|
let oops: Result<(), ()> = Err(());
|
|
|
|
assert_panicked(|| oops.unwrap());
|
|
|
|
assert_panicked(|| oops.expect(""));
|
|
|
|
|
|
|
|
let fine: Result<(), ()> = Ok(());
|
|
|
|
assert_panicked(|| fine.unwrap_err());
|
|
|
|
assert_panicked(|| fine.expect_err(""));
|
2020-03-22 20:21:09 -05:00
|
|
|
|
|
|
|
let mut small = [0]; // the implementation backing str, vec, etc
|
|
|
|
assert_panicked(move || { small.index(1); });
|
|
|
|
assert_panicked(move || { small[1]; });
|
|
|
|
assert_panicked(move || { small.index_mut(1); });
|
|
|
|
assert_panicked(move || { small[1] += 1; });
|
|
|
|
|
|
|
|
let sorted: BTreeMap<bool, bool> = Default::default();
|
|
|
|
assert_panicked(|| { sorted.index(&false); });
|
|
|
|
assert_panicked(|| { sorted[&false]; });
|
|
|
|
|
|
|
|
let unsorted: HashMap<bool, bool> = Default::default();
|
|
|
|
assert_panicked(|| { unsorted.index(&false); });
|
|
|
|
assert_panicked(|| { unsorted[&false]; });
|
|
|
|
|
|
|
|
let weirdo: VecDeque<()> = Default::default();
|
|
|
|
assert_panicked(|| { weirdo.index(1); });
|
|
|
|
assert_panicked(|| { weirdo[1]; });
|
2020-07-19 13:07:21 -05:00
|
|
|
|
|
|
|
let refcell: RefCell<()> = Default::default();
|
|
|
|
let _conflicting = refcell.borrow_mut();
|
|
|
|
assert_panicked(AssertUnwindSafe(|| { refcell.borrow(); }));
|
|
|
|
assert_panicked(AssertUnwindSafe(|| { refcell.borrow_mut(); }));
|
2020-01-04 21:30:44 -06:00
|
|
|
}
|