Skip to content

Commit e97686d

Browse files
committed
Move MetaItemKind's Name to a field of MetaItem.
1 parent 4b9b0d3 commit e97686d

File tree

10 files changed

+70
-71
lines changed

10 files changed

+70
-71
lines changed

src/librustc_driver/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,8 @@ impl RustcDefaultCalls {
615615
let mut cfgs = Vec::new();
616616
for &(name, ref value) in sess.parse_sess.config.iter() {
617617
let gated_cfg = GatedCfg::gate(&ast::MetaItem {
618-
node: ast::MetaItemKind::Word(name),
618+
name: name,
619+
node: ast::MetaItemKind::Word,
619620
span: DUMMY_SP,
620621
});
621622
if !allow_unstable_cfg && gated_cfg.is_some() {

src/librustc_incremental/calculate_svh/svh_visitor.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -874,22 +874,20 @@ impl<'a, 'hash, 'tcx> StrictVersionHashVisitor<'a, 'hash, 'tcx> {
874874

875875
// ignoring span information, it doesn't matter here
876876
self.hash_discriminant(&meta_item.node);
877+
let name = &*meta_item.name.as_str();
877878
match meta_item.node {
878-
ast::MetaItemKind::Word(s) => {
879-
let s = &*s.as_str();
880-
s.len().hash(self.st);
881-
s.hash(self.st);
879+
ast::MetaItemKind::Word => {
880+
name.len().hash(self.st);
881+
name.hash(self.st);
882882
}
883-
ast::MetaItemKind::NameValue(s, ref lit) => {
884-
let s = &*s.as_str();
885-
s.len().hash(self.st);
886-
s.hash(self.st);
883+
ast::MetaItemKind::NameValue(ref lit) => {
884+
name.len().hash(self.st);
885+
name.hash(self.st);
887886
lit.node.hash(self.st);
888887
}
889-
ast::MetaItemKind::List(s, ref items) => {
890-
let s = &*s.as_str();
891-
s.len().hash(self.st);
892-
s.hash(self.st);
888+
ast::MetaItemKind::List(ref items) => {
889+
name.len().hash(self.st);
890+
name.hash(self.st);
893891
// Sort subitems so the hash does not depend on their order
894892
let indices = self.indices_sorted_by(&items, |p| {
895893
(p.name(), fnv::hash(&p.literal().map(|i| &i.node)))

src/libsyntax/ast.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,12 @@ pub enum NestedMetaItemKind {
515515
/// A spanned compile-time attribute item.
516516
///
517517
/// E.g. `#[test]`, `#[derive(..)]` or `#[feature = "foo"]`
518-
pub type MetaItem = Spanned<MetaItemKind>;
518+
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
519+
pub struct MetaItem {
520+
pub name: Name,
521+
pub node: MetaItemKind,
522+
pub span: Span,
523+
}
519524

520525
/// A compile-time attribute item.
521526
///
@@ -525,15 +530,15 @@ pub enum MetaItemKind {
525530
/// Word meta item.
526531
///
527532
/// E.g. `test` as in `#[test]`
528-
Word(Name),
533+
Word,
529534
/// List meta item.
530535
///
531536
/// E.g. `derive(..)` as in `#[derive(..)]`
532-
List(Name, Vec<NestedMetaItem>),
537+
List(Vec<NestedMetaItem>),
533538
/// Name value meta item.
534539
///
535540
/// E.g. `feature = "foo"` as in `#[feature = "foo"]`
536-
NameValue(Name, Lit),
541+
NameValue(Lit)
537542
}
538543

539544
/// A Block (`{ .. }`).

src/libsyntax/attr.rs

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use ast;
1818
use ast::{AttrId, Attribute, Name};
1919
use ast::{MetaItem, MetaItemKind, NestedMetaItem, NestedMetaItemKind};
2020
use ast::{Lit, Expr, Item, Local, Stmt, StmtKind};
21-
use codemap::{respan, spanned, dummy_spanned, mk_sp};
21+
use codemap::{spanned, dummy_spanned, mk_sp};
2222
use syntax_pos::{Span, BytePos, DUMMY_SP};
2323
use errors::Handler;
2424
use feature_gate::{Features, GatedCfg};
@@ -219,16 +219,12 @@ impl Attribute {
219219

220220
impl MetaItem {
221221
pub fn name(&self) -> Name {
222-
match self.node {
223-
MetaItemKind::Word(n) => n,
224-
MetaItemKind::NameValue(n, _) => n,
225-
MetaItemKind::List(n, _) => n,
226-
}
222+
self.name
227223
}
228224

229225
pub fn value_str(&self) -> Option<InternedString> {
230226
match self.node {
231-
MetaItemKind::NameValue(_, ref v) => {
227+
MetaItemKind::NameValue(ref v) => {
232228
match v.node {
233229
ast::LitKind::Str(ref s, _) => Some((*s).clone()),
234230
_ => None,
@@ -240,14 +236,14 @@ impl MetaItem {
240236

241237
pub fn meta_item_list(&self) -> Option<&[NestedMetaItem]> {
242238
match self.node {
243-
MetaItemKind::List(_, ref l) => Some(&l[..]),
239+
MetaItemKind::List(ref l) => Some(&l[..]),
244240
_ => None
245241
}
246242
}
247243

248244
pub fn is_word(&self) -> bool {
249245
match self.node {
250-
MetaItemKind::Word(_) => true,
246+
MetaItemKind::Word => true,
251247
_ => false,
252248
}
253249
}
@@ -320,15 +316,15 @@ pub fn mk_word_item(name: Name) -> P<MetaItem> {
320316
}
321317

322318
pub fn mk_spanned_name_value_item(sp: Span, name: Name, value: ast::Lit) -> P<MetaItem> {
323-
P(respan(sp, MetaItemKind::NameValue(name, value)))
319+
P(MetaItem { span: sp, name: name, node: MetaItemKind::NameValue(value) })
324320
}
325321

326322
pub fn mk_spanned_list_item(sp: Span, name: Name, items: Vec<NestedMetaItem>) -> P<MetaItem> {
327-
P(respan(sp, MetaItemKind::List(name, items)))
323+
P(MetaItem { span: sp, name: name, node: MetaItemKind::List(items) })
328324
}
329325

330326
pub fn mk_spanned_word_item(sp: Span, name: Name) -> P<MetaItem> {
331-
P(respan(sp, MetaItemKind::Word(name)))
327+
P(MetaItem { span: sp, name: name, node: MetaItemKind::Word })
332328
}
333329

334330

@@ -394,7 +390,11 @@ pub fn mk_sugared_doc_attr(id: AttrId, text: InternedString, lo: BytePos, hi: By
394390
Attribute {
395391
id: id,
396392
style: style,
397-
value: P(spanned(lo, hi, MetaItemKind::NameValue(token::intern("doc"), lit))),
393+
value: P(MetaItem {
394+
span: mk_sp(lo, hi),
395+
name: token::intern("doc"),
396+
node: MetaItemKind::NameValue(lit),
397+
}),
398398
is_sugared_doc: true,
399399
span: mk_sp(lo, hi),
400400
}
@@ -472,13 +472,14 @@ pub enum InlineAttr {
472472

473473
/// Determine what `#[inline]` attribute is present in `attrs`, if any.
474474
pub fn find_inline_attr(diagnostic: Option<&Handler>, attrs: &[Attribute]) -> InlineAttr {
475-
attrs.iter().fold(InlineAttr::None, |ia,attr| {
475+
attrs.iter().fold(InlineAttr::None, |ia, attr| {
476476
match attr.value.node {
477-
MetaItemKind::Word(n) if n == "inline" => {
477+
_ if attr.value.name != "inline" => ia,
478+
MetaItemKind::Word => {
478479
mark_used(attr);
479480
InlineAttr::Hint
480481
}
481-
MetaItemKind::List(n, ref items) if n == "inline" => {
482+
MetaItemKind::List(ref items) => {
482483
mark_used(attr);
483484
if items.len() != 1 {
484485
diagnostic.map(|d|{ span_err!(d, attr.span, E0534, "expected one argument"); });
@@ -511,7 +512,7 @@ pub fn requests_inline(attrs: &[Attribute]) -> bool {
511512
/// Tests if a cfg-pattern matches the cfg set
512513
pub fn cfg_matches(cfg: &ast::MetaItem, sess: &ParseSess, features: Option<&Features>) -> bool {
513514
match cfg.node {
514-
ast::MetaItemKind::List(ref pred, ref mis) => {
515+
ast::MetaItemKind::List(ref mis) => {
515516
for mi in mis.iter() {
516517
if !mi.is_meta_item() {
517518
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
521522

522523
// The unwraps below may look dangerous, but we've already asserted
523524
// that they won't fail with the loop above.
524-
match &*pred.as_str() {
525+
match &*cfg.name.as_str() {
525526
"any" => mis.iter().any(|mi| {
526527
cfg_matches(mi.meta_item().unwrap(), sess, features)
527528
}),
@@ -542,7 +543,7 @@ pub fn cfg_matches(cfg: &ast::MetaItem, sess: &ParseSess, features: Option<&Feat
542543
}
543544
}
544545
},
545-
ast::MetaItemKind::Word(_) | ast::MetaItemKind::NameValue(..) => {
546+
ast::MetaItemKind::Word | ast::MetaItemKind::NameValue(..) => {
546547
if let (Some(feats), Some(gated_cfg)) = (features, GatedCfg::gate(cfg)) {
547548
gated_cfg.check_and_emit(sess, feats);
548549
}
@@ -880,7 +881,7 @@ pub fn require_unique_names(diagnostic: &Handler, metas: &[P<MetaItem>]) {
880881
pub fn find_repr_attrs(diagnostic: &Handler, attr: &Attribute) -> Vec<ReprAttr> {
881882
let mut acc = Vec::new();
882883
match attr.value.node {
883-
ast::MetaItemKind::List(s, ref items) if s == "repr" => {
884+
ast::MetaItemKind::List(ref items) if attr.value.name == "repr" => {
884885
mark_used(attr);
885886
for item in items {
886887
if !item.is_meta_item() {

src/libsyntax/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ impl<'a> StripUnconfigured<'a> {
133133
}
134134

135135
let mis = match attr.value.node {
136-
ast::MetaItemKind::List(_, ref mis) if is_cfg(&attr) => mis,
136+
ast::MetaItemKind::List(ref mis) if is_cfg(&attr) => mis,
137137
_ => return true
138138
};
139139

src/libsyntax/feature_gate.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -991,9 +991,9 @@ fn contains_novel_literal(item: &ast::MetaItem) -> bool {
991991
use ast::NestedMetaItemKind::*;
992992

993993
match item.node {
994-
Word(..) => false,
995-
NameValue(_, ref lit) => !lit.node.is_str(),
996-
List(_, ref list) => list.iter().any(|li| {
994+
Word => false,
995+
NameValue(ref lit) => !lit.node.is_str(),
996+
List(ref list) => list.iter().any(|li| {
997997
match li.node {
998998
MetaItem(ref mi) => contains_novel_literal(&**mi),
999999
Literal(_) => true,

src/libsyntax/fold.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -520,13 +520,14 @@ pub fn noop_fold_meta_list_item<T: Folder>(li: NestedMetaItem, fld: &mut T)
520520
}
521521

522522
pub fn noop_fold_meta_item<T: Folder>(mi: P<MetaItem>, fld: &mut T) -> P<MetaItem> {
523-
mi.map(|Spanned {node, span}| Spanned {
523+
mi.map(|MetaItem { name, node, span }| MetaItem {
524+
name: name,
524525
node: match node {
525-
MetaItemKind::Word(id) => MetaItemKind::Word(id),
526-
MetaItemKind::List(id, mis) => {
527-
MetaItemKind::List(id, mis.move_map(|e| fld.fold_meta_list_item(e)))
528-
}
529-
MetaItemKind::NameValue(id, s) => MetaItemKind::NameValue(id, s)
526+
MetaItemKind::Word => MetaItemKind::Word,
527+
MetaItemKind::List(mis) => {
528+
MetaItemKind::List(mis.move_map(|e| fld.fold_meta_list_item(e)))
529+
},
530+
MetaItemKind::NameValue(s) => MetaItemKind::NameValue(s),
530531
},
531532
span: fld.new_span(span)
532533
})

src/libsyntax/parse/attr.rs

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -227,23 +227,15 @@ impl<'a> Parser<'a> {
227227

228228
let lo = self.span.lo;
229229
let ident = self.parse_ident()?;
230-
match self.token {
231-
token::Eq => {
232-
self.bump();
233-
let lit = self.parse_unsuffixed_lit()?;
234-
let hi = self.prev_span.hi;
235-
Ok(P(spanned(lo, hi, ast::MetaItemKind::NameValue(ident.name, lit))))
236-
}
237-
token::OpenDelim(token::Paren) => {
238-
let inner_items = self.parse_meta_seq()?;
239-
let hi = self.prev_span.hi;
240-
Ok(P(spanned(lo, hi, ast::MetaItemKind::List(ident.name, inner_items))))
241-
}
242-
_ => {
243-
let hi = self.prev_span.hi;
244-
Ok(P(spanned(lo, hi, ast::MetaItemKind::Word(ident.name))))
245-
}
246-
}
230+
let node = if self.eat(&token::Eq) {
231+
ast::MetaItemKind::NameValue(self.parse_unsuffixed_lit()?)
232+
} else if self.token == token::OpenDelim(token::Paren) {
233+
ast::MetaItemKind::List(self.parse_meta_seq()?)
234+
} else {
235+
ast::MetaItemKind::Word
236+
};
237+
let hi = self.prev_span.hi;
238+
Ok(P(ast::MetaItem { name: ident.name, node: node, span: mk_sp(lo, hi) }))
247239
}
248240

249241
/// matches meta_item_inner : (meta_item | UNSUFFIXED_LIT) ;

src/libsyntax/print/pprust.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -777,16 +777,16 @@ pub trait PrintState<'a> {
777777
fn print_meta_item(&mut self, item: &ast::MetaItem) -> io::Result<()> {
778778
try!(self.ibox(INDENT_UNIT));
779779
match item.node {
780-
ast::MetaItemKind::Word(ref name) => {
781-
try!(word(self.writer(), &name.as_str()));
780+
ast::MetaItemKind::Word => {
781+
try!(word(self.writer(), &item.name.as_str()));
782782
}
783-
ast::MetaItemKind::NameValue(ref name, ref value) => {
784-
try!(self.word_space(&name.as_str()));
783+
ast::MetaItemKind::NameValue(ref value) => {
784+
try!(self.word_space(&item.name.as_str()));
785785
try!(self.word_space("="));
786786
try!(self.print_literal(value));
787787
}
788-
ast::MetaItemKind::List(ref name, ref items) => {
789-
try!(word(self.writer(), &name.as_str()));
788+
ast::MetaItemKind::List(ref items) => {
789+
try!(word(self.writer(), &item.name.as_str()));
790790
try!(self.popen());
791791
try!(self.commasep(Consistent,
792792
&items[..],

src/libsyntax/std_inject.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ pub fn maybe_inject_crates_ref(sess: &ParseSess,
7070
attrs: vec![ast::Attribute {
7171
style: ast::AttrStyle::Outer,
7272
value: P(ast::MetaItem {
73-
node: ast::MetaItemKind::Word(token::intern("prelude_import")),
73+
name: token::intern("prelude_import"),
74+
node: ast::MetaItemKind::Word,
7475
span: span,
7576
}),
7677
id: attr::mk_attr_id(),

0 commit comments

Comments
 (0)