Skip to content

Commit d69210d

Browse files
committed
Overhaul the intravisit::Map trait.
First of all, note that `Map` has three different relevant meanings. - The `intravisit::Map` trait. - The `map::Map` struct. - The `NestedFilter::Map` associated type. The `intravisit::Map` trait is impl'd twice. - For `!`, where the methods are all unreachable. - For `map::Map`, which gets things from the `TyCtxt`. As part of getting rid of `map::Map`, this commit changes `impl intravisit::Map for map::Map` to `impl intravisit::Map for TyCtxt`. It's fairly straightforward except various things are renamed, because the existing names would no longer have made sense. - `trait intravisit::Map` becomes `trait intravisit::HirCtxt`, so named because it's for getting some HIR stuff. - `intravisit::Map::hir_node` becomes `intravisit::HirCtxt::node`, to avoid a name conflict with the existing `TyCtxt::hir_node`. - `NestedFilter::Map` assoc type becomes `NestedFilter::Cx`. It has a `HirCtxt` bound. - `Visitor::nested_visit_map` becomes `Visitor::nested_visit_cx`. I deliberately made the new trait and associated type names different to avoid the old `type Map: Map` situation, which I found confusing. We now have `type Cx: HirCtxt`.
1 parent 06ae3b7 commit d69210d

Some content is hidden

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

58 files changed

+193
-199
lines changed

Diff for: compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+12-14
Original file line numberDiff line numberDiff line change
@@ -354,13 +354,13 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
354354
expr: Option<&'hir hir::Expr<'hir>>,
355355
pat: Option<&'hir hir::Pat<'hir>>,
356356
parent_pat: Option<&'hir hir::Pat<'hir>>,
357-
hir: rustc_middle::hir::map::Map<'hir>,
357+
tcx: TyCtxt<'hir>,
358358
}
359359
impl<'hir> Visitor<'hir> for ExpressionFinder<'hir> {
360360
type NestedFilter = OnlyBodies;
361361

362-
fn nested_visit_map(&mut self) -> Self::Map {
363-
self.hir
362+
fn nested_visit_cx(&mut self) -> Self::Cx {
363+
self.tcx
364364
}
365365

366366
fn visit_expr(&mut self, e: &'hir hir::Expr<'hir>) {
@@ -402,7 +402,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
402402
expr: None,
403403
pat: None,
404404
parent_pat: None,
405-
hir,
405+
tcx: self.infcx.tcx,
406406
};
407407
finder.visit_expr(expr);
408408
if let Some(span) = span
@@ -1895,7 +1895,6 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
18951895

18961896
fn suggest_copy_for_type_in_cloned_ref(&self, err: &mut Diag<'infcx>, place: Place<'tcx>) {
18971897
let tcx = self.infcx.tcx;
1898-
let hir = tcx.hir();
18991898
let Some(body_id) = tcx.hir_node(self.mir_hir_id()).body_id() else { return };
19001899

19011900
struct FindUselessClone<'tcx> {
@@ -1923,7 +1922,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
19231922

19241923
let mut expr_finder = FindUselessClone::new(tcx, self.mir_def_id());
19251924

1926-
let body = hir.body(body_id).value;
1925+
let body = tcx.hir_body(body_id).value;
19271926
expr_finder.visit_expr(body);
19281927

19291928
struct Holds<'tcx> {
@@ -2264,7 +2263,6 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
22642263
) {
22652264
let issue_span = issued_spans.args_or_use();
22662265
let tcx = self.infcx.tcx;
2267-
let hir = tcx.hir();
22682266

22692267
let Some(body_id) = tcx.hir_node(self.mir_hir_id()).body_id() else { return };
22702268
let typeck_results = tcx.typeck(self.mir_def_id());
@@ -2352,7 +2350,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
23522350
pat_span: None,
23532351
head: None,
23542352
};
2355-
finder.visit_expr(hir.body(body_id).value);
2353+
finder.visit_expr(tcx.hir_body(body_id).value);
23562354

23572355
if let Some(body_expr) = finder.body_expr
23582356
&& let Some(loop_span) = finder.loop_span
@@ -2460,10 +2458,10 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
24602458
// Get the body the error happens in
24612459
let Some(body_id) = tcx.hir_node(self.mir_hir_id()).body_id() else { return };
24622460

2463-
let body_expr = hir.body(body_id).value;
2461+
let body_expr = tcx.hir_body(body_id).value;
24642462

24652463
struct ClosureFinder<'hir> {
2466-
hir: rustc_middle::hir::map::Map<'hir>,
2464+
tcx: TyCtxt<'hir>,
24672465
borrow_span: Span,
24682466
res: Option<(&'hir hir::Expr<'hir>, &'hir hir::Closure<'hir>)>,
24692467
/// The path expression with the `borrow_span` span
@@ -2472,8 +2470,8 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
24722470
impl<'hir> Visitor<'hir> for ClosureFinder<'hir> {
24732471
type NestedFilter = OnlyBodies;
24742472

2475-
fn nested_visit_map(&mut self) -> Self::Map {
2476-
self.hir
2473+
fn nested_visit_cx(&mut self) -> Self::Cx {
2474+
self.tcx
24772475
}
24782476

24792477
fn visit_expr(&mut self, ex: &'hir hir::Expr<'hir>) {
@@ -2499,7 +2497,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
24992497

25002498
// Find the closure that most tightly wraps `capture_kind_span`
25012499
let mut finder =
2502-
ClosureFinder { hir, borrow_span: capture_kind_span, res: None, error_path: None };
2500+
ClosureFinder { tcx, borrow_span: capture_kind_span, res: None, error_path: None };
25032501
finder.visit_expr(body_expr);
25042502
let Some((closure_expr, closure)) = finder.res else { return };
25052503

@@ -2564,7 +2562,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
25642562
}
25652563

25662564
let mut finder = VariableUseFinder { local_id, spans: Vec::new() };
2567-
finder.visit_expr(hir.body(closure.body).value);
2565+
finder.visit_expr(tcx.hir_body(closure.body).value);
25682566

25692567
spans = finder.spans;
25702568
} else {

Diff for: compiler/rustc_borrowck/src/diagnostics/move_errors.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_hir::intravisit::Visitor;
77
use rustc_hir::{self as hir, CaptureBy, ExprKind, HirId, Node};
88
use rustc_middle::bug;
99
use rustc_middle::mir::*;
10-
use rustc_middle::ty::{self, Ty};
10+
use rustc_middle::ty::{self, Ty, TyCtxt};
1111
use rustc_mir_dataflow::move_paths::{LookupResult, MovePathIndex};
1212
use rustc_span::{BytePos, ExpnKind, MacroKind, Span};
1313
use rustc_trait_selection::error_reporting::traits::FindExprBySpan;
@@ -702,7 +702,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
702702
/// make it bind by reference instead (if possible)
703703
struct BindingFinder<'tcx> {
704704
typeck_results: &'tcx ty::TypeckResults<'tcx>,
705-
hir: rustc_middle::hir::map::Map<'tcx>,
705+
tcx: TyCtxt<'tcx>,
706706
/// Input: the span of the pattern we're finding bindings in
707707
pat_span: Span,
708708
/// Input: the spans of the bindings we're providing suggestions for
@@ -721,8 +721,8 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
721721
impl<'tcx> Visitor<'tcx> for BindingFinder<'tcx> {
722722
type NestedFilter = rustc_middle::hir::nested_filter::OnlyBodies;
723723

724-
fn nested_visit_map(&mut self) -> Self::Map {
725-
self.hir
724+
fn nested_visit_cx(&mut self) -> Self::Cx {
725+
self.tcx
726726
}
727727

728728
fn visit_expr(&mut self, ex: &'tcx hir::Expr<'tcx>) -> Self::Result {
@@ -794,7 +794,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
794794
let typeck_results = self.infcx.tcx.typeck(self.mir_def_id());
795795
let mut finder = BindingFinder {
796796
typeck_results,
797-
hir,
797+
tcx: self.infcx.tcx,
798798
pat_span,
799799
binding_spans,
800800
found_pat: false,

Diff for: compiler/rustc_hir/src/intravisit.rs

+24-24
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
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 `nested_visit_cx`), 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).
@@ -30,7 +30,7 @@
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 `nested_visit_cx`). 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,20 +106,20 @@ 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+
/// An abstract representation of HIR things retrievable from `TyCtxt`.
110+
pub trait HirCtxt<'hir> {
111111
/// 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>;
113113
fn body(&self, id: BodyId) -> &'hir Body<'hir>;
114114
fn item(&self, id: ItemId) -> &'hir Item<'hir>;
115115
fn trait_item(&self, id: TraitItemId) -> &'hir TraitItem<'hir>;
116116
fn impl_item(&self, id: ImplItemId) -> &'hir ImplItem<'hir>;
117117
fn foreign_item(&self, id: ForeignItemId) -> &'hir ForeignItem<'hir>;
118118
}
119119

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> {
123123
unreachable!();
124124
}
125125
fn body(&self, _: BodyId) -> &'hir Body<'hir> {
@@ -140,7 +140,7 @@ impl<'hir> Map<'hir> for ! {
140140
}
141141

142142
pub mod nested_filter {
143-
use super::Map;
143+
use super::HirCtxt;
144144

145145
/// Specifies what nested things a visitor wants to visit. By "nested
146146
/// things", we are referring to bits of HIR that are not directly embedded
@@ -155,7 +155,7 @@ pub mod nested_filter {
155155
/// See the comments at [`rustc_hir::intravisit`] for more details on the overall
156156
/// visit strategy.
157157
pub trait NestedFilter<'hir> {
158-
type Map: Map<'hir>;
158+
type Cx: HirCtxt<'hir>;
159159

160160
/// Whether the visitor visits nested "item-like" things.
161161
/// E.g., item, impl-item.
@@ -171,10 +171,10 @@ pub mod nested_filter {
171171
///
172172
/// Use this if you are only walking some particular kind of tree
173173
/// (i.e., a type, or fn signature) and you don't want to thread a
174-
/// HIR map around.
174+
/// `tcx` around.
175175
pub struct None(());
176176
impl NestedFilter<'_> for None {
177-
type Map = !;
177+
type Cx = !;
178178
const INTER: bool = false;
179179
const INTRA: bool = false;
180180
}
@@ -199,18 +199,18 @@ use nested_filter::NestedFilter;
199199
/// to monitor future changes to `Visitor` in case a new method with a
200200
/// new default implementation gets introduced.)
201201
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;
204204

205205
///////////////////////////////////////////////////////////////////////////
206206
// Nested items.
207207

208208
/// Override this type to control which nested HIR are visited; see
209209
/// [`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).
211211
///
212212
/// **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
214214
/// `visit_nested_XXX` methods. If a new `visit_nested_XXX` variant is
215215
/// added in the future, it will cause a panic which can be detected
216216
/// and fixed appropriately.
@@ -222,9 +222,9 @@ pub trait Visitor<'v>: Sized {
222222

223223
/// If `type NestedFilter` is set to visit nested items, this method
224224
/// 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 {
226226
panic!(
227-
"nested_visit_map must be implemented or consider using \
227+
"nested_visit_cx must be implemented or consider using \
228228
`type NestedFilter = nested_filter::None` (the default)"
229229
);
230230
}
@@ -236,10 +236,10 @@ pub trait Visitor<'v>: Sized {
236236
/// "deep" visit patterns described at
237237
/// [`rustc_hir::intravisit`]. The only reason to override
238238
/// 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.
240240
fn visit_nested_item(&mut self, id: ItemId) -> Self::Result {
241241
if Self::NestedFilter::INTER {
242-
let item = self.nested_visit_map().item(id);
242+
let item = self.nested_visit_cx().item(id);
243243
try_visit!(self.visit_item(item));
244244
}
245245
Self::Result::output()
@@ -250,7 +250,7 @@ pub trait Visitor<'v>: Sized {
250250
/// method.
251251
fn visit_nested_trait_item(&mut self, id: TraitItemId) -> Self::Result {
252252
if Self::NestedFilter::INTER {
253-
let item = self.nested_visit_map().trait_item(id);
253+
let item = self.nested_visit_cx().trait_item(id);
254254
try_visit!(self.visit_trait_item(item));
255255
}
256256
Self::Result::output()
@@ -261,7 +261,7 @@ pub trait Visitor<'v>: Sized {
261261
/// method.
262262
fn visit_nested_impl_item(&mut self, id: ImplItemId) -> Self::Result {
263263
if Self::NestedFilter::INTER {
264-
let item = self.nested_visit_map().impl_item(id);
264+
let item = self.nested_visit_cx().impl_item(id);
265265
try_visit!(self.visit_impl_item(item));
266266
}
267267
Self::Result::output()
@@ -272,7 +272,7 @@ pub trait Visitor<'v>: Sized {
272272
/// method.
273273
fn visit_nested_foreign_item(&mut self, id: ForeignItemId) -> Self::Result {
274274
if Self::NestedFilter::INTER {
275-
let item = self.nested_visit_map().foreign_item(id);
275+
let item = self.nested_visit_cx().foreign_item(id);
276276
try_visit!(self.visit_foreign_item(item));
277277
}
278278
Self::Result::output()
@@ -283,7 +283,7 @@ pub trait Visitor<'v>: Sized {
283283
/// `Self::NestedFilter`.
284284
fn visit_nested_body(&mut self, id: BodyId) -> Self::Result {
285285
if Self::NestedFilter::INTRA {
286-
let body = self.nested_visit_map().body(id);
286+
let body = self.nested_visit_cx().body(id);
287287
try_visit!(self.visit_body(body));
288288
}
289289
Self::Result::output()

Diff for: compiler/rustc_hir_analysis/src/check/check.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -468,8 +468,8 @@ fn best_definition_site_of_opaque<'tcx>(
468468
impl<'tcx> intravisit::Visitor<'tcx> for TaitConstraintLocator<'tcx> {
469469
type NestedFilter = nested_filter::All;
470470
type Result = ControlFlow<(Span, LocalDefId)>;
471-
fn nested_visit_map(&mut self) -> Self::Map {
472-
self.tcx.hir()
471+
fn nested_visit_cx(&mut self) -> Self::Cx {
472+
self.tcx
473473
}
474474
fn visit_expr(&mut self, ex: &'tcx hir::Expr<'tcx>) -> Self::Result {
475475
if let hir::ExprKind::Closure(closure) = ex.kind {

Diff for: compiler/rustc_hir_analysis/src/collect.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,8 @@ fn reject_placeholder_type_signatures_in_item<'tcx>(
277277
impl<'tcx> Visitor<'tcx> for CollectItemTypesVisitor<'tcx> {
278278
type NestedFilter = nested_filter::OnlyBodies;
279279

280-
fn nested_visit_map(&mut self) -> Self::Map {
281-
self.tcx.hir()
280+
fn nested_visit_cx(&mut self) -> Self::Cx {
281+
self.tcx
282282
}
283283

284284
fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) {

Diff for: compiler/rustc_hir_analysis/src/collect/dump.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ pub(crate) fn def_parents(tcx: TyCtxt<'_>) {
5454
impl<'tcx> intravisit::Visitor<'tcx> for AnonConstFinder<'tcx> {
5555
type NestedFilter = nested_filter::All;
5656

57-
fn nested_visit_map(&mut self) -> Self::Map {
58-
self.tcx.hir()
57+
fn nested_visit_cx(&mut self) -> Self::Cx {
58+
self.tcx
5959
}
6060

6161
fn visit_anon_const(&mut self, c: &'tcx rustc_hir::AnonConst) {

Diff for: compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -422,8 +422,8 @@ enum NonLifetimeBinderAllowed {
422422
impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
423423
type NestedFilter = nested_filter::OnlyBodies;
424424

425-
fn nested_visit_map(&mut self) -> Self::Map {
426-
self.tcx.hir()
425+
fn nested_visit_cx(&mut self) -> Self::Cx {
426+
self.tcx
427427
}
428428

429429
fn visit_nested_body(&mut self, body: hir::BodyId) {

Diff for: compiler/rustc_hir_analysis/src/collect/type_of/opaque.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -298,8 +298,8 @@ impl TaitConstraintLocator<'_> {
298298
impl<'tcx> intravisit::Visitor<'tcx> for TaitConstraintLocator<'tcx> {
299299
type NestedFilter = nested_filter::All;
300300

301-
fn nested_visit_map(&mut self) -> Self::Map {
302-
self.tcx.hir()
301+
fn nested_visit_cx(&mut self) -> Self::Cx {
302+
self.tcx
303303
}
304304
fn visit_expr(&mut self, ex: &'tcx Expr<'tcx>) {
305305
if let hir::ExprKind::Closure(closure) = ex.kind {
@@ -441,8 +441,8 @@ impl RpitConstraintChecker<'_> {
441441
impl<'tcx> intravisit::Visitor<'tcx> for RpitConstraintChecker<'tcx> {
442442
type NestedFilter = nested_filter::OnlyBodies;
443443

444-
fn nested_visit_map(&mut self) -> Self::Map {
445-
self.tcx.hir()
444+
fn nested_visit_cx(&mut self) -> Self::Cx {
445+
self.tcx
446446
}
447447
fn visit_expr(&mut self, ex: &'tcx Expr<'tcx>) {
448448
if let hir::ExprKind::Closure(closure) = ex.kind {

Diff for: compiler/rustc_hir_pretty/src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ use rustc_span::source_map::SourceMap;
2424
use rustc_span::{FileName, Ident, Span, Symbol, kw};
2525
use {rustc_ast as ast, rustc_hir as hir};
2626

27-
pub fn id_to_string(map: &dyn rustc_hir::intravisit::Map<'_>, hir_id: HirId) -> String {
28-
to_string(&map, |s| s.print_node(map.hir_node(hir_id)))
27+
pub fn id_to_string(cx: &dyn rustc_hir::intravisit::HirCtxt<'_>, hir_id: HirId) -> String {
28+
to_string(&cx, |s| s.print_node(cx.node(hir_id)))
2929
}
3030

3131
pub enum AnnNode<'a> {
@@ -53,7 +53,7 @@ pub trait PpAnn {
5353
fn post(&self, _state: &mut State<'_>, _node: AnnNode<'_>) {}
5454
}
5555

56-
impl PpAnn for &dyn rustc_hir::intravisit::Map<'_> {
56+
impl PpAnn for &dyn rustc_hir::intravisit::HirCtxt<'_> {
5757
fn nested(&self, state: &mut State<'_>, nested: Nested) {
5858
match nested {
5959
Nested::Item(id) => state.print_item(self.item(id)),

Diff for: compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2727,8 +2727,8 @@ struct FindClosureArg<'tcx> {
27272727
impl<'tcx> Visitor<'tcx> for FindClosureArg<'tcx> {
27282728
type NestedFilter = rustc_middle::hir::nested_filter::All;
27292729

2730-
fn nested_visit_map(&mut self) -> Self::Map {
2731-
self.tcx.hir()
2730+
fn nested_visit_cx(&mut self) -> Self::Cx {
2731+
self.tcx
27322732
}
27332733

27342734
fn visit_expr(&mut self, ex: &'tcx hir::Expr<'tcx>) {

0 commit comments

Comments
 (0)