Skip to content

Commit 139929a

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

34 files changed

+114
-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}\n")
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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
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>
6+
57
query stack during panic:
68
#0 [eval_to_allocation_raw] const-evaluating + checking `<impl at $DIR/issue-80742.rs:26:1: 28:32>::{constant#0}`
79
#1 [eval_to_valtree] evaluating type-level constant

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ 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+
8+
79
query stack during panic:
810
#0 [eval_to_allocation_raw] const-evaluating + checking `X`
911
#1 [eval_to_const_value_raw] simplifying constant for the type system `X`
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
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`
4+
35
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
46
thread caused non-unwinding panic. aborting.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
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`
4+
35
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
46
thread caused non-unwinding panic. aborting.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
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`
4+
35
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
46
thread caused non-unwinding panic. aborting.

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
2+
13
error: the compiler unexpectedly panicked. this is a bug.
24

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

0 commit comments

Comments
 (0)