1+ use crate :: dep_graph:: DepContext ;
12use crate :: query:: plumbing:: CycleError ;
2- use crate :: query:: QueryStackFrame ;
3+ use crate :: query:: { QueryContext , QueryStackFrame } ;
34
45use rustc_data_structures:: fx:: FxHashMap ;
5- use rustc_errors:: { struct_span_err, DiagnosticBuilder } ;
6+ use rustc_errors:: { struct_span_err, Diagnostic , DiagnosticBuilder , Handler , Level } ;
67use rustc_session:: Session ;
78use rustc_span:: Span ;
89
@@ -13,8 +14,6 @@ use std::num::NonZeroU32;
1314
1415#[ cfg( parallel_compiler) ]
1516use {
16- crate :: dep_graph:: DepContext ,
17- crate :: query:: QueryContext ,
1817 parking_lot:: { Condvar , Mutex } ,
1918 rustc_data_structures:: fx:: FxHashSet ,
2019 rustc_data_structures:: stable_hasher:: { HashStable , StableHasher } ,
@@ -626,3 +625,42 @@ pub(crate) fn report_cycle<'a>(
626625
627626 err
628627}
628+
629+ pub fn print_query_stack < CTX : QueryContext > (
630+ tcx : CTX ,
631+ mut current_query : Option < QueryJobId < CTX :: DepKind > > ,
632+ handler : & Handler ,
633+ num_frames : Option < usize > ,
634+ ) -> usize {
635+ // Be careful relying on global state here: this code is called from
636+ // a panic hook, which means that the global `Handler` may be in a weird
637+ // state if it was responsible for triggering the panic.
638+ let mut i = 0 ;
639+ let query_map = tcx. try_collect_active_jobs ( ) ;
640+
641+ while let Some ( query) = current_query {
642+ if Some ( i) == num_frames {
643+ break ;
644+ }
645+ let query_info = if let Some ( info) = query_map. as_ref ( ) . and_then ( |map| map. get ( & query) ) {
646+ info
647+ } else {
648+ break ;
649+ } ;
650+ let mut diag = Diagnostic :: new (
651+ Level :: FailureNote ,
652+ & format ! (
653+ "#{} [{}] {}" ,
654+ i, query_info. info. query. name, query_info. info. query. description
655+ ) ,
656+ ) ;
657+ diag. span =
658+ tcx. dep_context ( ) . sess ( ) . source_map ( ) . guess_head_span ( query_info. info . span ) . into ( ) ;
659+ handler. force_print_diagnostic ( diag) ;
660+
661+ current_query = query_info. job . parent ;
662+ i += 1 ;
663+ }
664+
665+ i
666+ }
0 commit comments