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
|
@ -38,8 +38,7 @@
|
|||
#![recursion_limit = "256"]
|
||||
#![allow(rustc::potential_query_instability)]
|
||||
|
||||
use rustc_ast::token::{Delimiter, Token};
|
||||
use rustc_ast::tokenstream::{CanSynthesizeMissingTokens, TokenStream, TokenTree};
|
||||
use rustc_ast::tokenstream::{CanSynthesizeMissingTokens, TokenStream};
|
||||
use rustc_ast::visit;
|
||||
use rustc_ast::{self as ast, *};
|
||||
use rustc_ast_pretty::pprust;
|
||||
|
@ -884,37 +883,24 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
|||
)
|
||||
}
|
||||
// This is an inert key-value attribute - it will never be visible to macros
|
||||
// after it gets lowered to HIR. Therefore, we can synthesize tokens with fake
|
||||
// spans to handle nonterminals in `#[doc]` (e.g. `#[doc = $e]`).
|
||||
MacArgs::Eq(eq_span, ref token) => {
|
||||
// In valid code the value is always representable as a single literal token.
|
||||
fn unwrap_single_token(sess: &Session, tokens: TokenStream, span: Span) -> Token {
|
||||
if tokens.len() != 1 {
|
||||
sess.diagnostic()
|
||||
.delay_span_bug(span, "multiple tokens in key-value attribute's value");
|
||||
// after it gets lowered to HIR. Therefore, we can extract literals to handle
|
||||
// nonterminals in `#[doc]` (e.g. `#[doc = $e]`).
|
||||
MacArgs::Eq(eq_span, MacArgsEq::Ast(ref expr)) => {
|
||||
// In valid code the value always ends up as a single literal. Otherwise, a dummy
|
||||
// literal suffices because the error is handled elsewhere.
|
||||
let lit = if let ExprKind::Lit(lit) = &expr.kind {
|
||||
lit.clone()
|
||||
} else {
|
||||
Lit {
|
||||
token: token::Lit::new(token::LitKind::Err, kw::Empty, None),
|
||||
kind: LitKind::Err(kw::Empty),
|
||||
span: DUMMY_SP,
|
||||
}
|
||||
match tokens.into_trees().next() {
|
||||
Some(TokenTree::Token(token)) => token,
|
||||
Some(TokenTree::Delimited(_, delim, tokens)) => {
|
||||
if delim != Delimiter::Invisible {
|
||||
sess.diagnostic().delay_span_bug(
|
||||
span,
|
||||
"unexpected delimiter in key-value attribute's value",
|
||||
);
|
||||
}
|
||||
unwrap_single_token(sess, tokens, span)
|
||||
}
|
||||
None => Token::dummy(),
|
||||
}
|
||||
}
|
||||
|
||||
let tokens = FlattenNonterminals {
|
||||
parse_sess: &self.sess.parse_sess,
|
||||
synthesize_tokens: CanSynthesizeMissingTokens::Yes,
|
||||
nt_to_tokenstream: self.nt_to_tokenstream,
|
||||
}
|
||||
.process_token(token.clone());
|
||||
MacArgs::Eq(eq_span, unwrap_single_token(self.sess, tokens, token.span))
|
||||
};
|
||||
MacArgs::Eq(eq_span, MacArgsEq::Hir(lit))
|
||||
}
|
||||
MacArgs::Eq(_, MacArgsEq::Hir(ref lit)) => {
|
||||
unreachable!("in literal form when lowering mac args eq: {:?}", lit)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue