]> git.lizzy.rs Git - rust.git/blob - src/librustc_parse/validate_attr.rs
Rollup merge of #67055 - lqd:const_qualif, r=oli-obk
[rust.git] / src / librustc_parse / validate_attr.rs
1 //! Meta-syntax validation logic of attributes for post-expansion.
2
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};
10
11 pub fn check_meta(sess: &ParseSess, attr: &Attribute) {
12     let attr_info =
13         attr.ident().and_then(|ident| BUILTIN_ATTRIBUTE_MAP.get(&ident.name)).map(|a| **a);
14
15     // Check input tokens for built-in and key-value attributes.
16     match attr_info {
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();
23         }
24     }
25 }
26
27 pub fn parse_meta<'a>(sess: &'a ParseSess, attr: &Attribute) -> PResult<'a, MetaItem> {
28     Ok(match attr.kind {
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())?,
32             span: attr.span,
33         },
34         AttrKind::DocComment(comment) => {
35             mk_name_value_item_str(Ident::new(sym::doc, attr.span), comment, attr.span)
36         }
37     })
38 }
39
40 /// Checks that the given meta-item is compatible with this `AttributeTemplate`.
41 fn is_attr_template_compatible(template: &AttributeTemplate, meta: &ast::MetaItemKind) -> bool {
42     match meta {
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,
47     }
48 }
49
50 pub fn check_builtin_attribute(
51     sess: &ParseSess,
52     attr: &Attribute,
53     name: Symbol,
54     template: AttributeTemplate,
55 ) {
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;
64
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![];
70             let mut first = true;
71             if template.word {
72                 first = false;
73                 let code = format!("#[{}]", name);
74                 msg.push_str(&format!("`{}`", &code));
75                 suggestions.push(code);
76             }
77             if let Some(descr) = template.list {
78                 if !first {
79                     msg.push_str(" or ");
80                 }
81                 first = false;
82                 let code = format!("#[{}({})]", name, descr);
83                 msg.push_str(&format!("`{}`", &code));
84                 suggestions.push(code);
85             }
86             if let Some(descr) = template.name_value_str {
87                 if !first {
88                     msg.push_str(" or ");
89                 }
90                 let code = format!("#[{} = \"{}\"]", name, descr);
91                 msg.push_str(&format!("`{}`", &code));
92                 suggestions.push(code);
93             }
94             if should_warn(name) {
95                 sess.buffer_lint(
96                     &ILL_FORMED_ATTRIBUTE_INPUT,
97                     meta.span,
98                     ast::CRATE_NODE_ID,
99                     &msg,
100                 );
101             } else {
102                 sess.span_diagnostic.struct_span_err(meta.span, &error_msg)
103                     .span_suggestions(
104                         meta.span,
105                         if suggestions.len() == 1 {
106                             "must be of the form"
107                         } else {
108                             "the following are the possible correct uses"
109                         },
110                         suggestions.into_iter(),
111                         Applicability::HasPlaceholders,
112                     ).emit();
113             }
114         }
115         Err(mut err) => err.emit(),
116     }
117 }