Skip to content

Commit cde25f8

Browse files
committed
Simplify lower_mac_args.
The `token` is always an interpolated non-terminal expression, and always a literal in valid code. This commit simplifies the processing accordingly, by directly extracting and using the literal.
1 parent 481a446 commit cde25f8

File tree

1 file changed

+14
-28
lines changed
  • compiler/rustc_ast_lowering/src

1 file changed

+14
-28
lines changed

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@
3838
#![recursion_limit = "256"]
3939
#![allow(rustc::potential_query_instability)]
4040

41-
use rustc_ast::token::{Delimiter, Token};
42-
use rustc_ast::tokenstream::{CanSynthesizeMissingTokens, TokenStream, TokenTree};
41+
use rustc_ast::token::{self, Token, TokenKind};
42+
use rustc_ast::tokenstream::{CanSynthesizeMissingTokens, TokenStream};
4343
use rustc_ast::visit;
4444
use rustc_ast::{self as ast, *};
4545
use rustc_ast_pretty::pprust;
@@ -878,33 +878,19 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
878878
// spans to handle nonterminals in `#[doc]` (e.g. `#[doc = $e]`).
879879
MacArgs::Eq(eq_span, ref token) => {
880880
// In valid code the value is always representable as a single literal token.
881-
fn unwrap_single_token(sess: &Session, tokens: TokenStream, span: Span) -> Token {
882-
if tokens.len() != 1 {
883-
sess.diagnostic()
884-
.delay_span_bug(span, "multiple tokens in key-value attribute's value");
885-
}
886-
match tokens.into_trees().next() {
887-
Some(TokenTree::Token(token)) => token,
888-
Some(TokenTree::Delimited(_, delim, tokens)) => {
889-
if delim != Delimiter::Invisible {
890-
sess.diagnostic().delay_span_bug(
891-
span,
892-
"unexpected delimiter in key-value attribute's value",
893-
);
894-
}
895-
unwrap_single_token(sess, tokens, span)
896-
}
897-
None => Token::dummy(),
881+
// Otherwise, a dummy token suffices because the error is handled elsewhere.
882+
let token = if let token::Interpolated(nt) = &token.kind
883+
&& let token::NtExpr(expr) = &**nt
884+
{
885+
if let ExprKind::Lit(Lit { token, span, .. }) = expr.kind {
886+
Token::new(TokenKind::Literal(token), span)
887+
} else {
888+
Token::dummy()
898889
}
899-
}
900-
901-
let tokens = FlattenNonterminals {
902-
parse_sess: &self.sess.parse_sess,
903-
synthesize_tokens: CanSynthesizeMissingTokens::Yes,
904-
nt_to_tokenstream: self.nt_to_tokenstream,
905-
}
906-
.process_token(token.clone());
907-
MacArgs::Eq(eq_span, unwrap_single_token(self.sess, tokens, token.span))
890+
} else {
891+
unreachable!()
892+
};
893+
MacArgs::Eq(eq_span, token)
908894
}
909895
}
910896
}

0 commit comments

Comments
 (0)