]> git.lizzy.rs Git - rust.git/blob - src/librustc_parse/validate_attr.rs
Remove ord lang item
[rust.git] / src / librustc_parse / validate_attr.rs
1 //! Meta-syntax validation logic of attributes for post-expansion.
2
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;
8 use syntax::token;
9 use syntax::tokenstream::TokenTree;
10 use syntax::sess::ParseSess;
11 use syntax_pos::{Symbol, sym};
12
13 pub fn check_meta(sess: &ParseSess, attr: &Attribute) {
14     let attr_info =
15         attr.ident().and_then(|ident| BUILTIN_ATTRIBUTE_MAP.get(&ident.name)).map(|a| **a);
16
17     // Check input tokens for built-in and key-value attributes.
18     match attr_info {
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();
26             }
27         }
28     }
29 }
30
31 pub fn parse_meta<'a>(sess: &'a ParseSess, attr: &Attribute) -> PResult<'a, MetaItem> {
32     Ok(match attr.kind {
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())?,
36             span: attr.span,
37         },
38         AttrKind::DocComment(comment) => {
39             mk_name_value_item_str(Ident::new(sym::doc, attr.span), comment, attr.span)
40         }
41     })
42 }
43
44 /// Checks that the given meta-item is compatible with this `AttributeTemplate`.
45 fn is_attr_template_compatible(template: &AttributeTemplate, meta: &ast::MetaItemKind) -> bool {
46     match meta {
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,
51     }
52 }
53
54 pub fn check_builtin_attribute(
55     sess: &ParseSess,
56     attr: &Attribute,
57     name: Symbol,
58     template: AttributeTemplate,
59 ) {
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;
68
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![];
74             let mut first = true;
75             if template.word {
76                 first = false;
77                 let code = format!("#[{}]", name);
78                 msg.push_str(&format!("`{}`", &code));
79                 suggestions.push(code);
80             }
81             if let Some(descr) = template.list {
82                 if !first {
83                     msg.push_str(" or ");
84                 }
85                 first = false;
86                 let code = format!("#[{}({})]", name, descr);
87                 msg.push_str(&format!("`{}`", &code));
88                 suggestions.push(code);
89             }
90             if let Some(descr) = template.name_value_str {
91                 if !first {
92                     msg.push_str(" or ");
93                 }
94                 let code = format!("#[{} = \"{}\"]", name, descr);
95                 msg.push_str(&format!("`{}`", &code));
96                 suggestions.push(code);
97             }
98             if should_warn(name) {
99                 sess.buffer_lint(
100                     BufferedEarlyLintId::IllFormedAttributeInput,
101                     meta.span,
102                     ast::CRATE_NODE_ID,
103                     &msg,
104                 );
105             } else {
106                 sess.span_diagnostic.struct_span_err(meta.span, &error_msg)
107                     .span_suggestions(
108                         meta.span,
109                         if suggestions.len() == 1 {
110                             "must be of the form"
111                         } else {
112                             "the following are the possible correct uses"
113                         },
114                         suggestions.into_iter(),
115                         Applicability::HasPlaceholders,
116                     ).emit();
117             }
118         }
119         Err(mut err) => err.emit(),
120     }
121 }