Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move ast::Item::ident into ast::ItemKind #138740

Merged
merged 6 commits into from
Apr 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 79 additions & 28 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3303,9 +3303,6 @@ pub struct Item<K = ItemKind> {
pub id: NodeId,
pub span: Span,
pub vis: Visibility,
/// The name of the item.
/// It might be a dummy name in case of anonymous items.
pub ident: Ident,

pub kind: K,

Expand All @@ -3327,23 +3324,23 @@ impl Item {

pub fn opt_generics(&self) -> Option<&Generics> {
match &self.kind {
ItemKind::ExternCrate(_)
ItemKind::ExternCrate(..)
| ItemKind::Use(_)
| ItemKind::Mod(_, _)
| ItemKind::Mod(..)
| ItemKind::ForeignMod(_)
| ItemKind::GlobalAsm(_)
| ItemKind::MacCall(_)
| ItemKind::Delegation(_)
| ItemKind::DelegationMac(_)
| ItemKind::MacroDef(_) => None,
| ItemKind::MacroDef(..) => None,
ItemKind::Static(_) => None,
ItemKind::Const(i) => Some(&i.generics),
ItemKind::Fn(i) => Some(&i.generics),
ItemKind::TyAlias(i) => Some(&i.generics),
ItemKind::TraitAlias(generics, _)
| ItemKind::Enum(_, generics)
| ItemKind::Struct(_, generics)
| ItemKind::Union(_, generics) => Some(&generics),
ItemKind::TraitAlias(_, generics, _)
| ItemKind::Enum(_, _, generics)
| ItemKind::Struct(_, _, generics)
| ItemKind::Union(_, _, generics) => Some(&generics),
ItemKind::Trait(i) => Some(&i.generics),
ItemKind::Impl(i) => Some(&i.generics),
}
Expand Down Expand Up @@ -3420,6 +3417,7 @@ impl Default for FnHeader {
pub struct Trait {
pub safety: Safety,
pub is_auto: IsAuto,
pub ident: Ident,
pub generics: Generics,
pub bounds: GenericBounds,
pub items: ThinVec<P<AssocItem>>,
Expand Down Expand Up @@ -3465,6 +3463,7 @@ pub struct TyAliasWhereClauses {
#[derive(Clone, Encodable, Decodable, Debug)]
pub struct TyAlias {
pub defaultness: Defaultness,
pub ident: Ident,
pub generics: Generics,
pub where_clauses: TyAliasWhereClauses,
pub bounds: GenericBounds,
Expand Down Expand Up @@ -3493,6 +3492,7 @@ pub struct FnContract {
#[derive(Clone, Encodable, Decodable, Debug)]
pub struct Fn {
pub defaultness: Defaultness,
pub ident: Ident,
pub generics: Generics,
pub sig: FnSig,
pub contract: Option<P<FnContract>>,
Expand All @@ -3506,6 +3506,7 @@ pub struct Delegation {
pub id: NodeId,
pub qself: Option<P<QSelf>>,
pub path: Path,
pub ident: Ident,
pub rename: Option<Ident>,
pub body: Option<P<Block>>,
/// The item was expanded from a glob delegation item.
Expand All @@ -3523,6 +3524,7 @@ pub struct DelegationMac {

#[derive(Clone, Encodable, Decodable, Debug)]
pub struct StaticItem {
pub ident: Ident,
pub ty: P<Ty>,
pub safety: Safety,
pub mutability: Mutability,
Expand All @@ -3533,6 +3535,7 @@ pub struct StaticItem {
#[derive(Clone, Encodable, Decodable, Debug)]
pub struct ConstItem {
pub defaultness: Defaultness,
pub ident: Ident,
pub generics: Generics,
pub ty: P<Ty>,
pub expr: Option<P<Expr>>,
Expand All @@ -3545,7 +3548,7 @@ pub enum ItemKind {
/// An `extern crate` item, with the optional *original* crate name if the crate was renamed.
///
/// E.g., `extern crate foo` or `extern crate foo_bar as foo`.
ExternCrate(Option<Symbol>),
ExternCrate(Option<Symbol>, Ident),
/// A use declaration item (`use`).
///
/// E.g., `use foo;`, `use foo::bar;` or `use foo::bar as FooBar;`.
Expand All @@ -3567,7 +3570,7 @@ pub enum ItemKind {
/// E.g., `mod foo;` or `mod foo { .. }`.
/// `unsafe` keyword on modules is accepted syntactically for macro DSLs, but not
/// semantically by Rust.
Mod(Safety, ModKind),
Mod(Safety, Ident, ModKind),
/// An external module (`extern`).
///
/// E.g., `extern {}` or `extern "C" {}`.
Expand All @@ -3581,23 +3584,23 @@ pub enum ItemKind {
/// An enum definition (`enum`).
///
/// E.g., `enum Foo<A, B> { C<A>, D<B> }`.
Enum(EnumDef, Generics),
Enum(Ident, EnumDef, Generics),
/// A struct definition (`struct`).
///
/// E.g., `struct Foo<A> { x: A }`.
Struct(VariantData, Generics),
Struct(Ident, VariantData, Generics),
/// A union definition (`union`).
///
/// E.g., `union Foo<A, B> { x: A, y: B }`.
Union(VariantData, Generics),
Union(Ident, VariantData, Generics),
/// A trait declaration (`trait`).
///
/// E.g., `trait Foo { .. }`, `trait Foo<T> { .. }` or `auto trait Foo {}`.
Trait(Box<Trait>),
/// Trait alias.
///
/// E.g., `trait Foo = Bar + Quux;`.
TraitAlias(Generics, GenericBounds),
TraitAlias(Ident, Generics, GenericBounds),
/// An implementation.
///
/// E.g., `impl<A> Foo<A> { .. }` or `impl<A> Trait for Foo<A> { .. }`.
Expand All @@ -3608,7 +3611,7 @@ pub enum ItemKind {
MacCall(P<MacCall>),

/// A macro definition.
MacroDef(MacroDef),
MacroDef(Ident, MacroDef),

/// A single delegation item (`reuse`).
///
Expand All @@ -3620,6 +3623,31 @@ pub enum ItemKind {
}

impl ItemKind {
pub fn ident(&self) -> Option<Ident> {
match *self {
ItemKind::ExternCrate(_, ident)
| ItemKind::Static(box StaticItem { ident, .. })
| ItemKind::Const(box ConstItem { ident, .. })
| ItemKind::Fn(box Fn { ident, .. })
| ItemKind::Mod(_, ident, _)
| ItemKind::TyAlias(box TyAlias { ident, .. })
| ItemKind::Enum(ident, ..)
| ItemKind::Struct(ident, ..)
| ItemKind::Union(ident, ..)
| ItemKind::Trait(box Trait { ident, .. })
| ItemKind::TraitAlias(ident, ..)
| ItemKind::MacroDef(ident, _)
| ItemKind::Delegation(box Delegation { ident, .. }) => Some(ident),

ItemKind::Use(_)
| ItemKind::ForeignMod(_)
| ItemKind::GlobalAsm(_)
| ItemKind::Impl(_)
| ItemKind::MacCall(_)
| ItemKind::DelegationMac(_) => None,
}
}

/// "a" or "an"
pub fn article(&self) -> &'static str {
use ItemKind::*;
Expand Down Expand Up @@ -3660,11 +3688,11 @@ impl ItemKind {
Self::Fn(box Fn { generics, .. })
| Self::TyAlias(box TyAlias { generics, .. })
| Self::Const(box ConstItem { generics, .. })
| Self::Enum(_, generics)
| Self::Struct(_, generics)
| Self::Union(_, generics)
| Self::Enum(_, _, generics)
| Self::Struct(_, _, generics)
| Self::Union(_, _, generics)
| Self::Trait(box Trait { generics, .. })
| Self::TraitAlias(generics, _)
| Self::TraitAlias(_, generics, _)
| Self::Impl(box Impl { generics, .. }) => Some(generics),
_ => None,
}
Expand Down Expand Up @@ -3700,6 +3728,17 @@ pub enum AssocItemKind {
}

impl AssocItemKind {
pub fn ident(&self) -> Option<Ident> {
match *self {
AssocItemKind::Const(box ConstItem { ident, .. })
| AssocItemKind::Fn(box Fn { ident, .. })
| AssocItemKind::Type(box TyAlias { ident, .. })
| AssocItemKind::Delegation(box Delegation { ident, .. }) => Some(ident),

AssocItemKind::MacCall(_) | AssocItemKind::DelegationMac(_) => None,
}
}

pub fn defaultness(&self) -> Defaultness {
match *self {
Self::Const(box ConstItem { defaultness, .. })
Expand Down Expand Up @@ -3746,14 +3785,26 @@ impl TryFrom<ItemKind> for AssocItemKind {
pub enum ForeignItemKind {
/// A foreign static item (`static FOO: u8`).
Static(Box<StaticItem>),
/// An foreign function.
/// A foreign function.
Fn(Box<Fn>),
/// An foreign type.
/// A foreign type.
TyAlias(Box<TyAlias>),
/// A macro expanding to foreign items.
MacCall(P<MacCall>),
}

impl ForeignItemKind {
pub fn ident(&self) -> Option<Ident> {
match *self {
ForeignItemKind::Static(box StaticItem { ident, .. })
| ForeignItemKind::Fn(box Fn { ident, .. })
| ForeignItemKind::TyAlias(box TyAlias { ident, .. }) => Some(ident),

ForeignItemKind::MacCall(_) => None,
}
}
}

impl From<ForeignItemKind> for ItemKind {
fn from(foreign_item_kind: ForeignItemKind) -> ItemKind {
match foreign_item_kind {
Expand Down Expand Up @@ -3790,21 +3841,21 @@ mod size_asserts {

use super::*;
// tidy-alphabetical-start
static_assert_size!(AssocItem, 88);
static_assert_size!(AssocItem, 80);
static_assert_size!(AssocItemKind, 16);
static_assert_size!(Attribute, 32);
static_assert_size!(Block, 32);
static_assert_size!(Expr, 72);
static_assert_size!(ExprKind, 40);
static_assert_size!(Fn, 176);
static_assert_size!(ForeignItem, 88);
static_assert_size!(Fn, 184);
static_assert_size!(ForeignItem, 80);
static_assert_size!(ForeignItemKind, 16);
static_assert_size!(GenericArg, 24);
static_assert_size!(GenericBound, 88);
static_assert_size!(Generics, 40);
static_assert_size!(Impl, 136);
static_assert_size!(Item, 136);
static_assert_size!(ItemKind, 64);
static_assert_size!(Item, 144);
static_assert_size!(ItemKind, 80);
static_assert_size!(LitKind, 24);
static_assert_size!(Local, 80);
static_assert_size!(MetaItemLit, 40);
Expand Down
Loading
Loading