diff options
| author | Nicholas Nethercote <n.nethercote@gmail.com> | 2025-02-03 14:42:01 +1100 |
|---|---|---|
| committer | Nicholas Nethercote <n.nethercote@gmail.com> | 2025-02-17 13:21:35 +1100 |
| commit | 661f99ba03738f1ee87888342aef920f3c0f7f48 (patch) | |
| tree | fbb141a208ea065626a599d2215a1e8b15c796a0 /compiler | |
| parent | f86f7ad5f2d3eb6631e5fab57630a4fee9a50d01 (diff) | |
| download | rust-661f99ba03738f1ee87888342aef920f3c0f7f48.tar.gz rust-661f99ba03738f1ee87888342aef920f3c0f7f48.zip | |
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 HIR stuff 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::HirTyCtxt`, so named because it gets some HIR stuff from a `TyCtxt`. - `NestedFilter::Map` assoc type becomes `NestedFilter::MaybeTyCtxt`, because it's always `!` or `TyCtxt`. - `Visitor::nested_visit_map` becomes `Visitor::maybe_tcx`. 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 MaybeTyCtxt: HirTyCtxt`.
Diffstat (limited to 'compiler')
29 files changed, 135 insertions, 134 deletions
diff --git a/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs b/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs index 4428d2246a1..59c76cb7f2b 100644 --- a/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs @@ -348,13 +348,13 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> { expr: Option<&'hir hir::Expr<'hir>>, pat: Option<&'hir hir::Pat<'hir>>, parent_pat: Option<&'hir hir::Pat<'hir>>, - hir: rustc_middle::hir::map::Map<'hir>, + tcx: TyCtxt<'hir>, } impl<'hir> Visitor<'hir> for ExpressionFinder<'hir> { type NestedFilter = OnlyBodies; - fn nested_visit_map(&mut self) -> Self::Map { - self.hir + fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt { + self.tcx } fn visit_expr(&mut self, e: &'hir hir::Expr<'hir>) { @@ -396,7 +396,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> { expr: None, pat: None, parent_pat: None, - hir, + tcx: self.infcx.tcx, }; finder.visit_expr(expr); if let Some(span) = span @@ -2455,7 +2455,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> { let body_expr = tcx.hir_body(body_id).value; struct ClosureFinder<'hir> { - hir: rustc_middle::hir::map::Map<'hir>, + tcx: TyCtxt<'hir>, borrow_span: Span, res: Option<(&'hir hir::Expr<'hir>, &'hir hir::Closure<'hir>)>, /// The path expression with the `borrow_span` span @@ -2464,8 +2464,8 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> { impl<'hir> Visitor<'hir> for ClosureFinder<'hir> { type NestedFilter = OnlyBodies; - fn nested_visit_map(&mut self) -> Self::Map { - self.hir + fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt { + self.tcx } fn visit_expr(&mut self, ex: &'hir hir::Expr<'hir>) { @@ -2491,7 +2491,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> { // Find the closure that most tightly wraps `capture_kind_span` let mut finder = - ClosureFinder { hir, borrow_span: capture_kind_span, res: None, error_path: None }; + ClosureFinder { tcx, borrow_span: capture_kind_span, res: None, error_path: None }; finder.visit_expr(body_expr); let Some((closure_expr, closure)) = finder.res else { return }; diff --git a/compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs b/compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs index 976d14bb245..a88b27f2476 100644 --- a/compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs +++ b/compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs @@ -256,8 +256,8 @@ impl<'tcx> BorrowExplanation<'tcx> { impl<'hir> rustc_hir::intravisit::Visitor<'hir> for FindLetExpr<'hir> { type NestedFilter = rustc_middle::hir::nested_filter::OnlyBodies; - fn nested_visit_map(&mut self) -> Self::Map { - self.tcx.hir() + fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt { + self.tcx } fn visit_expr(&mut self, expr: &'hir hir::Expr<'hir>) { if let hir::ExprKind::If(cond, _conseq, _alt) diff --git a/compiler/rustc_borrowck/src/diagnostics/move_errors.rs b/compiler/rustc_borrowck/src/diagnostics/move_errors.rs index c5449952f99..38f22473edf 100644 --- a/compiler/rustc_borrowck/src/diagnostics/move_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/move_errors.rs @@ -7,7 +7,7 @@ use rustc_hir::intravisit::Visitor; use rustc_hir::{self as hir, CaptureBy, ExprKind, HirId, Node}; use rustc_middle::bug; use rustc_middle::mir::*; -use rustc_middle::ty::{self, Ty}; +use rustc_middle::ty::{self, Ty, TyCtxt}; use rustc_mir_dataflow::move_paths::{LookupResult, MovePathIndex}; use rustc_span::{BytePos, ExpnKind, MacroKind, Span}; use rustc_trait_selection::error_reporting::traits::FindExprBySpan; @@ -690,7 +690,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> { /// make it bind by reference instead (if possible) struct BindingFinder<'tcx> { typeck_results: &'tcx ty::TypeckResults<'tcx>, - hir: rustc_middle::hir::map::Map<'tcx>, + tcx: TyCtxt<'tcx>, /// Input: the span of the pattern we're finding bindings in pat_span: Span, /// Input: the spans of the bindings we're providing suggestions for @@ -709,8 +709,8 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> { impl<'tcx> Visitor<'tcx> for BindingFinder<'tcx> { type NestedFilter = rustc_middle::hir::nested_filter::OnlyBodies; - fn nested_visit_map(&mut self) -> Self::Map { - self.hir + fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt { + self.tcx } fn visit_expr(&mut self, ex: &'tcx hir::Expr<'tcx>) -> Self::Result { @@ -782,7 +782,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> { let typeck_results = self.infcx.tcx.typeck(self.mir_def_id()); let mut finder = BindingFinder { typeck_results, - hir, + tcx: self.infcx.tcx, pat_span, binding_spans, found_pat: false, diff --git a/compiler/rustc_hir/src/intravisit.rs b/compiler/rustc_hir/src/intravisit.rs index f57d1a5cc2c..949f8daa4dc 100644 --- a/compiler/rustc_hir/src/intravisit.rs +++ b/compiler/rustc_hir/src/intravisit.rs @@ -18,7 +18,7 @@ //! within one another. //! - Example: Examine each expression to look for its type and do some check or other. //! - How: Implement `intravisit::Visitor` and override the `NestedFilter` type to -//! `nested_filter::OnlyBodies` (and implement `nested_visit_map`), and use +//! `nested_filter::OnlyBodies` (and implement `maybe_tcx`), and use //! `tcx.hir().visit_all_item_likes_in_crate(&mut visitor)`. Within your //! `intravisit::Visitor` impl, implement methods like `visit_expr()` (don't forget to invoke //! `intravisit::walk_expr()` to keep walking the subparts). @@ -30,7 +30,7 @@ //! - Example: Lifetime resolution, which wants to bring lifetimes declared on the //! impl into scope while visiting the impl-items, and then back out again. //! - How: Implement `intravisit::Visitor` and override the `NestedFilter` type to -//! `nested_filter::All` (and implement `nested_visit_map`). Walk your crate with +//! `nested_filter::All` (and implement `maybe_tcx`). Walk your crate with //! `tcx.hir().walk_toplevel_module(visitor)` invoked on `tcx.hir().krate()`. //! - Pro: Visitor methods for any kind of HIR node, not just item-like things. //! - Pro: Preserves nesting information @@ -106,41 +106,43 @@ impl<'a> FnKind<'a> { } } -/// An abstract representation of the HIR `rustc_middle::hir::map::Map`. -pub trait Map<'hir> { +/// HIR things retrievable from `TyCtxt`, avoiding an explicit dependence on +/// `TyCtxt`. The only impls are for `!` (where these functions are never +/// called) and `TyCtxt` (in `rustc_middle`). +pub trait HirTyCtxt<'hir> { /// Retrieves the `Node` corresponding to `id`. fn hir_node(&self, hir_id: HirId) -> Node<'hir>; - fn body(&self, id: BodyId) -> &'hir Body<'hir>; - fn item(&self, id: ItemId) -> &'hir Item<'hir>; - fn trait_item(&self, id: TraitItemId) -> &'hir TraitItem<'hir>; - fn impl_item(&self, id: ImplItemId) -> &'hir ImplItem<'hir>; - fn foreign_item(&self, id: ForeignItemId) -> &'hir ForeignItem<'hir>; + fn hir_body(&self, id: BodyId) -> &'hir Body<'hir>; + fn hir_item(&self, id: ItemId) -> &'hir Item<'hir>; + fn hir_trait_item(&self, id: TraitItemId) -> &'hir TraitItem<'hir>; + fn hir_impl_item(&self, id: ImplItemId) -> &'hir ImplItem<'hir>; + fn hir_foreign_item(&self, id: ForeignItemId) -> &'hir ForeignItem<'hir>; } -// Used when no map is actually available, forcing manual implementation of nested visitors. -impl<'hir> Map<'hir> for ! { +// Used when no tcx is actually available, forcing manual implementation of nested visitors. +impl<'hir> HirTyCtxt<'hir> for ! { fn hir_node(&self, _: HirId) -> Node<'hir> { unreachable!(); } - fn body(&self, _: BodyId) -> &'hir Body<'hir> { + fn hir_body(&self, _: BodyId) -> &'hir Body<'hir> { unreachable!(); } - fn item(&self, _: ItemId) -> &'hir Item<'hir> { + fn hir_item(&self, _: ItemId) -> &'hir Item<'hir> { unreachable!(); } - fn trait_item(&self, _: TraitItemId) -> &'hir TraitItem<'hir> { + fn hir_trait_item(&self, _: TraitItemId) -> &'hir TraitItem<'hir> { unreachable!(); } - fn impl_item(&self, _: ImplItemId) -> &'hir ImplItem<'hir> { + fn hir_impl_item(&self, _: ImplItemId) -> &'hir ImplItem<'hir> { unreachable!(); } - fn foreign_item(&self, _: ForeignItemId) -> &'hir ForeignItem<'hir> { + fn hir_foreign_item(&self, _: ForeignItemId) -> &'hir ForeignItem<'hir> { unreachable!(); } } pub mod nested_filter { - use super::Map; + use super::HirTyCtxt; /// Specifies what nested things a visitor wants to visit. By "nested /// things", we are referring to bits of HIR that are not directly embedded @@ -155,7 +157,7 @@ pub mod nested_filter { /// See the comments at [`rustc_hir::intravisit`] for more details on the overall /// visit strategy. pub trait NestedFilter<'hir> { - type Map: Map<'hir>; + type MaybeTyCtxt: HirTyCtxt<'hir>; /// Whether the visitor visits nested "item-like" things. /// E.g., item, impl-item. @@ -171,10 +173,10 @@ pub mod nested_filter { /// /// Use this if you are only walking some particular kind of tree /// (i.e., a type, or fn signature) and you don't want to thread a - /// HIR map around. + /// `tcx` around. pub struct None(()); impl NestedFilter<'_> for None { - type Map = !; + type MaybeTyCtxt = !; const INTER: bool = false; const INTRA: bool = false; } @@ -199,18 +201,18 @@ use nested_filter::NestedFilter; /// to monitor future changes to `Visitor` in case a new method with a /// new default implementation gets introduced.) pub trait Visitor<'v>: Sized { - // This type should not be overridden, it exists for convenient usage as `Self::Map`. - type Map: Map<'v> = <Self::NestedFilter as NestedFilter<'v>>::Map; + // This type should not be overridden, it exists for convenient usage as `Self::MaybeTyCtxt`. + type MaybeTyCtxt: HirTyCtxt<'v> = <Self::NestedFilter as NestedFilter<'v>>::MaybeTyCtxt; /////////////////////////////////////////////////////////////////////////// // Nested items. /// Override this type to control which nested HIR are visited; see /// [`NestedFilter`] for details. If you override this type, you - /// must also override [`nested_visit_map`](Self::nested_visit_map). + /// must also override [`maybe_tcx`](Self::maybe_tcx). /// /// **If for some reason you want the nested behavior, but don't - /// have a `Map` at your disposal:** then override the + /// have a `tcx` at your disposal:** then override the /// `visit_nested_XXX` methods. If a new `visit_nested_XXX` variant is /// added in the future, it will cause a panic which can be detected /// and fixed appropriately. @@ -222,9 +224,9 @@ pub trait Visitor<'v>: Sized { /// If `type NestedFilter` is set to visit nested items, this method /// must also be overridden to provide a map to retrieve nested items. - fn nested_visit_map(&mut self) -> Self::Map { + fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt { panic!( - "nested_visit_map must be implemented or consider using \ + "maybe_tcx must be implemented or consider using \ `type NestedFilter = nested_filter::None` (the default)" ); } @@ -236,10 +238,10 @@ pub trait Visitor<'v>: Sized { /// "deep" visit patterns described at /// [`rustc_hir::intravisit`]. The only reason to override /// this method is if you want a nested pattern but cannot supply a - /// [`Map`]; see `nested_visit_map` for advice. + /// `TyCtxt`; see `maybe_tcx` for advice. fn visit_nested_item(&mut self, id: ItemId) -> Self::Result { if Self::NestedFilter::INTER { - let item = self.nested_visit_map().item(id); + let item = self.maybe_tcx().hir_item(id); try_visit!(self.visit_item(item)); } Self::Result::output() @@ -250,7 +252,7 @@ pub trait Visitor<'v>: Sized { /// method. fn visit_nested_trait_item(&mut self, id: TraitItemId) -> Self::Result { if Self::NestedFilter::INTER { - let item = self.nested_visit_map().trait_item(id); + let item = self.maybe_tcx().hir_trait_item(id); try_visit!(self.visit_trait_item(item)); } Self::Result::output() @@ -261,7 +263,7 @@ pub trait Visitor<'v>: Sized { /// method. fn visit_nested_impl_item(&mut self, id: ImplItemId) -> Self::Result { if Self::NestedFilter::INTER { - let item = self.nested_visit_map().impl_item(id); + let item = self.maybe_tcx().hir_impl_item(id); try_visit!(self.visit_impl_item(item)); } Self::Result::output() @@ -272,7 +274,7 @@ pub trait Visitor<'v>: Sized { /// method. fn visit_nested_foreign_item(&mut self, id: ForeignItemId) -> Self::Result { if Self::NestedFilter::INTER { - let item = self.nested_visit_map().foreign_item(id); + let item = self.maybe_tcx().hir_foreign_item(id); try_visit!(self.visit_foreign_item(item)); } Self::Result::output() @@ -283,7 +285,7 @@ pub trait Visitor<'v>: Sized { /// `Self::NestedFilter`. fn visit_nested_body(&mut self, id: BodyId) -> Self::Result { if Self::NestedFilter::INTRA { - let body = self.nested_visit_map().body(id); + let body = self.maybe_tcx().hir_body(id); try_visit!(self.visit_body(body)); } Self::Result::output() diff --git a/compiler/rustc_hir_analysis/src/check/check.rs b/compiler/rustc_hir_analysis/src/check/check.rs index 0d33aaa5d84..bc0fd4b705d 100644 --- a/compiler/rustc_hir_analysis/src/check/check.rs +++ b/compiler/rustc_hir_analysis/src/check/check.rs @@ -435,8 +435,8 @@ fn best_definition_site_of_opaque<'tcx>( impl<'tcx> intravisit::Visitor<'tcx> for TaitConstraintLocator<'tcx> { type NestedFilter = nested_filter::All; type Result = ControlFlow<(Span, LocalDefId)>; - fn nested_visit_map(&mut self) -> Self::Map { - self.tcx.hir() + fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt { + self.tcx } fn visit_expr(&mut self, ex: &'tcx hir::Expr<'tcx>) -> Self::Result { if let hir::ExprKind::Closure(closure) = ex.kind { diff --git a/compiler/rustc_hir_analysis/src/collect.rs b/compiler/rustc_hir_analysis/src/collect.rs index 6b430df8ed2..75ea207a06b 100644 --- a/compiler/rustc_hir_analysis/src/collect.rs +++ b/compiler/rustc_hir_analysis/src/collect.rs @@ -277,8 +277,8 @@ fn reject_placeholder_type_signatures_in_item<'tcx>( impl<'tcx> Visitor<'tcx> for CollectItemTypesVisitor<'tcx> { type NestedFilter = nested_filter::OnlyBodies; - fn nested_visit_map(&mut self) -> Self::Map { - self.tcx.hir() + fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt { + self.tcx } fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) { diff --git a/compiler/rustc_hir_analysis/src/collect/dump.rs b/compiler/rustc_hir_analysis/src/collect/dump.rs index b4ba4d11221..63c445fa6a3 100644 --- a/compiler/rustc_hir_analysis/src/collect/dump.rs +++ b/compiler/rustc_hir_analysis/src/collect/dump.rs @@ -63,8 +63,8 @@ pub(crate) fn def_parents(tcx: TyCtxt<'_>) { impl<'tcx> intravisit::Visitor<'tcx> for AnonConstFinder<'tcx> { type NestedFilter = nested_filter::All; - fn nested_visit_map(&mut self) -> Self::Map { - self.tcx.hir() + fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt { + self.tcx } fn visit_anon_const(&mut self, c: &'tcx rustc_hir::AnonConst) { diff --git a/compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs b/compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs index 6bf0498a8b0..5a8a2f1fe28 100644 --- a/compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs +++ b/compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs @@ -422,8 +422,8 @@ enum NonLifetimeBinderAllowed { impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> { type NestedFilter = nested_filter::OnlyBodies; - fn nested_visit_map(&mut self) -> Self::Map { - self.tcx.hir() + fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt { + self.tcx } fn visit_nested_body(&mut self, body: hir::BodyId) { diff --git a/compiler/rustc_hir_analysis/src/collect/type_of/opaque.rs b/compiler/rustc_hir_analysis/src/collect/type_of/opaque.rs index e2b9fe0f9f7..88d4acbdd8d 100644 --- a/compiler/rustc_hir_analysis/src/collect/type_of/opaque.rs +++ b/compiler/rustc_hir_analysis/src/collect/type_of/opaque.rs @@ -298,8 +298,8 @@ impl TaitConstraintLocator<'_> { impl<'tcx> intravisit::Visitor<'tcx> for TaitConstraintLocator<'tcx> { type NestedFilter = nested_filter::All; - fn nested_visit_map(&mut self) -> Self::Map { - self.tcx.hir() + fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt { + self.tcx } fn visit_expr(&mut self, ex: &'tcx Expr<'tcx>) { if let hir::ExprKind::Closure(closure) = ex.kind { @@ -441,8 +441,8 @@ impl RpitConstraintChecker<'_> { impl<'tcx> intravisit::Visitor<'tcx> for RpitConstraintChecker<'tcx> { type NestedFilter = nested_filter::OnlyBodies; - fn nested_visit_map(&mut self) -> Self::Map { - self.tcx.hir() + fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt { + self.tcx } fn visit_expr(&mut self, ex: &'tcx Expr<'tcx>) { if let hir::ExprKind::Closure(closure) = ex.kind { diff --git a/compiler/rustc_hir_pretty/src/lib.rs b/compiler/rustc_hir_pretty/src/lib.rs index 57df3127a02..34124a6aa11 100644 --- a/compiler/rustc_hir_pretty/src/lib.rs +++ b/compiler/rustc_hir_pretty/src/lib.rs @@ -24,8 +24,8 @@ use rustc_span::source_map::SourceMap; use rustc_span::{FileName, Ident, Span, Symbol, kw}; use {rustc_ast as ast, rustc_hir as hir}; -pub fn id_to_string(map: &dyn rustc_hir::intravisit::Map<'_>, hir_id: HirId) -> String { - to_string(&map, |s| s.print_node(map.hir_node(hir_id))) +pub fn id_to_string(cx: &dyn rustc_hir::intravisit::HirTyCtxt<'_>, hir_id: HirId) -> String { + to_string(&cx, |s| s.print_node(cx.hir_node(hir_id))) } pub enum AnnNode<'a> { @@ -54,15 +54,15 @@ pub trait PpAnn { fn post(&self, _state: &mut State<'_>, _node: AnnNode<'_>) {} } -impl PpAnn for &dyn rustc_hir::intravisit::Map<'_> { +impl PpAnn for &dyn rustc_hir::intravisit::HirTyCtxt<'_> { fn nested(&self, state: &mut State<'_>, nested: Nested) { match nested { - Nested::Item(id) => state.print_item(self.item(id)), - Nested::TraitItem(id) => state.print_trait_item(self.trait_item(id)), - Nested::ImplItem(id) => state.print_impl_item(self.impl_item(id)), - Nested::ForeignItem(id) => state.print_foreign_item(self.foreign_item(id)), - Nested::Body(id) => state.print_expr(self.body(id).value), - Nested::BodyParamPat(id, i) => state.print_pat(self.body(id).params[i].pat), + Nested::Item(id) => state.print_item(self.hir_item(id)), + Nested::TraitItem(id) => state.print_trait_item(self.hir_trait_item(id)), + Nested::ImplItem(id) => state.print_impl_item(self.hir_impl_item(id)), + Nested::ForeignItem(id) => state.print_foreign_item(self.hir_foreign_item(id)), + Nested::Body(id) => state.print_expr(self.hir_body(id).value), + Nested::BodyParamPat(id, i) => state.print_pat(self.hir_body(id).params[i].pat), } } } diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs index 7963688203a..c0af5ea7bbf 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs @@ -2725,8 +2725,8 @@ struct FindClosureArg<'tcx> { impl<'tcx> Visitor<'tcx> for FindClosureArg<'tcx> { type NestedFilter = rustc_middle::hir::nested_filter::All; - fn nested_visit_map(&mut self) -> Self::Map { - self.tcx.hir() + fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt { + self.tcx } fn visit_expr(&mut self, ex: &'tcx hir::Expr<'tcx>) { diff --git a/compiler/rustc_incremental/src/assert_dep_graph.rs b/compiler/rustc_incremental/src/assert_dep_graph.rs index 569034954c3..c2f9ebc5262 100644 --- a/compiler/rustc_incremental/src/assert_dep_graph.rs +++ b/compiler/rustc_incremental/src/assert_dep_graph.rs @@ -174,8 +174,8 @@ impl<'tcx> IfThisChanged<'tcx> { impl<'tcx> Visitor<'tcx> for IfThisChanged<'tcx> { type NestedFilter = nested_filter::OnlyBodies; - fn nested_visit_map(&mut self) -> Self::Map { - self.tcx.hir() + fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt { + self.tcx } fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) { diff --git a/compiler/rustc_incremental/src/persist/dirty_clean.rs b/compiler/rustc_incremental/src/persist/dirty_clean.rs index 30590bf5d3c..a95599e7a86 100644 --- a/compiler/rustc_incremental/src/persist/dirty_clean.rs +++ b/compiler/rustc_incremental/src/persist/dirty_clean.rs @@ -455,8 +455,8 @@ impl<'tcx> FindAllAttrs<'tcx> { impl<'tcx> intravisit::Visitor<'tcx> for FindAllAttrs<'tcx> { type NestedFilter = nested_filter::All; - fn nested_visit_map(&mut self) -> Self::Map { - self.tcx.hir() + fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt { + self.tcx } fn visit_attribute(&mut self, attr: &'tcx Attribute) { diff --git a/compiler/rustc_lint/src/late.rs b/compiler/rustc_lint/src/late.rs index c0d3df17d77..f17570e4a81 100644 --- a/compiler/rustc_lint/src/late.rs +++ b/compiler/rustc_lint/src/late.rs @@ -84,8 +84,8 @@ impl<'tcx, T: LateLintPass<'tcx>> hir_visit::Visitor<'tcx> for LateContextAndPas /// Because lints are scoped lexically, we want to walk nested /// items in the context of the outer item, so enable /// deep-walking. - fn nested_visit_map(&mut self) -> Self::Map { - self.context.tcx.hir() + fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt { + self.context.tcx } fn visit_nested_body(&mut self, body_id: hir::BodyId) { diff --git a/compiler/rustc_lint/src/levels.rs b/compiler/rustc_lint/src/levels.rs index 5a4ed68440a..7e13205bbf1 100644 --- a/compiler/rustc_lint/src/levels.rs +++ b/compiler/rustc_lint/src/levels.rs @@ -270,8 +270,8 @@ impl<'tcx> LintLevelsBuilder<'_, LintLevelQueryMap<'tcx>> { impl<'tcx> Visitor<'tcx> for LintLevelsBuilder<'_, LintLevelQueryMap<'tcx>> { type NestedFilter = nested_filter::OnlyBodies; - fn nested_visit_map(&mut self) -> Self::Map { - self.provider.tcx.hir() + fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt { + self.provider.tcx } fn visit_param(&mut self, param: &'tcx hir::Param<'tcx>) { @@ -365,8 +365,8 @@ impl<'tcx> LintLevelMaximum<'tcx> { impl<'tcx> Visitor<'tcx> for LintLevelMaximum<'tcx> { type NestedFilter = nested_filter::All; - fn nested_visit_map(&mut self) -> Self::Map { - self.tcx.hir() + fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt { + self.tcx } /// FIXME(blyxyas): In a future revision, we should also graph #![allow]s, diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 3ca4b3e235f..1ac2f6e81e5 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -2410,7 +2410,6 @@ pub(crate) fn provide(providers: &mut Providers) { /// use a different method for pretty-printing. Ideally this function /// should only ever be used as a fallback. pub fn rendered_const<'tcx>(tcx: TyCtxt<'tcx>, body: &hir::Body<'_>, def_id: LocalDefId) -> String { - let hir = tcx.hir(); let value = body.value; #[derive(PartialEq, Eq)] @@ -2467,7 +2466,7 @@ pub fn rendered_const<'tcx>(tcx: TyCtxt<'tcx>, body: &hir::Body<'_>, def_id: Loc // Otherwise we prefer pretty-printing to get rid of extraneous whitespace, comments and // other formatting artifacts. - Literal | Simple => id_to_string(&hir, body.id().hir_id), + Literal | Simple => id_to_string(&tcx, body.id().hir_id), // FIXME: Omit the curly braces if the enclosing expression is an array literal // with a repeated element (an `ExprKind::Repeat`) as in such case it diff --git a/compiler/rustc_middle/src/hir/map.rs b/compiler/rustc_middle/src/hir/map.rs index 737b491ef57..9bd35fdd6f0 100644 --- a/compiler/rustc_middle/src/hir/map.rs +++ b/compiler/rustc_middle/src/hir/map.rs @@ -1034,35 +1034,35 @@ impl<'hir> Map<'hir> { } } -impl<'hir> intravisit::Map<'hir> for Map<'hir> { - fn hir_node(&self, hir_id: HirId) -> Node<'hir> { - self.tcx.hir_node(hir_id) +impl<'tcx> intravisit::HirTyCtxt<'tcx> for TyCtxt<'tcx> { + fn hir_node(&self, hir_id: HirId) -> Node<'tcx> { + (*self).hir_node(hir_id) } - fn body(&self, id: BodyId) -> &'hir Body<'hir> { - self.tcx.hir_body(id) + fn hir_body(&self, id: BodyId) -> &'tcx Body<'tcx> { + (*self).hir_body(id) } - fn item(&self, id: ItemId) -> &'hir Item<'hir> { - self.tcx.hir_item(id) + fn hir_item(&self, id: ItemId) -> &'tcx Item<'tcx> { + (*self).hir_item(id) } - fn trait_item(&self, id: TraitItemId) -> &'hir TraitItem<'hir> { - self.tcx.hir_trait_item(id) + fn hir_trait_item(&self, id: TraitItemId) -> &'tcx TraitItem<'tcx> { + (*self).hir_trait_item(id) } - fn impl_item(&self, id: ImplItemId) -> &'hir ImplItem<'hir> { - self.tcx.hir_impl_item(id) + fn hir_impl_item(&self, id: ImplItemId) -> &'tcx ImplItem<'tcx> { + (*self).hir_impl_item(id) } - fn foreign_item(&self, id: ForeignItemId) -> &'hir ForeignItem<'hir> { - self.tcx.hir_foreign_item(id) + fn hir_foreign_item(&self, id: ForeignItemId) -> &'tcx ForeignItem<'tcx> { + (*self).hir_foreign_item(id) } } impl<'tcx> pprust_hir::PpAnn for TyCtxt<'tcx> { fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) { - pprust_hir::PpAnn::nested(&(&self.hir() as &dyn intravisit::Map<'_>), state, nested) + pprust_hir::PpAnn::nested(&(self as &dyn intravisit::HirTyCtxt<'_>), state, nested) } } @@ -1349,8 +1349,8 @@ impl<'tcx> ItemCollector<'tcx> { impl<'hir> Visitor<'hir> for ItemCollector<'hir> { type NestedFilter = nested_filter::All; - fn nested_visit_map(&mut self) -> Self::Map { - self.tcx.hir() + fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt { + self.tcx } fn visit_item(&mut self, item: &'hir Item<'hir>) { diff --git a/compiler/rustc_middle/src/hir/nested_filter.rs b/compiler/rustc_middle/src/hir/nested_filter.rs index adbe81bb22c..e18c28a8eac 100644 --- a/compiler/rustc_middle/src/hir/nested_filter.rs +++ b/compiler/rustc_middle/src/hir/nested_filter.rs @@ -1,5 +1,7 @@ use rustc_hir::intravisit::nested_filter::NestedFilter; +use crate::ty::TyCtxt; + /// Do not visit nested item-like things, but visit nested things /// that are inside of an item-like. /// @@ -12,8 +14,8 @@ use rustc_hir::intravisit::nested_filter::NestedFilter; /// and to have the visitor that visits the contents of each item /// using this setting. pub struct OnlyBodies(()); -impl<'hir> NestedFilter<'hir> for OnlyBodies { - type Map = crate::hir::map::Map<'hir>; +impl<'tcx> NestedFilter<'tcx> for OnlyBodies { + type MaybeTyCtxt = TyCtxt<'tcx>; const INTER: bool = false; const INTRA: bool = true; } @@ -24,8 +26,8 @@ impl<'hir> NestedFilter<'hir> for OnlyBodies { /// process everything within their lexical context. Typically you /// kick off the visit by doing `walk_krate()`. pub struct All(()); -impl<'hir> NestedFilter<'hir> for All { - type Map = crate::hir::map::Map<'hir>; +impl<'tcx> NestedFilter<'tcx> for All { + type MaybeTyCtxt = TyCtxt<'tcx>; const INTER: bool = true; const INTRA: bool = true; } diff --git a/compiler/rustc_mir_transform/src/coverage/mod.rs b/compiler/rustc_mir_transform/src/coverage/mod.rs index 081e7b1a43c..264995efe8f 100644 --- a/compiler/rustc_mir_transform/src/coverage/mod.rs +++ b/compiler/rustc_mir_transform/src/coverage/mod.rs @@ -10,7 +10,6 @@ mod unexpand; use rustc_hir as hir; use rustc_hir::intravisit::{Visitor, walk_expr}; -use rustc_middle::hir::map::Map; use rustc_middle::hir::nested_filter; use rustc_middle::mir::coverage::{ CoverageKind, DecisionInfo, FunctionCoverageInfo, Mapping, MappingKind, @@ -348,7 +347,7 @@ fn extract_hole_spans_from_hir<'tcx>( hir_body: &hir::Body<'tcx>, ) -> Vec<Span> { struct HolesVisitor<'hir, F> { - hir: Map<'hir>, + tcx: TyCtxt<'hir>, visit_hole_span: F, } @@ -360,8 +359,8 @@ fn extract_hole_spans_from_hir<'tcx>( /// items contained within them. type NestedFilter = nested_filter::All; - fn nested_visit_map(&mut self) -> Self::Map { - self.hir + fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt { + self.tcx } fn visit_item(&mut self, item: &'hir hir::Item<'hir>) { @@ -388,7 +387,7 @@ fn extract_hole_spans_from_hir<'tcx>( let mut hole_spans = vec![]; let mut visitor = HolesVisitor { - hir: tcx.hir(), + tcx, visit_hole_span: |hole_span| { // Discard any holes that aren't directly visible within the body span. if body_span.contains(hole_span) && body_span.eq_ctxt(hole_span) { diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index e96db885131..396a0cbc889 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -2606,8 +2606,8 @@ impl<'tcx> CheckAttrVisitor<'tcx> { impl<'tcx> Visitor<'tcx> for CheckAttrVisitor<'tcx> { type NestedFilter = nested_filter::OnlyBodies; - fn nested_visit_map(&mut self) -> Self::Map { - self.tcx.hir() + fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt { + self.tcx } fn visit_item(&mut self, item: &'tcx Item<'tcx>) { diff --git a/compiler/rustc_passes/src/hir_id_validator.rs b/compiler/rustc_passes/src/hir_id_validator.rs index 34d60d90be5..43f2f892e77 100644 --- a/compiler/rustc_passes/src/hir_id_validator.rs +++ b/compiler/rustc_passes/src/hir_id_validator.rs @@ -105,8 +105,8 @@ impl<'a, 'hir> HirIdValidator<'a, 'hir> { impl<'a, 'hir> intravisit::Visitor<'hir> for HirIdValidator<'a, 'hir> { type NestedFilter = nested_filter::OnlyBodies; - fn nested_visit_map(&mut self) -> Self::Map { - self.tcx.hir() + fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt { + self.tcx } fn visit_nested_item(&mut self, id: hir::ItemId) { diff --git a/compiler/rustc_passes/src/lib_features.rs b/compiler/rustc_passes/src/lib_features.rs index 600c46eb3d0..09d59e1f717 100644 --- a/compiler/rustc_passes/src/lib_features.rs +++ b/compiler/rustc_passes/src/lib_features.rs @@ -129,8 +129,8 @@ impl<'tcx> LibFeatureCollector<'tcx> { impl<'tcx> Visitor<'tcx> for LibFeatureCollector<'tcx> { type NestedFilter = nested_filter::All; - fn nested_visit_map(&mut self) -> Self::Map { - self.tcx.hir() + fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt { + self.tcx } fn visit_attribute(&mut self, attr: &'tcx Attribute) { diff --git a/compiler/rustc_passes/src/loops.rs b/compiler/rustc_passes/src/loops.rs index 6eef0b926a1..0fd980af0b5 100644 --- a/compiler/rustc_passes/src/loops.rs +++ b/compiler/rustc_passes/src/loops.rs @@ -87,8 +87,8 @@ pub(crate) fn provide(providers: &mut Providers) { impl<'hir> Visitor<'hir> for CheckLoopVisitor<'hir> { type NestedFilter = nested_filter::OnlyBodies; - fn nested_visit_map(&mut self) -> Self::Map { - self.tcx.hir() + fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt { + self.tcx } fn visit_anon_const(&mut self, c: &'hir hir::AnonConst) { diff --git a/compiler/rustc_passes/src/naked_functions.rs b/compiler/rustc_passes/src/naked_functions.rs index 23322be8f55..cb17b0f6cf5 100644 --- a/compiler/rustc_passes/src/naked_functions.rs +++ b/compiler/rustc_passes/src/naked_functions.rs @@ -259,8 +259,8 @@ struct CheckNakedAsmInNakedFn<'tcx> { impl<'tcx> Visitor<'tcx> for CheckNakedAsmInNakedFn<'tcx> { type NestedFilter = OnlyBodies; - fn nested_visit_map(&mut self) -> Self::Map { - self.tcx.hir() + fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt { + self.tcx } fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) { diff --git a/compiler/rustc_passes/src/stability.rs b/compiler/rustc_passes/src/stability.rs index fd30d0d4867..d045e61d1d4 100644 --- a/compiler/rustc_passes/src/stability.rs +++ b/compiler/rustc_passes/src/stability.rs @@ -394,8 +394,8 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> { /// deep-walking. type NestedFilter = nested_filter::All; - fn nested_visit_map(&mut self) -> Self::Map { - self.tcx.hir() + fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt { + self.tcx } fn visit_item(&mut self, i: &'tcx Item<'tcx>) { @@ -616,8 +616,8 @@ impl<'tcx> MissingStabilityAnnotations<'tcx> { impl<'tcx> Visitor<'tcx> for MissingStabilityAnnotations<'tcx> { type NestedFilter = nested_filter::OnlyBodies; - fn nested_visit_map(&mut self) -> Self::Map { - self.tcx.hir() + fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt { + self.tcx } fn visit_item(&mut self, i: &'tcx Item<'tcx>) { @@ -756,8 +756,8 @@ impl<'tcx> Visitor<'tcx> for Checker<'tcx> { /// Because stability levels are scoped lexically, we want to walk /// nested items in the context of the outer item, so enable /// deep-walking. - fn nested_visit_map(&mut self) -> Self::Map { - self.tcx.hir() + fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt { + self.tcx } fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) { diff --git a/compiler/rustc_trait_selection/src/error_reporting/infer/need_type_info.rs b/compiler/rustc_trait_selection/src/error_reporting/infer/need_type_info.rs index 57b614833af..55eb3f777d6 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/infer/need_type_info.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/infer/need_type_info.rs @@ -1202,8 +1202,8 @@ impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> { impl<'a, 'tcx> Visitor<'tcx> for FindInferSourceVisitor<'a, 'tcx> { type NestedFilter = nested_filter::OnlyBodies; - fn nested_visit_map(&mut self) -> Self::Map { - self.tecx.tcx.hir() + fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt { + self.tecx.tcx } fn visit_local(&mut self, local: &'tcx LetStmt<'tcx>) { diff --git a/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/find_anon_type.rs b/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/find_anon_type.rs index b9f3abc2534..5a303c3cd03 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/find_anon_type.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/find_anon_type.rs @@ -3,7 +3,6 @@ use core::ops::ControlFlow; use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_hir::intravisit::{self, Visitor, VisitorExt}; use rustc_hir::{self as hir, AmbigArg}; -use rustc_middle::hir::map::Map; use rustc_middle::hir::nested_filter; use rustc_middle::middle::resolve_bound_vars as rbv; use rustc_middle::ty::{self, Region, TyCtxt}; @@ -70,8 +69,8 @@ impl<'tcx> Visitor<'tcx> for FindNestedTypeVisitor<'tcx> { type Result = ControlFlow<&'tcx hir::Ty<'tcx>>; type NestedFilter = nested_filter::OnlyBodies; - fn nested_visit_map(&mut self) -> Self::Map { - self.tcx.hir() + fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt { + self.tcx } fn visit_ty(&mut self, arg: &'tcx hir::Ty<'tcx, AmbigArg>) -> Self::Result { @@ -176,8 +175,8 @@ impl<'tcx> Visitor<'tcx> for TyPathVisitor<'tcx> { type Result = ControlFlow<()>; type NestedFilter = nested_filter::OnlyBodies; - fn nested_visit_map(&mut self) -> Map<'tcx> { - self.tcx.hir() + fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt { + self.tcx } fn visit_lifetime(&mut self, lifetime: &hir::Lifetime) -> Self::Result { diff --git a/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/trait_impl_difference.rs b/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/trait_impl_difference.rs index bbcd28c0835..0695cc57978 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/trait_impl_difference.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/trait_impl_difference.rs @@ -133,8 +133,8 @@ struct TypeParamSpanVisitor<'tcx> { impl<'tcx> Visitor<'tcx> for TypeParamSpanVisitor<'tcx> { type NestedFilter = nested_filter::OnlyBodies; - fn nested_visit_map(&mut self) -> Self::Map { - self.tcx.hir() + fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt { + self.tcx } fn visit_ty(&mut self, arg: &'tcx hir::Ty<'tcx, AmbigArg>) { diff --git a/compiler/rustc_trait_selection/src/error_reporting/traits/mod.rs b/compiler/rustc_trait_selection/src/error_reporting/traits/mod.rs index d786d04b8f6..22d219cd64d 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/traits/mod.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/traits/mod.rs @@ -68,8 +68,8 @@ impl<'hir> FindExprBySpan<'hir> { impl<'v> Visitor<'v> for FindExprBySpan<'v> { type NestedFilter = rustc_middle::hir::nested_filter::OnlyBodies; - fn nested_visit_map(&mut self) -> Self::Map { - self.tcx.hir() + fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt { + self.tcx } fn visit_expr(&mut self, ex: &'v hir::Expr<'v>) { |
