Skip to content

Commit 42a623a

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. 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 ```
1 parent 51cc83a commit 42a623a

37 files changed

+683
-572
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::NtIdent(..) | Nonterminal::NtLifetime(..) => None,
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::NtIdent(..) | Nonterminal::NtLifetime(..) => None,
242240
}
243241
}

compiler/rustc_ast/src/mut_visit.rs

-2
Original file line numberDiff line numberDiff line change
@@ -823,10 +823,8 @@ pub fn visit_token<T: MutVisitor>(t: &mut Token, vis: &mut T) {
823823
// multiple items there....
824824
pub fn visit_nonterminal<T: MutVisitor>(nt: &mut token::Nonterminal, vis: &mut T) {
825825
match nt {
826-
token::NtExpr(expr) => vis.visit_expr(expr),
827826
token::NtIdent(ident, _is_raw) => vis.visit_ident(ident),
828827
token::NtLifetime(ident) => vis.visit_ident(ident),
829-
token::NtLiteral(expr) => vis.visit_expr(expr),
830828
}
831829
}
832830

compiler/rustc_ast/src/token.rs

+42-45
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ pub use Nonterminal::*;
44
pub use TokenKind::*;
55

66
use crate::ast;
7-
use crate::ptr::P;
87
use crate::util::case::Case;
98

109
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
@@ -134,17 +133,27 @@ impl Lit {
134133
}
135134
}
136135

137-
/// Keep this in sync with `Token::can_begin_literal_maybe_minus` excluding unary negation.
136+
/// Keep this in sync with `Token::can_begin_literal_maybe_minus` and
137+
/// `Parser::maybe_parse_token_lit` (excluding unary negation).
138138
pub fn from_token(token: &Token) -> Option<Lit> {
139139
match token.uninterpolate().kind {
140140
Ident(name, IdentIsRaw::No) if name.is_bool_lit() => Some(Lit::new(Bool, name, None)),
141141
Literal(token_lit) => Some(token_lit),
142-
// njn: deal with later, with NtExpr/NtLiteral
143-
Interpolated(ref nt)
144-
if let NtExpr(expr) | NtLiteral(expr) = &nt.0
145-
&& let ast::ExprKind::Lit(token_lit) = expr.kind =>
146-
{
147-
Some(token_lit)
142+
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(NonterminalKind::Literal))) => {
143+
panic!("njn: FROM_TOKEN (1)");
144+
// if let NtExpr(expr) | NtLiteral(expr) = &**nt
145+
// && let ast::ExprKind::Lit(token_lit) = expr.kind =>
146+
// {
147+
// Some(token_lit)
148+
// }
149+
}
150+
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(NonterminalKind::Expr))) => {
151+
panic!("njn: FROM_TOKEN (2)");
152+
// if let NtExpr(expr) | NtLiteral(expr) = &**nt
153+
// && let ast::ExprKind::Lit(token_lit) = expr.kind =>
154+
// {
155+
// Some(token_lit)
156+
// }
148157
}
149158
_ => None,
150159
}
@@ -464,6 +473,7 @@ impl Token {
464473
Token::new(Ident(ident.name, ident.is_raw_guess().into()), ident.span)
465474
}
466475

476+
/// njn: phase this out in favour of Parser::uninterpolated_span
467477
/// For interpolated tokens, returns a span of the fragment to which the interpolated
468478
/// token refers. For all other tokens this is just a regular span.
469479
/// It is particularly important to use this for identifiers and lifetimes
@@ -520,7 +530,6 @@ impl Token {
520530
PathSep | // global path
521531
Lifetime(..) | // labeled loop
522532
Pound => true, // expression attributes
523-
Interpolated(ref nt) => matches!(&nt.0, NtLiteral(..) | NtExpr(..)),
524533
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
525534
NonterminalKind::Block |
526535
NonterminalKind::Expr |
@@ -547,7 +556,6 @@ impl Token {
547556
| DotDot | DotDotDot | DotDotEq // ranges
548557
| Lt | BinOp(Shl) // associated path
549558
| PathSep => true, // global path
550-
Interpolated(ref nt) => matches!(&nt.0, NtLiteral(..)),
551559
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
552560
NonterminalKind::Block |
553561
NonterminalKind::PatParam { .. } |
@@ -588,7 +596,6 @@ impl Token {
588596
pub fn can_begin_const_arg(&self) -> bool {
589597
match self.kind {
590598
OpenDelim(Delimiter::Brace) => true,
591-
Interpolated(ref nt) => matches!(&nt.0, NtExpr(..) | NtLiteral(..)),
592599
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
593600
NonterminalKind::Expr | NonterminalKind::Block | NonterminalKind::Literal,
594601
))) => true,
@@ -631,22 +638,24 @@ impl Token {
631638
///
632639
/// In other words, would this token be a valid start of `parse_literal_maybe_minus`?
633640
///
634-
/// Keep this in sync with and `Lit::from_token`, excluding unary negation.
641+
/// Keep this in sync with `Lit::from_token` and
642+
/// `Parser::maybe_parse_token_lit` (excluding unary negation).
635643
pub fn can_begin_literal_maybe_minus(&self) -> bool {
636644
match self.uninterpolate().kind {
637645
Literal(..) | BinOp(Minus) => true,
638646
Ident(name, IdentIsRaw::No) if name.is_bool_lit() => true,
639-
Interpolated(ref nt) => match &nt.0 {
640-
NtLiteral(_) => true,
641-
NtExpr(e) => match &e.kind {
642-
ast::ExprKind::Lit(_) => true,
643-
ast::ExprKind::Unary(ast::UnOp::Neg, e) => {
644-
matches!(&e.kind, ast::ExprKind::Lit(_))
645-
}
646-
_ => false,
647-
},
648-
_ => false,
649-
},
647+
// njn: fix up
648+
// Interpolated(ref nt) => match &nt.0 {
649+
// NtLiteral(_) => true,
650+
// NtExpr(e) => match &e.kind {
651+
// ast::ExprKind::Lit(_) => true,
652+
// ast::ExprKind::Unary(ast::UnOp::Neg, e) => {
653+
// matches!(&e.kind, ast::ExprKind::Lit(_))
654+
// }
655+
// _ => false,
656+
// },
657+
// _ => false,
658+
// },
650659
// njn: too simple compared to what's above?
651660
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
652661
NonterminalKind::Literal | NonterminalKind::Expr,
@@ -667,7 +676,6 @@ impl Token {
667676
Cow::Owned(Token::new(Ident(ident.name, *is_raw), ident.span))
668677
}
669678
NtLifetime(ident) => Cow::Owned(Token::new(Lifetime(ident.name), ident.span)),
670-
_ => Cow::Borrowed(self),
671679
},
672680
_ => Cow::Borrowed(self),
673681
}
@@ -719,23 +727,19 @@ impl Token {
719727
self.ident().is_some_and(|(ident, _)| ident.name == name)
720728
}
721729

722-
/// Would `maybe_whole_expr` in `parser.rs` return `Ok(..)`?
730+
/// Would `maybe_reparse_metavar_expr` in `parser.rs` return `Ok(..)`?
723731
/// That is, is this a pre-parsed expression dropped into the token stream
724732
/// (which happens while parsing the result of macro expansion)?
725-
pub fn is_whole_expr(&self) -> bool {
726-
// njn: nothing needed, just deal with NtExpr/NtLiteral/NtPath/NtBlock later
727-
if let Interpolated(nt) = &self.kind
728-
&& let NtExpr(_) | NtLiteral(_) = &nt.0
729-
{
730-
true
731-
} else if matches!(
733+
pub fn is_metavar_expr(&self) -> bool {
734+
matches!(
732735
self.is_metavar_seq(),
733-
Some(NonterminalKind::Block | NonterminalKind::Path)
734-
) {
735-
true
736-
} else {
737-
false
738-
}
736+
Some(
737+
NonterminalKind::Expr
738+
| NonterminalKind::Literal
739+
| NonterminalKind::Block
740+
| NonterminalKind::Path
741+
)
742+
)
739743
}
740744

741745
/// Are we at a block from a metavar (`$b:block`)?
@@ -904,10 +908,8 @@ impl PartialEq<TokenKind> for Token {
904908
#[derive(Clone, Encodable, Decodable)]
905909
/// For interpolation during macro expansion.
906910
pub enum Nonterminal {
907-
NtExpr(P<ast::Expr>),
908911
NtIdent(Ident, IdentIsRaw),
909912
NtLifetime(Ident),
910-
NtLiteral(P<ast::Expr>),
911913
}
912914

913915
#[derive(Debug, Copy, Clone, PartialEq, Eq, Encodable, Decodable, Hash, HashStable_Generic)]
@@ -991,15 +993,12 @@ impl fmt::Display for NonterminalKind {
991993
impl Nonterminal {
992994
pub fn use_span(&self) -> Span {
993995
match self {
994-
NtExpr(expr) | NtLiteral(expr) => expr.span,
995996
NtIdent(ident, _) | NtLifetime(ident) => ident.span,
996997
}
997998
}
998999

9991000
pub fn descr(&self) -> &'static str {
10001001
match self {
1001-
NtExpr(..) => "expression",
1002-
NtLiteral(..) => "literal",
10031002
NtIdent(..) => "identifier",
10041003
NtLifetime(..) => "lifetime",
10051004
}
@@ -1025,9 +1024,7 @@ impl PartialEq for Nonterminal {
10251024
impl fmt::Debug for Nonterminal {
10261025
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
10271026
match *self {
1028-
NtExpr(..) => f.pad("NtExpr(..)"),
10291027
NtIdent(..) => f.pad("NtIdent(..)"),
1030-
NtLiteral(..) => f.pad("NtLiteral(..)"),
10311028
NtLifetime(..) => f.pad("NtLifetime(..)"),
10321029
}
10331030
}

compiler/rustc_ast/src/tokenstream.rs

-1
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,6 @@ impl TokenStream {
483483
Nonterminal::NtLifetime(ident) => {
484484
TokenStream::token_alone(token::Lifetime(ident.name), ident.span)
485485
}
486-
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => TokenStream::from_ast(expr),
487486
}
488487
}
489488

compiler/rustc_ast_pretty/src/pprust/state.rs

-2
Original file line numberDiff line numberDiff line change
@@ -844,10 +844,8 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
844844

845845
fn nonterminal_to_string(&self, nt: &Nonterminal) -> String {
846846
match nt {
847-
token::NtExpr(e) => self.expr_to_string(e),
848847
&token::NtIdent(e, is_raw) => IdentPrinter::for_ast_ident(e, is_raw.into()).to_string(),
849848
token::NtLifetime(e) => e.to_string(),
850-
token::NtLiteral(e) => self.expr_to_string(e),
851849
}
852850
}
853851

compiler/rustc_expand/src/mbe/transcribe.rs

+9
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,9 @@ pub(super) fn transcribe<'a>(
254254
// Emit as a token stream within `Delimiter::Invisible` to maintain parsing
255255
// priorities.
256256
marker.visit_span(&mut sp);
257+
// njn: `sp` usage here means that both the open delim
258+
// and close delim end up with the same span, which
259+
// covers the `$foo` in the decl macro RHS
257260
TokenTree::Delimited(
258261
DelimSpan::from_single(sp),
259262
DelimSpacing::new(Spacing::Alone, Spacing::Alone), // njn: ?
@@ -289,6 +292,12 @@ pub(super) fn transcribe<'a>(
289292
MatchedSingle(ParseNtResult::PatWithOr(ref pat)) => {
290293
mk_delimited(NonterminalKind::PatWithOr, TokenStream::from_ast(pat))
291294
}
295+
MatchedSingle(ParseNtResult::Expr(ref expr)) => {
296+
mk_delimited(NonterminalKind::Expr, TokenStream::from_ast(expr))
297+
}
298+
MatchedSingle(ParseNtResult::Literal(ref expr)) => {
299+
mk_delimited(NonterminalKind::Literal, TokenStream::from_ast(expr))
300+
}
292301
MatchedSingle(ParseNtResult::Ty(ref ty)) => {
293302
mk_delimited(NonterminalKind::Ty, TokenStream::from_ast(ty))
294303
}

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
@@ -803,7 +803,7 @@ parse_unexpected_parentheses_in_match_arm_pattern = unexpected parentheses surro
803803
parse_unexpected_self_in_generic_parameters = unexpected keyword `Self` in generic parameters
804804
.note = you cannot use `Self` as a generic parameter because it is reserved for associated items
805805
806-
parse_unexpected_token_after_dot = unexpected token: `{$actual}`
806+
parse_unexpected_token_after_dot = unexpected token: {$actual}
807807
808808
parse_unexpected_token_after_label = expected `while`, `for`, `loop` or `{"{"}` after a label
809809
.suggestion_remove_label = consider removing the label

compiler/rustc_parse/src/errors.rs

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

16011601
#[derive(Diagnostic)]
16021602
#[diag(parse_unexpected_token_after_dot)]
1603-
pub struct UnexpectedTokenAfterDot<'a> {
1603+
pub struct UnexpectedTokenAfterDot {
16041604
#[primary_span]
16051605
pub span: Span,
1606-
pub actual: Cow<'a, str>,
1606+
pub actual: String,
16071607
}
16081608

16091609
#[derive(Diagnostic)]

0 commit comments

Comments
 (0)