Auto merge of #96546 - nnethercote:overhaul-MacArgs, r=petrochenkov
Overhaul `MacArgs` Motivation: - Clarify some code that I found hard to understand. - Eliminate one use of three places where `TokenKind::Interpolated` values are created. r? `@petrochenkov`
This commit is contained in:
commit
4c60a0ea5b
22 changed files with 193 additions and 115 deletions
|
@ -26,11 +26,10 @@ use rustc_ast::tokenstream::{TokenStream, TokenTree};
|
|||
use rustc_ast::AttrId;
|
||||
use rustc_ast::DUMMY_NODE_ID;
|
||||
use rustc_ast::{self as ast, AnonConst, AstLike, AttrStyle, AttrVec, Const, CrateSugar, Extern};
|
||||
use rustc_ast::{Async, Expr, ExprKind, MacArgs, MacDelimiter, Mutability, StrLit, Unsafe};
|
||||
use rustc_ast::{Visibility, VisibilityKind};
|
||||
use rustc_ast::{Async, Expr, ExprKind, MacArgs, MacArgsEq, MacDelimiter, Mutability, StrLit};
|
||||
use rustc_ast::{Unsafe, Visibility, VisibilityKind};
|
||||
use rustc_ast_pretty::pprust;
|
||||
use rustc_data_structures::fx::FxHashMap;
|
||||
use rustc_data_structures::sync::Lrc;
|
||||
use rustc_errors::PResult;
|
||||
use rustc_errors::{
|
||||
struct_span_err, Applicability, DiagnosticBuilder, ErrorGuaranteed, FatalError, MultiSpan,
|
||||
|
@ -1157,13 +1156,7 @@ impl<'a> Parser<'a> {
|
|||
} else if !delimited_only {
|
||||
if self.eat(&token::Eq) {
|
||||
let eq_span = self.prev_token.span;
|
||||
|
||||
// Collect tokens because they are used during lowering to HIR.
|
||||
let expr = self.parse_expr_force_collect()?;
|
||||
let span = expr.span;
|
||||
|
||||
let token_kind = token::Interpolated(Lrc::new(token::NtExpr(expr)));
|
||||
MacArgs::Eq(eq_span, Token::new(token_kind, span))
|
||||
MacArgs::Eq(eq_span, MacArgsEq::Ast(self.parse_expr_force_collect()?))
|
||||
} else {
|
||||
MacArgs::Empty
|
||||
}
|
||||
|
|
|
@ -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()),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue