@@ -102,16 +102,38 @@ impl<'tcx> TyCtxt<'tcx> {
102102 }
103103}
104104
105- /// Helper for `TyCtxtEnsure` to avoid a closure.
106- #[ inline( always) ]
107- fn noop < T > ( _: & T ) { }
108-
109105/// Helper to ensure that queries only return `Copy` types.
110106#[ inline( always) ]
111107fn copy < T : Copy > ( x : & T ) -> T {
112108 * x
113109}
114110
111+ fn evaluate_query < ' tcx , Cache > (
112+ tcx : TyCtxt < ' tcx > ,
113+ execute_query : fn (
114+ & ' tcx dyn QueryEngine < ' tcx > ,
115+ TyCtxt < ' tcx > ,
116+ Span ,
117+ Cache :: Key ,
118+ QueryMode ,
119+ ) -> Option < Cache :: Stored > ,
120+ query_cache : & Cache ,
121+ span : Span ,
122+ key : Cache :: Key ,
123+ mode : QueryMode ,
124+ ) -> Option < Cache :: Stored >
125+ where
126+ Cache :: Stored : Copy ,
127+ Cache : QueryCache ,
128+ {
129+ let cached = try_get_cached ( tcx, query_cache, & key, copy) ;
130+
131+ match cached {
132+ Ok ( value) => return value,
133+ Err ( ( ) ) => ( ) ,
134+ }
135+ }
136+
115137macro_rules! query_helper_param_ty {
116138 ( DefId ) => { impl IntoQueryParam <DefId > } ;
117139 ( $K: ty) => { $K } ;
@@ -220,14 +242,7 @@ macro_rules! define_callbacks {
220242 let key = key. into_query_param( ) ;
221243 opt_remap_env_constness!( [ $( $modifiers) * ] [ key] ) ;
222244
223- let cached = try_get_cached( self . tcx, & self . tcx. query_caches. $name, & key, noop) ;
224-
225- match cached {
226- Ok ( ( ) ) => return ,
227- Err ( ( ) ) => ( ) ,
228- }
229-
230- self . tcx. queries. $name( self . tcx, DUMMY_SP , key, QueryMode :: Ensure ) ;
245+ let _ = evaluate_query( self . tcx, QueryEngine :: $name, & self . tcx. query_caches. $name, DUMMY_SP , key, QueryMode :: Ensure ) ;
231246 } ) *
232247 }
233248
@@ -249,14 +264,7 @@ macro_rules! define_callbacks {
249264 let key = key. into_query_param( ) ;
250265 opt_remap_env_constness!( [ $( $modifiers) * ] [ key] ) ;
251266
252- let cached = try_get_cached( self . tcx, & self . tcx. query_caches. $name, & key, copy) ;
253-
254- match cached {
255- Ok ( value) => return value,
256- Err ( ( ) ) => ( ) ,
257- }
258-
259- self . tcx. queries. $name( self . tcx, self . span, key, QueryMode :: Get ) . unwrap( )
267+ evaluate_query( self . tcx, QueryEngine :: $name, & self . tcx. query_caches. $name, self . span, key, QueryMode :: Get ) . unwrap( )
260268 } ) *
261269 }
262270
0 commit comments