Skip to content

Commit 671fc18

Browse files
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
2 parents f6ca425 + 73fc170 commit 671fc18

File tree

1 file changed

+14
-8
lines changed
  • compiler/rustc_errors/src

1 file changed

+14
-8
lines changed

compiler/rustc_errors/src/lib.rs

+14-8
Original file line numberDiff line numberDiff line change
@@ -442,8 +442,8 @@ struct DiagCtxtInner {
442442
emitter: Box<DynEmitter>,
443443

444444
/// Must we produce a diagnostic to justify the use of the expensive
445-
/// `trimmed_def_paths` function?
446-
must_produce_diag: bool,
445+
/// `trimmed_def_paths` function? Backtrace is the location of the call.
446+
must_produce_diag: Option<Backtrace>,
447447

448448
/// Has this diagnostic context printed any diagnostics? (I.e. has
449449
/// `self.emitter.emit_diagnostic()` been called?
@@ -572,10 +572,11 @@ impl Drop for DiagCtxtInner {
572572
}
573573

574574
if !self.has_printed && !self.suppressed_expected_diag && !std::thread::panicking() {
575-
if self.must_produce_diag {
575+
if let Some(backtrace) = &self.must_produce_diag {
576576
panic!(
577-
"must_produce_diag: trimmed_def_paths called but no diagnostics emitted; \
578-
use `DelayDm` for lints or `with_no_trimmed_paths` for debugging"
577+
"must_produce_diag: `trimmed_def_paths` called but no diagnostics emitted; \
578+
use `DelayDm` for lints or `with_no_trimmed_paths` for debugging. \
579+
called at: {backtrace}"
579580
);
580581
}
581582
}
@@ -721,7 +722,7 @@ impl DiagCtxt {
721722
*delayed_bugs = Default::default();
722723
*deduplicated_err_count = 0;
723724
*deduplicated_warn_count = 0;
724-
*must_produce_diag = false;
725+
*must_produce_diag = None;
725726
*has_printed = false;
726727
*suppressed_expected_diag = false;
727728
*taught_diagnostics = Default::default();
@@ -1091,8 +1092,13 @@ impl DiagCtxt {
10911092

10921093
/// Used when trimmed_def_paths is called and we must produce a diagnostic
10931094
/// to justify its cost.
1095+
#[track_caller]
10941096
pub fn set_must_produce_diag(&self) {
1095-
self.inner.borrow_mut().must_produce_diag = true;
1097+
assert!(
1098+
self.inner.borrow().must_produce_diag.is_none(),
1099+
"should only need to collect a backtrace once"
1100+
);
1101+
self.inner.borrow_mut().must_produce_diag = Some(Backtrace::capture());
10961102
}
10971103
}
10981104

@@ -1384,7 +1390,7 @@ impl DiagCtxtInner {
13841390
deduplicated_err_count: 0,
13851391
deduplicated_warn_count: 0,
13861392
emitter,
1387-
must_produce_diag: false,
1393+
must_produce_diag: None,
13881394
has_printed: false,
13891395
suppressed_expected_diag: false,
13901396
taught_diagnostics: Default::default(),

0 commit comments

Comments
 (0)