|
| 1 | +//! Meta-syntax validation logic of attributes for post-expansion. |
| 2 | +
|
| 3 | +use crate::ast::{self, Attribute, AttrKind, Ident, MetaItem}; |
| 4 | +use crate::attr::{AttributeTemplate, mk_name_value_item_str}; |
| 5 | +use crate::sess::ParseSess; |
| 6 | +use crate::feature_gate::BUILTIN_ATTRIBUTE_MAP; |
| 7 | +use crate::early_buffered_lints::BufferedEarlyLintId; |
| 8 | +use crate::token; |
| 9 | +use crate::tokenstream::TokenTree; |
| 10 | + |
| 11 | +use errors::{PResult, Applicability}; |
| 12 | +use syntax_pos::{Symbol, sym}; |
| 13 | + |
| 14 | +pub fn check_meta(sess: &ParseSess, attr: &Attribute) { |
| 15 | + let attr_info = |
| 16 | + attr.ident().and_then(|ident| BUILTIN_ATTRIBUTE_MAP.get(&ident.name)).map(|a| **a); |
| 17 | + |
| 18 | + // Check input tokens for built-in and key-value attributes. |
| 19 | + match attr_info { |
| 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 Some(TokenTree::Token(token)) = attr.get_normal_item().tokens.trees().next() { |
| 24 | + if token == token::Eq { |
| 25 | + // All key-value attributes are restricted to meta-item syntax. |
| 26 | + parse_meta(sess, attr).map_err(|mut err| err.emit()).ok(); |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +pub fn parse_meta<'a>(sess: &'a ParseSess, attr: &Attribute) -> PResult<'a, MetaItem> { |
| 33 | + Ok(match attr.kind { |
| 34 | + AttrKind::Normal(ref item) => MetaItem { |
| 35 | + path: item.path.clone(), |
| 36 | + kind: super::parse_in_attr(sess, attr, |p| p.parse_meta_item_kind())?, |
| 37 | + span: attr.span, |
| 38 | + }, |
| 39 | + AttrKind::DocComment(comment) => { |
| 40 | + mk_name_value_item_str(Ident::new(sym::doc, attr.span), comment, attr.span) |
| 41 | + } |
| 42 | + }) |
| 43 | +} |
| 44 | + |
| 45 | +pub fn check_builtin_attribute( |
| 46 | + sess: &ParseSess, |
| 47 | + attr: &Attribute, |
| 48 | + name: Symbol, |
| 49 | + template: AttributeTemplate, |
| 50 | +) { |
| 51 | + // Some special attributes like `cfg` must be checked |
| 52 | + // before the generic check, so we skip them here. |
| 53 | + let should_skip = |name| name == sym::cfg; |
| 54 | + // Some of previously accepted forms were used in practice, |
| 55 | + // report them as warnings for now. |
| 56 | + let should_warn = |name| name == sym::doc || name == sym::ignore || |
| 57 | + name == sym::inline || name == sym::link || |
| 58 | + name == sym::test || name == sym::bench; |
| 59 | + |
| 60 | + match parse_meta(sess, attr) { |
| 61 | + Ok(meta) => if !should_skip(name) && !template.compatible(&meta.kind) { |
| 62 | + let error_msg = format!("malformed `{}` attribute input", name); |
| 63 | + let mut msg = "attribute must be of the form ".to_owned(); |
| 64 | + let mut suggestions = vec![]; |
| 65 | + let mut first = true; |
| 66 | + if template.word { |
| 67 | + first = false; |
| 68 | + let code = format!("#[{}]", name); |
| 69 | + msg.push_str(&format!("`{}`", &code)); |
| 70 | + suggestions.push(code); |
| 71 | + } |
| 72 | + if let Some(descr) = template.list { |
| 73 | + if !first { |
| 74 | + msg.push_str(" or "); |
| 75 | + } |
| 76 | + first = false; |
| 77 | + let code = format!("#[{}({})]", name, descr); |
| 78 | + msg.push_str(&format!("`{}`", &code)); |
| 79 | + suggestions.push(code); |
| 80 | + } |
| 81 | + if let Some(descr) = template.name_value_str { |
| 82 | + if !first { |
| 83 | + msg.push_str(" or "); |
| 84 | + } |
| 85 | + let code = format!("#[{} = \"{}\"]", name, descr); |
| 86 | + msg.push_str(&format!("`{}`", &code)); |
| 87 | + suggestions.push(code); |
| 88 | + } |
| 89 | + if should_warn(name) { |
| 90 | + sess.buffer_lint( |
| 91 | + BufferedEarlyLintId::IllFormedAttributeInput, |
| 92 | + meta.span, |
| 93 | + ast::CRATE_NODE_ID, |
| 94 | + &msg, |
| 95 | + ); |
| 96 | + } else { |
| 97 | + sess.span_diagnostic.struct_span_err(meta.span, &error_msg) |
| 98 | + .span_suggestions( |
| 99 | + meta.span, |
| 100 | + if suggestions.len() == 1 { |
| 101 | + "must be of the form" |
| 102 | + } else { |
| 103 | + "the following are the possible correct uses" |
| 104 | + }, |
| 105 | + suggestions.into_iter(), |
| 106 | + Applicability::HasPlaceholders, |
| 107 | + ).emit(); |
| 108 | + } |
| 109 | + } |
| 110 | + Err(mut err) => err.emit(), |
| 111 | + } |
| 112 | +} |
0 commit comments