Skip to content

Commit 535d474

Browse files
committed
syntax: Split ast::Attribute into container and inner parts
1 parent 22bc9e1 commit 535d474

File tree

8 files changed

+39
-27
lines changed

8 files changed

+39
-27
lines changed

src/librustc/hir/lowering.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -988,10 +988,12 @@ impl<'a> LoweringContext<'a> {
988988
// lower attributes (we use the AST version) there is nowhere to keep
989989
// the `HirId`s. We don't actually need HIR version of attributes anyway.
990990
Attribute {
991+
item: AttrItem {
992+
path: attr.path.clone(),
993+
tokens: self.lower_token_stream(attr.tokens.clone()),
994+
},
991995
id: attr.id,
992996
style: attr.style,
993-
path: attr.path.clone(),
994-
tokens: self.lower_token_stream(attr.tokens.clone()),
995997
is_sugared_doc: attr.is_sugared_doc,
996998
span: attr.span,
997999
}

src/librustc/ich/impls_syntax.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -196,26 +196,27 @@ impl<'a> HashStable<StableHashingContext<'a>> for ast::Path {
196196
}
197197
}
198198

199+
impl_stable_hash_for!(struct ::syntax::ast::AttrItem {
200+
path,
201+
tokens,
202+
});
203+
199204
impl<'a> HashStable<StableHashingContext<'a>> for ast::Attribute {
200205
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
201206
// Make sure that these have been filtered out.
202207
debug_assert!(!self.ident().map_or(false, |ident| hcx.is_ignored_attr(ident.name)));
203208
debug_assert!(!self.is_sugared_doc);
204209

205210
let ast::Attribute {
211+
ref item,
206212
id: _,
207213
style,
208-
ref path,
209-
ref tokens,
210214
is_sugared_doc: _,
211215
span,
212216
} = *self;
213217

218+
item.hash_stable(hcx, hasher);
214219
style.hash_stable(hcx, hasher);
215-
path.hash_stable(hcx, hasher);
216-
for tt in tokens.trees() {
217-
tt.hash_stable(hcx, hasher);
218-
}
219220
span.hash_stable(hcx, hasher);
220221
}
221222
}

src/libsyntax/ast.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -2139,18 +2139,28 @@ impl rustc_serialize::Decodable for AttrId {
21392139
}
21402140
}
21412141

2142+
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2143+
pub struct AttrItem {
2144+
pub path: Path,
2145+
pub tokens: TokenStream,
2146+
}
2147+
21422148
/// Metadata associated with an item.
21432149
/// Doc-comments are promoted to attributes that have `is_sugared_doc = true`.
21442150
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
21452151
pub struct Attribute {
2152+
pub item: AttrItem,
21462153
pub id: AttrId,
21472154
pub style: AttrStyle,
2148-
pub path: Path,
2149-
pub tokens: TokenStream,
21502155
pub is_sugared_doc: bool,
21512156
pub span: Span,
21522157
}
21532158

2159+
impl std::ops::Deref for Attribute {
2160+
type Target = AttrItem;
2161+
fn deref(&self) -> &Self::Target { &self.item }
2162+
}
2163+
21542164
/// `TraitRef`s appear in impls.
21552165
///
21562166
/// Resolution maps each `TraitRef`'s `ref_id` to its defining trait; that's all

src/libsyntax/attr/mod.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub use StabilityLevel::*;
99
pub use crate::ast::Attribute;
1010

1111
use crate::ast;
12-
use crate::ast::{AttrId, AttrStyle, Name, Ident, Path, PathSegment};
12+
use crate::ast::{AttrItem, AttrId, AttrStyle, Name, Ident, Path, PathSegment};
1313
use crate::ast::{MetaItem, MetaItemKind, NestedMetaItem};
1414
use crate::ast::{Lit, LitKind, Expr, Item, Local, Stmt, StmtKind, GenericParam};
1515
use crate::mut_visit::visit_clobber;
@@ -333,10 +333,9 @@ impl Attribute {
333333
DUMMY_SP,
334334
);
335335
f(&Attribute {
336+
item: AttrItem { path: meta.path, tokens: meta.kind.tokens(meta.span) },
336337
id: self.id,
337338
style: self.style,
338-
path: meta.path,
339-
tokens: meta.kind.tokens(meta.span),
340339
is_sugared_doc: true,
341340
span: self.span,
342341
})
@@ -384,10 +383,9 @@ crate fn mk_attr_id() -> AttrId {
384383

385384
pub fn mk_attr(style: AttrStyle, path: Path, tokens: TokenStream, span: Span) -> Attribute {
386385
Attribute {
386+
item: AttrItem { path, tokens },
387387
id: mk_attr_id(),
388388
style,
389-
path,
390-
tokens,
391389
is_sugared_doc: false,
392390
span,
393391
}
@@ -408,10 +406,12 @@ pub fn mk_sugared_doc_attr(text: Symbol, span: Span) -> Attribute {
408406
let lit_kind = LitKind::Str(text, ast::StrStyle::Cooked);
409407
let lit = Lit::from_lit_kind(lit_kind, span);
410408
Attribute {
409+
item: AttrItem {
410+
path: Path::from_ident(Ident::with_dummy_span(sym::doc).with_span_pos(span)),
411+
tokens: MetaItemKind::NameValue(lit).tokens(span),
412+
},
411413
id: mk_attr_id(),
412414
style,
413-
path: Path::from_ident(Ident::with_dummy_span(sym::doc).with_span_pos(span)),
414-
tokens: MetaItemKind::NameValue(lit).tokens(span),
415415
is_sugared_doc: true,
416416
span,
417417
}

src/libsyntax/config.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,9 @@ impl<'a> StripUnconfigured<'a> {
151151
// `#[cfg_attr(false, cfg_attr(true, some_attr))]`.
152152
expanded_attrs.into_iter()
153153
.flat_map(|(path, tokens, span)| self.process_cfg_attr(ast::Attribute {
154+
item: ast::AttrItem { path, tokens },
154155
id: attr::mk_attr_id(),
155156
style: attr.style,
156-
path,
157-
tokens,
158157
is_sugared_doc: false,
159158
span,
160159
}))

src/libsyntax/ext/expand.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::ast::{self, Block, Ident, LitKind, NodeId, PatKind, Path};
1+
use crate::ast::{self, AttrItem, Block, Ident, LitKind, NodeId, PatKind, Path};
22
use crate::ast::{MacStmtStyle, StmtKind, ItemKind};
33
use crate::attr::{self, HasAttrs};
44
use crate::source_map::respan;
@@ -625,9 +625,10 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
625625
| Annotatable::Variant(..)
626626
=> panic!("unexpected annotatable"),
627627
})), DUMMY_SP).into();
628-
let input = self.extract_proc_macro_attr_input(attr.tokens, span);
628+
let input = self.extract_proc_macro_attr_input(attr.item.tokens, span);
629629
let tok_result = expander.expand(self.cx, span, input, item_tok);
630-
let res = self.parse_ast_fragment(tok_result, fragment_kind, &attr.path, span);
630+
let res =
631+
self.parse_ast_fragment(tok_result, fragment_kind, &attr.item.path, span);
631632
self.gate_proc_macro_expansion(span, &res);
632633
res
633634
}
@@ -1530,11 +1531,10 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
15301531

15311532
let meta = attr::mk_list_item(Ident::with_dummy_span(sym::doc), items);
15321533
*at = attr::Attribute {
1534+
item: AttrItem { path: meta.path, tokens: meta.kind.tokens(meta.span) },
15331535
span: at.span,
15341536
id: at.id,
15351537
style: at.style,
1536-
path: meta.path,
1537-
tokens: meta.kind.tokens(meta.span),
15381538
is_sugared_doc: false,
15391539
};
15401540
} else {

src/libsyntax/mut_visit.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,8 @@ pub fn noop_visit_local<T: MutVisitor>(local: &mut P<Local>, vis: &mut T) {
550550
}
551551

552552
pub fn noop_visit_attribute<T: MutVisitor>(attr: &mut Attribute, vis: &mut T) {
553-
let Attribute { id: _, style: _, path, tokens, is_sugared_doc: _, span } = attr;
553+
let Attribute { item: AttrItem { path, tokens }, id: _, style: _, is_sugared_doc: _, span }
554+
= attr;
554555
vis.visit_path(path);
555556
vis.visit_tts(tokens);
556557
vis.visit_span(span);

src/libsyntax/parse/attr.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,9 @@ impl<'a> Parser<'a> {
151151
};
152152

153153
Ok(ast::Attribute {
154+
item: ast::AttrItem { path, tokens },
154155
id: attr::mk_attr_id(),
155156
style,
156-
path,
157-
tokens,
158157
is_sugared_doc: false,
159158
span,
160159
})

0 commit comments

Comments
 (0)