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 `nested_visit_cx `), 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 `nested_visit_cx `). 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,20 +106,20 @@ 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
+ /// An abstract representation of HIR things retrievable from `TyCtxt `.
110
+ pub trait HirCtxt < ' hir > {
111
111
/// Retrieves the `Node` corresponding to `id`.
112
- fn hir_node ( & self , hir_id : HirId ) -> Node < ' hir > ;
112
+ fn node ( & self , hir_id : HirId ) -> Node < ' hir > ;
113
113
fn body ( & self , id : BodyId ) -> & ' hir Body < ' hir > ;
114
114
fn item ( & self , id : ItemId ) -> & ' hir Item < ' hir > ;
115
115
fn trait_item ( & self , id : TraitItemId ) -> & ' hir TraitItem < ' hir > ;
116
116
fn impl_item ( & self , id : ImplItemId ) -> & ' hir ImplItem < ' hir > ;
117
117
fn foreign_item ( & self , id : ForeignItemId ) -> & ' hir ForeignItem < ' hir > ;
118
118
}
119
119
120
- // Used when no map is actually available, forcing manual implementation of nested visitors.
121
- impl < ' hir > Map < ' hir > for ! {
122
- fn hir_node ( & self , _: HirId ) -> Node < ' hir > {
120
+ // Used when no tcx is actually available, forcing manual implementation of nested visitors.
121
+ impl < ' hir > HirCtxt < ' hir > for ! {
122
+ fn node ( & self , _: HirId ) -> Node < ' hir > {
123
123
unreachable ! ( ) ;
124
124
}
125
125
fn body ( & self , _: BodyId ) -> & ' hir Body < ' hir > {
@@ -140,7 +140,7 @@ impl<'hir> Map<'hir> for ! {
140
140
}
141
141
142
142
pub mod nested_filter {
143
- use super :: Map ;
143
+ use super :: HirCtxt ;
144
144
145
145
/// Specifies what nested things a visitor wants to visit. By "nested
146
146
/// things", we are referring to bits of HIR that are not directly embedded
@@ -155,7 +155,7 @@ pub mod nested_filter {
155
155
/// See the comments at [`rustc_hir::intravisit`] for more details on the overall
156
156
/// visit strategy.
157
157
pub trait NestedFilter < ' hir > {
158
- type Map : Map < ' hir > ;
158
+ type Cx : HirCtxt < ' hir > ;
159
159
160
160
/// Whether the visitor visits nested "item-like" things.
161
161
/// E.g., item, impl-item.
@@ -171,10 +171,10 @@ pub mod nested_filter {
171
171
///
172
172
/// Use this if you are only walking some particular kind of tree
173
173
/// (i.e., a type, or fn signature) and you don't want to thread a
174
- /// HIR map around.
174
+ /// `tcx` around.
175
175
pub struct None ( ( ) ) ;
176
176
impl NestedFilter < ' _ > for None {
177
- type Map = !;
177
+ type Cx = !;
178
178
const INTER : bool = false ;
179
179
const INTRA : bool = false ;
180
180
}
@@ -199,18 +199,18 @@ use nested_filter::NestedFilter;
199
199
/// to monitor future changes to `Visitor` in case a new method with a
200
200
/// new default implementation gets introduced.)
201
201
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 ;
202
+ // This type should not be overridden, it exists for convenient usage as `Self::Cx `.
203
+ type Cx : HirCtxt < ' v > = <Self :: NestedFilter as NestedFilter < ' v > >:: Cx ;
204
204
205
205
///////////////////////////////////////////////////////////////////////////
206
206
// Nested items.
207
207
208
208
/// Override this type to control which nested HIR are visited; see
209
209
/// [`NestedFilter`] for details. If you override this type, you
210
- /// must also override [`nested_visit_map `](Self::nested_visit_map ).
210
+ /// must also override [`nested_visit_cx `](Self::nested_visit_cx ).
211
211
///
212
212
/// **If for some reason you want the nested behavior, but don't
213
- /// have a `Map ` at your disposal:** then override the
213
+ /// have a `tcx ` at your disposal:** then override the
214
214
/// `visit_nested_XXX` methods. If a new `visit_nested_XXX` variant is
215
215
/// added in the future, it will cause a panic which can be detected
216
216
/// and fixed appropriately.
@@ -222,9 +222,9 @@ pub trait Visitor<'v>: Sized {
222
222
223
223
/// If `type NestedFilter` is set to visit nested items, this method
224
224
/// must also be overridden to provide a map to retrieve nested items.
225
- fn nested_visit_map ( & mut self ) -> Self :: Map {
225
+ fn nested_visit_cx ( & mut self ) -> Self :: Cx {
226
226
panic ! (
227
- "nested_visit_map must be implemented or consider using \
227
+ "nested_visit_cx must be implemented or consider using \
228
228
`type NestedFilter = nested_filter::None` (the default)"
229
229
) ;
230
230
}
@@ -236,10 +236,10 @@ pub trait Visitor<'v>: Sized {
236
236
/// "deep" visit patterns described at
237
237
/// [`rustc_hir::intravisit`]. The only reason to override
238
238
/// this method is if you want a nested pattern but cannot supply a
239
- /// [`Map`]; see `nested_visit_map ` for advice.
239
+ /// [`Map`]; see `nested_visit_cx ` for advice.
240
240
fn visit_nested_item ( & mut self , id : ItemId ) -> Self :: Result {
241
241
if Self :: NestedFilter :: INTER {
242
- let item = self . nested_visit_map ( ) . item ( id) ;
242
+ let item = self . nested_visit_cx ( ) . item ( id) ;
243
243
try_visit ! ( self . visit_item( item) ) ;
244
244
}
245
245
Self :: Result :: output ( )
@@ -250,7 +250,7 @@ pub trait Visitor<'v>: Sized {
250
250
/// method.
251
251
fn visit_nested_trait_item ( & mut self , id : TraitItemId ) -> Self :: Result {
252
252
if Self :: NestedFilter :: INTER {
253
- let item = self . nested_visit_map ( ) . trait_item ( id) ;
253
+ let item = self . nested_visit_cx ( ) . trait_item ( id) ;
254
254
try_visit ! ( self . visit_trait_item( item) ) ;
255
255
}
256
256
Self :: Result :: output ( )
@@ -261,7 +261,7 @@ pub trait Visitor<'v>: Sized {
261
261
/// method.
262
262
fn visit_nested_impl_item ( & mut self , id : ImplItemId ) -> Self :: Result {
263
263
if Self :: NestedFilter :: INTER {
264
- let item = self . nested_visit_map ( ) . impl_item ( id) ;
264
+ let item = self . nested_visit_cx ( ) . impl_item ( id) ;
265
265
try_visit ! ( self . visit_impl_item( item) ) ;
266
266
}
267
267
Self :: Result :: output ( )
@@ -272,7 +272,7 @@ pub trait Visitor<'v>: Sized {
272
272
/// method.
273
273
fn visit_nested_foreign_item ( & mut self , id : ForeignItemId ) -> Self :: Result {
274
274
if Self :: NestedFilter :: INTER {
275
- let item = self . nested_visit_map ( ) . foreign_item ( id) ;
275
+ let item = self . nested_visit_cx ( ) . foreign_item ( id) ;
276
276
try_visit ! ( self . visit_foreign_item( item) ) ;
277
277
}
278
278
Self :: Result :: output ( )
@@ -283,7 +283,7 @@ pub trait Visitor<'v>: Sized {
283
283
/// `Self::NestedFilter`.
284
284
fn visit_nested_body ( & mut self , id : BodyId ) -> Self :: Result {
285
285
if Self :: NestedFilter :: INTRA {
286
- let body = self . nested_visit_map ( ) . body ( id) ;
286
+ let body = self . nested_visit_cx ( ) . body ( id) ;
287
287
try_visit ! ( self . visit_body( body) ) ;
288
288
}
289
289
Self :: Result :: output ( )
0 commit comments