Skip to content

Commit 6f5d4b8

Browse files
committed
Box ast::ItemKind
This reduces the size of ast::Item from 296 to 96 bytes.
1 parent 7fba12b commit 6f5d4b8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+144
-133
lines changed

compiler/rustc_ast/src/ast.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -2581,7 +2581,7 @@ pub struct Item<K = ItemKind> {
25812581
/// It might be a dummy name in case of anonymous items.
25822582
pub ident: Ident,
25832583

2584-
pub kind: K,
2584+
pub kind: Box<K>,
25852585

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

2596+
rustc_data_structures::static_assert_size!(Item<ItemKind>, 96);
2597+
rustc_data_structures::static_assert_size!(Item<AssocItemKind>, 96);
2598+
rustc_data_structures::static_assert_size!(Item<ForeignItemKind>, 96);
2599+
25962600
impl Item {
25972601
/// Return the span that encompasses the attributes.
25982602
pub fn span_with_attributes(&self) -> Span {
@@ -2603,7 +2607,7 @@ impl Item {
26032607
impl<K: Into<ItemKind>> Item<K> {
26042608
pub fn into_item(self) -> Item {
26052609
let Item { attrs, id, span, vis, ident, kind, tokens } = self;
2606-
Item { attrs, id, span, vis, ident, kind: kind.into(), tokens }
2610+
Item { attrs, id, span, vis, ident, kind: box (*kind).into(), tokens }
26072611
}
26082612
}
26092613

compiler/rustc_ast/src/mut_visit.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -971,7 +971,7 @@ pub fn noop_flat_map_assoc_item<T: MutVisitor>(
971971
visitor.visit_ident(ident);
972972
visitor.visit_vis(vis);
973973
visit_attrs(attrs, visitor);
974-
match kind {
974+
match &mut **kind {
975975
AssocItemKind::Const(_, ty, expr) => {
976976
visitor.visit_ty(ty);
977977
visit_opt(expr, |expr| visitor.visit_expr(expr));
@@ -1014,7 +1014,7 @@ pub fn noop_visit_crate<T: MutVisitor>(krate: &mut Crate, vis: &mut T) {
10141014
id: DUMMY_NODE_ID,
10151015
vis: item_vis,
10161016
span,
1017-
kind: ItemKind::Mod(module),
1017+
kind: box ItemKind::Mod(module),
10181018
tokens: None,
10191019
});
10201020
let items = vis.flat_map_item(item);
@@ -1025,7 +1025,7 @@ pub fn noop_visit_crate<T: MutVisitor>(krate: &mut Crate, vis: &mut T) {
10251025
Crate { module, attrs: vec![], span, proc_macros }
10261026
} else if len == 1 {
10271027
let Item { attrs, span, kind, .. } = items.into_iter().next().unwrap().into_inner();
1028-
match kind {
1028+
match *kind {
10291029
ItemKind::Mod(module) => Crate { module, attrs, span, proc_macros },
10301030
_ => panic!("visitor converted a module to not a module"),
10311031
}
@@ -1061,7 +1061,7 @@ pub fn noop_flat_map_foreign_item<T: MutVisitor>(
10611061
visitor.visit_ident(ident);
10621062
visitor.visit_vis(vis);
10631063
visit_attrs(attrs, visitor);
1064-
match kind {
1064+
match &mut **kind {
10651065
ForeignItemKind::Static(ty, _, expr) => {
10661066
visitor.visit_ty(ty);
10671067
visit_opt(expr, |expr| visitor.visit_expr(expr));

compiler/rustc_ast/src/token.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -805,7 +805,7 @@ impl Nonterminal {
805805

806806
let name = item.ident.name;
807807
if name == sym::ProceduralMasqueradeDummyType || name == sym::ProcMacroHack {
808-
if let ast::ItemKind::Enum(enum_def, _) = &item.kind {
808+
if let ast::ItemKind::Enum(enum_def, _) = &*item.kind {
809809
if let [variant] = &*enum_def.variants {
810810
return variant.ident.name == sym::Input;
811811
}

compiler/rustc_ast/src/visit.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ pub fn walk_trait_ref<'a, V: Visitor<'a>>(visitor: &mut V, trait_ref: &'a TraitR
281281
pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) {
282282
visitor.visit_vis(&item.vis);
283283
visitor.visit_ident(item.ident);
284-
match item.kind {
284+
match *item.kind {
285285
ItemKind::ExternCrate(orig_name) => {
286286
if let Some(orig_name) = orig_name {
287287
visitor.visit_name(item.span, orig_name);
@@ -538,7 +538,7 @@ pub fn walk_foreign_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a ForeignI
538538
visitor.visit_vis(vis);
539539
visitor.visit_ident(ident);
540540
walk_list!(visitor, visit_attribute, attrs);
541-
match kind {
541+
match &**kind {
542542
ForeignItemKind::Static(ty, _, expr) => {
543543
visitor.visit_ty(ty);
544544
walk_list!(visitor, visit_expr, expr);
@@ -648,7 +648,7 @@ pub fn walk_assoc_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a AssocItem,
648648
visitor.visit_vis(vis);
649649
visitor.visit_ident(ident);
650650
walk_list!(visitor, visit_attribute, attrs);
651-
match kind {
651+
match &**kind {
652652
AssocItemKind::Const(_, ty, expr) => {
653653
visitor.visit_ty(ty);
654654
walk_list!(visitor, visit_expr, expr);

compiler/rustc_ast_lowering/src/item.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> {
6767
if let Some(hir_id) = item_hir_id {
6868
self.lctx.with_parent_item_lifetime_defs(hir_id, |this| {
6969
let this = &mut ItemLowerer { lctx: this };
70-
if let ItemKind::Impl { ref of_trait, .. } = item.kind {
70+
if let ItemKind::Impl { ref of_trait, .. } = *item.kind {
7171
this.with_trait_impl_ref(of_trait, |this| visit::walk_item(this, item));
7272
} else {
7373
visit::walk_item(this, item);
@@ -182,7 +182,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
182182
}
183183

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

231-
if let ItemKind::MacroDef(MacroDef { ref body, macro_rules }) = i.kind {
231+
if let ItemKind::MacroDef(MacroDef { ref body, macro_rules }) = *i.kind {
232232
if !macro_rules || self.sess.contains_name(&i.attrs, sym::macro_export) {
233233
let hir_id = self.lower_node_id(i.id);
234234
let body = P(self.lower_mac_args(body));
@@ -697,7 +697,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
697697
hir_id: self.lower_node_id(i.id),
698698
ident: i.ident,
699699
attrs: self.lower_attrs(&i.attrs),
700-
kind: match i.kind {
700+
kind: match *i.kind {
701701
ForeignItemKind::Fn(_, ref sig, ref generics, _) => {
702702
let fdec = &sig.decl;
703703
let (generics, (fn_dec, fn_args)) = self.add_in_band_defs(
@@ -797,7 +797,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
797797
fn lower_trait_item(&mut self, i: &AssocItem) -> hir::TraitItem<'hir> {
798798
let trait_item_def_id = self.resolver.local_def_id(i.id);
799799

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

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

865-
let (generics, kind) = match &i.kind {
865+
let (generics, kind) = match &*i.kind {
866866
AssocItemKind::Const(_, ty, expr) => {
867867
let ty = self.lower_ty(ty, ImplTraitContext::disallowed());
868868
(
@@ -935,7 +935,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
935935
span: i.span,
936936
vis: self.lower_visibility(&i.vis, Some(i.id)),
937937
defaultness,
938-
kind: match &i.kind {
938+
kind: match &*i.kind {
939939
AssocItemKind::Const(..) => hir::AssocItemKind::Const,
940940
AssocItemKind::TyAlias(..) => hir::AssocItemKind::Type,
941941
AssocItemKind::Fn(_, sig, ..) => {

compiler/rustc_ast_lowering/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
497497
fn visit_item(&mut self, item: &'tcx Item) {
498498
let hir_id = self.lctx.allocate_hir_id_counter(item.id);
499499

500-
match item.kind {
500+
match *item.kind {
501501
ItemKind::Struct(_, ref generics)
502502
| ItemKind::Union(_, ref generics)
503503
| ItemKind::Enum(_, ref generics)

compiler/rustc_ast_passes/src/ast_validation.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -919,7 +919,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
919919
self.check_nomangle_item_asciionly(item.ident, item.span);
920920
}
921921

922-
match item.kind {
922+
match *item.kind {
923923
ItemKind::Impl {
924924
unsafety,
925925
polarity,
@@ -1090,7 +1090,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
10901090
}
10911091

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

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

13501350
if ctxt == AssocCtxt::Trait || self.in_trait_impl {
13511351
self.invalid_visibility(&item.vis, None);
1352-
if let AssocItemKind::Fn(_, sig, _, _) = &item.kind {
1352+
if let AssocItemKind::Fn(_, sig, _, _) = &*item.kind {
13531353
self.check_trait_fn_not_const(sig.header.constness);
13541354
self.check_trait_fn_not_async(item.span, sig.header.asyncness);
13551355
}
13561356
}
13571357

1358-
if let AssocItemKind::Const(..) = item.kind {
1358+
if let AssocItemKind::Const(..) = *item.kind {
13591359
self.check_item_named(item.ident, "const");
13601360
}
13611361

compiler/rustc_ast_passes/src/feature_gate.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
292292
}
293293

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

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

556556
fn visit_assoc_item(&mut self, i: &'a ast::AssocItem, ctxt: AssocCtxt) {
557-
let is_fn = match i.kind {
557+
let is_fn = match *i.kind {
558558
ast::AssocItemKind::Fn(_, ref sig, _, _) => {
559559
if let (ast::Const::Yes(_), AssocCtxt::Trait) = (sig.header.constness, ctxt) {
560560
gate_feature_post!(&self, const_fn, i.span, "const fn is unstable");

compiler/rustc_ast_pretty/src/pprust/state.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1021,7 +1021,7 @@ impl<'a> State<'a> {
10211021
self.hardbreak_if_not_bol();
10221022
self.maybe_print_comment(span.lo());
10231023
self.print_outer_attributes(attrs);
1024-
match kind {
1024+
match &**kind {
10251025
ast::ForeignItemKind::Fn(def, sig, gen, body) => {
10261026
self.print_fn_full(sig, ident, gen, vis, *def, body.as_deref(), attrs);
10271027
}
@@ -1106,7 +1106,7 @@ impl<'a> State<'a> {
11061106
self.maybe_print_comment(item.span.lo());
11071107
self.print_outer_attributes(&item.attrs);
11081108
self.ann.pre(self, AnnNode::Item(item));
1109-
match item.kind {
1109+
match *item.kind {
11101110
ast::ItemKind::ExternCrate(orig_name) => {
11111111
self.head(visibility_qualified(&item.vis, "extern crate"));
11121112
if let Some(orig_name) = orig_name {
@@ -1452,7 +1452,7 @@ impl<'a> State<'a> {
14521452
self.hardbreak_if_not_bol();
14531453
self.maybe_print_comment(span.lo());
14541454
self.print_outer_attributes(attrs);
1455-
match kind {
1455+
match &**kind {
14561456
ast::AssocItemKind::Fn(def, sig, gen, body) => {
14571457
self.print_fn_full(sig, ident, gen, vis, *def, body.as_deref(), attrs);
14581458
}

compiler/rustc_builtin_macros/src/deriving/clone.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub fn expand_deriving_clone(
3333
let substructure;
3434
let is_shallow;
3535
match *item {
36-
Annotatable::Item(ref annitem) => match annitem.kind {
36+
Annotatable::Item(ref annitem) => match *annitem.kind {
3737
ItemKind::Struct(_, Generics { ref params, .. })
3838
| ItemKind::Enum(_, Generics { ref params, .. }) => {
3939
let container_id = cx.current_expansion.id.expn_data().parent;

compiler/rustc_builtin_macros/src/deriving/generic/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ impl<'a> TraitDef<'a> {
401401
}
402402
false
403403
});
404-
let has_no_type_params = match item.kind {
404+
let has_no_type_params = match *item.kind {
405405
ast::ItemKind::Struct(_, ref generics)
406406
| ast::ItemKind::Enum(_, ref generics)
407407
| ast::ItemKind::Union(_, ref generics) => !generics
@@ -414,7 +414,7 @@ impl<'a> TraitDef<'a> {
414414
let always_copy = has_no_type_params && cx.resolver.has_derive_copy(container_id);
415415
let use_temporaries = is_packed && always_copy;
416416

417-
let newitem = match item.kind {
417+
let newitem = match *item.kind {
418418
ast::ItemKind::Struct(ref struct_def, ref generics) => self.expand_struct_def(
419419
cx,
420420
&struct_def,
@@ -527,7 +527,7 @@ impl<'a> TraitDef<'a> {
527527
tokens: None,
528528
},
529529
attrs: Vec::new(),
530-
kind: ast::AssocItemKind::TyAlias(
530+
kind: box ast::AssocItemKind::TyAlias(
531531
ast::Defaultness::Final,
532532
Generics::default(),
533533
Vec::new(),
@@ -929,7 +929,7 @@ impl<'a> MethodDef<'a> {
929929
tokens: None,
930930
},
931931
ident: method_ident,
932-
kind: ast::AssocItemKind::Fn(def, sig, fn_generics, Some(body_block)),
932+
kind: box ast::AssocItemKind::Fn(def, sig, fn_generics, Some(body_block)),
933933
tokens: None,
934934
})
935935
}
@@ -1732,7 +1732,7 @@ where
17321732
/// (for an enum, no variant has any fields)
17331733
pub fn is_type_without_fields(item: &Annotatable) -> bool {
17341734
if let Annotatable::Item(ref item) = *item {
1735-
match item.kind {
1735+
match *item.kind {
17361736
ast::ItemKind::Enum(ref enum_def, _) => {
17371737
enum_def.variants.iter().all(|v| v.data.fields().is_empty())
17381738
}

compiler/rustc_builtin_macros/src/deriving/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ fn inject_impl_of_structural_trait(
120120
_ => unreachable!(),
121121
};
122122

123-
let generics = match item.kind {
123+
let generics = match *item.kind {
124124
ItemKind::Struct(_, ref generics) | ItemKind::Enum(_, ref generics) => generics,
125125
// Do not inject `impl Structural for Union`. (`PartialEq` does not
126126
// support unions, so we will see error downstream.)

compiler/rustc_builtin_macros/src/global_allocator.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub fn expand(
3434
}
3535

3636
let item = match item {
37-
Annotatable::Item(item) => match item.kind {
37+
Annotatable::Item(item) => match *item.kind {
3838
ItemKind::Static(..) => item,
3939
_ => return not_static(Annotatable::Item(item)),
4040
},

compiler/rustc_builtin_macros/src/global_asm.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub fn expand_global_asm<'cx>(
2828
ident: Ident::invalid(),
2929
attrs: Vec::new(),
3030
id: ast::DUMMY_NODE_ID,
31-
kind: ast::ItemKind::GlobalAsm(P(global_asm)),
31+
kind: box ast::ItemKind::GlobalAsm(P(global_asm)),
3232
vis: ast::Visibility {
3333
span: sp.shrink_to_lo(),
3434
kind: ast::VisibilityKind::Inherited,

compiler/rustc_builtin_macros/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//! injecting code into the crate before it is lowered to HIR.
33
44
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
5+
#![feature(box_syntax)]
56
#![feature(bool_to_option)]
67
#![feature(crate_visibility_modifier)]
78
#![feature(decl_macro)]

compiler/rustc_builtin_macros/src/proc_macro_harness.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ impl<'a> CollectProcMacros<'a> {
244244

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

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

0 commit comments

Comments
 (0)