Skip to content

Commit

Permalink
Option's panics are all #[track_caller].
Browse files Browse the repository at this point in the history
Also includes a simple test with a custom panic hook to ensure we don't regress.
  • Loading branch information
anp committed Jan 5, 2020
1 parent 760ce94 commit 2e9d573
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/libcore/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ impl<T> Option<T> {
/// x.expect("the world is ending"); // panics with `the world is ending`
/// ```
#[inline]
#[track_caller]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn expect(self, msg: &str) -> T {
match self {
Expand Down Expand Up @@ -374,6 +375,7 @@ impl<T> Option<T> {
/// assert_eq!(x.unwrap(), "air"); // fails
/// ```
#[inline]
#[track_caller]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn unwrap(self) -> T {
match self {
Expand Down Expand Up @@ -1015,6 +1017,7 @@ impl<T: fmt::Debug> Option<T> {
/// }
/// ```
#[inline]
#[track_caller]
#[unstable(feature = "option_expect_none", reason = "newly added", issue = "62633")]
pub fn expect_none(self, msg: &str) {
if let Some(val) = self {
Expand Down Expand Up @@ -1057,6 +1060,7 @@ impl<T: fmt::Debug> Option<T> {
/// }
/// ```
#[inline]
#[track_caller]
#[unstable(feature = "option_unwrap_none", reason = "newly added", issue = "62633")]
pub fn unwrap_none(self) {
if let Some(val) = self {
Expand Down Expand Up @@ -1184,13 +1188,15 @@ impl<T, E> Option<Result<T, E>> {
// This is a separate function to reduce the code size of .expect() itself.
#[inline(never)]
#[cold]
#[track_caller]
fn expect_failed(msg: &str) -> ! {
panic!("{}", msg)
}

// This is a separate function to reduce the code size of .expect_none() itself.
#[inline(never)]
#[cold]
#[track_caller]
fn expect_none_failed(msg: &str, value: &dyn fmt::Debug) -> ! {
panic!("{}: {:?}", msg, value)
}
Expand Down
29 changes: 29 additions & 0 deletions src/test/ui/rfc-2091-track-caller/std-panic-locations.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// run-pass

#![feature(option_expect_none, option_unwrap_none)]

//! Test that panic locations for `#[track_caller]` functions in std have the correct
//! location reported.
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();
if actual.file() != file!(){
eprintln!("expected a location in the test file, found {:?}", actual);
panic!();
}
}));

fn assert_panicked(f: impl FnOnce() + std::panic::UnwindSafe) {
std::panic::catch_unwind(f).unwrap_err();
}

let nope: Option<()> = None;
assert_panicked(|| nope.unwrap());
assert_panicked(|| nope.expect(""));

let yep: Option<()> = Some(());
assert_panicked(|| yep.unwrap_none());
assert_panicked(|| yep.expect_none(""));
}

0 comments on commit 2e9d573

Please sign in to comment.