Rollup merge of #122299 - compiler-errors:bt-for-must-diag, r=nnethercote

Store backtrace for `must_produce_diag`

This makes it significantly easier to debug a `must_produce_diag` ICE, since we have no other way to know where the heck the bug originates from.

Backtrace rendering kinda sucks right now since we're just printing it in the panic message; happy to apply some suggestions to make it prettier or reuse other bug printing machinery, but also don't want to iterate too much on the rendering since this really is just for debug purposes.

r? nnethercote
This commit is contained in:
Jubilee 2024-03-11 09:29:37 -07:00 committed by GitHub
commit 671fc18ba2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -442,8 +442,8 @@ struct DiagCtxtInner {
emitter: Box<DynEmitter>, emitter: Box<DynEmitter>,
/// Must we produce a diagnostic to justify the use of the expensive /// Must we produce a diagnostic to justify the use of the expensive
/// `trimmed_def_paths` function? /// `trimmed_def_paths` function? Backtrace is the location of the call.
must_produce_diag: bool, must_produce_diag: Option<Backtrace>,
/// Has this diagnostic context printed any diagnostics? (I.e. has /// Has this diagnostic context printed any diagnostics? (I.e. has
/// `self.emitter.emit_diagnostic()` been called? /// `self.emitter.emit_diagnostic()` been called?
@ -572,10 +572,11 @@ fn drop(&mut self) {
} }
if !self.has_printed && !self.suppressed_expected_diag && !std::thread::panicking() { if !self.has_printed && !self.suppressed_expected_diag && !std::thread::panicking() {
if self.must_produce_diag { if let Some(backtrace) = &self.must_produce_diag {
panic!( panic!(
"must_produce_diag: trimmed_def_paths called but no diagnostics emitted; \ "must_produce_diag: `trimmed_def_paths` called but no diagnostics emitted; \
use `DelayDm` for lints or `with_no_trimmed_paths` for debugging" use `DelayDm` for lints or `with_no_trimmed_paths` for debugging. \
called at: {backtrace}"
); );
} }
} }
@ -721,7 +722,7 @@ pub fn reset_err_count(&self) {
*delayed_bugs = Default::default(); *delayed_bugs = Default::default();
*deduplicated_err_count = 0; *deduplicated_err_count = 0;
*deduplicated_warn_count = 0; *deduplicated_warn_count = 0;
*must_produce_diag = false; *must_produce_diag = None;
*has_printed = false; *has_printed = false;
*suppressed_expected_diag = false; *suppressed_expected_diag = false;
*taught_diagnostics = Default::default(); *taught_diagnostics = Default::default();
@ -1091,8 +1092,13 @@ pub fn flush_delayed(&self) {
/// Used when trimmed_def_paths is called and we must produce a diagnostic /// Used when trimmed_def_paths is called and we must produce a diagnostic
/// to justify its cost. /// to justify its cost.
#[track_caller]
pub fn set_must_produce_diag(&self) { pub fn set_must_produce_diag(&self) {
self.inner.borrow_mut().must_produce_diag = true; assert!(
self.inner.borrow().must_produce_diag.is_none(),
"should only need to collect a backtrace once"
);
self.inner.borrow_mut().must_produce_diag = Some(Backtrace::capture());
} }
} }
@ -1384,7 +1390,7 @@ fn new(emitter: Box<DynEmitter>) -> Self {
deduplicated_err_count: 0, deduplicated_err_count: 0,
deduplicated_warn_count: 0, deduplicated_warn_count: 0,
emitter, emitter,
must_produce_diag: false, must_produce_diag: None,
has_printed: false, has_printed: false,
suppressed_expected_diag: false, suppressed_expected_diag: false,
taught_diagnostics: Default::default(), taught_diagnostics: Default::default(),