@@ -3,10 +3,9 @@ use super::{DefineOpaqueTypes, InferResult};
33use crate :: errors:: OpaqueHiddenTypeDiag ;
44use crate :: infer:: { DefiningAnchor , InferCtxt , InferOk } ;
55use crate :: traits;
6- use hir:: def:: DefKind ;
76use hir:: def_id:: { DefId , LocalDefId } ;
87use hir:: OpaqueTyOrigin ;
9- use rustc_data_structures:: fx:: { FxHashSet , FxIndexMap } ;
8+ use rustc_data_structures:: fx:: FxIndexMap ;
109use rustc_data_structures:: sync:: Lrc ;
1110use rustc_hir as hir;
1211use rustc_middle:: traits:: ObligationCause ;
@@ -54,9 +53,7 @@ impl<'tcx> InferCtxt<'tcx> {
5453 }
5554 let mut obligations = vec ! [ ] ;
5655 let replace_opaque_type = |def_id : DefId | {
57- def_id
58- . as_local ( )
59- . map_or ( false , |def_id| self . opaque_type_origin ( def_id, param_env) . is_some ( ) )
56+ def_id. as_local ( ) . map_or ( false , |def_id| self . opaque_type_origin ( def_id) . is_some ( ) )
6057 } ;
6158 let value = value. fold_with ( & mut BottomUpFolder {
6259 tcx : self . tcx ,
@@ -141,7 +138,7 @@ impl<'tcx> InferCtxt<'tcx> {
141138 // let x = || foo(); // returns the Opaque assoc with `foo`
142139 // }
143140 // ```
144- self . opaque_type_origin ( def_id, param_env ) ?
141+ self . opaque_type_origin ( def_id) ?
145142 }
146143 DefiningAnchor :: Bubble => self . opaque_type_origin_unchecked ( def_id) ,
147144 DefiningAnchor :: Error => return None ,
@@ -152,9 +149,8 @@ impl<'tcx> InferCtxt<'tcx> {
152149 // no one encounters it in practice.
153150 // It does occur however in `fn fut() -> impl Future<Output = i32> { async { 42 } }`,
154151 // where it is of no concern, so we only check for TAITs.
155- if let Some ( OpaqueTyOrigin :: TyAlias { .. } ) = b_def_id
156- . as_local ( )
157- . and_then ( |b_def_id| self . opaque_type_origin ( b_def_id, param_env) )
152+ if let Some ( OpaqueTyOrigin :: TyAlias { .. } ) =
153+ b_def_id. as_local ( ) . and_then ( |b_def_id| self . opaque_type_origin ( b_def_id) )
158154 {
159155 self . tcx . sess . emit_err ( OpaqueHiddenTypeDiag {
160156 span : cause. span ,
@@ -370,12 +366,8 @@ impl<'tcx> InferCtxt<'tcx> {
370366
371367 /// Returns the origin of the opaque type `def_id` if we're currently
372368 /// in its defining scope.
373- #[ instrument( skip( self , param_env) , level = "trace" , ret) ]
374- pub fn opaque_type_origin (
375- & self ,
376- def_id : LocalDefId ,
377- param_env : ty:: ParamEnv < ' tcx > ,
378- ) -> Option < OpaqueTyOrigin > {
369+ #[ instrument( skip( self ) , level = "trace" , ret) ]
370+ pub fn opaque_type_origin ( & self , def_id : LocalDefId ) -> Option < OpaqueTyOrigin > {
379371 let opaque_hir_id = self . tcx . hir ( ) . local_def_id_to_hir_id ( def_id) ;
380372 let parent_def_id = match self . defining_use_anchor {
381373 DefiningAnchor :: Bubble | DefiningAnchor :: Error => return None ,
@@ -391,7 +383,7 @@ impl<'tcx> InferCtxt<'tcx> {
391383 // Named `type Foo = impl Bar;`
392384 hir:: OpaqueTyOrigin :: TyAlias { in_assoc_ty } => {
393385 if in_assoc_ty {
394- may_define_impl_trait_in_assoc_ty ( self . tcx , parent_def_id, def_id, param_env )
386+ self . tcx . opaque_types_defined_by ( parent_def_id) . contains ( & def_id)
395387 } else {
396388 may_define_opaque_type ( self . tcx , parent_def_id, opaque_hir_id)
397389 }
@@ -654,105 +646,3 @@ fn may_define_opaque_type(tcx: TyCtxt<'_>, def_id: LocalDefId, opaque_hir_id: hi
654646 ) ;
655647 res
656648}
657-
658- #[ derive( Debug , TypeVisitable , Clone ) ]
659- /// Helper datastructure containing the signature
660- /// that the opaque type extraction logic uses for determining
661- /// whether an opaque type may have its hidden types registered
662- /// by an item.
663- enum FnSigOrTy < ' tcx > {
664- FnSig ( ty:: PolyFnSig < ' tcx > ) ,
665- Ty ( Ty < ' tcx > ) ,
666- }
667-
668- /// Checks that the item may register hidden types for the
669- /// opaque type, if the opaque type shows up in its signature.
670- #[ instrument( level = "debug" , skip( tcx) , ret) ]
671- pub fn may_define_impl_trait_in_assoc_ty_modulo_sig < ' tcx > (
672- tcx : TyCtxt < ' tcx > ,
673- def_id : LocalDefId ,
674- opaque_def_id : LocalDefId ,
675- ) -> Option < impl TypeVisitable < TyCtxt < ' tcx > > > {
676- let sig = match tcx. def_kind ( def_id) {
677- DefKind :: AssocFn => FnSigOrTy :: FnSig ( tcx. fn_sig ( def_id) . subst_identity ( ) ) ,
678- DefKind :: AssocConst | DefKind :: AssocTy => {
679- FnSigOrTy :: Ty ( tcx. type_of ( def_id) . subst_identity ( ) )
680- }
681- _ => return None ,
682- } ;
683- let impl_id = tcx. local_parent ( def_id) ;
684- trace ! ( ?impl_id) ;
685- let mut assoc_id = opaque_def_id;
686- // Peel nested opaque types.
687- while let DefKind :: OpaqueTy = tcx. def_kind ( assoc_id) {
688- trace ! ( ?assoc_id) ;
689- assoc_id = tcx. local_parent ( assoc_id) ;
690- }
691- trace ! ( ?assoc_id) ;
692- if !matches ! ( tcx. def_kind( assoc_id) , DefKind :: AssocTy ) {
693- tcx. sess
694- . delay_span_bug ( tcx. def_span ( opaque_def_id) , format ! ( "{:?}" , tcx. def_kind( assoc_id) ) ) ;
695- }
696- let assoc_impl_id = tcx. local_parent ( assoc_id) ;
697- trace ! ( ?assoc_impl_id) ;
698-
699- if impl_id != assoc_impl_id {
700- return None ;
701- }
702-
703- Some ( sig)
704- }
705-
706- #[ instrument( level = "debug" , skip( tcx, param_env) , ret) ]
707- fn may_define_impl_trait_in_assoc_ty < ' tcx > (
708- tcx : TyCtxt < ' tcx > ,
709- def_id : LocalDefId ,
710- opaque_def_id : LocalDefId ,
711- param_env : ty:: ParamEnv < ' tcx > ,
712- ) -> bool {
713- let Some ( sig) = may_define_impl_trait_in_assoc_ty_modulo_sig ( tcx, def_id, opaque_def_id) else {
714- return false ;
715- } ;
716-
717- struct Visitor < ' tcx > {
718- opaque_def_id : LocalDefId ,
719- param_env : ty:: ParamEnv < ' tcx > ,
720- tcx : TyCtxt < ' tcx > ,
721- seen : FxHashSet < LocalDefId > ,
722- }
723-
724- impl < ' tcx > TypeVisitor < TyCtxt < ' tcx > > for Visitor < ' tcx > {
725- type BreakTy = ( ) ;
726- #[ instrument( level = "trace" , skip( self ) , ret) ]
727- fn visit_ty ( & mut self , ty : Ty < ' tcx > ) -> ControlFlow < ( ) > {
728- // FIXME(oli-obk): We should be checking if the associated type
729- // is mentioned instead of normalizing to find the opaque type.
730- // But that requires a way to figure out that a projection refers
731- // to a specific opaque type. That is probably doable by checking for
732- // `Self` as the `substs[0]`.
733- let normalized_ty = self . tcx . normalize_erasing_regions ( self . param_env , ty) ;
734- if let ty:: Alias ( ty:: Opaque , alias) = normalized_ty. kind ( ) {
735- if let Some ( def_id) = alias. def_id . as_local ( ) {
736- trace ! ( ?alias. def_id) ;
737- if def_id == self . opaque_def_id {
738- return ControlFlow :: Break ( ( ) ) ;
739- }
740-
741- if self . seen . insert ( def_id) {
742- // Look into nested obligations like `impl Trait<Assoc = impl OtherTrait>`.
743- for ( pred, _) in self
744- . tcx
745- . explicit_item_bounds ( alias. def_id )
746- . subst_iter_copied ( self . tcx , alias. substs )
747- {
748- pred. visit_with ( self ) ?;
749- }
750- }
751- }
752- }
753- normalized_ty. super_visit_with ( self )
754- }
755- }
756- sig. visit_with ( & mut Visitor { opaque_def_id, param_env, tcx, seen : Default :: default ( ) } )
757- . is_break ( )
758- }
0 commit comments