Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 7413663

Browse files
committed
XXX: NtExpr/NtLiteral
Notes about tests: - tests/ui/macros/stringify.rs: the `c2` macro is no longer needed, because the TokenStream pretty printer is now used for all cases. - 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. XXX: Getting a test failure here: ``` Building tool error_index_generator (stage1 -> stage2, x86_64-unknown-linux-gnu) Compiling cfg-if v1.0.0 ... Compiling mdbook v0.4.37 error: internal compiler error: the following error was constructed but not emitted error: unexpected token: keyword `self` --> /home/njn/.cargo/registry/src/index.crates.io-6f17d22bba15001f/mdbook-0.4.37/src/book/summary.rs:280:31 | 280 | ... bail!(self.parse_error("Suffix chapters cannot be followed by a list")); | ^^^^ thread 'rustc' panicked at /home/njn/dev/rust3/compiler/rustc_errors/src/diagnostic.rs:1375:17: error was constructed but not emitted ``` I get a similar compile error (not ICE) in a vanilla compiler if I change `can_begin_maybe_minus` to accept `NtExpr` without checking that `e` is a `Lit` or `Unary` error: format argument must be a string literal --> /home/njn/.cargo/registry/src/index.crates.io-6f17d22bba15001f/mdbook-0.4.37/src/book/summary.rs:280:31 | 280 | ... bail!(self.parse_error("Suffix chapters cannot be followed by a list")); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | help: you might be missing a string literal to format with | 280 | bail!("{}", self.parse_error("Suffix chapters cannot be followed by a list")); | +++++
1 parent 56d9dbf commit 7413663

33 files changed

+667
-532
lines changed

compiler/rustc_ast/src/ast_traits.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,13 +231,11 @@ impl HasTokens for Attribute {
231231
impl HasTokens for Nonterminal {
232232
fn tokens(&self) -> Option<&LazyAttrTokenStream> {
233233
match self {
234-
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => expr.tokens(),
235234
Nonterminal::NtBlock(block) => block.tokens(),
236235
}
237236
}
238237
fn tokens_mut(&mut self) -> Option<&mut Option<LazyAttrTokenStream>> {
239238
match self {
240-
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => expr.tokens_mut(),
241239
Nonterminal::NtBlock(block) => block.tokens_mut(),
242240
}
243241
}

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -820,8 +820,6 @@ pub fn visit_token<T: MutVisitor>(t: &mut Token, vis: &mut T) {
820820
fn visit_nonterminal<T: MutVisitor>(nt: &mut token::Nonterminal, vis: &mut T) {
821821
match nt {
822822
token::NtBlock(block) => vis.visit_block(block),
823-
token::NtExpr(expr) => vis.visit_expr(expr),
824-
token::NtLiteral(expr) => vis.visit_expr(expr),
825823
}
826824
}
827825

compiler/rustc_ast/src/token.rs

Lines changed: 58 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -136,16 +136,27 @@ impl Lit {
136136
}
137137
}
138138

139-
/// Keep this in sync with `Token::can_begin_literal_maybe_minus` excluding unary negation.
139+
/// Keep this in sync with `Token::can_begin_literal_maybe_minus` and
140+
/// `Parser::maybe_parse_token_lit` (excluding unary negation).
140141
pub fn from_token(token: &Token) -> Option<Lit> {
141142
match token.uninterpolate().kind {
142143
Ident(name, IdentIsRaw::No) if name.is_bool_lit() => Some(Lit::new(Bool, name, None)),
143144
Literal(token_lit) => Some(token_lit),
144-
Interpolated(ref nt)
145-
if let NtExpr(expr) | NtLiteral(expr) = &**nt
146-
&& let ast::ExprKind::Lit(token_lit) = expr.kind =>
147-
{
148-
Some(token_lit)
145+
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(NonterminalKind::Literal))) => {
146+
panic!("njn: FROM_TOKEN (1)");
147+
// if let NtExpr(expr) | NtLiteral(expr) = &**nt
148+
// && let ast::ExprKind::Lit(token_lit) = expr.kind =>
149+
// {
150+
// Some(token_lit)
151+
// }
152+
}
153+
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(NonterminalKind::Expr(_)))) => {
154+
panic!("njn: FROM_TOKEN (2)");
155+
// if let NtExpr(expr) | NtLiteral(expr) = &**nt
156+
// && let ast::ExprKind::Lit(token_lit) = expr.kind =>
157+
// {
158+
// Some(token_lit)
159+
// }
149160
}
150161
_ => None,
151162
}
@@ -478,6 +489,7 @@ impl Token {
478489
Token::new(Ident(ident.name, ident.is_raw_guess().into()), ident.span)
479490
}
480491

492+
/// njn: phase this out in favour of Parser::uninterpolated_token_span?
481493
/// For interpolated tokens, returns a span of the fragment to which the interpolated
482494
/// token refers. For all other tokens this is just a regular span.
483495
/// It is particularly important to use this for identifiers and lifetimes
@@ -533,9 +545,7 @@ impl Token {
533545
PathSep | // global path
534546
Lifetime(..) | // labeled loop
535547
Pound => true, // expression attributes
536-
Interpolated(ref nt) => matches!(&**nt, NtLiteral(..) |
537-
NtExpr(..) |
538-
NtBlock(..)),
548+
Interpolated(ref nt) => matches!(&**nt, NtBlock(..)),
539549
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
540550
NonterminalKind::Block |
541551
NonterminalKind::Expr(_) |
@@ -562,7 +572,7 @@ impl Token {
562572
| DotDot | DotDotDot | DotDotEq // ranges
563573
| Lt | BinOp(Shl) // associated path
564574
| PathSep => true, // global path
565-
Interpolated(ref nt) => matches!(&**nt, NtLiteral(..) | NtBlock(..)),
575+
Interpolated(ref nt) => matches!(&**nt, NtBlock(..)),
566576
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
567577
NonterminalKind::Block |
568578
NonterminalKind::Pat(_) |
@@ -603,7 +613,7 @@ impl Token {
603613
match self.kind {
604614
OpenDelim(Delimiter::Brace) | Literal(..) | BinOp(Minus) => true,
605615
Ident(name, IdentIsRaw::No) if name.is_bool_lit() => true,
606-
Interpolated(ref nt) => matches!(&**nt, NtExpr(..) | NtBlock(..) | NtLiteral(..)),
616+
Interpolated(ref nt) => matches!(&**nt, NtBlock(..)),
607617
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
608618
NonterminalKind::Expr(_) | NonterminalKind::Block | NonterminalKind::Literal,
609619
))) => true,
@@ -647,22 +657,24 @@ impl Token {
647657
///
648658
/// In other words, would this token be a valid start of `parse_literal_maybe_minus`?
649659
///
650-
/// Keep this in sync with and `Lit::from_token`, excluding unary negation.
660+
/// Keep this in sync with `Lit::from_token` and
661+
/// `Parser::maybe_parse_token_lit` (excluding unary negation).
651662
pub fn can_begin_literal_maybe_minus(&self) -> bool {
652663
match self.uninterpolate().kind {
653664
Literal(..) | BinOp(Minus) => true,
654665
Ident(name, IdentIsRaw::No) if name.is_bool_lit() => true,
655-
Interpolated(ref nt) => match &**nt {
656-
NtLiteral(_) => true,
657-
NtExpr(e) => match &e.kind {
658-
ast::ExprKind::Lit(_) => true,
659-
ast::ExprKind::Unary(ast::UnOp::Neg, e) => {
660-
matches!(&e.kind, ast::ExprKind::Lit(_))
661-
}
662-
_ => false,
663-
},
664-
_ => false,
665-
},
666+
// njn: fix up
667+
// Interpolated(ref nt) => match &**nt {
668+
// NtLiteral(_) => true,
669+
// NtExpr(e) => match &e.kind {
670+
// ast::ExprKind::Lit(_) => true,
671+
// ast::ExprKind::Unary(ast::UnOp::Neg, e) => {
672+
// matches!(&e.kind, ast::ExprKind::Lit(_))
673+
// }
674+
// _ => false,
675+
// },
676+
// _ => false,
677+
// },
666678
// njn: too simple compared to what's above?
667679
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
668680
NonterminalKind::Literal | NonterminalKind::Expr(_),
@@ -674,14 +686,19 @@ impl Token {
674686
pub fn can_begin_string_literal(&self) -> bool {
675687
match self.uninterpolate().kind {
676688
Literal(..) => true,
677-
Interpolated(ref nt) => match &**nt {
678-
NtLiteral(_) => true,
679-
NtExpr(e) => match &e.kind {
680-
ast::ExprKind::Lit(_) => true,
681-
_ => false,
682-
},
683-
_ => false,
684-
},
689+
// njn: fix up
690+
// Interpolated(ref nt) => match &**nt {
691+
// NtLiteral(_) => true,
692+
// NtExpr(e) => match &e.kind {
693+
// ast::ExprKind::Lit(_) => true,
694+
// _ => false,
695+
// },
696+
// _ => false,
697+
// },
698+
// njn: too simple compared to what's above?
699+
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
700+
NonterminalKind::Literal | NonterminalKind::Expr(_),
701+
))) => true,
685702
_ => false,
686703
}
687704
}
@@ -736,16 +753,19 @@ impl Token {
736753
self.ident().is_some_and(|(ident, _)| ident.name == name)
737754
}
738755

739-
/// Would `maybe_whole_expr` in `parser.rs` return `Ok(..)`?
756+
/// Would `maybe_reparse_metavar_expr` in `parser.rs` return `Ok(..)`?
740757
/// That is, is this a pre-parsed expression dropped into the token stream
741758
/// (which happens while parsing the result of macro expansion)?
742-
pub fn is_whole_expr(&self) -> bool {
743-
#[allow(irrefutable_let_patterns)] // njn: temp
759+
pub fn is_metavar_expr(&self) -> bool {
760+
#[allow(irrefutable_let_patterns)] // FIXME: temporary
744761
if let Interpolated(nt) = &self.kind
745-
&& let NtExpr(_) | NtLiteral(_) = &**nt
762+
&& let NtBlock(_) = &**nt
746763
{
747764
true
748-
} else if matches!(self.is_metavar_seq(), Some(NonterminalKind::Path)) {
765+
} else if matches!(
766+
self.is_metavar_seq(),
767+
Some(NonterminalKind::Expr(_) | NonterminalKind::Literal | NonterminalKind::Path)
768+
) {
749769
true
750770
} else {
751771
false
@@ -754,6 +774,7 @@ impl Token {
754774

755775
/// Is the token an interpolated block (`$b:block`)?
756776
pub fn is_whole_block(&self) -> bool {
777+
#[allow(irrefutable_let_patterns)] // FIXME: temporary
757778
if let Interpolated(nt) = &self.kind
758779
&& let NtBlock(..) = &**nt
759780
{
@@ -947,8 +968,6 @@ pub enum NtExprKind {
947968
/// For interpolation during macro expansion.
948969
pub enum Nonterminal {
949970
NtBlock(P<ast::Block>),
950-
NtExpr(P<ast::Expr>),
951-
NtLiteral(P<ast::Expr>),
952971
}
953972

954973
#[derive(Debug, Copy, Clone, PartialEq, Eq, Encodable, Decodable, Hash, HashStable_Generic)]
@@ -1038,15 +1057,12 @@ impl Nonterminal {
10381057
pub fn use_span(&self) -> Span {
10391058
match self {
10401059
NtBlock(block) => block.span,
1041-
NtExpr(expr) | NtLiteral(expr) => expr.span,
10421060
}
10431061
}
10441062

10451063
pub fn descr(&self) -> &'static str {
10461064
match self {
10471065
NtBlock(..) => "block",
1048-
NtExpr(..) => "expression",
1049-
NtLiteral(..) => "literal",
10501066
}
10511067
}
10521068
}
@@ -1065,8 +1081,6 @@ impl fmt::Debug for Nonterminal {
10651081
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
10661082
match *self {
10671083
NtBlock(..) => f.pad("NtBlock(..)"),
1068-
NtExpr(..) => f.pad("NtExpr(..)"),
1069-
NtLiteral(..) => f.pad("NtLiteral(..)"),
10701084
}
10711085
}
10721086
}
@@ -1088,7 +1102,7 @@ mod size_asserts {
10881102
// tidy-alphabetical-start
10891103
static_assert_size!(Lit, 12);
10901104
static_assert_size!(LitKind, 2);
1091-
static_assert_size!(Nonterminal, 16);
1105+
static_assert_size!(Nonterminal, 8);
10921106
static_assert_size!(Token, 24);
10931107
static_assert_size!(TokenKind, 16);
10941108
// tidy-alphabetical-end

compiler/rustc_ast/src/tokenstream.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,6 @@ impl TokenStream {
467467
pub fn from_nonterminal_ast(nt: &Nonterminal) -> TokenStream {
468468
match nt {
469469
Nonterminal::NtBlock(block) => TokenStream::from_ast(block),
470-
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => TokenStream::from_ast(expr),
471470
}
472471
}
473472

compiler/rustc_expand/src/mbe/transcribe.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,13 @@ pub(super) fn transcribe<'a>(
313313
NonterminalKind::Pat(*pat_kind),
314314
TokenStream::from_ast(pat),
315315
),
316+
MatchedSingle(ParseNtResult::Expr(expr, expr_kind)) => mk_delimited(
317+
NonterminalKind::Expr(*expr_kind),
318+
TokenStream::from_ast(expr),
319+
),
320+
MatchedSingle(ParseNtResult::Literal(ref expr)) => {
321+
mk_delimited(NonterminalKind::Literal, TokenStream::from_ast(expr))
322+
}
316323
MatchedSingle(ParseNtResult::Ty(ref ty)) => {
317324
mk_delimited(NonterminalKind::Ty, TokenStream::from_ast(ty))
318325
}

compiler/rustc_index/src/bit_set/tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ fn chunked_bitset() {
302302
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
303303
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x8000_0000_0000_0000
304304
])),
305-
],
305+
], // njn: trailing comma here causes a crash in reparse_metavar_seq?
306306
);
307307
assert_eq!(b4096.count(), 2);
308308
b4096.assert_valid();
@@ -336,7 +336,7 @@ fn chunked_bitset() {
336336
])),
337337
Zeros(2048),
338338
Zeros(1808),
339-
],
339+
], // njn: trailing comma here causes a crash in reparse_metavar_seq?
340340
);
341341
let mut b10000b = ChunkedBitSet::<usize>::new_empty(10000);
342342
b10000b.clone_from(&b10000);

compiler/rustc_parse/messages.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,7 @@ parse_unexpected_parentheses_in_match_arm_pattern = unexpected parentheses surro
807807
parse_unexpected_self_in_generic_parameters = unexpected keyword `Self` in generic parameters
808808
.note = you cannot use `Self` as a generic parameter because it is reserved for associated items
809809
810-
parse_unexpected_token_after_dot = unexpected token: `{$actual}`
810+
parse_unexpected_token_after_dot = unexpected token: {$actual}
811811
812812
parse_unexpected_token_after_label = expected `while`, `for`, `loop` or `{"{"}` after a label
813813
.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
@@ -1602,10 +1602,10 @@ pub(crate) struct SelfArgumentPointer {
16021602

16031603
#[derive(Diagnostic)]
16041604
#[diag(parse_unexpected_token_after_dot)]
1605-
pub struct UnexpectedTokenAfterDot<'a> {
1605+
pub struct UnexpectedTokenAfterDot {
16061606
#[primary_span]
16071607
pub span: Span,
1608-
pub actual: Cow<'a, str>,
1608+
pub actual: String,
16091609
}
16101610

16111611
#[derive(Diagnostic)]

0 commit comments

Comments
 (0)