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 f24ddfd
Show file tree
Hide file tree
Showing 43 changed files with 86 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,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
1 change: 1 addition & 0 deletions tests/ui/panics/location-detail-panic-no-file.run.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

thread 'main' panicked at <redacted>:7:5:
file-redacted
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
1 change: 1 addition & 0 deletions tests/ui/panics/location-detail-panic-no-line.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-line.rs:0:5:
line-redacted
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 <redacted>:0:0:
no location info
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
1 change: 1 addition & 0 deletions tests/ui/panics/location-detail-unwrap-no-file.run.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

thread 'main' panicked at <redacted>:8:9:
called `Option::unwrap()` on a `None` value
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
3 changes: 3 additions & 0 deletions tests/ui/panics/panic-in-cleanup.run.stderr
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@

thread 'main' panicked at $DIR/panic-in-cleanup.rs:22:5:
explicit panic
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

thread 'main' panicked at $DIR/panic-in-cleanup.rs:16:9:
BOOM
stack backtrace:

thread 'main' panicked at library/core/src/panicking.rs:$LINE:$COL:
panic in a destructor during cleanup
thread caused non-unwinding panic. aborting.
2 changes: 2 additions & 0 deletions tests/ui/panics/panic-in-ffi.run.stderr
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

thread 'main' panicked at $DIR/panic-in-ffi.rs:13:5:
Test
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

thread 'main' panicked at library/core/src/panicking.rs:$LINE:$COL:
panic in a function that cannot unwind
stack backtrace:
Expand Down
1 change: 1 addition & 0 deletions tests/ui/panics/runtime-switch.legacy.run.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

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

thread 'main' panicked at $DIR/runtime-switch.rs:26:5:
explicit panic
stack backtrace:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

thread 'main' panicked at $DIR/short-ice-remove-middle-frames-2.rs:56:5:
debug!!!
stack backtrace:
Expand Down
1 change: 1 addition & 0 deletions tests/ui/panics/short-ice-remove-middle-frames.run.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

thread 'main' panicked at $DIR/short-ice-remove-middle-frames.rs:52:5:
debug!!!
stack backtrace:
Expand Down
1 change: 1 addition & 0 deletions tests/ui/proc-macro/load-panic-backtrace.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

at $DIR/auxiliary/test-macros.rs:43:5:
panic-derive
error: proc-macro derive panicked
Expand Down
32 changes: 21 additions & 11 deletions tests/ui/process/multi-panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,17 @@ fn check_for_no_backtrace(test: std::process::Output) {
let err = String::from_utf8_lossy(&test.stderr);
let mut it = err.lines();

assert_eq!(it.next().map(|l| l.is_empty()), Some(true));
assert_eq!(it.next().map(|l| l.starts_with("thread '<unnamed>' panicked")), Some(true));
assert_eq!(it.next().is_some(), true);
assert_eq!(it.next(), Some("note: run with `RUST_BACKTRACE=1` \
environment variable to display a backtrace"));
assert_eq!(
it.next(),
Some(
"note: run with `RUST_BACKTRACE=1` \
environment variable to display a backtrace"
)
);
assert_eq!(it.next().map(|l| l.is_empty()), Some(true));
assert_eq!(it.next().map(|l| l.starts_with("thread 'main' panicked at")), Some(true));
assert_eq!(it.next().is_some(), true);
assert_eq!(it.next(), None);
Expand All @@ -22,19 +29,22 @@ fn main() {
if args.len() > 1 && args[1] == "run_test" {
let _ = std::thread::spawn(|| {
panic!();
}).join();
})
.join();

panic!();
} else {
let test = std::process::Command::new(&args[0]).arg("run_test")
.env_remove("RUST_BACKTRACE")
.output()
.unwrap();
let test = std::process::Command::new(&args[0])
.arg("run_test")
.env_remove("RUST_BACKTRACE")
.output()
.unwrap();
check_for_no_backtrace(test);
let test = std::process::Command::new(&args[0]).arg("run_test")
.env("RUST_BACKTRACE","0")
.output()
.unwrap();
let test = std::process::Command::new(&args[0])
.arg("run_test")
.env("RUST_BACKTRACE", "0")
.output()
.unwrap();
check_for_no_backtrace(test);
}
}
1 change: 1 addition & 0 deletions tests/ui/process/println-with-broken-pipe.run.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

thread 'main' panicked at library/std/src/io/stdio.rs:LL:CC:
failed printing to stdout: Broken pipe (os error 32)
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ LL | #[repr(C)]
LL | struct Dealigned<T>(u8, T);
| ^
|
= Box<dyn Any>
=
Box<dyn Any>
query stack during panic:
#0 [mir_const] preparing `<impl at $DIR/multiple_definitions_attribute_merging.rs:15:10: 15:19>::eq` for borrow checking
#1 [mir_promoted] promoting constants in MIR for `<impl at $DIR/multiple_definitions_attribute_merging.rs:15:10: 15:19>::eq`
Expand Down
3 changes: 2 additions & 1 deletion tests/ui/resolve/proc_macro_generated_packed.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ LL | #[derive(PartialEq)]
LL | struct Dealigned<T>(u8, T);
| ^
|
= Box<dyn Any>
=
Box<dyn Any>
query stack during panic:
#0 [mir_const] preparing `<impl at $DIR/proc_macro_generated_packed.rs:15:10: 15:19>::eq` for borrow checking
#1 [mir_promoted] promoting constants in MIR for `<impl at $DIR/proc_macro_generated_packed.rs:15:10: 15:19>::eq`
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/test-attrs/terse.run.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@ foo2 --- FAILED
failures:

---- abc stdout ----

thread 'abc' panicked at $DIR/terse.rs:12:5:
explicit panic
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

---- foo stdout ----

thread 'foo' panicked at $DIR/terse.rs:17:5:
explicit panic

---- foo2 stdout ----

thread 'foo2' panicked at $DIR/terse.rs:22:5:
explicit panic

Expand Down
2 changes: 2 additions & 0 deletions tests/ui/test-attrs/test-panic-abort-nocapture.run.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@

thread 'main' panicked at $DIR/test-panic-abort-nocapture.rs:34:5:
assertion `left == right` failed
left: 2
right: 4
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

thread 'main' panicked at $DIR/test-panic-abort-nocapture.rs:28:5:
assertion `left == right` failed
left: 2
Expand Down
1 change: 1 addition & 0 deletions tests/ui/test-attrs/test-panic-abort.run.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ hello, world
testing123
---- it_fails stderr ----
testing321

thread 'main' panicked at $DIR/test-panic-abort.rs:39:5:
assertion `left == right` failed
left: 2
Expand Down
1 change: 1 addition & 0 deletions tests/ui/test-attrs/test-thread-capture.run.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ fee
fie
foe
fum

thread 'thready_fail' panicked at $DIR/test-thread-capture.rs:32:5:
explicit panic
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Expand Down
1 change: 1 addition & 0 deletions tests/ui/test-attrs/test-thread-nocapture.run.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

thread 'thready_fail' panicked at $DIR/test-thread-nocapture.rs:32:5:
explicit panic
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
1 change: 1 addition & 0 deletions tests/ui/track-diagnostics/track.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ LL | break rust
= note: rustc $VERSION running on $TARGET
= note: compiler flags: ... -Z ui-testing ... -Z track-diagnostics


thread 'rustc' panicked at compiler/rustc_hir_typeck/src/lib.rs:LL:CC:
Box<dyn Any>
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Expand Down
1 change: 1 addition & 0 deletions tests/ui/treat-err-as-bug/err.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ error: internal compiler error[E0080]: could not evaluate static initializer
LL | pub static C: u32 = 0 - 1;
| ^^^^^ attempt to compute `0_u32 - 1_u32`, which would overflow


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

query stack during panic:
Expand Down
1 change: 1 addition & 0 deletions tests/ui/treat-err-as-bug/span_delayed_bug.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ error: internal compiler error: delayed bug triggered by #[rustc_error(delayed_b
LL | fn main() {}
| ^^^^^^^^^


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

query stack during panic:
Expand Down

0 comments on commit f24ddfd

Please sign in to comment.