@@ -17,7 +17,6 @@ use rustc_data_structures::sharded::Sharded;
1717use rustc_data_structures:: sync:: { Lock , LockGuard } ;
1818use rustc_data_structures:: thin_vec:: ThinVec ;
1919use rustc_errors:: { Diagnostic , FatalError } ;
20- use rustc_span:: source_map:: DUMMY_SP ;
2120use rustc_span:: Span ;
2221use std:: collections:: hash_map:: Entry ;
2322use std:: fmt:: Debug ;
@@ -641,31 +640,26 @@ where
641640
642641/// Ensure that either this query has all green inputs or been executed.
643642/// Executing `query::ensure(D)` is considered a read of the dep-node `D`.
643+ /// Returns true if the query should still run.
644644///
645645/// This function is particularly useful when executing passes for their
646646/// side-effects -- e.g., in order to report errors for erroneous programs.
647647///
648648/// Note: The optimization is only available during incr. comp.
649649#[ inline( never) ]
650- fn ensure_query_impl < CTX , C > (
651- tcx : CTX ,
652- state : & QueryState < CTX :: DepKind , CTX :: Query , C > ,
653- key : C :: Key ,
654- query : & QueryVtable < CTX , C :: Key , C :: Value > ,
655- ) where
656- C : QueryCache ,
657- C :: Key : crate :: dep_graph:: DepNodeParams < CTX > ,
650+ fn ensure_must_run < CTX , K , V > ( tcx : CTX , key : & K , query : & QueryVtable < CTX , K , V > ) -> bool
651+ where
652+ K : crate :: dep_graph:: DepNodeParams < CTX > ,
658653 CTX : QueryContext ,
659654{
660655 if query. eval_always {
661- let _ = get_query_impl ( tcx, state, DUMMY_SP , key, query) ;
662- return ;
656+ return true ;
663657 }
664658
665659 // Ensuring an anonymous query makes no sense
666660 assert ! ( !query. anon) ;
667661
668- let dep_node = query. to_dep_node ( tcx, & key) ;
662+ let dep_node = query. to_dep_node ( tcx, key) ;
669663
670664 match tcx. dep_graph ( ) . try_mark_green_and_read ( tcx, & dep_node) {
671665 None => {
@@ -675,10 +669,11 @@ fn ensure_query_impl<CTX, C>(
675669 // DepNodeIndex. We must invoke the query itself. The performance cost
676670 // this introduces should be negligible as we'll immediately hit the
677671 // in-memory cache, or another query down the line will.
678- let _ = get_query_impl ( tcx , state , DUMMY_SP , key , query ) ;
672+ true
679673 }
680674 Some ( ( _, dep_node_index) ) => {
681675 tcx. profiler ( ) . query_cache_hit ( dep_node_index. into ( ) ) ;
676+ false
682677 }
683678 }
684679}
@@ -720,24 +715,27 @@ fn force_query_impl<CTX, C>(
720715 ) ;
721716}
722717
723- pub fn get_query < Q , CTX > ( tcx : CTX , span : Span , key : Q :: Key ) -> Q :: Stored
724- where
725- Q : QueryDescription < CTX > ,
726- Q :: Key : crate :: dep_graph:: DepNodeParams < CTX > ,
727- CTX : QueryContext ,
728- {
729- debug ! ( "ty::query::get_query<{}>(key={:?}, span={:?})" , Q :: NAME , key, span) ;
730-
731- get_query_impl ( tcx, Q :: query_state ( tcx) , span, key, & Q :: VTABLE )
718+ pub enum QueryMode {
719+ Get ,
720+ Ensure ,
732721}
733722
734- pub fn ensure_query < Q , CTX > ( tcx : CTX , key : Q :: Key )
723+ pub fn get_query < Q , CTX > ( tcx : CTX , span : Span , key : Q :: Key , mode : QueryMode ) -> Option < Q :: Stored >
735724where
736725 Q : QueryDescription < CTX > ,
737726 Q :: Key : crate :: dep_graph:: DepNodeParams < CTX > ,
738727 CTX : QueryContext ,
739728{
740- ensure_query_impl ( tcx, Q :: query_state ( tcx) , key, & Q :: VTABLE )
729+ let query = & Q :: VTABLE ;
730+ if let QueryMode :: Ensure = mode {
731+ if !ensure_must_run ( tcx, & key, query) {
732+ return None ;
733+ }
734+ }
735+
736+ debug ! ( "ty::query::get_query<{}>(key={:?}, span={:?})" , Q :: NAME , key, span) ;
737+ let value = get_query_impl ( tcx, Q :: query_state ( tcx) , span, key, query) ;
738+ Some ( value)
741739}
742740
743741pub fn force_query < Q , CTX > ( tcx : CTX , key : Q :: Key , span : Span , dep_node : DepNode < CTX :: DepKind > )
0 commit comments