syntax: Split ast::Attribute
into container and inner parts
This commit is contained in:
parent
22bc9e1d9c
commit
535d4743a4
8 changed files with 39 additions and 27 deletions
|
@ -988,10 +988,12 @@ impl<'a> LoweringContext<'a> {
|
||||||
// lower attributes (we use the AST version) there is nowhere to keep
|
// lower attributes (we use the AST version) there is nowhere to keep
|
||||||
// the `HirId`s. We don't actually need HIR version of attributes anyway.
|
// the `HirId`s. We don't actually need HIR version of attributes anyway.
|
||||||
Attribute {
|
Attribute {
|
||||||
id: attr.id,
|
item: AttrItem {
|
||||||
style: attr.style,
|
|
||||||
path: attr.path.clone(),
|
path: attr.path.clone(),
|
||||||
tokens: self.lower_token_stream(attr.tokens.clone()),
|
tokens: self.lower_token_stream(attr.tokens.clone()),
|
||||||
|
},
|
||||||
|
id: attr.id,
|
||||||
|
style: attr.style,
|
||||||
is_sugared_doc: attr.is_sugared_doc,
|
is_sugared_doc: attr.is_sugared_doc,
|
||||||
span: attr.span,
|
span: attr.span,
|
||||||
}
|
}
|
||||||
|
|
|
@ -196,6 +196,11 @@ impl<'a> HashStable<StableHashingContext<'a>> for ast::Path {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl_stable_hash_for!(struct ::syntax::ast::AttrItem {
|
||||||
|
path,
|
||||||
|
tokens,
|
||||||
|
});
|
||||||
|
|
||||||
impl<'a> HashStable<StableHashingContext<'a>> for ast::Attribute {
|
impl<'a> HashStable<StableHashingContext<'a>> for ast::Attribute {
|
||||||
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
|
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
|
||||||
// Make sure that these have been filtered out.
|
// Make sure that these have been filtered out.
|
||||||
|
@ -203,19 +208,15 @@ impl<'a> HashStable<StableHashingContext<'a>> for ast::Attribute {
|
||||||
debug_assert!(!self.is_sugared_doc);
|
debug_assert!(!self.is_sugared_doc);
|
||||||
|
|
||||||
let ast::Attribute {
|
let ast::Attribute {
|
||||||
|
ref item,
|
||||||
id: _,
|
id: _,
|
||||||
style,
|
style,
|
||||||
ref path,
|
|
||||||
ref tokens,
|
|
||||||
is_sugared_doc: _,
|
is_sugared_doc: _,
|
||||||
span,
|
span,
|
||||||
} = *self;
|
} = *self;
|
||||||
|
|
||||||
|
item.hash_stable(hcx, hasher);
|
||||||
style.hash_stable(hcx, hasher);
|
style.hash_stable(hcx, hasher);
|
||||||
path.hash_stable(hcx, hasher);
|
|
||||||
for tt in tokens.trees() {
|
|
||||||
tt.hash_stable(hcx, hasher);
|
|
||||||
}
|
|
||||||
span.hash_stable(hcx, hasher);
|
span.hash_stable(hcx, hasher);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2139,18 +2139,28 @@ impl rustc_serialize::Decodable for AttrId {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
|
||||||
|
pub struct AttrItem {
|
||||||
|
pub path: Path,
|
||||||
|
pub tokens: TokenStream,
|
||||||
|
}
|
||||||
|
|
||||||
/// Metadata associated with an item.
|
/// Metadata associated with an item.
|
||||||
/// Doc-comments are promoted to attributes that have `is_sugared_doc = true`.
|
/// Doc-comments are promoted to attributes that have `is_sugared_doc = true`.
|
||||||
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
|
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
|
||||||
pub struct Attribute {
|
pub struct Attribute {
|
||||||
|
pub item: AttrItem,
|
||||||
pub id: AttrId,
|
pub id: AttrId,
|
||||||
pub style: AttrStyle,
|
pub style: AttrStyle,
|
||||||
pub path: Path,
|
|
||||||
pub tokens: TokenStream,
|
|
||||||
pub is_sugared_doc: bool,
|
pub is_sugared_doc: bool,
|
||||||
pub span: Span,
|
pub span: Span,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl std::ops::Deref for Attribute {
|
||||||
|
type Target = AttrItem;
|
||||||
|
fn deref(&self) -> &Self::Target { &self.item }
|
||||||
|
}
|
||||||
|
|
||||||
/// `TraitRef`s appear in impls.
|
/// `TraitRef`s appear in impls.
|
||||||
///
|
///
|
||||||
/// Resolution maps each `TraitRef`'s `ref_id` to its defining trait; that's all
|
/// Resolution maps each `TraitRef`'s `ref_id` to its defining trait; that's all
|
||||||
|
|
|
@ -9,7 +9,7 @@ pub use StabilityLevel::*;
|
||||||
pub use crate::ast::Attribute;
|
pub use crate::ast::Attribute;
|
||||||
|
|
||||||
use crate::ast;
|
use crate::ast;
|
||||||
use crate::ast::{AttrId, AttrStyle, Name, Ident, Path, PathSegment};
|
use crate::ast::{AttrItem, AttrId, AttrStyle, Name, Ident, Path, PathSegment};
|
||||||
use crate::ast::{MetaItem, MetaItemKind, NestedMetaItem};
|
use crate::ast::{MetaItem, MetaItemKind, NestedMetaItem};
|
||||||
use crate::ast::{Lit, LitKind, Expr, Item, Local, Stmt, StmtKind, GenericParam};
|
use crate::ast::{Lit, LitKind, Expr, Item, Local, Stmt, StmtKind, GenericParam};
|
||||||
use crate::mut_visit::visit_clobber;
|
use crate::mut_visit::visit_clobber;
|
||||||
|
@ -333,10 +333,9 @@ impl Attribute {
|
||||||
DUMMY_SP,
|
DUMMY_SP,
|
||||||
);
|
);
|
||||||
f(&Attribute {
|
f(&Attribute {
|
||||||
|
item: AttrItem { path: meta.path, tokens: meta.kind.tokens(meta.span) },
|
||||||
id: self.id,
|
id: self.id,
|
||||||
style: self.style,
|
style: self.style,
|
||||||
path: meta.path,
|
|
||||||
tokens: meta.kind.tokens(meta.span),
|
|
||||||
is_sugared_doc: true,
|
is_sugared_doc: true,
|
||||||
span: self.span,
|
span: self.span,
|
||||||
})
|
})
|
||||||
|
@ -384,10 +383,9 @@ crate fn mk_attr_id() -> AttrId {
|
||||||
|
|
||||||
pub fn mk_attr(style: AttrStyle, path: Path, tokens: TokenStream, span: Span) -> Attribute {
|
pub fn mk_attr(style: AttrStyle, path: Path, tokens: TokenStream, span: Span) -> Attribute {
|
||||||
Attribute {
|
Attribute {
|
||||||
|
item: AttrItem { path, tokens },
|
||||||
id: mk_attr_id(),
|
id: mk_attr_id(),
|
||||||
style,
|
style,
|
||||||
path,
|
|
||||||
tokens,
|
|
||||||
is_sugared_doc: false,
|
is_sugared_doc: false,
|
||||||
span,
|
span,
|
||||||
}
|
}
|
||||||
|
@ -408,10 +406,12 @@ pub fn mk_sugared_doc_attr(text: Symbol, span: Span) -> Attribute {
|
||||||
let lit_kind = LitKind::Str(text, ast::StrStyle::Cooked);
|
let lit_kind = LitKind::Str(text, ast::StrStyle::Cooked);
|
||||||
let lit = Lit::from_lit_kind(lit_kind, span);
|
let lit = Lit::from_lit_kind(lit_kind, span);
|
||||||
Attribute {
|
Attribute {
|
||||||
id: mk_attr_id(),
|
item: AttrItem {
|
||||||
style,
|
|
||||||
path: Path::from_ident(Ident::with_dummy_span(sym::doc).with_span_pos(span)),
|
path: Path::from_ident(Ident::with_dummy_span(sym::doc).with_span_pos(span)),
|
||||||
tokens: MetaItemKind::NameValue(lit).tokens(span),
|
tokens: MetaItemKind::NameValue(lit).tokens(span),
|
||||||
|
},
|
||||||
|
id: mk_attr_id(),
|
||||||
|
style,
|
||||||
is_sugared_doc: true,
|
is_sugared_doc: true,
|
||||||
span,
|
span,
|
||||||
}
|
}
|
||||||
|
|
|
@ -151,10 +151,9 @@ impl<'a> StripUnconfigured<'a> {
|
||||||
// `#[cfg_attr(false, cfg_attr(true, some_attr))]`.
|
// `#[cfg_attr(false, cfg_attr(true, some_attr))]`.
|
||||||
expanded_attrs.into_iter()
|
expanded_attrs.into_iter()
|
||||||
.flat_map(|(path, tokens, span)| self.process_cfg_attr(ast::Attribute {
|
.flat_map(|(path, tokens, span)| self.process_cfg_attr(ast::Attribute {
|
||||||
|
item: ast::AttrItem { path, tokens },
|
||||||
id: attr::mk_attr_id(),
|
id: attr::mk_attr_id(),
|
||||||
style: attr.style,
|
style: attr.style,
|
||||||
path,
|
|
||||||
tokens,
|
|
||||||
is_sugared_doc: false,
|
is_sugared_doc: false,
|
||||||
span,
|
span,
|
||||||
}))
|
}))
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::ast::{self, Block, Ident, LitKind, NodeId, PatKind, Path};
|
use crate::ast::{self, AttrItem, Block, Ident, LitKind, NodeId, PatKind, Path};
|
||||||
use crate::ast::{MacStmtStyle, StmtKind, ItemKind};
|
use crate::ast::{MacStmtStyle, StmtKind, ItemKind};
|
||||||
use crate::attr::{self, HasAttrs};
|
use crate::attr::{self, HasAttrs};
|
||||||
use crate::source_map::respan;
|
use crate::source_map::respan;
|
||||||
|
@ -625,9 +625,10 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
|
||||||
| Annotatable::Variant(..)
|
| Annotatable::Variant(..)
|
||||||
=> panic!("unexpected annotatable"),
|
=> panic!("unexpected annotatable"),
|
||||||
})), DUMMY_SP).into();
|
})), DUMMY_SP).into();
|
||||||
let input = self.extract_proc_macro_attr_input(attr.tokens, span);
|
let input = self.extract_proc_macro_attr_input(attr.item.tokens, span);
|
||||||
let tok_result = expander.expand(self.cx, span, input, item_tok);
|
let tok_result = expander.expand(self.cx, span, input, item_tok);
|
||||||
let res = self.parse_ast_fragment(tok_result, fragment_kind, &attr.path, span);
|
let res =
|
||||||
|
self.parse_ast_fragment(tok_result, fragment_kind, &attr.item.path, span);
|
||||||
self.gate_proc_macro_expansion(span, &res);
|
self.gate_proc_macro_expansion(span, &res);
|
||||||
res
|
res
|
||||||
}
|
}
|
||||||
|
@ -1530,11 +1531,10 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
|
||||||
|
|
||||||
let meta = attr::mk_list_item(Ident::with_dummy_span(sym::doc), items);
|
let meta = attr::mk_list_item(Ident::with_dummy_span(sym::doc), items);
|
||||||
*at = attr::Attribute {
|
*at = attr::Attribute {
|
||||||
|
item: AttrItem { path: meta.path, tokens: meta.kind.tokens(meta.span) },
|
||||||
span: at.span,
|
span: at.span,
|
||||||
id: at.id,
|
id: at.id,
|
||||||
style: at.style,
|
style: at.style,
|
||||||
path: meta.path,
|
|
||||||
tokens: meta.kind.tokens(meta.span),
|
|
||||||
is_sugared_doc: false,
|
is_sugared_doc: false,
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -550,7 +550,8 @@ 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) {
|
pub fn noop_visit_attribute<T: MutVisitor>(attr: &mut Attribute, vis: &mut T) {
|
||||||
let Attribute { id: _, style: _, path, tokens, is_sugared_doc: _, span } = attr;
|
let Attribute { item: AttrItem { path, tokens }, id: _, style: _, is_sugared_doc: _, span }
|
||||||
|
= attr;
|
||||||
vis.visit_path(path);
|
vis.visit_path(path);
|
||||||
vis.visit_tts(tokens);
|
vis.visit_tts(tokens);
|
||||||
vis.visit_span(span);
|
vis.visit_span(span);
|
||||||
|
|
|
@ -151,10 +151,9 @@ impl<'a> Parser<'a> {
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(ast::Attribute {
|
Ok(ast::Attribute {
|
||||||
|
item: ast::AttrItem { path, tokens },
|
||||||
id: attr::mk_attr_id(),
|
id: attr::mk_attr_id(),
|
||||||
style,
|
style,
|
||||||
path,
|
|
||||||
tokens,
|
|
||||||
is_sugared_doc: false,
|
is_sugared_doc: false,
|
||||||
span,
|
span,
|
||||||
})
|
})
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue