2019-10-11 14:00:09 -05:00
|
|
|
//! Meta-syntax validation logic of attributes for post-expansion.
|
|
|
|
|
2023-04-26 19:53:06 -05:00
|
|
|
use crate::{errors, parse_in};
|
2019-12-05 07:19:00 -06:00
|
|
|
|
Overhaul `MacArgs::Eq`.
The value in `MacArgs::Eq` is currently represented as a `Token`.
Because of `TokenKind::Interpolated`, `Token` can be either a token or
an arbitrary AST fragment. In practice, a `MacArgs::Eq` starts out as a
literal or macro call AST fragment, and then is later lowered to a
literal token. But this is very non-obvious. `Token` is a much more
general type than what is needed.
This commit restricts things, by introducing a new type `MacArgsEqKind`
that is either an AST expression (pre-lowering) or an AST literal
(post-lowering). The downside is that the code is a bit more verbose in
a few places. The benefit is that makes it much clearer what the
possibilities are (though also shorter in some other places). Also, it
removes one use of `TokenKind::Interpolated`, taking us a step closer to
removing that variant, which will let us make `Token` impl `Copy` and
remove many "handle Interpolated" code paths in the parser.
Things to note:
- Error messages have improved. Messages like this:
```
unexpected token: `"bug" + "found"`
```
now say "unexpected expression", which makes more sense. Although
arbitrary expressions can exist within tokens thanks to
`TokenKind::Interpolated`, that's not obvious to anyone who doesn't
know compiler internals.
- In `parse_mac_args_common`, we no longer need to collect tokens for
the value expression.
2022-04-28 15:52:01 -05:00
|
|
|
use rustc_ast::tokenstream::DelimSpan;
|
2022-11-17 18:24:21 -06:00
|
|
|
use rustc_ast::MetaItemKind;
|
|
|
|
use rustc_ast::{self as ast, AttrArgs, AttrArgsEq, Attribute, DelimArgs, MacDelimiter, MetaItem};
|
Overhaul `MacArgs::Eq`.
The value in `MacArgs::Eq` is currently represented as a `Token`.
Because of `TokenKind::Interpolated`, `Token` can be either a token or
an arbitrary AST fragment. In practice, a `MacArgs::Eq` starts out as a
literal or macro call AST fragment, and then is later lowered to a
literal token. But this is very non-obvious. `Token` is a much more
general type than what is needed.
This commit restricts things, by introducing a new type `MacArgsEqKind`
that is either an AST expression (pre-lowering) or an AST literal
(post-lowering). The downside is that the code is a bit more verbose in
a few places. The benefit is that makes it much clearer what the
possibilities are (though also shorter in some other places). Also, it
removes one use of `TokenKind::Interpolated`, taking us a step closer to
removing that variant, which will let us make `Token` impl `Copy` and
remove many "handle Interpolated" code paths in the parser.
Things to note:
- Error messages have improved. Messages like this:
```
unexpected token: `"bug" + "found"`
```
now say "unexpected expression", which makes more sense. Although
arbitrary expressions can exist within tokens thanks to
`TokenKind::Interpolated`, that's not obvious to anyone who doesn't
know compiler internals.
- In `parse_mac_args_common`, we no longer need to collect tokens for
the value expression.
2022-04-28 15:52:01 -05:00
|
|
|
use rustc_ast_pretty::pprust;
|
2021-09-17 15:08:56 -05:00
|
|
|
use rustc_errors::{Applicability, FatalError, PResult};
|
2021-11-12 06:15:14 -06:00
|
|
|
use rustc_feature::{AttributeTemplate, BuiltinAttribute, BUILTIN_ATTRIBUTE_MAP};
|
2020-01-05 03:47:20 -06:00
|
|
|
use rustc_session::lint::builtin::ILL_FORMED_ATTRIBUTE_INPUT;
|
|
|
|
use rustc_session::parse::ParseSess;
|
2022-11-23 23:00:57 -06:00
|
|
|
use rustc_span::{sym, Span, Symbol};
|
2019-10-11 14:00:09 -05:00
|
|
|
|
2022-11-23 23:00:57 -06:00
|
|
|
pub fn check_attr(sess: &ParseSess, attr: &Attribute) {
|
2019-12-07 12:28:29 -06:00
|
|
|
if attr.is_doc_comment() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-11-12 06:15:14 -06:00
|
|
|
let attr_info = attr.ident().and_then(|ident| BUILTIN_ATTRIBUTE_MAP.get(&ident.name));
|
2019-10-11 14:00:09 -05:00
|
|
|
|
|
|
|
// Check input tokens for built-in and key-value attributes.
|
|
|
|
match attr_info {
|
|
|
|
// `rustc_dummy` doesn't have any restrictions specific to built-in attributes.
|
2021-11-12 06:15:14 -06:00
|
|
|
Some(BuiltinAttribute { name, template, .. }) if *name != sym::rustc_dummy => {
|
|
|
|
check_builtin_attribute(sess, attr, *name, *template)
|
2019-12-22 16:42:04 -06:00
|
|
|
}
|
2022-11-17 18:24:21 -06:00
|
|
|
_ if let AttrArgs::Eq(..) = attr.get_normal_item().args => {
|
2021-08-16 10:29:49 -05:00
|
|
|
// All key-value attributes are restricted to meta-item syntax.
|
|
|
|
parse_meta(sess, attr)
|
|
|
|
.map_err(|mut err| {
|
|
|
|
err.emit();
|
|
|
|
})
|
|
|
|
.ok();
|
2019-10-11 14:00:09 -05:00
|
|
|
}
|
2021-08-16 10:29:49 -05:00
|
|
|
_ => {}
|
2019-10-11 14:00:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn parse_meta<'a>(sess: &'a ParseSess, attr: &Attribute) -> PResult<'a, MetaItem> {
|
2019-12-07 12:28:29 -06:00
|
|
|
let item = attr.get_normal_item();
|
|
|
|
Ok(MetaItem {
|
|
|
|
span: attr.span,
|
|
|
|
path: item.path.clone(),
|
|
|
|
kind: match &item.args {
|
2022-11-17 18:24:21 -06:00
|
|
|
AttrArgs::Empty => MetaItemKind::Word,
|
|
|
|
AttrArgs::Delimited(DelimArgs { dspan, delim, tokens }) => {
|
2023-04-26 19:53:06 -05:00
|
|
|
check_meta_bad_delim(sess, *dspan, *delim);
|
2022-11-17 18:24:21 -06:00
|
|
|
let nmis = parse_in(sess, tokens.clone(), "meta list", |p| p.parse_meta_seq_top())?;
|
2019-12-07 12:28:29 -06:00
|
|
|
MetaItemKind::List(nmis)
|
|
|
|
}
|
2022-11-17 18:24:21 -06:00
|
|
|
AttrArgs::Eq(_, AttrArgsEq::Ast(expr)) => {
|
2022-10-09 21:40:56 -05:00
|
|
|
if let ast::ExprKind::Lit(token_lit) = expr.kind
|
2022-11-22 22:39:42 -06:00
|
|
|
&& let Ok(lit) = ast::MetaItemLit::from_token_lit(token_lit, expr.span)
|
2022-10-09 21:40:56 -05:00
|
|
|
{
|
|
|
|
if token_lit.suffix.is_some() {
|
Overhaul `MacArgs::Eq`.
The value in `MacArgs::Eq` is currently represented as a `Token`.
Because of `TokenKind::Interpolated`, `Token` can be either a token or
an arbitrary AST fragment. In practice, a `MacArgs::Eq` starts out as a
literal or macro call AST fragment, and then is later lowered to a
literal token. But this is very non-obvious. `Token` is a much more
general type than what is needed.
This commit restricts things, by introducing a new type `MacArgsEqKind`
that is either an AST expression (pre-lowering) or an AST literal
(post-lowering). The downside is that the code is a bit more verbose in
a few places. The benefit is that makes it much clearer what the
possibilities are (though also shorter in some other places). Also, it
removes one use of `TokenKind::Interpolated`, taking us a step closer to
removing that variant, which will let us make `Token` impl `Copy` and
remove many "handle Interpolated" code paths in the parser.
Things to note:
- Error messages have improved. Messages like this:
```
unexpected token: `"bug" + "found"`
```
now say "unexpected expression", which makes more sense. Although
arbitrary expressions can exist within tokens thanks to
`TokenKind::Interpolated`, that's not obvious to anyone who doesn't
know compiler internals.
- In `parse_mac_args_common`, we no longer need to collect tokens for
the value expression.
2022-04-28 15:52:01 -05:00
|
|
|
let mut err = sess.span_diagnostic.struct_span_err(
|
2022-10-09 21:40:56 -05:00
|
|
|
expr.span,
|
Overhaul `MacArgs::Eq`.
The value in `MacArgs::Eq` is currently represented as a `Token`.
Because of `TokenKind::Interpolated`, `Token` can be either a token or
an arbitrary AST fragment. In practice, a `MacArgs::Eq` starts out as a
literal or macro call AST fragment, and then is later lowered to a
literal token. But this is very non-obvious. `Token` is a much more
general type than what is needed.
This commit restricts things, by introducing a new type `MacArgsEqKind`
that is either an AST expression (pre-lowering) or an AST literal
(post-lowering). The downside is that the code is a bit more verbose in
a few places. The benefit is that makes it much clearer what the
possibilities are (though also shorter in some other places). Also, it
removes one use of `TokenKind::Interpolated`, taking us a step closer to
removing that variant, which will let us make `Token` impl `Copy` and
remove many "handle Interpolated" code paths in the parser.
Things to note:
- Error messages have improved. Messages like this:
```
unexpected token: `"bug" + "found"`
```
now say "unexpected expression", which makes more sense. Although
arbitrary expressions can exist within tokens thanks to
`TokenKind::Interpolated`, that's not obvious to anyone who doesn't
know compiler internals.
- In `parse_mac_args_common`, we no longer need to collect tokens for
the value expression.
2022-04-28 15:52:01 -05:00
|
|
|
"suffixed literals are not allowed in attributes",
|
|
|
|
);
|
|
|
|
err.help(
|
|
|
|
"instead of using a suffixed literal (`1u8`, `1.0f32`, etc.), \
|
|
|
|
use an unsuffixed version (`1`, `1.0`, etc.)",
|
|
|
|
);
|
|
|
|
return Err(err);
|
|
|
|
} else {
|
2022-10-09 21:40:56 -05:00
|
|
|
MetaItemKind::NameValue(lit)
|
Overhaul `MacArgs::Eq`.
The value in `MacArgs::Eq` is currently represented as a `Token`.
Because of `TokenKind::Interpolated`, `Token` can be either a token or
an arbitrary AST fragment. In practice, a `MacArgs::Eq` starts out as a
literal or macro call AST fragment, and then is later lowered to a
literal token. But this is very non-obvious. `Token` is a much more
general type than what is needed.
This commit restricts things, by introducing a new type `MacArgsEqKind`
that is either an AST expression (pre-lowering) or an AST literal
(post-lowering). The downside is that the code is a bit more verbose in
a few places. The benefit is that makes it much clearer what the
possibilities are (though also shorter in some other places). Also, it
removes one use of `TokenKind::Interpolated`, taking us a step closer to
removing that variant, which will let us make `Token` impl `Copy` and
remove many "handle Interpolated" code paths in the parser.
Things to note:
- Error messages have improved. Messages like this:
```
unexpected token: `"bug" + "found"`
```
now say "unexpected expression", which makes more sense. Although
arbitrary expressions can exist within tokens thanks to
`TokenKind::Interpolated`, that's not obvious to anyone who doesn't
know compiler internals.
- In `parse_mac_args_common`, we no longer need to collect tokens for
the value expression.
2022-04-28 15:52:01 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// The non-error case can happen with e.g. `#[foo = 1+1]`. The error case can
|
2023-04-09 16:35:02 -05:00
|
|
|
// happen with e.g. `#[foo = include_str!("nonexistent-file.rs")]`; in that
|
Overhaul `MacArgs::Eq`.
The value in `MacArgs::Eq` is currently represented as a `Token`.
Because of `TokenKind::Interpolated`, `Token` can be either a token or
an arbitrary AST fragment. In practice, a `MacArgs::Eq` starts out as a
literal or macro call AST fragment, and then is later lowered to a
literal token. But this is very non-obvious. `Token` is a much more
general type than what is needed.
This commit restricts things, by introducing a new type `MacArgsEqKind`
that is either an AST expression (pre-lowering) or an AST literal
(post-lowering). The downside is that the code is a bit more verbose in
a few places. The benefit is that makes it much clearer what the
possibilities are (though also shorter in some other places). Also, it
removes one use of `TokenKind::Interpolated`, taking us a step closer to
removing that variant, which will let us make `Token` impl `Copy` and
remove many "handle Interpolated" code paths in the parser.
Things to note:
- Error messages have improved. Messages like this:
```
unexpected token: `"bug" + "found"`
```
now say "unexpected expression", which makes more sense. Although
arbitrary expressions can exist within tokens thanks to
`TokenKind::Interpolated`, that's not obvious to anyone who doesn't
know compiler internals.
- In `parse_mac_args_common`, we no longer need to collect tokens for
the value expression.
2022-04-28 15:52:01 -05:00
|
|
|
// case we delay the error because an earlier error will have already been
|
|
|
|
// reported.
|
|
|
|
let msg = format!("unexpected expression: `{}`", pprust::expr_to_string(expr));
|
|
|
|
let mut err = sess.span_diagnostic.struct_span_err(expr.span, msg);
|
|
|
|
if let ast::ExprKind::Err = expr.kind {
|
|
|
|
err.downgrade_to_delayed_bug();
|
|
|
|
}
|
|
|
|
return Err(err);
|
|
|
|
}
|
|
|
|
}
|
2022-11-17 18:24:21 -06:00
|
|
|
AttrArgs::Eq(_, AttrArgsEq::Hir(lit)) => MetaItemKind::NameValue(lit.clone()),
|
2019-10-11 14:00:09 -05:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-04-26 19:53:06 -05:00
|
|
|
pub fn check_meta_bad_delim(sess: &ParseSess, span: DelimSpan, delim: MacDelimiter) {
|
2019-12-05 07:19:00 -06:00
|
|
|
if let ast::MacDelimiter::Parenthesis = delim {
|
|
|
|
return;
|
|
|
|
}
|
2023-04-26 19:53:06 -05:00
|
|
|
sess.emit_err(errors::MetaBadDelim {
|
|
|
|
span: span.entire(),
|
|
|
|
sugg: errors::MetaBadDelimSugg { open: span.open, close: span.close },
|
|
|
|
});
|
|
|
|
}
|
2019-12-05 07:19:00 -06:00
|
|
|
|
2023-04-26 19:53:06 -05:00
|
|
|
pub fn check_cfg_attr_bad_delim(sess: &ParseSess, span: DelimSpan, delim: MacDelimiter) {
|
|
|
|
if let ast::MacDelimiter::Parenthesis = delim {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
sess.emit_err(errors::CfgAttrBadDelim {
|
|
|
|
span: span.entire(),
|
|
|
|
sugg: errors::MetaBadDelimSugg { open: span.open, close: span.close },
|
|
|
|
});
|
2019-12-05 07:19:00 -06:00
|
|
|
}
|
|
|
|
|
2019-11-29 17:56:46 -06:00
|
|
|
/// Checks that the given meta-item is compatible with this `AttributeTemplate`.
|
|
|
|
fn is_attr_template_compatible(template: &AttributeTemplate, meta: &ast::MetaItemKind) -> bool {
|
|
|
|
match meta {
|
|
|
|
MetaItemKind::Word => template.word,
|
|
|
|
MetaItemKind::List(..) => template.list.is_some(),
|
|
|
|
MetaItemKind::NameValue(lit) if lit.kind.is_str() => template.name_value_str.is_some(),
|
|
|
|
MetaItemKind::NameValue(..) => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-11 14:00:09 -05:00
|
|
|
pub fn check_builtin_attribute(
|
|
|
|
sess: &ParseSess,
|
|
|
|
attr: &Attribute,
|
|
|
|
name: Symbol,
|
|
|
|
template: AttributeTemplate,
|
|
|
|
) {
|
|
|
|
match parse_meta(sess, attr) {
|
2022-11-23 23:00:57 -06:00
|
|
|
Ok(meta) => check_builtin_meta_item(sess, &meta, attr.style, name, template),
|
2020-02-01 17:47:58 -06:00
|
|
|
Err(mut err) => {
|
|
|
|
err.emit();
|
|
|
|
}
|
2019-10-11 14:00:09 -05:00
|
|
|
}
|
|
|
|
}
|
2021-09-16 19:48:06 -05:00
|
|
|
|
2022-11-23 23:00:57 -06:00
|
|
|
pub fn check_builtin_meta_item(
|
|
|
|
sess: &ParseSess,
|
|
|
|
meta: &MetaItem,
|
|
|
|
style: ast::AttrStyle,
|
|
|
|
name: Symbol,
|
|
|
|
template: AttributeTemplate,
|
|
|
|
) {
|
|
|
|
// Some special attributes like `cfg` must be checked
|
|
|
|
// before the generic check, so we skip them here.
|
|
|
|
let should_skip = |name| name == sym::cfg;
|
|
|
|
|
|
|
|
if !should_skip(name) && !is_attr_template_compatible(&template, &meta.kind) {
|
|
|
|
emit_malformed_attribute(sess, style, meta.span, name, template);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-16 19:48:06 -05:00
|
|
|
fn emit_malformed_attribute(
|
|
|
|
sess: &ParseSess,
|
2022-11-23 23:00:57 -06:00
|
|
|
style: ast::AttrStyle,
|
|
|
|
span: Span,
|
2021-09-16 19:48:06 -05:00
|
|
|
name: Symbol,
|
|
|
|
template: AttributeTemplate,
|
|
|
|
) {
|
|
|
|
// Some of previously accepted forms were used in practice,
|
|
|
|
// report them as warnings for now.
|
|
|
|
let should_warn = |name| {
|
|
|
|
matches!(name, sym::doc | sym::ignore | sym::inline | sym::link | sym::test | sym::bench)
|
|
|
|
};
|
|
|
|
|
|
|
|
let error_msg = format!("malformed `{}` attribute input", name);
|
|
|
|
let mut msg = "attribute must be of the form ".to_owned();
|
|
|
|
let mut suggestions = vec![];
|
|
|
|
let mut first = true;
|
2022-11-23 23:00:57 -06:00
|
|
|
let inner = if style == ast::AttrStyle::Inner { "!" } else { "" };
|
2021-09-16 19:48:06 -05:00
|
|
|
if template.word {
|
|
|
|
first = false;
|
|
|
|
let code = format!("#{}[{}]", inner, name);
|
|
|
|
msg.push_str(&format!("`{}`", &code));
|
|
|
|
suggestions.push(code);
|
|
|
|
}
|
|
|
|
if let Some(descr) = template.list {
|
|
|
|
if !first {
|
|
|
|
msg.push_str(" or ");
|
|
|
|
}
|
|
|
|
first = false;
|
|
|
|
let code = format!("#{}[{}({})]", inner, name, descr);
|
|
|
|
msg.push_str(&format!("`{}`", &code));
|
|
|
|
suggestions.push(code);
|
|
|
|
}
|
|
|
|
if let Some(descr) = template.name_value_str {
|
|
|
|
if !first {
|
|
|
|
msg.push_str(" or ");
|
|
|
|
}
|
|
|
|
let code = format!("#{}[{} = \"{}\"]", inner, name, descr);
|
|
|
|
msg.push_str(&format!("`{}`", &code));
|
|
|
|
suggestions.push(code);
|
|
|
|
}
|
|
|
|
if should_warn(name) {
|
2023-05-16 01:04:03 -05:00
|
|
|
sess.buffer_lint(&ILL_FORMED_ATTRIBUTE_INPUT, span, ast::CRATE_NODE_ID, msg);
|
2021-09-16 19:48:06 -05:00
|
|
|
} else {
|
|
|
|
sess.span_diagnostic
|
Restrict `From<S>` for `{D,Subd}iagnosticMessage`.
Currently a `{D,Subd}iagnosticMessage` can be created from any type that
impls `Into<String>`. That includes `&str`, `String`, and `Cow<'static,
str>`, which are reasonable. It also includes `&String`, which is pretty
weird, and results in many places making unnecessary allocations for
patterns like this:
```
self.fatal(&format!(...))
```
This creates a string with `format!`, takes a reference, passes the
reference to `fatal`, which does an `into()`, which clones the
reference, doing a second allocation. Two allocations for a single
string, bleh.
This commit changes the `From` impls so that you can only create a
`{D,Subd}iagnosticMessage` from `&str`, `String`, or `Cow<'static,
str>`. This requires changing all the places that currently create one
from a `&String`. Most of these are of the `&format!(...)` form
described above; each one removes an unnecessary static `&`, plus an
allocation when executed. There are also a few places where the existing
use of `&String` was more reasonable; these now just use `clone()` at
the call site.
As well as making the code nicer and more efficient, this is a step
towards possibly using `Cow<'static, str>` in
`{D,Subd}iagnosticMessage::{Str,Eager}`. That would require changing
the `From<&'a str>` impls to `From<&'static str>`, which is doable, but
I'm not yet sure if it's worthwhile.
2023-04-19 22:26:58 -05:00
|
|
|
.struct_span_err(span, error_msg)
|
2021-09-16 19:48:06 -05:00
|
|
|
.span_suggestions(
|
2022-11-23 23:00:57 -06:00
|
|
|
span,
|
2021-09-16 19:48:06 -05:00
|
|
|
if suggestions.len() == 1 {
|
|
|
|
"must be of the form"
|
|
|
|
} else {
|
|
|
|
"the following are the possible correct uses"
|
|
|
|
},
|
|
|
|
suggestions.into_iter(),
|
|
|
|
Applicability::HasPlaceholders,
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
}
|
2021-09-17 15:08:56 -05:00
|
|
|
|
|
|
|
pub fn emit_fatal_malformed_builtin_attribute(
|
|
|
|
sess: &ParseSess,
|
|
|
|
attr: &Attribute,
|
|
|
|
name: Symbol,
|
|
|
|
) -> ! {
|
2021-11-12 06:15:14 -06:00
|
|
|
let template = BUILTIN_ATTRIBUTE_MAP.get(&name).expect("builtin attr defined").template;
|
2022-11-23 23:00:57 -06:00
|
|
|
emit_malformed_attribute(sess, attr.style, attr.span, name, template);
|
2021-09-17 15:08:56 -05:00
|
|
|
// This is fatal, otherwise it will likely cause a cascade of other errors
|
|
|
|
// (and an error here is expected to be very rare).
|
|
|
|
FatalError.raise()
|
|
|
|
}
|