Skip to content

Commit a860b64

Browse files
committed
Box StaticKind, ConstKind, Mac
1 parent 7d5dad6 commit a860b64

File tree

16 files changed

+89
-64
lines changed

16 files changed

+89
-64
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2685,6 +2685,12 @@ pub struct ImplKind {
26852685
#[derive(Clone, Encodable, Decodable, Debug)]
26862686
pub struct FnKind(pub Defaultness, pub FnSig, pub Generics, pub Option<P<Block>>);
26872687

2688+
#[derive(Clone, Encodable, Decodable, Debug)]
2689+
pub struct StaticKind(pub P<Ty>, pub Mutability, pub Option<P<Expr>>);
2690+
2691+
#[derive(Clone, Encodable, Decodable, Debug)]
2692+
pub struct ConstKind(pub Defaultness, pub P<Ty>, pub Option<P<Expr>>);
2693+
26882694
#[derive(Clone, Encodable, Decodable, Debug)]
26892695
pub enum ItemKind {
26902696
/// An `extern crate` item, with the optional *original* crate name if the crate was renamed.
@@ -2698,11 +2704,11 @@ pub enum ItemKind {
26982704
/// A static item (`static`).
26992705
///
27002706
/// E.g., `static FOO: i32 = 42;` or `static FOO: &'static str = "bar";`.
2701-
Static(P<Ty>, Mutability, Option<P<Expr>>),
2707+
Static(Box<StaticKind>),
27022708
/// A constant item (`const`).
27032709
///
27042710
/// E.g., `const FOO: i32 = 42;`.
2705-
Const(Defaultness, P<Ty>, Option<P<Expr>>),
2711+
Const(Box<ConstKind>),
27062712
/// A function declaration (`fn`).
27072713
///
27082714
/// E.g., `fn foo(bar: usize) -> usize { .. }`.
@@ -2748,7 +2754,7 @@ pub enum ItemKind {
27482754
/// A macro invocation.
27492755
///
27502756
/// E.g., `foo!(..)`.
2751-
MacCall(MacCall),
2757+
MacCall(Box<MacCall>),
27522758

27532759
/// A macro definition.
27542760
MacroDef(MacroDef),
@@ -2819,22 +2825,22 @@ pub type AssocItem = Item<AssocItemKind>;
28192825
pub enum AssocItemKind {
28202826
/// An associated constant, `const $ident: $ty $def?;` where `def ::= "=" $expr? ;`.
28212827
/// If `def` is parsed, then the constant is provided, and otherwise required.
2822-
Const(Defaultness, P<Ty>, Option<P<Expr>>),
2828+
Const(Box<ConstKind>),
28232829
/// An associated function.
28242830
Fn(Box<FnKind>),
28252831
/// An associated type.
28262832
TyAlias(Box<TyAliasKind>),
28272833
/// A macro expanding to associated items.
2828-
MacCall(MacCall),
2834+
MacCall(Box<MacCall>),
28292835
}
28302836

28312837
#[cfg(target_arch = "x86_64")]
2832-
rustc_data_structures::static_assert_size!(AssocItemKind, 72);
2838+
rustc_data_structures::static_assert_size!(AssocItemKind, 16);
28332839

28342840
impl AssocItemKind {
28352841
pub fn defaultness(&self) -> Defaultness {
28362842
match *self {
2837-
Self::Const(def, ..)
2843+
Self::Const(box ConstKind(def, ..))
28382844
| Self::Fn(box FnKind(def, ..))
28392845
| Self::TyAlias(box TyAliasKind(def, ..)) => def,
28402846
Self::MacCall(..) => Defaultness::Final,
@@ -2845,7 +2851,7 @@ impl AssocItemKind {
28452851
impl From<AssocItemKind> for ItemKind {
28462852
fn from(assoc_item_kind: AssocItemKind) -> ItemKind {
28472853
match assoc_item_kind {
2848-
AssocItemKind::Const(a, b, c) => ItemKind::Const(a, b, c),
2854+
AssocItemKind::Const(const_kind) => ItemKind::Const(const_kind),
28492855
AssocItemKind::Fn(fn_kind) => ItemKind::Fn(fn_kind),
28502856
AssocItemKind::TyAlias(ty_alias_kind) => ItemKind::TyAlias(ty_alias_kind),
28512857
AssocItemKind::MacCall(a) => ItemKind::MacCall(a),
@@ -2858,7 +2864,7 @@ impl TryFrom<ItemKind> for AssocItemKind {
28582864

28592865
fn try_from(item_kind: ItemKind) -> Result<AssocItemKind, ItemKind> {
28602866
Ok(match item_kind {
2861-
ItemKind::Const(a, b, c) => AssocItemKind::Const(a, b, c),
2867+
ItemKind::Const(const_kind) => AssocItemKind::Const(const_kind),
28622868
ItemKind::Fn(fn_kind) => AssocItemKind::Fn(fn_kind),
28632869
ItemKind::TyAlias(ty_alias_kind) => AssocItemKind::TyAlias(ty_alias_kind),
28642870
ItemKind::MacCall(a) => AssocItemKind::MacCall(a),
@@ -2871,22 +2877,22 @@ impl TryFrom<ItemKind> for AssocItemKind {
28712877
#[derive(Clone, Encodable, Decodable, Debug)]
28722878
pub enum ForeignItemKind {
28732879
/// A foreign static item (`static FOO: u8`).
2874-
Static(P<Ty>, Mutability, Option<P<Expr>>),
2880+
Static(Box<StaticKind>),
28752881
/// An foreign function.
28762882
Fn(Box<FnKind>),
28772883
/// An foreign type.
28782884
TyAlias(Box<TyAliasKind>),
28792885
/// A macro expanding to foreign items.
2880-
MacCall(MacCall),
2886+
MacCall(Box<MacCall>),
28812887
}
28822888

28832889
#[cfg(target_arch = "x86_64")]
2884-
rustc_data_structures::static_assert_size!(ForeignItemKind, 72);
2890+
rustc_data_structures::static_assert_size!(ForeignItemKind, 16);
28852891

28862892
impl From<ForeignItemKind> for ItemKind {
28872893
fn from(foreign_item_kind: ForeignItemKind) -> ItemKind {
28882894
match foreign_item_kind {
2889-
ForeignItemKind::Static(a, b, c) => ItemKind::Static(a, b, c),
2895+
ForeignItemKind::Static(static_kind) => ItemKind::Static(static_kind),
28902896
ForeignItemKind::Fn(fn_kind) => ItemKind::Fn(fn_kind),
28912897
ForeignItemKind::TyAlias(ty_alias_kind) => ItemKind::TyAlias(ty_alias_kind),
28922898
ForeignItemKind::MacCall(a) => ItemKind::MacCall(a),
@@ -2899,7 +2905,7 @@ impl TryFrom<ItemKind> for ForeignItemKind {
28992905

29002906
fn try_from(item_kind: ItemKind) -> Result<ForeignItemKind, ItemKind> {
29012907
Ok(match item_kind {
2902-
ItemKind::Static(a, b, c) => ForeignItemKind::Static(a, b, c),
2908+
ItemKind::Static(static_kind) => ForeignItemKind::Static(static_kind),
29032909
ItemKind::Fn(fn_kind) => ForeignItemKind::Fn(fn_kind),
29042910
ItemKind::TyAlias(ty_alias_kind) => ForeignItemKind::TyAlias(ty_alias_kind),
29052911
ItemKind::MacCall(a) => ForeignItemKind::MacCall(a),

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -908,7 +908,8 @@ pub fn noop_visit_item_kind<T: MutVisitor>(kind: &mut ItemKind, vis: &mut T) {
908908
match kind {
909909
ItemKind::ExternCrate(_orig_name) => {}
910910
ItemKind::Use(use_tree) => vis.visit_use_tree(use_tree),
911-
ItemKind::Static(ty, _, expr) | ItemKind::Const(_, ty, expr) => {
911+
ItemKind::Static(box StaticKind(ty, _, expr))
912+
| ItemKind::Const(box ConstKind(_, ty, expr)) => {
912913
vis.visit_ty(ty);
913914
visit_opt(expr, |expr| vis.visit_expr(expr));
914915
}
@@ -972,7 +973,7 @@ pub fn noop_flat_map_assoc_item<T: MutVisitor>(
972973
visitor.visit_vis(vis);
973974
visit_attrs(attrs, visitor);
974975
match kind {
975-
AssocItemKind::Const(_, ty, expr) => {
976+
AssocItemKind::Const(box ConstKind(_, ty, expr)) => {
976977
visitor.visit_ty(ty);
977978
visit_opt(expr, |expr| visitor.visit_expr(expr));
978979
}
@@ -1062,7 +1063,7 @@ pub fn noop_flat_map_foreign_item<T: MutVisitor>(
10621063
visitor.visit_vis(vis);
10631064
visit_attrs(attrs, visitor);
10641065
match kind {
1065-
ForeignItemKind::Static(ty, _, expr) => {
1066+
ForeignItemKind::Static(box StaticKind(ty, _, expr)) => {
10661067
visitor.visit_ty(ty);
10671068
visit_opt(expr, |expr| visitor.visit_expr(expr));
10681069
}

compiler/rustc_ast/src/visit.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,8 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) {
288288
}
289289
}
290290
ItemKind::Use(ref use_tree) => visitor.visit_use_tree(use_tree, item.id, false),
291-
ItemKind::Static(ref typ, _, ref expr) | ItemKind::Const(_, ref typ, ref expr) => {
291+
ItemKind::Static(box StaticKind(ref typ, _, ref expr))
292+
| ItemKind::Const(box ConstKind(_, ref typ, ref expr)) => {
292293
visitor.visit_ty(typ);
293294
walk_list!(visitor, visit_expr, expr);
294295
}
@@ -539,7 +540,7 @@ pub fn walk_foreign_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a ForeignI
539540
visitor.visit_ident(ident);
540541
walk_list!(visitor, visit_attribute, attrs);
541542
match kind {
542-
ForeignItemKind::Static(ty, _, expr) => {
543+
ForeignItemKind::Static(box StaticKind(ty, _, expr)) => {
543544
visitor.visit_ty(ty);
544545
walk_list!(visitor, visit_expr, expr);
545546
}
@@ -649,7 +650,7 @@ pub fn walk_assoc_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a AssocItem,
649650
visitor.visit_ident(ident);
650651
walk_list!(visitor, visit_attribute, attrs);
651652
match kind {
652-
AssocItemKind::Const(_, ty, expr) => {
653+
AssocItemKind::Const(box ConstKind(_, ty, expr)) => {
653654
visitor.visit_ty(ty);
654655
walk_list!(visitor, visit_expr, expr);
655656
}

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -270,11 +270,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
270270

271271
self.lower_use_tree(use_tree, &prefix, id, vis, ident, attrs)
272272
}
273-
ItemKind::Static(ref t, m, ref e) => {
273+
ItemKind::Static(box StaticKind(ref t, m, ref e)) => {
274274
let (ty, body_id) = self.lower_const_item(t, span, e.as_deref());
275275
hir::ItemKind::Static(ty, m, body_id)
276276
}
277-
ItemKind::Const(_, ref t, ref e) => {
277+
ItemKind::Const(box ConstKind(_, ref t, ref e)) => {
278278
let (ty, body_id) = self.lower_const_item(t, span, e.as_deref());
279279
hir::ItemKind::Const(ty, body_id)
280280
}
@@ -723,7 +723,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
723723

724724
hir::ForeignItemKind::Fn(fn_dec, fn_args, generics)
725725
}
726-
ForeignItemKind::Static(ref t, m, _) => {
726+
ForeignItemKind::Static(box StaticKind(ref t, m, _)) => {
727727
let ty = self.lower_ty(t, ImplTraitContext::disallowed());
728728
hir::ForeignItemKind::Static(ty, m)
729729
}
@@ -806,7 +806,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
806806
let trait_item_def_id = self.resolver.local_def_id(i.id);
807807

808808
let (generics, kind) = match i.kind {
809-
AssocItemKind::Const(_, ref ty, ref default) => {
809+
AssocItemKind::Const(box ConstKind(_, ref ty, ref default)) => {
810810
let ty = self.lower_ty(ty, ImplTraitContext::disallowed());
811811
let body = default.as_ref().map(|x| self.lower_const_body(i.span, Some(x)));
812812
(hir::Generics::empty(), hir::TraitItemKind::Const(ty, body))
@@ -848,7 +848,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
848848

849849
fn lower_trait_item_ref(&mut self, i: &AssocItem) -> hir::TraitItemRef {
850850
let (kind, has_default) = match &i.kind {
851-
AssocItemKind::Const(_, _, default) => (hir::AssocItemKind::Const, default.is_some()),
851+
AssocItemKind::Const(box ConstKind(_, _, default)) => {
852+
(hir::AssocItemKind::Const, default.is_some())
853+
}
852854
AssocItemKind::TyAlias(box TyAliasKind(_, _, _, default)) => {
853855
(hir::AssocItemKind::Type, default.is_some())
854856
}
@@ -871,7 +873,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
871873
let impl_item_def_id = self.resolver.local_def_id(i.id);
872874

873875
let (generics, kind) = match &i.kind {
874-
AssocItemKind::Const(_, ty, expr) => {
876+
AssocItemKind::Const(box ConstKind(_, ty, expr)) => {
875877
let ty = self.lower_ty(ty, ImplTraitContext::disallowed());
876878
(
877879
hir::Generics::empty(),

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,12 +1072,12 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
10721072
self.err_handler().span_err(item.span, "unions cannot have zero fields");
10731073
}
10741074
}
1075-
ItemKind::Const(def, .., None) => {
1075+
ItemKind::Const(box ConstKind(def, .., None)) => {
10761076
self.check_defaultness(item.span, def);
10771077
let msg = "free constant item without body";
10781078
self.error_item_without_body(item.span, "constant", msg, " = <expr>;");
10791079
}
1080-
ItemKind::Static(.., None) => {
1080+
ItemKind::Static(box StaticKind(.., None)) => {
10811081
let msg = "free static item without body";
10821082
self.error_item_without_body(item.span, "static", msg, " = <expr>;");
10831083
}
@@ -1108,7 +1108,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
11081108
self.check_type_no_bounds(bounds, "`extern` blocks");
11091109
self.check_foreign_ty_genericless(generics);
11101110
}
1111-
ForeignItemKind::Static(_, _, body) => {
1111+
ForeignItemKind::Static(box StaticKind(_, _, body)) => {
11121112
self.check_foreign_kind_bodyless(fi.ident, "static", body.as_ref().map(|b| b.span));
11131113
}
11141114
ForeignItemKind::MacCall(..) => {}
@@ -1339,7 +1339,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
13391339

13401340
if ctxt == AssocCtxt::Impl {
13411341
match &item.kind {
1342-
AssocItemKind::Const(_, _, body) => {
1342+
AssocItemKind::Const(box ConstKind(_, _, body)) => {
13431343
self.check_impl_item_provided(item.span, body, "constant", " = <expr>;");
13441344
}
13451345
AssocItemKind::Fn(box FnKind(_, _, _, body)) => {

compiler/rustc_ast_pretty/src/pprust/state.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,7 +1025,7 @@ impl<'a> State<'a> {
10251025
ast::ForeignItemKind::Fn(box ast::FnKind(def, sig, gen, body)) => {
10261026
self.print_fn_full(sig, ident, gen, vis, *def, body.as_deref(), attrs);
10271027
}
1028-
ast::ForeignItemKind::Static(ty, mutbl, body) => {
1028+
ast::ForeignItemKind::Static(box ast::StaticKind(ty, mutbl, body)) => {
10291029
let def = ast::Defaultness::Final;
10301030
self.print_item_const(ident, Some(*mutbl), ty, body.as_deref(), vis, def);
10311031
}
@@ -1127,11 +1127,11 @@ impl<'a> State<'a> {
11271127
self.end(); // end inner head-block
11281128
self.end(); // end outer head-block
11291129
}
1130-
ast::ItemKind::Static(ref ty, mutbl, ref body) => {
1130+
ast::ItemKind::Static(box ast::StaticKind(ref ty, mutbl, ref body)) => {
11311131
let def = ast::Defaultness::Final;
11321132
self.print_item_const(item.ident, Some(mutbl), ty, body.as_deref(), &item.vis, def);
11331133
}
1134-
ast::ItemKind::Const(def, ref ty, ref body) => {
1134+
ast::ItemKind::Const(box ast::ConstKind(def, ref ty, ref body)) => {
11351135
self.print_item_const(item.ident, None, ty, body.as_deref(), &item.vis, def);
11361136
}
11371137
ast::ItemKind::Fn(box ast::FnKind(def, ref sig, ref gen, ref body)) => {
@@ -1462,7 +1462,7 @@ impl<'a> State<'a> {
14621462
ast::AssocItemKind::Fn(box ast::FnKind(def, sig, gen, body)) => {
14631463
self.print_fn_full(sig, ident, gen, vis, *def, body.as_deref(), attrs);
14641464
}
1465-
ast::AssocItemKind::Const(def, ty, body) => {
1465+
ast::AssocItemKind::Const(box ast::ConstKind(def, ty, body)) => {
14661466
self.print_item_const(ident, None, ty, body.as_deref(), vis, *def);
14671467
}
14681468
ast::AssocItemKind::TyAlias(box ast::TyAliasKind(def, generics, bounds, ty)) => {

compiler/rustc_builtin_macros/src/test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ pub fn expand_test_or_bench(
212212
cx.attribute(cx.meta_word(attr_sp, sym::rustc_test_marker)),
213213
],
214214
// const $ident: test::TestDescAndFn =
215-
ast::ItemKind::Const(
215+
ast::ItemKind::Const(box ast::ConstKind(
216216
ast::Defaultness::Final,
217217
cx.ty(sp, ast::TyKind::Path(None, test_path("TestDescAndFn"))),
218218
// test::TestDescAndFn {
@@ -301,7 +301,7 @@ pub fn expand_test_or_bench(
301301
],
302302
), // }
303303
),
304-
),
304+
)),
305305
);
306306
test_const = test_const.map(|mut tc| {
307307
tc.vis.kind = ast::VisibilityKind::Public;

compiler/rustc_expand/src/build.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,12 @@ impl<'a> ExtCtxt<'a> {
548548
mutbl: ast::Mutability,
549549
expr: P<ast::Expr>,
550550
) -> P<ast::Item> {
551-
self.item(span, name, Vec::new(), ast::ItemKind::Static(ty, mutbl, Some(expr)))
551+
self.item(
552+
span,
553+
name,
554+
Vec::new(),
555+
ast::ItemKind::Static(box ast::StaticKind(ty, mutbl, Some(expr))),
556+
)
552557
}
553558

554559
pub fn item_const(
@@ -559,7 +564,12 @@ impl<'a> ExtCtxt<'a> {
559564
expr: P<ast::Expr>,
560565
) -> P<ast::Item> {
561566
let def = ast::Defaultness::Final;
562-
self.item(span, name, Vec::new(), ast::ItemKind::Const(def, ty, Some(expr)))
567+
self.item(
568+
span,
569+
name,
570+
Vec::new(),
571+
ast::ItemKind::Const(box ast::ConstKind(def, ty, Some(expr))),
572+
)
563573
}
564574

565575
pub fn attribute(&self, mi: ast::MetaItem) -> ast::Attribute {

compiler/rustc_expand/src/expand.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,7 +1373,7 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
13731373
item.attrs = attrs;
13741374
self.check_attributes(&item.attrs);
13751375
item.and_then(|item| match item.kind {
1376-
ItemKind::MacCall(mac) => {
1376+
ItemKind::MacCall(box mac) => {
13771377
self.collect_bang(mac, span, AstFragmentKind::Items).make_items()
13781378
}
13791379
_ => unreachable!(),
@@ -1464,7 +1464,7 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
14641464
ast::AssocItemKind::MacCall(..) => {
14651465
self.check_attributes(&item.attrs);
14661466
item.and_then(|item| match item.kind {
1467-
ast::AssocItemKind::MacCall(mac) => self
1467+
ast::AssocItemKind::MacCall(box mac) => self
14681468
.collect_bang(mac, item.span, AstFragmentKind::TraitItems)
14691469
.make_trait_items(),
14701470
_ => unreachable!(),
@@ -1487,7 +1487,7 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
14871487
ast::AssocItemKind::MacCall(..) => {
14881488
self.check_attributes(&item.attrs);
14891489
item.and_then(|item| match item.kind {
1490-
ast::AssocItemKind::MacCall(mac) => self
1490+
ast::AssocItemKind::MacCall(box mac) => self
14911491
.collect_bang(mac, item.span, AstFragmentKind::ImplItems)
14921492
.make_impl_items(),
14931493
_ => unreachable!(),
@@ -1534,7 +1534,7 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
15341534
ast::ForeignItemKind::MacCall(..) => {
15351535
self.check_attributes(&foreign_item.attrs);
15361536
foreign_item.and_then(|item| match item.kind {
1537-
ast::ForeignItemKind::MacCall(mac) => self
1537+
ast::ForeignItemKind::MacCall(box mac) => self
15381538
.collect_bang(mac, item.span, AstFragmentKind::ForeignItems)
15391539
.make_foreign_items(),
15401540
_ => unreachable!(),

compiler/rustc_expand/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#![feature(bool_to_option)]
2+
#![feature(box_patterns)]
3+
#![feature(box_syntax)]
24
#![feature(crate_visibility_modifier)]
35
#![feature(decl_macro)]
46
#![feature(or_patterns)]

0 commit comments

Comments
 (0)