31
31
//! This order consistency is required in a few places in rustc, for
32
32
//! example generator inference, and possibly also HIR borrowck.
33
33
34
- use crate :: hir:: map:: Map ;
35
-
36
34
use rustc_hir:: itemlikevisit:: { ItemLikeVisitor , ParItemLikeVisitor } ;
37
35
use rustc_hir:: * ;
38
36
use rustc_span:: Span ;
@@ -42,10 +40,7 @@ pub struct DeepVisitor<'v, V> {
42
40
visitor : & ' v mut V ,
43
41
}
44
42
45
- impl < ' v , ' hir , V > DeepVisitor < ' v , V >
46
- where
47
- V : Visitor < ' hir > + ' v ,
48
- {
43
+ impl < ' v , V > DeepVisitor < ' v , V > {
49
44
pub fn new ( base : & ' v mut V ) -> Self {
50
45
DeepVisitor { visitor : base }
51
46
}
@@ -122,14 +117,22 @@ impl<'a> FnKind<'a> {
122
117
}
123
118
}
124
119
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
+
125
128
/// Specifies what nested things a visitor wants to visit. The most
126
129
/// common choice is `OnlyBodies`, which will cause the visitor to
127
130
/// visit fn bodies for fns that it encounters, but skip over nested
128
131
/// item-like things.
129
132
///
130
133
/// See the comments on `ItemLikeVisitor` for more details on the overall
131
134
/// visit strategy.
132
- pub enum NestedVisitorMap < ' this , ' tcx > {
135
+ pub enum NestedVisitorMap < ' this , M > {
133
136
/// Do not visit any nested things. When you add a new
134
137
/// "non-nested" thing, you will want to audit such uses to see if
135
138
/// they remain valid.
@@ -146,20 +149,20 @@ pub enum NestedVisitorMap<'this, 'tcx> {
146
149
/// to use `visit_all_item_likes()` as an outer loop,
147
150
/// and to have the visitor that visits the contents of each item
148
151
/// using this setting.
149
- OnlyBodies ( & ' this Map < ' tcx > ) ,
152
+ OnlyBodies ( & ' this M ) ,
150
153
151
154
/// Visits all nested things, including item-likes.
152
155
///
153
156
/// **This is an unusual choice.** It is used when you want to
154
157
/// process everything within their lexical context. Typically you
155
158
/// kick off the visit by doing `walk_krate()`.
156
- All ( & ' this Map < ' tcx > ) ,
159
+ All ( & ' this M ) ,
157
160
}
158
161
159
- impl < ' this , ' tcx > NestedVisitorMap < ' this , ' tcx > {
162
+ impl < ' this , M > NestedVisitorMap < ' this , M > {
160
163
/// Returns the map to use for an "intra item-like" thing (if any).
161
164
/// E.g., function body.
162
- fn intra ( self ) -> Option < & ' this Map < ' tcx > > {
165
+ fn intra ( self ) -> Option < & ' this M > {
163
166
match self {
164
167
NestedVisitorMap :: None => None ,
165
168
NestedVisitorMap :: OnlyBodies ( map) => Some ( map) ,
@@ -169,7 +172,7 @@ impl<'this, 'tcx> NestedVisitorMap<'this, 'tcx> {
169
172
170
173
/// Returns the map to use for an "item-like" thing (if any).
171
174
/// E.g., item, impl-item.
172
- fn inter ( self ) -> Option < & ' this Map < ' tcx > > {
175
+ fn inter ( self ) -> Option < & ' this M > {
173
176
match self {
174
177
NestedVisitorMap :: None => None ,
175
178
NestedVisitorMap :: OnlyBodies ( _) => None ,
@@ -195,6 +198,8 @@ impl<'this, 'tcx> NestedVisitorMap<'this, 'tcx> {
195
198
/// to monitor future changes to `Visitor` in case a new method with a
196
199
/// new default implementation gets introduced.)
197
200
pub trait Visitor < ' v > : Sized {
201
+ type Map : Map < ' v > ;
202
+
198
203
///////////////////////////////////////////////////////////////////////////
199
204
// Nested items.
200
205
@@ -214,7 +219,7 @@ pub trait Visitor<'v>: Sized {
214
219
/// `panic!()`. This way, if a new `visit_nested_XXX` variant is
215
220
/// added in the future, we will see the panic in your code and
216
221
/// fix it appropriately.
217
- fn nested_visit_map ( & mut self ) -> NestedVisitorMap < ' _ , ' v > ;
222
+ fn nested_visit_map ( & mut self ) -> NestedVisitorMap < ' _ , Self :: Map > ;
218
223
219
224
/// Invoked when a nested item is encountered. By default does
220
225
/// 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
496
501
}
497
502
}
498
503
499
- pub fn walk_poly_trait_ref < ' v , V > (
504
+ pub fn walk_poly_trait_ref < ' v , V : Visitor < ' v > > (
500
505
visitor : & mut V ,
501
506
trait_ref : & ' v PolyTraitRef < ' v > ,
502
507
_modifier : TraitBoundModifier ,
503
- ) where
504
- V : Visitor < ' v > ,
505
- {
508
+ ) {
506
509
walk_list ! ( visitor, visit_generic_param, trait_ref. bound_generic_params) ;
507
510
visitor. visit_trait_ref ( & trait_ref. trait_ref ) ;
508
511
}
509
512
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 > ) {
514
514
visitor. visit_id ( trait_ref. hir_ref_id ) ;
515
515
visitor. visit_path ( & trait_ref. path , trait_ref. hir_ref_id )
516
516
}
0 commit comments