Skip to content

Commit 6303640

Browse files
committed
Rollup merge of rust-lang#35850 - SergioBenitez:master, r=nrc
Implement RFC#1559: allow all literals in attributes Implemented rust-lang/rfcs#1559, tracked by rust-lang#34981.
2 parents 413ecde + 8250a26 commit 6303640

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

+12-7
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

+20-3
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
}
@@ -480,13 +484,26 @@ pub fn noop_fold_attribute<T: Folder>(at: Attribute, fld: &mut T) -> Option<Attr
480484
})
481485
}
482486

487+
pub fn noop_fold_meta_list_item<T: Folder>(li: NestedMetaItem, fld: &mut T)
488+
-> NestedMetaItem {
489+
Spanned {
490+
node: match li.node {
491+
NestedMetaItemKind::MetaItem(mi) => {
492+
NestedMetaItemKind::MetaItem(fld.fold_meta_item(mi))
493+
},
494+
NestedMetaItemKind::Literal(lit) => NestedMetaItemKind::Literal(lit)
495+
},
496+
span: fld.new_span(li.span)
497+
}
498+
}
499+
483500
pub fn noop_fold_meta_item<T: Folder>(mi: P<MetaItem>, fld: &mut T) -> P<MetaItem> {
484501
mi.map(|Spanned { node, span }| {
485502
Spanned {
486503
node: match node {
487504
MetaItemKind::Word(id) => MetaItemKind::Word(id),
488505
MetaItemKind::List(id, mis) => {
489-
MetaItemKind::List(id, mis.move_map(|e| fld.fold_meta_item(e)))
506+
MetaItemKind::List(id, mis.move_map(|e| fld.fold_meta_list_item(e)))
490507
}
491508
MetaItemKind::NameValue(id, s) => MetaItemKind::NameValue(id, s),
492509
},

src/librustc/lint/context.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use util::nodemap::FnvHashMap;
3838
use std::cmp;
3939
use std::default::Default as StdDefault;
4040
use std::mem;
41-
use syntax::attr::{self, AttrMetaMethods};
41+
use syntax::attr::{self, AttrMetaMethods, AttrNestedMetaItemMethods};
4242
use syntax::parse::token::InternedString;
4343
use syntax::ast;
4444
use syntax_pos::Span;
@@ -372,12 +372,10 @@ pub fn gather_attr(attr: &ast::Attribute)
372372
return out;
373373
};
374374

375-
for meta in metas {
376-
out.push(if meta.is_word() {
377-
Ok((meta.name().clone(), level, meta.span))
378-
} else {
379-
Err(meta.span)
380-
});
375+
for li in metas {
376+
out.push(li.word().map_or(Err(li.span), |word| {
377+
Ok((word.name().clone(), level, word.span))
378+
}));
381379
}
382380

383381
out

src/librustc_borrowck/borrowck/mir/mod.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use borrowck::BorrowckCtxt;
1212

1313
use syntax::ast::{self, MetaItem};
14-
use syntax::attr::AttrMetaMethods;
14+
use syntax::attr::{AttrMetaMethods, AttrNestedMetaItemMethods};
1515
use syntax::ptr::P;
1616
use syntax_pos::{Span, DUMMY_SP};
1717

@@ -43,8 +43,9 @@ fn has_rustc_mir_with(attrs: &[ast::Attribute], name: &str) -> Option<P<MetaItem
4343
if attr.check_name("rustc_mir") {
4444
let items = attr.meta_item_list();
4545
for item in items.iter().flat_map(|l| l.iter()) {
46-
if item.check_name(name) {
47-
return Some(item.clone())
46+
match item.meta_item() {
47+
Some(mi) if mi.check_name(name) => return Some(mi.clone()),
48+
_ => continue
4849
}
4950
}
5051
}

src/librustc_driver/lib.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -655,17 +655,19 @@ impl RustcDefaultCalls {
655655
if !allow_unstable_cfg && GatedCfg::gate(&*cfg).is_some() {
656656
continue;
657657
}
658+
658659
if cfg.is_word() {
659660
println!("{}", cfg.name());
660-
} else if cfg.is_value_str() {
661-
if let Some(s) = cfg.value_str() {
662-
println!("{}=\"{}\"", cfg.name(), s);
663-
}
661+
} else if let Some(s) = cfg.value_str() {
662+
println!("{}=\"{}\"", cfg.name(), s);
664663
} else if cfg.is_meta_item_list() {
665664
// Right now there are not and should not be any
666665
// MetaItemKind::List items in the configuration returned by
667666
// `build_configuration`.
668-
panic!("MetaItemKind::List encountered in default cfg")
667+
panic!("Found an unexpected list in cfg attribute '{}'!", cfg.name())
668+
} else {
669+
// There also shouldn't be literals.
670+
panic!("Found an unexpected literal in cfg attribute '{}'!", cfg.name())
669671
}
670672
}
671673
}

src/librustc_incremental/assert_dep_graph.rs

+24-15
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ use std::env;
5656
use std::fs::File;
5757
use std::io::Write;
5858
use syntax::ast;
59-
use syntax::attr::AttrMetaMethods;
59+
use syntax::attr::{AttrNestedMetaItemMethods, AttrMetaMethods};
6060
use syntax::parse::token::InternedString;
6161
use syntax_pos::Span;
6262

@@ -116,31 +116,40 @@ impl<'a, 'tcx> IfThisChanged<'a, 'tcx> {
116116
for attr in self.tcx.get_attrs(def_id).iter() {
117117
if attr.check_name(IF_THIS_CHANGED) {
118118
let mut id = None;
119-
for meta_item in attr.meta_item_list().unwrap_or_default() {
120-
if meta_item.is_word() && id.is_none() {
121-
id = Some(meta_item.name().clone());
122-
} else {
123-
// FIXME better-encapsulate meta_item (don't directly access `node`)
124-
span_bug!(meta_item.span(), "unexpected meta-item {:?}", meta_item.node)
119+
for list_item in attr.meta_item_list().unwrap_or_default() {
120+
match list_item.word() {
121+
Some(word) if id.is_none() => {
122+
id = Some(word.name().clone())
123+
},
124+
_ => {
125+
// FIXME better-encapsulate meta_item (don't directly access `node`)
126+
span_bug!(list_item.span(), "unexpected list-item {:?}", list_item.node)
127+
}
125128
}
126129
}
130+
127131
let id = id.unwrap_or(InternedString::new(ID));
128132
self.if_this_changed.entry(id)
129133
.or_insert(FnvHashSet())
130134
.insert((attr.span, def_id, DepNode::Hir(def_id)));
131135
} else if attr.check_name(THEN_THIS_WOULD_NEED) {
132136
let mut dep_node_interned = None;
133137
let mut id = None;
134-
for meta_item in attr.meta_item_list().unwrap_or_default() {
135-
if meta_item.is_word() && dep_node_interned.is_none() {
136-
dep_node_interned = Some(meta_item.name().clone());
137-
} else if meta_item.is_word() && id.is_none() {
138-
id = Some(meta_item.name().clone());
139-
} else {
140-
// FIXME better-encapsulate meta_item (don't directly access `node`)
141-
span_bug!(meta_item.span(), "unexpected meta-item {:?}", meta_item.node)
138+
for list_item in attr.meta_item_list().unwrap_or_default() {
139+
match list_item.word() {
140+
Some(word) if dep_node_interned.is_none() => {
141+
dep_node_interned = Some(word.name().clone());
142+
},
143+
Some(word) if id.is_none() => {
144+
id = Some(word.name().clone())
145+
},
146+
_ => {
147+
// FIXME better-encapsulate meta_item (don't directly access `node`)
148+
span_bug!(list_item.span(), "unexpected meta-item {:?}", list_item.node)
149+
}
142150
}
143151
}
152+
144153
let dep_node = match dep_node_interned {
145154
Some(ref n) => {
146155
match DepNode::from_label_string(&n[..], def_id) {

src/librustc_incremental/persist/dirty_clean.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ use rustc::hir;
3131
use rustc::hir::def_id::DefId;
3232
use rustc::hir::intravisit::Visitor;
3333
use rustc_data_structures::fnv::FnvHashSet;
34-
use syntax::ast::{self, Attribute, MetaItem};
35-
use syntax::attr::AttrMetaMethods;
34+
use syntax::ast::{self, Attribute, NestedMetaItem};
35+
use syntax::attr::{AttrNestedMetaItemMethods, AttrMetaMethods};
3636
use syntax::parse::token::InternedString;
3737
use rustc::ty::TyCtxt;
3838

@@ -71,13 +71,17 @@ pub struct DirtyCleanVisitor<'a, 'tcx:'a> {
7171
}
7272

7373
impl<'a, 'tcx> DirtyCleanVisitor<'a, 'tcx> {
74-
fn expect_associated_value(&self, item: &MetaItem) -> InternedString {
74+
fn expect_associated_value(&self, item: &NestedMetaItem) -> InternedString {
7575
if let Some(value) = item.value_str() {
7676
value
7777
} else {
78-
self.tcx.sess.span_fatal(
79-
item.span,
80-
&format!("associated value expected for `{}`", item.name()));
78+
let msg = if let Some(name) = item.name() {
79+
format!("associated value expected for `{}`", name)
80+
} else {
81+
"expected an associated value".to_string()
82+
};
83+
84+
self.tcx.sess.span_fatal(item.span, &msg);
8185
}
8286
}
8387

src/librustc_lint/builtin.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ use lint::{LintPass, LateLintPass};
4444
use std::collections::HashSet;
4545

4646
use syntax::{ast};
47-
use syntax::attr::{self, AttrMetaMethods, AttributeMethods};
48-
use syntax_pos::Span;
47+
use syntax::attr::{self, AttrMetaMethods, AttrNestedMetaItemMethods, AttributeMethods};
48+
use syntax_pos::{Span};
4949

5050
use rustc::hir::{self, PatKind};
5151
use rustc::hir::intravisit::FnKind;
@@ -317,7 +317,7 @@ impl LateLintPass for MissingDoc {
317317
let doc_hidden = self.doc_hidden() || attrs.iter().any(|attr| {
318318
attr.check_name("doc") && match attr.meta_item_list() {
319319
None => false,
320-
Some(l) => attr::contains_name(&l[..], "hidden"),
320+
Some(l) => attr::list_contains_name(&l[..], "hidden"),
321321
}
322322
});
323323
self.doc_hidden_stack.push(doc_hidden);

src/librustc_lint/unused.rs

+7
Original file line numberDiff line numberDiff line change
@@ -235,10 +235,13 @@ impl LintPass for UnusedAttributes {
235235

236236
impl LateLintPass for UnusedAttributes {
237237
fn check_attribute(&mut self, cx: &LateContext, attr: &ast::Attribute) {
238+
debug!("checking attribute: {:?}", attr);
239+
238240
// Note that check_name() marks the attribute as used if it matches.
239241
for &(ref name, ty, _) in KNOWN_ATTRIBUTES {
240242
match ty {
241243
AttributeType::Whitelisted if attr.check_name(name) => {
244+
debug!("{:?} is Whitelisted", name);
242245
break;
243246
},
244247
_ => ()
@@ -248,11 +251,13 @@ impl LateLintPass for UnusedAttributes {
248251
let plugin_attributes = cx.sess().plugin_attributes.borrow_mut();
249252
for &(ref name, ty) in plugin_attributes.iter() {
250253
if ty == AttributeType::Whitelisted && attr.check_name(&name) {
254+
debug!("{:?} (plugin attr) is whitelisted with ty {:?}", name, ty);
251255
break;
252256
}
253257
}
254258

255259
if !attr::is_used(attr) {
260+
debug!("Emitting warning for: {:?}", attr);
256261
cx.span_lint(UNUSED_ATTRIBUTES, attr.span, "unused attribute");
257262
// Is it a builtin attribute that must be used at the crate level?
258263
let known_crate = KNOWN_ATTRIBUTES.iter().find(|&&(name, ty, _)| {
@@ -276,6 +281,8 @@ impl LateLintPass for UnusedAttributes {
276281
};
277282
cx.span_lint(UNUSED_ATTRIBUTES, attr.span, msg);
278283
}
284+
} else {
285+
debug!("Attr was used: {:?}", attr);
279286
}
280287
}
281288
}

src/librustc_metadata/common.rs

+1-14
Original file line numberDiff line numberDiff line change
@@ -45,26 +45,13 @@ pub const tag_items_closure_kind: usize = 0x2a;
4545
pub const tag_items_closure_ty: usize = 0x2b;
4646
pub const tag_def_key: usize = 0x2c;
4747

48-
// GAP 0x2d 0x2e
48+
// GAP 0x2d 0x34
4949

5050
pub const tag_index: usize = 0x110; // top-level only
5151
pub const tag_xref_index: usize = 0x111; // top-level only
5252
pub const tag_xref_data: usize = 0x112; // top-level only
53-
54-
pub const tag_meta_item_name_value: usize = 0x2f;
55-
56-
pub const tag_meta_item_name: usize = 0x30;
57-
58-
pub const tag_meta_item_value: usize = 0x31;
59-
6053
pub const tag_attributes: usize = 0x101; // top-level only
6154

62-
pub const tag_attribute: usize = 0x32;
63-
64-
pub const tag_meta_item_word: usize = 0x33;
65-
66-
pub const tag_meta_item_list: usize = 0x34;
67-
6855
// The list of crates that this crate depends on
6956
pub const tag_crate_deps: usize = 0x102; // top-level only
7057

src/librustc_metadata/creader.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ use syntax::ast;
3535
use syntax::abi::Abi;
3636
use syntax::codemap;
3737
use syntax::parse;
38-
use syntax::attr;
39-
use syntax::attr::AttrMetaMethods;
38+
use syntax::attr::{self, AttrMetaMethods, AttrNestedMetaItemMethods};
4039
use syntax::parse::token::InternedString;
4140
use syntax::visit;
4241
use syntax_pos::{self, Span, mk_sp, Pos};

0 commit comments

Comments
 (0)