1
Fork 0

Rollup merge of #138384 - nnethercote:hir-ItemKind-idents, r=fmease

Move `hir::Item::ident` into `hir::ItemKind`.

 `hir::Item` has an `ident` field.

- It's always non-empty for these item kinds: `ExternCrate`, `Static`, `Const`, `Fn`, `Macro`, `Mod`, `TyAlias`, `Enum`, `Struct`, `Union`, Trait`, TraitAalis`.

- It's always empty for these item kinds: `ForeignMod`, `GlobalAsm`, `Impl`.

- For `Use`, it is non-empty for `UseKind::Single` and empty for `UseKind::{Glob,ListStem}`.

All of this is quite non-obvious; the only documentation is a single comment saying "The name might be a dummy name in case of anonymous items". Some sites that handle items check for an empty ident, some don't. This is a very C-like way of doing things, but this is Rust, we have sum types, we can do this properly and never forget to check for the exceptional case and never YOLO possibly empty identifiers (or possibly dummy spans) around and hope that things will work out.

This is step towards `kw::Empty` elimination (#137978).

r? `@fmease`
This commit is contained in:
Matthias Krüger 2025-03-17 22:49:04 +01:00 committed by GitHub
commit e1acc68c9d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
83 changed files with 667 additions and 556 deletions

View file

@ -385,7 +385,7 @@ impl<'tcx> TyCtxt<'tcx> {
pub fn hir_get_module(self, module: LocalModDefId) -> (&'tcx Mod<'tcx>, Span, HirId) {
let hir_id = HirId::make_owner(module.to_local_def_id());
match self.hir_owner_node(hir_id.owner) {
OwnerNode::Item(&Item { span, kind: ItemKind::Mod(m), .. }) => (m, span, hir_id),
OwnerNode::Item(&Item { span, kind: ItemKind::Mod(_, m), .. }) => (m, span, hir_id),
OwnerNode::Crate(item) => (item, item.spans.inner_span, hir_id),
node => panic!("not a module: {node:?}"),
}
@ -847,7 +847,7 @@ impl<'tcx> TyCtxt<'tcx> {
// A `Ctor` doesn't have an identifier itself, but its parent
// struct/variant does. Compare with `hir::Map::span`.
Node::Ctor(..) => match self.parent_hir_node(id) {
Node::Item(item) => Some(item.ident),
Node::Item(item) => Some(item.kind.ident().unwrap()),
Node::Variant(variant) => Some(variant.ident),
_ => unreachable!(),
},
@ -894,18 +894,14 @@ impl<'hir> Map<'hir> {
}
fn named_span(item_span: Span, ident: Ident, generics: Option<&Generics<'_>>) -> Span {
if ident.name != kw::Empty {
let mut span = until_within(item_span, ident.span);
if let Some(g) = generics
&& !g.span.is_dummy()
&& let Some(g_span) = g.span.find_ancestor_inside(item_span)
{
span = span.to(g_span);
}
span
} else {
item_span
let mut span = until_within(item_span, ident.span);
if let Some(g) = generics
&& !g.span.is_dummy()
&& let Some(g_span) = g.span.find_ancestor_inside(item_span)
{
span = span.to(g_span);
}
span
}
let span = match self.tcx.hir_node(hir_id) {
@ -936,7 +932,7 @@ impl<'hir> Map<'hir> {
}) => until_within(*outer_span, generics.where_clause_span),
// Constants and Statics.
Node::Item(Item {
kind: ItemKind::Const(ty, ..) | ItemKind::Static(ty, ..),
kind: ItemKind::Const(_, ty, ..) | ItemKind::Static(_, ty, ..),
span: outer_span,
..
})
@ -957,7 +953,7 @@ impl<'hir> Map<'hir> {
}) => until_within(*outer_span, ty.span),
// With generics and bounds.
Node::Item(Item {
kind: ItemKind::Trait(_, _, generics, bounds, _),
kind: ItemKind::Trait(_, _, _, generics, bounds, _),
span: outer_span,
..
})
@ -977,7 +973,13 @@ impl<'hir> Map<'hir> {
// SyntaxContext of the path.
path.span.find_ancestor_in_same_ctxt(item.span).unwrap_or(item.span)
}
_ => named_span(item.span, item.ident, item.kind.generics()),
_ => {
if let Some(ident) = item.kind.ident() {
named_span(item.span, ident, item.kind.generics())
} else {
item.span
}
}
},
Node::Variant(variant) => named_span(variant.span, variant.ident, None),
Node::ImplItem(item) => named_span(item.span, item.ident, Some(item.generics)),
@ -1327,7 +1329,7 @@ impl<'hir> Visitor<'hir> for ItemCollector<'hir> {
self.items.push(item.item_id());
// Items that are modules are handled here instead of in visit_mod.
if let ItemKind::Mod(module) = &item.kind {
if let ItemKind::Mod(_, module) = &item.kind {
self.submodules.push(item.owner_id);
// A module collector does not recurse inside nested modules.
if self.crate_collector {

View file

@ -3406,20 +3406,18 @@ define_print_and_forward_display! {
}
fn for_each_def(tcx: TyCtxt<'_>, mut collect_fn: impl for<'b> FnMut(&'b Ident, Namespace, DefId)) {
// Iterate all local crate items no matter where they are defined.
// Iterate all (non-anonymous) local crate items no matter where they are defined.
for id in tcx.hir_free_items() {
if matches!(tcx.def_kind(id.owner_id), DefKind::Use) {
continue;
}
let item = tcx.hir_item(id);
if item.ident.name == kw::Empty {
continue;
}
let Some(ident) = item.kind.ident() else { continue };
let def_id = item.owner_id.to_def_id();
let ns = tcx.def_kind(def_id).ns().unwrap_or(Namespace::TypeNS);
collect_fn(&item.ident, ns, def_id);
collect_fn(&ident, ns, def_id);
}
// Now take care of extern crate items.