Skip to content

Commit 8b1d161

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 2ecc947 commit 8b1d161

29 files changed

+731
-551
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
@@ -893,8 +893,6 @@ pub fn visit_token<T: MutVisitor>(vis: &mut T, t: &mut Token) {
893893
fn visit_nonterminal<T: MutVisitor>(vis: &mut T, nt: &mut token::Nonterminal) {
894894
match nt {
895895
token::NtBlock(block) => vis.visit_block(block),
896-
token::NtExpr(expr) => vis.visit_expr(expr),
897-
token::NtLiteral(expr) => vis.visit_expr(expr),
898896
}
899897
}
900898

compiler/rustc_ast/src/token.rs

Lines changed: 22 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -214,16 +214,17 @@ impl Lit {
214214
}
215215
}
216216

217-
/// Keep this in sync with `Token::can_begin_literal_maybe_minus` excluding unary negation.
217+
/// Keep this in sync with `Token::can_begin_literal_maybe_minus` and
218+
/// `Parser::eat_token_lit` (excluding unary negation).
218219
pub fn from_token(token: &Token) -> Option<Lit> {
219220
match token.uninterpolate().kind {
220221
Ident(name, IdentIsRaw::No) if name.is_bool_lit() => Some(Lit::new(Bool, name, None)),
221222
Literal(token_lit) => Some(token_lit),
222-
Interpolated(ref nt)
223-
if let NtExpr(expr) | NtLiteral(expr) = &**nt
224-
&& let ast::ExprKind::Lit(token_lit) = expr.kind =>
225-
{
226-
Some(token_lit)
223+
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
224+
MetaVarKind::Literal | MetaVarKind::Expr { .. },
225+
))) => {
226+
// Unreachable with the current test suite.
227+
panic!("from_token metavar");
227228
}
228229
_ => None,
229230
}
@@ -568,6 +569,9 @@ impl Token {
568569
/// for which spans affect name resolution and edition checks.
569570
/// Note that keywords are also identifiers, so they should use this
570571
/// if they keep spans or perform edition checks.
572+
//
573+
// Note: `Parser::uninterpolated_token_span` may give better information
574+
// than this method does.
571575
pub fn uninterpolated_span(&self) -> Span {
572576
match self.kind {
573577
NtIdent(ident, _) | NtLifetime(ident, _) => ident.span,
@@ -620,12 +624,7 @@ impl Token {
620624
PathSep | // global path
621625
Lifetime(..) | // labeled loop
622626
Pound => true, // expression attributes
623-
Interpolated(ref nt) =>
624-
matches!(&**nt,
625-
NtBlock(..) |
626-
NtExpr(..) |
627-
NtLiteral(..)
628-
),
627+
Interpolated(ref nt) => matches!(&**nt, NtBlock(..)),
629628
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
630629
MetaVarKind::Block |
631630
MetaVarKind::Expr { .. } |
@@ -656,11 +655,6 @@ impl Token {
656655
BinOp(Shl) => true, // path (double UFCS)
657656
// leading vert `|` or-pattern
658657
BinOp(Or) => matches!(pat_kind, PatWithOr),
659-
Interpolated(nt) =>
660-
matches!(&**nt,
661-
| NtExpr(..)
662-
| NtLiteral(..)
663-
),
664658
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
665659
MetaVarKind::Expr { .. } |
666660
MetaVarKind::Literal |
@@ -703,7 +697,7 @@ impl Token {
703697
match self.kind {
704698
OpenDelim(Delimiter::Brace) | Literal(..) | BinOp(Minus) => true,
705699
Ident(name, IdentIsRaw::No) if name.is_bool_lit() => true,
706-
Interpolated(ref nt) => matches!(&**nt, NtExpr(..) | NtBlock(..) | NtLiteral(..)),
700+
Interpolated(ref nt) => matches!(&**nt, NtBlock(..)),
707701
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
708702
MetaVarKind::Expr { .. } | MetaVarKind::Block | MetaVarKind::Literal,
709703
))) => true,
@@ -747,22 +741,12 @@ impl Token {
747741
///
748742
/// In other words, would this token be a valid start of `parse_literal_maybe_minus`?
749743
///
750-
/// Keep this in sync with and `Lit::from_token`, excluding unary negation.
744+
/// Keep this in sync with `Lit::from_token` and `Parser::eat_token_lit`
745+
/// (excluding unary negation).
751746
pub fn can_begin_literal_maybe_minus(&self) -> bool {
752747
match self.uninterpolate().kind {
753748
Literal(..) | BinOp(Minus) => true,
754749
Ident(name, IdentIsRaw::No) if name.is_bool_lit() => true,
755-
Interpolated(ref nt) => match &**nt {
756-
NtLiteral(_) => true,
757-
NtExpr(e) => match &e.kind {
758-
ast::ExprKind::Lit(_) => true,
759-
ast::ExprKind::Unary(ast::UnOp::Neg, e) => {
760-
matches!(&e.kind, ast::ExprKind::Lit(_))
761-
}
762-
_ => false,
763-
},
764-
_ => false,
765-
},
766750
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(mv_kind))) => match mv_kind {
767751
MetaVarKind::Literal => true,
768752
MetaVarKind::Expr { can_begin_literal_maybe_minus, .. } => {
@@ -777,14 +761,6 @@ impl Token {
777761
pub fn can_begin_string_literal(&self) -> bool {
778762
match self.uninterpolate().kind {
779763
Literal(..) => true,
780-
Interpolated(ref nt) => match &**nt {
781-
NtLiteral(_) => true,
782-
NtExpr(e) => match &e.kind {
783-
ast::ExprKind::Lit(_) => true,
784-
_ => false,
785-
},
786-
_ => false,
787-
},
788764
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(mv_kind))) => match mv_kind {
789765
MetaVarKind::Literal => true,
790766
MetaVarKind::Expr { can_begin_string_literal, .. } => can_begin_string_literal,
@@ -848,13 +824,16 @@ impl Token {
848824

849825
/// Is this a pre-parsed expression dropped into the token stream
850826
/// (which happens while parsing the result of macro expansion)?
851-
pub fn is_whole_expr(&self) -> bool {
827+
pub fn is_metavar_expr(&self) -> bool {
852828
#[allow(irrefutable_let_patterns)] // FIXME: temporary
853829
if let Interpolated(nt) = &self.kind
854-
&& let NtExpr(_) | NtLiteral(_) | NtBlock(_) = &**nt
830+
&& let NtBlock(_) = &**nt
855831
{
856832
true
857-
} else if matches!(self.is_metavar_seq(), Some(MetaVarKind::Path)) {
833+
} else if matches!(
834+
self.is_metavar_seq(),
835+
Some(MetaVarKind::Expr { .. } | MetaVarKind::Literal | MetaVarKind::Path)
836+
) {
858837
true
859838
} else {
860839
false
@@ -863,6 +842,7 @@ impl Token {
863842

864843
/// Is the token an interpolated block (`$b:block`)?
865844
pub fn is_whole_block(&self) -> bool {
845+
#[allow(irrefutable_let_patterns)] // FIXME: temporary
866846
if let Interpolated(nt) = &self.kind
867847
&& let NtBlock(..) = &**nt
868848
{
@@ -1063,8 +1043,6 @@ pub enum NtExprKind {
10631043
/// For interpolation during macro expansion.
10641044
pub enum Nonterminal {
10651045
NtBlock(P<ast::Block>),
1066-
NtExpr(P<ast::Expr>),
1067-
NtLiteral(P<ast::Expr>),
10681046
}
10691047

10701048
#[derive(Debug, Copy, Clone, PartialEq, Eq, Encodable, Decodable, Hash, HashStable_Generic)]
@@ -1154,15 +1132,12 @@ impl Nonterminal {
11541132
pub fn use_span(&self) -> Span {
11551133
match self {
11561134
NtBlock(block) => block.span,
1157-
NtExpr(expr) | NtLiteral(expr) => expr.span,
11581135
}
11591136
}
11601137

11611138
pub fn descr(&self) -> &'static str {
11621139
match self {
11631140
NtBlock(..) => "block",
1164-
NtExpr(..) => "expression",
1165-
NtLiteral(..) => "literal",
11661141
}
11671142
}
11681143
}
@@ -1181,8 +1156,6 @@ impl fmt::Debug for Nonterminal {
11811156
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
11821157
match *self {
11831158
NtBlock(..) => f.pad("NtBlock(..)"),
1184-
NtExpr(..) => f.pad("NtExpr(..)"),
1185-
NtLiteral(..) => f.pad("NtLiteral(..)"),
11861159
}
11871160
}
11881161
}
@@ -1205,7 +1178,7 @@ mod size_asserts {
12051178
// tidy-alphabetical-start
12061179
static_assert_size!(Lit, 12);
12071180
static_assert_size!(LitKind, 2);
1208-
static_assert_size!(Nonterminal, 16);
1181+
static_assert_size!(Nonterminal, 8);
12091182
static_assert_size!(Token, 24);
12101183
static_assert_size!(TokenKind, 16);
12111184
// 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: 28 additions & 7 deletions
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,7 @@ parse_unexpected_parentheses_in_match_arm_pattern = unexpected parentheses surro
856856
parse_unexpected_self_in_generic_parameters = unexpected keyword `Self` in generic parameters
857857
.note = you cannot use `Self` as a generic parameter because it is reserved for associated items
858858
859-
parse_unexpected_token_after_dot = unexpected token: `{$actual}`
859+
parse_unexpected_token_after_dot = unexpected token: {$actual}
860860
861861
parse_unexpected_token_after_label = expected `while`, `for`, `loop` or `{"{"}` after a label
862862
.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
@@ -1687,10 +1687,10 @@ pub(crate) struct SelfArgumentPointer {
16871687

16881688
#[derive(Diagnostic)]
16891689
#[diag(parse_unexpected_token_after_dot)]
1690-
pub(crate) struct UnexpectedTokenAfterDot<'a> {
1690+
pub(crate) struct UnexpectedTokenAfterDot {
16911691
#[primary_span]
16921692
pub span: Span,
1693-
pub actual: Cow<'a, str>,
1693+
pub actual: String,
16941694
}
16951695

16961696
#[derive(Diagnostic)]

0 commit comments

Comments
 (0)