Skip to content

Commit df24796

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 43018ea commit df24796

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

+1072
-864
lines changed

compiler/rustc_ast/src/ast.rs

+79-28
Original file line numberDiff line numberDiff line change
@@ -3303,9 +3303,6 @@ pub struct Item<K = ItemKind> {
33033303
pub id: NodeId,
33043304
pub span: Span,
33053305
pub vis: Visibility,
3306-
/// The name of the item.
3307-
/// It might be a dummy name in case of anonymous items.
3308-
pub ident: Ident,
33093306

33103307
pub kind: K,
33113308

@@ -3327,23 +3324,23 @@ impl Item {
33273324

33283325
pub fn opt_generics(&self) -> Option<&Generics> {
33293326
match &self.kind {
3330-
ItemKind::ExternCrate(_)
3327+
ItemKind::ExternCrate(..)
33313328
| ItemKind::Use(_)
3332-
| ItemKind::Mod(_, _)
3329+
| ItemKind::Mod(..)
33333330
| ItemKind::ForeignMod(_)
33343331
| ItemKind::GlobalAsm(_)
33353332
| ItemKind::MacCall(_)
33363333
| ItemKind::Delegation(_)
33373334
| ItemKind::DelegationMac(_)
3338-
| ItemKind::MacroDef(_) => None,
3335+
| ItemKind::MacroDef(..) => None,
33393336
ItemKind::Static(_) => None,
33403337
ItemKind::Const(i) => Some(&i.generics),
33413338
ItemKind::Fn(i) => Some(&i.generics),
33423339
ItemKind::TyAlias(i) => Some(&i.generics),
3343-
ItemKind::TraitAlias(generics, _)
3344-
| ItemKind::Enum(_, generics)
3345-
| ItemKind::Struct(_, generics)
3346-
| ItemKind::Union(_, generics) => Some(&generics),
3340+
ItemKind::TraitAlias(_, generics, _)
3341+
| ItemKind::Enum(_, _, generics)
3342+
| ItemKind::Struct(_, _, generics)
3343+
| ItemKind::Union(_, _, generics) => Some(&generics),
33473344
ItemKind::Trait(i) => Some(&i.generics),
33483345
ItemKind::Impl(i) => Some(&i.generics),
33493346
}
@@ -3420,6 +3417,7 @@ impl Default for FnHeader {
34203417
pub struct Trait {
34213418
pub safety: Safety,
34223419
pub is_auto: IsAuto,
3420+
pub ident: Ident,
34233421
pub generics: Generics,
34243422
pub bounds: GenericBounds,
34253423
pub items: ThinVec<P<AssocItem>>,
@@ -3465,6 +3463,7 @@ pub struct TyAliasWhereClauses {
34653463
#[derive(Clone, Encodable, Decodable, Debug)]
34663464
pub struct TyAlias {
34673465
pub defaultness: Defaultness,
3466+
pub ident: Ident,
34683467
pub generics: Generics,
34693468
pub where_clauses: TyAliasWhereClauses,
34703469
pub bounds: GenericBounds,
@@ -3493,6 +3492,7 @@ pub struct FnContract {
34933492
#[derive(Clone, Encodable, Decodable, Debug)]
34943493
pub struct Fn {
34953494
pub defaultness: Defaultness,
3495+
pub ident: Ident,
34963496
pub generics: Generics,
34973497
pub sig: FnSig,
34983498
pub contract: Option<P<FnContract>>,
@@ -3506,6 +3506,7 @@ pub struct Delegation {
35063506
pub id: NodeId,
35073507
pub qself: Option<P<QSelf>>,
35083508
pub path: Path,
3509+
pub ident: Ident,
35093510
pub rename: Option<Ident>,
35103511
pub body: Option<P<Block>>,
35113512
/// The item was expanded from a glob delegation item.
@@ -3523,6 +3524,7 @@ pub struct DelegationMac {
35233524

35243525
#[derive(Clone, Encodable, Decodable, Debug)]
35253526
pub struct StaticItem {
3527+
pub ident: Ident,
35263528
pub ty: P<Ty>,
35273529
pub safety: Safety,
35283530
pub mutability: Mutability,
@@ -3533,6 +3535,7 @@ pub struct StaticItem {
35333535
#[derive(Clone, Encodable, Decodable, Debug)]
35343536
pub struct ConstItem {
35353537
pub defaultness: Defaultness,
3538+
pub ident: Ident,
35363539
pub generics: Generics,
35373540
pub ty: P<Ty>,
35383541
pub expr: Option<P<Expr>>,
@@ -3545,7 +3548,7 @@ pub enum ItemKind {
35453548
/// An `extern crate` item, with the optional *original* crate name if the crate was renamed.
35463549
///
35473550
/// E.g., `extern crate foo` or `extern crate foo_bar as foo`.
3548-
ExternCrate(Option<Symbol>),
3551+
ExternCrate(Option<Symbol>, Ident),
35493552
/// A use declaration item (`use`).
35503553
///
35513554
/// E.g., `use foo;`, `use foo::bar;` or `use foo::bar as FooBar;`.
@@ -3567,7 +3570,7 @@ pub enum ItemKind {
35673570
/// E.g., `mod foo;` or `mod foo { .. }`.
35683571
/// `unsafe` keyword on modules is accepted syntactically for macro DSLs, but not
35693572
/// semantically by Rust.
3570-
Mod(Safety, ModKind),
3573+
Mod(Safety, Ident, ModKind),
35713574
/// An external module (`extern`).
35723575
///
35733576
/// E.g., `extern {}` or `extern "C" {}`.
@@ -3581,23 +3584,23 @@ pub enum ItemKind {
35813584
/// An enum definition (`enum`).
35823585
///
35833586
/// E.g., `enum Foo<A, B> { C<A>, D<B> }`.
3584-
Enum(EnumDef, Generics),
3587+
Enum(Ident, EnumDef, Generics),
35853588
/// A struct definition (`struct`).
35863589
///
35873590
/// E.g., `struct Foo<A> { x: A }`.
3588-
Struct(VariantData, Generics),
3591+
Struct(Ident, VariantData, Generics),
35893592
/// A union definition (`union`).
35903593
///
35913594
/// E.g., `union Foo<A, B> { x: A, y: B }`.
3592-
Union(VariantData, Generics),
3595+
Union(Ident, VariantData, Generics),
35933596
/// A trait declaration (`trait`).
35943597
///
35953598
/// E.g., `trait Foo { .. }`, `trait Foo<T> { .. }` or `auto trait Foo {}`.
35963599
Trait(Box<Trait>),
35973600
/// Trait alias.
35983601
///
35993602
/// E.g., `trait Foo = Bar + Quux;`.
3600-
TraitAlias(Generics, GenericBounds),
3603+
TraitAlias(Ident, Generics, GenericBounds),
36013604
/// An implementation.
36023605
///
36033606
/// E.g., `impl<A> Foo<A> { .. }` or `impl<A> Trait for Foo<A> { .. }`.
@@ -3608,7 +3611,7 @@ pub enum ItemKind {
36083611
MacCall(P<MacCall>),
36093612

36103613
/// A macro definition.
3611-
MacroDef(MacroDef),
3614+
MacroDef(Ident, MacroDef),
36123615

36133616
/// A single delegation item (`reuse`).
36143617
///
@@ -3620,6 +3623,31 @@ pub enum ItemKind {
36203623
}
36213624

36223625
impl ItemKind {
3626+
pub fn ident(&self) -> Option<Ident> {
3627+
match *self {
3628+
ItemKind::ExternCrate(_, ident)
3629+
| ItemKind::Static(box StaticItem { ident, .. })
3630+
| ItemKind::Const(box ConstItem { ident, .. })
3631+
| ItemKind::Fn(box Fn { ident, .. })
3632+
| ItemKind::Mod(_, ident, _)
3633+
| ItemKind::TyAlias(box TyAlias { ident, .. })
3634+
| ItemKind::Enum(ident, ..)
3635+
| ItemKind::Struct(ident, ..)
3636+
| ItemKind::Union(ident, ..)
3637+
| ItemKind::Trait(box Trait { ident, .. })
3638+
| ItemKind::TraitAlias(ident, ..)
3639+
| ItemKind::MacroDef(ident, _)
3640+
| ItemKind::Delegation(box Delegation { ident, .. }) => Some(ident),
3641+
3642+
ItemKind::Use(_)
3643+
| ItemKind::ForeignMod(_)
3644+
| ItemKind::GlobalAsm(_)
3645+
| ItemKind::Impl(_)
3646+
| ItemKind::MacCall(_)
3647+
| ItemKind::DelegationMac(_) => None,
3648+
}
3649+
}
3650+
36233651
/// "a" or "an"
36243652
pub fn article(&self) -> &'static str {
36253653
use ItemKind::*;
@@ -3660,11 +3688,11 @@ impl ItemKind {
36603688
Self::Fn(box Fn { generics, .. })
36613689
| Self::TyAlias(box TyAlias { generics, .. })
36623690
| Self::Const(box ConstItem { generics, .. })
3663-
| Self::Enum(_, generics)
3664-
| Self::Struct(_, generics)
3665-
| Self::Union(_, generics)
3691+
| Self::Enum(_, _, generics)
3692+
| Self::Struct(_, _, generics)
3693+
| Self::Union(_, _, generics)
36663694
| Self::Trait(box Trait { generics, .. })
3667-
| Self::TraitAlias(generics, _)
3695+
| Self::TraitAlias(_, generics, _)
36683696
| Self::Impl(box Impl { generics, .. }) => Some(generics),
36693697
_ => None,
36703698
}
@@ -3700,6 +3728,17 @@ pub enum AssocItemKind {
37003728
}
37013729

37023730
impl AssocItemKind {
3731+
pub fn ident(&self) -> Option<Ident> {
3732+
match *self {
3733+
AssocItemKind::Const(box ConstItem { ident, .. })
3734+
| AssocItemKind::Fn(box Fn { ident, .. })
3735+
| AssocItemKind::Type(box TyAlias { ident, .. })
3736+
| AssocItemKind::Delegation(box Delegation { ident, .. }) => Some(ident),
3737+
3738+
AssocItemKind::MacCall(_) | AssocItemKind::DelegationMac(_) => None,
3739+
}
3740+
}
3741+
37033742
pub fn defaultness(&self) -> Defaultness {
37043743
match *self {
37053744
Self::Const(box ConstItem { defaultness, .. })
@@ -3746,14 +3785,26 @@ impl TryFrom<ItemKind> for AssocItemKind {
37463785
pub enum ForeignItemKind {
37473786
/// A foreign static item (`static FOO: u8`).
37483787
Static(Box<StaticItem>),
3749-
/// An foreign function.
3788+
/// A foreign function.
37503789
Fn(Box<Fn>),
3751-
/// An foreign type.
3790+
/// A foreign type.
37523791
TyAlias(Box<TyAlias>),
37533792
/// A macro expanding to foreign items.
37543793
MacCall(P<MacCall>),
37553794
}
37563795

3796+
impl ForeignItemKind {
3797+
pub fn ident(&self) -> Option<Ident> {
3798+
match *self {
3799+
ForeignItemKind::Static(box StaticItem { ident, .. })
3800+
| ForeignItemKind::Fn(box Fn { ident, .. })
3801+
| ForeignItemKind::TyAlias(box TyAlias { ident, .. }) => Some(ident),
3802+
3803+
ForeignItemKind::MacCall(_) => None,
3804+
}
3805+
}
3806+
}
3807+
37573808
impl From<ForeignItemKind> for ItemKind {
37583809
fn from(foreign_item_kind: ForeignItemKind) -> ItemKind {
37593810
match foreign_item_kind {
@@ -3790,21 +3841,21 @@ mod size_asserts {
37903841

37913842
use super::*;
37923843
// tidy-alphabetical-start
3793-
static_assert_size!(AssocItem, 88);
3844+
static_assert_size!(AssocItem, 80);
37943845
static_assert_size!(AssocItemKind, 16);
37953846
static_assert_size!(Attribute, 32);
37963847
static_assert_size!(Block, 32);
37973848
static_assert_size!(Expr, 72);
37983849
static_assert_size!(ExprKind, 40);
3799-
static_assert_size!(Fn, 176);
3800-
static_assert_size!(ForeignItem, 88);
3850+
static_assert_size!(Fn, 184);
3851+
static_assert_size!(ForeignItem, 80);
38013852
static_assert_size!(ForeignItemKind, 16);
38023853
static_assert_size!(GenericArg, 24);
38033854
static_assert_size!(GenericBound, 88);
38043855
static_assert_size!(Generics, 40);
38053856
static_assert_size!(Impl, 136);
3806-
static_assert_size!(Item, 136);
3807-
static_assert_size!(ItemKind, 64);
3857+
static_assert_size!(Item, 144);
3858+
static_assert_size!(ItemKind, 80);
38083859
static_assert_size!(LitKind, 24);
38093860
static_assert_size!(Local, 80);
38103861
static_assert_size!(MetaItemLit, 40);

0 commit comments

Comments
 (0)