1 //! Meta-syntax validation logic of attributes for post-expansion.
3 use rustc_errors::{PResult, Applicability};
4 use rustc_feature::{AttributeTemplate, BUILTIN_ATTRIBUTE_MAP};
5 use syntax::ast::{self, Attribute, AttrKind, Ident, MacArgs, MetaItem, MetaItemKind};
6 use syntax::attr::mk_name_value_item_str;
7 use syntax::early_buffered_lints::ILL_FORMED_ATTRIBUTE_INPUT;
8 use syntax::sess::ParseSess;
9 use syntax_pos::{Symbol, sym};
11 pub fn check_meta(sess: &ParseSess, attr: &Attribute) {
13 attr.ident().and_then(|ident| BUILTIN_ATTRIBUTE_MAP.get(&ident.name)).map(|a| **a);
15 // Check input tokens for built-in and key-value attributes.
17 // `rustc_dummy` doesn't have any restrictions specific to built-in attributes.
18 Some((name, _, template, _)) if name != sym::rustc_dummy =>
19 check_builtin_attribute(sess, attr, name, template),
20 _ => if let MacArgs::Eq(..) = attr.get_normal_item().args {
21 // All key-value attributes are restricted to meta-item syntax.
22 parse_meta(sess, attr).map_err(|mut err| err.emit()).ok();
27 pub fn parse_meta<'a>(sess: &'a ParseSess, attr: &Attribute) -> PResult<'a, MetaItem> {
29 AttrKind::Normal(ref item) => MetaItem {
30 path: item.path.clone(),
31 kind: super::parse_in_attr(sess, attr, |p| p.parse_meta_item_kind())?,
34 AttrKind::DocComment(comment) => {
35 mk_name_value_item_str(Ident::new(sym::doc, attr.span), comment, attr.span)
40 /// Checks that the given meta-item is compatible with this `AttributeTemplate`.
41 fn is_attr_template_compatible(template: &AttributeTemplate, meta: &ast::MetaItemKind) -> bool {
43 MetaItemKind::Word => template.word,
44 MetaItemKind::List(..) => template.list.is_some(),
45 MetaItemKind::NameValue(lit) if lit.kind.is_str() => template.name_value_str.is_some(),
46 MetaItemKind::NameValue(..) => false,
50 pub fn check_builtin_attribute(
54 template: AttributeTemplate,
56 // Some special attributes like `cfg` must be checked
57 // before the generic check, so we skip them here.
58 let should_skip = |name| name == sym::cfg;
59 // Some of previously accepted forms were used in practice,
60 // report them as warnings for now.
61 let should_warn = |name| name == sym::doc || name == sym::ignore ||
62 name == sym::inline || name == sym::link ||
63 name == sym::test || name == sym::bench;
65 match parse_meta(sess, attr) {
66 Ok(meta) => if !should_skip(name) && !is_attr_template_compatible(&template, &meta.kind) {
67 let error_msg = format!("malformed `{}` attribute input", name);
68 let mut msg = "attribute must be of the form ".to_owned();
69 let mut suggestions = vec![];
73 let code = format!("#[{}]", name);
74 msg.push_str(&format!("`{}`", &code));
75 suggestions.push(code);
77 if let Some(descr) = template.list {
82 let code = format!("#[{}({})]", name, descr);
83 msg.push_str(&format!("`{}`", &code));
84 suggestions.push(code);
86 if let Some(descr) = template.name_value_str {
90 let code = format!("#[{} = \"{}\"]", name, descr);
91 msg.push_str(&format!("`{}`", &code));
92 suggestions.push(code);
94 if should_warn(name) {
96 &ILL_FORMED_ATTRIBUTE_INPUT,
102 sess.span_diagnostic.struct_span_err(meta.span, &error_msg)
105 if suggestions.len() == 1 {
106 "must be of the form"
108 "the following are the possible correct uses"
110 suggestions.into_iter(),
111 Applicability::HasPlaceholders,
115 Err(mut err) => err.emit(),