@@ -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
@@ -1387,7 +1393,7 @@ impl DiagCtxtInner {
13871393 deduplicated_err_count : 0 ,
13881394 deduplicated_warn_count : 0 ,
13891395 emitter,
1390- must_produce_diag : false ,
1396+ must_produce_diag : None ,
13911397 has_printed : false ,
13921398 suppressed_expected_diag : false ,
13931399 taught_diagnostics : Default :: default ( ) ,
0 commit comments