3131//! This order consistency is required in a few places in rustc, for
3232//! example generator inference, and possibly also HIR borrowck.
3333
34- use crate :: hir:: map:: Map ;
35-
3634use rustc_hir:: itemlikevisit:: { ItemLikeVisitor , ParItemLikeVisitor } ;
3735use rustc_hir:: * ;
3836use rustc_span:: Span ;
@@ -42,10 +40,7 @@ pub struct DeepVisitor<'v, V> {
4240 visitor : & ' v mut V ,
4341}
4442
45- impl < ' v , ' hir , V > DeepVisitor < ' v , V >
46- where
47- V : Visitor < ' hir > + ' v ,
48- {
43+ impl < ' v , V > DeepVisitor < ' v , V > {
4944 pub fn new ( base : & ' v mut V ) -> Self {
5045 DeepVisitor { visitor : base }
5146 }
@@ -122,14 +117,22 @@ impl<'a> FnKind<'a> {
122117 }
123118}
124119
120+ /// An abstract representation of the HIR `rustc::hir::map::Map`.
121+ pub trait Map < ' hir > {
122+ fn body ( & self , id : BodyId ) -> & ' hir Body < ' hir > ;
123+ fn item ( & self , id : HirId ) -> & ' hir Item < ' hir > ;
124+ fn trait_item ( & self , id : TraitItemId ) -> & ' hir TraitItem < ' hir > ;
125+ fn impl_item ( & self , id : ImplItemId ) -> & ' hir ImplItem < ' hir > ;
126+ }
127+
125128/// Specifies what nested things a visitor wants to visit. The most
126129/// common choice is `OnlyBodies`, which will cause the visitor to
127130/// visit fn bodies for fns that it encounters, but skip over nested
128131/// item-like things.
129132///
130133/// See the comments on `ItemLikeVisitor` for more details on the overall
131134/// visit strategy.
132- pub enum NestedVisitorMap < ' this , ' tcx > {
135+ pub enum NestedVisitorMap < ' this , M > {
133136 /// Do not visit any nested things. When you add a new
134137 /// "non-nested" thing, you will want to audit such uses to see if
135138 /// they remain valid.
@@ -146,20 +149,20 @@ pub enum NestedVisitorMap<'this, 'tcx> {
146149 /// to use `visit_all_item_likes()` as an outer loop,
147150 /// and to have the visitor that visits the contents of each item
148151 /// using this setting.
149- OnlyBodies ( & ' this Map < ' tcx > ) ,
152+ OnlyBodies ( & ' this M ) ,
150153
151154 /// Visits all nested things, including item-likes.
152155 ///
153156 /// **This is an unusual choice.** It is used when you want to
154157 /// process everything within their lexical context. Typically you
155158 /// kick off the visit by doing `walk_krate()`.
156- All ( & ' this Map < ' tcx > ) ,
159+ All ( & ' this M ) ,
157160}
158161
159- impl < ' this , ' tcx > NestedVisitorMap < ' this , ' tcx > {
162+ impl < ' this , M > NestedVisitorMap < ' this , M > {
160163 /// Returns the map to use for an "intra item-like" thing (if any).
161164 /// E.g., function body.
162- fn intra ( self ) -> Option < & ' this Map < ' tcx > > {
165+ fn intra ( self ) -> Option < & ' this M > {
163166 match self {
164167 NestedVisitorMap :: None => None ,
165168 NestedVisitorMap :: OnlyBodies ( map) => Some ( map) ,
@@ -169,7 +172,7 @@ impl<'this, 'tcx> NestedVisitorMap<'this, 'tcx> {
169172
170173 /// Returns the map to use for an "item-like" thing (if any).
171174 /// E.g., item, impl-item.
172- fn inter ( self ) -> Option < & ' this Map < ' tcx > > {
175+ fn inter ( self ) -> Option < & ' this M > {
173176 match self {
174177 NestedVisitorMap :: None => None ,
175178 NestedVisitorMap :: OnlyBodies ( _) => None ,
@@ -195,6 +198,8 @@ impl<'this, 'tcx> NestedVisitorMap<'this, 'tcx> {
195198/// to monitor future changes to `Visitor` in case a new method with a
196199/// new default implementation gets introduced.)
197200pub trait Visitor < ' v > : Sized {
201+ type Map : Map < ' v > ;
202+
198203 ///////////////////////////////////////////////////////////////////////////
199204 // Nested items.
200205
@@ -214,7 +219,7 @@ pub trait Visitor<'v>: Sized {
214219 /// `panic!()`. This way, if a new `visit_nested_XXX` variant is
215220 /// added in the future, we will see the panic in your code and
216221 /// fix it appropriately.
217- fn nested_visit_map ( & mut self ) -> NestedVisitorMap < ' _ , ' v > ;
222+ fn nested_visit_map ( & mut self ) -> NestedVisitorMap < ' _ , Self :: Map > ;
218223
219224 /// Invoked when a nested item is encountered. By default does
220225 /// nothing unless you override `nested_visit_map` to return other than
@@ -496,21 +501,16 @@ pub fn walk_lifetime<'v, V: Visitor<'v>>(visitor: &mut V, lifetime: &'v Lifetime
496501 }
497502}
498503
499- pub fn walk_poly_trait_ref < ' v , V > (
504+ pub fn walk_poly_trait_ref < ' v , V : Visitor < ' v > > (
500505 visitor : & mut V ,
501506 trait_ref : & ' v PolyTraitRef < ' v > ,
502507 _modifier : TraitBoundModifier ,
503- ) where
504- V : Visitor < ' v > ,
505- {
508+ ) {
506509 walk_list ! ( visitor, visit_generic_param, trait_ref. bound_generic_params) ;
507510 visitor. visit_trait_ref ( & trait_ref. trait_ref ) ;
508511}
509512
510- pub fn walk_trait_ref < ' v , V > ( visitor : & mut V , trait_ref : & ' v TraitRef < ' v > )
511- where
512- V : Visitor < ' v > ,
513- {
513+ pub fn walk_trait_ref < ' v , V : Visitor < ' v > > ( visitor : & mut V , trait_ref : & ' v TraitRef < ' v > ) {
514514 visitor. visit_id ( trait_ref. hir_ref_id ) ;
515515 visitor. visit_path ( & trait_ref. path , trait_ref. hir_ref_id )
516516}
0 commit comments