1 //! Meta-syntax validation logic of attributes for post-expansion.
5 use rustc_errors::{Applicability, PResult};
6 use rustc_feature::{AttributeTemplate, BUILTIN_ATTRIBUTE_MAP};
7 use rustc_session::lint::builtin::ILL_FORMED_ATTRIBUTE_INPUT;
8 use rustc_session::parse::ParseSess;
9 use rustc_span::{sym, Symbol};
10 use syntax::ast::{self, Attribute, MacArgs, MacDelimiter, MetaItem, MetaItemKind};
11 use syntax::tokenstream::DelimSpan;
13 pub fn check_meta(sess: &ParseSess, attr: &Attribute) {
14 if attr.is_doc_comment() {
19 attr.ident().and_then(|ident| BUILTIN_ATTRIBUTE_MAP.get(&ident.name)).map(|a| **a);
21 // Check input tokens for built-in and key-value attributes.
23 // `rustc_dummy` doesn't have any restrictions specific to built-in attributes.
24 Some((name, _, template, _)) if name != sym::rustc_dummy => {
25 check_builtin_attribute(sess, attr, name, template)
28 if let MacArgs::Eq(..) = attr.get_normal_item().args {
29 // All key-value attributes are restricted to meta-item syntax.
30 parse_meta(sess, attr).map_err(|mut err| err.emit()).ok();
36 pub fn parse_meta<'a>(sess: &'a ParseSess, attr: &Attribute) -> PResult<'a, MetaItem> {
37 let item = attr.get_normal_item();
40 path: item.path.clone(),
41 kind: match &item.args {
42 MacArgs::Empty => MetaItemKind::Word,
43 MacArgs::Eq(_, t) => {
44 let v = parse_in(sess, t.clone(), "name value", |p| p.parse_unsuffixed_lit())?;
45 MetaItemKind::NameValue(v)
47 MacArgs::Delimited(dspan, delim, t) => {
48 check_meta_bad_delim(sess, *dspan, *delim, "wrong meta list delimiters");
49 let nmis = parse_in(sess, t.clone(), "meta list", |p| p.parse_meta_seq_top())?;
50 MetaItemKind::List(nmis)
56 crate fn check_meta_bad_delim(sess: &ParseSess, span: DelimSpan, delim: MacDelimiter, msg: &str) {
57 if let ast::MacDelimiter::Parenthesis = delim {
62 .struct_span_err(span.entire(), msg)
63 .multipart_suggestion(
64 "the delimiters should be `(` and `)`",
65 vec![(span.open, "(".to_string()), (span.close, ")".to_string())],
66 Applicability::MachineApplicable,
71 /// Checks that the given meta-item is compatible with this `AttributeTemplate`.
72 fn is_attr_template_compatible(template: &AttributeTemplate, meta: &ast::MetaItemKind) -> bool {
74 MetaItemKind::Word => template.word,
75 MetaItemKind::List(..) => template.list.is_some(),
76 MetaItemKind::NameValue(lit) if lit.kind.is_str() => template.name_value_str.is_some(),
77 MetaItemKind::NameValue(..) => false,
81 pub fn check_builtin_attribute(
85 template: AttributeTemplate,
87 // Some special attributes like `cfg` must be checked
88 // before the generic check, so we skip them here.
89 let should_skip = |name| name == sym::cfg;
90 // Some of previously accepted forms were used in practice,
91 // report them as warnings for now.
92 let should_warn = |name| {
94 || name == sym::ignore
95 || name == sym::inline
101 match parse_meta(sess, attr) {
103 if !should_skip(name) && !is_attr_template_compatible(&template, &meta.kind) {
104 let error_msg = format!("malformed `{}` attribute input", name);
105 let mut msg = "attribute must be of the form ".to_owned();
106 let mut suggestions = vec![];
107 let mut first = true;
110 let code = format!("#[{}]", name);
111 msg.push_str(&format!("`{}`", &code));
112 suggestions.push(code);
114 if let Some(descr) = template.list {
116 msg.push_str(" or ");
119 let code = format!("#[{}({})]", name, descr);
120 msg.push_str(&format!("`{}`", &code));
121 suggestions.push(code);
123 if let Some(descr) = template.name_value_str {
125 msg.push_str(" or ");
127 let code = format!("#[{} = \"{}\"]", name, descr);
128 msg.push_str(&format!("`{}`", &code));
129 suggestions.push(code);
131 if should_warn(name) {
133 &ILL_FORMED_ATTRIBUTE_INPUT,
140 .struct_span_err(meta.span, &error_msg)
143 if suggestions.len() == 1 {
144 "must be of the form"
146 "the following are the possible correct uses"
148 suggestions.into_iter(),
149 Applicability::HasPlaceholders,
155 Err(mut err) => err.emit(),