Skip to content

Commit 0681a96

Browse files
authored
Rollup merge of rust-lang#106321 - compiler-errors:delayed-bug-backtrace, r=Nilstrieb
Collect and emit proper backtraces for `delay_span_bug`s This is a follow-up to rust-lang#106317, which addresses this comment (rust-lang#106267 (comment)) which notes that `delay_span_bug`s' backtraces are nonsense. Captures and emits the backtrace of the delayed span bug when it's *created*, rather than using the backtrace of the place where delayed bugs are flushed. --- To test, I delayed a span bug during HIR typeck, specifically in `typeck_with_fallback`... Before, note `flush_delayed` on frame 18. This is at the end of the compilation session, far from where the bug is being delayed. ``` error: internal compiler error: test --> /home/ubuntu/test.rs:1:1 | 1 | fn main() {} | ^^^^^^^^^ | = note: delayed at compiler/rustc_hir_typeck/src/lib.rs:196:14 thread 'rustc' panicked at 'Box<dyn Any>', compiler/rustc_errors/src/lib.rs:1634:13 stack backtrace: 0: 0x7f9c3ec69dd1 - std::backtrace_rs::backtrace::libunwind::trace::h26056f81198c6594 at /home/ubuntu/rust2/library/std/src/../../backtrace/src/backtrace/libunwind.rs:93:5 1: 0x7f9c3ec69dd1 - std::backtrace_rs::backtrace::trace_unsynchronized::hacfb345a0c6d5bb1 at /home/ubuntu/rust2/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5 2: 0x7f9c3ec69dd1 - std::sys_common::backtrace::_print_fmt::h18ea6016ac8030f3 at /home/ubuntu/rust2/library/std/src/sys_common/backtrace.rs:65:5 3: 0x7f9c3ec69dd1 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::he35dde201d0c2d09 at /home/ubuntu/rust2/library/std/src/sys_common/backtrace.rs:44:22 4: 0x7f9c3ecee308 - core::fmt::write::h094ad263467a053c at /home/ubuntu/rust2/library/core/src/fmt/mod.rs:1208:17 5: 0x7f9c3ec8aaf1 - std::io::Write::write_fmt::hd47b4e2324b4d9b7 at /home/ubuntu/rust2/library/std/src/io/mod.rs:1682:15 6: 0x7f9c3ec69bfa - std::sys_common::backtrace::_print::h43044162653a17fc at /home/ubuntu/rust2/library/std/src/sys_common/backtrace.rs:47:5 7: 0x7f9c3ec69bfa - std::sys_common::backtrace::print::hc8605da258fa5aeb at /home/ubuntu/rust2/library/std/src/sys_common/backtrace.rs:34:9 8: 0x7f9c3ec4db87 - std::panicking::default_hook::{{closure}}::h9e37f23f75122a15 9: 0x7f9c3ec4d97b - std::panicking::default_hook::h602873a063f84da2 at /home/ubuntu/rust2/library/std/src/panicking.rs:286:9 10: 0x7f9c3f6672b2 - <alloc[48d7b30605060536]::boxed::Box<dyn for<'a, 'b> core[672e3947e150d6c6]::ops::function::Fn<(&'a core[672e3947e150d6c6]::panic::panic_info::PanicInfo<'b>,), Output = ()> + core[672e3947e150d6c6]::marker::Send + core[672e3947e150d6c6]::marker::Sync> as core[672e3947e150d6c6]::ops::function::Fn<(&core[672e3947e150d6c6]::panic::panic_info::PanicInfo,)>>::call at /home/ubuntu/rust2/library/alloc/src/boxed.rs:2002:9 11: 0x7f9c3f6672b2 - rustc_driver[f5b6d32d8905ecdd]::DEFAULT_HOOK::{closure#0}::{closure#0} at /home/ubuntu/rust2/compiler/rustc_driver/src/lib.rs:1204:17 12: 0x7f9c3ec4e0d3 - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::hfd13333ca953ae8e at /home/ubuntu/rust2/library/alloc/src/boxed.rs:2002:9 13: 0x7f9c3ec4e0d3 - std::panicking::rust_panic_with_hook::h45753e10264ebe7e at /home/ubuntu/rust2/library/std/src/panicking.rs:692:13 14: 0x7f9c422a1aa3 - std[3330b4673efabfce]::panicking::begin_panic::<rustc_errors[1b15f4e7e49d1fd5]::ExplicitBug>::{closure#0} at /home/ubuntu/rust2/library/std/src/panicking.rs:608:9 15: 0x7f9c422a1a46 - std[3330b4673efabfce]::sys_common::backtrace::__rust_end_short_backtrace::<std[3330b4673efabfce]::panicking::begin_panic<rustc_errors[1b15f4e7e49d1fd5]::ExplicitBug>::{closure#0}, !> at /home/ubuntu/rust2/library/std/src/sys_common/backtrace.rs:137:18 16: 0x7f9c3f63a996 - std[3330b4673efabfce]::panicking::begin_panic::<rustc_errors[1b15f4e7e49d1fd5]::ExplicitBug> at /home/ubuntu/rust2/library/std/src/panicking.rs:607:12 17: 0x7f9c4227a496 - std[3330b4673efabfce]::panic::panic_any::<rustc_errors[1b15f4e7e49d1fd5]::ExplicitBug> at /home/ubuntu/rust2/library/std/src/panic.rs:61:5 18: 0x7f9c4227cdf7 - <rustc_errors[1b15f4e7e49d1fd5]::HandlerInner>::flush_delayed::<alloc[48d7b30605060536]::vec::Vec<rustc_errors[1b15f4e7e49d1fd5]::diagnostic::Diagnostic>, &str, rustc_errors[1b15f4e7e49d1fd5]::ExplicitBug> at /home/ubuntu/rust2/compiler/rustc_errors/src/lib.rs:1634:13 19: 0x7f9c422498cf - <rustc_errors[1b15f4e7e49d1fd5]::Handler>::flush_delayed at /home/ubuntu/rust2/compiler/rustc_errors/src/lib.rs:1225:9 [ FRAMES INTENTIONALLY OMITTED ] 44: 0x7f9c3f6f3584 - <std[3330b4673efabfce]::thread::Builder>::spawn_unchecked_::<rustc_interface[947706ead88047d0]::util::run_in_thread_pool_with_globals<rustc_interface[947706ead88047d0]::interface::run_compiler<core[672e3947e150d6c6]::result::Result<(), rustc_errors[1b15f4e7e49d1fd5]::ErrorGuaranteed>, rustc_driver[f5b6d32d8905ecdd]::run_compiler::{closure#1}>::{closure#0}, core[672e3947e150d6c6]::result::Result<(), rustc_errors[1b15f4e7e49d1fd5]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[672e3947e150d6c6]::result::Result<(), rustc_errors[1b15f4e7e49d1fd5]::ErrorGuaranteed>>::{closure#1} at /home/ubuntu/rust2/library/std/src/thread/mod.rs:549:30 45: 0x7f9c3f6f3584 - <<std[3330b4673efabfce]::thread::Builder>::spawn_unchecked_<rustc_interface[947706ead88047d0]::util::run_in_thread_pool_with_globals<rustc_interface[947706ead88047d0]::interface::run_compiler<core[672e3947e150d6c6]::result::Result<(), rustc_errors[1b15f4e7e49d1fd5]::ErrorGuaranteed>, rustc_driver[f5b6d32d8905ecdd]::run_compiler::{closure#1}>::{closure#0}, core[672e3947e150d6c6]::result::Result<(), rustc_errors[1b15f4e7e49d1fd5]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[672e3947e150d6c6]::result::Result<(), rustc_errors[1b15f4e7e49d1fd5]::ErrorGuaranteed>>::{closure#1} as core[672e3947e150d6c6]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0} at /home/ubuntu/rust2/library/core/src/ops/function.rs:250:5 46: 0x7f9c3ec81968 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::he8b26fc22c6f51ec at /home/ubuntu/rust2/library/alloc/src/boxed.rs:1988:9 47: 0x7f9c3ec81968 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h5cf9cbe75a8c3ddc at /home/ubuntu/rust2/library/alloc/src/boxed.rs:1988:9 48: 0x7f9c3ec5f99c - std::sys::unix::thread::Thread::new::thread_start::h2d6dd4455e97d031 at /home/ubuntu/rust2/library/std/src/sys/unix/thread.rs:108:17 49: 0x7f9c37c69609 - start_thread 50: 0x7f9c3ead0133 - clone 51: 0x0 - <unknown> ``` After, note `typeck_with_fallback` on the 5th frame, that's where we *actually* need to be pointed to: ``` error: internal compiler error: no errors encountered even though `delay_span_bug` issued error: internal compiler error: test --> /home/ubuntu/test.rs:1:1 | 1 | fn main() {} | ^^^^^^^^^ | = note: delayed at 0: <rustc_errors::HandlerInner>::emit_diagnostic at ./compiler/rustc_errors/src/lib.rs:1279:29 1: <rustc_errors::HandlerInner>::delay_span_bug::<rustc_span::span_encoding::Span, &str> at ./compiler/rustc_errors/src/lib.rs:1553:9 2: <rustc_errors::Handler>::delay_span_bug::<rustc_span::span_encoding::Span, &str> at ./compiler/rustc_errors/src/lib.rs:995:9 3: <rustc_session::session::Session>::delay_span_bug::<rustc_span::span_encoding::Span, &str> at ./compiler/rustc_session/src/session.rs:600:9 4: rustc_hir_typeck::typeck_with_fallback::<rustc_hir_typeck::typeck::{closure#0}>::{closure#0} at ./compiler/rustc_hir_typeck/src/lib.rs:196:5 5: rustc_hir_typeck::typeck_with_fallback::<rustc_hir_typeck::typeck::{closure#0}> at ./compiler/rustc_hir_typeck/src/lib.rs:185:36 6: rustc_hir_typeck::typeck at ./compiler/rustc_hir_typeck/src/lib.rs:166:9 [ FRAMES INTENTIONALLY OMITTED ] 108: std::panicking::try::<core::result::Result<(), rustc_errors::ErrorGuaranteed>, core::panic::unwind_safe::AssertUnwindSafe<<std::thread::Builder>::spawn_unchecked_<rustc_interface::util::run_in_thread_pool_with_globals<rustc_interface::interface::run_compiler<core::result::Result<(), rustc_errors::ErrorGuaranteed>, rustc_driver::run_compiler::{closure#1}>::{closure#0}, core::result::Result<(), rustc_errors::ErrorGuaranteed>>::{closure#0}::{closure#0}, core::result::Result<(), rustc_errors::ErrorGuaranteed>>::{closure#1}::{closure#0}>> at ./library/std/src/panicking.rs:447:19 109: std::panic::catch_unwind::<core::panic::unwind_safe::AssertUnwindSafe<<std::thread::Builder>::spawn_unchecked_<rustc_interface::util::run_in_thread_pool_with_globals<rustc_interface::interface::run_compiler<core::result::Result<(), rustc_errors::ErrorGuaranteed>, rustc_driver::run_compiler::{closure#1}>::{closure#0}, core::result::Result<(), rustc_errors::ErrorGuaranteed>>::{closure#0}::{closure#0}, core::result::Result<(), rustc_errors::ErrorGuaranteed>>::{closure#1}::{closure#0}>, core::result::Result<(), rustc_errors::ErrorGuaranteed>> at ./library/std/src/panic.rs:140:14 110: <std::thread::Builder>::spawn_unchecked_::<rustc_interface::util::run_in_thread_pool_with_globals<rustc_interface::interface::run_compiler<core::result::Result<(), rustc_errors::ErrorGuaranteed>, rustc_driver::run_compiler::{closure#1}>::{closure#0}, core::result::Result<(), rustc_errors::ErrorGuaranteed>>::{closure#0}::{closure#0}, core::result::Result<(), rustc_errors::ErrorGuaranteed>>::{closure#1} at ./library/std/src/thread/mod.rs:549:30 111: <<std::thread::Builder>::spawn_unchecked_<rustc_interface::util::run_in_thread_pool_with_globals<rustc_interface::interface::run_compiler<core::result::Result<(), rustc_errors::ErrorGuaranteed>, rustc_driver::run_compiler::{closure#1}>::{closure#0}, core::result::Result<(), rustc_errors::ErrorGuaranteed>>::{closure#0}::{closure#0}, core::result::Result<(), rustc_errors::ErrorGuaranteed>>::{closure#1} as core::ops::function::FnOnce<()>>::call_once::{shim:vtable#0} at ./library/core/src/ops/function.rs:250:5 112: <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once at ./library/alloc/src/boxed.rs:1988:9 113: <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once at ./library/alloc/src/boxed.rs:1988:9 114: std::sys::unix::thread::Thread::new::thread_start at ./library/std/src/sys/unix/thread.rs:108:17 115: start_thread 116: clone ```
2 parents b22c152 + 05c1ac0 commit 0681a96

File tree

2 files changed

+18
-26
lines changed

2 files changed

+18
-26
lines changed

compiler/rustc_driver/src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1196,8 +1196,8 @@ static DEFAULT_HOOK: LazyLock<Box<dyn Fn(&panic::PanicInfo<'_>) + Sync + Send +
11961196
};
11971197

11981198
// Invoke the default handler, which prints the actual panic message and optionally a backtrace
1199-
// Don't do this for `GoodPathBug`, which already emits its own more useful backtrace.
1200-
if !info.payload().is::<rustc_errors::GoodPathBug>() {
1199+
// Don't do this for delayed bugs, which already emit their own more useful backtrace.
1200+
if !info.payload().is::<rustc_errors::DelayedBugPanic>() {
12011201
(*DEFAULT_HOOK)(info);
12021202

12031203
// Separate the output with an empty line
@@ -1235,7 +1235,7 @@ pub fn report_ice(info: &panic::PanicInfo<'_>, bug_report_url: &str) {
12351235
// a .span_bug or .bug call has already printed what
12361236
// it wants to print.
12371237
if !info.payload().is::<rustc_errors::ExplicitBug>()
1238-
&& !info.payload().is::<rustc_errors::GoodPathBug>()
1238+
&& !info.payload().is::<rustc_errors::DelayedBugPanic>()
12391239
{
12401240
let mut d = rustc_errors::Diagnostic::new(rustc_errors::Level::Bug, "unexpected panic");
12411241
handler.emit_diagnostic(&mut d);

compiler/rustc_errors/src/lib.rs

+15-23
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ use rustc_span::source_map::SourceMap;
4040
use rustc_span::HashStableContext;
4141
use rustc_span::{Loc, Span};
4242

43-
use std::any::Any;
4443
use std::borrow::Cow;
4544
use std::fmt;
4645
use std::hash::Hash;
@@ -364,9 +363,9 @@ pub use rustc_span::fatal_error::{FatalError, FatalErrorMarker};
364363
/// or `.span_bug` rather than a failed assertion, etc.
365364
pub struct ExplicitBug;
366365

367-
/// Signifies that the compiler died with an explicit call to `.delay_good_path_bug`
366+
/// Signifies that the compiler died with an explicit call to `.delay_*_bug`
368367
/// rather than a failed assertion, etc.
369-
pub struct GoodPathBug;
368+
pub struct DelayedBugPanic;
370369

371370
pub use diagnostic::{
372371
AddToDiagnostic, DecorateLint, Diagnostic, DiagnosticArg, DiagnosticArgValue, DiagnosticId,
@@ -399,7 +398,7 @@ struct HandlerInner {
399398
warn_count: usize,
400399
deduplicated_err_count: usize,
401400
emitter: Box<dyn Emitter + sync::Send>,
402-
delayed_span_bugs: Vec<Diagnostic>,
401+
delayed_span_bugs: Vec<DelayedDiagnostic>,
403402
delayed_good_path_bugs: Vec<DelayedDiagnostic>,
404403
/// This flag indicates that an expected diagnostic was emitted and suppressed.
405404
/// This is used for the `delayed_good_path_bugs` check.
@@ -505,11 +504,7 @@ impl Drop for HandlerInner {
505504

506505
if !self.has_errors() {
507506
let bugs = std::mem::replace(&mut self.delayed_span_bugs, Vec::new());
508-
self.flush_delayed(
509-
bugs,
510-
"no errors encountered even though `delay_span_bug` issued",
511-
ExplicitBug,
512-
);
507+
self.flush_delayed(bugs, "no errors encountered even though `delay_span_bug` issued");
513508
}
514509

515510
// FIXME(eddyb) this explains what `delayed_good_path_bugs` are!
@@ -520,9 +515,8 @@ impl Drop for HandlerInner {
520515
if !self.has_any_message() && !self.suppressed_expected_diag {
521516
let bugs = std::mem::replace(&mut self.delayed_good_path_bugs, Vec::new());
522517
self.flush_delayed(
523-
bugs.into_iter().map(DelayedDiagnostic::decorate),
518+
bugs,
524519
"no warnings or errors encountered even though `delayed_good_path_bugs` issued",
525-
GoodPathBug,
526520
);
527521
}
528522

@@ -1223,11 +1217,7 @@ impl Handler {
12231217
pub fn flush_delayed(&self) {
12241218
let mut inner = self.inner.lock();
12251219
let bugs = std::mem::replace(&mut inner.delayed_span_bugs, Vec::new());
1226-
inner.flush_delayed(
1227-
bugs,
1228-
"no errors encountered even though `delay_span_bug` issued",
1229-
ExplicitBug,
1230-
);
1220+
inner.flush_delayed(bugs, "no errors encountered even though `delay_span_bug` issued");
12311221
}
12321222
}
12331223

@@ -1287,7 +1277,9 @@ impl HandlerInner {
12871277
// once *any* errors were emitted (and truncate `delayed_span_bugs`
12881278
// when an error is first emitted, also), but maybe there's a case
12891279
// in which that's not sound? otherwise this is really inefficient.
1290-
self.delayed_span_bugs.push(diagnostic.clone());
1280+
let backtrace = std::backtrace::Backtrace::force_capture();
1281+
self.delayed_span_bugs
1282+
.push(DelayedDiagnostic::with_backtrace(diagnostic.clone(), backtrace));
12911283

12921284
if !self.flags.report_delayed_bugs {
12931285
return Some(ErrorGuaranteed::unchecked_claim_error_was_emitted());
@@ -1562,7 +1554,6 @@ impl HandlerInner {
15621554
}
15631555
let mut diagnostic = Diagnostic::new(Level::DelayedBug, msg);
15641556
diagnostic.set_span(sp.into());
1565-
diagnostic.note(&format!("delayed at {}", std::panic::Location::caller()));
15661557
self.emit_diagnostic(&mut diagnostic).unwrap()
15671558
}
15681559

@@ -1605,12 +1596,13 @@ impl HandlerInner {
16051596

16061597
fn flush_delayed(
16071598
&mut self,
1608-
bugs: impl IntoIterator<Item = Diagnostic>,
1599+
bugs: impl IntoIterator<Item = DelayedDiagnostic>,
16091600
explanation: impl Into<DiagnosticMessage> + Copy,
1610-
panic_with: impl Any + Send + 'static,
16111601
) {
16121602
let mut no_bugs = true;
1613-
for mut bug in bugs {
1603+
for bug in bugs {
1604+
let mut bug = bug.decorate();
1605+
16141606
if no_bugs {
16151607
// Put the overall explanation before the `DelayedBug`s, to
16161608
// frame them better (e.g. separate warnings from them).
@@ -1633,9 +1625,9 @@ impl HandlerInner {
16331625
self.emit_diagnostic(&mut bug);
16341626
}
16351627

1636-
// Panic with `ExplicitBug` to avoid "unexpected panic" messages.
1628+
// Panic with `DelayedBugPanic` to avoid "unexpected panic" messages.
16371629
if !no_bugs {
1638-
panic::panic_any(panic_with);
1630+
panic::panic_any(DelayedBugPanic);
16391631
}
16401632
}
16411633

0 commit comments

Comments
 (0)