1 //! Meta-syntax validation logic of attributes for post-expansion.
5 use rustc_ast::tokenstream::{DelimSpan, TokenTree};
6 use rustc_ast::{self as ast, Attribute, MacArgs, MacDelimiter, MetaItem, MetaItemKind};
7 use rustc_errors::{Applicability, PResult};
8 use rustc_feature::{AttributeTemplate, BUILTIN_ATTRIBUTE_MAP};
9 use rustc_session::lint::builtin::ILL_FORMED_ATTRIBUTE_INPUT;
10 use rustc_session::parse::ParseSess;
11 use rustc_span::{sym, Symbol};
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)
40 pub fn parse_meta<'a>(sess: &'a ParseSess, attr: &Attribute) -> PResult<'a, MetaItem> {
41 let item = attr.get_normal_item();
44 path: item.path.clone(),
45 kind: match &item.args {
46 MacArgs::Empty => MetaItemKind::Word,
47 MacArgs::Eq(_, t) => {
48 let t = TokenTree::Token(t.clone()).into();
49 let v = parse_in(sess, t, "name value", |p| p.parse_unsuffixed_lit())?;
50 MetaItemKind::NameValue(v)
52 MacArgs::Delimited(dspan, delim, t) => {
53 check_meta_bad_delim(sess, *dspan, *delim, "wrong meta list delimiters");
54 let nmis = parse_in(sess, t.clone(), "meta list", |p| p.parse_meta_seq_top())?;
55 MetaItemKind::List(nmis)
61 pub fn check_meta_bad_delim(sess: &ParseSess, span: DelimSpan, delim: MacDelimiter, msg: &str) {
62 if let ast::MacDelimiter::Parenthesis = delim {
67 .struct_span_err(span.entire(), msg)
68 .multipart_suggestion(
69 "the delimiters should be `(` and `)`",
70 vec![(span.open, "(".to_string()), (span.close, ")".to_string())],
71 Applicability::MachineApplicable,
76 /// Checks that the given meta-item is compatible with this `AttributeTemplate`.
77 fn is_attr_template_compatible(template: &AttributeTemplate, meta: &ast::MetaItemKind) -> bool {
79 MetaItemKind::Word => template.word,
80 MetaItemKind::List(..) => template.list.is_some(),
81 MetaItemKind::NameValue(lit) if lit.kind.is_str() => template.name_value_str.is_some(),
82 MetaItemKind::NameValue(..) => false,
86 pub fn check_builtin_attribute(
90 template: AttributeTemplate,
92 // Some special attributes like `cfg` must be checked
93 // before the generic check, so we skip them here.
94 let should_skip = |name| name == sym::cfg;
95 // Some of previously accepted forms were used in practice,
96 // report them as warnings for now.
97 let should_warn = |name| {
99 || name == sym::ignore
100 || name == sym::inline
103 || name == sym::bench
106 match parse_meta(sess, attr) {
108 if !should_skip(name) && !is_attr_template_compatible(&template, &meta.kind) {
109 let error_msg = format!("malformed `{}` attribute input", name);
110 let mut msg = "attribute must be of the form ".to_owned();
111 let mut suggestions = vec![];
112 let mut first = true;
115 let code = format!("#[{}]", name);
116 msg.push_str(&format!("`{}`", &code));
117 suggestions.push(code);
119 if let Some(descr) = template.list {
121 msg.push_str(" or ");
124 let code = format!("#[{}({})]", name, descr);
125 msg.push_str(&format!("`{}`", &code));
126 suggestions.push(code);
128 if let Some(descr) = template.name_value_str {
130 msg.push_str(" or ");
132 let code = format!("#[{} = \"{}\"]", name, descr);
133 msg.push_str(&format!("`{}`", &code));
134 suggestions.push(code);
136 if should_warn(name) {
138 &ILL_FORMED_ATTRIBUTE_INPUT,
145 .struct_span_err(meta.span, &error_msg)
148 if suggestions.len() == 1 {
149 "must be of the form"
151 "the following are the possible correct uses"
153 suggestions.into_iter(),
154 Applicability::HasPlaceholders,