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

@ -703,7 +703,7 @@ fn build_module_items(
pub(crate) fn print_inlined_const(tcx: TyCtxt<'_>, did: DefId) -> String {
if let Some(did) = did.as_local() {
let hir_id = tcx.local_def_id_to_hir_id(did);
rustc_hir_pretty::id_to_string(&tcx.hir(), hir_id)
rustc_hir_pretty::id_to_string(&tcx, hir_id)
} else {
tcx.rendered_const(did).clone()
}

View file

@ -464,10 +464,10 @@ impl<'tcx> EmitIgnoredResolutionErrors<'tcx> {
impl<'tcx> Visitor<'tcx> for EmitIgnoredResolutionErrors<'tcx> {
type NestedFilter = nested_filter::OnlyBodies;
fn nested_visit_map(&mut self) -> Self::Map {
fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
// We need to recurse into nested closures,
// since those will fallback to the parent for type checking.
self.tcx.hir()
self.tcx
}
fn visit_path(&mut self, path: &Path<'tcx>, _id: HirId) {

View file

@ -147,14 +147,14 @@ impl HirCollector<'_> {
impl<'tcx> intravisit::Visitor<'tcx> for HirCollector<'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_item(&mut self, item: &'tcx hir::Item<'_>) {
let name = match &item.kind {
hir::ItemKind::Impl(impl_) => {
rustc_hir_pretty::id_to_string(&self.tcx.hir(), impl_.self_ty.hir_id)
rustc_hir_pretty::id_to_string(&self.tcx, impl_.self_ty.hir_id)
}
_ => item.ident.to_string(),
};

View file

@ -221,8 +221,8 @@ impl SpanMapVisitor<'_> {
impl<'tcx> Visitor<'tcx> for SpanMapVisitor<'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_path(&mut self, path: &rustc_hir::Path<'tcx>, _id: HirId) {

View file

@ -123,8 +123,8 @@ where
{
type NestedFilter = nested_filter::OnlyBodies;
fn nested_visit_map(&mut self) -> Self::Map {
self.cx.tcx().hir()
fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
self.cx.tcx()
}
fn visit_expr(&mut self, ex: &'tcx hir::Expr<'tcx>) {

View file

@ -563,8 +563,8 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
impl<'tcx> Visitor<'tcx> for RustdocVisitor<'_, 'tcx> {
type NestedFilter = nested_filter::All;
fn nested_visit_map(&mut self) -> Self::Map {
self.cx.tcx.hir()
fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
self.cx.tcx
}
fn visit_item(&mut self, i: &'tcx hir::Item<'tcx>) {