Skip to content

Commit 3674358

Browse files
committed
Auto merge of #112849 - m-ou-se:panic-message-format, r=thomcc
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.
2 parents ae46414 + 85eca92 commit 3674358

File tree

4 files changed

+13
-8
lines changed

4 files changed

+13
-8
lines changed

core/src/error.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ information that is already communicated by the source error being
9393
unwrapped:
9494

9595
```text
96-
thread 'main' panicked at 'env variable `IMPORTANT_PATH` is not set: NotPresent', src/main.rs:4:6
96+
thread 'main' panicked at src/main.rs:4:6:
97+
env variable `IMPORTANT_PATH` is not set: NotPresent
9798
```
9899

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

111112
```text
112-
thread 'main' panicked at 'env variable `IMPORTANT_PATH` should be set by `wrapper_script.sh`: NotPresent', src/main.rs:4:6
113+
thread 'main' panicked at src/main.rs:4:6:
114+
env variable `IMPORTANT_PATH` should be set by `wrapper_script.sh`: NotPresent
113115
```
114116

115117
In this example we are communicating not only the name of the

core/src/panic/panic_info.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,16 +147,18 @@ impl<'a> PanicInfo<'a> {
147147
impl fmt::Display for PanicInfo<'_> {
148148
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
149149
formatter.write_str("panicked at ")?;
150+
self.location.fmt(formatter)?;
150151
if let Some(message) = self.message {
151-
write!(formatter, "'{}', ", message)?
152+
formatter.write_str(":\n")?;
153+
formatter.write_fmt(*message)?;
152154
} else if let Some(payload) = self.payload.downcast_ref::<&'static str>() {
153-
write!(formatter, "'{}', ", payload)?
155+
formatter.write_str(":\n")?;
156+
formatter.write_str(payload)?;
154157
}
155158
// NOTE: we cannot use downcast_ref::<String>() here
156159
// since String is not available in core!
157160
// The payload is a String when `std::panic!` is called with multiple arguments,
158161
// but in that case the message is also available.
159-
160-
self.location.fmt(formatter)
162+
Ok(())
161163
}
162164
}

std/src/error.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ mod private {
121121
/// This example produces the following output:
122122
///
123123
/// ```console
124-
/// thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: SuperError is here!: SuperErrorSideKick is here!', src/error.rs:34:40
124+
/// thread 'main' panicked at src/error.rs:34:40:
125+
/// called `Result::unwrap()` on an `Err` value: SuperError is here!: SuperErrorSideKick is here!
125126
/// note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
126127
/// ```
127128
///

std/src/panicking.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ pub fn panic_hook_with_disk_dump(info: &PanicInfo<'_>, path: Option<&crate::path
266266
let name = thread.as_ref().and_then(|t| t.name()).unwrap_or("<unnamed>");
267267

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

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

0 commit comments

Comments
 (0)