Skip to content

Commit

Permalink
Auto merge of #85745 - veber-alex:panic_any, r=m-ou-se
Browse files Browse the repository at this point in the history
Add #[track_caller] to panic_any

Report the panic location from the user code.

```rust
use std::panic;
use std::panic::panic_any;

fn main() {
    panic::set_hook(Box::new(|panic_info| {
        if let Some(location) = panic_info.location() {
            println!(
                "panic occurred in file '{}' at line {}",
                location.file(),
                location.line(),
            );
        } else {
            println!("panic occurred but can't get location information...");
        }
    }));

    panic_any(42);
}
````

Before:
`panic occurred in file '/rustc/ff2c947c00f867b9f012e28ba88cecfbe556f904/library/std/src/panic.rs' at line 59`

After:
`panic occurred in file 'src/main.rs' at line 17`
  • Loading branch information
bors committed May 28, 2021
2 parents 8eef79c + ef13f27 commit 18135ec
Showing 1 changed file with 1 addition and 0 deletions.
1 change: 1 addition & 0 deletions library/std/src/panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ pub use core::panic::{Location, PanicInfo};
/// See the [`panic!`] macro for more information about panicking.
#[stable(feature = "panic_any", since = "1.51.0")]
#[inline]
#[track_caller]
pub fn panic_any<M: 'static + Any + Send>(msg: M) -> ! {
crate::panicking::begin_panic(msg);
}
Expand Down

0 comments on commit 18135ec

Please sign in to comment.