1
Fork 0

Move MetaItemKind's Name to a field of MetaItem.

This commit is contained in:
Jeffrey Seyfried 2016-11-15 07:37:10 +00:00
parent 4b9b0d3474
commit e97686d048
10 changed files with 70 additions and 71 deletions

View file

@ -615,7 +615,8 @@ impl RustcDefaultCalls {
let mut cfgs = Vec::new(); let mut cfgs = Vec::new();
for &(name, ref value) in sess.parse_sess.config.iter() { for &(name, ref value) in sess.parse_sess.config.iter() {
let gated_cfg = GatedCfg::gate(&ast::MetaItem { let gated_cfg = GatedCfg::gate(&ast::MetaItem {
node: ast::MetaItemKind::Word(name), name: name,
node: ast::MetaItemKind::Word,
span: DUMMY_SP, span: DUMMY_SP,
}); });
if !allow_unstable_cfg && gated_cfg.is_some() { if !allow_unstable_cfg && gated_cfg.is_some() {

View file

@ -874,22 +874,20 @@ impl<'a, 'hash, 'tcx> StrictVersionHashVisitor<'a, 'hash, 'tcx> {
// ignoring span information, it doesn't matter here // ignoring span information, it doesn't matter here
self.hash_discriminant(&meta_item.node); self.hash_discriminant(&meta_item.node);
let name = &*meta_item.name.as_str();
match meta_item.node { match meta_item.node {
ast::MetaItemKind::Word(s) => { ast::MetaItemKind::Word => {
let s = &*s.as_str(); name.len().hash(self.st);
s.len().hash(self.st); name.hash(self.st);
s.hash(self.st);
} }
ast::MetaItemKind::NameValue(s, ref lit) => { ast::MetaItemKind::NameValue(ref lit) => {
let s = &*s.as_str(); name.len().hash(self.st);
s.len().hash(self.st); name.hash(self.st);
s.hash(self.st);
lit.node.hash(self.st); lit.node.hash(self.st);
} }
ast::MetaItemKind::List(s, ref items) => { ast::MetaItemKind::List(ref items) => {
let s = &*s.as_str(); name.len().hash(self.st);
s.len().hash(self.st); name.hash(self.st);
s.hash(self.st);
// Sort subitems so the hash does not depend on their order // Sort subitems so the hash does not depend on their order
let indices = self.indices_sorted_by(&items, |p| { let indices = self.indices_sorted_by(&items, |p| {
(p.name(), fnv::hash(&p.literal().map(|i| &i.node))) (p.name(), fnv::hash(&p.literal().map(|i| &i.node)))

View file

@ -515,7 +515,12 @@ pub enum NestedMetaItemKind {
/// A spanned compile-time attribute item. /// A spanned compile-time attribute item.
/// ///
/// E.g. `#[test]`, `#[derive(..)]` or `#[feature = "foo"]` /// E.g. `#[test]`, `#[derive(..)]` or `#[feature = "foo"]`
pub type MetaItem = Spanned<MetaItemKind>; #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub struct MetaItem {
pub name: Name,
pub node: MetaItemKind,
pub span: Span,
}
/// A compile-time attribute item. /// A compile-time attribute item.
/// ///
@ -525,15 +530,15 @@ pub enum MetaItemKind {
/// Word meta item. /// Word meta item.
/// ///
/// E.g. `test` as in `#[test]` /// E.g. `test` as in `#[test]`
Word(Name), Word,
/// List meta item. /// List meta item.
/// ///
/// E.g. `derive(..)` as in `#[derive(..)]` /// E.g. `derive(..)` as in `#[derive(..)]`
List(Name, Vec<NestedMetaItem>), List(Vec<NestedMetaItem>),
/// Name value meta item. /// Name value meta item.
/// ///
/// E.g. `feature = "foo"` as in `#[feature = "foo"]` /// E.g. `feature = "foo"` as in `#[feature = "foo"]`
NameValue(Name, Lit), NameValue(Lit)
} }
/// A Block (`{ .. }`). /// A Block (`{ .. }`).

View file

@ -18,7 +18,7 @@ use ast;
use ast::{AttrId, Attribute, Name}; use ast::{AttrId, Attribute, Name};
use ast::{MetaItem, MetaItemKind, NestedMetaItem, NestedMetaItemKind}; use ast::{MetaItem, MetaItemKind, NestedMetaItem, NestedMetaItemKind};
use ast::{Lit, Expr, Item, Local, Stmt, StmtKind}; use ast::{Lit, Expr, Item, Local, Stmt, StmtKind};
use codemap::{respan, spanned, dummy_spanned, mk_sp}; use codemap::{spanned, dummy_spanned, mk_sp};
use syntax_pos::{Span, BytePos, DUMMY_SP}; use syntax_pos::{Span, BytePos, DUMMY_SP};
use errors::Handler; use errors::Handler;
use feature_gate::{Features, GatedCfg}; use feature_gate::{Features, GatedCfg};
@ -219,16 +219,12 @@ impl Attribute {
impl MetaItem { impl MetaItem {
pub fn name(&self) -> Name { pub fn name(&self) -> Name {
match self.node { self.name
MetaItemKind::Word(n) => n,
MetaItemKind::NameValue(n, _) => n,
MetaItemKind::List(n, _) => n,
}
} }
pub fn value_str(&self) -> Option<InternedString> { pub fn value_str(&self) -> Option<InternedString> {
match self.node { match self.node {
MetaItemKind::NameValue(_, ref v) => { MetaItemKind::NameValue(ref v) => {
match v.node { match v.node {
ast::LitKind::Str(ref s, _) => Some((*s).clone()), ast::LitKind::Str(ref s, _) => Some((*s).clone()),
_ => None, _ => None,
@ -240,14 +236,14 @@ impl MetaItem {
pub fn meta_item_list(&self) -> Option<&[NestedMetaItem]> { pub fn meta_item_list(&self) -> Option<&[NestedMetaItem]> {
match self.node { match self.node {
MetaItemKind::List(_, ref l) => Some(&l[..]), MetaItemKind::List(ref l) => Some(&l[..]),
_ => None _ => None
} }
} }
pub fn is_word(&self) -> bool { pub fn is_word(&self) -> bool {
match self.node { match self.node {
MetaItemKind::Word(_) => true, MetaItemKind::Word => true,
_ => false, _ => false,
} }
} }
@ -320,15 +316,15 @@ pub fn mk_word_item(name: Name) -> P<MetaItem> {
} }
pub fn mk_spanned_name_value_item(sp: Span, name: Name, value: ast::Lit) -> P<MetaItem> { pub fn mk_spanned_name_value_item(sp: Span, name: Name, value: ast::Lit) -> P<MetaItem> {
P(respan(sp, MetaItemKind::NameValue(name, value))) P(MetaItem { span: sp, name: name, node: MetaItemKind::NameValue(value) })
} }
pub fn mk_spanned_list_item(sp: Span, name: Name, items: Vec<NestedMetaItem>) -> P<MetaItem> { pub fn mk_spanned_list_item(sp: Span, name: Name, items: Vec<NestedMetaItem>) -> P<MetaItem> {
P(respan(sp, MetaItemKind::List(name, items))) P(MetaItem { span: sp, name: name, node: MetaItemKind::List(items) })
} }
pub fn mk_spanned_word_item(sp: Span, name: Name) -> P<MetaItem> { pub fn mk_spanned_word_item(sp: Span, name: Name) -> P<MetaItem> {
P(respan(sp, MetaItemKind::Word(name))) P(MetaItem { span: sp, name: name, node: MetaItemKind::Word })
} }
@ -394,7 +390,11 @@ pub fn mk_sugared_doc_attr(id: AttrId, text: InternedString, lo: BytePos, hi: By
Attribute { Attribute {
id: id, id: id,
style: style, style: style,
value: P(spanned(lo, hi, MetaItemKind::NameValue(token::intern("doc"), lit))), value: P(MetaItem {
span: mk_sp(lo, hi),
name: token::intern("doc"),
node: MetaItemKind::NameValue(lit),
}),
is_sugared_doc: true, is_sugared_doc: true,
span: mk_sp(lo, hi), span: mk_sp(lo, hi),
} }
@ -472,13 +472,14 @@ pub enum InlineAttr {
/// Determine what `#[inline]` attribute is present in `attrs`, if any. /// Determine what `#[inline]` attribute is present in `attrs`, if any.
pub fn find_inline_attr(diagnostic: Option<&Handler>, attrs: &[Attribute]) -> InlineAttr { pub fn find_inline_attr(diagnostic: Option<&Handler>, attrs: &[Attribute]) -> InlineAttr {
attrs.iter().fold(InlineAttr::None, |ia,attr| { attrs.iter().fold(InlineAttr::None, |ia, attr| {
match attr.value.node { match attr.value.node {
MetaItemKind::Word(n) if n == "inline" => { _ if attr.value.name != "inline" => ia,
MetaItemKind::Word => {
mark_used(attr); mark_used(attr);
InlineAttr::Hint InlineAttr::Hint
} }
MetaItemKind::List(n, ref items) if n == "inline" => { MetaItemKind::List(ref items) => {
mark_used(attr); mark_used(attr);
if items.len() != 1 { if items.len() != 1 {
diagnostic.map(|d|{ span_err!(d, attr.span, E0534, "expected one argument"); }); diagnostic.map(|d|{ span_err!(d, attr.span, E0534, "expected one argument"); });
@ -511,7 +512,7 @@ pub fn requests_inline(attrs: &[Attribute]) -> bool {
/// Tests if a cfg-pattern matches the cfg set /// Tests if a cfg-pattern matches the cfg set
pub fn cfg_matches(cfg: &ast::MetaItem, sess: &ParseSess, features: Option<&Features>) -> bool { pub fn cfg_matches(cfg: &ast::MetaItem, sess: &ParseSess, features: Option<&Features>) -> bool {
match cfg.node { match cfg.node {
ast::MetaItemKind::List(ref pred, ref mis) => { ast::MetaItemKind::List(ref mis) => {
for mi in mis.iter() { for mi in mis.iter() {
if !mi.is_meta_item() { if !mi.is_meta_item() {
handle_errors(&sess.span_diagnostic, mi.span, AttrError::UnsupportedLiteral); handle_errors(&sess.span_diagnostic, mi.span, AttrError::UnsupportedLiteral);
@ -521,7 +522,7 @@ pub fn cfg_matches(cfg: &ast::MetaItem, sess: &ParseSess, features: Option<&Feat
// The unwraps below may look dangerous, but we've already asserted // The unwraps below may look dangerous, but we've already asserted
// that they won't fail with the loop above. // that they won't fail with the loop above.
match &*pred.as_str() { match &*cfg.name.as_str() {
"any" => mis.iter().any(|mi| { "any" => mis.iter().any(|mi| {
cfg_matches(mi.meta_item().unwrap(), sess, features) cfg_matches(mi.meta_item().unwrap(), sess, features)
}), }),
@ -542,7 +543,7 @@ pub fn cfg_matches(cfg: &ast::MetaItem, sess: &ParseSess, features: Option<&Feat
} }
} }
}, },
ast::MetaItemKind::Word(_) | ast::MetaItemKind::NameValue(..) => { ast::MetaItemKind::Word | ast::MetaItemKind::NameValue(..) => {
if let (Some(feats), Some(gated_cfg)) = (features, GatedCfg::gate(cfg)) { if let (Some(feats), Some(gated_cfg)) = (features, GatedCfg::gate(cfg)) {
gated_cfg.check_and_emit(sess, feats); gated_cfg.check_and_emit(sess, feats);
} }
@ -880,7 +881,7 @@ pub fn require_unique_names(diagnostic: &Handler, metas: &[P<MetaItem>]) {
pub fn find_repr_attrs(diagnostic: &Handler, attr: &Attribute) -> Vec<ReprAttr> { pub fn find_repr_attrs(diagnostic: &Handler, attr: &Attribute) -> Vec<ReprAttr> {
let mut acc = Vec::new(); let mut acc = Vec::new();
match attr.value.node { match attr.value.node {
ast::MetaItemKind::List(s, ref items) if s == "repr" => { ast::MetaItemKind::List(ref items) if attr.value.name == "repr" => {
mark_used(attr); mark_used(attr);
for item in items { for item in items {
if !item.is_meta_item() { if !item.is_meta_item() {

View file

@ -133,7 +133,7 @@ impl<'a> StripUnconfigured<'a> {
} }
let mis = match attr.value.node { let mis = match attr.value.node {
ast::MetaItemKind::List(_, ref mis) if is_cfg(&attr) => mis, ast::MetaItemKind::List(ref mis) if is_cfg(&attr) => mis,
_ => return true _ => return true
}; };

View file

@ -991,9 +991,9 @@ fn contains_novel_literal(item: &ast::MetaItem) -> bool {
use ast::NestedMetaItemKind::*; use ast::NestedMetaItemKind::*;
match item.node { match item.node {
Word(..) => false, Word => false,
NameValue(_, ref lit) => !lit.node.is_str(), NameValue(ref lit) => !lit.node.is_str(),
List(_, ref list) => list.iter().any(|li| { List(ref list) => list.iter().any(|li| {
match li.node { match li.node {
MetaItem(ref mi) => contains_novel_literal(&**mi), MetaItem(ref mi) => contains_novel_literal(&**mi),
Literal(_) => true, Literal(_) => true,

View file

@ -520,13 +520,14 @@ pub fn noop_fold_meta_list_item<T: Folder>(li: NestedMetaItem, fld: &mut T)
} }
pub fn noop_fold_meta_item<T: Folder>(mi: P<MetaItem>, fld: &mut T) -> P<MetaItem> { pub fn noop_fold_meta_item<T: Folder>(mi: P<MetaItem>, fld: &mut T) -> P<MetaItem> {
mi.map(|Spanned {node, span}| Spanned { mi.map(|MetaItem { name, node, span }| MetaItem {
name: name,
node: match node { node: match node {
MetaItemKind::Word(id) => MetaItemKind::Word(id), MetaItemKind::Word => MetaItemKind::Word,
MetaItemKind::List(id, mis) => { MetaItemKind::List(mis) => {
MetaItemKind::List(id, mis.move_map(|e| fld.fold_meta_list_item(e))) MetaItemKind::List(mis.move_map(|e| fld.fold_meta_list_item(e)))
} },
MetaItemKind::NameValue(id, s) => MetaItemKind::NameValue(id, s) MetaItemKind::NameValue(s) => MetaItemKind::NameValue(s),
}, },
span: fld.new_span(span) span: fld.new_span(span)
}) })

View file

@ -227,23 +227,15 @@ impl<'a> Parser<'a> {
let lo = self.span.lo; let lo = self.span.lo;
let ident = self.parse_ident()?; let ident = self.parse_ident()?;
match self.token { let node = if self.eat(&token::Eq) {
token::Eq => { ast::MetaItemKind::NameValue(self.parse_unsuffixed_lit()?)
self.bump(); } else if self.token == token::OpenDelim(token::Paren) {
let lit = self.parse_unsuffixed_lit()?; ast::MetaItemKind::List(self.parse_meta_seq()?)
let hi = self.prev_span.hi; } else {
Ok(P(spanned(lo, hi, ast::MetaItemKind::NameValue(ident.name, lit)))) ast::MetaItemKind::Word
} };
token::OpenDelim(token::Paren) => { let hi = self.prev_span.hi;
let inner_items = self.parse_meta_seq()?; Ok(P(ast::MetaItem { name: ident.name, node: node, span: mk_sp(lo, hi) }))
let hi = self.prev_span.hi;
Ok(P(spanned(lo, hi, ast::MetaItemKind::List(ident.name, inner_items))))
}
_ => {
let hi = self.prev_span.hi;
Ok(P(spanned(lo, hi, ast::MetaItemKind::Word(ident.name))))
}
}
} }
/// matches meta_item_inner : (meta_item | UNSUFFIXED_LIT) ; /// matches meta_item_inner : (meta_item | UNSUFFIXED_LIT) ;

View file

@ -777,16 +777,16 @@ pub trait PrintState<'a> {
fn print_meta_item(&mut self, item: &ast::MetaItem) -> io::Result<()> { fn print_meta_item(&mut self, item: &ast::MetaItem) -> io::Result<()> {
try!(self.ibox(INDENT_UNIT)); try!(self.ibox(INDENT_UNIT));
match item.node { match item.node {
ast::MetaItemKind::Word(ref name) => { ast::MetaItemKind::Word => {
try!(word(self.writer(), &name.as_str())); try!(word(self.writer(), &item.name.as_str()));
} }
ast::MetaItemKind::NameValue(ref name, ref value) => { ast::MetaItemKind::NameValue(ref value) => {
try!(self.word_space(&name.as_str())); try!(self.word_space(&item.name.as_str()));
try!(self.word_space("=")); try!(self.word_space("="));
try!(self.print_literal(value)); try!(self.print_literal(value));
} }
ast::MetaItemKind::List(ref name, ref items) => { ast::MetaItemKind::List(ref items) => {
try!(word(self.writer(), &name.as_str())); try!(word(self.writer(), &item.name.as_str()));
try!(self.popen()); try!(self.popen());
try!(self.commasep(Consistent, try!(self.commasep(Consistent,
&items[..], &items[..],

View file

@ -70,7 +70,8 @@ pub fn maybe_inject_crates_ref(sess: &ParseSess,
attrs: vec![ast::Attribute { attrs: vec![ast::Attribute {
style: ast::AttrStyle::Outer, style: ast::AttrStyle::Outer,
value: P(ast::MetaItem { value: P(ast::MetaItem {
node: ast::MetaItemKind::Word(token::intern("prelude_import")), name: token::intern("prelude_import"),
node: ast::MetaItemKind::Word,
span: span, span: span,
}), }),
id: attr::mk_attr_id(), id: attr::mk_attr_id(),