1
Fork 0

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`.
This commit is contained in:
Nicholas Nethercote 2025-02-03 14:42:01 +11:00
parent f86f7ad5f2
commit 661f99ba03
59 changed files with 208 additions and 210 deletions

View file

@ -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>) {

View file

@ -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;
}