Skip to content

Commit 12e6bea

Browse files
committed
syntax: Split ast::Attribute into container and inner parts
1 parent 2111aed commit 12e6bea

File tree

8 files changed

+41
-31
lines changed

8 files changed

+41
-31
lines changed

src/librustc/hir/lowering.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -986,10 +986,12 @@ impl<'a> LoweringContext<'a> {
986986
// lower attributes (we use the AST version) there is nowhere to keep
987987
// the `HirId`s. We don't actually need HIR version of attributes anyway.
988988
Attribute {
989+
item: AttrItem {
990+
path: attr.path.clone(),
991+
tokens: self.lower_token_stream(attr.tokens.clone()),
992+
},
989993
id: attr.id,
990994
style: attr.style,
991-
path: attr.path.clone(),
992-
tokens: self.lower_token_stream(attr.tokens.clone()),
993995
is_sugared_doc: attr.is_sugared_doc,
994996
span: attr.span,
995997
}

src/librustc/ich/impls_syntax.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,11 @@ impl<'a> HashStable<StableHashingContext<'a>> for ast::Path {
227227
}
228228
}
229229

230+
impl_stable_hash_for!(struct ::syntax::ast::AttrItem {
231+
path,
232+
tokens,
233+
});
234+
230235
impl<'a> HashStable<StableHashingContext<'a>> for ast::Attribute {
231236
fn hash_stable<W: StableHasherResult>(&self,
232237
hcx: &mut StableHashingContext<'a>,
@@ -236,19 +241,15 @@ impl<'a> HashStable<StableHashingContext<'a>> for ast::Attribute {
236241
debug_assert!(!self.is_sugared_doc);
237242

238243
let ast::Attribute {
244+
ref item,
239245
id: _,
240246
style,
241-
ref path,
242-
ref tokens,
243247
is_sugared_doc: _,
244248
span,
245249
} = *self;
246250

251+
item.hash_stable(hcx, hasher);
247252
style.hash_stable(hcx, hasher);
248-
path.hash_stable(hcx, hasher);
249-
for tt in tokens.trees() {
250-
tt.hash_stable(hcx, hasher);
251-
}
252253
span.hash_stable(hcx, hasher);
253254
}
254255
}

src/libsyntax/ast.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -2124,18 +2124,28 @@ impl rustc_serialize::Decodable for AttrId {
21242124
}
21252125
}
21262126

2127+
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2128+
pub struct AttrItem {
2129+
pub path: Path,
2130+
pub tokens: TokenStream,
2131+
}
2132+
21272133
/// Metadata associated with an item.
21282134
/// Doc-comments are promoted to attributes that have `is_sugared_doc = true`.
21292135
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
21302136
pub struct Attribute {
2137+
pub item: AttrItem,
21312138
pub id: AttrId,
21322139
pub style: AttrStyle,
2133-
pub path: Path,
2134-
pub tokens: TokenStream,
21352140
pub is_sugared_doc: bool,
21362141
pub span: Span,
21372142
}
21382143

2144+
impl std::ops::Deref for Attribute {
2145+
type Target = AttrItem;
2146+
fn deref(&self) -> &Self::Target { &self.item }
2147+
}
2148+
21392149
/// `TraitRef`s appear in impls.
21402150
///
21412151
/// Resolution maps each `TraitRef`'s `ref_id` to its defining trait; that's all

src/libsyntax/attr/mod.rs

+9-11
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;
@@ -332,10 +332,9 @@ impl Attribute {
332332
DUMMY_SP,
333333
);
334334
f(&Attribute {
335+
item: AttrItem { path: meta.path, tokens: meta.node.tokens(meta.span) },
335336
id: self.id,
336337
style: self.style,
337-
path: meta.path,
338-
tokens: meta.node.tokens(meta.span),
339338
is_sugared_doc: true,
340339
span: self.span,
341340
})
@@ -384,10 +383,9 @@ crate fn mk_attr_id() -> AttrId {
384383
/// Returns an inner attribute with the given value and span.
385384
pub fn mk_attr_inner(item: MetaItem) -> Attribute {
386385
Attribute {
386+
item: AttrItem { path: item.path, tokens: item.node.tokens(item.span) },
387387
id: mk_attr_id(),
388388
style: ast::AttrStyle::Inner,
389-
path: item.path,
390-
tokens: item.node.tokens(item.span),
391389
is_sugared_doc: false,
392390
span: item.span,
393391
}
@@ -396,10 +394,9 @@ pub fn mk_attr_inner(item: MetaItem) -> Attribute {
396394
/// Returns an outer attribute with the given value and span.
397395
pub fn mk_attr_outer(item: MetaItem) -> Attribute {
398396
Attribute {
397+
item: AttrItem { path: item.path, tokens: item.node.tokens(item.span) },
399398
id: mk_attr_id(),
400399
style: ast::AttrStyle::Outer,
401-
path: item.path,
402-
tokens: item.node.tokens(item.span),
403400
is_sugared_doc: false,
404401
span: item.span,
405402
}
@@ -410,10 +407,12 @@ pub fn mk_sugared_doc_attr(text: Symbol, span: Span) -> Attribute {
410407
let lit_kind = LitKind::Str(text, ast::StrStyle::Cooked);
411408
let lit = Lit::from_lit_kind(lit_kind, span);
412409
Attribute {
410+
item: AttrItem {
411+
path: Path::from_ident(Ident::with_dummy_span(sym::doc).with_span_pos(span)),
412+
tokens: MetaItemKind::NameValue(lit).tokens(span),
413+
},
413414
id: mk_attr_id(),
414415
style,
415-
path: Path::from_ident(Ident::with_dummy_span(sym::doc).with_span_pos(span)),
416-
tokens: MetaItemKind::NameValue(lit).tokens(span),
417416
is_sugared_doc: true,
418417
span,
419418
}
@@ -735,10 +734,9 @@ pub fn inject(mut krate: ast::Crate, parse_sess: &ParseSess, attrs: &[String]) -
735734
}
736735

737736
krate.attrs.push(Attribute {
737+
item: AttrItem { path, tokens },
738738
id: mk_attr_id(),
739739
style: AttrStyle::Inner,
740-
path,
741-
tokens,
742740
is_sugared_doc: false,
743741
span: start_span.to(end_span),
744742
});

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;
@@ -526,9 +526,10 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
526526
Annotatable::Stmt(stmt) => token::NtStmt(stmt.into_inner()),
527527
Annotatable::Expr(expr) => token::NtExpr(expr),
528528
})), DUMMY_SP).into();
529-
let input = self.extract_proc_macro_attr_input(attr.tokens, span);
529+
let input = self.extract_proc_macro_attr_input(attr.item.tokens, span);
530530
let tok_result = expander.expand(self.cx, span, input, item_tok);
531-
let res = self.parse_ast_fragment(tok_result, fragment_kind, &attr.path, span);
531+
let res =
532+
self.parse_ast_fragment(tok_result, fragment_kind, &attr.item.path, span);
532533
self.gate_proc_macro_expansion(span, &res);
533534
res
534535
}
@@ -1327,11 +1328,10 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
13271328

13281329
let meta = attr::mk_list_item(Ident::with_dummy_span(sym::doc), items);
13291330
*at = attr::Attribute {
1331+
item: AttrItem { path: meta.path, tokens: meta.node.tokens(meta.span) },
13301332
span: at.span,
13311333
id: at.id,
13321334
style: at.style,
1333-
path: meta.path,
1334-
tokens: meta.node.tokens(meta.span),
13351335
is_sugared_doc: false,
13361336
};
13371337
} else {

src/libsyntax/mut_visit.rs

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

529529
pub fn noop_visit_attribute<T: MutVisitor>(attr: &mut Attribute, vis: &mut T) {
530-
let Attribute { id: _, style: _, path, tokens, is_sugared_doc: _, span } = attr;
530+
let Attribute { item: AttrItem { path, tokens }, id: _, style: _, is_sugared_doc: _, span }
531+
= attr;
531532
vis.visit_path(path);
532533
vis.visit_tts(tokens);
533534
vis.visit_span(span);

src/libsyntax/parse/attr.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,9 @@ impl<'a> Parser<'a> {
159159
};
160160

161161
Ok(ast::Attribute {
162+
item: ast::AttrItem { path, tokens },
162163
id: attr::mk_attr_id(),
163164
style,
164-
path,
165-
tokens,
166165
is_sugared_doc: false,
167166
span,
168167
})

0 commit comments

Comments
 (0)