Skip to content

Commit

Permalink
Box ast::ItemKind
Browse files Browse the repository at this point in the history
This reduces the size of ast::Item from 296 to 96 bytes.
  • Loading branch information
jyn514 committed Jan 25, 2021
1 parent 7fba12b commit 6f5d4b8
Show file tree
Hide file tree
Showing 53 changed files with 144 additions and 133 deletions.
8 changes: 6 additions & 2 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2581,7 +2581,7 @@ pub struct Item<K = ItemKind> {
/// It might be a dummy name in case of anonymous items.
pub ident: Ident,

pub kind: K,
pub kind: Box<K>,

/// Original tokens this item was parsed from. This isn't necessarily
/// available for all items, although over time more and more items should
Expand All @@ -2593,6 +2593,10 @@ pub struct Item<K = ItemKind> {
pub tokens: Option<LazyTokenStream>,
}

rustc_data_structures::static_assert_size!(Item<ItemKind>, 96);
rustc_data_structures::static_assert_size!(Item<AssocItemKind>, 96);
rustc_data_structures::static_assert_size!(Item<ForeignItemKind>, 96);

impl Item {
/// Return the span that encompasses the attributes.
pub fn span_with_attributes(&self) -> Span {
Expand All @@ -2603,7 +2607,7 @@ impl Item {
impl<K: Into<ItemKind>> Item<K> {
pub fn into_item(self) -> Item {
let Item { attrs, id, span, vis, ident, kind, tokens } = self;
Item { attrs, id, span, vis, ident, kind: kind.into(), tokens }
Item { attrs, id, span, vis, ident, kind: box (*kind).into(), tokens }
}
}

Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_ast/src/mut_visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -971,7 +971,7 @@ pub fn noop_flat_map_assoc_item<T: MutVisitor>(
visitor.visit_ident(ident);
visitor.visit_vis(vis);
visit_attrs(attrs, visitor);
match kind {
match &mut **kind {
AssocItemKind::Const(_, ty, expr) => {
visitor.visit_ty(ty);
visit_opt(expr, |expr| visitor.visit_expr(expr));
Expand Down Expand Up @@ -1014,7 +1014,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: box ItemKind::Mod(module),
tokens: None,
});
let items = vis.flat_map_item(item);
Expand All @@ -1025,7 +1025,7 @@ pub fn noop_visit_crate<T: MutVisitor>(krate: &mut Crate, vis: &mut T) {
Crate { module, attrs: vec![], span, proc_macros }
} else if len == 1 {
let Item { attrs, span, kind, .. } = items.into_iter().next().unwrap().into_inner();
match kind {
match *kind {
ItemKind::Mod(module) => Crate { module, attrs, span, proc_macros },
_ => panic!("visitor converted a module to not a module"),
}
Expand Down Expand Up @@ -1061,7 +1061,7 @@ pub fn noop_flat_map_foreign_item<T: MutVisitor>(
visitor.visit_ident(ident);
visitor.visit_vis(vis);
visit_attrs(attrs, visitor);
match kind {
match &mut **kind {
ForeignItemKind::Static(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(enum_def, _) = &*item.kind {
if let [variant] = &*enum_def.variants {
return variant.ident.name == sym::Input;
}
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_ast/src/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ pub fn walk_trait_ref<'a, V: Visitor<'a>>(visitor: &mut V, trait_ref: &'a TraitR
pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) {
visitor.visit_vis(&item.vis);
visitor.visit_ident(item.ident);
match item.kind {
match *item.kind {
ItemKind::ExternCrate(orig_name) => {
if let Some(orig_name) = orig_name {
visitor.visit_name(item.span, orig_name);
Expand Down Expand Up @@ -538,7 +538,7 @@ pub fn walk_foreign_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a ForeignI
visitor.visit_vis(vis);
visitor.visit_ident(ident);
walk_list!(visitor, visit_attribute, attrs);
match kind {
match &**kind {
ForeignItemKind::Static(ty, _, expr) => {
visitor.visit_ty(ty);
walk_list!(visitor, visit_expr, expr);
Expand Down Expand Up @@ -648,7 +648,7 @@ pub fn walk_assoc_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a AssocItem,
visitor.visit_vis(vis);
visitor.visit_ident(ident);
walk_list!(visitor, visit_attribute, attrs);
match kind {
match &**kind {
AssocItemKind::Const(_, ty, expr) => {
visitor.visit_ty(ty);
walk_list!(visitor, visit_expr, expr);
Expand Down
16 changes: 8 additions & 8 deletions compiler/rustc_ast_lowering/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> {
if let Some(hir_id) = item_hir_id {
self.lctx.with_parent_item_lifetime_defs(hir_id, |this| {
let this = &mut ItemLowerer { lctx: this };
if let ItemKind::Impl { ref of_trait, .. } = item.kind {
if let ItemKind::Impl { ref of_trait, .. } = *item.kind {
this.with_trait_impl_ref(of_trait, |this| visit::walk_item(this, item));
} else {
visit::walk_item(this, item);
Expand Down Expand Up @@ -182,7 +182,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
}

pub(super) fn lower_item_id(&mut self, i: &Item) -> SmallVec<[hir::ItemId; 1]> {
let node_ids = match i.kind {
let node_ids = match *i.kind {
ItemKind::Use(ref use_tree) => {
let mut vec = smallvec![i.id];
self.lower_item_id_use_tree(use_tree, i.id, &mut vec);
Expand Down Expand Up @@ -228,7 +228,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
let mut vis = self.lower_visibility(&i.vis, None);
let attrs = self.lower_attrs(&i.attrs);

if let ItemKind::MacroDef(MacroDef { ref body, macro_rules }) = i.kind {
if let ItemKind::MacroDef(MacroDef { ref body, macro_rules }) = *i.kind {
if !macro_rules || self.sess.contains_name(&i.attrs, sym::macro_export) {
let hir_id = self.lower_node_id(i.id);
let body = P(self.lower_mac_args(body));
Expand Down Expand Up @@ -697,7 +697,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
hir_id: self.lower_node_id(i.id),
ident: i.ident,
attrs: self.lower_attrs(&i.attrs),
kind: match i.kind {
kind: match *i.kind {
ForeignItemKind::Fn(_, ref sig, ref generics, _) => {
let fdec = &sig.decl;
let (generics, (fn_dec, fn_args)) = self.add_in_band_defs(
Expand Down Expand Up @@ -797,7 +797,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
fn lower_trait_item(&mut self, i: &AssocItem) -> hir::TraitItem<'hir> {
let trait_item_def_id = self.resolver.local_def_id(i.id);

let (generics, kind) = match i.kind {
let (generics, kind) = match *i.kind {
AssocItemKind::Const(_, ref ty, ref default) => {
let ty = self.lower_ty(ty, ImplTraitContext::disallowed());
let body = default.as_ref().map(|x| self.lower_const_body(i.span, Some(x)));
Expand Down Expand Up @@ -839,7 +839,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
}

fn lower_trait_item_ref(&mut self, i: &AssocItem) -> hir::TraitItemRef {
let (kind, has_default) = match &i.kind {
let (kind, has_default) = match &*i.kind {
AssocItemKind::Const(_, _, default) => (hir::AssocItemKind::Const, default.is_some()),
AssocItemKind::TyAlias(_, _, _, default) => {
(hir::AssocItemKind::Type, default.is_some())
Expand All @@ -862,7 +862,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
fn lower_impl_item(&mut self, i: &AssocItem) -> hir::ImplItem<'hir> {
let impl_item_def_id = self.resolver.local_def_id(i.id);

let (generics, kind) = match &i.kind {
let (generics, kind) = match &*i.kind {
AssocItemKind::Const(_, ty, expr) => {
let ty = self.lower_ty(ty, ImplTraitContext::disallowed());
(
Expand Down Expand Up @@ -935,7 +935,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
span: i.span,
vis: self.lower_visibility(&i.vis, Some(i.id)),
defaultness,
kind: match &i.kind {
kind: match &*i.kind {
AssocItemKind::Const(..) => hir::AssocItemKind::Const,
AssocItemKind::TyAlias(..) => hir::AssocItemKind::Type,
AssocItemKind::Fn(_, sig, ..) => {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
fn visit_item(&mut self, item: &'tcx Item) {
let hir_id = self.lctx.allocate_hir_id_counter(item.id);

match item.kind {
match *item.kind {
ItemKind::Struct(_, ref generics)
| ItemKind::Union(_, ref generics)
| ItemKind::Enum(_, ref generics)
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_ast_passes/src/ast_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -919,7 +919,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
self.check_nomangle_item_asciionly(item.ident, item.span);
}

match item.kind {
match *item.kind {
ItemKind::Impl {
unsafety,
polarity,
Expand Down Expand Up @@ -1090,7 +1090,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
}

fn visit_foreign_item(&mut self, fi: &'a ForeignItem) {
match &fi.kind {
match &*fi.kind {
ForeignItemKind::Fn(def, sig, _, body) => {
self.check_defaultness(fi.span, *def);
self.check_foreign_fn_bodyless(fi.ident, body.as_deref());
Expand Down Expand Up @@ -1332,7 +1332,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
}

if ctxt == AssocCtxt::Impl {
match &item.kind {
match &*item.kind {
AssocItemKind::Const(_, _, body) => {
self.check_impl_item_provided(item.span, body, "constant", " = <expr>;");
}
Expand All @@ -1349,13 +1349,13 @@ impl<'a> Visitor<'a> for AstValidator<'a> {

if ctxt == AssocCtxt::Trait || self.in_trait_impl {
self.invalid_visibility(&item.vis, None);
if let AssocItemKind::Fn(_, sig, _, _) = &item.kind {
if let AssocItemKind::Fn(_, sig, _, _) = &*item.kind {
self.check_trait_fn_not_const(sig.header.constness);
self.check_trait_fn_not_async(item.span, sig.header.asyncness);
}
}

if let AssocItemKind::Const(..) = item.kind {
if let AssocItemKind::Const(..) = *item.kind {
self.check_item_named(item.ident, "const");
}

Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_ast_passes/src/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
}

fn visit_item(&mut self, i: &'a ast::Item) {
match i.kind {
match *i.kind {
ast::ItemKind::ForeignMod(ref foreign_module) => {
if let Some(abi) = foreign_module.abi {
self.check_abi(abi);
Expand Down Expand Up @@ -408,7 +408,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
}

fn visit_foreign_item(&mut self, i: &'a ast::ForeignItem) {
match i.kind {
match *i.kind {
ast::ForeignItemKind::Fn(..) | ast::ForeignItemKind::Static(..) => {
let link_name = self.sess.first_attr_value_str_by_name(&i.attrs, sym::link_name);
let links_to_llvm =
Expand Down Expand Up @@ -554,7 +554,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
}

fn visit_assoc_item(&mut self, i: &'a ast::AssocItem, ctxt: AssocCtxt) {
let is_fn = match i.kind {
let is_fn = match *i.kind {
ast::AssocItemKind::Fn(_, ref sig, _, _) => {
if let (ast::Const::Yes(_), AssocCtxt::Trait) = (sig.header.constness, ctxt) {
gate_feature_post!(&self, const_fn, i.span, "const fn is unstable");
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_ast_pretty/src/pprust/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1021,7 +1021,7 @@ impl<'a> State<'a> {
self.hardbreak_if_not_bol();
self.maybe_print_comment(span.lo());
self.print_outer_attributes(attrs);
match kind {
match &**kind {
ast::ForeignItemKind::Fn(def, sig, gen, body) => {
self.print_fn_full(sig, ident, gen, vis, *def, body.as_deref(), attrs);
}
Expand Down Expand Up @@ -1106,7 +1106,7 @@ impl<'a> State<'a> {
self.maybe_print_comment(item.span.lo());
self.print_outer_attributes(&item.attrs);
self.ann.pre(self, AnnNode::Item(item));
match item.kind {
match *item.kind {
ast::ItemKind::ExternCrate(orig_name) => {
self.head(visibility_qualified(&item.vis, "extern crate"));
if let Some(orig_name) = orig_name {
Expand Down Expand Up @@ -1452,7 +1452,7 @@ impl<'a> State<'a> {
self.hardbreak_if_not_bol();
self.maybe_print_comment(span.lo());
self.print_outer_attributes(attrs);
match kind {
match &**kind {
ast::AssocItemKind::Fn(def, sig, gen, body) => {
self.print_fn_full(sig, ident, gen, vis, *def, body.as_deref(), attrs);
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_builtin_macros/src/deriving/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub fn expand_deriving_clone(
let substructure;
let is_shallow;
match *item {
Annotatable::Item(ref annitem) => match annitem.kind {
Annotatable::Item(ref annitem) => match *annitem.kind {
ItemKind::Struct(_, Generics { ref params, .. })
| ItemKind::Enum(_, Generics { ref params, .. }) => {
let container_id = cx.current_expansion.id.expn_data().parent;
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_builtin_macros/src/deriving/generic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ impl<'a> TraitDef<'a> {
}
false
});
let has_no_type_params = match item.kind {
let has_no_type_params = match *item.kind {
ast::ItemKind::Struct(_, ref generics)
| ast::ItemKind::Enum(_, ref generics)
| ast::ItemKind::Union(_, ref generics) => !generics
Expand All @@ -414,7 +414,7 @@ impl<'a> TraitDef<'a> {
let always_copy = has_no_type_params && cx.resolver.has_derive_copy(container_id);
let use_temporaries = is_packed && always_copy;

let newitem = match item.kind {
let newitem = match *item.kind {
ast::ItemKind::Struct(ref struct_def, ref generics) => self.expand_struct_def(
cx,
&struct_def,
Expand Down Expand Up @@ -527,7 +527,7 @@ impl<'a> TraitDef<'a> {
tokens: None,
},
attrs: Vec::new(),
kind: ast::AssocItemKind::TyAlias(
kind: box ast::AssocItemKind::TyAlias(
ast::Defaultness::Final,
Generics::default(),
Vec::new(),
Expand Down Expand Up @@ -929,7 +929,7 @@ impl<'a> MethodDef<'a> {
tokens: None,
},
ident: method_ident,
kind: ast::AssocItemKind::Fn(def, sig, fn_generics, Some(body_block)),
kind: box ast::AssocItemKind::Fn(def, sig, fn_generics, Some(body_block)),
tokens: None,
})
}
Expand Down Expand Up @@ -1732,7 +1732,7 @@ where
/// (for an enum, no variant has any fields)
pub fn is_type_without_fields(item: &Annotatable) -> bool {
if let Annotatable::Item(ref item) = *item {
match item.kind {
match *item.kind {
ast::ItemKind::Enum(ref enum_def, _) => {
enum_def.variants.iter().all(|v| v.data.fields().is_empty())
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_builtin_macros/src/deriving/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ fn inject_impl_of_structural_trait(
_ => unreachable!(),
};

let generics = match item.kind {
let generics = match *item.kind {
ItemKind::Struct(_, ref generics) | ItemKind::Enum(_, ref generics) => generics,
// Do not inject `impl Structural for Union`. (`PartialEq` does not
// support unions, so we will see error downstream.)
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_builtin_macros/src/global_allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub fn expand(
}

let item = match item {
Annotatable::Item(item) => match item.kind {
Annotatable::Item(item) => match *item.kind {
ItemKind::Static(..) => item,
_ => return not_static(Annotatable::Item(item)),
},
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_builtin_macros/src/global_asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub fn expand_global_asm<'cx>(
ident: Ident::invalid(),
attrs: Vec::new(),
id: ast::DUMMY_NODE_ID,
kind: ast::ItemKind::GlobalAsm(P(global_asm)),
kind: box ast::ItemKind::GlobalAsm(P(global_asm)),
vis: ast::Visibility {
span: sp.shrink_to_lo(),
kind: ast::VisibilityKind::Inherited,
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_builtin_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//! injecting code into the crate before it is lowered to HIR.

#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
#![feature(box_syntax)]
#![feature(bool_to_option)]
#![feature(crate_visibility_modifier)]
#![feature(decl_macro)]
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_builtin_macros/src/proc_macro_harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ impl<'a> CollectProcMacros<'a> {

impl<'a> Visitor<'a> for CollectProcMacros<'a> {
fn visit_item(&mut self, item: &'a ast::Item) {
if let ast::ItemKind::MacroDef(..) = item.kind {
if let ast::ItemKind::MacroDef(..) = *item.kind {
if self.is_proc_macro_crate && self.sess.contains_name(&item.attrs, sym::macro_export) {
let msg =
"cannot export macro_rules! macros from a `proc-macro` crate type currently";
Expand All @@ -256,7 +256,7 @@ impl<'a> Visitor<'a> for CollectProcMacros<'a> {
// we're just not interested in this item.
//
// If we find one, try to locate a `#[proc_macro_derive]` attribute on it.
let is_fn = matches!(item.kind, ast::ItemKind::Fn(..));
let is_fn = matches!(*item.kind, ast::ItemKind::Fn(..));

let mut found_attr: Option<&'a ast::Attribute> = None;

Expand Down
Loading

0 comments on commit 6f5d4b8

Please sign in to comment.