1- use rustc_data_structures :: fx :: FxIndexSet ;
1+ use rustc_hir as hir ;
22use rustc_hir:: def:: DefKind ;
33use rustc_hir:: def_id:: { DefId , DefIdMap , LocalDefId } ;
44use rustc_hir:: definitions:: { DefPathData , DisambiguatorState } ;
55use rustc_hir:: intravisit:: { self , Visitor } ;
6- use rustc_hir:: { self as hir, AmbigArg } ;
76use rustc_middle:: query:: Providers ;
87use rustc_middle:: ty:: { self , ImplTraitInTraitData , TyCtxt } ;
98use rustc_middle:: { bug, span_bug} ;
@@ -14,7 +13,6 @@ pub(crate) fn provide(providers: &mut Providers) {
1413 associated_item_def_ids,
1514 associated_items,
1615 associated_types_for_impl_traits_in_associated_fn,
17- associated_type_for_impl_trait_in_trait,
1816 impl_item_implementor_ids,
1917 ..* providers
2018 } ;
@@ -160,20 +158,22 @@ fn associated_item_from_impl_item_ref(impl_item_ref: &hir::ImplItemRef) -> ty::A
160158 container : ty:: AssocItemContainer :: Impl ,
161159 }
162160}
163- struct RPITVisitor {
164- rpits : FxIndexSet < LocalDefId > ,
161+ struct RPITVisitor < ' tcx > {
162+ tcx : TyCtxt < ' tcx > ,
163+ synthetics : Vec < LocalDefId > ,
164+ data : DefPathData ,
165+ disambiguator : DisambiguatorState ,
165166}
166167
167- impl < ' tcx > Visitor < ' tcx > for RPITVisitor {
168- fn visit_ty ( & mut self , ty : & ' tcx hir:: Ty < ' tcx , AmbigArg > ) {
169- if let hir:: TyKind :: OpaqueDef ( opaq) = ty. kind
170- && self . rpits . insert ( opaq. def_id )
171- {
172- for bound in opaq. bounds {
173- intravisit:: walk_param_bound ( self , bound) ;
174- }
175- }
176- intravisit:: walk_ty ( self , ty)
168+ impl < ' tcx > Visitor < ' tcx > for RPITVisitor < ' tcx > {
169+ fn visit_opaque_ty ( & mut self , opaque : & ' tcx hir:: OpaqueTy < ' tcx > ) -> Self :: Result {
170+ self . synthetics . push ( associated_type_for_impl_trait_in_trait (
171+ self . tcx ,
172+ opaque. def_id ,
173+ self . data ,
174+ & mut self . disambiguator ,
175+ ) ) ;
176+ intravisit:: walk_opaque_ty ( self , opaque)
177177 }
178178}
179179
@@ -194,14 +194,18 @@ fn associated_types_for_impl_traits_in_associated_fn(
194194
195195 match tcx. def_kind ( parent_def_id) {
196196 DefKind :: Trait => {
197- let mut visitor = RPITVisitor { rpits : FxIndexSet :: default ( ) } ;
198-
199197 if let Some ( output) = tcx. hir_get_fn_output ( fn_def_id) {
198+ let data = DefPathData :: AnonAssocTy ( tcx. item_name ( fn_def_id. to_def_id ( ) ) ) ;
199+ let mut visitor = RPITVisitor {
200+ tcx,
201+ synthetics : vec ! [ ] ,
202+ data,
203+ disambiguator : DisambiguatorState :: with ( parent_def_id, data, 0 ) ,
204+ } ;
200205 visitor. visit_fn_ret_ty ( output) ;
201-
202- tcx. arena . alloc_from_iter ( visitor. rpits . iter ( ) . map ( |opaque_ty_def_id| {
203- tcx. associated_type_for_impl_trait_in_trait ( opaque_ty_def_id) . to_def_id ( )
204- } ) )
206+ tcx. arena . alloc_from_iter (
207+ visitor. synthetics . into_iter ( ) . map ( |def_id| def_id. to_def_id ( ) ) ,
208+ )
205209 } else {
206210 & [ ]
207211 }
@@ -211,7 +215,6 @@ fn associated_types_for_impl_traits_in_associated_fn(
211215 let Some ( trait_fn_def_id) = tcx. associated_item ( fn_def_id) . trait_item_def_id else {
212216 return & [ ] ;
213217 } ;
214-
215218 tcx. arena . alloc_from_iter (
216219 tcx. associated_types_for_impl_traits_in_associated_fn ( trait_fn_def_id) . iter ( ) . map (
217220 move |& trait_assoc_def_id| {
@@ -236,6 +239,8 @@ fn associated_types_for_impl_traits_in_associated_fn(
236239fn associated_type_for_impl_trait_in_trait (
237240 tcx : TyCtxt < ' _ > ,
238241 opaque_ty_def_id : LocalDefId ,
242+ data : DefPathData ,
243+ disambiguator : & mut DisambiguatorState ,
239244) -> LocalDefId {
240245 let ( hir:: OpaqueTyOrigin :: FnReturn { parent : fn_def_id, .. }
241246 | hir:: OpaqueTyOrigin :: AsyncFn { parent : fn_def_id, .. } ) =
@@ -246,22 +251,15 @@ fn associated_type_for_impl_trait_in_trait(
246251 let trait_def_id = tcx. local_parent ( fn_def_id) ;
247252 assert_eq ! ( tcx. def_kind( trait_def_id) , DefKind :: Trait ) ;
248253
249- // Collect all opaque types in return position for the method and use
250- // the index as the disambiguator to make an unique def path.
251- let mut visitor = RPITVisitor { rpits : FxIndexSet :: default ( ) } ;
252- visitor. visit_fn_ret_ty ( tcx. hir_get_fn_output ( fn_def_id) . unwrap ( ) ) ;
253- let disambiguator = visitor. rpits . get_index_of ( & opaque_ty_def_id) . unwrap ( ) . try_into ( ) . unwrap ( ) ;
254-
255254 let span = tcx. def_span ( opaque_ty_def_id) ;
256255 // Also use the method name to create an unique def path.
257- let data = DefPathData :: AnonAssocTy ( tcx. item_name ( fn_def_id. to_def_id ( ) ) ) ;
258256 let trait_assoc_ty = tcx. at ( span) . create_def (
259257 trait_def_id,
260258 // No name because this is an anonymous associated type.
261259 None ,
262260 DefKind :: AssocTy ,
263261 Some ( data) ,
264- & mut DisambiguatorState :: with ( trait_def_id , data , disambiguator) ,
262+ disambiguator,
265263 ) ;
266264
267265 let local_def_id = trait_assoc_ty. def_id ( ) ;
0 commit comments