@@ -34,7 +34,7 @@ use rustc_middle::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKin
3434use rustc_middle:: infer:: unify_key:: { ConstVidKey , EffectVidKey } ;
3535use rustc_middle:: mir:: interpret:: { ErrorHandled , EvalToValTreeResult } ;
3636use rustc_middle:: mir:: ConstraintCategory ;
37- use rustc_middle:: traits:: { select, DefiningAnchor } ;
37+ use rustc_middle:: traits:: select;
3838use rustc_middle:: ty:: error:: { ExpectedFound , TypeError } ;
3939use rustc_middle:: ty:: fold:: BoundVarReplacerDelegate ;
4040use rustc_middle:: ty:: fold:: { TypeFoldable , TypeFolder , TypeSuperFoldable } ;
@@ -234,17 +234,8 @@ impl<'tcx> InferCtxtInner<'tcx> {
234234pub struct InferCtxt < ' tcx > {
235235 pub tcx : TyCtxt < ' tcx > ,
236236
237- /// The `DefId` of the item in whose context we are performing inference or typeck.
238- /// It is used to check whether an opaque type use is a defining use.
239- ///
240- /// If it is `DefiningAnchor::Bubble`, we can't resolve opaque types here and need to bubble up
241- /// the obligation. This frequently happens for
242- /// short lived InferCtxt within queries. The opaque type obligations are forwarded
243- /// to the outside until the end up in an `InferCtxt` for typeck or borrowck.
244- ///
245- /// Its default value is `DefiningAnchor::Error`, this way it is easier to catch errors that
246- /// might come up during inference or typeck.
247- pub defining_use_anchor : DefiningAnchor ,
237+ /// The `DefIds` of the opaque types that may have their hidden types constrained.
238+ pub defining_opaque_types : & ' tcx ty:: List < LocalDefId > ,
248239
249240 /// Whether this inference context should care about region obligations in
250241 /// the root universe. Most notably, this is used during hir typeck as region
@@ -391,6 +382,10 @@ impl<'tcx> ty::InferCtxtLike for InferCtxt<'tcx> {
391382 fn probe_ct_var ( & self , vid : ConstVid ) -> Option < ty:: Const < ' tcx > > {
392383 self . probe_const_var ( vid) . ok ( )
393384 }
385+
386+ fn defining_opaque_types ( & self ) -> & ' tcx ty:: List < LocalDefId > {
387+ self . defining_opaque_types
388+ }
394389}
395390
396391/// See the `error_reporting` module for more details.
@@ -605,7 +600,7 @@ impl fmt::Display for FixupError {
605600/// Used to configure inference contexts before their creation.
606601pub struct InferCtxtBuilder < ' tcx > {
607602 tcx : TyCtxt < ' tcx > ,
608- defining_use_anchor : DefiningAnchor ,
603+ defining_opaque_types : & ' tcx ty :: List < LocalDefId > ,
609604 considering_regions : bool ,
610605 skip_leak_check : bool ,
611606 /// Whether we are in coherence mode.
@@ -620,7 +615,7 @@ impl<'tcx> TyCtxt<'tcx> {
620615 fn infer_ctxt ( self ) -> InferCtxtBuilder < ' tcx > {
621616 InferCtxtBuilder {
622617 tcx : self ,
623- defining_use_anchor : DefiningAnchor :: Error ,
618+ defining_opaque_types : ty :: List :: empty ( ) ,
624619 considering_regions : true ,
625620 skip_leak_check : false ,
626621 intercrate : false ,
@@ -636,8 +631,16 @@ impl<'tcx> InferCtxtBuilder<'tcx> {
636631 /// It is only meant to be called in two places, for typeck
637632 /// (via `Inherited::build`) and for the inference context used
638633 /// in mir borrowck.
639- pub fn with_opaque_type_inference ( mut self , defining_use_anchor : DefiningAnchor ) -> Self {
640- self . defining_use_anchor = defining_use_anchor;
634+ pub fn with_opaque_type_inference ( mut self , defining_anchor : LocalDefId ) -> Self {
635+ self . defining_opaque_types = self . tcx . opaque_types_defined_by ( defining_anchor) ;
636+ self
637+ }
638+
639+ pub fn with_defining_opaque_types (
640+ mut self ,
641+ defining_opaque_types : & ' tcx ty:: List < LocalDefId > ,
642+ ) -> Self {
643+ self . defining_opaque_types = defining_opaque_types;
641644 self
642645 }
643646
@@ -669,30 +672,30 @@ impl<'tcx> InferCtxtBuilder<'tcx> {
669672 /// the bound values in `C` to their instantiated values in `V`
670673 /// (in other words, `S(C) = V`).
671674 pub fn build_with_canonical < T > (
672- & mut self ,
675+ self ,
673676 span : Span ,
674677 canonical : & Canonical < ' tcx , T > ,
675678 ) -> ( InferCtxt < ' tcx > , T , CanonicalVarValues < ' tcx > )
676679 where
677680 T : TypeFoldable < TyCtxt < ' tcx > > ,
678681 {
679- let infcx = self . build ( ) ;
682+ let infcx = self . with_defining_opaque_types ( canonical . defining_opaque_types ) . build ( ) ;
680683 let ( value, args) = infcx. instantiate_canonical_with_fresh_inference_vars ( span, canonical) ;
681684 ( infcx, value, args)
682685 }
683686
684687 pub fn build ( & mut self ) -> InferCtxt < ' tcx > {
685688 let InferCtxtBuilder {
686689 tcx,
687- defining_use_anchor ,
690+ defining_opaque_types ,
688691 considering_regions,
689692 skip_leak_check,
690693 intercrate,
691694 next_trait_solver,
692695 } = * self ;
693696 InferCtxt {
694697 tcx,
695- defining_use_anchor ,
698+ defining_opaque_types ,
696699 considering_regions,
697700 skip_leak_check,
698701 inner : RefCell :: new ( InferCtxtInner :: new ( ) ) ,
@@ -1208,13 +1211,11 @@ impl<'tcx> InferCtxt<'tcx> {
12081211
12091212 #[ instrument( level = "debug" , skip( self ) , ret) ]
12101213 pub fn take_opaque_types ( & self ) -> opaque_types:: OpaqueTypeMap < ' tcx > {
1211- debug_assert_ne ! ( self . defining_use_anchor, DefiningAnchor :: Error ) ;
12121214 std:: mem:: take ( & mut self . inner . borrow_mut ( ) . opaque_type_storage . opaque_types )
12131215 }
12141216
12151217 #[ instrument( level = "debug" , skip( self ) , ret) ]
12161218 pub fn clone_opaque_types ( & self ) -> opaque_types:: OpaqueTypeMap < ' tcx > {
1217- debug_assert_ne ! ( self . defining_use_anchor, DefiningAnchor :: Error ) ;
12181219 self . inner . borrow ( ) . opaque_type_storage . opaque_types . clone ( )
12191220 }
12201221
0 commit comments