Skip to content

Commit f48b2a1

Browse files
nnethercotecompiler-errors
authored andcommitted
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 1517eb7 commit f48b2a1

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
}
@@ -558,6 +559,9 @@ impl Token {
558559
/// for which spans affect name resolution and edition checks.
559560
/// Note that keywords are also identifiers, so they should use this
560561
/// if they keep spans or perform edition checks.
562+
//
563+
// Note: `Parser::uninterpolated_token_span` may give better information
564+
// than this method does.
561565
pub fn uninterpolated_span(&self) -> Span {
562566
match self.kind {
563567
NtIdent(ident, _) | NtLifetime(ident, _) => ident.span,
@@ -610,12 +614,7 @@ impl Token {
610614
PathSep | // global path
611615
Lifetime(..) | // labeled loop
612616
Pound => true, // expression attributes
613-
Interpolated(ref nt) =>
614-
matches!(&**nt,
615-
NtBlock(..) |
616-
NtExpr(..) |
617-
NtLiteral(..)
618-
),
617+
Interpolated(ref nt) => matches!(&**nt, NtBlock(..)),
619618
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
620619
MetaVarKind::Block |
621620
MetaVarKind::Expr { .. } |
@@ -646,11 +645,6 @@ impl Token {
646645
BinOp(Shl) => true, // path (double UFCS)
647646
// leading vert `|` or-pattern
648647
BinOp(Or) => matches!(pat_kind, PatWithOr),
649-
Interpolated(nt) =>
650-
matches!(&**nt,
651-
| NtExpr(..)
652-
| NtLiteral(..)
653-
),
654648
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
655649
MetaVarKind::Expr { .. } |
656650
MetaVarKind::Literal |
@@ -693,7 +687,7 @@ impl Token {
693687
match self.kind {
694688
OpenDelim(Delimiter::Brace) | Literal(..) | BinOp(Minus) => true,
695689
Ident(name, IdentIsRaw::No) if name.is_bool_lit() => true,
696-
Interpolated(ref nt) => matches!(&**nt, NtExpr(..) | NtBlock(..) | NtLiteral(..)),
690+
Interpolated(ref nt) => matches!(&**nt, NtBlock(..)),
697691
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
698692
MetaVarKind::Expr { .. } | MetaVarKind::Block | MetaVarKind::Literal,
699693
))) => true,
@@ -737,22 +731,12 @@ impl Token {
737731
///
738732
/// In other words, would this token be a valid start of `parse_literal_maybe_minus`?
739733
///
740-
/// Keep this in sync with and `Lit::from_token`, excluding unary negation.
734+
/// Keep this in sync with `Lit::from_token` and `Parser::eat_token_lit`
735+
/// (excluding unary negation).
741736
pub fn can_begin_literal_maybe_minus(&self) -> bool {
742737
match self.uninterpolate().kind {
743738
Literal(..) | BinOp(Minus) => true,
744739
Ident(name, IdentIsRaw::No) if name.is_bool_lit() => true,
745-
Interpolated(ref nt) => match &**nt {
746-
NtLiteral(_) => true,
747-
NtExpr(e) => match &e.kind {
748-
ast::ExprKind::Lit(_) => true,
749-
ast::ExprKind::Unary(ast::UnOp::Neg, e) => {
750-
matches!(&e.kind, ast::ExprKind::Lit(_))
751-
}
752-
_ => false,
753-
},
754-
_ => false,
755-
},
756740
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(mv_kind))) => match mv_kind {
757741
MetaVarKind::Literal => true,
758742
MetaVarKind::Expr { can_begin_literal_maybe_minus, .. } => {
@@ -767,14 +751,6 @@ impl Token {
767751
pub fn can_begin_string_literal(&self) -> bool {
768752
match self.uninterpolate().kind {
769753
Literal(..) => true,
770-
Interpolated(ref nt) => match &**nt {
771-
NtLiteral(_) => true,
772-
NtExpr(e) => match &e.kind {
773-
ast::ExprKind::Lit(_) => true,
774-
_ => false,
775-
},
776-
_ => false,
777-
},
778754
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(mv_kind))) => match mv_kind {
779755
MetaVarKind::Literal => true,
780756
MetaVarKind::Expr { can_begin_string_literal, .. } => can_begin_string_literal,
@@ -838,13 +814,16 @@ impl Token {
838814

839815
/// Is this a pre-parsed expression dropped into the token stream
840816
/// (which happens while parsing the result of macro expansion)?
841-
pub fn is_whole_expr(&self) -> bool {
817+
pub fn is_metavar_expr(&self) -> bool {
842818
#[allow(irrefutable_let_patterns)] // FIXME: temporary
843819
if let Interpolated(nt) = &self.kind
844-
&& let NtExpr(_) | NtLiteral(_) | NtBlock(_) = &**nt
820+
&& let NtBlock(_) = &**nt
845821
{
846822
true
847-
} else if matches!(self.is_metavar_seq(), Some(MetaVarKind::Path)) {
823+
} else if matches!(
824+
self.is_metavar_seq(),
825+
Some(MetaVarKind::Expr { .. } | MetaVarKind::Literal | MetaVarKind::Path)
826+
) {
848827
true
849828
} else {
850829
false
@@ -853,6 +832,7 @@ impl Token {
853832

854833
/// Is the token an interpolated block (`$b:block`)?
855834
pub fn is_whole_block(&self) -> bool {
835+
#[allow(irrefutable_let_patterns)] // FIXME: temporary
856836
if let Interpolated(nt) = &self.kind
857837
&& let NtBlock(..) = &**nt
858838
{
@@ -1046,8 +1026,6 @@ pub enum NtExprKind {
10461026
/// For interpolation during macro expansion.
10471027
pub enum Nonterminal {
10481028
NtBlock(P<ast::Block>),
1049-
NtExpr(P<ast::Expr>),
1050-
NtLiteral(P<ast::Expr>),
10511029
}
10521030

10531031
#[derive(Debug, Copy, Clone, PartialEq, Eq, Encodable, Decodable, Hash, HashStable_Generic)]
@@ -1137,15 +1115,12 @@ impl Nonterminal {
11371115
pub fn use_span(&self) -> Span {
11381116
match self {
11391117
NtBlock(block) => block.span,
1140-
NtExpr(expr) | NtLiteral(expr) => expr.span,
11411118
}
11421119
}
11431120

11441121
pub fn descr(&self) -> &'static str {
11451122
match self {
11461123
NtBlock(..) => "block",
1147-
NtExpr(..) => "expression",
1148-
NtLiteral(..) => "literal",
11491124
}
11501125
}
11511126
}
@@ -1164,8 +1139,6 @@ impl fmt::Debug for Nonterminal {
11641139
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
11651140
match *self {
11661141
NtBlock(..) => f.pad("NtBlock(..)"),
1167-
NtExpr(..) => f.pad("NtExpr(..)"),
1168-
NtLiteral(..) => f.pad("NtLiteral(..)"),
11691142
}
11701143
}
11711144
}
@@ -1188,7 +1161,7 @@ mod size_asserts {
11881161
// tidy-alphabetical-start
11891162
static_assert_size!(Lit, 12);
11901163
static_assert_size!(LitKind, 2);
1191-
static_assert_size!(Nonterminal, 16);
1164+
static_assert_size!(Nonterminal, 8);
11921165
static_assert_size!(Token, 24);
11931166
static_assert_size!(TokenKind, 16);
11941167
// 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::{pluralize, Diag, DiagCtxtHandle, PResult};
1211
use rustc_parse::lexer::nfc_normalize;
@@ -318,6 +317,29 @@ pub(super) fn transcribe<'a>(
318317
MatchedSingle(ParseNtResult::Pat(pat, pat_kind)) => {
319318
mk_delimited(MetaVarKind::Pat(*pat_kind), TokenStream::from_ast(pat))
320319
}
320+
MatchedSingle(ParseNtResult::Expr(expr, kind)) => {
321+
let (can_begin_literal_maybe_minus, can_begin_string_literal) =
322+
match &expr.kind {
323+
ExprKind::Lit(_) => (true, true),
324+
ExprKind::Unary(UnOp::Neg, e)
325+
if matches!(&e.kind, ExprKind::Lit(_)) =>
326+
{
327+
(true, false)
328+
}
329+
_ => (false, false),
330+
};
331+
mk_delimited(
332+
MetaVarKind::Expr {
333+
kind: *kind,
334+
can_begin_literal_maybe_minus,
335+
can_begin_string_literal,
336+
},
337+
TokenStream::from_ast(expr),
338+
)
339+
}
340+
MatchedSingle(ParseNtResult::Literal(lit)) => {
341+
mk_delimited(MetaVarKind::Literal, TokenStream::from_ast(lit))
342+
}
321343
MatchedSingle(ParseNtResult::Ty(ty)) => {
322344
mk_delimited(MetaVarKind::Ty, TokenStream::from_ast(ty))
323345
}
@@ -837,10 +859,8 @@ fn extract_symbol_from_pnr<'a>(
837859
},
838860
_,
839861
)) => Ok(*symbol),
840-
ParseNtResult::Nt(nt)
841-
if let Nonterminal::NtLiteral(expr) = &**nt
842-
&& let ExprKind::Lit(Lit { kind: LitKind::Str, symbol, suffix: None }) =
843-
&expr.kind =>
862+
ParseNtResult::Literal(expr)
863+
if let ExprKind::Lit(Lit { kind: LitKind::Str, symbol, suffix: None }) = &expr.kind =>
844864
{
845865
Ok(*symbol)
846866
}

compiler/rustc_parse/messages.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,7 @@ parse_unexpected_parentheses_in_match_arm_pattern = unexpected parentheses surro
836836
parse_unexpected_self_in_generic_parameters = unexpected keyword `Self` in generic parameters
837837
.note = you cannot use `Self` as a generic parameter because it is reserved for associated items
838838
839-
parse_unexpected_token_after_dot = unexpected token: `{$actual}`
839+
parse_unexpected_token_after_dot = unexpected token: {$actual}
840840
841841
parse_unexpected_token_after_label = expected `while`, `for`, `loop` or `{"{"}` after a label
842842
.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
@@ -1676,10 +1676,10 @@ pub(crate) struct SelfArgumentPointer {
16761676

16771677
#[derive(Diagnostic)]
16781678
#[diag(parse_unexpected_token_after_dot)]
1679-
pub(crate) struct UnexpectedTokenAfterDot<'a> {
1679+
pub(crate) struct UnexpectedTokenAfterDot {
16801680
#[primary_span]
16811681
pub span: Span,
1682-
pub actual: Cow<'a, str>,
1682+
pub actual: String,
16831683
}
16841684

16851685
#[derive(Diagnostic)]

0 commit comments

Comments
 (0)