1
Fork 0

Rollup merge of #139232 - nnethercote:remove-Map-5, r=Zalathar

Move methods from `Map` to `TyCtxt`, part 5.

This eliminates all methods on `Map`. Actually removing `Map` will occur in a follow-up PR.

A follow-up to #137504.

r? `@Zalathar`
This commit is contained in:
Takayuki Maeda 2025-04-02 22:52:46 +09:00 committed by GitHub
commit bda2ea4d01
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
75 changed files with 175 additions and 208 deletions

View file

@ -22,6 +22,7 @@ use crate::ty::TyCtxt;
// only serves as "namespace" for HIR-related methods, and can be
// removed if all the methods are reasonably renamed and moved to tcx
// (https://github.com/rust-lang/rust/pull/118256#issuecomment-1826442834).
#[allow(unused)] // FIXME: temporary
#[derive(Copy, Clone)]
pub struct Map<'hir> {
pub(super) tcx: TyCtxt<'hir>,
@ -273,7 +274,7 @@ impl<'tcx> TyCtxt<'tcx> {
self.hir_maybe_body_owned_by(id).unwrap_or_else(|| {
let hir_id = self.local_def_id_to_hir_id(id);
span_bug!(
self.hir().span(hir_id),
self.hir_span(hir_id),
"body_owned_by: {} has no associated body",
self.hir_id_to_string(hir_id)
);
@ -677,9 +678,8 @@ impl<'tcx> TyCtxt<'tcx> {
pub fn hir_id_to_string(self, id: HirId) -> String {
let path_str = |def_id: LocalDefId| self.def_path_str(def_id);
let span_str = || {
self.sess.source_map().span_to_snippet(Map { tcx: self }.span(id)).unwrap_or_default()
};
let span_str =
|| self.sess.source_map().span_to_snippet(self.hir_span(id)).unwrap_or_default();
let node_str = |prefix| format!("{id} ({prefix} `{}`)", span_str());
match self.hir_node(id) {
@ -875,12 +875,10 @@ impl<'tcx> TyCtxt<'tcx> {
pub fn hir_attrs(self, id: HirId) -> &'tcx [Attribute] {
self.hir_attr_map(id.owner).get(id.local_id)
}
}
impl<'hir> Map<'hir> {
/// Gets the span of the definition of the specified HIR node.
/// This is used by `tcx.def_span`.
pub fn span(self, hir_id: HirId) -> Span {
pub fn hir_span(self, hir_id: HirId) -> Span {
fn until_within(outer: Span, end: Span) -> Span {
if let Some(end) = end.find_ancestor_inside(outer) {
outer.with_hi(end.hi())
@ -900,7 +898,7 @@ impl<'hir> Map<'hir> {
span
}
let span = match self.tcx.hir_node(hir_id) {
let span = match self.hir_node(hir_id) {
// Function-like.
Node::Item(Item { kind: ItemKind::Fn { sig, .. }, span: outer_span, .. })
| Node::TraitItem(TraitItem {
@ -980,7 +978,7 @@ impl<'hir> Map<'hir> {
Node::Variant(variant) => named_span(variant.span, variant.ident, None),
Node::ImplItem(item) => named_span(item.span, item.ident, Some(item.generics)),
Node::ForeignItem(item) => named_span(item.span, item.ident, None),
Node::Ctor(_) => return self.span(self.tcx.parent_hir_id(hir_id)),
Node::Ctor(_) => return self.hir_span(self.parent_hir_id(hir_id)),
Node::Expr(Expr {
kind: ExprKind::Closure(Closure { fn_decl_span, .. }),
span,
@ -989,16 +987,16 @@ impl<'hir> Map<'hir> {
// Ensure that the returned span has the item's SyntaxContext.
fn_decl_span.find_ancestor_inside(*span).unwrap_or(*span)
}
_ => self.span_with_body(hir_id),
_ => self.hir_span_with_body(hir_id),
};
debug_assert_eq!(span.ctxt(), self.span_with_body(hir_id).ctxt());
debug_assert_eq!(span.ctxt(), self.hir_span_with_body(hir_id).ctxt());
span
}
/// Like `hir.span()`, but includes the body of items
/// Like `hir_span()`, but includes the body of items
/// (instead of just the item header)
pub fn span_with_body(self, hir_id: HirId) -> Span {
match self.tcx.hir_node(hir_id) {
pub fn hir_span_with_body(self, hir_id: HirId) -> Span {
match self.hir_node(hir_id) {
Node::Param(param) => param.span,
Node::Item(item) => item.span,
Node::ForeignItem(foreign_item) => foreign_item.span,
@ -1007,7 +1005,7 @@ impl<'hir> Map<'hir> {
Node::Variant(variant) => variant.span,
Node::Field(field) => field.span,
Node::AnonConst(constant) => constant.span,
Node::ConstBlock(constant) => self.tcx.hir_body(constant.body).value.span,
Node::ConstBlock(constant) => self.hir_body(constant.body).value.span,
Node::ConstArg(const_arg) => const_arg.span(),
Node::Expr(expr) => expr.span,
Node::ExprField(field) => field.span,
@ -1027,7 +1025,7 @@ impl<'hir> Map<'hir> {
Node::PatExpr(lit) => lit.span,
Node::Arm(arm) => arm.span,
Node::Block(block) => block.span,
Node::Ctor(..) => self.span_with_body(self.tcx.parent_hir_id(hir_id)),
Node::Ctor(..) => self.hir_span_with_body(self.parent_hir_id(hir_id)),
Node::Lifetime(lifetime) => lifetime.ident.span,
Node::GenericParam(param) => param.span,
Node::Infer(i) => i.span,
@ -1040,23 +1038,23 @@ impl<'hir> Map<'hir> {
}
}
pub fn span_if_local(self, id: DefId) -> Option<Span> {
id.is_local().then(|| self.tcx.def_span(id))
pub fn hir_span_if_local(self, id: DefId) -> Option<Span> {
id.is_local().then(|| self.def_span(id))
}
pub fn res_span(self, res: Res) -> Option<Span> {
pub fn hir_res_span(self, res: Res) -> Option<Span> {
match res {
Res::Err => None,
Res::Local(id) => Some(self.span(id)),
res => self.span_if_local(res.opt_def_id()?),
Res::Local(id) => Some(self.hir_span(id)),
res => self.hir_span_if_local(res.opt_def_id()?),
}
}
/// Returns the HirId of `N` in `struct Foo<const N: usize = { ... }>` when
/// called with the HirId for the `{ ... }` anon const
pub fn opt_const_param_default_param_def_id(self, anon_const: HirId) -> Option<LocalDefId> {
let const_arg = self.tcx.parent_hir_id(anon_const);
match self.tcx.parent_hir_node(const_arg) {
pub fn hir_opt_const_param_default_param_def_id(self, anon_const: HirId) -> Option<LocalDefId> {
let const_arg = self.parent_hir_id(anon_const);
match self.parent_hir_node(const_arg) {
Node::GenericParam(GenericParam {
def_id: param_id,
kind: GenericParamKind::Const { .. },
@ -1066,7 +1064,7 @@ impl<'hir> Map<'hir> {
}
}
pub fn maybe_get_struct_pattern_shorthand_field(&self, expr: &Expr<'_>) -> Option<Symbol> {
pub fn hir_maybe_get_struct_pattern_shorthand_field(self, expr: &Expr<'_>) -> Option<Symbol> {
let local = match expr {
Expr {
kind:
@ -1081,7 +1079,7 @@ impl<'hir> Map<'hir> {
_ => None,
}?;
match self.tcx.parent_hir_node(expr.hir_id) {
match self.parent_hir_node(expr.hir_id) {
Node::ExprField(field) => {
if field.ident.name == local.name && field.is_shorthand {
return Some(local.name);

View file

@ -210,13 +210,12 @@ pub fn provide(providers: &mut Providers) {
providers.hir_attr_map = |tcx, id| {
tcx.hir_crate(()).owners[id.def_id].as_owner().map_or(AttributeMap::EMPTY, |o| &o.attrs)
};
providers.def_span = |tcx, def_id| tcx.hir().span(tcx.local_def_id_to_hir_id(def_id));
providers.def_span = |tcx, def_id| tcx.hir_span(tcx.local_def_id_to_hir_id(def_id));
providers.def_ident_span = |tcx, def_id| {
let hir_id = tcx.local_def_id_to_hir_id(def_id);
tcx.hir_opt_ident_span(hir_id)
};
providers.fn_arg_names = |tcx, def_id| {
let hir = tcx.hir();
if let Some(body_id) = tcx.hir_node_by_def_id(def_id).body_id() {
tcx.arena.alloc_from_iter(tcx.hir_body_param_names(body_id))
} else if let Node::TraitItem(&TraitItem {
@ -231,7 +230,7 @@ pub fn provide(providers: &mut Providers) {
idents
} else {
span_bug!(
hir.span(tcx.local_def_id_to_hir_id(def_id)),
tcx.hir_span(tcx.local_def_id_to_hir_id(def_id)),
"fn_arg_names: unexpected item {:?}",
def_id
);

View file

@ -175,7 +175,7 @@ impl Scope {
let Some(hir_id) = self.hir_id(scope_tree) else {
return DUMMY_SP;
};
let span = tcx.hir().span(hir_id);
let span = tcx.hir_span(hir_id);
if let ScopeData::Remainder(first_statement_index) = self.data {
if let Node::Block(blk) = tcx.hir_node(hir_id) {
// Want span for scope starting after the

View file

@ -586,7 +586,7 @@ impl Key for HirId {
type Cache<V> = DefaultCache<Self, V>;
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
tcx.hir().span(*self)
tcx.hir_span(*self)
}
#[inline(always)]
@ -599,7 +599,7 @@ impl Key for (LocalDefId, HirId) {
type Cache<V> = DefaultCache<Self, V>;
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
tcx.hir().span(self.1)
tcx.hir_span(self.1)
}
#[inline(always)]

View file

@ -150,9 +150,9 @@ impl<'tcx> CapturedPlace<'tcx> {
/// Return span pointing to use that resulted in selecting the captured path
pub fn get_path_span(&self, tcx: TyCtxt<'tcx>) -> Span {
if let Some(path_expr_id) = self.info.path_expr_id {
tcx.hir().span(path_expr_id)
tcx.hir_span(path_expr_id)
} else if let Some(capture_kind_expr_id) = self.info.capture_kind_expr_id {
tcx.hir().span(capture_kind_expr_id)
tcx.hir_span(capture_kind_expr_id)
} else {
// Fallback on upvars mentioned if neither path or capture expr id is captured
@ -166,9 +166,9 @@ impl<'tcx> CapturedPlace<'tcx> {
/// Return span pointing to use that resulted in selecting the current capture kind
pub fn get_capture_kind_span(&self, tcx: TyCtxt<'tcx>) -> Span {
if let Some(capture_kind_expr_id) = self.info.capture_kind_expr_id {
tcx.hir().span(capture_kind_expr_id)
tcx.hir_span(capture_kind_expr_id)
} else if let Some(path_expr_id) = self.info.path_expr_id {
tcx.hir().span(path_expr_id)
tcx.hir_span(path_expr_id)
} else {
// Fallback on upvars mentioned if neither path or capture expr id is captured

View file

@ -720,7 +720,7 @@ impl<'tcx> Instance<'tcx> {
ty::TypingEnv::fully_monomorphized(),
def_id,
args,
ty.ty_adt_def().and_then(|adt| tcx.hir().span_if_local(adt.did())).unwrap_or(DUMMY_SP),
ty.ty_adt_def().and_then(|adt| tcx.hir_span_if_local(adt.did())).unwrap_or(DUMMY_SP),
)
}
@ -732,7 +732,7 @@ impl<'tcx> Instance<'tcx> {
ty::TypingEnv::fully_monomorphized(),
def_id,
args,
ty.ty_adt_def().and_then(|adt| tcx.hir().span_if_local(adt.did())).unwrap_or(DUMMY_SP),
ty.ty_adt_def().and_then(|adt| tcx.hir_span_if_local(adt.did())).unwrap_or(DUMMY_SP),
)
}