Skip to content

Commit 0174bf4

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.
1 parent 62a8dbe commit 0174bf4

29 files changed

+596
-416
lines changed

compiler/rustc_ast/src/ast_traits.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,13 +199,11 @@ impl HasTokens for Attribute {
199199
impl HasTokens for Nonterminal {
200200
fn tokens(&self) -> Option<&LazyAttrTokenStream> {
201201
match self {
202-
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => expr.tokens(),
203202
Nonterminal::NtBlock(block) => block.tokens(),
204203
}
205204
}
206205
fn tokens_mut(&mut self) -> Option<&mut Option<LazyAttrTokenStream>> {
207206
match self {
208-
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => expr.tokens_mut(),
209207
Nonterminal::NtBlock(block) => block.tokens_mut(),
210208
}
211209
}

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -801,8 +801,6 @@ pub fn visit_token<T: MutVisitor>(vis: &mut T, t: &mut Token) {
801801
fn visit_nonterminal<T: MutVisitor>(vis: &mut T, nt: &mut token::Nonterminal) {
802802
match nt {
803803
token::NtBlock(block) => vis.visit_block(block),
804-
token::NtExpr(expr) => vis.visit_expr(expr),
805-
token::NtLiteral(expr) => vis.visit_expr(expr),
806804
}
807805
}
808806

compiler/rustc_ast/src/token.rs

Lines changed: 22 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -210,16 +210,17 @@ impl Lit {
210210
}
211211
}
212212

213-
/// Keep this in sync with `Token::can_begin_literal_maybe_minus` excluding unary negation.
213+
/// Keep this in sync with `Token::can_begin_literal_maybe_minus` and
214+
/// `Parser::eat_token_lit` (excluding unary negation).
214215
pub fn from_token(token: &Token) -> Option<Lit> {
215216
match token.uninterpolate().kind {
216217
Ident(name, IdentIsRaw::No) if name.is_bool_lit() => Some(Lit::new(Bool, name, None)),
217218
Literal(token_lit) => Some(token_lit),
218-
Interpolated(ref nt)
219-
if let NtExpr(expr) | NtLiteral(expr) = &**nt
220-
&& let ast::ExprKind::Lit(token_lit) = expr.kind =>
221-
{
222-
Some(token_lit)
219+
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
220+
MetaVarKind::Literal | MetaVarKind::Expr { .. },
221+
))) => {
222+
// Unreachable with the current test suite.
223+
panic!("from_token metavar");
223224
}
224225
_ => None,
225226
}
@@ -564,6 +565,9 @@ impl Token {
564565
/// for which spans affect name resolution and edition checks.
565566
/// Note that keywords are also identifiers, so they should use this
566567
/// if they keep spans or perform edition checks.
568+
//
569+
// Note: `Parser::uninterpolated_token_span` may give better information
570+
// than this method does.
567571
pub fn uninterpolated_span(&self) -> Span {
568572
match self.kind {
569573
NtIdent(ident, _) | NtLifetime(ident, _) => ident.span,
@@ -616,12 +620,7 @@ impl Token {
616620
PathSep | // global path
617621
Lifetime(..) | // labeled loop
618622
Pound => true, // expression attributes
619-
Interpolated(ref nt) =>
620-
matches!(&**nt,
621-
NtBlock(..) |
622-
NtExpr(..) |
623-
NtLiteral(..)
624-
),
623+
Interpolated(ref nt) => matches!(&**nt, NtBlock(..)),
625624
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
626625
MetaVarKind::Block |
627626
MetaVarKind::Expr { .. } |
@@ -652,11 +651,6 @@ impl Token {
652651
BinOp(Shl) => true, // path (double UFCS)
653652
// leading vert `|` or-pattern
654653
BinOp(Or) => matches!(pat_kind, PatWithOr),
655-
Interpolated(nt) =>
656-
matches!(&**nt,
657-
| NtExpr(..)
658-
| NtLiteral(..)
659-
),
660654
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
661655
MetaVarKind::Expr { .. } |
662656
MetaVarKind::Literal |
@@ -699,7 +693,7 @@ impl Token {
699693
match self.kind {
700694
OpenDelim(Delimiter::Brace) | Literal(..) | BinOp(Minus) => true,
701695
Ident(name, IdentIsRaw::No) if name.is_bool_lit() => true,
702-
Interpolated(ref nt) => matches!(&**nt, NtExpr(..) | NtBlock(..) | NtLiteral(..)),
696+
Interpolated(ref nt) => matches!(&**nt, NtBlock(..)),
703697
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
704698
MetaVarKind::Expr { .. } | MetaVarKind::Block | MetaVarKind::Literal,
705699
))) => true,
@@ -743,22 +737,12 @@ impl Token {
743737
///
744738
/// In other words, would this token be a valid start of `parse_literal_maybe_minus`?
745739
///
746-
/// Keep this in sync with and `Lit::from_token`, excluding unary negation.
740+
/// Keep this in sync with `Lit::from_token` and `Parser::eat_token_lit`
741+
/// (excluding unary negation).
747742
pub fn can_begin_literal_maybe_minus(&self) -> bool {
748743
match self.uninterpolate().kind {
749744
Literal(..) | BinOp(Minus) => true,
750745
Ident(name, IdentIsRaw::No) if name.is_bool_lit() => true,
751-
Interpolated(ref nt) => match &**nt {
752-
NtLiteral(_) => true,
753-
NtExpr(e) => match &e.kind {
754-
ast::ExprKind::Lit(_) => true,
755-
ast::ExprKind::Unary(ast::UnOp::Neg, e) => {
756-
matches!(&e.kind, ast::ExprKind::Lit(_))
757-
}
758-
_ => false,
759-
},
760-
_ => false,
761-
},
762746
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(mv_kind))) => match mv_kind {
763747
MetaVarKind::Literal => true,
764748
MetaVarKind::Expr { can_begin_literal_maybe_minus, .. } => {
@@ -773,14 +757,6 @@ impl Token {
773757
pub fn can_begin_string_literal(&self) -> bool {
774758
match self.uninterpolate().kind {
775759
Literal(..) => true,
776-
Interpolated(ref nt) => match &**nt {
777-
NtLiteral(_) => true,
778-
NtExpr(e) => match &e.kind {
779-
ast::ExprKind::Lit(_) => true,
780-
_ => false,
781-
},
782-
_ => false,
783-
},
784760
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(mv_kind))) => match mv_kind {
785761
MetaVarKind::Literal => true,
786762
MetaVarKind::Expr { can_begin_string_literal, .. } => can_begin_string_literal,
@@ -844,13 +820,16 @@ impl Token {
844820

845821
/// Is this a pre-parsed expression dropped into the token stream
846822
/// (which happens while parsing the result of macro expansion)?
847-
pub fn is_whole_expr(&self) -> bool {
823+
pub fn is_metavar_expr(&self) -> bool {
848824
#[allow(irrefutable_let_patterns)] // FIXME: temporary
849825
if let Interpolated(nt) = &self.kind
850-
&& let NtExpr(_) | NtLiteral(_) | NtBlock(_) = &**nt
826+
&& let NtBlock(_) = &**nt
851827
{
852828
true
853-
} else if matches!(self.is_metavar_seq(), Some(MetaVarKind::Path)) {
829+
} else if matches!(
830+
self.is_metavar_seq(),
831+
Some(MetaVarKind::Expr { .. } | MetaVarKind::Literal | MetaVarKind::Path)
832+
) {
854833
true
855834
} else {
856835
false
@@ -859,6 +838,7 @@ impl Token {
859838

860839
/// Is the token an interpolated block (`$b:block`)?
861840
pub fn is_whole_block(&self) -> bool {
841+
#[allow(irrefutable_let_patterns)] // FIXME: temporary
862842
if let Interpolated(nt) = &self.kind
863843
&& let NtBlock(..) = &**nt
864844
{
@@ -1052,8 +1032,6 @@ pub enum NtExprKind {
10521032
/// For interpolation during macro expansion.
10531033
pub enum Nonterminal {
10541034
NtBlock(P<ast::Block>),
1055-
NtExpr(P<ast::Expr>),
1056-
NtLiteral(P<ast::Expr>),
10571035
}
10581036

10591037
#[derive(Debug, Copy, Clone, PartialEq, Eq, Encodable, Decodable, Hash, HashStable_Generic)]
@@ -1143,15 +1121,12 @@ impl Nonterminal {
11431121
pub fn use_span(&self) -> Span {
11441122
match self {
11451123
NtBlock(block) => block.span,
1146-
NtExpr(expr) | NtLiteral(expr) => expr.span,
11471124
}
11481125
}
11491126

11501127
pub fn descr(&self) -> &'static str {
11511128
match self {
11521129
NtBlock(..) => "block",
1153-
NtExpr(..) => "expression",
1154-
NtLiteral(..) => "literal",
11551130
}
11561131
}
11571132
}
@@ -1170,8 +1145,6 @@ impl fmt::Debug for Nonterminal {
11701145
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
11711146
match *self {
11721147
NtBlock(..) => f.pad("NtBlock(..)"),
1173-
NtExpr(..) => f.pad("NtExpr(..)"),
1174-
NtLiteral(..) => f.pad("NtLiteral(..)"),
11751148
}
11761149
}
11771150
}
@@ -1194,7 +1167,7 @@ mod size_asserts {
11941167
// tidy-alphabetical-start
11951168
static_assert_size!(Lit, 12);
11961169
static_assert_size!(LitKind, 2);
1197-
static_assert_size!(Nonterminal, 16);
1170+
static_assert_size!(Nonterminal, 8);
11981171
static_assert_size!(Token, 24);
11991172
static_assert_size!(TokenKind, 16);
12001173
// tidy-alphabetical-end

compiler/rustc_ast/src/tokenstream.rs

Lines changed: 0 additions & 1 deletion
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

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ use std::mem;
22

33
use rustc_ast::mut_visit::{self, MutVisitor};
44
use rustc_ast::token::{
5-
self, Delimiter, IdentIsRaw, InvisibleOrigin, Lit, LitKind, MetaVarKind, Nonterminal, Token,
6-
TokenKind,
5+
self, Delimiter, IdentIsRaw, InvisibleOrigin, Lit, LitKind, MetaVarKind, Token, TokenKind,
76
};
87
use rustc_ast::tokenstream::{DelimSpacing, DelimSpan, Spacing, TokenStream, TokenTree};
9-
use rustc_ast::{ExprKind, StmtKind};
8+
use rustc_ast::{ExprKind, StmtKind, UnOp};
109
use rustc_data_structures::fx::FxHashMap;
1110
use rustc_errors::{Diag, DiagCtxtHandle, PResult, pluralize};
1211
use rustc_parse::lexer::nfc_normalize;
@@ -332,6 +331,29 @@ pub(super) fn transcribe<'a>(
332331
MatchedSingle(ParseNtResult::Pat(pat, pat_kind)) => {
333332
mk_delimited(MetaVarKind::Pat(*pat_kind), TokenStream::from_ast(pat))
334333
}
334+
MatchedSingle(ParseNtResult::Expr(expr, kind)) => {
335+
let (can_begin_literal_maybe_minus, can_begin_string_literal) =
336+
match &expr.kind {
337+
ExprKind::Lit(_) => (true, true),
338+
ExprKind::Unary(UnOp::Neg, e)
339+
if matches!(&e.kind, ExprKind::Lit(_)) =>
340+
{
341+
(true, false)
342+
}
343+
_ => (false, false),
344+
};
345+
mk_delimited(
346+
MetaVarKind::Expr {
347+
kind: *kind,
348+
can_begin_literal_maybe_minus,
349+
can_begin_string_literal,
350+
},
351+
TokenStream::from_ast(expr),
352+
)
353+
}
354+
MatchedSingle(ParseNtResult::Literal(lit)) => {
355+
mk_delimited(MetaVarKind::Literal, TokenStream::from_ast(lit))
356+
}
335357
MatchedSingle(ParseNtResult::Ty(ty)) => {
336358
mk_delimited(MetaVarKind::Ty, TokenStream::from_ast(ty))
337359
}
@@ -851,10 +873,8 @@ fn extract_symbol_from_pnr<'a>(
851873
},
852874
_,
853875
)) => Ok(*symbol),
854-
ParseNtResult::Nt(nt)
855-
if let Nonterminal::NtLiteral(expr) = &**nt
856-
&& let ExprKind::Lit(Lit { kind: LitKind::Str, symbol, suffix: None }) =
857-
&expr.kind =>
876+
ParseNtResult::Literal(expr)
877+
if let ExprKind::Lit(Lit { kind: LitKind::Str, symbol, suffix: None }) = &expr.kind =>
858878
{
859879
Ok(*symbol)
860880
}

compiler/rustc_parse/messages.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,7 @@ parse_unexpected_parentheses_in_match_arm_pattern = unexpected parentheses surro
838838
parse_unexpected_self_in_generic_parameters = unexpected keyword `Self` in generic parameters
839839
.note = you cannot use `Self` as a generic parameter because it is reserved for associated items
840840
841-
parse_unexpected_token_after_dot = unexpected token: `{$actual}`
841+
parse_unexpected_token_after_dot = unexpected token: {$actual}
842842
843843
parse_unexpected_token_after_label = expected `while`, `for`, `loop` or `{"{"}` after a label
844844
.suggestion_remove_label = consider removing the label

compiler/rustc_parse/src/errors.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1674,10 +1674,10 @@ pub(crate) struct SelfArgumentPointer {
16741674

16751675
#[derive(Diagnostic)]
16761676
#[diag(parse_unexpected_token_after_dot)]
1677-
pub(crate) struct UnexpectedTokenAfterDot<'a> {
1677+
pub(crate) struct UnexpectedTokenAfterDot {
16781678
#[primary_span]
16791679
pub span: Span,
1680-
pub actual: Cow<'a, str>,
1680+
pub actual: String,
16811681
}
16821682

16831683
#[derive(Diagnostic)]

0 commit comments

Comments
 (0)