1 //! Meta-syntax validation logic of attributes for post-expansion.
3 use errors::{PResult, Applicability};
4 use rustc_feature::{AttributeTemplate, BUILTIN_ATTRIBUTE_MAP};
5 use syntax::ast::{self, Attribute, AttrKind, Ident, MetaItem, MetaItemKind};
6 use syntax::attr::mk_name_value_item_str;
7 use syntax::early_buffered_lints::BufferedEarlyLintId;
9 use syntax::tokenstream::TokenTree;
10 use syntax::sess::ParseSess;
11 use syntax_pos::{Symbol, sym};
13 pub fn check_meta(sess: &ParseSess, attr: &Attribute) {
15 attr.ident().and_then(|ident| BUILTIN_ATTRIBUTE_MAP.get(&ident.name)).map(|a| **a);
17 // Check input tokens for built-in and key-value attributes.
19 // `rustc_dummy` doesn't have any restrictions specific to built-in attributes.
20 Some((name, _, template, _)) if name != sym::rustc_dummy =>
21 check_builtin_attribute(sess, attr, name, template),
22 _ => if let Some(TokenTree::Token(token)) = attr.get_normal_item().tokens.trees().next() {
23 if token == token::Eq {
24 // All key-value attributes are restricted to meta-item syntax.
25 parse_meta(sess, attr).map_err(|mut err| err.emit()).ok();
31 pub fn parse_meta<'a>(sess: &'a ParseSess, attr: &Attribute) -> PResult<'a, MetaItem> {
33 AttrKind::Normal(ref item) => MetaItem {
34 path: item.path.clone(),
35 kind: super::parse_in_attr(sess, attr, |p| p.parse_meta_item_kind())?,
38 AttrKind::DocComment(comment) => {
39 mk_name_value_item_str(Ident::new(sym::doc, attr.span), comment, attr.span)
44 /// Checks that the given meta-item is compatible with this `AttributeTemplate`.
45 fn is_attr_template_compatible(template: &AttributeTemplate, meta: &ast::MetaItemKind) -> bool {
47 MetaItemKind::Word => template.word,
48 MetaItemKind::List(..) => template.list.is_some(),
49 MetaItemKind::NameValue(lit) if lit.kind.is_str() => template.name_value_str.is_some(),
50 MetaItemKind::NameValue(..) => false,
54 pub fn check_builtin_attribute(
58 template: AttributeTemplate,
60 // Some special attributes like `cfg` must be checked
61 // before the generic check, so we skip them here.
62 let should_skip = |name| name == sym::cfg;
63 // Some of previously accepted forms were used in practice,
64 // report them as warnings for now.
65 let should_warn = |name| name == sym::doc || name == sym::ignore ||
66 name == sym::inline || name == sym::link ||
67 name == sym::test || name == sym::bench;
69 match parse_meta(sess, attr) {
70 Ok(meta) => if !should_skip(name) && !is_attr_template_compatible(&template, &meta.kind) {
71 let error_msg = format!("malformed `{}` attribute input", name);
72 let mut msg = "attribute must be of the form ".to_owned();
73 let mut suggestions = vec![];
77 let code = format!("#[{}]", name);
78 msg.push_str(&format!("`{}`", &code));
79 suggestions.push(code);
81 if let Some(descr) = template.list {
86 let code = format!("#[{}({})]", name, descr);
87 msg.push_str(&format!("`{}`", &code));
88 suggestions.push(code);
90 if let Some(descr) = template.name_value_str {
94 let code = format!("#[{} = \"{}\"]", name, descr);
95 msg.push_str(&format!("`{}`", &code));
96 suggestions.push(code);
98 if should_warn(name) {
100 BufferedEarlyLintId::IllFormedAttributeInput,
106 sess.span_diagnostic.struct_span_err(meta.span, &error_msg)
109 if suggestions.len() == 1 {
110 "must be of the form"
112 "the following are the possible correct uses"
114 suggestions.into_iter(),
115 Applicability::HasPlaceholders,
119 Err(mut err) => err.emit(),