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

Shrink AST items #100363

Closed
wants to merge 14 commits into from
Closed
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
139 changes: 85 additions & 54 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
//! - [`Stmt`] and [`StmtKind`]: An executable action that does not return a value.
//! - [`FnDecl`], [`FnHeader`] and [`Param`]: Metadata associated with a function declaration.
//! - [`Generics`], [`GenericParam`], [`WhereClause`]: Metadata associated with generic parameters.
//! - [`EnumDef`] and [`Variant`]: Enum declaration.
//! - [`Enum`] and [`Variant`]: Enum declaration.
//! - [`Lit`] and [`LitKind`]: Literal expressions.
//! - [`MacroDef`], [`MacStmtStyle`], [`MacCall`], [`MacDelimiter`]: Macro definition and invocation.
//! - [`Attribute`]: Metadata associated with item.
Expand Down Expand Up @@ -771,7 +771,7 @@ pub enum PatKind {
Paren(P<Pat>),

/// A macro pattern; pre-expansion.
MacCall(MacCall),
MacCall(P<MacCall>),
}

#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Copy)]
Expand Down Expand Up @@ -981,7 +981,7 @@ pub enum StmtKind {

#[derive(Clone, Encodable, Decodable, Debug)]
pub struct MacCallStmt {
pub mac: MacCall,
pub mac: P<MacCall>,
pub style: MacStmtStyle,
pub attrs: AttrVec,
pub tokens: Option<LazyTokenStream>,
Expand Down Expand Up @@ -1439,7 +1439,7 @@ pub enum ExprKind {
InlineAsm(P<InlineAsm>),

/// A macro invocation; pre-expansion.
MacCall(MacCall),
MacCall(P<MacCall>),

/// A struct literal expression.
///
Expand Down Expand Up @@ -2042,7 +2042,7 @@ pub enum TyKind {
/// Inferred type of a `self` or `&self` argument in a method.
ImplicitSelf,
/// A macro in the type position.
MacCall(MacCall),
MacCall(P<MacCall>),
/// Placeholder for a kind that has failed to be defined.
Err,
/// Placeholder for a `va_list`.
Expand Down Expand Up @@ -2346,7 +2346,7 @@ impl Async {

#[derive(Copy, Clone, PartialEq, Eq, Hash, Encodable, Decodable, Debug)]
#[derive(HashStable_Generic)]
pub enum Const {
pub enum Constness {
Yes(Span),
No,
}
Expand Down Expand Up @@ -2440,10 +2440,6 @@ pub struct ForeignMod {
pub items: Vec<P<ForeignItem>>,
}

#[derive(Clone, Encodable, Decodable, Debug)]
pub struct EnumDef {
pub variants: Vec<Variant>,
}
/// Enum variant.
#[derive(Clone, Encodable, Decodable, Debug)]
pub struct Variant {
Expand Down Expand Up @@ -2717,7 +2713,7 @@ impl Extern {
pub struct FnHeader {
pub unsafety: Unsafe,
pub asyncness: Async,
pub constness: Const,
pub constness: Constness,
pub ext: Extern,
}

Expand All @@ -2727,7 +2723,7 @@ impl FnHeader {
let Self { unsafety, asyncness, constness, ext } = self;
matches!(unsafety, Unsafe::Yes(_))
|| asyncness.is_async()
|| matches!(constness, Const::Yes(_))
|| matches!(constness, Constness::Yes(_))
|| !matches!(ext, Extern::None)
}
}
Expand All @@ -2737,7 +2733,7 @@ impl Default for FnHeader {
FnHeader {
unsafety: Unsafe::No,
asyncness: Async::No,
constness: Const::No,
constness: Constness::No,
ext: Extern::None,
}
}
Expand Down Expand Up @@ -2792,7 +2788,7 @@ pub struct Impl {
pub defaultness: Defaultness,
pub unsafety: Unsafe,
pub generics: Generics,
pub constness: Const,
pub constness: Constness,
pub polarity: ImplPolarity,
/// The trait being implemented, if any.
pub of_trait: Option<TraitRef>,
Expand All @@ -2808,6 +2804,38 @@ pub struct Fn {
pub body: Option<P<Block>>,
}

#[derive(Clone, Encodable, Decodable, Debug)]
pub struct Enum {
pub variants: Vec<Variant>,
pub generics: Generics,
}

#[derive(Clone, Encodable, Decodable, Debug)]
pub struct Struct {
pub vdata: VariantData,
pub generics: Generics,
}

#[derive(Clone, Encodable, Decodable, Debug)]
pub struct TraitAlias {
pub generics: Generics,
pub bounds: GenericBounds,
}

#[derive(Clone, Encodable, Decodable, Debug)]
pub struct Static {
pub ty: P<Ty>,
pub mutbl: Mutability,
pub expr: Option<P<Expr>>,
}

#[derive(Clone, Encodable, Decodable, Debug)]
pub struct Const {
pub defaultness: Defaultness,
pub ty: P<Ty>,
pub expr: Option<P<Expr>>,
}

#[derive(Clone, Encodable, Decodable, Debug)]
pub enum ItemKind {
/// An `extern crate` item, with the optional *original* crate name if the crate was renamed.
Expand All @@ -2817,15 +2845,15 @@ pub enum ItemKind {
/// A use declaration item (`use`).
///
/// E.g., `use foo;`, `use foo::bar;` or `use foo::bar as FooBar;`.
Use(UseTree),
Use(P<UseTree>),
/// A static item (`static`).
///
/// E.g., `static FOO: i32 = 42;` or `static FOO: &'static str = "bar";`.
Static(P<Ty>, Mutability, Option<P<Expr>>),
Static(Box<Static>),
/// A constant item (`const`).
///
/// E.g., `const FOO: i32 = 42;`.
Const(Defaultness, P<Ty>, Option<P<Expr>>),
Const(Box<Const>),
/// A function declaration (`fn`).
///
/// E.g., `fn foo(bar: usize) -> usize { .. }`.
Expand All @@ -2835,11 +2863,11 @@ 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(Unsafe, ModKind),
Mod(Unsafe, P<ModKind>),
/// An external module (`extern`).
///
/// E.g., `extern {}` or `extern "C" {}`.
ForeignMod(ForeignMod),
ForeignMod(P<ForeignMod>),
/// Module-level inline assembly (from `global_asm!()`).
GlobalAsm(Box<InlineAsm>),
/// A type alias (`type`).
Expand All @@ -2849,31 +2877,31 @@ pub enum ItemKind {
/// An enum definition (`enum`).
///
/// E.g., `enum Foo<A, B> { C<A>, D<B> }`.
Enum(EnumDef, Generics),
Enum(Box<Enum>),
/// A struct definition (`struct`).
///
/// E.g., `struct Foo<A> { x: A }`.
Struct(VariantData, Generics),
Struct(Box<Struct>),
/// A union definition (`union`).
///
/// E.g., `union Foo<A, B> { x: A, y: B }`.
Union(VariantData, Generics),
Union(Box<Struct>),
/// 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(Box<TraitAlias>),
/// An implementation.
///
/// E.g., `impl<A> Foo<A> { .. }` or `impl<A> Trait for Foo<A> { .. }`.
Impl(Box<Impl>),
/// A macro invocation.
///
/// E.g., `foo!(..)`.
MacCall(MacCall),
MacCall(P<MacCall>),

/// A macro definition.
MacroDef(MacroDef),
Expand Down Expand Up @@ -2915,11 +2943,11 @@ impl ItemKind {
match self {
Self::Fn(box Fn { generics, .. })
| Self::TyAlias(box TyAlias { generics, .. })
| Self::Enum(_, generics)
| Self::Struct(_, generics)
| Self::Union(_, generics)
| Self::Enum(box Enum { generics, .. })
| Self::Struct(box Struct { generics, .. })
| Self::Union(box Struct { generics, .. })
| Self::Trait(box Trait { generics, .. })
| Self::TraitAlias(generics, _)
| Self::TraitAlias(box TraitAlias { generics, .. })
| Self::Impl(box Impl { generics, .. }) => Some(generics),
_ => None,
}
Expand All @@ -2941,19 +2969,19 @@ pub type AssocItem = Item<AssocItemKind>;
pub enum AssocItemKind {
/// An associated constant, `const $ident: $ty $def?;` where `def ::= "=" $expr? ;`.
/// If `def` is parsed, then the constant is provided, and otherwise required.
Const(Defaultness, P<Ty>, Option<P<Expr>>),
Const(Box<Const>),
/// An associated function.
Fn(Box<Fn>),
/// An associated type.
TyAlias(Box<TyAlias>),
/// A macro expanding to associated items.
MacCall(MacCall),
MacCall(P<MacCall>),
}

impl AssocItemKind {
pub fn defaultness(&self) -> Defaultness {
match *self {
Self::Const(defaultness, ..)
Self::Const(box Const { defaultness, .. })
| Self::Fn(box Fn { defaultness, .. })
| Self::TyAlias(box TyAlias { defaultness, .. }) => defaultness,
Self::MacCall(..) => Defaultness::Final,
Expand All @@ -2964,7 +2992,7 @@ impl AssocItemKind {
impl From<AssocItemKind> for ItemKind {
fn from(assoc_item_kind: AssocItemKind) -> ItemKind {
match assoc_item_kind {
AssocItemKind::Const(a, b, c) => ItemKind::Const(a, b, c),
AssocItemKind::Const(const_) => ItemKind::Const(const_),
AssocItemKind::Fn(fn_kind) => ItemKind::Fn(fn_kind),
AssocItemKind::TyAlias(ty_alias_kind) => ItemKind::TyAlias(ty_alias_kind),
AssocItemKind::MacCall(a) => ItemKind::MacCall(a),
Expand All @@ -2977,7 +3005,7 @@ impl TryFrom<ItemKind> for AssocItemKind {

fn try_from(item_kind: ItemKind) -> Result<AssocItemKind, ItemKind> {
Ok(match item_kind {
ItemKind::Const(a, b, c) => AssocItemKind::Const(a, b, c),
ItemKind::Const(const_) => AssocItemKind::Const(const_),
ItemKind::Fn(fn_kind) => AssocItemKind::Fn(fn_kind),
ItemKind::TyAlias(ty_alias_kind) => AssocItemKind::TyAlias(ty_alias_kind),
ItemKind::MacCall(a) => AssocItemKind::MacCall(a),
Expand All @@ -2990,19 +3018,19 @@ impl TryFrom<ItemKind> for AssocItemKind {
#[derive(Clone, Encodable, Decodable, Debug)]
pub enum ForeignItemKind {
/// A foreign static item (`static FOO: u8`).
Static(P<Ty>, Mutability, Option<P<Expr>>),
Static(Box<Static>),
/// An foreign function.
Fn(Box<Fn>),
/// An foreign type.
TyAlias(Box<TyAlias>),
/// A macro expanding to foreign items.
MacCall(MacCall),
MacCall(P<MacCall>),
}

impl From<ForeignItemKind> for ItemKind {
fn from(foreign_item_kind: ForeignItemKind) -> ItemKind {
match foreign_item_kind {
ForeignItemKind::Static(a, b, c) => ItemKind::Static(a, b, c),
ForeignItemKind::Static(static_) => ItemKind::Static(static_),
ForeignItemKind::Fn(fn_kind) => ItemKind::Fn(fn_kind),
ForeignItemKind::TyAlias(ty_alias_kind) => ItemKind::TyAlias(ty_alias_kind),
ForeignItemKind::MacCall(a) => ItemKind::MacCall(a),
Expand All @@ -3015,7 +3043,7 @@ impl TryFrom<ItemKind> for ForeignItemKind {

fn try_from(item_kind: ItemKind) -> Result<ForeignItemKind, ItemKind> {
Ok(match item_kind {
ItemKind::Static(a, b, c) => ForeignItemKind::Static(a, b, c),
ItemKind::Static(static_) => ForeignItemKind::Static(static_),
ItemKind::Fn(fn_kind) => ForeignItemKind::Fn(fn_kind),
ItemKind::TyAlias(ty_alias_kind) => ForeignItemKind::TyAlias(ty_alias_kind),
ItemKind::MacCall(a) => ForeignItemKind::MacCall(a),
Expand All @@ -3030,22 +3058,25 @@ pub type ForeignItem = Item<ForeignItemKind>;
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
mod size_asserts {
use super::*;
use rustc_data_structures::static_assert_size;
// These are in alphabetical order, which is easy to maintain.
rustc_data_structures::static_assert_size!(AssocItemKind, 72);
rustc_data_structures::static_assert_size!(Attribute, 152);
rustc_data_structures::static_assert_size!(Block, 48);
rustc_data_structures::static_assert_size!(Expr, 104);
rustc_data_structures::static_assert_size!(Fn, 192);
rustc_data_structures::static_assert_size!(ForeignItemKind, 72);
rustc_data_structures::static_assert_size!(GenericBound, 88);
rustc_data_structures::static_assert_size!(Generics, 72);
rustc_data_structures::static_assert_size!(Impl, 200);
rustc_data_structures::static_assert_size!(Item, 200);
rustc_data_structures::static_assert_size!(ItemKind, 112);
rustc_data_structures::static_assert_size!(Lit, 48);
rustc_data_structures::static_assert_size!(Pat, 120);
rustc_data_structures::static_assert_size!(Path, 40);
rustc_data_structures::static_assert_size!(PathSegment, 24);
rustc_data_structures::static_assert_size!(Stmt, 32);
rustc_data_structures::static_assert_size!(Ty, 96);
static_assert_size!(AssocItem, 104);
static_assert_size!(AssocItemKind, 16);
static_assert_size!(Attribute, 152);
static_assert_size!(Block, 48);
static_assert_size!(Expr, 104);
static_assert_size!(Fn, 192);
static_assert_size!(ForeignItem, 104);
static_assert_size!(ForeignItemKind, 16);
static_assert_size!(GenericBound, 88);
static_assert_size!(Generics, 72);
static_assert_size!(Impl, 200);
static_assert_size!(Item, 112);
static_assert_size!(ItemKind, 24);
static_assert_size!(Lit, 48);
static_assert_size!(Pat, 120);
static_assert_size!(Path, 40);
static_assert_size!(PathSegment, 24);
static_assert_size!(Stmt, 32);
static_assert_size!(Ty, 96);
}
Loading