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

Box all fields of ast::*ItemKind #81683

Closed
wants to merge 7 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
71 changes: 43 additions & 28 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2685,6 +2685,21 @@ pub struct ImplKind {
#[derive(Clone, Encodable, Decodable, Debug)]
pub struct FnKind(pub Defaultness, pub FnSig, pub Generics, pub Option<P<Block>>);

#[derive(Clone, Encodable, Decodable, Debug)]
pub struct StaticKind(pub P<Ty>, pub Mutability, pub Option<P<Expr>>);

#[derive(Clone, Encodable, Decodable, Debug)]
pub struct ConstKind(pub Defaultness, pub P<Ty>, pub Option<P<Expr>>);

#[derive(Clone, Encodable, Decodable, Debug)]
pub struct StructUnionKind(pub VariantData, pub Generics);

#[derive(Clone, Encodable, Decodable, Debug)]
pub struct EnumKind(pub EnumDef, pub Generics);

#[derive(Clone, Encodable, Decodable, Debug)]
pub struct TraitAliasKind(pub Generics, pub GenericBounds);

#[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 @@ -2694,68 +2709,68 @@ pub enum ItemKind {
/// A use declaration item (`use`).
///
/// E.g., `use foo;`, `use foo::bar;` or `use foo::bar as FooBar;`.
Use(P<UseTree>),
Use(Box<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<StaticKind>),
/// A constant item (`const`).
///
/// E.g., `const FOO: i32 = 42;`.
Const(Defaultness, P<Ty>, Option<P<Expr>>),
Const(Box<ConstKind>),
/// A function declaration (`fn`).
///
/// E.g., `fn foo(bar: usize) -> usize { .. }`.
Fn(Box<FnKind>),
/// A module declaration (`mod`).
///
/// E.g., `mod foo;` or `mod foo { .. }`.
Mod(Mod),
Mod(Box<Mod>),
/// An external module (`extern`).
///
/// E.g., `extern {}` or `extern "C" {}`.
ForeignMod(ForeignMod),
ForeignMod(Box<ForeignMod>),
/// Module-level inline assembly (from `global_asm!()`).
GlobalAsm(P<GlobalAsm>),
GlobalAsm(Box<GlobalAsm>),
/// A type alias (`type`).
///
/// E.g., `type Foo = Bar<u8>;`.
TyAlias(Box<TyAliasKind>),
/// An enum definition (`enum`).
///
/// E.g., `enum Foo<A, B> { C<A>, D<B> }`.
Enum(EnumDef, Generics),
Enum(Box<EnumKind>),
/// A struct definition (`struct`).
///
/// E.g., `struct Foo<A> { x: A }`.
Struct(VariantData, Generics),
Struct(Box<StructUnionKind>),
/// A union definition (`union`).
///
/// E.g., `union Foo<A, B> { x: A, y: B }`.
Union(VariantData, Generics),
Union(Box<StructUnionKind>),
/// A trait declaration (`trait`).
///
/// E.g., `trait Foo { .. }`, `trait Foo<T> { .. }` or `auto trait Foo {}`.
Trait(Box<TraitKind>),
/// Trait alias
///
/// E.g., `trait Foo = Bar + Quux;`.
TraitAlias(Generics, GenericBounds),
TraitAlias(Box<TraitAliasKind>),
/// An implementation.
///
/// E.g., `impl<A> Foo<A> { .. }` or `impl<A> Trait for Foo<A> { .. }`.
Impl(Box<ImplKind>),
/// A macro invocation.
///
/// E.g., `foo!(..)`.
MacCall(MacCall),
MacCall(Box<MacCall>),

/// A macro definition.
MacroDef(MacroDef),
MacroDef(Box<MacroDef>),
}

#[cfg(target_arch = "x86_64")]
rustc_data_structures::static_assert_size!(ItemKind, 112);
rustc_data_structures::static_assert_size!(ItemKind, 16);

impl ItemKind {
pub fn article(&self) -> &str {
Expand Down Expand Up @@ -2793,11 +2808,11 @@ impl ItemKind {
match self {
Self::Fn(box FnKind(_, _, generics, _))
| Self::TyAlias(box TyAliasKind(_, generics, ..))
| Self::Enum(_, generics)
| Self::Struct(_, generics)
| Self::Union(_, generics)
| Self::Enum(box EnumKind(_, generics))
| Self::Struct(box StructUnionKind(_, generics))
| Self::Union(box StructUnionKind(_, generics))
| Self::Trait(box TraitKind(_, _, generics, ..))
| Self::TraitAlias(generics, _)
| Self::TraitAlias(box TraitAliasKind(generics, _))
| Self::Impl(box ImplKind { generics, .. }) => Some(generics),
_ => None,
}
Expand All @@ -2819,22 +2834,22 @@ 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<ConstKind>),
/// An associated function.
Fn(Box<FnKind>),
/// An associated type.
TyAlias(Box<TyAliasKind>),
/// A macro expanding to associated items.
MacCall(MacCall),
MacCall(Box<MacCall>),
}

#[cfg(target_arch = "x86_64")]
rustc_data_structures::static_assert_size!(AssocItemKind, 72);
rustc_data_structures::static_assert_size!(AssocItemKind, 16);

impl AssocItemKind {
pub fn defaultness(&self) -> Defaultness {
match *self {
Self::Const(def, ..)
Self::Const(box ConstKind(def, ..))
| Self::Fn(box FnKind(def, ..))
| Self::TyAlias(box TyAliasKind(def, ..)) => def,
Self::MacCall(..) => Defaultness::Final,
Expand All @@ -2845,7 +2860,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_kind) => ItemKind::Const(const_kind),
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 @@ -2858,7 +2873,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_kind) => AssocItemKind::Const(const_kind),
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 @@ -2871,22 +2886,22 @@ 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<StaticKind>),
/// An foreign function.
Fn(Box<FnKind>),
/// An foreign type.
TyAlias(Box<TyAliasKind>),
/// A macro expanding to foreign items.
MacCall(MacCall),
MacCall(Box<MacCall>),
}

#[cfg(target_arch = "x86_64")]
rustc_data_structures::static_assert_size!(ForeignItemKind, 72);
rustc_data_structures::static_assert_size!(ForeignItemKind, 16);

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_kind) => ItemKind::Static(static_kind),
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 @@ -2899,7 +2914,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_kind) => ForeignItemKind::Static(static_kind),
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 Down
18 changes: 10 additions & 8 deletions compiler/rustc_ast/src/mut_visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -908,7 +908,8 @@ pub fn noop_visit_item_kind<T: MutVisitor>(kind: &mut ItemKind, vis: &mut T) {
match kind {
ItemKind::ExternCrate(_orig_name) => {}
ItemKind::Use(use_tree) => vis.visit_use_tree(use_tree),
ItemKind::Static(ty, _, expr) | ItemKind::Const(_, ty, expr) => {
ItemKind::Static(box StaticKind(ty, _, expr))
| ItemKind::Const(box ConstKind(_, ty, expr)) => {
vis.visit_ty(ty);
visit_opt(expr, |expr| vis.visit_expr(expr));
}
Expand All @@ -925,11 +926,12 @@ pub fn noop_visit_item_kind<T: MutVisitor>(kind: &mut ItemKind, vis: &mut T) {
visit_bounds(bounds, vis);
visit_opt(ty, |ty| vis.visit_ty(ty));
}
ItemKind::Enum(EnumDef { variants }, generics) => {
ItemKind::Enum(box EnumKind(EnumDef { variants }, generics)) => {
variants.flat_map_in_place(|variant| vis.flat_map_variant(variant));
vis.visit_generics(generics);
}
ItemKind::Struct(variant_data, generics) | ItemKind::Union(variant_data, generics) => {
ItemKind::Struct(box StructUnionKind(variant_data, generics))
| ItemKind::Union(box StructUnionKind(variant_data, generics)) => {
vis.visit_variant_data(variant_data);
vis.visit_generics(generics);
}
Expand All @@ -953,7 +955,7 @@ pub fn noop_visit_item_kind<T: MutVisitor>(kind: &mut ItemKind, vis: &mut T) {
visit_bounds(bounds, vis);
items.flat_map_in_place(|item| vis.flat_map_trait_item(item));
}
ItemKind::TraitAlias(generics, bounds) => {
ItemKind::TraitAlias(box TraitAliasKind(generics, bounds)) => {
vis.visit_generics(generics);
visit_bounds(bounds, vis);
}
Expand All @@ -972,7 +974,7 @@ pub fn noop_flat_map_assoc_item<T: MutVisitor>(
visitor.visit_vis(vis);
visit_attrs(attrs, visitor);
match kind {
AssocItemKind::Const(_, ty, expr) => {
AssocItemKind::Const(box ConstKind(_, ty, expr)) => {
visitor.visit_ty(ty);
visit_opt(expr, |expr| visitor.visit_expr(expr));
}
Expand Down Expand Up @@ -1014,7 +1016,7 @@ pub fn noop_visit_crate<T: MutVisitor>(krate: &mut Crate, vis: &mut T) {
id: DUMMY_NODE_ID,
vis: item_vis,
span,
kind: ItemKind::Mod(module),
kind: ItemKind::Mod(box module),
tokens: None,
});
let items = vis.flat_map_item(item);
Expand All @@ -1026,7 +1028,7 @@ pub fn noop_visit_crate<T: MutVisitor>(krate: &mut Crate, vis: &mut T) {
} else if len == 1 {
let Item { attrs, span, kind, .. } = items.into_iter().next().unwrap().into_inner();
match kind {
ItemKind::Mod(module) => Crate { module, attrs, span, proc_macros },
ItemKind::Mod(box module) => Crate { module, attrs, span, proc_macros },
_ => panic!("visitor converted a module to not a module"),
}
} else {
Expand Down Expand Up @@ -1062,7 +1064,7 @@ pub fn noop_flat_map_foreign_item<T: MutVisitor>(
visitor.visit_vis(vis);
visit_attrs(attrs, visitor);
match kind {
ForeignItemKind::Static(ty, _, expr) => {
ForeignItemKind::Static(box StaticKind(ty, _, expr)) => {
visitor.visit_ty(ty);
visit_opt(expr, |expr| visitor.visit_expr(expr));
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,7 @@ impl Nonterminal {

let name = item.ident.name;
if name == sym::ProceduralMasqueradeDummyType || name == sym::ProcMacroHack {
if let ast::ItemKind::Enum(enum_def, _) = &item.kind {
if let ast::ItemKind::Enum(box ast::EnumKind(enum_def, _)) = &item.kind {
if let [variant] = &*enum_def.variants {
return variant.ident.name == sym::Input;
}
Expand Down
15 changes: 8 additions & 7 deletions compiler/rustc_ast/src/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,8 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) {
}
}
ItemKind::Use(ref use_tree) => visitor.visit_use_tree(use_tree, item.id, false),
ItemKind::Static(ref typ, _, ref expr) | ItemKind::Const(_, ref typ, ref expr) => {
ItemKind::Static(box StaticKind(ref typ, _, ref expr))
| ItemKind::Const(box ConstKind(_, ref typ, ref expr)) => {
visitor.visit_ty(typ);
walk_list!(visitor, visit_expr, expr);
}
Expand All @@ -307,7 +308,7 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) {
walk_list!(visitor, visit_param_bound, bounds);
walk_list!(visitor, visit_ty, ty);
}
ItemKind::Enum(ref enum_definition, ref generics) => {
ItemKind::Enum(box EnumKind(ref enum_definition, ref generics)) => {
visitor.visit_generics(generics);
visitor.visit_enum_def(enum_definition, generics, item.id, item.span)
}
Expand All @@ -326,8 +327,8 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) {
visitor.visit_ty(self_ty);
walk_list!(visitor, visit_assoc_item, items, AssocCtxt::Impl);
}
ItemKind::Struct(ref struct_definition, ref generics)
| ItemKind::Union(ref struct_definition, ref generics) => {
ItemKind::Struct(box StructUnionKind(ref struct_definition, ref generics))
| ItemKind::Union(box StructUnionKind(ref struct_definition, ref generics)) => {
visitor.visit_generics(generics);
visitor.visit_variant_data(struct_definition);
}
Expand All @@ -336,7 +337,7 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) {
walk_list!(visitor, visit_param_bound, bounds);
walk_list!(visitor, visit_assoc_item, items, AssocCtxt::Trait);
}
ItemKind::TraitAlias(ref generics, ref bounds) => {
ItemKind::TraitAlias(box TraitAliasKind(ref generics, ref bounds)) => {
visitor.visit_generics(generics);
walk_list!(visitor, visit_param_bound, bounds);
}
Expand Down Expand Up @@ -539,7 +540,7 @@ pub fn walk_foreign_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a ForeignI
visitor.visit_ident(ident);
walk_list!(visitor, visit_attribute, attrs);
match kind {
ForeignItemKind::Static(ty, _, expr) => {
ForeignItemKind::Static(box StaticKind(ty, _, expr)) => {
visitor.visit_ty(ty);
walk_list!(visitor, visit_expr, expr);
}
Expand Down Expand Up @@ -649,7 +650,7 @@ pub fn walk_assoc_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a AssocItem,
visitor.visit_ident(ident);
walk_list!(visitor, visit_attribute, attrs);
match kind {
AssocItemKind::Const(_, ty, expr) => {
AssocItemKind::Const(box ConstKind(_, ty, expr)) => {
visitor.visit_ty(ty);
walk_list!(visitor, visit_expr, expr);
}
Expand Down
Loading