Skip to content

Commit 8351667

Browse files
committed
intravisit: abstract over HIR Map
1 parent 62e9ccb commit 8351667

File tree

41 files changed

+258
-89
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+258
-89
lines changed

src/librustc/hir/check_attr.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
//! item.
66
77
use crate::hir::intravisit::{self, NestedVisitorMap, Visitor};
8+
use crate::hir::map::Map;
89
use crate::lint::builtin::UNUSED_ATTRIBUTES;
910
use crate::ty::query::Providers;
1011
use crate::ty::TyCtxt;
@@ -519,7 +520,9 @@ impl CheckAttrVisitor<'tcx> {
519520
}
520521

521522
impl Visitor<'tcx> for CheckAttrVisitor<'tcx> {
522-
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
523+
type Map = Map<'tcx>;
524+
525+
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, Self::Map> {
523526
NestedVisitorMap::OnlyBodies(&self.tcx.hir())
524527
}
525528

src/librustc/hir/intravisit.rs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@
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-
3634
use rustc_hir::itemlikevisit::{ItemLikeVisitor, ParItemLikeVisitor};
3735
use rustc_hir::*;
3836
use 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.)
197200
pub 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
}

src/librustc/hir/map/collector.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::dep_graph::{DepGraph, DepKind, DepNode, DepNodeIndex};
22
use crate::hir::intravisit::{self, NestedVisitorMap, Visitor};
33
use crate::hir::map::definitions::{self, DefPathHash};
4-
use crate::hir::map::{Entry, HirEntryMap};
4+
use crate::hir::map::{Entry, HirEntryMap, Map};
55
use crate::ich::StableHashingContext;
66
use crate::middle::cstore::CrateStore;
77
use rustc_data_structures::fingerprint::Fingerprint;
@@ -336,11 +336,13 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
336336
}
337337

338338
impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
339+
type Map = Map<'hir>;
340+
339341
/// Because we want to track parent items and so forth, enable
340342
/// deep walking so that we walk nested items in the context of
341343
/// their outer items.
342344
343-
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'hir> {
345+
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, Self::Map> {
344346
panic!("`visit_nested_xxx` must be manually implemented in this visitor");
345347
}
346348

src/librustc/hir/map/hir_id_validator.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,9 @@ impl<'a, 'hir> HirIdValidator<'a, 'hir> {
133133
}
134134

135135
impl<'a, 'hir> intravisit::Visitor<'hir> for HirIdValidator<'a, 'hir> {
136-
fn nested_visit_map<'this>(&'this mut self) -> intravisit::NestedVisitorMap<'this, 'hir> {
136+
type Map = Map<'hir>;
137+
138+
fn nested_visit_map<'this>(&'this mut self) -> intravisit::NestedVisitorMap<'this, Self::Map> {
137139
intravisit::NestedVisitorMap::OnlyBodies(self.hir_map)
138140
}
139141

src/librustc/hir/map/mod.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,6 +1093,24 @@ impl<'hir> Map<'hir> {
10931093
}
10941094
}
10951095

1096+
impl<'hir> intravisit::Map<'hir> for Map<'hir> {
1097+
fn body(&self, id: BodyId) -> &'hir Body<'hir> {
1098+
self.body(id)
1099+
}
1100+
1101+
fn item(&self, id: HirId) -> &'hir Item<'hir> {
1102+
self.item(id)
1103+
}
1104+
1105+
fn trait_item(&self, id: TraitItemId) -> &'hir TraitItem<'hir> {
1106+
self.trait_item(id)
1107+
}
1108+
1109+
fn impl_item(&self, id: ImplItemId) -> &'hir ImplItem<'hir> {
1110+
self.impl_item(id)
1111+
}
1112+
}
1113+
10961114
pub struct NodesMatchingSuffix<'a> {
10971115
map: &'a Map<'a>,
10981116
item_name: &'a String,

src/librustc/hir/upvars.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Upvar (closure capture) collection from cross-body HIR uses of `Res::Local`s.
22
33
use crate::hir::intravisit::{self, NestedVisitorMap, Visitor};
4+
use crate::hir::map::Map;
45
use crate::ty::query::Providers;
56
use crate::ty::TyCtxt;
67
use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
@@ -43,7 +44,9 @@ struct LocalCollector {
4344
}
4445

4546
impl Visitor<'tcx> for LocalCollector {
46-
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
47+
type Map = Map<'tcx>;
48+
49+
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, Self::Map> {
4750
NestedVisitorMap::None
4851
}
4952

@@ -70,7 +73,9 @@ impl CaptureCollector<'_, '_> {
7073
}
7174

7275
impl Visitor<'tcx> for CaptureCollector<'a, 'tcx> {
73-
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
76+
type Map = Map<'tcx>;
77+
78+
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, Self::Map> {
7479
NestedVisitorMap::None
7580
}
7681

src/librustc/infer/error_reporting/need_type_info.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ impl<'a, 'tcx> FindLocalByTypeVisitor<'a, 'tcx> {
6666
}
6767

6868
impl<'a, 'tcx> Visitor<'tcx> for FindLocalByTypeVisitor<'a, 'tcx> {
69-
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
69+
type Map = Map<'tcx>;
70+
71+
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, Self::Map> {
7072
NestedVisitorMap::OnlyBodies(&self.hir_map)
7173
}
7274

src/librustc/infer/error_reporting/nice_region_error/find_anon_type.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::hir::intravisit::{self, NestedVisitorMap, Visitor};
2+
use crate::hir::map::Map;
23
use crate::infer::error_reporting::nice_region_error::NiceRegionError;
34
use crate::middle::resolve_lifetime as rl;
45
use crate::ty::{self, Region, TyCtxt};
@@ -90,7 +91,9 @@ struct FindNestedTypeVisitor<'tcx> {
9091
}
9192

9293
impl Visitor<'tcx> for FindNestedTypeVisitor<'tcx> {
93-
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
94+
type Map = Map<'tcx>;
95+
96+
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, Self::Map> {
9497
NestedVisitorMap::OnlyBodies(&self.tcx.hir())
9598
}
9699

@@ -207,7 +210,9 @@ struct TyPathVisitor<'tcx> {
207210
}
208211

209212
impl Visitor<'tcx> for TyPathVisitor<'tcx> {
210-
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
213+
type Map = Map<'tcx>;
214+
215+
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, Map<'tcx>> {
211216
NestedVisitorMap::OnlyBodies(&self.tcx.hir())
212217
}
213218

src/librustc/middle/weak_lang_items.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::session::config;
55

66
use crate::hir::intravisit;
77
use crate::hir::intravisit::{NestedVisitorMap, Visitor};
8+
use crate::hir::map::Map;
89
use crate::ty::TyCtxt;
910
use errors::struct_span_err;
1011
use rustc_data_structures::fx::FxHashSet;
@@ -136,7 +137,9 @@ impl<'a, 'tcx> Context<'a, 'tcx> {
136137
}
137138

138139
impl<'a, 'tcx, 'v> Visitor<'v> for Context<'a, 'tcx> {
139-
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'v> {
140+
type Map = Map<'v>;
141+
142+
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, Map<'v>> {
140143
NestedVisitorMap::None
141144
}
142145

src/librustc_ast_lowering/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737
use rustc::arena::Arena;
3838
use rustc::dep_graph::DepGraph;
3939
use rustc::hir::intravisit;
40-
use rustc::hir::map::{DefKey, DefPathData, Definitions};
40+
use rustc::hir::map::definitions::{DefKey, DefPathData, Definitions};
41+
use rustc::hir::map::Map;
4142
use rustc::lint;
4243
use rustc::lint::builtin::{self, ELIDED_LIFETIMES_IN_PATHS};
4344
use rustc::middle::cstore::CrateStore;
@@ -1484,7 +1485,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
14841485
}
14851486

14861487
impl<'r, 'a, 'v, 'hir> intravisit::Visitor<'v> for ImplTraitLifetimeCollector<'r, 'a, 'hir> {
1487-
fn nested_visit_map<'this>(&'this mut self) -> intravisit::NestedVisitorMap<'this, 'v> {
1488+
type Map = Map<'v>;
1489+
1490+
fn nested_visit_map(&mut self) -> intravisit::NestedVisitorMap<'_, Self::Map> {
14881491
intravisit::NestedVisitorMap::None
14891492
}
14901493

src/librustc_incremental/assert_dep_graph.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ use graphviz as dot;
3737
use rustc::dep_graph::debug::{DepNodeFilter, EdgeFilter};
3838
use rustc::dep_graph::{DepGraphQuery, DepKind, DepNode};
3939
use rustc::hir::intravisit::{self, NestedVisitorMap, Visitor};
40+
use rustc::hir::map::Map;
4041
use rustc::ty::TyCtxt;
4142
use rustc_data_structures::fx::FxHashSet;
4243
use rustc_data_structures::graph::implementation::{Direction, NodeIndex, INCOMING, OUTGOING};
@@ -159,7 +160,9 @@ impl IfThisChanged<'tcx> {
159160
}
160161

161162
impl Visitor<'tcx> for IfThisChanged<'tcx> {
162-
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
163+
type Map = Map<'tcx>;
164+
165+
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, Self::Map> {
163166
NestedVisitorMap::OnlyBodies(&self.tcx.hir())
164167
}
165168

src/librustc_incremental/persist/dirty_clean.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
1616
use rustc::dep_graph::{label_strs, DepNode};
1717
use rustc::hir::intravisit;
18+
use rustc::hir::map::Map;
1819
use rustc::ty::TyCtxt;
1920
use rustc_data_structures::fingerprint::Fingerprint;
2021
use rustc_data_structures::fx::FxHashSet;
@@ -547,7 +548,9 @@ impl FindAllAttrs<'tcx> {
547548
}
548549

549550
impl intravisit::Visitor<'tcx> for FindAllAttrs<'tcx> {
550-
fn nested_visit_map<'this>(&'this mut self) -> intravisit::NestedVisitorMap<'this, 'tcx> {
551+
type Map = Map<'tcx>;
552+
553+
fn nested_visit_map<'this>(&'this mut self) -> intravisit::NestedVisitorMap<'this, Self::Map> {
551554
intravisit::NestedVisitorMap::All(&self.tcx.hir())
552555
}
553556

src/librustc_lint/builtin.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use std::fmt::Write;
2525

2626
use lint::{EarlyContext, EarlyLintPass, LateLintPass, LintPass};
2727
use lint::{LateContext, LintArray, LintContext};
28+
use rustc::hir::map::Map;
2829
use rustc::lint;
2930
use rustc::lint::FutureIncompatibleInfo;
3031
use rustc::traits::misc::can_type_implement_copy;
@@ -1093,7 +1094,9 @@ impl TypeAliasBounds {
10931094
err: &'a mut DiagnosticBuilder<'db>,
10941095
}
10951096
impl<'a, 'db, 'v> Visitor<'v> for WalkAssocTypes<'a, 'db> {
1096-
fn nested_visit_map<'this>(&'this mut self) -> intravisit::NestedVisitorMap<'this, 'v> {
1097+
type Map = Map<'v>;
1098+
1099+
fn nested_visit_map(&mut self) -> intravisit::NestedVisitorMap<'_, Self::Map> {
10971100
intravisit::NestedVisitorMap::None
10981101
}
10991102

src/librustc_lint/late.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
1717
use rustc::hir::intravisit as hir_visit;
1818
use rustc::hir::intravisit::Visitor;
19+
use rustc::hir::map::Map;
1920
use rustc::lint::LateContext;
2021
use rustc::lint::LintPass;
2122
use rustc::lint::{LateLintPass, LateLintPassObject};
@@ -86,10 +87,12 @@ impl<'a, 'tcx, T: LateLintPass<'a, 'tcx>> LateContextAndPass<'a, 'tcx, T> {
8687
impl<'a, 'tcx, T: LateLintPass<'a, 'tcx>> hir_visit::Visitor<'tcx>
8788
for LateContextAndPass<'a, 'tcx, T>
8889
{
90+
type Map = Map<'tcx>;
91+
8992
/// Because lints are scoped lexically, we want to walk nested
9093
/// items in the context of the outer item, so enable
9194
/// deep-walking.
92-
fn nested_visit_map<'this>(&'this mut self) -> hir_visit::NestedVisitorMap<'this, 'tcx> {
95+
fn nested_visit_map<'this>(&'this mut self) -> hir_visit::NestedVisitorMap<'this, Self::Map> {
9396
hir_visit::NestedVisitorMap::All(&self.context.tcx.hir())
9497
}
9598

0 commit comments

Comments
 (0)