Skip to content

Commit 85a6cd6

Browse files
committed
Shrink ast::Attribute.
1 parent 4033686 commit 85a6cd6

File tree

15 files changed

+167
-149
lines changed

15 files changed

+167
-149
lines changed

compiler/rustc_ast/src/ast.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -2547,10 +2547,16 @@ pub struct Attribute {
25472547
pub span: Span,
25482548
}
25492549

2550+
#[derive(Clone, Encodable, Decodable, Debug)]
2551+
pub struct NormalAttr {
2552+
pub item: AttrItem,
2553+
pub tokens: Option<LazyTokenStream>,
2554+
}
2555+
25502556
#[derive(Clone, Encodable, Decodable, Debug)]
25512557
pub enum AttrKind {
25522558
/// A normal attribute.
2553-
Normal(AttrItem, Option<LazyTokenStream>),
2559+
Normal(P<NormalAttr>),
25542560

25552561
/// A doc comment (e.g. `/// ...`, `//! ...`, `/** ... */`, `/*! ... */`).
25562562
/// Doc attributes (e.g. `#[doc="..."]`) are represented with the `Normal`
@@ -3033,7 +3039,7 @@ mod size_asserts {
30333039
// These are in alphabetical order, which is easy to maintain.
30343040
static_assert_size!(AssocItem, 160);
30353041
static_assert_size!(AssocItemKind, 72);
3036-
static_assert_size!(Attribute, 152);
3042+
static_assert_size!(Attribute, 32);
30373043
static_assert_size!(Block, 48);
30383044
static_assert_size!(Expr, 104);
30393045
static_assert_size!(Fn, 192);

compiler/rustc_ast/src/ast_traits.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -212,15 +212,15 @@ impl HasTokens for Stmt {
212212
impl HasTokens for Attribute {
213213
fn tokens(&self) -> Option<&LazyTokenStream> {
214214
match &self.kind {
215-
AttrKind::Normal(_, tokens) => tokens.as_ref(),
215+
AttrKind::Normal(normal) => normal.tokens.as_ref(),
216216
kind @ AttrKind::DocComment(..) => {
217217
panic!("Called tokens on doc comment attr {:?}", kind)
218218
}
219219
}
220220
}
221221
fn tokens_mut(&mut self) -> Option<&mut Option<LazyTokenStream>> {
222222
Some(match &mut self.kind {
223-
AttrKind::Normal(_, tokens) => tokens,
223+
AttrKind::Normal(normal) => &mut normal.tokens,
224224
kind @ AttrKind::DocComment(..) => {
225225
panic!("Called tokens_mut on doc comment attr {:?}", kind)
226226
}

compiler/rustc_ast/src/attr/mod.rs

+26-17
Original file line numberDiff line numberDiff line change
@@ -114,17 +114,17 @@ impl Attribute {
114114
#[inline]
115115
pub fn has_name(&self, name: Symbol) -> bool {
116116
match self.kind {
117-
AttrKind::Normal(ref item, _) => item.path == name,
117+
AttrKind::Normal(ref normal) => normal.item.path == name,
118118
AttrKind::DocComment(..) => false,
119119
}
120120
}
121121

122122
/// For a single-segment attribute, returns its name; otherwise, returns `None`.
123123
pub fn ident(&self) -> Option<Ident> {
124124
match self.kind {
125-
AttrKind::Normal(ref item, _) => {
126-
if item.path.segments.len() == 1 {
127-
Some(item.path.segments[0].ident)
125+
AttrKind::Normal(ref normal) => {
126+
if normal.item.path.segments.len() == 1 {
127+
Some(normal.item.path.segments[0].ident)
128128
} else {
129129
None
130130
}
@@ -138,14 +138,16 @@ impl Attribute {
138138

139139
pub fn value_str(&self) -> Option<Symbol> {
140140
match self.kind {
141-
AttrKind::Normal(ref item, _) => item.meta_kind().and_then(|kind| kind.value_str()),
141+
AttrKind::Normal(ref normal) => {
142+
normal.item.meta_kind().and_then(|kind| kind.value_str())
143+
}
142144
AttrKind::DocComment(..) => None,
143145
}
144146
}
145147

146148
pub fn meta_item_list(&self) -> Option<Vec<NestedMetaItem>> {
147149
match self.kind {
148-
AttrKind::Normal(ref item, _) => match item.meta_kind() {
150+
AttrKind::Normal(ref normal) => match normal.item.meta_kind() {
149151
Some(MetaItemKind::List(list)) => Some(list),
150152
_ => None,
151153
},
@@ -154,8 +156,8 @@ impl Attribute {
154156
}
155157

156158
pub fn is_word(&self) -> bool {
157-
if let AttrKind::Normal(item, _) = &self.kind {
158-
matches!(item.args, MacArgs::Empty)
159+
if let AttrKind::Normal(normal) = &self.kind {
160+
matches!(normal.item.args, MacArgs::Empty)
159161
} else {
160162
false
161163
}
@@ -247,7 +249,8 @@ impl Attribute {
247249
pub fn doc_str_and_comment_kind(&self) -> Option<(Symbol, CommentKind)> {
248250
match self.kind {
249251
AttrKind::DocComment(kind, data) => Some((data, kind)),
250-
AttrKind::Normal(ref item, _) if item.path == sym::doc => item
252+
AttrKind::Normal(ref normal) if normal.item.path == sym::doc => normal
253+
.item
251254
.meta_kind()
252255
.and_then(|kind| kind.value_str())
253256
.map(|data| (data, CommentKind::Line)),
@@ -258,8 +261,8 @@ impl Attribute {
258261
pub fn doc_str(&self) -> Option<Symbol> {
259262
match self.kind {
260263
AttrKind::DocComment(.., data) => Some(data),
261-
AttrKind::Normal(ref item, _) if item.path == sym::doc => {
262-
item.meta_kind().and_then(|kind| kind.value_str())
264+
AttrKind::Normal(ref normal) if normal.item.path == sym::doc => {
265+
normal.item.meta_kind().and_then(|kind| kind.value_str())
263266
}
264267
_ => None,
265268
}
@@ -271,36 +274,37 @@ impl Attribute {
271274

272275
pub fn get_normal_item(&self) -> &AttrItem {
273276
match self.kind {
274-
AttrKind::Normal(ref item, _) => item,
277+
AttrKind::Normal(ref normal) => &normal.item,
275278
AttrKind::DocComment(..) => panic!("unexpected doc comment"),
276279
}
277280
}
278281

279282
pub fn unwrap_normal_item(self) -> AttrItem {
280283
match self.kind {
281-
AttrKind::Normal(item, _) => item,
284+
AttrKind::Normal(normal) => normal.into_inner().item,
282285
AttrKind::DocComment(..) => panic!("unexpected doc comment"),
283286
}
284287
}
285288

286289
/// Extracts the MetaItem from inside this Attribute.
287290
pub fn meta(&self) -> Option<MetaItem> {
288291
match self.kind {
289-
AttrKind::Normal(ref item, _) => item.meta(self.span),
292+
AttrKind::Normal(ref normal) => normal.item.meta(self.span),
290293
AttrKind::DocComment(..) => None,
291294
}
292295
}
293296

294297
pub fn meta_kind(&self) -> Option<MetaItemKind> {
295298
match self.kind {
296-
AttrKind::Normal(ref item, _) => item.meta_kind(),
299+
AttrKind::Normal(ref normal) => normal.item.meta_kind(),
297300
AttrKind::DocComment(..) => None,
298301
}
299302
}
300303

301304
pub fn tokens(&self) -> AttrAnnotatedTokenStream {
302305
match self.kind {
303-
AttrKind::Normal(_, ref tokens) => tokens
306+
AttrKind::Normal(ref normal) => normal
307+
.tokens
304308
.as_ref()
305309
.unwrap_or_else(|| panic!("attribute is missing tokens: {:?}", self))
306310
.create_token_stream(),
@@ -361,7 +365,12 @@ pub fn mk_attr_from_item(
361365
style: AttrStyle,
362366
span: Span,
363367
) -> Attribute {
364-
Attribute { kind: AttrKind::Normal(item, tokens), id: mk_attr_id(), style, span }
368+
Attribute {
369+
kind: AttrKind::Normal(P(ast::NormalAttr { item, tokens })),
370+
id: mk_attr_id(),
371+
style,
372+
span,
373+
}
365374
}
366375

367376
/// Returns an inner attribute with the given value and span.

compiler/rustc_ast/src/mut_visit.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,9 @@ pub fn noop_visit_local<T: MutVisitor>(local: &mut P<Local>, vis: &mut T) {
596596
pub fn noop_visit_attribute<T: MutVisitor>(attr: &mut Attribute, vis: &mut T) {
597597
let Attribute { kind, id: _, style: _, span } = attr;
598598
match kind {
599-
AttrKind::Normal(AttrItem { path, args, tokens }, attr_tokens) => {
599+
AttrKind::Normal(normal) => {
600+
let NormalAttr { item: AttrItem { path, args, tokens }, tokens: attr_tokens } =
601+
&mut **normal;
600602
vis.visit_path(path);
601603
visit_mac_args(args, vis);
602604
visit_lazy_tts(tokens, vis);
@@ -659,8 +661,8 @@ pub fn visit_attr_annotated_tt<T: MutVisitor>(tt: &mut AttrAnnotatedTokenTree, v
659661
AttrAnnotatedTokenTree::Attributes(data) => {
660662
for attr in &mut *data.attrs {
661663
match &mut attr.kind {
662-
AttrKind::Normal(_, attr_tokens) => {
663-
visit_lazy_tts(attr_tokens, vis);
664+
AttrKind::Normal(normal) => {
665+
visit_lazy_tts(&mut normal.tokens, vis);
664666
}
665667
AttrKind::DocComment(..) => {
666668
vis.visit_span(&mut attr.span);

compiler/rustc_ast/src/visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -929,7 +929,7 @@ pub fn walk_vis<'a, V: Visitor<'a>>(visitor: &mut V, vis: &'a Visibility) {
929929

930930
pub fn walk_attribute<'a, V: Visitor<'a>>(visitor: &mut V, attr: &'a Attribute) {
931931
match attr.kind {
932-
AttrKind::Normal(ref item, ref _tokens) => walk_mac_args(visitor, &item.args),
932+
AttrKind::Normal(ref normal) => walk_mac_args(visitor, &normal.item.args),
933933
AttrKind::DocComment(..) => {}
934934
}
935935
}

compiler/rustc_ast_lowering/src/lib.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#[macro_use]
4040
extern crate tracing;
4141

42+
use rustc_ast::ptr::P;
4243
use rustc_ast::visit;
4344
use rustc_ast::{self as ast, *};
4445
use rustc_ast_pretty::pprust;
@@ -873,14 +874,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
873874
// the `HirId`s. We don't actually need HIR version of attributes anyway.
874875
// Tokens are also not needed after macro expansion and parsing.
875876
let kind = match attr.kind {
876-
AttrKind::Normal(ref item, _) => AttrKind::Normal(
877-
AttrItem {
878-
path: item.path.clone(),
879-
args: self.lower_mac_args(&item.args),
877+
AttrKind::Normal(ref normal) => AttrKind::Normal(P(NormalAttr {
878+
item: AttrItem {
879+
path: normal.item.path.clone(),
880+
args: self.lower_mac_args(&normal.item.args),
880881
tokens: None,
881882
},
882-
None,
883-
),
883+
tokens: None,
884+
})),
884885
AttrKind::DocComment(comment_kind, data) => AttrKind::DocComment(comment_kind, data),
885886
};
886887

compiler/rustc_ast_pretty/src/pprust/state.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -442,12 +442,12 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
442442
}
443443
self.maybe_print_comment(attr.span.lo());
444444
match attr.kind {
445-
ast::AttrKind::Normal(ref item, _) => {
445+
ast::AttrKind::Normal(ref normal) => {
446446
match attr.style {
447447
ast::AttrStyle::Inner => self.word("#!["),
448448
ast::AttrStyle::Outer => self.word("#["),
449449
}
450-
self.print_attr_item(&item, attr.span);
450+
self.print_attr_item(&normal.item, attr.span);
451451
self.word("]");
452452
}
453453
ast::AttrKind::DocComment(comment_kind, data) => {

compiler/rustc_query_system/src/ich/impls_syntax.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ impl<'ctx> rustc_ast::HashStableContext for StableHashingContext<'ctx> {
4242
debug_assert!(!attr.is_doc_comment());
4343

4444
let ast::Attribute { kind, id: _, style, span } = attr;
45-
if let ast::AttrKind::Normal(item, tokens) = kind {
46-
item.hash_stable(self, hasher);
45+
if let ast::AttrKind::Normal(normal) = kind {
46+
normal.item.hash_stable(self, hasher);
4747
style.hash_stable(self, hasher);
4848
span.hash_stable(self, hasher);
4949
assert_matches!(
50-
tokens.as_ref(),
50+
normal.tokens.as_ref(),
5151
None,
5252
"Tokens should have been removed during lowering!"
5353
);

0 commit comments

Comments
 (0)