2019-10-11 21:00:09 +02:00
|
|
|
//! Meta-syntax validation logic of attributes for post-expansion.
|
|
|
|
|
2023-08-02 09:56:26 +10:00
|
|
|
use rustc_ast::token::Delimiter;
|
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-29 06:52:01 +10:00
|
|
|
use rustc_ast::tokenstream::DelimSpan;
|
2024-06-20 17:44:11 +10:00
|
|
|
use rustc_ast::{
|
2024-10-17 01:14:01 +02:00
|
|
|
self as ast, AttrArgs, Attribute, DelimArgs, MetaItem, MetaItemInner, MetaItemKind, Safety,
|
2024-06-20 17:44:11 +10:00
|
|
|
};
|
2021-09-17 13:08:56 -07:00
|
|
|
use rustc_errors::{Applicability, FatalError, PResult};
|
2024-08-07 01:36:28 -05:00
|
|
|
use rustc_feature::{AttributeSafety, AttributeTemplate, BUILTIN_ATTRIBUTE_MAP, BuiltinAttribute};
|
2023-12-12 09:55:50 +11:00
|
|
|
use rustc_session::errors::report_lit_error;
|
2024-04-14 20:11:14 +00:00
|
|
|
use rustc_session::lint::BuiltinLintDiag;
|
2024-06-08 18:39:09 -05:00
|
|
|
use rustc_session::lint::builtin::{ILL_FORMED_ATTRIBUTE_INPUT, UNSAFE_ATTR_OUTSIDE_UNSAFE};
|
2020-01-05 10:47:20 +01:00
|
|
|
use rustc_session::parse::ParseSess;
|
2024-12-18 18:28:08 +00:00
|
|
|
use rustc_span::{Span, Symbol, sym};
|
2019-10-11 21:00:09 +02:00
|
|
|
|
2023-04-27 01:53:06 +01:00
|
|
|
use crate::{errors, parse_in};
|
2024-07-29 08:13:50 +10:00
|
|
|
|
2024-08-07 01:36:28 -05:00
|
|
|
pub fn check_attr(psess: &ParseSess, attr: &Attribute) {
|
2025-03-22 21:42:34 +03:00
|
|
|
if attr.is_doc_comment() || attr.has_name(sym::cfg_trace) || attr.has_name(sym::cfg_attr_trace)
|
|
|
|
{
|
2019-12-07 21:28:29 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-11-12 20:15:14 +08:00
|
|
|
let attr_info = attr.ident().and_then(|ident| BUILTIN_ATTRIBUTE_MAP.get(&ident.name));
|
2024-06-08 18:39:09 -05:00
|
|
|
let attr_item = attr.get_normal_item();
|
|
|
|
|
2024-07-02 23:52:16 -05:00
|
|
|
// All non-builtin attributes are considered safe
|
|
|
|
let safety = attr_info.map(|x| x.safety).unwrap_or(AttributeSafety::Normal);
|
2024-08-07 01:36:28 -05:00
|
|
|
check_attribute_safety(psess, safety, attr);
|
2019-10-11 21:00:09 +02: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 20:15:14 +08:00
|
|
|
Some(BuiltinAttribute { name, template, .. }) if *name != sym::rustc_dummy => {
|
2024-06-04 13:33:03 +10:00
|
|
|
match parse_meta(psess, attr) {
|
2024-07-02 23:52:16 -05:00
|
|
|
// Don't check safety again, we just did that
|
2024-08-07 01:36:28 -05:00
|
|
|
Ok(meta) => {
|
|
|
|
check_builtin_meta_item(psess, &meta, attr.style, *name, *template, false)
|
|
|
|
}
|
2024-06-04 13:33:03 +10:00
|
|
|
Err(err) => {
|
|
|
|
err.emit();
|
|
|
|
}
|
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2024-07-02 23:52:16 -05:00
|
|
|
_ => {
|
2024-12-02 10:06:26 +00:00
|
|
|
if let AttrArgs::Eq { .. } = attr_item.args {
|
2024-07-02 23:52:16 -05:00
|
|
|
// All key-value attributes are restricted to meta-item syntax.
|
|
|
|
match parse_meta(psess, attr) {
|
|
|
|
Ok(_) => {}
|
|
|
|
Err(err) => {
|
|
|
|
err.emit();
|
|
|
|
}
|
2024-06-04 13:33:03 +10:00
|
|
|
}
|
|
|
|
}
|
2019-10-11 21:00:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-04 16:31:49 +11:00
|
|
|
pub fn parse_meta<'a>(psess: &'a ParseSess, attr: &Attribute) -> PResult<'a, MetaItem> {
|
2019-12-07 21:28:29 +03:00
|
|
|
let item = attr.get_normal_item();
|
|
|
|
Ok(MetaItem {
|
2024-04-20 23:54:50 -05:00
|
|
|
unsafety: item.unsafety,
|
2019-12-07 21:28:29 +03:00
|
|
|
span: attr.span,
|
|
|
|
path: item.path.clone(),
|
|
|
|
kind: match &item.args {
|
2022-11-18 11:24:21 +11:00
|
|
|
AttrArgs::Empty => MetaItemKind::Word,
|
|
|
|
AttrArgs::Delimited(DelimArgs { dspan, delim, tokens }) => {
|
2024-03-04 16:31:49 +11:00
|
|
|
check_meta_bad_delim(psess, *dspan, *delim);
|
|
|
|
let nmis =
|
|
|
|
parse_in(psess, tokens.clone(), "meta list", |p| p.parse_meta_seq_top())?;
|
2019-12-07 21:28:29 +03:00
|
|
|
MetaItemKind::List(nmis)
|
|
|
|
}
|
2024-10-17 01:14:01 +02:00
|
|
|
AttrArgs::Eq { expr, .. } => {
|
2023-12-12 09:55:50 +11:00
|
|
|
if let ast::ExprKind::Lit(token_lit) = expr.kind {
|
|
|
|
let res = ast::MetaItemLit::from_token_lit(token_lit, expr.span);
|
|
|
|
let res = match res {
|
|
|
|
Ok(lit) => {
|
|
|
|
if token_lit.suffix.is_some() {
|
2024-06-18 09:43:28 +00:00
|
|
|
let mut err = psess.dcx().struct_span_err(
|
2023-12-12 09:55:50 +11:00
|
|
|
expr.span,
|
|
|
|
"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 {
|
|
|
|
MetaItemKind::NameValue(lit)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(err) => {
|
2024-03-04 16:31:49 +11:00
|
|
|
let guar = report_lit_error(psess, err, token_lit, expr.span);
|
2023-12-12 09:55:50 +11:00
|
|
|
let lit = ast::MetaItemLit {
|
|
|
|
symbol: token_lit.symbol,
|
|
|
|
suffix: token_lit.suffix,
|
2024-02-14 20:12:05 +11:00
|
|
|
kind: ast::LitKind::Err(guar),
|
2023-12-12 09:55:50 +11:00
|
|
|
span: expr.span,
|
|
|
|
};
|
|
|
|
MetaItemKind::NameValue(lit)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
res
|
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-29 06:52:01 +10:00
|
|
|
} else {
|
2023-12-12 09:55:50 +11:00
|
|
|
// Example cases:
|
|
|
|
// - `#[foo = 1+1]`: results in `ast::ExprKind::BinOp`.
|
|
|
|
// - `#[foo = include_str!("nonexistent-file.rs")]`:
|
|
|
|
// results in `ast::ExprKind::Err`. In that case we delay
|
|
|
|
// the error because an earlier error will have already
|
|
|
|
// been reported.
|
2024-02-23 19:56:35 +01:00
|
|
|
let msg = "attribute value must be a literal";
|
2024-06-18 09:43:28 +00:00
|
|
|
let mut err = psess.dcx().struct_span_err(expr.span, msg);
|
2024-02-25 22:22:11 +01:00
|
|
|
if let ast::ExprKind::Err(_) = expr.kind {
|
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-29 06:52:01 +10:00
|
|
|
err.downgrade_to_delayed_bug();
|
|
|
|
}
|
|
|
|
return Err(err);
|
|
|
|
}
|
|
|
|
}
|
2019-10-11 21:00:09 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-06-03 15:47:46 +10:00
|
|
|
fn check_meta_bad_delim(psess: &ParseSess, span: DelimSpan, delim: Delimiter) {
|
2023-08-02 09:56:26 +10:00
|
|
|
if let Delimiter::Parenthesis = delim {
|
2019-12-05 14:19:00 +01:00
|
|
|
return;
|
|
|
|
}
|
2024-06-18 09:43:28 +00:00
|
|
|
psess.dcx().emit_err(errors::MetaBadDelim {
|
2023-04-27 01:53:06 +01:00
|
|
|
span: span.entire(),
|
|
|
|
sugg: errors::MetaBadDelimSugg { open: span.open, close: span.close },
|
|
|
|
});
|
|
|
|
}
|
2019-12-05 14:19:00 +01:00
|
|
|
|
2024-06-03 15:47:46 +10:00
|
|
|
pub(super) fn check_cfg_attr_bad_delim(psess: &ParseSess, span: DelimSpan, delim: Delimiter) {
|
2023-08-02 09:56:26 +10:00
|
|
|
if let Delimiter::Parenthesis = delim {
|
2023-04-27 01:53:06 +01:00
|
|
|
return;
|
|
|
|
}
|
2024-06-18 09:43:28 +00:00
|
|
|
psess.dcx().emit_err(errors::CfgAttrBadDelim {
|
2023-04-27 01:53:06 +01:00
|
|
|
span: span.entire(),
|
|
|
|
sugg: errors::MetaBadDelimSugg { open: span.open, close: span.close },
|
|
|
|
});
|
2019-12-05 14:19:00 +01:00
|
|
|
}
|
|
|
|
|
2019-11-30 00:56:46 +01:00
|
|
|
/// Checks that the given meta-item is compatible with this `AttributeTemplate`.
|
|
|
|
fn is_attr_template_compatible(template: &AttributeTemplate, meta: &ast::MetaItemKind) -> bool {
|
2024-10-04 21:59:04 +09:00
|
|
|
let is_one_allowed_subword = |items: &[MetaItemInner]| match items {
|
2024-06-20 17:44:11 +10:00
|
|
|
[item] => item.is_word() && template.one_of.iter().any(|&word| item.has_name(word)),
|
|
|
|
_ => false,
|
|
|
|
};
|
2019-11-30 00:56:46 +01:00
|
|
|
match meta {
|
|
|
|
MetaItemKind::Word => template.word,
|
2024-06-20 17:44:11 +10:00
|
|
|
MetaItemKind::List(items) => template.list.is_some() || is_one_allowed_subword(items),
|
2019-11-30 00:56:46 +01:00
|
|
|
MetaItemKind::NameValue(lit) if lit.kind.is_str() => template.name_value_str.is_some(),
|
|
|
|
MetaItemKind::NameValue(..) => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-07 01:36:28 -05:00
|
|
|
pub fn check_attribute_safety(psess: &ParseSess, safety: AttributeSafety, attr: &Attribute) {
|
2024-07-09 19:06:49 -05:00
|
|
|
let attr_item = attr.get_normal_item();
|
|
|
|
|
2025-04-12 19:58:19 +02:00
|
|
|
if let AttributeSafety::Unsafe { unsafe_since } = safety {
|
2024-07-09 19:06:49 -05:00
|
|
|
if let ast::Safety::Default = attr_item.unsafety {
|
|
|
|
let path_span = attr_item.path.span;
|
|
|
|
|
|
|
|
// If the `attr_item`'s span is not from a macro, then just suggest
|
|
|
|
// wrapping it in `unsafe(...)`. Otherwise, we suggest putting the
|
|
|
|
// `unsafe(`, `)` right after and right before the opening and closing
|
|
|
|
// square bracket respectively.
|
2024-12-18 18:28:08 +00:00
|
|
|
let diag_span = attr_item.span();
|
2024-07-09 19:06:49 -05:00
|
|
|
|
2025-04-12 19:58:19 +02:00
|
|
|
// Attributes can be safe in earlier editions, and become unsafe in later ones.
|
|
|
|
let emit_error = match unsafe_since {
|
|
|
|
None => true,
|
|
|
|
Some(unsafe_since) => attr.span.edition() >= unsafe_since,
|
|
|
|
};
|
|
|
|
|
|
|
|
if emit_error {
|
2024-07-09 19:06:49 -05:00
|
|
|
psess.dcx().emit_err(errors::UnsafeAttrOutsideUnsafe {
|
|
|
|
span: path_span,
|
|
|
|
suggestion: errors::UnsafeAttrOutsideUnsafeSuggestion {
|
|
|
|
left: diag_span.shrink_to_lo(),
|
|
|
|
right: diag_span.shrink_to_hi(),
|
|
|
|
},
|
2024-07-02 23:52:16 -05:00
|
|
|
});
|
2024-07-09 19:06:49 -05:00
|
|
|
} else {
|
|
|
|
psess.buffer_lint(
|
|
|
|
UNSAFE_ATTR_OUTSIDE_UNSAFE,
|
|
|
|
path_span,
|
|
|
|
ast::CRATE_NODE_ID,
|
|
|
|
BuiltinLintDiag::UnsafeAttrOutsideUnsafe {
|
|
|
|
attribute_name_span: path_span,
|
|
|
|
sugg_spans: (diag_span.shrink_to_lo(), diag_span.shrink_to_hi()),
|
|
|
|
},
|
|
|
|
);
|
2024-07-02 23:52:16 -05:00
|
|
|
}
|
|
|
|
}
|
2024-09-11 17:23:56 -04:00
|
|
|
} else if let Safety::Unsafe(unsafe_span) = attr_item.unsafety {
|
|
|
|
psess.dcx().emit_err(errors::InvalidAttrUnsafe {
|
|
|
|
span: unsafe_span,
|
|
|
|
name: attr_item.path.clone(),
|
|
|
|
});
|
2024-07-02 23:52:16 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called by `check_builtin_meta_item` and code that manually denies
|
|
|
|
// `unsafe(...)` in `cfg`
|
2024-08-07 01:36:28 -05:00
|
|
|
pub fn deny_builtin_meta_unsafety(psess: &ParseSess, meta: &MetaItem) {
|
2024-07-02 23:52:16 -05:00
|
|
|
// This only supports denying unsafety right now - making builtin attributes
|
|
|
|
// support unsafety will requite us to thread the actual `Attribute` through
|
|
|
|
// for the nice diagnostics.
|
2024-08-07 01:36:28 -05:00
|
|
|
if let Safety::Unsafe(unsafe_span) = meta.unsafety {
|
|
|
|
psess
|
|
|
|
.dcx()
|
|
|
|
.emit_err(errors::InvalidAttrUnsafe { span: unsafe_span, name: meta.path.clone() });
|
2024-07-02 23:52:16 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-24 16:00:57 +11:00
|
|
|
pub fn check_builtin_meta_item(
|
2024-03-04 16:31:49 +11:00
|
|
|
psess: &ParseSess,
|
2022-11-24 16:00:57 +11:00
|
|
|
meta: &MetaItem,
|
|
|
|
style: ast::AttrStyle,
|
|
|
|
name: Symbol,
|
|
|
|
template: AttributeTemplate,
|
2024-07-02 23:52:16 -05:00
|
|
|
deny_unsafety: bool,
|
2022-11-24 16:00:57 +11:00
|
|
|
) {
|
2025-03-22 21:42:34 +03:00
|
|
|
if !is_attr_template_compatible(&template, &meta.kind) {
|
2024-03-04 16:31:49 +11:00
|
|
|
emit_malformed_attribute(psess, style, meta.span, name, template);
|
2022-11-24 16:00:57 +11:00
|
|
|
}
|
2024-07-02 23:52:16 -05:00
|
|
|
|
|
|
|
if deny_unsafety {
|
2024-08-07 01:36:28 -05:00
|
|
|
deny_builtin_meta_unsafety(psess, meta);
|
2024-07-02 23:52:16 -05:00
|
|
|
}
|
2022-11-24 16:00:57 +11:00
|
|
|
}
|
|
|
|
|
2021-09-16 17:48:06 -07:00
|
|
|
fn emit_malformed_attribute(
|
2024-03-04 16:31:49 +11:00
|
|
|
psess: &ParseSess,
|
2022-11-24 16:00:57 +11:00
|
|
|
style: ast::AttrStyle,
|
|
|
|
span: Span,
|
2021-09-16 17:48:06 -07: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)
|
|
|
|
};
|
|
|
|
|
2023-07-24 00:18:52 -04:00
|
|
|
let error_msg = format!("malformed `{name}` attribute input");
|
2021-09-16 17:48:06 -07:00
|
|
|
let mut suggestions = vec![];
|
2022-11-24 16:00:57 +11:00
|
|
|
let inner = if style == ast::AttrStyle::Inner { "!" } else { "" };
|
2021-09-16 17:48:06 -07:00
|
|
|
if template.word {
|
2024-04-14 20:11:14 +00:00
|
|
|
suggestions.push(format!("#{inner}[{name}]"));
|
2021-09-16 17:48:06 -07:00
|
|
|
}
|
|
|
|
if let Some(descr) = template.list {
|
2024-04-14 20:11:14 +00:00
|
|
|
suggestions.push(format!("#{inner}[{name}({descr})]"));
|
2021-09-16 17:48:06 -07:00
|
|
|
}
|
2024-06-20 17:44:11 +10:00
|
|
|
suggestions.extend(template.one_of.iter().map(|&word| format!("#{inner}[{name}({word})]")));
|
2021-09-16 17:48:06 -07:00
|
|
|
if let Some(descr) = template.name_value_str {
|
2024-04-14 20:11:14 +00:00
|
|
|
suggestions.push(format!("#{inner}[{name} = \"{descr}\"]"));
|
2021-09-16 17:48:06 -07:00
|
|
|
}
|
|
|
|
if should_warn(name) {
|
2024-05-20 17:47:54 +00:00
|
|
|
psess.buffer_lint(
|
2024-04-14 20:11:14 +00:00
|
|
|
ILL_FORMED_ATTRIBUTE_INPUT,
|
|
|
|
span,
|
|
|
|
ast::CRATE_NODE_ID,
|
|
|
|
BuiltinLintDiag::IllFormedAttributeInput { suggestions: suggestions.clone() },
|
|
|
|
);
|
2021-09-16 17:48:06 -07:00
|
|
|
} else {
|
2024-04-14 20:11:14 +00:00
|
|
|
suggestions.sort();
|
2024-03-04 16:31:49 +11:00
|
|
|
psess
|
2024-06-18 09:43:28 +00:00
|
|
|
.dcx()
|
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-20 13:26:58 +10:00
|
|
|
.struct_span_err(span, error_msg)
|
2024-01-09 09:08:49 +11:00
|
|
|
.with_span_suggestions(
|
2022-11-24 16:00:57 +11:00
|
|
|
span,
|
2021-09-16 17:48:06 -07:00
|
|
|
if suggestions.len() == 1 {
|
|
|
|
"must be of the form"
|
|
|
|
} else {
|
|
|
|
"the following are the possible correct uses"
|
|
|
|
},
|
2023-12-15 23:56:24 +01:00
|
|
|
suggestions,
|
2021-09-16 17:48:06 -07:00
|
|
|
Applicability::HasPlaceholders,
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
}
|
2021-09-17 13:08:56 -07:00
|
|
|
|
|
|
|
pub fn emit_fatal_malformed_builtin_attribute(
|
2024-03-04 16:31:49 +11:00
|
|
|
psess: &ParseSess,
|
2021-09-17 13:08:56 -07:00
|
|
|
attr: &Attribute,
|
|
|
|
name: Symbol,
|
|
|
|
) -> ! {
|
2021-11-12 20:15:14 +08:00
|
|
|
let template = BUILTIN_ATTRIBUTE_MAP.get(&name).expect("builtin attr defined").template;
|
2024-03-04 16:31:49 +11:00
|
|
|
emit_malformed_attribute(psess, attr.style, attr.span, name, template);
|
2021-09-17 13:08:56 -07: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()
|
|
|
|
}
|