diff --git a/library/std/src/panicking.rs b/library/std/src/panicking.rs index b20e5464e6e01..8ee5a047f1ce3 100644 --- a/library/std/src/panicking.rs +++ b/library/std/src/panicking.rs @@ -260,15 +260,23 @@ pub fn panic_hook_with_disk_dump(info: &PanicInfo<'_>, path: Option<&crate::path let msg = match info.payload().downcast_ref::<&'static str>() { Some(s) => *s, None => match info.payload().downcast_ref::() { - Some(s) => &s[..], + Some(s) => s.as_str(), None => "Box", }, }; let thread = thread_info::current_thread(); - let name = thread.as_ref().and_then(|t| t.name()).unwrap_or(""); let write = |err: &mut dyn crate::io::Write, backtrace: Option| { - let _ = writeln!(err, "thread '{name}' panicked at {location}:\n{msg}"); + let _ = match thread.as_ref().map(|t| t.name().ok_or(t)) { + Some(Ok(name)) => { + writeln!(err, "thread '{name}' panicked at {location}:\n{msg}") + } + Some(Err(t)) => { + let id = t.id().as_u64(); + writeln!(err, "thread '' (id {id}) panicked at {location}:\n{msg}",) + } + None => writeln!(err, "thread '' panicked at {location}:\n{msg}"), + }; static FIRST_PANIC: AtomicBool = AtomicBool::new(true); diff --git a/src/tools/miri/tests/fail/concurrency/unwind_top_of_stack.rs b/src/tools/miri/tests/fail/concurrency/unwind_top_of_stack.rs index 4704cfed03938..00335542ad60b 100644 --- a/src/tools/miri/tests/fail/concurrency/unwind_top_of_stack.rs +++ b/src/tools/miri/tests/fail/concurrency/unwind_top_of_stack.rs @@ -1,6 +1,7 @@ //@ignore-target-windows: No libc on Windows //@compile-flags: -Zmiri-disable-abi-check +//@normalize-stderr-test: "thread '' \(id [0-9]+\)" -> "thread ''" //! Unwinding past the top frame of a stack is Undefined Behavior. diff --git a/src/tools/miri/tests/pass/concurrency/simple.rs b/src/tools/miri/tests/pass/concurrency/simple.rs index 556e0a24769d7..bb6528beedc90 100644 --- a/src/tools/miri/tests/pass/concurrency/simple.rs +++ b/src/tools/miri/tests/pass/concurrency/simple.rs @@ -1,4 +1,5 @@ //@compile-flags: -Zmiri-strict-provenance +//@normalize-stderr-test: "thread '' \(id [0-9]+\)" -> "thread ''" use std::thread; diff --git a/src/tools/miri/tests/pass/panic/concurrent-panic.rs b/src/tools/miri/tests/pass/panic/concurrent-panic.rs index 7cc1e2a973fd6..003ea81800376 100644 --- a/src/tools/miri/tests/pass/panic/concurrent-panic.rs +++ b/src/tools/miri/tests/pass/panic/concurrent-panic.rs @@ -1,5 +1,6 @@ // We are making scheduler assumptions here. //@compile-flags: -Zmiri-preemption-rate=0 +//@normalize-stderr-test: "thread '' \(id [0-9]+\)" -> "thread ''" //! Cause a panic in one thread while another thread is unwinding. This checks //! that separate threads have their own panicking state. diff --git a/tests/ui/panics/panic-task-name-none.rs b/tests/ui/panics/panic-task-name-none.rs index 3fb0f3412f6b8..7bc5282309baf 100644 --- a/tests/ui/panics/panic-task-name-none.rs +++ b/tests/ui/panics/panic-task-name-none.rs @@ -1,14 +1,16 @@ +// Test panic error messages for unnamed threads + // run-fail -// error-pattern:thread '' panicked +// regex-error-pattern:thread '' \(id \d+\) panicked // error-pattern:test // ignore-emscripten Needs threads use std::thread; fn main() { - let r: Result<(), _> = thread::spawn(move || { - panic!("test"); - }) - .join(); - assert!(r.is_ok()); + let _: () = thread::spawn(move || { + panic!("test"); + }) + .join() + .unwrap(); } diff --git a/tests/ui/proc-macro/load-panic-backtrace.rs b/tests/ui/proc-macro/load-panic-backtrace.rs index bcdcb704a7538..d7be394f70ff8 100644 --- a/tests/ui/proc-macro/load-panic-backtrace.rs +++ b/tests/ui/proc-macro/load-panic-backtrace.rs @@ -1,7 +1,7 @@ // aux-build:test-macros.rs // compile-flags: -Z proc-macro-backtrace // rustc-env:RUST_BACKTRACE=0 -// normalize-stderr-test "thread '.*' panicked " -> "" +// normalize-stderr-test "thread '.*' (\(id \d+\) )?panicked " -> "" // normalize-stderr-test "note:.*RUST_BACKTRACE=1.*\n" -> "" // needs-unwind proc macro panics to report errors diff --git a/tests/ui/process/multi-panic.rs b/tests/ui/process/multi-panic.rs index c240dc18fd366..2a8f28d22ca28 100644 --- a/tests/ui/process/multi-panic.rs +++ b/tests/ui/process/multi-panic.rs @@ -8,7 +8,7 @@ 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.starts_with("thread '' panicked")), Some(true)); + assert_eq!(it.next().map(|l| l.starts_with("thread '' (id ")), 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"));