@@ -84,7 +84,7 @@ pub trait DefIdVisitor<'tcx> {
8484 fn skeleton ( & mut self ) -> DefIdVisitorSkeleton < ' _ , ' tcx , Self > {
8585 DefIdVisitorSkeleton {
8686 def_id_visitor : self ,
87- visited_def_ids : Default :: default ( ) ,
87+ visited_tys : Default :: default ( ) ,
8888 dummy : Default :: default ( ) ,
8989 }
9090 }
@@ -104,7 +104,7 @@ pub trait DefIdVisitor<'tcx> {
104104
105105pub struct DefIdVisitorSkeleton < ' v , ' tcx , V : ?Sized > {
106106 def_id_visitor : & ' v mut V ,
107- visited_def_ids : FxHashSet < DefId > ,
107+ visited_tys : FxHashSet < Ty < ' tcx > > ,
108108 dummy : PhantomData < TyCtxt < ' tcx > > ,
109109}
110110
@@ -114,13 +114,11 @@ where
114114{
115115 fn visit_trait ( & mut self , trait_ref : TraitRef < ' tcx > ) -> V :: Result {
116116 let TraitRef { def_id, args, .. } = trait_ref;
117- if self . visited_def_ids . insert ( def_id) {
118- try_visit ! ( self . def_id_visitor. visit_def_id(
119- def_id,
120- "trait" ,
121- & trait_ref. print_only_trait_path( )
122- ) ) ;
123- }
117+ try_visit ! ( self . def_id_visitor. visit_def_id(
118+ def_id,
119+ "trait" ,
120+ & trait_ref. print_only_trait_path( )
121+ ) ) ;
124122 if V :: SHALLOW { V :: Result :: output ( ) } else { args. visit_with ( self ) }
125123 }
126124
@@ -184,6 +182,9 @@ where
184182 }
185183
186184 fn visit_ty ( & mut self , ty : Ty < ' tcx > ) -> Self :: Result {
185+ if !self . visited_tys . insert ( ty) {
186+ return V :: Result :: output ( ) ;
187+ }
187188 let tcx = self . def_id_visitor . tcx ( ) ;
188189 // GenericArgs are not visited here because they are visited below
189190 // in `super_visit_with`.
@@ -194,9 +195,7 @@ where
194195 | ty:: Closure ( def_id, ..)
195196 | ty:: CoroutineClosure ( def_id, ..)
196197 | ty:: Coroutine ( def_id, ..) => {
197- if self . visited_def_ids . insert ( def_id) {
198- try_visit ! ( self . def_id_visitor. visit_def_id( def_id, "type" , & ty) ) ;
199- }
198+ try_visit ! ( self . def_id_visitor. visit_def_id( def_id, "type" , & ty) ) ;
200199 if V :: SHALLOW {
201200 return V :: Result :: output ( ) ;
202201 }
@@ -227,17 +226,15 @@ where
227226 return V :: Result :: output ( ) ;
228227 }
229228
230- if self . visited_def_ids . insert ( data. def_id ) {
231- try_visit ! ( self . def_id_visitor. visit_def_id(
232- data. def_id,
233- match kind {
234- ty:: Inherent | ty:: Projection => "associated type" ,
235- ty:: Free => "type alias" ,
236- ty:: Opaque => unreachable!( ) ,
237- } ,
238- & LazyDefPathStr { def_id: data. def_id, tcx } ,
239- ) ) ;
240- }
229+ try_visit ! ( self . def_id_visitor. visit_def_id(
230+ data. def_id,
231+ match kind {
232+ ty:: Inherent | ty:: Projection => "associated type" ,
233+ ty:: Free => "type alias" ,
234+ ty:: Opaque => unreachable!( ) ,
235+ } ,
236+ & LazyDefPathStr { def_id: data. def_id, tcx } ,
237+ ) ) ;
241238
242239 // This will also visit args if necessary, so we don't need to recurse.
243240 return if V :: SHALLOW {
@@ -262,23 +259,18 @@ where
262259 }
263260 } ;
264261 let ty:: ExistentialTraitRef { def_id, .. } = trait_ref;
265- if self . visited_def_ids . insert ( def_id) {
266- try_visit ! ( self . def_id_visitor. visit_def_id( def_id, "trait" , & trait_ref) ) ;
267- }
262+ try_visit ! ( self . def_id_visitor. visit_def_id( def_id, "trait" , & trait_ref) ) ;
268263 }
269264 }
270265 ty:: Alias ( ty:: Opaque , ty:: AliasTy { def_id, .. } ) => {
271- // Skip repeated `Opaque`s to avoid infinite recursion.
272- if self . visited_def_ids . insert ( def_id) {
273- // The intent is to treat `impl Trait1 + Trait2` identically to
274- // `dyn Trait1 + Trait2`. Therefore we ignore def-id of the opaque type itself
275- // (it either has no visibility, or its visibility is insignificant, like
276- // visibilities of type aliases) and recurse into bounds instead to go
277- // through the trait list (default type visitor doesn't visit those traits).
278- // All traits in the list are considered the "primary" part of the type
279- // and are visited by shallow visitors.
280- try_visit ! ( self . visit_clauses( tcx. explicit_item_bounds( def_id) . skip_binder( ) ) ) ;
281- }
266+ // The intent is to treat `impl Trait1 + Trait2` identically to
267+ // `dyn Trait1 + Trait2`. Therefore we ignore def-id of the opaque type itself
268+ // (it either has no visibility, or its visibility is insignificant, like
269+ // visibilities of type aliases) and recurse into bounds instead to go
270+ // through the trait list (default type visitor doesn't visit those traits).
271+ // All traits in the list are considered the "primary" part of the type
272+ // and are visited by shallow visitors.
273+ try_visit ! ( self . visit_clauses( tcx. explicit_item_bounds( def_id) . skip_binder( ) ) ) ;
282274 }
283275 // These types don't have their own def-ids (but may have subcomponents
284276 // with def-ids that should be visited recursively).
0 commit comments