Skip to content

Commit c977a9d

Browse files
committed
Move ast::Item::ident into ast::ItemKind.
`ast::Item` has an `ident` field. - It's always non-empty for these item kinds: `ExternCrate`, `Static`, `Const`, `Fn`, `Mod`, `TyAlias`, `Enum`, `Struct`, `Union`, `Trait`, `TraitAlias`, `MacroDef`, `Delegation`. - It's always empty for these item kinds: `Use`, `ForeignMod`, `GlobalAsm`, `Impl`, `MacCall`, `DelegationMac`. There is a similar story for `AssocItemKind` and `ForeignItemKind`. 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. The commit is large but it's mostly obvious plumbing work. Some notable things. - `ast::Item` got 8 bytes bigger. This could be avoided by boxing the fields within some of the `ast::ItemKind` variants (specifically: `Struct`, `Union`, `Enum`). I might do that in a follow-up; this commit is big enough already. - For the visitors: `FnKind` no longer needs an `ident` field because the `Fn` within how has one. - In the parser, the `ItemInfo` typedef is no longer needed. It was used in various places to return an `Ident` alongside an `ItemKind`, but now the `Ident` (if present) is within the `ItemKind`. - In a few places I renamed identifier variables called `name` (or `foo_name`) as `ident` (or `foo_ident`), to better match the type, and because `name` is normally used for `Symbol`s. It's confusing to see something like `foo_name.name`.
1 parent f219816 commit c977a9d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+1100
-883
lines changed

compiler/rustc_ast/src/ast.rs

+79-28
Original file line numberDiff line numberDiff line change
@@ -3273,9 +3273,6 @@ pub struct Item<K = ItemKind> {
32733273
pub id: NodeId,
32743274
pub span: Span,
32753275
pub vis: Visibility,
3276-
/// The name of the item.
3277-
/// It might be a dummy name in case of anonymous items.
3278-
pub ident: Ident,
32793276

32803277
pub kind: K,
32813278

@@ -3297,23 +3294,23 @@ impl Item {
32973294

32983295
pub fn opt_generics(&self) -> Option<&Generics> {
32993296
match &self.kind {
3300-
ItemKind::ExternCrate(_)
3297+
ItemKind::ExternCrate(..)
33013298
| ItemKind::Use(_)
3302-
| ItemKind::Mod(_, _)
3299+
| ItemKind::Mod(..)
33033300
| ItemKind::ForeignMod(_)
33043301
| ItemKind::GlobalAsm(_)
33053302
| ItemKind::MacCall(_)
33063303
| ItemKind::Delegation(_)
33073304
| ItemKind::DelegationMac(_)
3308-
| ItemKind::MacroDef(_) => None,
3305+
| ItemKind::MacroDef(..) => None,
33093306
ItemKind::Static(_) => None,
33103307
ItemKind::Const(i) => Some(&i.generics),
33113308
ItemKind::Fn(i) => Some(&i.generics),
33123309
ItemKind::TyAlias(i) => Some(&i.generics),
3313-
ItemKind::TraitAlias(generics, _)
3314-
| ItemKind::Enum(_, generics)
3315-
| ItemKind::Struct(_, generics)
3316-
| ItemKind::Union(_, generics) => Some(&generics),
3310+
ItemKind::TraitAlias(_, generics, _)
3311+
| ItemKind::Enum(_, _, generics)
3312+
| ItemKind::Struct(_, _, generics)
3313+
| ItemKind::Union(_, _, generics) => Some(&generics),
33173314
ItemKind::Trait(i) => Some(&i.generics),
33183315
ItemKind::Impl(i) => Some(&i.generics),
33193316
}
@@ -3390,6 +3387,7 @@ impl Default for FnHeader {
33903387
pub struct Trait {
33913388
pub safety: Safety,
33923389
pub is_auto: IsAuto,
3390+
pub ident: Ident,
33933391
pub generics: Generics,
33943392
pub bounds: GenericBounds,
33953393
pub items: ThinVec<P<AssocItem>>,
@@ -3435,6 +3433,7 @@ pub struct TyAliasWhereClauses {
34353433
#[derive(Clone, Encodable, Decodable, Debug)]
34363434
pub struct TyAlias {
34373435
pub defaultness: Defaultness,
3436+
pub ident: Ident,
34383437
pub generics: Generics,
34393438
pub where_clauses: TyAliasWhereClauses,
34403439
pub bounds: GenericBounds,
@@ -3463,6 +3462,7 @@ pub struct FnContract {
34633462
#[derive(Clone, Encodable, Decodable, Debug)]
34643463
pub struct Fn {
34653464
pub defaultness: Defaultness,
3465+
pub ident: Ident,
34663466
pub generics: Generics,
34673467
pub sig: FnSig,
34683468
pub contract: Option<P<FnContract>>,
@@ -3476,6 +3476,7 @@ pub struct Delegation {
34763476
pub id: NodeId,
34773477
pub qself: Option<P<QSelf>>,
34783478
pub path: Path,
3479+
pub ident: Ident,
34793480
pub rename: Option<Ident>,
34803481
pub body: Option<P<Block>>,
34813482
/// The item was expanded from a glob delegation item.
@@ -3493,6 +3494,7 @@ pub struct DelegationMac {
34933494

34943495
#[derive(Clone, Encodable, Decodable, Debug)]
34953496
pub struct StaticItem {
3497+
pub ident: Ident,
34963498
pub ty: P<Ty>,
34973499
pub safety: Safety,
34983500
pub mutability: Mutability,
@@ -3502,6 +3504,7 @@ pub struct StaticItem {
35023504
#[derive(Clone, Encodable, Decodable, Debug)]
35033505
pub struct ConstItem {
35043506
pub defaultness: Defaultness,
3507+
pub ident: Ident,
35053508
pub generics: Generics,
35063509
pub ty: P<Ty>,
35073510
pub expr: Option<P<Expr>>,
@@ -3513,7 +3516,7 @@ pub enum ItemKind {
35133516
/// An `extern crate` item, with the optional *original* crate name if the crate was renamed.
35143517
///
35153518
/// E.g., `extern crate foo` or `extern crate foo_bar as foo`.
3516-
ExternCrate(Option<Symbol>),
3519+
ExternCrate(Option<Symbol>, Ident),
35173520
/// A use declaration item (`use`).
35183521
///
35193522
/// E.g., `use foo;`, `use foo::bar;` or `use foo::bar as FooBar;`.
@@ -3535,7 +3538,7 @@ pub enum ItemKind {
35353538
/// E.g., `mod foo;` or `mod foo { .. }`.
35363539
/// `unsafe` keyword on modules is accepted syntactically for macro DSLs, but not
35373540
/// semantically by Rust.
3538-
Mod(Safety, ModKind),
3541+
Mod(Safety, Ident, ModKind),
35393542
/// An external module (`extern`).
35403543
///
35413544
/// E.g., `extern {}` or `extern "C" {}`.
@@ -3549,23 +3552,23 @@ pub enum ItemKind {
35493552
/// An enum definition (`enum`).
35503553
///
35513554
/// E.g., `enum Foo<A, B> { C<A>, D<B> }`.
3552-
Enum(EnumDef, Generics),
3555+
Enum(Ident, EnumDef, Generics),
35533556
/// A struct definition (`struct`).
35543557
///
35553558
/// E.g., `struct Foo<A> { x: A }`.
3556-
Struct(VariantData, Generics),
3559+
Struct(Ident, VariantData, Generics),
35573560
/// A union definition (`union`).
35583561
///
35593562
/// E.g., `union Foo<A, B> { x: A, y: B }`.
3560-
Union(VariantData, Generics),
3563+
Union(Ident, VariantData, Generics),
35613564
/// A trait declaration (`trait`).
35623565
///
35633566
/// E.g., `trait Foo { .. }`, `trait Foo<T> { .. }` or `auto trait Foo {}`.
35643567
Trait(Box<Trait>),
35653568
/// Trait alias.
35663569
///
35673570
/// E.g., `trait Foo = Bar + Quux;`.
3568-
TraitAlias(Generics, GenericBounds),
3571+
TraitAlias(Ident, Generics, GenericBounds),
35693572
/// An implementation.
35703573
///
35713574
/// E.g., `impl<A> Foo<A> { .. }` or `impl<A> Trait for Foo<A> { .. }`.
@@ -3576,7 +3579,7 @@ pub enum ItemKind {
35763579
MacCall(P<MacCall>),
35773580

35783581
/// A macro definition.
3579-
MacroDef(MacroDef),
3582+
MacroDef(Ident, MacroDef),
35803583

35813584
/// A single delegation item (`reuse`).
35823585
///
@@ -3588,6 +3591,31 @@ pub enum ItemKind {
35883591
}
35893592

35903593
impl ItemKind {
3594+
pub fn ident(&self) -> Option<Ident> {
3595+
match *self {
3596+
ItemKind::ExternCrate(_, ident)
3597+
| ItemKind::Static(box StaticItem { ident, .. })
3598+
| ItemKind::Const(box ConstItem { ident, .. })
3599+
| ItemKind::Fn(box Fn { ident, .. })
3600+
| ItemKind::Mod(_, ident, _)
3601+
| ItemKind::TyAlias(box TyAlias { ident, .. })
3602+
| ItemKind::Enum(ident, ..)
3603+
| ItemKind::Struct(ident, ..)
3604+
| ItemKind::Union(ident, ..)
3605+
| ItemKind::Trait(box Trait { ident, .. })
3606+
| ItemKind::TraitAlias(ident, ..)
3607+
| ItemKind::MacroDef(ident, _)
3608+
| ItemKind::Delegation(box Delegation { ident, .. }) => Some(ident),
3609+
3610+
ItemKind::Use(_)
3611+
| ItemKind::ForeignMod(_)
3612+
| ItemKind::GlobalAsm(_)
3613+
| ItemKind::Impl(_)
3614+
| ItemKind::MacCall(_)
3615+
| ItemKind::DelegationMac(_) => None,
3616+
}
3617+
}
3618+
35913619
/// "a" or "an"
35923620
pub fn article(&self) -> &'static str {
35933621
use ItemKind::*;
@@ -3628,11 +3656,11 @@ impl ItemKind {
36283656
Self::Fn(box Fn { generics, .. })
36293657
| Self::TyAlias(box TyAlias { generics, .. })
36303658
| Self::Const(box ConstItem { generics, .. })
3631-
| Self::Enum(_, generics)
3632-
| Self::Struct(_, generics)
3633-
| Self::Union(_, generics)
3659+
| Self::Enum(_, _, generics)
3660+
| Self::Struct(_, _, generics)
3661+
| Self::Union(_, _, generics)
36343662
| Self::Trait(box Trait { generics, .. })
3635-
| Self::TraitAlias(generics, _)
3663+
| Self::TraitAlias(_, generics, _)
36363664
| Self::Impl(box Impl { generics, .. }) => Some(generics),
36373665
_ => None,
36383666
}
@@ -3668,6 +3696,17 @@ pub enum AssocItemKind {
36683696
}
36693697

36703698
impl AssocItemKind {
3699+
pub fn ident(&self) -> Option<Ident> {
3700+
match *self {
3701+
AssocItemKind::Const(box ConstItem { ident, .. })
3702+
| AssocItemKind::Fn(box Fn { ident, .. })
3703+
| AssocItemKind::Type(box TyAlias { ident, .. })
3704+
| AssocItemKind::Delegation(box Delegation { ident, .. }) => Some(ident),
3705+
3706+
AssocItemKind::MacCall(_) | AssocItemKind::DelegationMac(_) => None,
3707+
}
3708+
}
3709+
36713710
pub fn defaultness(&self) -> Defaultness {
36723711
match *self {
36733712
Self::Const(box ConstItem { defaultness, .. })
@@ -3714,14 +3753,26 @@ impl TryFrom<ItemKind> for AssocItemKind {
37143753
pub enum ForeignItemKind {
37153754
/// A foreign static item (`static FOO: u8`).
37163755
Static(Box<StaticItem>),
3717-
/// An foreign function.
3756+
/// A foreign function.
37183757
Fn(Box<Fn>),
3719-
/// An foreign type.
3758+
/// A foreign type.
37203759
TyAlias(Box<TyAlias>),
37213760
/// A macro expanding to foreign items.
37223761
MacCall(P<MacCall>),
37233762
}
37243763

3764+
impl ForeignItemKind {
3765+
pub fn ident(&self) -> Option<Ident> {
3766+
match *self {
3767+
ForeignItemKind::Static(box StaticItem { ident, .. })
3768+
| ForeignItemKind::Fn(box Fn { ident, .. })
3769+
| ForeignItemKind::TyAlias(box TyAlias { ident, .. }) => Some(ident),
3770+
3771+
ForeignItemKind::MacCall(_) => None,
3772+
}
3773+
}
3774+
}
3775+
37253776
impl From<ForeignItemKind> for ItemKind {
37263777
fn from(foreign_item_kind: ForeignItemKind) -> ItemKind {
37273778
match foreign_item_kind {
@@ -3758,21 +3809,21 @@ mod size_asserts {
37583809

37593810
use super::*;
37603811
// tidy-alphabetical-start
3761-
static_assert_size!(AssocItem, 88);
3812+
static_assert_size!(AssocItem, 80);
37623813
static_assert_size!(AssocItemKind, 16);
37633814
static_assert_size!(Attribute, 32);
37643815
static_assert_size!(Block, 32);
37653816
static_assert_size!(Expr, 72);
37663817
static_assert_size!(ExprKind, 40);
3767-
static_assert_size!(Fn, 176);
3768-
static_assert_size!(ForeignItem, 88);
3818+
static_assert_size!(Fn, 184);
3819+
static_assert_size!(ForeignItem, 80);
37693820
static_assert_size!(ForeignItemKind, 16);
37703821
static_assert_size!(GenericArg, 24);
37713822
static_assert_size!(GenericBound, 88);
37723823
static_assert_size!(Generics, 40);
37733824
static_assert_size!(Impl, 136);
3774-
static_assert_size!(Item, 136);
3775-
static_assert_size!(ItemKind, 64);
3825+
static_assert_size!(Item, 144);
3826+
static_assert_size!(ItemKind, 80);
37763827
static_assert_size!(LitKind, 24);
37773828
static_assert_size!(Local, 80);
37783829
static_assert_size!(MetaItemLit, 40);

0 commit comments

Comments
 (0)