Skip to content

Commit df017a6

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 010b8ec commit df017a6

37 files changed

+682
-573
lines changed

compiler/rustc_ast/src/ast_traits.rs

-2
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

-2
Original file line numberDiff line numberDiff line change
@@ -823,8 +823,6 @@ pub fn visit_token<T: MutVisitor>(t: &mut Token, vis: &mut T) {
823823
fn visit_nonterminal<T: MutVisitor>(nt: &mut token::Nonterminal, vis: &mut T) {
824824
match nt {
825825
token::NtBlock(block) => vis.visit_block(block),
826-
token::NtExpr(expr) => vis.visit_expr(expr),
827-
token::NtLiteral(expr) => vis.visit_expr(expr),
828826
}
829827
}
830828

compiler/rustc_ast/src/token.rs

+45-36
Original file line numberDiff line numberDiff line change
@@ -134,16 +134,27 @@ impl Lit {
134134
}
135135
}
136136

137-
/// Keep this in sync with `Token::can_begin_literal_maybe_minus` excluding unary negation.
137+
/// Keep this in sync with `Token::can_begin_literal_maybe_minus` and
138+
/// `Parser::maybe_parse_token_lit` (excluding unary negation).
138139
pub fn from_token(token: &Token) -> Option<Lit> {
139140
match token.uninterpolate().kind {
140141
Ident(name, IdentIsRaw::No) if name.is_bool_lit() => Some(Lit::new(Bool, name, None)),
141142
Literal(token_lit) => Some(token_lit),
142-
Interpolated(ref nt)
143-
if let NtExpr(expr) | NtLiteral(expr) = &**nt
144-
&& let ast::ExprKind::Lit(token_lit) = expr.kind =>
145-
{
146-
Some(token_lit)
143+
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(NonterminalKind::Literal))) => {
144+
panic!("njn: FROM_TOKEN (1)");
145+
// if let NtExpr(expr) | NtLiteral(expr) = &**nt
146+
// && let ast::ExprKind::Lit(token_lit) = expr.kind =>
147+
// {
148+
// Some(token_lit)
149+
// }
150+
}
151+
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(NonterminalKind::Expr))) => {
152+
panic!("njn: FROM_TOKEN (2)");
153+
// if let NtExpr(expr) | NtLiteral(expr) = &**nt
154+
// && let ast::ExprKind::Lit(token_lit) = expr.kind =>
155+
// {
156+
// Some(token_lit)
157+
// }
147158
}
148159
_ => None,
149160
}
@@ -475,6 +486,7 @@ impl Token {
475486
Token::new(Ident(ident.name, ident.is_raw_guess().into()), ident.span)
476487
}
477488

489+
/// njn: phase this out in favour of Parser::uninterpolated_token_span?
478490
/// For interpolated tokens, returns a span of the fragment to which the interpolated
479491
/// token refers. For all other tokens this is just a regular span.
480492
/// It is particularly important to use this for identifiers and lifetimes
@@ -530,9 +542,7 @@ impl Token {
530542
PathSep | // global path
531543
Lifetime(..) | // labeled loop
532544
Pound => true, // expression attributes
533-
Interpolated(ref nt) => matches!(&**nt, NtLiteral(..) |
534-
NtExpr(..) |
535-
NtBlock(..)),
545+
Interpolated(ref nt) => matches!(&**nt, NtBlock(..)),
536546
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
537547
NonterminalKind::Block |
538548
NonterminalKind::Expr |
@@ -559,7 +569,7 @@ impl Token {
559569
| DotDot | DotDotDot | DotDotEq // ranges
560570
| Lt | BinOp(Shl) // associated path
561571
| PathSep => true, // global path
562-
Interpolated(ref nt) => matches!(&**nt, NtLiteral(..) | NtBlock(..)),
572+
Interpolated(ref nt) => matches!(&**nt, NtBlock(..)),
563573
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
564574
NonterminalKind::Block |
565575
NonterminalKind::PatParam { .. } |
@@ -600,7 +610,7 @@ impl Token {
600610
pub fn can_begin_const_arg(&self) -> bool {
601611
match self.kind {
602612
OpenDelim(Delimiter::Brace) => true,
603-
Interpolated(ref nt) => matches!(&**nt, NtExpr(..) | NtBlock(..) | NtLiteral(..)),
613+
Interpolated(ref nt) => matches!(&**nt, NtBlock(..)),
604614
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
605615
NonterminalKind::Expr | NonterminalKind::Block | NonterminalKind::Literal,
606616
))) => true,
@@ -643,22 +653,24 @@ impl Token {
643653
///
644654
/// In other words, would this token be a valid start of `parse_literal_maybe_minus`?
645655
///
646-
/// Keep this in sync with and `Lit::from_token`, excluding unary negation.
656+
/// Keep this in sync with `Lit::from_token` and
657+
/// `Parser::maybe_parse_token_lit` (excluding unary negation).
647658
pub fn can_begin_literal_maybe_minus(&self) -> bool {
648659
match self.uninterpolate().kind {
649660
Literal(..) | BinOp(Minus) => true,
650661
Ident(name, IdentIsRaw::No) if name.is_bool_lit() => true,
651-
Interpolated(ref nt) => match &**nt {
652-
NtLiteral(_) => true,
653-
NtExpr(e) => match &e.kind {
654-
ast::ExprKind::Lit(_) => true,
655-
ast::ExprKind::Unary(ast::UnOp::Neg, e) => {
656-
matches!(&e.kind, ast::ExprKind::Lit(_))
657-
}
658-
_ => false,
659-
},
660-
_ => false,
661-
},
662+
// njn: fix up
663+
// Interpolated(ref nt) => match &**nt {
664+
// NtLiteral(_) => true,
665+
// NtExpr(e) => match &e.kind {
666+
// ast::ExprKind::Lit(_) => true,
667+
// ast::ExprKind::Unary(ast::UnOp::Neg, e) => {
668+
// matches!(&e.kind, ast::ExprKind::Lit(_))
669+
// }
670+
// _ => false,
671+
// },
672+
// _ => false,
673+
// },
662674
// njn: too simple compared to what's above?
663675
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
664676
NonterminalKind::Literal | NonterminalKind::Expr,
@@ -717,16 +729,19 @@ impl Token {
717729
self.ident().is_some_and(|(ident, _)| ident.name == name)
718730
}
719731

720-
/// Would `maybe_whole_expr` in `parser.rs` return `Ok(..)`?
732+
/// Would `maybe_reparse_metavar_expr` in `parser.rs` return `Ok(..)`?
721733
/// That is, is this a pre-parsed expression dropped into the token stream
722734
/// (which happens while parsing the result of macro expansion)?
723-
pub fn is_whole_expr(&self) -> bool {
724-
#[allow(irrefutable_let_patterns)] // njn: temp
735+
pub fn is_metavar_expr(&self) -> bool {
736+
#[allow(irrefutable_let_patterns)] // FIXME: temporary
725737
if let Interpolated(nt) = &self.kind
726-
&& let NtExpr(_) | NtLiteral(_) = &**nt
738+
&& let NtBlock(_) = &**nt
727739
{
728740
true
729-
} else if matches!(self.is_metavar_seq(), Some(NonterminalKind::Path)) {
741+
} else if matches!(
742+
self.is_metavar_seq(),
743+
Some(NonterminalKind::Expr | NonterminalKind::Literal | NonterminalKind::Path)
744+
) {
730745
true
731746
} else {
732747
false
@@ -735,6 +750,7 @@ impl Token {
735750

736751
/// Is the token an interpolated block (`$b:block`)?
737752
pub fn is_whole_block(&self) -> bool {
753+
#[allow(irrefutable_let_patterns)] // FIXME: temporary
738754
if let Interpolated(nt) = &self.kind
739755
&& let NtBlock(..) = &**nt
740756
{
@@ -907,8 +923,6 @@ impl PartialEq<TokenKind> for Token {
907923
/// For interpolation during macro expansion.
908924
pub enum Nonterminal {
909925
NtBlock(P<ast::Block>),
910-
NtExpr(P<ast::Expr>),
911-
NtLiteral(P<ast::Expr>),
912926
}
913927

914928
#[derive(Debug, Copy, Clone, PartialEq, Eq, Encodable, Decodable, Hash, HashStable_Generic)]
@@ -993,15 +1007,12 @@ impl Nonterminal {
9931007
pub fn use_span(&self) -> Span {
9941008
match self {
9951009
NtBlock(block) => block.span,
996-
NtExpr(expr) | NtLiteral(expr) => expr.span,
9971010
}
9981011
}
9991012

10001013
pub fn descr(&self) -> &'static str {
10011014
match self {
10021015
NtBlock(..) => "block",
1003-
NtExpr(..) => "expression",
1004-
NtLiteral(..) => "literal",
10051016
}
10061017
}
10071018
}
@@ -1020,8 +1031,6 @@ impl fmt::Debug for Nonterminal {
10201031
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
10211032
match *self {
10221033
NtBlock(..) => f.pad("NtBlock(..)"),
1023-
NtExpr(..) => f.pad("NtExpr(..)"),
1024-
NtLiteral(..) => f.pad("NtLiteral(..)"),
10251034
}
10261035
}
10271036
}
@@ -1043,7 +1052,7 @@ mod size_asserts {
10431052
// tidy-alphabetical-start
10441053
static_assert_size!(Lit, 12);
10451054
static_assert_size!(LitKind, 2);
1046-
static_assert_size!(Nonterminal, 16);
1055+
static_assert_size!(Nonterminal, 8);
10471056
static_assert_size!(Token, 24);
10481057
static_assert_size!(TokenKind, 16);
10491058
// tidy-alphabetical-end

compiler/rustc_ast/src/tokenstream.rs

-1
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,6 @@ impl TokenStream {
477477
pub fn from_nonterminal_ast(nt: &Nonterminal) -> TokenStream {
478478
match nt {
479479
Nonterminal::NtBlock(block) => TokenStream::from_ast(block),
480-
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => TokenStream::from_ast(expr),
481480
}
482481
}
483482

compiler/rustc_ast_pretty/src/pprust/state.rs

-2
Original file line numberDiff line numberDiff line change
@@ -853,9 +853,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
853853

854854
fn nonterminal_to_string(&self, nt: &Nonterminal) -> String {
855855
match nt {
856-
token::NtExpr(e) => self.expr_to_string(e),
857856
token::NtBlock(e) => self.block_to_string(e),
858-
token::NtLiteral(e) => self.expr_to_string(e),
859857
}
860858
}
861859

compiler/rustc_expand/src/mbe/transcribe.rs

+6
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,12 @@ pub(super) fn transcribe<'a>(
303303
MatchedSingle(ParseNtResult::PatWithOr(ref pat)) => {
304304
mk_delimited(NonterminalKind::PatWithOr, TokenStream::from_ast(pat))
305305
}
306+
MatchedSingle(ParseNtResult::Expr(ref expr)) => {
307+
mk_delimited(NonterminalKind::Expr, TokenStream::from_ast(expr))
308+
}
309+
MatchedSingle(ParseNtResult::Literal(ref expr)) => {
310+
mk_delimited(NonterminalKind::Literal, TokenStream::from_ast(expr))
311+
}
306312
MatchedSingle(ParseNtResult::Ty(ref ty)) => {
307313
mk_delimited(NonterminalKind::Ty, TokenStream::from_ast(ty))
308314
}

compiler/rustc_index/src/bit_set/tests.rs

+2-2
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

+1-1
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

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

16021602
#[derive(Diagnostic)]
16031603
#[diag(parse_unexpected_token_after_dot)]
1604-
pub struct UnexpectedTokenAfterDot<'a> {
1604+
pub struct UnexpectedTokenAfterDot {
16051605
#[primary_span]
16061606
pub span: Span,
1607-
pub actual: Cow<'a, str>,
1607+
pub actual: String,
16081608
}
16091609

16101610
#[derive(Diagnostic)]

0 commit comments

Comments
 (0)