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

@ -2026,7 +2026,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
}
LetVisitor { span }.visit_body(body).break_value()
}
hir::Node::Item(hir::Item { kind: hir::ItemKind::Const(ty, _, _), .. }) => {
hir::Node::Item(hir::Item { kind: hir::ItemKind::Const(_, ty, _, _), .. }) => {
Some(&ty.peel_refs().kind)
}
_ => None,

View file

@ -338,7 +338,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
..
},
hir::PathSegment {
ident: assoc_item_name,
ident: assoc_item_ident,
res: Res::Def(_, item_id),
..
},
@ -368,17 +368,16 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
if let Some(local_def_id) = data.trait_ref.def_id.as_local()
&& let hir::Node::Item(hir::Item {
ident: trait_name,
kind: hir::ItemKind::Trait(_, _, _, _, trait_item_refs),
kind: hir::ItemKind::Trait(_, _, trait_ident, _, _, trait_item_refs),
..
}) = self.tcx.hir_node_by_def_id(local_def_id)
&& let Some(method_ref) = trait_item_refs
.iter()
.find(|item_ref| item_ref.ident == *assoc_item_name)
.find(|item_ref| item_ref.ident == *assoc_item_ident)
{
err.span_label(
method_ref.span,
format!("`{trait_name}::{assoc_item_name}` defined here"),
format!("`{trait_ident}::{assoc_item_ident}` defined here"),
);
}

View file

@ -419,7 +419,12 @@ pub fn report_dyn_incompatibility<'tcx>(
) -> Diag<'tcx> {
let trait_str = tcx.def_path_str(trait_def_id);
let trait_span = tcx.hir_get_if_local(trait_def_id).and_then(|node| match node {
hir::Node::Item(item) => Some(item.ident.span),
hir::Node::Item(item) => match item.kind {
hir::ItemKind::Trait(_, _, ident, ..) | hir::ItemKind::TraitAlias(ident, _, _) => {
Some(ident.span)
}
_ => unreachable!(),
},
_ => None,
});

View file

@ -267,8 +267,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
let node = self.tcx.hir_node_by_def_id(body_id);
match node {
hir::Node::Item(hir::Item {
ident,
kind: hir::ItemKind::Trait(_, _, generics, bounds, _),
kind: hir::ItemKind::Trait(_, _, ident, generics, bounds, _),
..
}) if self_ty == self.tcx.types.self_param => {
assert!(param_ty);
@ -282,7 +281,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
None,
projection,
trait_pred,
Some((ident, bounds)),
Some((&ident, bounds)),
);
return;
}
@ -331,7 +330,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
}
hir::Node::Item(hir::Item {
kind:
hir::ItemKind::Trait(_, _, generics, ..)
hir::ItemKind::Trait(_, _, _, generics, ..)
| hir::ItemKind::Impl(hir::Impl { generics, .. }),
..
}) if projection.is_some() => {
@ -352,15 +351,15 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
hir::Node::Item(hir::Item {
kind:
hir::ItemKind::Struct(_, generics)
| hir::ItemKind::Enum(_, generics)
| hir::ItemKind::Union(_, generics)
| hir::ItemKind::Trait(_, _, generics, ..)
hir::ItemKind::Struct(_, _, generics)
| hir::ItemKind::Enum(_, _, generics)
| hir::ItemKind::Union(_, _, generics)
| hir::ItemKind::Trait(_, _, _, generics, ..)
| hir::ItemKind::Impl(hir::Impl { generics, .. })
| hir::ItemKind::Fn { generics, .. }
| hir::ItemKind::TyAlias(_, generics)
| hir::ItemKind::Const(_, generics, _)
| hir::ItemKind::TraitAlias(generics, _),
| hir::ItemKind::TyAlias(_, _, generics)
| hir::ItemKind::Const(_, _, generics, _)
| hir::ItemKind::TraitAlias(_, generics, _),
..
})
| hir::Node::TraitItem(hir::TraitItem { generics, .. })
@ -412,15 +411,15 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
hir::Node::Item(hir::Item {
kind:
hir::ItemKind::Struct(_, generics)
| hir::ItemKind::Enum(_, generics)
| hir::ItemKind::Union(_, generics)
| hir::ItemKind::Trait(_, _, generics, ..)
hir::ItemKind::Struct(_, _, generics)
| hir::ItemKind::Enum(_, _, generics)
| hir::ItemKind::Union(_, _, generics)
| hir::ItemKind::Trait(_, _, _, generics, ..)
| hir::ItemKind::Impl(hir::Impl { generics, .. })
| hir::ItemKind::Fn { generics, .. }
| hir::ItemKind::TyAlias(_, generics)
| hir::ItemKind::Const(_, generics, _)
| hir::ItemKind::TraitAlias(generics, _),
| hir::ItemKind::TyAlias(_, _, generics)
| hir::ItemKind::Const(_, _, generics, _)
| hir::ItemKind::TraitAlias(_, generics, _),
..
}) if !param_ty => {
// Missing generic type parameter bound.
@ -842,7 +841,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
name.to_string()
}
Some(hir::Node::Item(hir::Item {
ident, kind: hir::ItemKind::Fn { .. }, ..
kind: hir::ItemKind::Fn { ident, .. }, ..
})) => {
err.span_label(ident.span, "consider calling this function");
ident.to_string()
@ -1588,20 +1587,20 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
if let Some(typeck_results) = &self.typeck_results
&& let ty = typeck_results.expr_ty_adjusted(base)
&& let ty::FnDef(def_id, _args) = ty.kind()
&& let Some(hir::Node::Item(hir::Item { ident, span, vis_span, .. })) =
self.tcx.hir_get_if_local(*def_id)
&& let Some(hir::Node::Item(item)) = self.tcx.hir_get_if_local(*def_id)
{
let (ident, _, _, _) = item.expect_fn();
let msg = format!("alternatively, consider making `fn {ident}` asynchronous");
if vis_span.is_empty() {
if item.vis_span.is_empty() {
err.span_suggestion_verbose(
span.shrink_to_lo(),
item.span.shrink_to_lo(),
msg,
"async ",
Applicability::MaybeIncorrect,
);
} else {
err.span_suggestion_verbose(
vis_span.shrink_to_hi(),
item.vis_span.shrink_to_hi(),
msg,
" async",
Applicability::MaybeIncorrect,
@ -3303,8 +3302,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
let mut is_auto_trait = false;
match tcx.hir_get_if_local(data.impl_or_alias_def_id) {
Some(Node::Item(hir::Item {
kind: hir::ItemKind::Trait(is_auto, ..),
ident,
kind: hir::ItemKind::Trait(is_auto, _, ident, ..),
..
})) => {
// FIXME: we should do something else so that it works even on crate foreign

View file

@ -516,7 +516,7 @@ impl Subdiagnostic for AddLifetimeParamsSuggestion<'_> {
match self.tcx.parent_hir_node(self.tcx.local_def_id_to_hir_id(anon_reg.scope))
{
hir::Node::Item(hir::Item {
kind: hir::ItemKind::Trait(_, _, generics, ..),
kind: hir::ItemKind::Trait(_, _, _, generics, ..),
..
})
| hir::Node::Item(hir::Item {