Skip to content

Commit

Permalink
Auto merge of #112849 - m-ou-se:panic-message-format, r=thomcc
Browse files Browse the repository at this point in the history
Change default panic handler message format.

This changes the default panic hook's message format from:

```
thread '{thread}' panicked at '{message}', {location}
```

to

```
thread '{thread}' panicked at {location}:
{message}
```

This puts the message on its own line without surrounding quotes, making it easiser to read. For example:

Before:
```
thread 'main' panicked at 'env variable `IMPORTANT_PATH` should be set by `wrapper_script.sh`', src/main.rs:4:6
```
After:
```
thread 'main' panicked at src/main.rs:4:6:
env variable `IMPORTANT_PATH` should be set by `wrapper_script.sh`
```

---

See this PR by `@nyurik,` which does that for only multi-line messages (specifically because of `assert_eq`): rust-lang/rust#111071

This is the change that does that for *all* panic messages.
  • Loading branch information
bors committed Aug 1, 2023
2 parents ae46414 + 85eca92 commit 3674358
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
6 changes: 4 additions & 2 deletions core/src/error.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ information that is already communicated by the source error being
unwrapped:

```text
thread 'main' panicked at 'env variable `IMPORTANT_PATH` is not set: NotPresent', src/main.rs:4:6
thread 'main' panicked at src/main.rs:4:6:
env variable `IMPORTANT_PATH` is not set: NotPresent
```

In this example we end up mentioning that an env variable is not set,
Expand All @@ -109,7 +110,8 @@ prevent the source error, we end up introducing new information that is
independent from our source error.

```text
thread 'main' panicked at 'env variable `IMPORTANT_PATH` should be set by `wrapper_script.sh`: NotPresent', src/main.rs:4:6
thread 'main' panicked at src/main.rs:4:6:
env variable `IMPORTANT_PATH` should be set by `wrapper_script.sh`: NotPresent
```

In this example we are communicating not only the name of the
Expand Down
10 changes: 6 additions & 4 deletions core/src/panic/panic_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,16 +147,18 @@ impl<'a> PanicInfo<'a> {
impl fmt::Display for PanicInfo<'_> {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str("panicked at ")?;
self.location.fmt(formatter)?;
if let Some(message) = self.message {
write!(formatter, "'{}', ", message)?
formatter.write_str(":\n")?;
formatter.write_fmt(*message)?;
} else if let Some(payload) = self.payload.downcast_ref::<&'static str>() {
write!(formatter, "'{}', ", payload)?
formatter.write_str(":\n")?;
formatter.write_str(payload)?;
}
// NOTE: we cannot use downcast_ref::<String>() here
// since String is not available in core!
// The payload is a String when `std::panic!` is called with multiple arguments,
// but in that case the message is also available.

self.location.fmt(formatter)
Ok(())
}
}
3 changes: 2 additions & 1 deletion std/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ mod private {
/// This example produces the following output:
///
/// ```console
/// thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: SuperError is here!: SuperErrorSideKick is here!', src/error.rs:34:40
/// thread 'main' panicked at src/error.rs:34:40:
/// called `Result::unwrap()` on an `Err` value: SuperError is here!: SuperErrorSideKick is here!
/// note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
/// ```
///
Expand Down
2 changes: 1 addition & 1 deletion std/src/panicking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ pub fn panic_hook_with_disk_dump(info: &PanicInfo<'_>, path: Option<&crate::path
let name = thread.as_ref().and_then(|t| t.name()).unwrap_or("<unnamed>");

let write = |err: &mut dyn crate::io::Write, backtrace: Option<BacktraceStyle>| {
let _ = writeln!(err, "thread '{name}' panicked at '{msg}', {location}");
let _ = writeln!(err, "thread '{name}' panicked at {location}:\n{msg}");

static FIRST_PANIC: AtomicBool = AtomicBool::new(true);

Expand Down

0 comments on commit 3674358

Please sign in to comment.