Skip to content

Commit 8ba9610

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 f723811 commit 8ba9610

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
@@ -3311,9 +3311,6 @@ pub struct Item<K = ItemKind> {
33113311
pub id: NodeId,
33123312
pub span: Span,
33133313
pub vis: Visibility,
3314-
/// The name of the item.
3315-
/// It might be a dummy name in case of anonymous items.
3316-
pub ident: Ident,
33173314

33183315
pub kind: K,
33193316

@@ -3335,23 +3332,23 @@ impl Item {
33353332

33363333
pub fn opt_generics(&self) -> Option<&Generics> {
33373334
match &self.kind {
3338-
ItemKind::ExternCrate(_)
3335+
ItemKind::ExternCrate(..)
33393336
| ItemKind::Use(_)
3340-
| ItemKind::Mod(_, _)
3337+
| ItemKind::Mod(..)
33413338
| ItemKind::ForeignMod(_)
33423339
| ItemKind::GlobalAsm(_)
33433340
| ItemKind::MacCall(_)
33443341
| ItemKind::Delegation(_)
33453342
| ItemKind::DelegationMac(_)
3346-
| ItemKind::MacroDef(_) => None,
3343+
| ItemKind::MacroDef(..) => None,
33473344
ItemKind::Static(_) => None,
33483345
ItemKind::Const(i) => Some(&i.generics),
33493346
ItemKind::Fn(i) => Some(&i.generics),
33503347
ItemKind::TyAlias(i) => Some(&i.generics),
3351-
ItemKind::TraitAlias(generics, _)
3352-
| ItemKind::Enum(_, generics)
3353-
| ItemKind::Struct(_, generics)
3354-
| ItemKind::Union(_, generics) => Some(&generics),
3348+
ItemKind::TraitAlias(_, generics, _)
3349+
| ItemKind::Enum(_, _, generics)
3350+
| ItemKind::Struct(_, _, generics)
3351+
| ItemKind::Union(_, _, generics) => Some(&generics),
33553352
ItemKind::Trait(i) => Some(&i.generics),
33563353
ItemKind::Impl(i) => Some(&i.generics),
33573354
}
@@ -3428,6 +3425,7 @@ impl Default for FnHeader {
34283425
pub struct Trait {
34293426
pub safety: Safety,
34303427
pub is_auto: IsAuto,
3428+
pub ident: Ident,
34313429
pub generics: Generics,
34323430
pub bounds: GenericBounds,
34333431
pub items: ThinVec<P<AssocItem>>,
@@ -3473,6 +3471,7 @@ pub struct TyAliasWhereClauses {
34733471
#[derive(Clone, Encodable, Decodable, Debug)]
34743472
pub struct TyAlias {
34753473
pub defaultness: Defaultness,
3474+
pub ident: Ident,
34763475
pub generics: Generics,
34773476
pub where_clauses: TyAliasWhereClauses,
34783477
pub bounds: GenericBounds,
@@ -3501,6 +3500,7 @@ pub struct FnContract {
35013500
#[derive(Clone, Encodable, Decodable, Debug)]
35023501
pub struct Fn {
35033502
pub defaultness: Defaultness,
3503+
pub ident: Ident,
35043504
pub generics: Generics,
35053505
pub sig: FnSig,
35063506
pub contract: Option<P<FnContract>>,
@@ -3514,6 +3514,7 @@ pub struct Delegation {
35143514
pub id: NodeId,
35153515
pub qself: Option<P<QSelf>>,
35163516
pub path: Path,
3517+
pub ident: Ident,
35173518
pub rename: Option<Ident>,
35183519
pub body: Option<P<Block>>,
35193520
/// The item was expanded from a glob delegation item.
@@ -3531,6 +3532,7 @@ pub struct DelegationMac {
35313532

35323533
#[derive(Clone, Encodable, Decodable, Debug)]
35333534
pub struct StaticItem {
3535+
pub ident: Ident,
35343536
pub ty: P<Ty>,
35353537
pub safety: Safety,
35363538
pub mutability: Mutability,
@@ -3540,6 +3542,7 @@ pub struct StaticItem {
35403542
#[derive(Clone, Encodable, Decodable, Debug)]
35413543
pub struct ConstItem {
35423544
pub defaultness: Defaultness,
3545+
pub ident: Ident,
35433546
pub generics: Generics,
35443547
pub ty: P<Ty>,
35453548
pub expr: Option<P<Expr>>,
@@ -3551,7 +3554,7 @@ pub enum ItemKind {
35513554
/// An `extern crate` item, with the optional *original* crate name if the crate was renamed.
35523555
///
35533556
/// E.g., `extern crate foo` or `extern crate foo_bar as foo`.
3554-
ExternCrate(Option<Symbol>),
3557+
ExternCrate(Option<Symbol>, Ident),
35553558
/// A use declaration item (`use`).
35563559
///
35573560
/// E.g., `use foo;`, `use foo::bar;` or `use foo::bar as FooBar;`.
@@ -3573,7 +3576,7 @@ pub enum ItemKind {
35733576
/// E.g., `mod foo;` or `mod foo { .. }`.
35743577
/// `unsafe` keyword on modules is accepted syntactically for macro DSLs, but not
35753578
/// semantically by Rust.
3576-
Mod(Safety, ModKind),
3579+
Mod(Safety, Ident, ModKind),
35773580
/// An external module (`extern`).
35783581
///
35793582
/// E.g., `extern {}` or `extern "C" {}`.
@@ -3587,23 +3590,23 @@ pub enum ItemKind {
35873590
/// An enum definition (`enum`).
35883591
///
35893592
/// E.g., `enum Foo<A, B> { C<A>, D<B> }`.
3590-
Enum(EnumDef, Generics),
3593+
Enum(Ident, EnumDef, Generics),
35913594
/// A struct definition (`struct`).
35923595
///
35933596
/// E.g., `struct Foo<A> { x: A }`.
3594-
Struct(VariantData, Generics),
3597+
Struct(Ident, VariantData, Generics),
35953598
/// A union definition (`union`).
35963599
///
35973600
/// E.g., `union Foo<A, B> { x: A, y: B }`.
3598-
Union(VariantData, Generics),
3601+
Union(Ident, VariantData, Generics),
35993602
/// A trait declaration (`trait`).
36003603
///
36013604
/// E.g., `trait Foo { .. }`, `trait Foo<T> { .. }` or `auto trait Foo {}`.
36023605
Trait(Box<Trait>),
36033606
/// Trait alias.
36043607
///
36053608
/// E.g., `trait Foo = Bar + Quux;`.
3606-
TraitAlias(Generics, GenericBounds),
3609+
TraitAlias(Ident, Generics, GenericBounds),
36073610
/// An implementation.
36083611
///
36093612
/// E.g., `impl<A> Foo<A> { .. }` or `impl<A> Trait for Foo<A> { .. }`.
@@ -3614,7 +3617,7 @@ pub enum ItemKind {
36143617
MacCall(P<MacCall>),
36153618

36163619
/// A macro definition.
3617-
MacroDef(MacroDef),
3620+
MacroDef(Ident, MacroDef),
36183621

36193622
/// A single delegation item (`reuse`).
36203623
///
@@ -3626,6 +3629,31 @@ pub enum ItemKind {
36263629
}
36273630

36283631
impl ItemKind {
3632+
pub fn ident(&self) -> Option<Ident> {
3633+
match *self {
3634+
ItemKind::ExternCrate(_, ident)
3635+
| ItemKind::Static(box StaticItem { ident, .. })
3636+
| ItemKind::Const(box ConstItem { ident, .. })
3637+
| ItemKind::Fn(box Fn { ident, .. })
3638+
| ItemKind::Mod(_, ident, _)
3639+
| ItemKind::TyAlias(box TyAlias { ident, .. })
3640+
| ItemKind::Enum(ident, ..)
3641+
| ItemKind::Struct(ident, ..)
3642+
| ItemKind::Union(ident, ..)
3643+
| ItemKind::Trait(box Trait { ident, .. })
3644+
| ItemKind::TraitAlias(ident, ..)
3645+
| ItemKind::MacroDef(ident, _)
3646+
| ItemKind::Delegation(box Delegation { ident, .. }) => Some(ident),
3647+
3648+
ItemKind::Use(_)
3649+
| ItemKind::ForeignMod(_)
3650+
| ItemKind::GlobalAsm(_)
3651+
| ItemKind::Impl(_)
3652+
| ItemKind::MacCall(_)
3653+
| ItemKind::DelegationMac(_) => None,
3654+
}
3655+
}
3656+
36293657
/// "a" or "an"
36303658
pub fn article(&self) -> &'static str {
36313659
use ItemKind::*;
@@ -3666,11 +3694,11 @@ impl ItemKind {
36663694
Self::Fn(box Fn { generics, .. })
36673695
| Self::TyAlias(box TyAlias { generics, .. })
36683696
| Self::Const(box ConstItem { generics, .. })
3669-
| Self::Enum(_, generics)
3670-
| Self::Struct(_, generics)
3671-
| Self::Union(_, generics)
3697+
| Self::Enum(_, _, generics)
3698+
| Self::Struct(_, _, generics)
3699+
| Self::Union(_, _, generics)
36723700
| Self::Trait(box Trait { generics, .. })
3673-
| Self::TraitAlias(generics, _)
3701+
| Self::TraitAlias(_, generics, _)
36743702
| Self::Impl(box Impl { generics, .. }) => Some(generics),
36753703
_ => None,
36763704
}
@@ -3706,6 +3734,17 @@ pub enum AssocItemKind {
37063734
}
37073735

37083736
impl AssocItemKind {
3737+
pub fn ident(&self) -> Option<Ident> {
3738+
match *self {
3739+
AssocItemKind::Const(box ConstItem { ident, .. })
3740+
| AssocItemKind::Fn(box Fn { ident, .. })
3741+
| AssocItemKind::Type(box TyAlias { ident, .. })
3742+
| AssocItemKind::Delegation(box Delegation { ident, .. }) => Some(ident),
3743+
3744+
AssocItemKind::MacCall(_) | AssocItemKind::DelegationMac(_) => None,
3745+
}
3746+
}
3747+
37093748
pub fn defaultness(&self) -> Defaultness {
37103749
match *self {
37113750
Self::Const(box ConstItem { defaultness, .. })
@@ -3752,14 +3791,26 @@ impl TryFrom<ItemKind> for AssocItemKind {
37523791
pub enum ForeignItemKind {
37533792
/// A foreign static item (`static FOO: u8`).
37543793
Static(Box<StaticItem>),
3755-
/// An foreign function.
3794+
/// A foreign function.
37563795
Fn(Box<Fn>),
3757-
/// An foreign type.
3796+
/// A foreign type.
37583797
TyAlias(Box<TyAlias>),
37593798
/// A macro expanding to foreign items.
37603799
MacCall(P<MacCall>),
37613800
}
37623801

3802+
impl ForeignItemKind {
3803+
pub fn ident(&self) -> Option<Ident> {
3804+
match *self {
3805+
ForeignItemKind::Static(box StaticItem { ident, .. })
3806+
| ForeignItemKind::Fn(box Fn { ident, .. })
3807+
| ForeignItemKind::TyAlias(box TyAlias { ident, .. }) => Some(ident),
3808+
3809+
ForeignItemKind::MacCall(_) => None,
3810+
}
3811+
}
3812+
}
3813+
37633814
impl From<ForeignItemKind> for ItemKind {
37643815
fn from(foreign_item_kind: ForeignItemKind) -> ItemKind {
37653816
match foreign_item_kind {
@@ -3796,21 +3847,21 @@ mod size_asserts {
37963847

37973848
use super::*;
37983849
// tidy-alphabetical-start
3799-
static_assert_size!(AssocItem, 88);
3850+
static_assert_size!(AssocItem, 80);
38003851
static_assert_size!(AssocItemKind, 16);
38013852
static_assert_size!(Attribute, 32);
38023853
static_assert_size!(Block, 32);
38033854
static_assert_size!(Expr, 72);
38043855
static_assert_size!(ExprKind, 40);
3805-
static_assert_size!(Fn, 176);
3806-
static_assert_size!(ForeignItem, 88);
3856+
static_assert_size!(Fn, 184);
3857+
static_assert_size!(ForeignItem, 80);
38073858
static_assert_size!(ForeignItemKind, 16);
38083859
static_assert_size!(GenericArg, 24);
38093860
static_assert_size!(GenericBound, 88);
38103861
static_assert_size!(Generics, 40);
38113862
static_assert_size!(Impl, 136);
3812-
static_assert_size!(Item, 136);
3813-
static_assert_size!(ItemKind, 64);
3863+
static_assert_size!(Item, 144);
3864+
static_assert_size!(ItemKind, 80);
38143865
static_assert_size!(LitKind, 24);
38153866
static_assert_size!(Local, 80);
38163867
static_assert_size!(MetaItemLit, 40);

0 commit comments

Comments
 (0)