diff options
| author | Nicholas Nethercote <n.nethercote@gmail.com> | 2022-04-29 06:52:01 +1000 |
|---|---|---|
| committer | Nicholas Nethercote <n.nethercote@gmail.com> | 2022-05-05 07:06:12 +1000 |
| commit | 99f5945f85342e1eff8d31507410ddd66ea94d64 (patch) | |
| tree | 6594fd89e3820be4bfa2b6d99ec0447c4fc1c1ff /compiler/rustc_parse/src/validate_attr.rs | |
| parent | ae5f67f9e8a560c66d1c4afea1750d21f1d093e7 (diff) | |
| download | rust-99f5945f85342e1eff8d31507410ddd66ea94d64.tar.gz rust-99f5945f85342e1eff8d31507410ddd66ea94d64.zip | |
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.
Diffstat (limited to 'compiler/rustc_parse/src/validate_attr.rs')
| -rw-r--r-- | compiler/rustc_parse/src/validate_attr.rs | 39 |
1 files changed, 32 insertions, 7 deletions
diff --git a/compiler/rustc_parse/src/validate_attr.rs b/compiler/rustc_parse/src/validate_attr.rs index 4781813ee8e..47477898b24 100644 --- a/compiler/rustc_parse/src/validate_attr.rs +++ b/compiler/rustc_parse/src/validate_attr.rs @@ -2,8 +2,9 @@ use crate::parse_in; -use rustc_ast::tokenstream::{DelimSpan, TokenTree}; -use rustc_ast::{self as ast, Attribute, MacArgs, MacDelimiter, MetaItem, MetaItemKind}; +use rustc_ast::tokenstream::DelimSpan; +use rustc_ast::{self as ast, Attribute, MacArgs, MacArgsEq, MacDelimiter, MetaItem, MetaItemKind}; +use rustc_ast_pretty::pprust; use rustc_errors::{Applicability, FatalError, PResult}; use rustc_feature::{AttributeTemplate, BuiltinAttribute, BUILTIN_ATTRIBUTE_MAP}; use rustc_session::lint::builtin::ILL_FORMED_ATTRIBUTE_INPUT; @@ -42,16 +43,40 @@ pub fn parse_meta<'a>(sess: &'a ParseSess, attr: &Attribute) -> PResult<'a, Meta path: item.path.clone(), kind: match &item.args { MacArgs::Empty => MetaItemKind::Word, - MacArgs::Eq(_, t) => { - let t = TokenTree::Token(t.clone()).into(); - let v = parse_in(sess, t, "name value", |p| p.parse_unsuffixed_lit())?; - MetaItemKind::NameValue(v) - } MacArgs::Delimited(dspan, delim, t) => { check_meta_bad_delim(sess, *dspan, *delim, "wrong meta list delimiters"); let nmis = parse_in(sess, t.clone(), "meta list", |p| p.parse_meta_seq_top())?; MetaItemKind::List(nmis) } + MacArgs::Eq(_, MacArgsEq::Ast(expr)) => { + if let ast::ExprKind::Lit(lit) = &expr.kind { + if !lit.kind.is_unsuffixed() { + let mut err = sess.span_diagnostic.struct_span_err( + lit.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.clone()) + } + } else { + // The non-error case can happen with e.g. `#[foo = 1+1]`. The error case can + // happen with e.g. `#[foo = include_str!("non-existent-file.rs")]`; in that + // 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); + } + } + MacArgs::Eq(_, MacArgsEq::Hir(lit)) => MetaItemKind::NameValue(lit.clone()), }, }) } |
