18
18
//! within one another.
19
19
//! - Example: Examine each expression to look for its type and do some check or other.
20
20
//! - How: Implement `intravisit::Visitor` and override the `NestedFilter` type to
21
- //! `nested_filter::OnlyBodies` (and implement `nested_visit_map `), and use
21
+ //! `nested_filter::OnlyBodies` (and implement `maybe_tcx `), and use
22
22
//! `tcx.hir().visit_all_item_likes_in_crate(&mut visitor)`. Within your
23
23
//! `intravisit::Visitor` impl, implement methods like `visit_expr()` (don't forget to invoke
24
24
//! `intravisit::walk_expr()` to keep walking the subparts).
30
30
//! - Example: Lifetime resolution, which wants to bring lifetimes declared on the
31
31
//! impl into scope while visiting the impl-items, and then back out again.
32
32
//! - How: Implement `intravisit::Visitor` and override the `NestedFilter` type to
33
- //! `nested_filter::All` (and implement `nested_visit_map `). Walk your crate with
33
+ //! `nested_filter::All` (and implement `maybe_tcx `). Walk your crate with
34
34
//! `tcx.hir().walk_toplevel_module(visitor)` invoked on `tcx.hir().krate()`.
35
35
//! - Pro: Visitor methods for any kind of HIR node, not just item-like things.
36
36
//! - Pro: Preserves nesting information
@@ -106,41 +106,43 @@ impl<'a> FnKind<'a> {
106
106
}
107
107
}
108
108
109
- /// An abstract representation of the HIR `rustc_middle::hir::map::Map`.
110
- pub trait Map < ' hir > {
109
+ /// HIR things retrievable from `TyCtxt`, avoiding an explicit dependence on
110
+ /// `TyCtxt`. The only impls are for `!` (where these functions are never
111
+ /// called) and `TyCtxt` (in `rustc_middle`).
112
+ pub trait HirTyCtxt < ' hir > {
111
113
/// Retrieves the `Node` corresponding to `id`.
112
114
fn hir_node ( & self , hir_id : HirId ) -> Node < ' hir > ;
113
- fn body ( & self , id : BodyId ) -> & ' hir Body < ' hir > ;
114
- fn item ( & self , id : ItemId ) -> & ' hir Item < ' hir > ;
115
- fn trait_item ( & self , id : TraitItemId ) -> & ' hir TraitItem < ' hir > ;
116
- fn impl_item ( & self , id : ImplItemId ) -> & ' hir ImplItem < ' hir > ;
117
- fn foreign_item ( & self , id : ForeignItemId ) -> & ' hir ForeignItem < ' hir > ;
115
+ fn hir_body ( & self , id : BodyId ) -> & ' hir Body < ' hir > ;
116
+ fn hir_item ( & self , id : ItemId ) -> & ' hir Item < ' hir > ;
117
+ fn hir_trait_item ( & self , id : TraitItemId ) -> & ' hir TraitItem < ' hir > ;
118
+ fn hir_impl_item ( & self , id : ImplItemId ) -> & ' hir ImplItem < ' hir > ;
119
+ fn hir_foreign_item ( & self , id : ForeignItemId ) -> & ' hir ForeignItem < ' hir > ;
118
120
}
119
121
120
- // Used when no map is actually available, forcing manual implementation of nested visitors.
121
- impl < ' hir > Map < ' hir > for ! {
122
+ // Used when no tcx is actually available, forcing manual implementation of nested visitors.
123
+ impl < ' hir > HirTyCtxt < ' hir > for ! {
122
124
fn hir_node ( & self , _: HirId ) -> Node < ' hir > {
123
125
unreachable ! ( ) ;
124
126
}
125
- fn body ( & self , _: BodyId ) -> & ' hir Body < ' hir > {
127
+ fn hir_body ( & self , _: BodyId ) -> & ' hir Body < ' hir > {
126
128
unreachable ! ( ) ;
127
129
}
128
- fn item ( & self , _: ItemId ) -> & ' hir Item < ' hir > {
130
+ fn hir_item ( & self , _: ItemId ) -> & ' hir Item < ' hir > {
129
131
unreachable ! ( ) ;
130
132
}
131
- fn trait_item ( & self , _: TraitItemId ) -> & ' hir TraitItem < ' hir > {
133
+ fn hir_trait_item ( & self , _: TraitItemId ) -> & ' hir TraitItem < ' hir > {
132
134
unreachable ! ( ) ;
133
135
}
134
- fn impl_item ( & self , _: ImplItemId ) -> & ' hir ImplItem < ' hir > {
136
+ fn hir_impl_item ( & self , _: ImplItemId ) -> & ' hir ImplItem < ' hir > {
135
137
unreachable ! ( ) ;
136
138
}
137
- fn foreign_item ( & self , _: ForeignItemId ) -> & ' hir ForeignItem < ' hir > {
139
+ fn hir_foreign_item ( & self , _: ForeignItemId ) -> & ' hir ForeignItem < ' hir > {
138
140
unreachable ! ( ) ;
139
141
}
140
142
}
141
143
142
144
pub mod nested_filter {
143
- use super :: Map ;
145
+ use super :: HirTyCtxt ;
144
146
145
147
/// Specifies what nested things a visitor wants to visit. By "nested
146
148
/// things", we are referring to bits of HIR that are not directly embedded
@@ -155,7 +157,7 @@ pub mod nested_filter {
155
157
/// See the comments at [`rustc_hir::intravisit`] for more details on the overall
156
158
/// visit strategy.
157
159
pub trait NestedFilter < ' hir > {
158
- type Map : Map < ' hir > ;
160
+ type MaybeTyCtxt : HirTyCtxt < ' hir > ;
159
161
160
162
/// Whether the visitor visits nested "item-like" things.
161
163
/// E.g., item, impl-item.
@@ -171,10 +173,10 @@ pub mod nested_filter {
171
173
///
172
174
/// Use this if you are only walking some particular kind of tree
173
175
/// (i.e., a type, or fn signature) and you don't want to thread a
174
- /// HIR map around.
176
+ /// `tcx` around.
175
177
pub struct None ( ( ) ) ;
176
178
impl NestedFilter < ' _ > for None {
177
- type Map = !;
179
+ type MaybeTyCtxt = !;
178
180
const INTER : bool = false ;
179
181
const INTRA : bool = false ;
180
182
}
@@ -199,18 +201,18 @@ use nested_filter::NestedFilter;
199
201
/// to monitor future changes to `Visitor` in case a new method with a
200
202
/// new default implementation gets introduced.)
201
203
pub trait Visitor < ' v > : Sized {
202
- // This type should not be overridden, it exists for convenient usage as `Self::Map `.
203
- type Map : Map < ' v > = <Self :: NestedFilter as NestedFilter < ' v > >:: Map ;
204
+ // This type should not be overridden, it exists for convenient usage as `Self::MaybeTyCtxt `.
205
+ type MaybeTyCtxt : HirTyCtxt < ' v > = <Self :: NestedFilter as NestedFilter < ' v > >:: MaybeTyCtxt ;
204
206
205
207
///////////////////////////////////////////////////////////////////////////
206
208
// Nested items.
207
209
208
210
/// Override this type to control which nested HIR are visited; see
209
211
/// [`NestedFilter`] for details. If you override this type, you
210
- /// must also override [`nested_visit_map `](Self::nested_visit_map ).
212
+ /// must also override [`maybe_tcx `](Self::maybe_tcx ).
211
213
///
212
214
/// **If for some reason you want the nested behavior, but don't
213
- /// have a `Map ` at your disposal:** then override the
215
+ /// have a `tcx ` at your disposal:** then override the
214
216
/// `visit_nested_XXX` methods. If a new `visit_nested_XXX` variant is
215
217
/// added in the future, it will cause a panic which can be detected
216
218
/// and fixed appropriately.
@@ -222,9 +224,9 @@ pub trait Visitor<'v>: Sized {
222
224
223
225
/// If `type NestedFilter` is set to visit nested items, this method
224
226
/// must also be overridden to provide a map to retrieve nested items.
225
- fn nested_visit_map ( & mut self ) -> Self :: Map {
227
+ fn maybe_tcx ( & mut self ) -> Self :: MaybeTyCtxt {
226
228
panic ! (
227
- "nested_visit_map must be implemented or consider using \
229
+ "maybe_tcx must be implemented or consider using \
228
230
`type NestedFilter = nested_filter::None` (the default)"
229
231
) ;
230
232
}
@@ -236,10 +238,10 @@ pub trait Visitor<'v>: Sized {
236
238
/// "deep" visit patterns described at
237
239
/// [`rustc_hir::intravisit`]. The only reason to override
238
240
/// this method is if you want a nested pattern but cannot supply a
239
- /// [`Map `]; see `nested_visit_map ` for advice.
241
+ /// [`TyCtxt `]; see `maybe_tcx ` for advice.
240
242
fn visit_nested_item ( & mut self , id : ItemId ) -> Self :: Result {
241
243
if Self :: NestedFilter :: INTER {
242
- let item = self . nested_visit_map ( ) . item ( id) ;
244
+ let item = self . maybe_tcx ( ) . hir_item ( id) ;
243
245
try_visit ! ( self . visit_item( item) ) ;
244
246
}
245
247
Self :: Result :: output ( )
@@ -250,7 +252,7 @@ pub trait Visitor<'v>: Sized {
250
252
/// method.
251
253
fn visit_nested_trait_item ( & mut self , id : TraitItemId ) -> Self :: Result {
252
254
if Self :: NestedFilter :: INTER {
253
- let item = self . nested_visit_map ( ) . trait_item ( id) ;
255
+ let item = self . maybe_tcx ( ) . hir_trait_item ( id) ;
254
256
try_visit ! ( self . visit_trait_item( item) ) ;
255
257
}
256
258
Self :: Result :: output ( )
@@ -261,7 +263,7 @@ pub trait Visitor<'v>: Sized {
261
263
/// method.
262
264
fn visit_nested_impl_item ( & mut self , id : ImplItemId ) -> Self :: Result {
263
265
if Self :: NestedFilter :: INTER {
264
- let item = self . nested_visit_map ( ) . impl_item ( id) ;
266
+ let item = self . maybe_tcx ( ) . hir_impl_item ( id) ;
265
267
try_visit ! ( self . visit_impl_item( item) ) ;
266
268
}
267
269
Self :: Result :: output ( )
@@ -272,7 +274,7 @@ pub trait Visitor<'v>: Sized {
272
274
/// method.
273
275
fn visit_nested_foreign_item ( & mut self , id : ForeignItemId ) -> Self :: Result {
274
276
if Self :: NestedFilter :: INTER {
275
- let item = self . nested_visit_map ( ) . foreign_item ( id) ;
277
+ let item = self . maybe_tcx ( ) . hir_foreign_item ( id) ;
276
278
try_visit ! ( self . visit_foreign_item( item) ) ;
277
279
}
278
280
Self :: Result :: output ( )
@@ -283,7 +285,7 @@ pub trait Visitor<'v>: Sized {
283
285
/// `Self::NestedFilter`.
284
286
fn visit_nested_body ( & mut self , id : BodyId ) -> Self :: Result {
285
287
if Self :: NestedFilter :: INTRA {
286
- let body = self . nested_visit_map ( ) . body ( id) ;
288
+ let body = self . maybe_tcx ( ) . hir_body ( id) ;
287
289
try_visit ! ( self . visit_body( body) ) ;
288
290
}
289
291
Self :: Result :: output ( )
0 commit comments