diff options
| author | Matthias Krüger <476013+matthiaskrgr@users.noreply.github.com> | 2025-03-17 22:49:04 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-03-17 22:49:04 +0100 |
| commit | e1acc68c9dcbeea515948e5ed40cd8131c1ef793 (patch) | |
| tree | 4024acc2ac05070d8a75fc56ca663cbe5dc81910 /compiler/rustc_passes/src | |
| parent | 8279176ccdfd4eebd40a671f75b6d3024ae56b42 (diff) | |
| parent | f2ddbcd24b3e71a2c919721f854043f1bb81ff79 (diff) | |
| download | rust-e1acc68c9dcbeea515948e5ed40cd8131c1ef793.tar.gz rust-e1acc68c9dcbeea515948e5ed40cd8131c1ef793.zip | |
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`
Diffstat (limited to 'compiler/rustc_passes/src')
| -rw-r--r-- | compiler/rustc_passes/src/check_attr.rs | 16 | ||||
| -rw-r--r-- | compiler/rustc_passes/src/dead.rs | 8 | ||||
| -rw-r--r-- | compiler/rustc_passes/src/reachable.rs | 4 | ||||
| -rw-r--r-- | compiler/rustc_passes/src/stability.rs | 6 |
4 files changed, 17 insertions, 17 deletions
diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index ece5a53aaa9..6b76b19103c 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -743,7 +743,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> { match target { Target::Struct => { if let Some(ItemLike::Item(hir::Item { - kind: hir::ItemKind::Struct(hir::VariantData::Struct { fields, .. }, _), + kind: hir::ItemKind::Struct(_, hir::VariantData::Struct { fields, .. }, _), .. })) = item && !fields.is_empty() @@ -1019,7 +1019,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> { _ => None, }; match item_kind { - Some(ItemKind::Mod(module)) => { + Some(ItemKind::Mod(_, module)) => { if !module.item_ids.is_empty() { self.dcx().emit_err(errors::DocKeywordEmptyMod { span: meta.span() }); return; @@ -1072,9 +1072,9 @@ impl<'tcx> CheckAttrVisitor<'tcx> { return; }; match item.kind { - ItemKind::Enum(_, generics) | ItemKind::Struct(_, generics) + ItemKind::Enum(_, _, generics) | ItemKind::Struct(_, _, generics) if generics.params.len() != 0 => {} - ItemKind::Trait(_, _, generics, _, items) + ItemKind::Trait(_, _, _, generics, _, items) if generics.params.len() != 0 || items.iter().any(|item| matches!(item.kind, AssocItemKind::Type)) => {} _ => { @@ -2293,7 +2293,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> { } } else { // special case when `#[macro_export]` is applied to a macro 2.0 - let (macro_definition, _) = self.tcx.hir_node(hir_id).expect_item().expect_macro(); + let (_, macro_definition, _) = self.tcx.hir_node(hir_id).expect_item().expect_macro(); let is_decl_macro = !macro_definition.macro_rules; if is_decl_macro { @@ -2624,7 +2624,7 @@ impl<'tcx> Visitor<'tcx> for CheckAttrVisitor<'tcx> { // Historically we've run more checks on non-exported than exported macros, // so this lets us continue to run them while maintaining backwards compatibility. // In the long run, the checks should be harmonized. - if let ItemKind::Macro(macro_def, _) = item.kind { + if let ItemKind::Macro(_, macro_def, _) = item.kind { let def_id = item.owner_id.to_def_id(); if macro_def.macro_rules && !self.tcx.has_attr(def_id, sym::macro_export) { check_non_exported_macro_for_invalid_attrs(self.tcx, item); @@ -2736,7 +2736,7 @@ impl<'tcx> Visitor<'tcx> for CheckAttrVisitor<'tcx> { } fn is_c_like_enum(item: &Item<'_>) -> bool { - if let ItemKind::Enum(ref def, _) = item.kind { + if let ItemKind::Enum(_, ref def, _) = item.kind { for variant in def.variants { match variant.data { hir::VariantData::Unit(..) => { /* continue */ } @@ -2784,7 +2784,7 @@ fn check_invalid_crate_level_attr(tcx: TyCtxt<'_>, attrs: &[Attribute]) { .map(|id| tcx.hir_item(id)) .find(|item| !item.span.is_dummy()) // Skip prelude `use`s .map(|item| errors::ItemFollowingInnerAttr { - span: item.ident.span, + span: if let Some(ident) = item.kind.ident() { ident.span } else { item.span }, kind: item.kind.descr(), }); let err = tcx.dcx().create_err(errors::InvalidAttrAtCrateLevel { diff --git a/compiler/rustc_passes/src/dead.rs b/compiler/rustc_passes/src/dead.rs index d036cb74a56..f77e1db42d4 100644 --- a/compiler/rustc_passes/src/dead.rs +++ b/compiler/rustc_passes/src/dead.rs @@ -751,7 +751,7 @@ fn check_item<'tcx>( match tcx.def_kind(id.owner_id) { DefKind::Enum => { let item = tcx.hir_item(id); - if let hir::ItemKind::Enum(ref enum_def, _) = item.kind { + if let hir::ItemKind::Enum(_, ref enum_def, _) = item.kind { if let Some(comes_from_allow) = allow_dead_code { worklist.extend( enum_def.variants.iter().map(|variant| (variant.def_id, comes_from_allow)), @@ -806,7 +806,7 @@ fn check_item<'tcx>( } DefKind::Struct => { let item = tcx.hir_item(id); - if let hir::ItemKind::Struct(ref variant_data, _) = item.kind + if let hir::ItemKind::Struct(_, ref variant_data, _) = item.kind && let Some(ctor_def_id) = variant_data.ctor_def_id() { struct_constructors.insert(ctor_def_id, item.owner_id.def_id); @@ -987,7 +987,7 @@ impl<'tcx> DeadVisitor<'tcx> { && let Some(enum_did) = tcx.opt_parent(may_variant.to_def_id()) && let Some(enum_local_id) = enum_did.as_local() && let Node::Item(item) = tcx.hir_node_by_def_id(enum_local_id) - && let ItemKind::Enum(_, _) = item.kind + && let ItemKind::Enum(..) = item.kind { enum_local_id } else { @@ -1062,7 +1062,7 @@ impl<'tcx> DeadVisitor<'tcx> { let tuple_fields = if let Some(parent_id) = parent_item && let node = tcx.hir_node_by_def_id(parent_id) && let hir::Node::Item(hir::Item { - kind: hir::ItemKind::Struct(hir::VariantData::Tuple(fields, _, _), _), + kind: hir::ItemKind::Struct(_, hir::VariantData::Tuple(fields, _, _), _), .. }) = node { diff --git a/compiler/rustc_passes/src/reachable.rs b/compiler/rustc_passes/src/reachable.rs index 599a08bac20..f65c1f40bed 100644 --- a/compiler/rustc_passes/src/reachable.rs +++ b/compiler/rustc_passes/src/reachable.rs @@ -206,7 +206,7 @@ impl<'tcx> ReachableContext<'tcx> { } } - hir::ItemKind::Const(_, _, init) => { + hir::ItemKind::Const(_, _, _, init) => { // Only things actually ending up in the final constant value are reachable // for codegen. Everything else is only needed during const-eval, so even if // const-eval happens in a downstream crate, all they need is @@ -234,7 +234,7 @@ impl<'tcx> ReachableContext<'tcx> { // These are normal, nothing reachable about these // inherently and their children are already in the // worklist, as determined by the privacy pass - hir::ItemKind::ExternCrate(_) + hir::ItemKind::ExternCrate(..) | hir::ItemKind::Use(..) | hir::ItemKind::TyAlias(..) | hir::ItemKind::Macro(..) diff --git a/compiler/rustc_passes/src/stability.rs b/compiler/rustc_passes/src/stability.rs index aea4386295f..9e55914f8f2 100644 --- a/compiler/rustc_passes/src/stability.rs +++ b/compiler/rustc_passes/src/stability.rs @@ -430,7 +430,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> { kind = AnnotationKind::DeprecationProhibited; const_stab_inherit = InheritConstStability::Yes; } - hir::ItemKind::Struct(ref sd, _) => { + hir::ItemKind::Struct(_, ref sd, _) => { if let Some(ctor_def_id) = sd.ctor_def_id() { self.annotate( ctor_def_id, @@ -773,10 +773,10 @@ impl<'tcx> Visitor<'tcx> for Checker<'tcx> { fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) { match item.kind { - hir::ItemKind::ExternCrate(_) => { + hir::ItemKind::ExternCrate(_, ident) => { // compiler-generated `extern crate` items have a dummy span. // `std` is still checked for the `restricted-std` feature. - if item.span.is_dummy() && item.ident.name != sym::std { + if item.span.is_dummy() && ident.name != sym::std { return; } |
