Skip to content

Commit 8250a26

Browse files
committedAug 25, 2016
Implement RFC#1559: allow all literals in attributes.
1 parent 528c6f3 commit 8250a26

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+942
-373
lines changed
 

‎src/librustc/hir/check_attr.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use session::Session;
1212

1313
use syntax::ast;
14-
use syntax::attr::AttrMetaMethods;
14+
use syntax::attr::{AttrNestedMetaItemMethods, AttrMetaMethods};
1515
use syntax::visit;
1616
use syntax::visit::Visitor;
1717

@@ -52,18 +52,22 @@ impl<'a> CheckAttrVisitor<'a> {
5252
return;
5353
}
5454
};
55+
5556
for word in words {
56-
let word: &str = &word.name();
57-
let message = match word {
57+
let name = match word.name() {
58+
Some(word) => word,
59+
None => continue,
60+
};
61+
62+
let message = match &*name {
5863
"C" => {
5964
if target != Target::Struct && target != Target::Enum {
60-
"attribute should be applied to struct or enum"
65+
"attribute should be applied to struct or enum"
6166
} else {
6267
continue
6368
}
6469
}
65-
"packed" |
66-
"simd" => {
70+
"packed" | "simd" => {
6771
if target != Target::Struct {
6872
"attribute should be applied to struct"
6973
} else {
@@ -74,13 +78,14 @@ impl<'a> CheckAttrVisitor<'a> {
7478
"i32" | "u32" | "i64" | "u64" |
7579
"isize" | "usize" => {
7680
if target != Target::Enum {
77-
"attribute should be applied to enum"
81+
"attribute should be applied to enum"
7882
} else {
7983
continue
8084
}
8185
}
8286
_ => continue,
8387
};
88+
8489
span_err!(self.sess, attr.span, E0517, "{}", message);
8590
}
8691
}

‎src/librustc/hir/fold.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
//! and returns a piece of the same type.
1313
1414
use hir::*;
15-
use syntax::ast::{Name, NodeId, DUMMY_NODE_ID, Attribute, Attribute_, MetaItem};
16-
use syntax::ast::MetaItemKind;
15+
use syntax::ast::{Name, NodeId, DUMMY_NODE_ID, Attribute, Attribute_};
16+
use syntax::ast::{NestedMetaItem, NestedMetaItemKind, MetaItem, MetaItemKind};
1717
use hir;
1818
use syntax_pos::Span;
1919
use syntax::codemap::{respan, Spanned};
@@ -38,6 +38,10 @@ pub trait Folder : Sized {
3838
noop_fold_meta_items(meta_items, self)
3939
}
4040

41+
fn fold_meta_list_item(&mut self, list_item: NestedMetaItem) -> NestedMetaItem {
42+
noop_fold_meta_list_item(list_item, self)
43+
}
44+
4145
fn fold_meta_item(&mut self, meta_item: P<MetaItem>) -> P<MetaItem> {
4246
noop_fold_meta_item(meta_item, self)
4347
}
@@ -486,13 +490,26 @@ pub fn noop_fold_attribute<T: Folder>(at: Attribute, fld: &mut T) -> Option<Attr
486490
})
487491
}
488492

493+
pub fn noop_fold_meta_list_item<T: Folder>(li: NestedMetaItem, fld: &mut T)
494+
-> NestedMetaItem {
495+
Spanned {
496+
node: match li.node {
497+
NestedMetaItemKind::MetaItem(mi) => {
498+
NestedMetaItemKind::MetaItem(fld.fold_meta_item(mi))
499+
},
500+
NestedMetaItemKind::Literal(lit) => NestedMetaItemKind::Literal(lit)
501+
},
502+
span: fld.new_span(li.span)
503+
}
504+
}
505+
489506
pub fn noop_fold_meta_item<T: Folder>(mi: P<MetaItem>, fld: &mut T) -> P<MetaItem> {
490507
mi.map(|Spanned { node, span }| {
491508
Spanned {
492509
node: match node {
493510
MetaItemKind::Word(id) => MetaItemKind::Word(id),
494511
MetaItemKind::List(id, mis) => {
495-
MetaItemKind::List(id, mis.move_map(|e| fld.fold_meta_item(e)))
512+
MetaItemKind::List(id, mis.move_map(|e| fld.fold_meta_list_item(e)))
496513
}
497514
MetaItemKind::NameValue(id, s) => MetaItemKind::NameValue(id, s),
498515
},

0 commit comments

Comments
 (0)
Please sign in to comment.