1
Fork 0

Move some Map methods onto TyCtxt.

The end goal is to eliminate `Map` altogether.

I added a `hir_` prefix to all of them, that seemed simplest. The
exceptions are `module_items` which became `hir_module_free_items` because
there was already a `hir_module_items`, and `items` which became
`hir_free_items` for consistency with `hir_module_free_items`.
This commit is contained in:
Nicholas Nethercote 2025-02-03 10:45:49 +11:00
parent cd1d84cdf7
commit f86f7ad5f2
197 changed files with 465 additions and 476 deletions

View file

@ -627,7 +627,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
fn check_object_lifetime_default(&self, hir_id: HirId) {
let tcx = self.tcx;
if let Some(owner_id) = hir_id.as_owner()
&& let Some(generics) = tcx.hir().get_generics(owner_id.def_id)
&& let Some(generics) = tcx.hir_get_generics(owner_id.def_id)
{
for p in generics.params {
let hir::GenericParamKind::Type { .. } = p.kind else { continue };
@ -2740,9 +2740,8 @@ fn check_invalid_crate_level_attr(tcx: TyCtxt<'_>, attrs: &[Attribute]) {
for attr_to_check in ATTRS_TO_CHECK {
if attr.has_name(*attr_to_check) {
let item = tcx
.hir()
.items()
.map(|id| tcx.hir().item(id))
.hir_free_items()
.map(|id| tcx.hir_item(id))
.find(|item| !item.span.is_dummy()) // Skip prelude `use`s
.map(|item| errors::ItemFollowingInnerAttr {
span: item.ident.span,

View file

@ -531,7 +531,7 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
fn impl_item_with_used_self(&mut self, impl_id: hir::ItemId, impl_item_id: LocalDefId) -> bool {
if let TyKind::Path(hir::QPath::Resolved(_, path)) =
self.tcx.hir().item(impl_id).expect_impl().self_ty.kind
self.tcx.hir_item(impl_id).expect_impl().self_ty.kind
&& let Res::Def(def_kind, def_id) = path.res
&& let Some(local_def_id) = def_id.as_local()
&& matches!(def_kind, DefKind::Struct | DefKind::Enum | DefKind::Union)
@ -559,7 +559,7 @@ impl<'tcx> Visitor<'tcx> for MarkSymbolVisitor<'tcx> {
fn visit_nested_body(&mut self, body: hir::BodyId) {
let old_maybe_typeck_results =
self.maybe_typeck_results.replace(self.tcx.typeck_body(body));
let body = self.tcx.hir().body(body);
let body = self.tcx.hir_body(body);
self.visit_body(body);
self.maybe_typeck_results = old_maybe_typeck_results;
}
@ -750,7 +750,7 @@ fn check_item<'tcx>(
match tcx.def_kind(id.owner_id) {
DefKind::Enum => {
let item = tcx.hir().item(id);
let item = tcx.hir_item(id);
if let hir::ItemKind::Enum(ref enum_def, _) = item.kind {
if let Some(comes_from_allow) = allow_dead_code {
worklist.extend(
@ -772,7 +772,7 @@ fn check_item<'tcx>(
.iter()
.filter_map(|def_id| def_id.as_local());
let ty_is_pub = ty_ref_to_pub_struct(tcx, tcx.hir().item(id).expect_impl().self_ty);
let ty_is_pub = ty_ref_to_pub_struct(tcx, tcx.hir_item(id).expect_impl().self_ty);
// And we access the Map here to get HirId from LocalDefId
for local_def_id in local_def_ids {
@ -805,7 +805,7 @@ fn check_item<'tcx>(
}
}
DefKind::Struct => {
let item = tcx.hir().item(id);
let item = tcx.hir_item(id);
if let hir::ItemKind::Struct(ref variant_data, _) = item.kind
&& let Some(ctor_def_id) = variant_data.ctor_def_id()
{
@ -827,7 +827,7 @@ fn check_trait_item(
) {
use hir::TraitItemKind::{Const, Fn};
if matches!(tcx.def_kind(id.owner_id), DefKind::AssocConst | DefKind::AssocFn) {
let trait_item = tcx.hir().trait_item(id);
let trait_item = tcx.hir_trait_item(id);
if matches!(trait_item.kind, Const(_, Some(_)) | Fn(..))
&& let Some(comes_from_allow) =
has_allow_dead_code_or_lang_attr(tcx, trait_item.owner_id.def_id)

View file

@ -37,7 +37,7 @@ fn entry_fn(tcx: TyCtxt<'_>, (): ()) -> Option<(DefId, EntryFnType)> {
let mut ctxt = EntryContext { tcx, rustc_main_fn: None, non_main_fns: Vec::new() };
for id in tcx.hir().items() {
for id in tcx.hir_free_items() {
check_and_search_item(id, &mut ctxt);
}

View file

@ -61,7 +61,7 @@ impl<'a, 'hir> HirIdValidator<'a, 'hir> {
if max != self.hir_ids_seen.len() - 1 {
let hir = self.tcx.hir();
let pretty_owner = hir.def_path(owner.def_id).to_string_no_crate_verbose();
let pretty_owner = self.tcx.hir_def_path(owner.def_id).to_string_no_crate_verbose();
let missing_items: Vec<_> = (0..=max as u32)
.map(|i| ItemLocalId::from_u32(i))
@ -138,8 +138,8 @@ impl<'a, 'hir> intravisit::Visitor<'hir> for HirIdValidator<'a, 'hir> {
format!(
"HirIdValidator: The recorded owner of {} is {} instead of {}",
self.tcx.hir().node_to_string(hir_id),
self.tcx.hir().def_path(hir_id.owner.def_id).to_string_no_crate_verbose(),
self.tcx.hir().def_path(owner.def_id).to_string_no_crate_verbose()
self.tcx.hir_def_path(hir_id.owner.def_id).to_string_no_crate_verbose(),
self.tcx.hir_def_path(owner.def_id).to_string_no_crate_verbose()
)
});
}

View file

@ -7,7 +7,6 @@ use rustc_ast::{self as ast, NodeId, visit as ast_visit};
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::thousands::format_with_underscores;
use rustc_hir::{self as hir, AmbigArg, HirId, intravisit as hir_visit};
use rustc_middle::hir::map::Map;
use rustc_middle::ty::TyCtxt;
use rustc_span::Span;
use rustc_span::def_id::LocalDefId;
@ -56,17 +55,14 @@ impl Node {
/// a `visit_*` method for, and so we cannot measure these, which is
/// unfortunate.
struct StatCollector<'k> {
krate: Option<Map<'k>>,
tcx: Option<TyCtxt<'k>>,
nodes: FxHashMap<&'static str, Node>,
seen: FxHashSet<HirId>,
}
pub fn print_hir_stats(tcx: TyCtxt<'_>) {
let mut collector = StatCollector {
krate: Some(tcx.hir()),
nodes: FxHashMap::default(),
seen: FxHashSet::default(),
};
let mut collector =
StatCollector { tcx: Some(tcx), nodes: FxHashMap::default(), seen: FxHashSet::default() };
tcx.hir().walk_toplevel_module(&mut collector);
tcx.hir().walk_attributes(&mut collector);
collector.print("HIR STATS", "hir-stats");
@ -76,7 +72,7 @@ pub fn print_ast_stats(krate: &ast::Crate, title: &str, prefix: &str) {
use rustc_ast::visit::Visitor;
let mut collector =
StatCollector { krate: None, nodes: FxHashMap::default(), seen: FxHashSet::default() };
StatCollector { tcx: None, nodes: FxHashMap::default(), seen: FxHashSet::default() };
collector.visit_crate(krate);
collector.print(title, prefix);
}
@ -205,27 +201,27 @@ impl<'v> hir_visit::Visitor<'v> for StatCollector<'v> {
}
fn visit_nested_item(&mut self, id: hir::ItemId) {
let nested_item = self.krate.unwrap().item(id);
let nested_item = self.tcx.unwrap().hir_item(id);
self.visit_item(nested_item)
}
fn visit_nested_trait_item(&mut self, trait_item_id: hir::TraitItemId) {
let nested_trait_item = self.krate.unwrap().trait_item(trait_item_id);
let nested_trait_item = self.tcx.unwrap().hir_trait_item(trait_item_id);
self.visit_trait_item(nested_trait_item)
}
fn visit_nested_impl_item(&mut self, impl_item_id: hir::ImplItemId) {
let nested_impl_item = self.krate.unwrap().impl_item(impl_item_id);
let nested_impl_item = self.tcx.unwrap().hir_impl_item(impl_item_id);
self.visit_impl_item(nested_impl_item)
}
fn visit_nested_foreign_item(&mut self, id: hir::ForeignItemId) {
let nested_foreign_item = self.krate.unwrap().foreign_item(id);
let nested_foreign_item = self.tcx.unwrap().hir_foreign_item(id);
self.visit_foreign_item(nested_foreign_item);
}
fn visit_nested_body(&mut self, body_id: hir::BodyId) {
let nested_body = self.krate.unwrap().body(body_id);
let nested_body = self.tcx.unwrap().hir_body(body_id);
self.visit_body(nested_body)
}

View file

@ -45,7 +45,7 @@ fn check_mod_naked_functions(tcx: TyCtxt<'_>, module_def_id: LocalModDefId) {
_ => continue,
};
let body = tcx.hir().body(body_id);
let body = tcx.hir_body(body_id);
if tcx.has_attr(def_id, sym::naked) {
check_abi(tcx, def_id, fn_header.abi);

View file

@ -66,7 +66,7 @@ impl<'tcx> Visitor<'tcx> for ReachableContext<'tcx> {
fn visit_nested_body(&mut self, body: hir::BodyId) {
let old_maybe_typeck_results =
self.maybe_typeck_results.replace(self.tcx.typeck_body(body));
let body = self.tcx.hir().body(body);
let body = self.tcx.hir_body(body);
self.visit_body(body);
self.maybe_typeck_results = old_maybe_typeck_results;
}