99// except according to those terms.
1010
1111use dep_graph:: { DepConstructor , DepNode , DepNodeIndex } ;
12+ use errors:: { Diagnostic , DiagnosticBuilder } ;
1213use hir:: def_id:: { CrateNum , DefId , LOCAL_CRATE } ;
1314use hir:: def:: Def ;
1415use hir;
@@ -32,7 +33,7 @@ use util::common::{profq_msg, ProfileQueriesMsg};
3233
3334use rustc_data_structures:: indexed_vec:: IndexVec ;
3435use rustc_data_structures:: fx:: FxHashMap ;
35- use std:: cell:: { RefCell , RefMut } ;
36+ use std:: cell:: { RefCell , RefMut , Cell } ;
3637use std:: fmt:: Debug ;
3738use std:: hash:: Hash ;
3839use std:: marker:: PhantomData ;
@@ -188,7 +189,18 @@ impl<'tcx> Value<'tcx> for ty::SymbolName {
188189
189190struct QueryMap < D : QueryDescription > {
190191 phantom : PhantomData < D > ,
191- map : FxHashMap < D :: Key , ( D :: Value , DepNodeIndex ) > ,
192+ map : FxHashMap < D :: Key , QueryValue < D :: Value > > ,
193+ }
194+
195+ struct QueryValue < T > {
196+ value : T ,
197+ index : DepNodeIndex ,
198+ diagnostics : Option < Box < QueryDiagnostics > > ,
199+ }
200+
201+ struct QueryDiagnostics {
202+ diagnostics : Vec < Diagnostic > ,
203+ emitted_diagnostics : Cell < bool > ,
192204}
193205
194206impl < M : QueryDescription > QueryMap < M > {
@@ -618,10 +630,20 @@ macro_rules! define_maps {
618630 )
619631 ) ;
620632
621- if let Some ( & ( ref result, dep_node_index) ) = tcx. maps. $name. borrow( ) . map. get( & key) {
622- tcx. dep_graph. read_index( dep_node_index) ;
633+ if let Some ( value) = tcx. maps. $name. borrow( ) . map. get( & key) {
634+ if let Some ( ref d) = value. diagnostics {
635+ if !d. emitted_diagnostics. get( ) {
636+ d. emitted_diagnostics. set( true ) ;
637+ let handle = tcx. sess. diagnostic( ) ;
638+ for diagnostic in d. diagnostics. iter( ) {
639+ DiagnosticBuilder :: new_diagnostic( handle, diagnostic. clone( ) )
640+ . emit( ) ;
641+ }
642+ }
643+ }
623644 profq_msg!( tcx, ProfileQueriesMsg :: CacheHit ) ;
624- return Ok ( f( result) ) ;
645+ tcx. dep_graph. read_index( value. index) ;
646+ return Ok ( f( & value. value) ) ;
625647 }
626648 // else, we are going to run the provider:
627649 profq_msg!( tcx, ProfileQueriesMsg :: ProviderBegin ) ;
@@ -633,36 +655,52 @@ macro_rules! define_maps {
633655 span = key. default_span( tcx)
634656 }
635657
636- let ( result , dep_node_index ) = tcx. cycle_check( span, Query :: $name( key) , || {
658+ let res = tcx. cycle_check( span, Query :: $name( key) , || {
637659 let dep_node = Self :: to_dep_node( tcx, & key) ;
638660
639- if dep_node. kind. is_anon( ) {
640- tcx. dep_graph. with_anon_task( dep_node. kind, || {
641- let provider = tcx. maps. providers[ key. map_crate( ) ] . $name;
642- provider( tcx. global_tcx( ) , key)
643- } )
644- } else {
645- fn run_provider<' a, ' tcx, ' lcx>( tcx: TyCtxt <' a, ' tcx, ' lcx>,
646- key: $K)
647- -> $V {
648- let provider = tcx. maps. providers[ key. map_crate( ) ] . $name;
649- provider( tcx. global_tcx( ) , key)
661+ tcx. sess. diagnostic( ) . track_diagnostics( || {
662+ if dep_node. kind. is_anon( ) {
663+ tcx. dep_graph. with_anon_task( dep_node. kind, || {
664+ let provider = tcx. maps. providers[ key. map_crate( ) ] . $name;
665+ provider( tcx. global_tcx( ) , key)
666+ } )
667+ } else {
668+ fn run_provider<' a, ' tcx, ' lcx>( tcx: TyCtxt <' a, ' tcx, ' lcx>,
669+ key: $K)
670+ -> $V {
671+ let provider = tcx. maps. providers[ key. map_crate( ) ] . $name;
672+ provider( tcx. global_tcx( ) , key)
673+ }
674+
675+ tcx. dep_graph. with_task( dep_node, tcx, key, run_provider)
650676 }
651-
652- tcx. dep_graph. with_task( dep_node, tcx, key, run_provider)
653- }
677+ } )
654678 } ) ?;
655679 profq_msg!( tcx, ProfileQueriesMsg :: ProviderEnd ) ;
680+ let ( ( result, dep_node_index) , diagnostics) = res;
656681
657682 tcx. dep_graph. read_index( dep_node_index) ;
658683
684+ let value = QueryValue {
685+ value: result,
686+ index: dep_node_index,
687+ diagnostics: if diagnostics. len( ) == 0 {
688+ None
689+ } else {
690+ Some ( Box :: new( QueryDiagnostics {
691+ diagnostics,
692+ emitted_diagnostics: Cell :: new( true ) ,
693+ } ) )
694+ } ,
695+ } ;
696+
659697 Ok ( f( & tcx. maps
660698 . $name
661699 . borrow_mut( )
662700 . map
663701 . entry( key)
664- . or_insert( ( result , dep_node_index ) )
665- . 0 ) )
702+ . or_insert( value )
703+ . value ) )
666704 }
667705
668706 pub fn try_get( tcx: TyCtxt <' a, $tcx, ' lcx>, span: Span , key: $K)
0 commit comments