1 //! Meta-syntax validation logic of attributes for post-expansion.
5 use rustc_errors::{PResult, Applicability};
6 use rustc_feature::{AttributeTemplate, BUILTIN_ATTRIBUTE_MAP};
7 use syntax::ast::{self, Attribute, AttrKind, Ident, MacArgs, MacDelimiter, MetaItem, MetaItemKind};
8 use syntax::attr::mk_name_value_item_str;
9 use syntax::early_buffered_lints::ILL_FORMED_ATTRIBUTE_INPUT;
10 use syntax::tokenstream::DelimSpan;
11 use syntax::sess::ParseSess;
12 use syntax_pos::{Symbol, sym};
14 pub fn check_meta(sess: &ParseSess, attr: &Attribute) {
16 attr.ident().and_then(|ident| BUILTIN_ATTRIBUTE_MAP.get(&ident.name)).map(|a| **a);
18 // Check input tokens for built-in and key-value attributes.
20 // `rustc_dummy` doesn't have any restrictions specific to built-in attributes.
21 Some((name, _, template, _)) if name != sym::rustc_dummy =>
22 check_builtin_attribute(sess, attr, name, template),
23 _ => if let MacArgs::Eq(..) = attr.get_normal_item().args {
24 // All key-value attributes are restricted to meta-item syntax.
25 parse_meta(sess, attr).map_err(|mut err| err.emit()).ok();
30 pub fn parse_meta<'a>(sess: &'a ParseSess, attr: &Attribute) -> PResult<'a, MetaItem> {
32 AttrKind::Normal(ref item) => MetaItem {
34 path: item.path.clone(),
35 kind: match &attr.get_normal_item().args {
36 MacArgs::Empty => MetaItemKind::Word,
37 MacArgs::Eq(_, t) => {
38 let v = parse_in(sess, t.clone(), "name value", |p| p.parse_unsuffixed_lit())?;
39 MetaItemKind::NameValue(v)
41 MacArgs::Delimited(dspan, delim, t) => {
42 check_meta_bad_delim(sess, *dspan, *delim, "wrong meta list delimiters");
43 let nmis = parse_in(sess, t.clone(), "meta list", |p| p.parse_meta_seq_top())?;
44 MetaItemKind::List(nmis)
48 AttrKind::DocComment(comment) => {
49 mk_name_value_item_str(Ident::new(sym::doc, attr.span), comment, attr.span)
54 crate fn check_meta_bad_delim(sess: &ParseSess, span: DelimSpan, delim: MacDelimiter, msg: &str) {
55 if let ast::MacDelimiter::Parenthesis = delim {
60 .struct_span_err(span.entire(), msg)
61 .multipart_suggestion(
62 "the delimiters should be `(` and `)`",
64 (span.open, "(".to_string()),
65 (span.close, ")".to_string()),
67 Applicability::MachineApplicable,
72 /// Checks that the given meta-item is compatible with this `AttributeTemplate`.
73 fn is_attr_template_compatible(template: &AttributeTemplate, meta: &ast::MetaItemKind) -> bool {
75 MetaItemKind::Word => template.word,
76 MetaItemKind::List(..) => template.list.is_some(),
77 MetaItemKind::NameValue(lit) if lit.kind.is_str() => template.name_value_str.is_some(),
78 MetaItemKind::NameValue(..) => false,
82 pub fn check_builtin_attribute(
86 template: AttributeTemplate,
88 // Some special attributes like `cfg` must be checked
89 // before the generic check, so we skip them here.
90 let should_skip = |name| name == sym::cfg;
91 // Some of previously accepted forms were used in practice,
92 // report them as warnings for now.
93 let should_warn = |name| name == sym::doc || name == sym::ignore ||
94 name == sym::inline || name == sym::link ||
95 name == sym::test || name == sym::bench;
97 match parse_meta(sess, attr) {
98 Ok(meta) => if !should_skip(name) && !is_attr_template_compatible(&template, &meta.kind) {
99 let error_msg = format!("malformed `{}` attribute input", name);
100 let mut msg = "attribute must be of the form ".to_owned();
101 let mut suggestions = vec![];
102 let mut first = true;
105 let code = format!("#[{}]", name);
106 msg.push_str(&format!("`{}`", &code));
107 suggestions.push(code);
109 if let Some(descr) = template.list {
111 msg.push_str(" or ");
114 let code = format!("#[{}({})]", name, descr);
115 msg.push_str(&format!("`{}`", &code));
116 suggestions.push(code);
118 if let Some(descr) = template.name_value_str {
120 msg.push_str(" or ");
122 let code = format!("#[{} = \"{}\"]", name, descr);
123 msg.push_str(&format!("`{}`", &code));
124 suggestions.push(code);
126 if should_warn(name) {
128 &ILL_FORMED_ATTRIBUTE_INPUT,
134 sess.span_diagnostic.struct_span_err(meta.span, &error_msg)
137 if suggestions.len() == 1 {
138 "must be of the form"
140 "the following are the possible correct uses"
142 suggestions.into_iter(),
143 Applicability::HasPlaceholders,
147 Err(mut err) => err.emit(),