Skip to content

Commit 49ed25b

Browse files
committed
Remove NtExpr and NtLiteral.
Notes about tests: - tests/ui/rfcs/rfc-2294-if-let-guard/feature-gate.rs: some messages are now duplicated due to repeated parsing. - tests/ui/rfcs/rfc-2497-if-let-chains/disallowed-positions.rs: ditto. - `tests/ui/proc-macro/macro-rules-derive-cfg.rs`: the diff looks large but the only difference is the insertion of a single invisible-delimited group around a metavar. - `tests/ui/attributes/nonterminal-expansion.rs`: a slight span degradation, somehow related to the recent massive attr parsing rewrite (rust-lang#135726). I couldn't work out exactly what is going wrong, but I don't think it's worth holding things up for a single slightly suboptimal error message.
1 parent 0b4a81a commit 49ed25b

30 files changed

+801
-585
lines changed

compiler/rustc_ast/src/ast_traits.rs

-2
Original file line numberDiff line numberDiff line change
@@ -209,13 +209,11 @@ impl HasTokens for Attribute {
209209
impl HasTokens for Nonterminal {
210210
fn tokens(&self) -> Option<&LazyAttrTokenStream> {
211211
match self {
212-
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => expr.tokens(),
213212
Nonterminal::NtBlock(block) => block.tokens(),
214213
}
215214
}
216215
fn tokens_mut(&mut self) -> Option<&mut Option<LazyAttrTokenStream>> {
217216
match self {
218-
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => expr.tokens_mut(),
219217
Nonterminal::NtBlock(block) => block.tokens_mut(),
220218
}
221219
}

compiler/rustc_ast/src/mut_visit.rs

-2
Original file line numberDiff line numberDiff line change
@@ -899,8 +899,6 @@ pub fn visit_token<T: MutVisitor>(vis: &mut T, t: &mut Token) {
899899
fn visit_nonterminal<T: MutVisitor>(vis: &mut T, nt: &mut token::Nonterminal) {
900900
match nt {
901901
token::NtBlock(block) => vis.visit_block(block),
902-
token::NtExpr(expr) => vis.visit_expr(expr),
903-
token::NtLiteral(expr) => vis.visit_expr(expr),
904902
}
905903
}
906904

compiler/rustc_ast/src/token.rs

+23-48
Original file line numberDiff line numberDiff line change
@@ -198,16 +198,17 @@ impl Lit {
198198
}
199199
}
200200

201-
/// Keep this in sync with `Token::can_begin_literal_maybe_minus` excluding unary negation.
201+
/// Keep this in sync with `Token::can_begin_literal_maybe_minus` and
202+
/// `Parser::eat_token_lit` (excluding unary negation).
202203
pub fn from_token(token: &Token) -> Option<Lit> {
203204
match token.uninterpolate().kind {
204205
Ident(name, IdentIsRaw::No) if name.is_bool_lit() => Some(Lit::new(Bool, name, None)),
205206
Literal(token_lit) => Some(token_lit),
206-
Interpolated(ref nt)
207-
if let NtExpr(expr) | NtLiteral(expr) = &**nt
208-
&& let ast::ExprKind::Lit(token_lit) = expr.kind =>
209-
{
210-
Some(token_lit)
207+
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
208+
MetaVarKind::Literal | MetaVarKind::Expr { .. },
209+
))) => {
210+
// Unreachable with the current test suite.
211+
panic!("from_token metavar");
211212
}
212213
_ => None,
213214
}
@@ -590,6 +591,9 @@ impl Token {
590591
/// for which spans affect name resolution and edition checks.
591592
/// Note that keywords are also identifiers, so they should use this
592593
/// if they keep spans or perform edition checks.
594+
//
595+
// Note: `Parser::uninterpolated_token_span` may give better information
596+
// than this method does.
593597
pub fn uninterpolated_span(&self) -> Span {
594598
match self.kind {
595599
NtIdent(ident, _) | NtLifetime(ident, _) => ident.span,
@@ -642,12 +646,7 @@ impl Token {
642646
PathSep | // global path
643647
Lifetime(..) | // labeled loop
644648
Pound => true, // expression attributes
645-
Interpolated(ref nt) =>
646-
matches!(&**nt,
647-
NtBlock(..) |
648-
NtExpr(..) |
649-
NtLiteral(..)
650-
),
649+
Interpolated(ref nt) => matches!(&**nt, NtBlock(..)),
651650
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
652651
MetaVarKind::Block |
653652
MetaVarKind::Expr { .. } |
@@ -677,11 +676,6 @@ impl Token {
677676
Lt | // path (UFCS constant)
678677
Shl => true, // path (double UFCS)
679678
Or => matches!(pat_kind, PatWithOr), // leading vert `|` or-pattern
680-
Interpolated(nt) =>
681-
matches!(&**nt,
682-
| NtExpr(..)
683-
| NtLiteral(..)
684-
),
685679
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
686680
MetaVarKind::Expr { .. } |
687681
MetaVarKind::Literal |
@@ -724,7 +718,7 @@ impl Token {
724718
match self.kind {
725719
OpenDelim(Delimiter::Brace) | Literal(..) | Minus => true,
726720
Ident(name, IdentIsRaw::No) if name.is_bool_lit() => true,
727-
Interpolated(ref nt) => matches!(&**nt, NtExpr(..) | NtBlock(..) | NtLiteral(..)),
721+
Interpolated(ref nt) => matches!(&**nt, NtBlock(..)),
728722
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
729723
MetaVarKind::Expr { .. } | MetaVarKind::Block | MetaVarKind::Literal,
730724
))) => true,
@@ -768,22 +762,12 @@ impl Token {
768762
///
769763
/// In other words, would this token be a valid start of `parse_literal_maybe_minus`?
770764
///
771-
/// Keep this in sync with and `Lit::from_token`, excluding unary negation.
765+
/// Keep this in sync with `Lit::from_token` and `Parser::eat_token_lit`
766+
/// (excluding unary negation).
772767
pub fn can_begin_literal_maybe_minus(&self) -> bool {
773768
match self.uninterpolate().kind {
774769
Literal(..) | Minus => true,
775770
Ident(name, IdentIsRaw::No) if name.is_bool_lit() => true,
776-
Interpolated(ref nt) => match &**nt {
777-
NtLiteral(_) => true,
778-
NtExpr(e) => match &e.kind {
779-
ast::ExprKind::Lit(_) => true,
780-
ast::ExprKind::Unary(ast::UnOp::Neg, e) => {
781-
matches!(&e.kind, ast::ExprKind::Lit(_))
782-
}
783-
_ => false,
784-
},
785-
_ => false,
786-
},
787771
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(mv_kind))) => match mv_kind {
788772
MetaVarKind::Literal => true,
789773
MetaVarKind::Expr { can_begin_literal_maybe_minus, .. } => {
@@ -798,14 +782,6 @@ impl Token {
798782
pub fn can_begin_string_literal(&self) -> bool {
799783
match self.uninterpolate().kind {
800784
Literal(..) => true,
801-
Interpolated(ref nt) => match &**nt {
802-
NtLiteral(_) => true,
803-
NtExpr(e) => match &e.kind {
804-
ast::ExprKind::Lit(_) => true,
805-
_ => false,
806-
},
807-
_ => false,
808-
},
809785
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(mv_kind))) => match mv_kind {
810786
MetaVarKind::Literal => true,
811787
MetaVarKind::Expr { can_begin_string_literal, .. } => can_begin_string_literal,
@@ -869,19 +845,25 @@ impl Token {
869845

870846
/// Is this a pre-parsed expression dropped into the token stream
871847
/// (which happens while parsing the result of macro expansion)?
872-
pub fn is_whole_expr(&self) -> bool {
848+
pub fn is_metavar_expr(&self) -> bool {
873849
#[allow(irrefutable_let_patterns)] // FIXME: temporary
874850
if let Interpolated(nt) = &self.kind
875-
&& let NtExpr(_) | NtLiteral(_) | NtBlock(_) = &**nt
851+
&& let NtBlock(_) = &**nt
876852
{
877853
true
854+
} else if matches!(
855+
self.is_metavar_seq(),
856+
Some(MetaVarKind::Expr { .. } | MetaVarKind::Literal | MetaVarKind::Path)
857+
) {
858+
true
878859
} else {
879860
matches!(self.is_metavar_seq(), Some(MetaVarKind::Path))
880861
}
881862
}
882863

883864
/// Is the token an interpolated block (`$b:block`)?
884865
pub fn is_whole_block(&self) -> bool {
866+
#[allow(irrefutable_let_patterns)] // FIXME: temporary
885867
if let Interpolated(nt) = &self.kind
886868
&& let NtBlock(..) = &**nt
887869
{
@@ -1100,8 +1082,6 @@ pub enum NtExprKind {
11001082
/// For interpolation during macro expansion.
11011083
pub enum Nonterminal {
11021084
NtBlock(P<ast::Block>),
1103-
NtExpr(P<ast::Expr>),
1104-
NtLiteral(P<ast::Expr>),
11051085
}
11061086

11071087
#[derive(Debug, Copy, Clone, PartialEq, Eq, Encodable, Decodable, Hash, HashStable_Generic)]
@@ -1191,15 +1171,12 @@ impl Nonterminal {
11911171
pub fn use_span(&self) -> Span {
11921172
match self {
11931173
NtBlock(block) => block.span,
1194-
NtExpr(expr) | NtLiteral(expr) => expr.span,
11951174
}
11961175
}
11971176

11981177
pub fn descr(&self) -> &'static str {
11991178
match self {
12001179
NtBlock(..) => "block",
1201-
NtExpr(..) => "expression",
1202-
NtLiteral(..) => "literal",
12031180
}
12041181
}
12051182
}
@@ -1218,8 +1195,6 @@ impl fmt::Debug for Nonterminal {
12181195
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
12191196
match *self {
12201197
NtBlock(..) => f.pad("NtBlock(..)"),
1221-
NtExpr(..) => f.pad("NtExpr(..)"),
1222-
NtLiteral(..) => f.pad("NtLiteral(..)"),
12231198
}
12241199
}
12251200
}
@@ -1242,7 +1217,7 @@ mod size_asserts {
12421217
// tidy-alphabetical-start
12431218
static_assert_size!(Lit, 12);
12441219
static_assert_size!(LitKind, 2);
1245-
static_assert_size!(Nonterminal, 16);
1220+
static_assert_size!(Nonterminal, 8);
12461221
static_assert_size!(Token, 24);
12471222
static_assert_size!(TokenKind, 16);
12481223
// tidy-alphabetical-end

compiler/rustc_ast/src/tokenstream.rs

-1
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,6 @@ impl TokenStream {
462462
pub fn from_nonterminal_ast(nt: &Nonterminal) -> TokenStream {
463463
match nt {
464464
Nonterminal::NtBlock(block) => TokenStream::from_ast(block),
465-
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => TokenStream::from_ast(expr),
466465
}
467466
}
468467

compiler/rustc_expand/src/mbe/transcribe.rs

+28-7
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@ use std::sync::Arc;
33

44
use rustc_ast::mut_visit::{self, MutVisitor};
55
use rustc_ast::token::{
6-
self, Delimiter, IdentIsRaw, InvisibleOrigin, Lit, LitKind, MetaVarKind, Nonterminal, Token,
7-
TokenKind,
6+
self, Delimiter, IdentIsRaw, InvisibleOrigin, Lit, LitKind, MetaVarKind, Token, TokenKind,
87
};
98
use rustc_ast::tokenstream::{DelimSpacing, DelimSpan, Spacing, TokenStream, TokenTree};
10-
use rustc_ast::{ExprKind, StmtKind, TyKind};
9+
use rustc_ast::{ExprKind, StmtKind, TyKind, UnOp};
1110
use rustc_data_structures::fx::FxHashMap;
1211
use rustc_errors::{Diag, DiagCtxtHandle, PResult, pluralize};
1312
use rustc_parse::lexer::nfc_normalize;
@@ -340,6 +339,30 @@ pub(super) fn transcribe<'a>(
340339
MetaVarKind::Pat(*pat_kind),
341340
TokenStream::from_ast(pat),
342341
),
342+
MatchedSingle(ParseNtResult::Expr(expr, kind)) => {
343+
let (can_begin_literal_maybe_minus, can_begin_string_literal) =
344+
match &expr.kind {
345+
ExprKind::Lit(_) => (true, true),
346+
ExprKind::Unary(UnOp::Neg, e)
347+
if matches!(&e.kind, ExprKind::Lit(_)) =>
348+
{
349+
(true, false)
350+
}
351+
_ => (false, false),
352+
};
353+
mk_delimited(
354+
expr.span,
355+
MetaVarKind::Expr {
356+
kind: *kind,
357+
can_begin_literal_maybe_minus,
358+
can_begin_string_literal,
359+
},
360+
TokenStream::from_ast(expr),
361+
)
362+
}
363+
MatchedSingle(ParseNtResult::Literal(lit)) => {
364+
mk_delimited(lit.span, MetaVarKind::Literal, TokenStream::from_ast(lit))
365+
}
343366
MatchedSingle(ParseNtResult::Ty(ty)) => {
344367
let is_path = matches!(&ty.kind, TyKind::Path(None, _path));
345368
mk_delimited(
@@ -869,10 +892,8 @@ fn extract_symbol_from_pnr<'a>(
869892
},
870893
_,
871894
)) => Ok(*symbol),
872-
ParseNtResult::Nt(nt)
873-
if let Nonterminal::NtLiteral(expr) = &**nt
874-
&& let ExprKind::Lit(Lit { kind: LitKind::Str, symbol, suffix: None }) =
875-
&expr.kind =>
895+
ParseNtResult::Literal(expr)
896+
if let ExprKind::Lit(Lit { kind: LitKind::Str, symbol, suffix: None }) = &expr.kind =>
876897
{
877898
Ok(*symbol)
878899
}

compiler/rustc_parse/messages.ftl

+1-1
Original file line numberDiff line numberDiff line change
@@ -858,7 +858,7 @@ parse_unexpected_parentheses_in_match_arm_pattern = unexpected parentheses surro
858858
parse_unexpected_self_in_generic_parameters = unexpected keyword `Self` in generic parameters
859859
.note = you cannot use `Self` as a generic parameter because it is reserved for associated items
860860
861-
parse_unexpected_token_after_dot = unexpected token: `{$actual}`
861+
parse_unexpected_token_after_dot = unexpected token: {$actual}
862862
863863
parse_unexpected_token_after_label = expected `while`, `for`, `loop` or `{"{"}` after a label
864864
.suggestion_remove_label = consider removing the label

compiler/rustc_parse/src/errors.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1696,10 +1696,10 @@ pub(crate) struct SelfArgumentPointer {
16961696

16971697
#[derive(Diagnostic)]
16981698
#[diag(parse_unexpected_token_after_dot)]
1699-
pub(crate) struct UnexpectedTokenAfterDot<'a> {
1699+
pub(crate) struct UnexpectedTokenAfterDot {
17001700
#[primary_span]
17011701
pub span: Span,
1702-
pub actual: Cow<'a, str>,
1702+
pub actual: String,
17031703
}
17041704

17051705
#[derive(Diagnostic)]

0 commit comments

Comments
 (0)