diff options
| author | bors <bors@rust-lang.org> | 2022-08-16 07:54:22 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2022-08-16 07:54:22 +0000 |
| commit | 14a459bf37bc19476d43e0045d078121c12d3fef (patch) | |
| tree | 36e797a07bf83ddd338f6bb369f3ea09d4a384ea /compiler | |
| parent | 8556e6620e4866526b3cea767ad8c20ae877a569 (diff) | |
| parent | 85a6cd6a47ea0f7129dff5038fce7398ceb18222 (diff) | |
| download | rust-14a459bf37bc19476d43e0045d078121c12d3fef.tar.gz rust-14a459bf37bc19476d43e0045d078121c12d3fef.zip | |
Auto merge of #100441 - nnethercote:shrink-ast-Attribute, r=petrochenkov
Shrink `ast::Attribute`. r? `@ghost`
Diffstat (limited to 'compiler')
| -rw-r--r-- | compiler/rustc_ast/src/ast.rs | 10 | ||||
| -rw-r--r-- | compiler/rustc_ast/src/ast_traits.rs | 4 | ||||
| -rw-r--r-- | compiler/rustc_ast/src/attr/mod.rs | 43 | ||||
| -rw-r--r-- | compiler/rustc_ast/src/mut_visit.rs | 8 | ||||
| -rw-r--r-- | compiler/rustc_ast/src/visit.rs | 2 | ||||
| -rw-r--r-- | compiler/rustc_ast_lowering/src/lib.rs | 13 | ||||
| -rw-r--r-- | compiler/rustc_ast_pretty/src/pprust/state.rs | 4 | ||||
| -rw-r--r-- | compiler/rustc_query_system/src/ich/impls_syntax.rs | 6 |
8 files changed, 54 insertions, 36 deletions
diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs index 3f71fce0e3b..3a55396248e 100644 --- a/compiler/rustc_ast/src/ast.rs +++ b/compiler/rustc_ast/src/ast.rs @@ -2548,9 +2548,15 @@ pub struct Attribute { } #[derive(Clone, Encodable, Decodable, Debug)] +pub struct NormalAttr { + pub item: AttrItem, + pub tokens: Option<LazyTokenStream>, +} + +#[derive(Clone, Encodable, Decodable, Debug)] pub enum AttrKind { /// A normal attribute. - Normal(AttrItem, Option<LazyTokenStream>), + Normal(P<NormalAttr>), /// A doc comment (e.g. `/// ...`, `//! ...`, `/** ... */`, `/*! ... */`). /// Doc attributes (e.g. `#[doc="..."]`) are represented with the `Normal` @@ -3033,7 +3039,7 @@ mod size_asserts { // These are in alphabetical order, which is easy to maintain. static_assert_size!(AssocItem, 160); static_assert_size!(AssocItemKind, 72); - static_assert_size!(Attribute, 152); + static_assert_size!(Attribute, 32); static_assert_size!(Block, 48); static_assert_size!(Expr, 104); static_assert_size!(Fn, 192); diff --git a/compiler/rustc_ast/src/ast_traits.rs b/compiler/rustc_ast/src/ast_traits.rs index 5c30a75a140..1fc5e480215 100644 --- a/compiler/rustc_ast/src/ast_traits.rs +++ b/compiler/rustc_ast/src/ast_traits.rs @@ -212,7 +212,7 @@ impl HasTokens for Stmt { impl HasTokens for Attribute { fn tokens(&self) -> Option<&LazyTokenStream> { match &self.kind { - AttrKind::Normal(_, tokens) => tokens.as_ref(), + AttrKind::Normal(normal) => normal.tokens.as_ref(), kind @ AttrKind::DocComment(..) => { panic!("Called tokens on doc comment attr {:?}", kind) } @@ -220,7 +220,7 @@ impl HasTokens for Attribute { } fn tokens_mut(&mut self) -> Option<&mut Option<LazyTokenStream>> { Some(match &mut self.kind { - AttrKind::Normal(_, tokens) => tokens, + AttrKind::Normal(normal) => &mut normal.tokens, kind @ AttrKind::DocComment(..) => { panic!("Called tokens_mut on doc comment attr {:?}", kind) } diff --git a/compiler/rustc_ast/src/attr/mod.rs b/compiler/rustc_ast/src/attr/mod.rs index 86af7769d1b..bc4f183d9b1 100644 --- a/compiler/rustc_ast/src/attr/mod.rs +++ b/compiler/rustc_ast/src/attr/mod.rs @@ -114,7 +114,7 @@ impl Attribute { #[inline] pub fn has_name(&self, name: Symbol) -> bool { match self.kind { - AttrKind::Normal(ref item, _) => item.path == name, + AttrKind::Normal(ref normal) => normal.item.path == name, AttrKind::DocComment(..) => false, } } @@ -122,9 +122,9 @@ impl Attribute { /// For a single-segment attribute, returns its name; otherwise, returns `None`. pub fn ident(&self) -> Option<Ident> { match self.kind { - AttrKind::Normal(ref item, _) => { - if item.path.segments.len() == 1 { - Some(item.path.segments[0].ident) + AttrKind::Normal(ref normal) => { + if normal.item.path.segments.len() == 1 { + Some(normal.item.path.segments[0].ident) } else { None } @@ -138,14 +138,16 @@ impl Attribute { pub fn value_str(&self) -> Option<Symbol> { match self.kind { - AttrKind::Normal(ref item, _) => item.meta_kind().and_then(|kind| kind.value_str()), + AttrKind::Normal(ref normal) => { + normal.item.meta_kind().and_then(|kind| kind.value_str()) + } AttrKind::DocComment(..) => None, } } pub fn meta_item_list(&self) -> Option<Vec<NestedMetaItem>> { match self.kind { - AttrKind::Normal(ref item, _) => match item.meta_kind() { + AttrKind::Normal(ref normal) => match normal.item.meta_kind() { Some(MetaItemKind::List(list)) => Some(list), _ => None, }, @@ -154,8 +156,8 @@ impl Attribute { } pub fn is_word(&self) -> bool { - if let AttrKind::Normal(item, _) = &self.kind { - matches!(item.args, MacArgs::Empty) + if let AttrKind::Normal(normal) = &self.kind { + matches!(normal.item.args, MacArgs::Empty) } else { false } @@ -247,7 +249,8 @@ impl Attribute { pub fn doc_str_and_comment_kind(&self) -> Option<(Symbol, CommentKind)> { match self.kind { AttrKind::DocComment(kind, data) => Some((data, kind)), - AttrKind::Normal(ref item, _) if item.path == sym::doc => item + AttrKind::Normal(ref normal) if normal.item.path == sym::doc => normal + .item .meta_kind() .and_then(|kind| kind.value_str()) .map(|data| (data, CommentKind::Line)), @@ -258,8 +261,8 @@ impl Attribute { pub fn doc_str(&self) -> Option<Symbol> { match self.kind { AttrKind::DocComment(.., data) => Some(data), - AttrKind::Normal(ref item, _) if item.path == sym::doc => { - item.meta_kind().and_then(|kind| kind.value_str()) + AttrKind::Normal(ref normal) if normal.item.path == sym::doc => { + normal.item.meta_kind().and_then(|kind| kind.value_str()) } _ => None, } @@ -271,14 +274,14 @@ impl Attribute { pub fn get_normal_item(&self) -> &AttrItem { match self.kind { - AttrKind::Normal(ref item, _) => item, + AttrKind::Normal(ref normal) => &normal.item, AttrKind::DocComment(..) => panic!("unexpected doc comment"), } } pub fn unwrap_normal_item(self) -> AttrItem { match self.kind { - AttrKind::Normal(item, _) => item, + AttrKind::Normal(normal) => normal.into_inner().item, AttrKind::DocComment(..) => panic!("unexpected doc comment"), } } @@ -286,21 +289,22 @@ impl Attribute { /// Extracts the MetaItem from inside this Attribute. pub fn meta(&self) -> Option<MetaItem> { match self.kind { - AttrKind::Normal(ref item, _) => item.meta(self.span), + AttrKind::Normal(ref normal) => normal.item.meta(self.span), AttrKind::DocComment(..) => None, } } pub fn meta_kind(&self) -> Option<MetaItemKind> { match self.kind { - AttrKind::Normal(ref item, _) => item.meta_kind(), + AttrKind::Normal(ref normal) => normal.item.meta_kind(), AttrKind::DocComment(..) => None, } } pub fn tokens(&self) -> AttrAnnotatedTokenStream { match self.kind { - AttrKind::Normal(_, ref tokens) => tokens + AttrKind::Normal(ref normal) => normal + .tokens .as_ref() .unwrap_or_else(|| panic!("attribute is missing tokens: {:?}", self)) .create_token_stream(), @@ -361,7 +365,12 @@ pub fn mk_attr_from_item( style: AttrStyle, span: Span, ) -> Attribute { - Attribute { kind: AttrKind::Normal(item, tokens), id: mk_attr_id(), style, span } + Attribute { + kind: AttrKind::Normal(P(ast::NormalAttr { item, tokens })), + id: mk_attr_id(), + style, + span, + } } /// Returns an inner attribute with the given value and span. diff --git a/compiler/rustc_ast/src/mut_visit.rs b/compiler/rustc_ast/src/mut_visit.rs index 40c05f43f68..54e2f7557e5 100644 --- a/compiler/rustc_ast/src/mut_visit.rs +++ b/compiler/rustc_ast/src/mut_visit.rs @@ -596,7 +596,9 @@ pub fn noop_visit_local<T: MutVisitor>(local: &mut P<Local>, vis: &mut T) { pub fn noop_visit_attribute<T: MutVisitor>(attr: &mut Attribute, vis: &mut T) { let Attribute { kind, id: _, style: _, span } = attr; match kind { - AttrKind::Normal(AttrItem { path, args, tokens }, attr_tokens) => { + AttrKind::Normal(normal) => { + let NormalAttr { item: AttrItem { path, args, tokens }, tokens: attr_tokens } = + &mut **normal; vis.visit_path(path); visit_mac_args(args, vis); visit_lazy_tts(tokens, vis); @@ -659,8 +661,8 @@ pub fn visit_attr_annotated_tt<T: MutVisitor>(tt: &mut AttrAnnotatedTokenTree, v AttrAnnotatedTokenTree::Attributes(data) => { for attr in &mut *data.attrs { match &mut attr.kind { - AttrKind::Normal(_, attr_tokens) => { - visit_lazy_tts(attr_tokens, vis); + AttrKind::Normal(normal) => { + visit_lazy_tts(&mut normal.tokens, vis); } AttrKind::DocComment(..) => { vis.visit_span(&mut attr.span); diff --git a/compiler/rustc_ast/src/visit.rs b/compiler/rustc_ast/src/visit.rs index 4b485b547f4..f687bfeced8 100644 --- a/compiler/rustc_ast/src/visit.rs +++ b/compiler/rustc_ast/src/visit.rs @@ -929,7 +929,7 @@ pub fn walk_vis<'a, V: Visitor<'a>>(visitor: &mut V, vis: &'a Visibility) { pub fn walk_attribute<'a, V: Visitor<'a>>(visitor: &mut V, attr: &'a Attribute) { match attr.kind { - AttrKind::Normal(ref item, ref _tokens) => walk_mac_args(visitor, &item.args), + AttrKind::Normal(ref normal) => walk_mac_args(visitor, &normal.item.args), AttrKind::DocComment(..) => {} } } diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index 488bd05fd6b..5f5667ce905 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -39,6 +39,7 @@ #[macro_use] extern crate tracing; +use rustc_ast::ptr::P; use rustc_ast::visit; use rustc_ast::{self as ast, *}; use rustc_ast_pretty::pprust; @@ -871,14 +872,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { // the `HirId`s. We don't actually need HIR version of attributes anyway. // Tokens are also not needed after macro expansion and parsing. let kind = match attr.kind { - AttrKind::Normal(ref item, _) => AttrKind::Normal( - AttrItem { - path: item.path.clone(), - args: self.lower_mac_args(&item.args), + AttrKind::Normal(ref normal) => AttrKind::Normal(P(NormalAttr { + item: AttrItem { + path: normal.item.path.clone(), + args: self.lower_mac_args(&normal.item.args), tokens: None, }, - None, - ), + tokens: None, + })), AttrKind::DocComment(comment_kind, data) => AttrKind::DocComment(comment_kind, data), }; diff --git a/compiler/rustc_ast_pretty/src/pprust/state.rs b/compiler/rustc_ast_pretty/src/pprust/state.rs index 5eb7bf6347f..c454034efa7 100644 --- a/compiler/rustc_ast_pretty/src/pprust/state.rs +++ b/compiler/rustc_ast_pretty/src/pprust/state.rs @@ -442,12 +442,12 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere } self.maybe_print_comment(attr.span.lo()); match attr.kind { - ast::AttrKind::Normal(ref item, _) => { + ast::AttrKind::Normal(ref normal) => { match attr.style { ast::AttrStyle::Inner => self.word("#!["), ast::AttrStyle::Outer => self.word("#["), } - self.print_attr_item(&item, attr.span); + self.print_attr_item(&normal.item, attr.span); self.word("]"); } ast::AttrKind::DocComment(comment_kind, data) => { diff --git a/compiler/rustc_query_system/src/ich/impls_syntax.rs b/compiler/rustc_query_system/src/ich/impls_syntax.rs index 1fa08592676..d7732cb1825 100644 --- a/compiler/rustc_query_system/src/ich/impls_syntax.rs +++ b/compiler/rustc_query_system/src/ich/impls_syntax.rs @@ -42,12 +42,12 @@ impl<'ctx> rustc_ast::HashStableContext for StableHashingContext<'ctx> { debug_assert!(!attr.is_doc_comment()); let ast::Attribute { kind, id: _, style, span } = attr; - if let ast::AttrKind::Normal(item, tokens) = kind { - item.hash_stable(self, hasher); + if let ast::AttrKind::Normal(normal) = kind { + normal.item.hash_stable(self, hasher); style.hash_stable(self, hasher); span.hash_stable(self, hasher); assert_matches!( - tokens.as_ref(), + normal.tokens.as_ref(), None, "Tokens should have been removed during lowering!" ); |
