1818//! within one another.
1919//! - Example: Examine each expression to look for its type and do some check or other.
2020//! - 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
2222//! `tcx.hir().visit_all_item_likes_in_crate(&mut visitor)`. Within your
2323//! `intravisit::Visitor` impl, implement methods like `visit_expr()` (don't forget to invoke
2424//! `intravisit::walk_expr()` to keep walking the subparts).
3030//! - Example: Lifetime resolution, which wants to bring lifetimes declared on the
3131//! impl into scope while visiting the impl-items, and then back out again.
3232//! - 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
3434//! `tcx.hir().walk_toplevel_module(visitor)` invoked on `tcx.hir().krate()`.
3535//! - Pro: Visitor methods for any kind of HIR node, not just item-like things.
3636//! - Pro: Preserves nesting information
@@ -106,41 +106,43 @@ impl<'a> FnKind<'a> {
106106 }
107107}
108108
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 > {
111113 /// Retrieves the `Node` corresponding to `id`.
112114 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 > ;
118120}
119121
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 ! {
122124 fn hir_node ( & self , _: HirId ) -> Node < ' hir > {
123125 unreachable ! ( ) ;
124126 }
125- fn body ( & self , _: BodyId ) -> & ' hir Body < ' hir > {
127+ fn hir_body ( & self , _: BodyId ) -> & ' hir Body < ' hir > {
126128 unreachable ! ( ) ;
127129 }
128- fn item ( & self , _: ItemId ) -> & ' hir Item < ' hir > {
130+ fn hir_item ( & self , _: ItemId ) -> & ' hir Item < ' hir > {
129131 unreachable ! ( ) ;
130132 }
131- fn trait_item ( & self , _: TraitItemId ) -> & ' hir TraitItem < ' hir > {
133+ fn hir_trait_item ( & self , _: TraitItemId ) -> & ' hir TraitItem < ' hir > {
132134 unreachable ! ( ) ;
133135 }
134- fn impl_item ( & self , _: ImplItemId ) -> & ' hir ImplItem < ' hir > {
136+ fn hir_impl_item ( & self , _: ImplItemId ) -> & ' hir ImplItem < ' hir > {
135137 unreachable ! ( ) ;
136138 }
137- fn foreign_item ( & self , _: ForeignItemId ) -> & ' hir ForeignItem < ' hir > {
139+ fn hir_foreign_item ( & self , _: ForeignItemId ) -> & ' hir ForeignItem < ' hir > {
138140 unreachable ! ( ) ;
139141 }
140142}
141143
142144pub mod nested_filter {
143- use super :: Map ;
145+ use super :: HirTyCtxt ;
144146
145147 /// Specifies what nested things a visitor wants to visit. By "nested
146148 /// things", we are referring to bits of HIR that are not directly embedded
@@ -155,7 +157,7 @@ pub mod nested_filter {
155157 /// See the comments at [`rustc_hir::intravisit`] for more details on the overall
156158 /// visit strategy.
157159 pub trait NestedFilter < ' hir > {
158- type Map : Map < ' hir > ;
160+ type MaybeTyCtxt : HirTyCtxt < ' hir > ;
159161
160162 /// Whether the visitor visits nested "item-like" things.
161163 /// E.g., item, impl-item.
@@ -171,10 +173,10 @@ pub mod nested_filter {
171173 ///
172174 /// Use this if you are only walking some particular kind of tree
173175 /// (i.e., a type, or fn signature) and you don't want to thread a
174- /// HIR map around.
176+ /// `tcx` around.
175177 pub struct None ( ( ) ) ;
176178 impl NestedFilter < ' _ > for None {
177- type Map = !;
179+ type MaybeTyCtxt = !;
178180 const INTER : bool = false ;
179181 const INTRA : bool = false ;
180182 }
@@ -199,18 +201,18 @@ use nested_filter::NestedFilter;
199201/// to monitor future changes to `Visitor` in case a new method with a
200202/// new default implementation gets introduced.)
201203pub 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 ;
204206
205207 ///////////////////////////////////////////////////////////////////////////
206208 // Nested items.
207209
208210 /// Override this type to control which nested HIR are visited; see
209211 /// [`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 ).
211213 ///
212214 /// **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
214216 /// `visit_nested_XXX` methods. If a new `visit_nested_XXX` variant is
215217 /// added in the future, it will cause a panic which can be detected
216218 /// and fixed appropriately.
@@ -222,9 +224,9 @@ pub trait Visitor<'v>: Sized {
222224
223225 /// If `type NestedFilter` is set to visit nested items, this method
224226 /// 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 {
226228 panic ! (
227- "nested_visit_map must be implemented or consider using \
229+ "maybe_tcx must be implemented or consider using \
228230 `type NestedFilter = nested_filter::None` (the default)"
229231 ) ;
230232 }
@@ -236,10 +238,10 @@ pub trait Visitor<'v>: Sized {
236238 /// "deep" visit patterns described at
237239 /// [`rustc_hir::intravisit`]. The only reason to override
238240 /// 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.
240242 fn visit_nested_item ( & mut self , id : ItemId ) -> Self :: Result {
241243 if Self :: NestedFilter :: INTER {
242- let item = self . nested_visit_map ( ) . item ( id) ;
244+ let item = self . maybe_tcx ( ) . hir_item ( id) ;
243245 try_visit ! ( self . visit_item( item) ) ;
244246 }
245247 Self :: Result :: output ( )
@@ -250,7 +252,7 @@ pub trait Visitor<'v>: Sized {
250252 /// method.
251253 fn visit_nested_trait_item ( & mut self , id : TraitItemId ) -> Self :: Result {
252254 if Self :: NestedFilter :: INTER {
253- let item = self . nested_visit_map ( ) . trait_item ( id) ;
255+ let item = self . maybe_tcx ( ) . hir_trait_item ( id) ;
254256 try_visit ! ( self . visit_trait_item( item) ) ;
255257 }
256258 Self :: Result :: output ( )
@@ -261,7 +263,7 @@ pub trait Visitor<'v>: Sized {
261263 /// method.
262264 fn visit_nested_impl_item ( & mut self , id : ImplItemId ) -> Self :: Result {
263265 if Self :: NestedFilter :: INTER {
264- let item = self . nested_visit_map ( ) . impl_item ( id) ;
266+ let item = self . maybe_tcx ( ) . hir_impl_item ( id) ;
265267 try_visit ! ( self . visit_impl_item( item) ) ;
266268 }
267269 Self :: Result :: output ( )
@@ -272,7 +274,7 @@ pub trait Visitor<'v>: Sized {
272274 /// method.
273275 fn visit_nested_foreign_item ( & mut self , id : ForeignItemId ) -> Self :: Result {
274276 if Self :: NestedFilter :: INTER {
275- let item = self . nested_visit_map ( ) . foreign_item ( id) ;
277+ let item = self . maybe_tcx ( ) . hir_foreign_item ( id) ;
276278 try_visit ! ( self . visit_foreign_item( item) ) ;
277279 }
278280 Self :: Result :: output ( )
@@ -283,7 +285,7 @@ pub trait Visitor<'v>: Sized {
283285 /// `Self::NestedFilter`.
284286 fn visit_nested_body ( & mut self , id : BodyId ) -> Self :: Result {
285287 if Self :: NestedFilter :: INTRA {
286- let body = self . nested_visit_map ( ) . body ( id) ;
288+ let body = self . maybe_tcx ( ) . hir_body ( id) ;
287289 try_visit ! ( self . visit_body( body) ) ;
288290 }
289291 Self :: Result :: output ( )
0 commit comments