@@ -10,7 +10,6 @@ pub(crate) use self::undo_log::{InferCtxtUndoLogs, Snapshot, UndoLog};
1010
1111use crate :: traits:: { self , ObligationCause , PredicateObligations , TraitEngine } ;
1212
13- use hir:: def_id:: CRATE_DEF_ID ;
1413use rustc_data_structures:: fx:: { FxHashMap , FxHashSet } ;
1514use rustc_data_structures:: sync:: Lrc ;
1615use rustc_data_structures:: undo_log:: Rollback ;
@@ -291,7 +290,12 @@ pub struct InferCtxt<'a, 'tcx> {
291290
292291 /// The `DefId` of the item in whose context we are performing inference or typeck.
293292 /// It is used to check whether an opaque type use is a defining use.
294- pub defining_use_anchor : LocalDefId ,
293+ ///
294+ /// If it is `None`, we can't resolve opaque types here and need to bubble up
295+ /// the obligation. This frequently happens for
296+ /// short lived InferCtxt within queries. The opaque type obligations are forwarded
297+ /// to the outside until the end up in an `InferCtxt` for typeck or borrowck.
298+ pub defining_use_anchor : Option < LocalDefId > ,
295299
296300 /// During type-checking/inference of a body, `in_progress_typeck_results`
297301 /// contains a reference to the typeck results being built up, which are
@@ -547,7 +551,7 @@ impl<'tcx> fmt::Display for FixupError<'tcx> {
547551pub struct InferCtxtBuilder < ' tcx > {
548552 tcx : TyCtxt < ' tcx > ,
549553 fresh_typeck_results : Option < RefCell < ty:: TypeckResults < ' tcx > > > ,
550- defining_use_anchor : LocalDefId ,
554+ defining_use_anchor : Option < LocalDefId > ,
551555}
552556
553557pub trait TyCtxtInferExt < ' tcx > {
@@ -556,11 +560,7 @@ pub trait TyCtxtInferExt<'tcx> {
556560
557561impl < ' tcx > TyCtxtInferExt < ' tcx > for TyCtxt < ' tcx > {
558562 fn infer_ctxt ( self ) -> InferCtxtBuilder < ' tcx > {
559- InferCtxtBuilder {
560- tcx : self ,
561- defining_use_anchor : CRATE_DEF_ID ,
562- fresh_typeck_results : None ,
563- }
563+ InferCtxtBuilder { tcx : self , defining_use_anchor : None , fresh_typeck_results : None }
564564 }
565565}
566566
@@ -580,7 +580,7 @@ impl<'tcx> InferCtxtBuilder<'tcx> {
580580 /// (via `with_fresh_in_progress_typeck_results`) and for the inference context used
581581 /// in mir borrowck.
582582 pub fn with_opaque_type_inference ( mut self , defining_use_anchor : LocalDefId ) -> Self {
583- self . defining_use_anchor = defining_use_anchor;
583+ self . defining_use_anchor = Some ( defining_use_anchor) ;
584584 self
585585 }
586586
0 commit comments