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

+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}\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

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

+2
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`
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.
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.
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

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

tests/ui/intrinsics/not-overridden.stderr

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

7+
8+
79
query stack during panic:
810
end of query stack
911
error: aborting due to 1 previous error
+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
12
thread 'main' panicked at $DIR/issue-87707.rs:14:24:
23
Here Once instance is poisoned.
4+
35
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
6+
47
thread 'main' panicked at $DIR/issue-87707.rs:16:7:
58
Once instance has previously been poisoned
9+

tests/ui/layout/valid_range_oob.stderr

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

46
query stack during panic:
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
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
5+
46
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

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

+2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ 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`
16+
1517
error: the compiler unexpectedly panicked. this is a bug.
1618

1719
query stack during panic:
+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
fmt
2+
23
thread 'main' panicked at $DIR/fmt-only-once.rs:20:5:
34
PrintOnFmt
5+
46
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
12
thread 'main' panicked at $DIR/location-detail-panic-no-column.rs:7:0:
23
column-redacted
4+
35
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
12
thread 'main' panicked at <redacted>:7:5:
23
file-redacted
4+
35
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
12
thread 'main' panicked at $DIR/location-detail-panic-no-line.rs:0:5:
23
line-redacted
4+
35
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
12
thread 'main' panicked at <redacted>:0:0:
23
no location info
4+
35
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
12
thread 'main' panicked at <redacted>:8:9:
23
called `Option::unwrap()` on a `None` value
4+
35
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
+4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
12
thread 'main' panicked at $DIR/panic-in-ffi.rs:13:5:
23
Test
4+
35
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
6+
47
thread 'main' panicked at library/core/src/panicking.rs:$LINE:$COL:
58
panic in a function that cannot unwind
9+
610
stack backtrace:
711
thread caused non-unwinding panic. aborting.

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

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
12
at $DIR/auxiliary/test-macros.rs:43:5:
23
panic-derive
4+
35
error: proc-macro derive panicked
46
--> $DIR/load-panic-backtrace.rs:11:10
57
|

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ LL | #[repr(C)]
1616
LL | struct Dealigned<T>(u8, T);
1717
| ^
1818
|
19-
= Box<dyn Any>
19+
=
20+
Box<dyn Any>
21+
2022
query stack during panic:
2123
#0 [mir_const] preparing `<impl at $DIR/multiple_definitions_attribute_merging.rs:15:10: 15:19>::eq` for borrow checking
2224
#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

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ LL | #[derive(PartialEq)]
77
LL | struct Dealigned<T>(u8, T);
88
| ^
99
|
10-
= Box<dyn Any>
10+
=
11+
Box<dyn Any>
12+
1113
query stack during panic:
1214
#0 [mir_const] preparing `<impl at $DIR/proc_macro_generated_packed.rs:15:10: 15:19>::eq` for borrow checking
1315
#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

+6
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,25 @@ foo2 --- FAILED
99
failures:
1010

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

1618
---- foo stdout ----
19+
1720
thread 'foo' panicked at $DIR/terse.rs:17:5:
1821
explicit panic
1922

23+
2024
---- foo2 stdout ----
25+
2126
thread 'foo2' panicked at $DIR/terse.rs:22:5:
2227
explicit panic
2328

2429

30+
2531
failures:
2632
abc
2733
foo
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
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
6+
57
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
8+
69
thread 'main' panicked at $DIR/test-panic-abort-nocapture.rs:28:5:
710
assertion `left == right` failed
811
left: 2
912
right: 4
13+
1014
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
1115
testing321

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

+2
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ 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
2324
right: 5
25+
2426
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
2527

2628

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

+2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ fee
1010
fie
1111
foe
1212
fum
13+
1314
thread 'thready_fail' panicked at $DIR/test-thread-capture.rs:32:5:
1415
explicit panic
16+
1517
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
1618

1719

Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
12
thread 'thready_fail' panicked at $DIR/test-thread-nocapture.rs:32:5:
23
explicit panic
4+
35
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

tests/ui/track-diagnostics/track.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ 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>
30+
2931
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
3032

3133
note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md

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

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ 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+
8+
79
error: the compiler unexpectedly panicked. this is a bug.
810

911
query stack during panic:

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

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

7+
8+
79
error: the compiler unexpectedly panicked. this is a bug.
810

911
query stack during panic:

0 commit comments

Comments
 (0)