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

+15-1
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

+1
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

+1
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

+1
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

+1
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

+1
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

+1
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:
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
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
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

tests/ui/intrinsics/not-overridden.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ error: must be overridden by codegen backend, but isn't
44
LL | unsafe { const_deallocate(std::ptr::null_mut(), 0, 0) }
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66

7+
78
query stack during panic:
89
end of query stack
910
error: aborting due to 1 previous error
+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
12
thread 'main' panicked at $DIR/issue-87707.rs:14:24:
23
Here Once instance is poisoned.
34
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
5+
46
thread 'main' panicked at $DIR/issue-87707.rs:16:7:
57
Once instance has previously been poisoned

tests/ui/layout/valid_range_oob.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
257 > 255
23
error: the compiler unexpectedly panicked. this is a bug.
34

tests/ui/macros/assert-long-condition.run.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
thread 'main' panicked at $DIR/assert-long-condition.rs:7:5:
23
assertion failed: 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + 15 + 16 + 17 + 18
34
+ 19 + 20 + 21 + 22 + 23 + 24 + 25 == 0

tests/ui/mir/lint/storage-live.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ note: delayed at compiler/rustc_mir_transform/src/lint.rs:97:26 - disabled backt
1111
LL | StorageLive(a);
1212
| ^^^^^^^^^^^^^^
1313

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

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
fmt
2+
23
thread 'main' panicked at $DIR/fmt-only-once.rs:20:5:
34
PrintOnFmt
45
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
thread 'main' panicked at $DIR/location-detail-panic-no-column.rs:7:0:
23
column-redacted
34
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
thread 'main' panicked at <redacted>:7:5:
23
file-redacted
34
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
thread 'main' panicked at $DIR/location-detail-panic-no-line.rs:0:5:
23
line-redacted
34
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
thread 'main' panicked at <redacted>:0:0:
23
no location info
34
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
thread 'main' panicked at <redacted>:8:9:
23
called `Option::unwrap()` on a `None` value
34
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

tests/ui/panics/panic-in-ffi.run.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
12
thread 'main' panicked at $DIR/panic-in-ffi.rs:13:5:
23
Test
34
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
5+
46
thread 'main' panicked at library/core/src/panicking.rs:$LINE:$COL:
57
panic in a function that cannot unwind
68
stack backtrace:

tests/ui/proc-macro/load-panic-backtrace.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
at $DIR/auxiliary/test-macros.rs:43:5:
23
panic-derive
34
error: proc-macro derive panicked

tests/ui/process/multi-panic.rs

+23-11
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,21 @@ fn check_for_no_backtrace(test: std::process::Output) {
88
let err = String::from_utf8_lossy(&test.stderr);
99
let mut it = err.lines();
1010

11+
assert_eq!(it.next().map(|l| l.is_empty()), Some(true));
1112
assert_eq!(it.next().map(|l| l.starts_with("thread '<unnamed>' panicked")), Some(true));
1213
assert_eq!(it.next().is_some(), true);
13-
assert_eq!(it.next(), Some("note: run with `RUST_BACKTRACE=1` \
14-
environment variable to display a backtrace"));
14+
assert_eq!(it.next().map(|l| l.is_empty()), Some(true));
15+
assert_eq!(
16+
it.next(),
17+
Some(
18+
"note: run with `RUST_BACKTRACE=1` \
19+
environment variable to display a backtrace"
20+
)
21+
);
22+
assert_eq!(it.next().map(|l| l.is_empty()), Some(true));
1523
assert_eq!(it.next().map(|l| l.starts_with("thread 'main' panicked at")), Some(true));
1624
assert_eq!(it.next().is_some(), true);
25+
assert_eq!(it.next().map(|l| l.is_empty()), Some(true));
1726
assert_eq!(it.next(), None);
1827
}
1928

@@ -22,19 +31,22 @@ fn main() {
2231
if args.len() > 1 && args[1] == "run_test" {
2332
let _ = std::thread::spawn(|| {
2433
panic!();
25-
}).join();
34+
})
35+
.join();
2636

2737
panic!();
2838
} else {
29-
let test = std::process::Command::new(&args[0]).arg("run_test")
30-
.env_remove("RUST_BACKTRACE")
31-
.output()
32-
.unwrap();
39+
let test = std::process::Command::new(&args[0])
40+
.arg("run_test")
41+
.env_remove("RUST_BACKTRACE")
42+
.output()
43+
.unwrap();
3344
check_for_no_backtrace(test);
34-
let test = std::process::Command::new(&args[0]).arg("run_test")
35-
.env("RUST_BACKTRACE","0")
36-
.output()
37-
.unwrap();
45+
let test = std::process::Command::new(&args[0])
46+
.arg("run_test")
47+
.env("RUST_BACKTRACE", "0")
48+
.output()
49+
.unwrap();
3850
check_for_no_backtrace(test);
3951
}
4052
}

tests/ui/resolve/multiple_definitions_attribute_merging.stderr

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ LL | #[repr(C)]
1616
LL | struct Dealigned<T>(u8, T);
1717
| ^
1818
|
19-
= Box<dyn Any>
19+
=
20+
Box<dyn Any>
2021
query stack during panic:
2122
#0 [mir_const] preparing `<impl at $DIR/multiple_definitions_attribute_merging.rs:15:10: 15:19>::eq` for borrow checking
2223
#1 [mir_promoted] promoting constants in MIR for `<impl at $DIR/multiple_definitions_attribute_merging.rs:15:10: 15:19>::eq`

tests/ui/resolve/proc_macro_generated_packed.stderr

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ LL | #[derive(PartialEq)]
77
LL | struct Dealigned<T>(u8, T);
88
| ^
99
|
10-
= Box<dyn Any>
10+
=
11+
Box<dyn Any>
1112
query stack during panic:
1213
#0 [mir_const] preparing `<impl at $DIR/proc_macro_generated_packed.rs:15:10: 15:19>::eq` for borrow checking
1314
#1 [mir_promoted] promoting constants in MIR for `<impl at $DIR/proc_macro_generated_packed.rs:15:10: 15:19>::eq`

tests/ui/test-attrs/terse.run.stdout

+3
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,18 @@ foo2 --- FAILED
99
failures:
1010

1111
---- abc stdout ----
12+
1213
thread 'abc' panicked at $DIR/terse.rs:12:5:
1314
explicit panic
1415
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
1516

1617
---- foo stdout ----
18+
1719
thread 'foo' panicked at $DIR/terse.rs:17:5:
1820
explicit panic
1921

2022
---- foo2 stdout ----
23+
2124
thread 'foo2' panicked at $DIR/terse.rs:22:5:
2225
explicit panic
2326

tests/ui/test-attrs/test-panic-abort-nocapture.run.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
12
thread 'main' panicked at $DIR/test-panic-abort-nocapture.rs:34:5:
23
assertion `left == right` failed
34
left: 2
45
right: 4
56
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
7+
68
thread 'main' panicked at $DIR/test-panic-abort-nocapture.rs:28:5:
79
assertion `left == right` failed
810
left: 2

tests/ui/test-attrs/test-panic-abort.run.stdout

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ hello, world
1717
testing123
1818
---- it_fails stderr ----
1919
testing321
20+
2021
thread 'main' panicked at $DIR/test-panic-abort.rs:39:5:
2122
assertion `left == right` failed
2223
left: 2

tests/ui/test-attrs/test-thread-capture.run.stdout

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ fee
1010
fie
1111
foe
1212
fum
13+
1314
thread 'thready_fail' panicked at $DIR/test-thread-capture.rs:32:5:
1415
explicit panic
1516
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
thread 'thready_fail' panicked at $DIR/test-thread-nocapture.rs:32:5:
23
explicit panic
34
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

tests/ui/track-diagnostics/track.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ LL | break rust
2424
= note: rustc $VERSION running on $TARGET
2525
= note: compiler flags: ... -Z ui-testing ... -Z track-diagnostics
2626

27+
2728
thread 'rustc' panicked at compiler/rustc_hir_typeck/src/lib.rs:LL:CC:
2829
Box<dyn Any>
2930
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

tests/ui/treat-err-as-bug/err.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ error: internal compiler error[E0080]: could not evaluate static initializer
44
LL | pub static C: u32 = 0 - 1;
55
| ^^^^^ attempt to compute `0_u32 - 1_u32`, which would overflow
66

7+
78
error: the compiler unexpectedly panicked. this is a bug.
89

910
query stack during panic:

tests/ui/treat-err-as-bug/span_delayed_bug.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ error: internal compiler error: delayed bug triggered by #[rustc_error(delayed_b
44
LL | fn main() {}
55
| ^^^^^^^^^
66

7+
78
error: the compiler unexpectedly panicked. this is a bug.
89

910
query stack during panic:

0 commit comments

Comments
 (0)