@@ -173,6 +173,45 @@ pub fn query_ensure<'tcx, Cache>(
173173 }
174174}
175175
176+ #[ inline]
177+ pub fn query_ensure_error_guaranteed < ' tcx , Cache > (
178+ tcx : TyCtxt < ' tcx > ,
179+ execute_query : fn ( TyCtxt < ' tcx > , Span , Cache :: Key , QueryMode ) -> Option < Cache :: Value > ,
180+ query_cache : & Cache ,
181+ key : Cache :: Key ,
182+ check_cache : bool ,
183+ ) -> Result < ( ) , ErrorGuaranteed >
184+ where
185+ Cache : QueryCache < Value = super :: erase:: Erase < Result < ( ) , ErrorGuaranteed > > > ,
186+ {
187+ let key = key. into_query_param ( ) ;
188+ if let Some ( res) = try_get_cached ( tcx, query_cache, & key) {
189+ super :: erase:: restore ( res)
190+ } else {
191+ execute_query ( tcx, DUMMY_SP , key, QueryMode :: Ensure { check_cache } )
192+ . map ( super :: erase:: restore)
193+ // Either we actually executed the query, which means we got a full `Result`,
194+ // or we can just assume the query succeeded, because it was green in the
195+ // incremental cache. If it is green, that means that the previous compilation
196+ // that wrote to the incremental cache compiles successfully. That is only
197+ // possible if the cache entry was `Ok(())`, so we emit that here, without
198+ // actually encoding the `Result` in the cache or loading it from there.
199+ . unwrap_or ( Ok ( ( ) ) )
200+ }
201+ }
202+
203+ macro_rules! query_ensure {
204+ ( [ ] $( $args: tt) * ) => {
205+ query_ensure( $( $args) * )
206+ } ;
207+ ( [ ( ensure_forwards_result_if_red) $( $rest: tt) * ] $( $args: tt) * ) => {
208+ query_ensure_error_guaranteed( $( $args) * )
209+ } ;
210+ ( [ $other: tt $( $modifiers: tt) * ] $( $args: tt) * ) => {
211+ query_ensure!( [ $( $modifiers) * ] $( $args) * )
212+ } ;
213+ }
214+
176215macro_rules! query_helper_param_ty {
177216 ( DefId ) => { impl IntoQueryParam <DefId > } ;
178217 ( LocalDefId ) => { impl IntoQueryParam <LocalDefId > } ;
@@ -220,6 +259,18 @@ macro_rules! separate_provide_extern_decl {
220259 } ;
221260}
222261
262+ macro_rules! ensure_result {
263+ ( [ ] [ $ty: ty] ) => {
264+ ( )
265+ } ;
266+ ( [ ( ensure_forwards_result_if_red) $( $rest: tt) * ] [ $ty: ty] ) => {
267+ Result <( ) , ErrorGuaranteed >
268+ } ;
269+ ( [ $other: tt $( $modifiers: tt) * ] [ $( $args: tt) * ] ) => {
270+ ensure_result!( [ $( $modifiers) * ] [ $( $args) * ] )
271+ } ;
272+ }
273+
223274macro_rules! separate_provide_extern_default {
224275 ( [ ] [ $name: ident] ) => {
225276 ( )
@@ -343,14 +394,15 @@ macro_rules! define_callbacks {
343394 impl <' tcx> TyCtxtEnsure <' tcx> {
344395 $( $( #[ $attr] ) *
345396 #[ inline( always) ]
346- pub fn $name( self , key: query_helper_param_ty!( $( $K) * ) ) {
347- query_ensure(
397+ pub fn $name( self , key: query_helper_param_ty!( $( $K) * ) ) -> ensure_result!( [ $( $modifiers) * ] [ $V] ) {
398+ query_ensure!(
399+ [ $( $modifiers) * ]
348400 self . tcx,
349401 self . tcx. query_system. fns. engine. $name,
350402 & self . tcx. query_system. caches. $name,
351403 key. into_query_param( ) ,
352404 false ,
353- ) ;
405+ )
354406 } ) *
355407 }
356408
0 commit comments