Skip to content

Commit e03a5d0

Browse files
committed
Try to write the panic message with a single write_all call
1 parent c5b5713 commit e03a5d0

34 files changed

+77
-14
lines changed

library/std/src/panicking.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,21 @@ fn default_hook(info: &PanicInfo<'_>) {
260260
let name = thread.as_ref().and_then(|t| t.name()).unwrap_or("<unnamed>");
261261

262262
let write = |err: &mut dyn crate::io::Write| {
263-
let _ = writeln!(err, "thread '{name}' panicked at {location}:\n{msg}");
263+
// Try to write the panic message to a buffer first to prevent other concurrent outputs
264+
// interleaving with it.
265+
let mut buffer = [0u8; 512];
266+
let mut cursor = crate::io::Cursor::new(&mut buffer[..]);
267+
268+
let write_msg = |dst: &mut dyn crate::io::Write| {
269+
writeln!(dst, "\nthread '{name}' panicked at {location}:\n{msg}")
270+
};
271+
272+
let _ = if write_msg(&mut cursor).is_ok() {
273+
let pos = cursor.position() as usize;
274+
err.write_all(&buffer[0..pos])
275+
} else {
276+
write_msg(err)
277+
};
264278

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

tests/ui/const-generics/generic_const_exprs/issue-80742.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
error: internal compiler error: compiler/rustc_const_eval/src/interpret/step.rs:LL:CC: SizeOf MIR operator called for unsized type dyn Debug
22
--> $SRC_DIR/core/src/mem/mod.rs:LL:COL
33

4+
45
Box<dyn Any>
56
query stack during panic:
67
#0 [eval_to_allocation_raw] const-evaluating + checking `<impl at $DIR/issue-80742.rs:26:1: 28:32>::{constant#0}`

tests/ui/consts/const-eval/const-eval-query-stack.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ error: internal compiler error[E0080]: evaluation of constant value failed
44
LL | const X: i32 = 1 / 0;
55
| ^^^^^ attempt to divide `1_i32` by zero
66

7+
78
query stack during panic:
89
#0 [eval_to_allocation_raw] const-evaluating + checking `X`
910
#1 [eval_to_const_value_raw] simplifying constant for the type system `X`

tests/ui/extern/extern-types-field-offset.run.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
thread 'main' panicked at library/core/src/panicking.rs:$LINE:$COL:
23
attempted to compute the size or alignment of extern type `Opaque`
34
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

tests/ui/extern/extern-types-size_of_val.align.run.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
thread 'main' panicked at library/core/src/panicking.rs:$LINE:$COL:
23
attempted to compute the size or alignment of extern type `A`
34
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

tests/ui/extern/extern-types-size_of_val.size.run.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
thread 'main' panicked at library/core/src/panicking.rs:$LINE:$COL:
23
attempted to compute the size or alignment of extern type `A`
34
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

tests/ui/higher-ranked/trait-bounds/future.current.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
error: the compiler unexpectedly panicked. this is a bug.
23

34
query stack during panic:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
thread 'main' panicked at library/alloc/src/raw_vec.rs:LL:CC:
23
capacity overflow
34
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
thread 'main' panicked at $DIR/const-eval-select-backtrace-std.rs:6:8:
23
byte index 1 is out of bounds of ``
34
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
thread 'main' panicked at $DIR/const-eval-select-backtrace.rs:15:5:
23
Aaah!
34
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

0 commit comments

Comments
 (0)