Skip to content

Commit

Permalink
Try to write the panic message with a single write_all call
Browse files Browse the repository at this point in the history
  • Loading branch information
Zoxc committed Mar 16, 2024
1 parent c5b5713 commit 4962cfe
Show file tree
Hide file tree
Showing 77 changed files with 141 additions and 14 deletions.
16 changes: 15 additions & 1 deletion library/std/src/panicking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,21 @@ fn default_hook(info: &PanicInfo<'_>) {
let name = thread.as_ref().and_then(|t| t.name()).unwrap_or("<unnamed>");

let write = |err: &mut dyn crate::io::Write| {
let _ = writeln!(err, "thread '{name}' panicked at {location}:\n{msg}");
// Try to write the panic message to a buffer first to prevent other concurrent outputs
// interleaving with it.
let mut buffer = [0u8; 512];
let mut cursor = crate::io::Cursor::new(&mut buffer[..]);

let write_msg = |dst: &mut dyn crate::io::Write| {
writeln!(dst, "\nthread '{name}' panicked at {location}:\n{msg}")
};

let _ = if write_msg(&mut cursor).is_ok() {
let pos = cursor.position() as usize;
err.write_all(&buffer[0..pos])
} else {
write_msg(err)
};

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

thread 'main' panicked at $DIR/exported_symbol_bad_unwind1.rs:LL:CC:
explicit panic
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

thread 'main' panicked at $DIR/exported_symbol_bad_unwind2.rs:LL:CC:
explicit panic
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

thread 'main' panicked at RUSTLIB/core/src/panicking.rs:LL:CC:
panic in a function that cannot unwind
stack backtrace:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

thread 'main' panicked at $DIR/exported_symbol_bad_unwind2.rs:LL:CC:
explicit panic
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

thread 'main' panicked at RUSTLIB/core/src/panicking.rs:LL:CC:
panic in a function that cannot unwind
stack backtrace:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

thread 'main' panicked at $DIR/exported_symbol_bad_unwind2.rs:LL:CC:
explicit panic
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

thread 'main' panicked at $DIR/return_pointer_on_unwind.rs:LL:CC:
explicit panic
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

thread 'main' panicked at RUSTLIB/core/src/panicking.rs:LL:CC:
aborted execution: attempted to instantiate uninhabited type `!`
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Expand Down
1 change: 1 addition & 0 deletions src/tools/miri/tests/fail/intrinsics/zero_fn_ptr.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

thread 'main' panicked at RUSTLIB/core/src/panicking.rs:LL:CC:
aborted execution: attempted to zero-initialize type `fn()`, which is invalid
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Expand Down
1 change: 1 addition & 0 deletions src/tools/miri/tests/fail/panic/bad_unwind.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

thread 'main' panicked at $DIR/bad_unwind.rs:LL:CC:
explicit panic
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Expand Down
3 changes: 3 additions & 0 deletions src/tools/miri/tests/fail/panic/double_panic.stderr
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@

thread 'main' panicked at $DIR/double_panic.rs:LL:CC:
first
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

thread 'main' panicked at $DIR/double_panic.rs:LL:CC:
second
stack backtrace:

thread 'main' panicked at RUSTLIB/core/src/panicking.rs:LL:CC:
panic in a destructor during cleanup
thread caused non-unwinding panic. aborting.
Expand Down
1 change: 1 addition & 0 deletions src/tools/miri/tests/fail/panic/panic_abort1.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

thread 'main' panicked at $DIR/panic_abort1.rs:LL:CC:
panicking from libstd
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Expand Down
1 change: 1 addition & 0 deletions src/tools/miri/tests/fail/panic/panic_abort2.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

thread 'main' panicked at $DIR/panic_abort2.rs:LL:CC:
42-panicking from libstd
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Expand Down
1 change: 1 addition & 0 deletions src/tools/miri/tests/fail/panic/panic_abort3.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

thread 'main' panicked at $DIR/panic_abort3.rs:LL:CC:
panicking from libcore
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Expand Down
1 change: 1 addition & 0 deletions src/tools/miri/tests/fail/panic/panic_abort4.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

thread 'main' panicked at $DIR/panic_abort4.rs:LL:CC:
42-panicking from libcore
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

thread $NAME panicked at $DIR/tls_macro_const_drop_panic.rs:LL:CC:
ow
fatal runtime error: thread local panicked on drop
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

thread $NAME panicked at $DIR/tls_macro_drop_panic.rs:LL:CC:
ow
fatal runtime error: thread local panicked on drop
Expand Down
2 changes: 2 additions & 0 deletions src/tools/miri/tests/fail/terminate-terminator.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
warning: You have explicitly enabled MIR optimizations, overriding Miri's default which is to completely disable them. Any optimizations may hide UB that Miri would otherwise detect, and it is not necessarily possible to predict what kind of UB will be missed. If you are enabling optimizations to make Miri run faster, we advise using cfg(miri) to shrink your workload instead. The performance benefit of enabling MIR optimizations is usually marginal at best.


thread 'main' panicked at $DIR/terminate-terminator.rs:LL:CC:
explicit panic
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

thread 'main' panicked at RUSTLIB/core/src/panicking.rs:LL:CC:
panic in a function that cannot unwind
stack backtrace:
Expand Down
2 changes: 2 additions & 0 deletions src/tools/miri/tests/fail/unwind-action-terminate.stderr
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

thread 'main' panicked at $DIR/unwind-action-terminate.rs:LL:CC:
explicit panic
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

thread 'main' panicked at RUSTLIB/core/src/panicking.rs:LL:CC:
panic in a function that cannot unwind
stack backtrace:
Expand Down
1 change: 1 addition & 0 deletions src/tools/miri/tests/panic/div-by-zero-2.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

thread 'main' panicked at $DIR/div-by-zero-2.rs:LL:CC:
attempt to divide by zero
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@

thread 'main' panicked at $DIR/exported_symbol_good_unwind.rs:LL:CC:
explicit panic
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

thread 'main' panicked at $DIR/exported_symbol_good_unwind.rs:LL:CC:
explicit panic

thread 'main' panicked at $DIR/exported_symbol_good_unwind.rs:LL:CC:
explicit panic
1 change: 1 addition & 0 deletions src/tools/miri/tests/panic/oob_subslice.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

thread 'main' panicked at $DIR/oob_subslice.rs:LL:CC:
range end index 5 out of range for slice of length 4
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
1 change: 1 addition & 0 deletions src/tools/miri/tests/panic/overflowing-lsh-neg.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

thread 'main' panicked at $DIR/overflowing-lsh-neg.rs:LL:CC:
attempt to shift left with overflow
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
1 change: 1 addition & 0 deletions src/tools/miri/tests/panic/overflowing-rsh-1.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

thread 'main' panicked at $DIR/overflowing-rsh-1.rs:LL:CC:
attempt to shift right with overflow
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
1 change: 1 addition & 0 deletions src/tools/miri/tests/panic/overflowing-rsh-2.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

thread 'main' panicked at $DIR/overflowing-rsh-2.rs:LL:CC:
attempt to shift right with overflow
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
1 change: 1 addition & 0 deletions src/tools/miri/tests/panic/panic1.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

thread 'main' panicked at $DIR/panic1.rs:LL:CC:
panicking from libstd
stack backtrace:
Expand Down
1 change: 1 addition & 0 deletions src/tools/miri/tests/panic/panic2.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

thread 'main' panicked at $DIR/panic2.rs:LL:CC:
42-panicking from libstd
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
1 change: 1 addition & 0 deletions src/tools/miri/tests/panic/panic3.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

thread 'main' panicked at $DIR/panic3.rs:LL:CC:
panicking from libcore
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
1 change: 1 addition & 0 deletions src/tools/miri/tests/panic/panic4.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

thread 'main' panicked at $DIR/panic4.rs:LL:CC:
42-panicking from libcore
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
1 change: 1 addition & 0 deletions src/tools/miri/tests/panic/transmute_fat2.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

thread 'main' panicked at $DIR/transmute_fat2.rs:LL:CC:
index out of bounds: the len is 0 but the index is 0
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

thread 'main' panicked at $DIR/unsupported_foreign_function.rs:LL:CC:
unsupported Miri functionality: can't call foreign function `foo` on $OS
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
1 change: 1 addition & 0 deletions src/tools/miri/tests/panic/unsupported_syscall.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

thread 'main' panicked at $DIR/unsupported_syscall.rs:LL:CC:
unsupported Miri functionality: can't execute syscall with ID 0
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
11 changes: 11 additions & 0 deletions src/tools/miri/tests/pass/panic/catch_panic.stderr
Original file line number Diff line number Diff line change
@@ -1,34 +1,45 @@

thread 'main' panicked at $DIR/catch_panic.rs:LL:CC:
Hello from std::panic
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Caught panic message (&str): Hello from std::panic

thread 'main' panicked at $DIR/catch_panic.rs:LL:CC:
Hello from std::panic: 1
Caught panic message (String): Hello from std::panic: 1

thread 'main' panicked at $DIR/catch_panic.rs:LL:CC:
Hello from std::panic_any: 2
Caught panic message (String): Hello from std::panic_any: 2

thread 'main' panicked at $DIR/catch_panic.rs:LL:CC:
Box<dyn Any>
Failed to get caught panic message.

thread 'main' panicked at $DIR/catch_panic.rs:LL:CC:
Hello from core::panic
Caught panic message (&str): Hello from core::panic

thread 'main' panicked at $DIR/catch_panic.rs:LL:CC:
Hello from core::panic: 5
Caught panic message (String): Hello from core::panic: 5

thread 'main' panicked at $DIR/catch_panic.rs:LL:CC:
index out of bounds: the len is 3 but the index is 4
Caught panic message (String): index out of bounds: the len is 3 but the index is 4

thread 'main' panicked at $DIR/catch_panic.rs:LL:CC:
attempt to divide by zero
Caught panic message (&str): attempt to divide by zero

thread 'main' panicked at RUSTLIB/core/src/ptr/const_ptr.rs:LL:CC:
align_offset: align is not a power-of-two
Caught panic message (&str): align_offset: align is not a power-of-two

thread 'main' panicked at $DIR/catch_panic.rs:LL:CC:
assertion failed: false
Caught panic message (&str): assertion failed: false

thread 'main' panicked at $DIR/catch_panic.rs:LL:CC:
assertion failed: false
Caught panic message (&str): assertion failed: false
Expand Down
2 changes: 2 additions & 0 deletions src/tools/miri/tests/pass/panic/concurrent-panic.stderr
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
Thread 1 starting, will block on mutex
Thread 1 reported it has started

thread '<unnamed>' panicked at $DIR/concurrent-panic.rs:LL:CC:
panic in thread 2
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Thread 2 blocking on thread 1
Thread 2 reported it has started
Unlocking mutex

thread '<unnamed>' panicked at $DIR/concurrent-panic.rs:LL:CC:
panic in thread 1
Thread 1 has exited
Expand Down
2 changes: 2 additions & 0 deletions src/tools/miri/tests/pass/panic/nested_panic_caught.stderr
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

thread 'main' panicked at $DIR/nested_panic_caught.rs:LL:CC:
once
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

thread 'main' panicked at $DIR/nested_panic_caught.rs:LL:CC:
twice
stack backtrace:
2 changes: 2 additions & 0 deletions src/tools/miri/tests/pass/panic/thread_panic.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@

thread '<unnamed>' panicked at $DIR/thread_panic.rs:LL:CC:
Hello!
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

thread 'childthread' panicked at $DIR/thread_panic.rs:LL:CC:
Hello, world!
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
error: internal compiler error: compiler/rustc_const_eval/src/interpret/step.rs:LL:CC: SizeOf MIR operator called for unsized type dyn Debug
--> $SRC_DIR/core/src/mem/mod.rs:LL:COL


Box<dyn Any>
query stack during panic:
#0 [eval_to_allocation_raw] const-evaluating + checking `<impl at $DIR/issue-80742.rs:26:1: 28:32>::{constant#0}`
Expand Down
1 change: 1 addition & 0 deletions tests/ui/consts/const-eval/const-eval-query-stack.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ error: internal compiler error[E0080]: evaluation of constant value failed
LL | const X: i32 = 1 / 0;
| ^^^^^ attempt to divide `1_i32` by zero


query stack during panic:
#0 [eval_to_allocation_raw] const-evaluating + checking `X`
#1 [eval_to_const_value_raw] simplifying constant for the type system `X`
Expand Down
1 change: 1 addition & 0 deletions tests/ui/extern/extern-types-field-offset.run.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

thread 'main' panicked at library/core/src/panicking.rs:$LINE:$COL:
attempted to compute the size or alignment of extern type `Opaque`
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Expand Down
1 change: 1 addition & 0 deletions tests/ui/extern/extern-types-size_of_val.align.run.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

thread 'main' panicked at library/core/src/panicking.rs:$LINE:$COL:
attempted to compute the size or alignment of extern type `A`
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Expand Down
1 change: 1 addition & 0 deletions tests/ui/extern/extern-types-size_of_val.size.run.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

thread 'main' panicked at library/core/src/panicking.rs:$LINE:$COL:
attempted to compute the size or alignment of extern type `A`
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Expand Down
1 change: 1 addition & 0 deletions tests/ui/higher-ranked/trait-bounds/future.current.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

error: the compiler unexpectedly panicked. this is a bug.

query stack during panic:
Expand Down
1 change: 1 addition & 0 deletions tests/ui/hygiene/panic-location.run.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

thread 'main' panicked at library/alloc/src/raw_vec.rs:LL:CC:
capacity overflow
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

thread 'main' panicked at $DIR/const-eval-select-backtrace-std.rs:6:8:
byte index 1 is out of bounds of ``
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
1 change: 1 addition & 0 deletions tests/ui/intrinsics/const-eval-select-backtrace.run.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

thread 'main' panicked at $DIR/const-eval-select-backtrace.rs:15:5:
Aaah!
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
1 change: 1 addition & 0 deletions tests/ui/intrinsics/not-overridden.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ error: must be overridden by codegen backend, but isn't
LL | unsafe { const_deallocate(std::ptr::null_mut(), 0, 0) }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


query stack during panic:
end of query stack
error: aborting due to 1 previous error
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/issues/issue-87707.run.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@

thread 'main' panicked at $DIR/issue-87707.rs:14:24:
Here Once instance is poisoned.
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

thread 'main' panicked at $DIR/issue-87707.rs:16:7:
Once instance has previously been poisoned
1 change: 1 addition & 0 deletions tests/ui/layout/valid_range_oob.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

257 > 255
error: the compiler unexpectedly panicked. this is a bug.

Expand Down
1 change: 1 addition & 0 deletions tests/ui/macros/assert-long-condition.run.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

thread 'main' panicked at $DIR/assert-long-condition.rs:7:5:
assertion failed: 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + 15 + 16 + 17 + 18
+ 19 + 20 + 21 + 22 + 23 + 24 + 25 == 0
Expand Down
1 change: 1 addition & 0 deletions tests/ui/mir/lint/storage-live.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ note: delayed at compiler/rustc_mir_transform/src/lint.rs:97:26 - disabled backt
LL | StorageLive(a);
| ^^^^^^^^^^^^^^


aborting due to `-Z treat-err-as-bug=1`
error: the compiler unexpectedly panicked. this is a bug.

Expand Down
1 change: 1 addition & 0 deletions tests/ui/panics/default-backtrace-ice.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ LL | fn main() { missing_ident; }
| ^^^^^^^^^^^^^ not found in this scope



aborting due to `-Z treat-err-as-bug=1`
stack backtrace:
(end_short_backtrace)
Expand Down
1 change: 1 addition & 0 deletions tests/ui/panics/fmt-only-once.run.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
fmt

thread 'main' panicked at $DIR/fmt-only-once.rs:20:5:
PrintOnFmt
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

thread 'main' panicked at $DIR/issue-47429-short-backtraces.rs:23:5:
explicit panic
stack backtrace:
Expand Down
1 change: 1 addition & 0 deletions tests/ui/panics/issue-47429-short-backtraces.v0.run.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

thread 'main' panicked at $DIR/issue-47429-short-backtraces.rs:23:5:
explicit panic
stack backtrace:
Expand Down
1 change: 1 addition & 0 deletions tests/ui/panics/location-detail-panic-no-column.run.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

thread 'main' panicked at $DIR/location-detail-panic-no-column.rs:7:0:
column-redacted
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Loading

0 comments on commit 4962cfe

Please sign in to comment.