Refactor away ast::Attribute_.

This commit is contained in:
Jeffrey Seyfried 2016-11-14 12:00:25 +00:00
parent bfa709a38a
commit 3ea2bc4e93
19 changed files with 85 additions and 93 deletions

View File

@ -405,7 +405,7 @@ pub fn gather_attr(attr: &ast::Attribute)
attr::mark_used(attr);
let meta = &attr.node.value;
let meta = &attr.value;
let metas = if let Some(metas) = meta.meta_item_list() {
metas
} else {

View File

@ -914,7 +914,7 @@ impl<'a, 'hash, 'tcx> StrictVersionHashVisitor<'a, 'hash, 'tcx> {
let indices = self.indices_sorted_by(attributes, |attr| attr.name());
for i in indices {
let attr = &attributes[i].node;
let attr = &attributes[i];
if !attr.is_sugared_doc &&
!IGNORED_ATTRIBUTES.contains(&&*attr.value.name()) {
SawAttribute(attr.style).hash(self.st);

View File

@ -277,7 +277,7 @@ impl LateLintPass for UnusedAttributes {
.find(|&&(ref x, t)| &*attr.name() == x && AttributeType::CrateLevel == t)
.is_some();
if known_crate || plugin_crate {
let msg = match attr.node.style {
let msg = match attr.style {
ast::AttrStyle::Outer => {
"crate-level attribute should be an inner attribute: add an exclamation \
mark: #![foo]"

View File

@ -934,7 +934,7 @@ impl<'a, 'tcx> CrateMetadata {
.decode(self)
.map(|mut attr| {
// Need new unique IDs: old thread-local IDs won't map to new threads.
attr.node.id = attr::mk_attr_id();
attr.id = attr::mk_attr_id();
attr
})
.collect()

View File

@ -240,7 +240,7 @@ impl<'v> hir_visit::Visitor<'v> for StatCollector<'v> {
hir_visit::walk_assoc_type_binding(self, type_binding)
}
fn visit_attribute(&mut self, attr: &'v ast::Attribute) {
self.record("Attribute", Id::Attr(attr.node.id), attr);
self.record("Attribute", Id::Attr(attr.id), attr);
}
fn visit_macro_def(&mut self, macro_def: &'v hir::MacroDef) {
self.record("MacroDef", Id::Node(macro_def.id), macro_def);

View File

@ -607,7 +607,7 @@ impl<'b> Resolver<'b> {
if attr.check_name("macro_escape") {
let msg = "macro_escape is a deprecated synonym for macro_use";
let mut err = self.session.struct_span_warn(attr.span, msg);
if let ast::AttrStyle::Inner = attr.node.style {
if let ast::AttrStyle::Inner = attr.style {
err.help("consider an outer attribute, #[macro_use] mod ...").emit();
} else {
err.emit();

View File

@ -734,7 +734,7 @@ fn docs_for_attrs(attrs: &[Attribute]) -> String {
for attr in attrs {
if attr.name() == doc {
if let Some(ref val) = attr.value_str() {
if attr.node.is_sugared_doc {
if attr.is_sugared_doc {
result.push_str(&strip_doc_comment_decoration(val));
} else {
result.push_str(val);

View File

@ -1755,8 +1755,6 @@ impl ViewPath_ {
}
}
/// Meta-data associated with an item
pub type Attribute = Spanned<Attribute_>;
/// Distinguishes between Attributes that decorate items and Attributes that
/// are contained as statements within items. These two cases need to be
@ -1770,13 +1768,15 @@ pub enum AttrStyle {
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
pub struct AttrId(pub usize);
/// Meta-data associated with an item
/// Doc-comments are promoted to attributes that have is_sugared_doc = true
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub struct Attribute_ {
pub struct Attribute {
pub id: AttrId,
pub style: AttrStyle,
pub value: P<MetaItem>,
pub is_sugared_doc: bool,
pub span: Span,
}
/// TraitRef's appear in impls.

View File

@ -15,10 +15,10 @@ pub use self::ReprAttr::*;
pub use self::IntType::*;
use ast;
use ast::{AttrId, Attribute, Attribute_};
use ast::{AttrId, Attribute};
use ast::{MetaItem, MetaItemKind, NestedMetaItem, NestedMetaItemKind};
use ast::{Lit, Expr, Item, Local, Stmt, StmtKind};
use codemap::{respan, spanned, dummy_spanned};
use codemap::{respan, spanned, dummy_spanned, mk_sp};
use syntax_pos::{Span, BytePos, DUMMY_SP};
use errors::Handler;
use feature_gate::{Features, GatedCfg};
@ -61,7 +61,7 @@ fn handle_errors(diag: &Handler, span: Span, error: AttrError) {
pub fn mark_used(attr: &Attribute) {
debug!("Marking {:?} as used.", attr);
let AttrId(id) = attr.node.id;
let AttrId(id) = attr.id;
USED_ATTRS.with(|slot| {
let idx = (id / 64) as usize;
let shift = id % 64;
@ -73,7 +73,7 @@ pub fn mark_used(attr: &Attribute) {
}
pub fn is_used(attr: &Attribute) -> bool {
let AttrId(id) = attr.node.id;
let AttrId(id) = attr.id;
USED_ATTRS.with(|slot| {
let idx = (id / 64) as usize;
let shift = id % 64;
@ -84,7 +84,7 @@ pub fn is_used(attr: &Attribute) -> bool {
pub fn mark_known(attr: &Attribute) {
debug!("Marking {:?} as known.", attr);
let AttrId(id) = attr.node.id;
let AttrId(id) = attr.id;
KNOWN_ATTRS.with(|slot| {
let idx = (id / 64) as usize;
let shift = id % 64;
@ -96,7 +96,7 @@ pub fn mark_known(attr: &Attribute) {
}
pub fn is_known(attr: &Attribute) -> bool {
let AttrId(id) = attr.node.id;
let AttrId(id) = attr.id;
KNOWN_ATTRS.with(|slot| {
let idx = (id / 64) as usize;
let shift = id % 64;
@ -270,7 +270,7 @@ impl MetaItem {
impl Attribute {
/// Extract the MetaItem from inside this Attribute.
pub fn meta(&self) -> &MetaItem {
&self.node.value
&self.value
}
/// Convert self to a normal #[doc="foo"] comment, if it is a
@ -279,16 +279,16 @@ impl Attribute {
pub fn with_desugared_doc<T, F>(&self, f: F) -> T where
F: FnOnce(&Attribute) -> T,
{
if self.node.is_sugared_doc {
if self.is_sugared_doc {
let comment = self.value_str().unwrap();
let meta = mk_name_value_item_str(
InternedString::new("doc"),
token::intern_and_get_ident(&strip_doc_comment_decoration(
&comment)));
if self.node.style == ast::AttrStyle::Outer {
f(&mk_attr_outer(self.node.id, meta))
if self.style == ast::AttrStyle::Outer {
f(&mk_attr_outer(self.id, meta))
} else {
f(&mk_attr_inner(self.node.id, meta))
f(&mk_attr_inner(self.id, meta))
}
} else {
f(self)
@ -355,13 +355,13 @@ pub fn mk_attr_inner(id: AttrId, item: P<MetaItem>) -> Attribute {
/// Returns an innter attribute with the given value and span.
pub fn mk_spanned_attr_inner(sp: Span, id: AttrId, item: P<MetaItem>) -> Attribute {
respan(sp,
Attribute_ {
id: id,
style: ast::AttrStyle::Inner,
value: item,
is_sugared_doc: false,
})
Attribute {
id: id,
style: ast::AttrStyle::Inner,
value: item,
is_sugared_doc: false,
span: sp,
}
}
@ -372,36 +372,36 @@ pub fn mk_attr_outer(id: AttrId, item: P<MetaItem>) -> Attribute {
/// Returns an outer attribute with the given value and span.
pub fn mk_spanned_attr_outer(sp: Span, id: AttrId, item: P<MetaItem>) -> Attribute {
respan(sp,
Attribute_ {
id: id,
style: ast::AttrStyle::Outer,
value: item,
is_sugared_doc: false,
})
Attribute {
id: id,
style: ast::AttrStyle::Outer,
value: item,
is_sugared_doc: false,
span: sp,
}
}
pub fn mk_doc_attr_outer(id: AttrId, item: P<MetaItem>, is_sugared_doc: bool) -> Attribute {
dummy_spanned(Attribute_ {
Attribute {
id: id,
style: ast::AttrStyle::Outer,
value: item,
is_sugared_doc: is_sugared_doc,
})
span: DUMMY_SP,
}
}
pub fn mk_sugared_doc_attr(id: AttrId, text: InternedString, lo: BytePos,
hi: BytePos)
pub fn mk_sugared_doc_attr(id: AttrId, text: InternedString, lo: BytePos, hi: BytePos)
-> Attribute {
let style = doc_comment_style(&text);
let lit = spanned(lo, hi, ast::LitKind::Str(text, ast::StrStyle::Cooked));
let attr = Attribute_ {
Attribute {
id: id,
style: style,
value: P(spanned(lo, hi, MetaItemKind::NameValue(InternedString::new("doc"), lit))),
is_sugared_doc: true
};
spanned(lo, hi, attr)
is_sugared_doc: true,
span: mk_sp(lo, hi),
}
}
/* Searching */
@ -489,7 +489,7 @@ pub enum InlineAttr {
/// Determine what `#[inline]` attribute is present in `attrs`, if any.
pub fn find_inline_attr(diagnostic: Option<&Handler>, attrs: &[Attribute]) -> InlineAttr {
attrs.iter().fold(InlineAttr::None, |ia,attr| {
match attr.node.value.node {
match attr.value.node {
MetaItemKind::Word(ref n) if n == "inline" => {
mark_used(attr);
InlineAttr::Hint
@ -896,7 +896,7 @@ pub fn require_unique_names(diagnostic: &Handler, metas: &[P<MetaItem>]) {
/// structure layout, and `packed` to remove padding.
pub fn find_repr_attrs(diagnostic: &Handler, attr: &Attribute) -> Vec<ReprAttr> {
let mut acc = Vec::new();
match attr.node.value.node {
match attr.value.node {
ast::MetaItemKind::List(ref s, ref items) if s == "repr" => {
mark_used(attr);
for item in items {

View File

@ -12,7 +12,7 @@ use attr::HasAttrs;
use feature_gate::{feature_err, EXPLAIN_STMT_ATTR_SYNTAX, Features, get_features, GateIssue};
use {fold, attr};
use ast;
use codemap::{Spanned, respan};
use codemap::Spanned;
use parse::ParseSess;
use ptr::P;
@ -106,12 +106,13 @@ impl<'a> StripUnconfigured<'a> {
match (cfg.meta_item(), mi.meta_item()) {
(Some(cfg), Some(mi)) =>
if cfg_matches(&cfg, self.sess, self.features) {
self.process_cfg_attr(respan(mi.span, ast::Attribute_ {
self.process_cfg_attr(ast::Attribute {
id: attr::mk_attr_id(),
style: attr.node.style,
style: attr.style,
value: mi.clone(),
is_sugared_doc: false,
}))
span: mi.span,
})
} else {
None
},
@ -131,7 +132,7 @@ impl<'a> StripUnconfigured<'a> {
return false;
}
let mis = match attr.node.value.node {
let mis = match attr.value.node {
ast::MetaItemKind::List(_, ref mis) if is_cfg(&attr) => mis,
_ => return true
};
@ -160,7 +161,7 @@ impl<'a> StripUnconfigured<'a> {
attr.span,
GateIssue::Language,
EXPLAIN_STMT_ATTR_SYNTAX);
if attr.node.is_sugared_doc {
if attr.is_sugared_doc {
err.help("`///` is for documentation comments. For a plain comment, use `//`.");
}
err.emit();

View File

@ -353,12 +353,12 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
match *ext {
MultiModifier(ref mac) => {
let item = mac.expand(self.cx, attr.span, &attr.node.value, item);
let item = mac.expand(self.cx, attr.span, &attr.value, item);
kind.expect_from_annotatables(item)
}
MultiDecorator(ref mac) => {
let mut items = Vec::new();
mac.expand(self.cx, attr.span, &attr.node.value, &item,
mac.expand(self.cx, attr.span, &attr.value, &item,
&mut |item| items.push(item));
items.push(item);
kind.expect_from_annotatables(items)

View File

@ -223,13 +223,13 @@ pub mod rt {
let mut r = vec![];
// FIXME: The spans could be better
r.push(TokenTree::Token(self.span, token::Pound));
if self.node.style == ast::AttrStyle::Inner {
if self.style == ast::AttrStyle::Inner {
r.push(TokenTree::Token(self.span, token::Not));
}
r.push(TokenTree::Delimited(self.span, Rc::new(tokenstream::Delimited {
delim: token::Bracket,
open_span: self.span,
tts: self.node.value.to_tokens(cx),
tts: self.value.to_tokens(cx),
close_span: self.span,
})));
r

View File

@ -1013,7 +1013,7 @@ impl<'a> Visitor for PostExpansionVisitor<'a> {
self.context.check_attribute(attr, false);
}
if contains_novel_literal(&*(attr.node.value)) {
if contains_novel_literal(&*(attr.value)) {
gate_feature_post!(&self, attr_literals, attr.span,
"non-string literals in attributes, or string \
literals in top-level positions, are experimental");

View File

@ -486,16 +486,13 @@ pub fn noop_fold_local<T: Folder>(l: P<Local>, fld: &mut T) -> P<Local> {
})
}
pub fn noop_fold_attribute<T: Folder>(at: Attribute, fld: &mut T) -> Option<Attribute> {
let Spanned {node: Attribute_ {id, style, value, is_sugared_doc}, span} = at;
Some(Spanned {
node: Attribute_ {
id: id,
style: style,
value: fld.fold_meta_item(value),
is_sugared_doc: is_sugared_doc
},
span: fld.new_span(span)
pub fn noop_fold_attribute<T: Folder>(attr: Attribute, fld: &mut T) -> Option<Attribute> {
Some(Attribute {
id: attr.id,
style: attr.style,
value: fld.fold_meta_item(attr.value),
is_sugared_doc: attr.is_sugared_doc,
span: fld.new_span(attr.span),
})
}

View File

@ -11,7 +11,7 @@
use attr;
use ast;
use syntax_pos::{mk_sp, Span};
use codemap::{spanned, Spanned};
use codemap::spanned;
use parse::common::SeqSep;
use parse::PResult;
use parse::token;
@ -55,7 +55,7 @@ impl<'a> Parser<'a> {
self.span.lo,
self.span.hi
);
if attr.node.style != ast::AttrStyle::Outer {
if attr.style != ast::AttrStyle::Outer {
let mut err = self.fatal("expected outer doc comment");
err.note("inner doc comments like this (starting with \
`//!` or `/*!`) can only appear before items");
@ -145,14 +145,12 @@ impl<'a> Parser<'a> {
style = ast::AttrStyle::Inner;
}
Ok(Spanned {
Ok(ast::Attribute {
id: attr::mk_attr_id(),
style: style,
value: value,
is_sugared_doc: false,
span: span,
node: ast::Attribute_ {
id: attr::mk_attr_id(),
style: style,
value: value,
is_sugared_doc: false,
},
})
}
@ -172,7 +170,7 @@ impl<'a> Parser<'a> {
}
let attr = self.parse_attribute(true)?;
assert!(attr.node.style == ast::AttrStyle::Inner);
assert!(attr.style == ast::AttrStyle::Inner);
attrs.push(attr);
}
token::DocComment(s) => {
@ -180,7 +178,7 @@ impl<'a> Parser<'a> {
let Span { lo, hi, .. } = self.span;
let str = self.id_to_interned_str(ast::Ident::with_empty_ctxt(s));
let attr = attr::mk_sugared_doc_attr(attr::mk_attr_id(), str, lo, hi);
if attr.node.style == ast::AttrStyle::Inner {
if attr.style == ast::AttrStyle::Inner {
attrs.push(attr);
self.bump();
} else {

View File

@ -3751,9 +3751,7 @@ impl<'a> Parser<'a> {
/// Emit an expected item after attributes error.
fn expected_item_err(&self, attrs: &[Attribute]) {
let message = match attrs.last() {
Some(&Attribute { node: ast::Attribute_ { is_sugared_doc: true, .. }, .. }) => {
"expected item after doc comment"
}
Some(&Attribute { is_sugared_doc: true, .. }) => "expected item after doc comment",
_ => "expected item after attributes",
};

View File

@ -727,7 +727,7 @@ pub trait PrintState<'a> {
trailing_hardbreak: bool) -> io::Result<()> {
let mut count = 0;
for attr in attrs {
if attr.node.style == kind {
if attr.style == kind {
try!(self.print_attribute_inline(attr, is_inline));
if is_inline {
try!(self.nbsp());
@ -751,11 +751,11 @@ pub trait PrintState<'a> {
try!(self.hardbreak_if_not_bol());
}
try!(self.maybe_print_comment(attr.span.lo));
if attr.node.is_sugared_doc {
if attr.is_sugared_doc {
try!(word(self.writer(), &attr.value_str().unwrap()));
hardbreak(self.writer())
} else {
match attr.node.style {
match attr.style {
ast::AttrStyle::Inner => try!(word(self.writer(), "#![")),
ast::AttrStyle::Outer => try!(word(self.writer(), "#[")),
}

View File

@ -68,15 +68,13 @@ pub fn maybe_inject_crates_ref(sess: &ParseSess,
let span = ignored_span(sess, DUMMY_SP);
krate.module.items.insert(0, P(ast::Item {
attrs: vec![ast::Attribute {
node: ast::Attribute_ {
style: ast::AttrStyle::Outer,
value: P(ast::MetaItem {
node: ast::MetaItemKind::Word(token::intern_and_get_ident("prelude_import")),
span: span,
}),
id: attr::mk_attr_id(),
is_sugared_doc: false,
},
style: ast::AttrStyle::Outer,
value: P(ast::MetaItem {
node: ast::MetaItemKind::Word(token::intern_and_get_ident("prelude_import")),
span: span,
}),
id: attr::mk_attr_id(),
is_sugared_doc: false,
span: span,
}],
vis: ast::Visibility::Inherited,

View File

@ -135,7 +135,7 @@ pub fn expand_derive(cx: &mut ExtCtxt,
let mut traits = get_traits(mitem, cx);
for derive_attr in derive_attrs {
traits.extend(get_traits(&derive_attr.node.value, cx));
traits.extend(get_traits(&derive_attr.value, cx));
}
// First, weed out malformed #[derive]