forked from rust-lang/rust
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#67887 - anp:tracked-std-panics, r=nagisa
`Option::{expect,unwrap}` and `Result::{expect, expect_err, unwrap, unwrap_err}` have `#[track_caller]` The annotated functions now produce panic messages pointing to the location where they were called, rather than `core`'s internals.
- Loading branch information
Showing
3 changed files
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// run-pass | ||
// ignore-wasm32-bare compiled with panic=abort by default | ||
|
||
#![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("")); | ||
|
||
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("")); | ||
} |