Skip to content

Commit 5c1809a

Browse files
committed
XXX: NtExpr/NtLiteral
[xt] move from_token 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.31 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.31/src/book/summary.rs:275:31 | 275 | bail!(self.parse_error("Suffix chapters cannot be followed by a list")); | ^^^^ thread 'rustc' panicked at compiler/rustc_errors/src/diagnostic_builder.rs:775:21: error was constructed but not emitted ```
1 parent 776faaa commit 5c1809a

36 files changed

+678
-625
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
@@ -789,10 +789,8 @@ pub fn visit_token<T: MutVisitor>(t: &mut Token, vis: &mut T) {
789789
// multiple items there....
790790
pub fn visit_nonterminal<T: MutVisitor>(nt: &mut token::Nonterminal, vis: &mut T) {
791791
match nt {
792-
token::NtExpr(expr) => vis.visit_expr(expr),
793792
token::NtIdent(ident, _is_raw) => vis.visit_ident(ident),
794793
token::NtLifetime(ident) => vis.visit_ident(ident),
795-
token::NtLiteral(expr) => vis.visit_expr(expr),
796794
}
797795
}
798796

compiler/rustc_ast/src/token.rs

+43-49
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,18 +133,27 @@ impl Lit {
134133
}
135134
}
136135

137-
/// Keep this in sync with `Token::can_begin_literal_or_bool` 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 {
140-
Ident(name, false) if name.is_bool_lit() => {
141-
Some(Lit::new(Bool, name, None))
142-
}
140+
Ident(name, false) if name.is_bool_lit() => Some(Lit::new(Bool, name, None)),
143141
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)
142+
OpenDelim(Delimiter::Invisible(InvisibleSource::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(InvisibleSource::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+
// }
149157
}
150158
_ => None,
151159
}
@@ -417,6 +425,7 @@ impl Token {
417425
Token::new(Ident(ident.name, ident.is_raw_guess()), ident.span)
418426
}
419427

428+
/// njn: phase this out in favour of Parser::uninterpolated_span
420429
/// For interpolated tokens, returns a span of the fragment to which the interpolated
421430
/// token refers. For all other tokens this is just a regular span.
422431
/// It is particularly important to use this for identifiers and lifetimes
@@ -469,9 +478,11 @@ impl Token {
469478
ModSep | // global path
470479
Lifetime(..) | // labeled loop
471480
Pound => true, // expression attributes
472-
Interpolated(ref nt) => matches!(**nt, NtLiteral(..) | NtExpr(..)),
473481
OpenDelim(Delimiter::Invisible(InvisibleSource::MetaVar(
474-
NonterminalKind::Block | NonterminalKind::Path
482+
NonterminalKind::Block |
483+
NonterminalKind::Expr |
484+
NonterminalKind::Literal |
485+
NonterminalKind::Path
475486
)))
476487
=> true,
477488
_ => false,
@@ -494,12 +505,12 @@ impl Token {
494505
| DotDot | DotDotDot | DotDotEq // ranges
495506
| Lt | BinOp(Shl) // associated path
496507
| ModSep => true, // global path
497-
Interpolated(ref nt) => matches!(**nt, NtLiteral(..)),
498508
| OpenDelim(Delimiter::Invisible(InvisibleSource::MetaVar(
499509
NonterminalKind::Block |
500510
NonterminalKind::PatParam { .. } |
501511
NonterminalKind::PatWithOr |
502-
NonterminalKind::Path
512+
NonterminalKind::Path |
513+
NonterminalKind::Literal
503514
))) => true,
504515
_ => false,
505516
}
@@ -532,10 +543,9 @@ impl Token {
532543
pub fn can_begin_const_arg(&self) -> bool {
533544
match self.kind {
534545
OpenDelim(Delimiter::Brace) => true,
535-
Interpolated(ref nt) => matches!(**nt, NtExpr(..) | NtLiteral(..)),
536-
OpenDelim(Delimiter::Invisible(InvisibleSource::MetaVar(NonterminalKind::Block))) => {
537-
true
538-
}
546+
OpenDelim(Delimiter::Invisible(InvisibleSource::MetaVar(
547+
NonterminalKind::Block | NonterminalKind::Expr | NonterminalKind::Literal,
548+
))) => true,
539549
_ => self.can_begin_literal_maybe_minus(),
540550
}
541551
}
@@ -584,22 +594,15 @@ impl Token {
584594
///
585595
/// In other words, would this token be a valid start of `parse_literal_maybe_minus`?
586596
///
587-
/// Keep this in sync with and `Lit::from_token`, excluding unary negation.
597+
/// Keep this in sync with `Lit::from_token` and
598+
/// `Parser::maybe_parse_token_lit` (excluding unary negation).
588599
pub fn can_begin_literal_maybe_minus(&self) -> bool {
589600
match self.uninterpolate().kind {
590601
Literal(..) | BinOp(Minus) => true,
591602
Ident(name, false) if name.is_bool_lit() => true,
592-
Interpolated(ref nt) => match &**nt {
593-
NtLiteral(_) => true,
594-
NtExpr(e) => match &e.kind {
595-
ast::ExprKind::Lit(_) => true,
596-
ast::ExprKind::Unary(ast::UnOp::Neg, e) => {
597-
matches!(&e.kind, ast::ExprKind::Lit(_))
598-
}
599-
_ => false,
600-
},
601-
_ => false,
602-
},
603+
OpenDelim(Delimiter::Invisible(InvisibleSource::MetaVar(
604+
NonterminalKind::Literal | NonterminalKind::Expr,
605+
))) => true,
603606
_ => false,
604607
}
605608
}
@@ -615,7 +618,6 @@ impl Token {
615618
Cow::Owned(Token::new(Ident(ident.name, is_raw), ident.span))
616619
}
617620
NtLifetime(ident) => Cow::Owned(Token::new(Lifetime(ident.name), ident.span)),
618-
_ => Cow::Borrowed(self),
619621
},
620622
_ => Cow::Borrowed(self),
621623
}
@@ -665,22 +667,19 @@ impl Token {
665667
self.ident().is_some_and(|(ident, _)| ident.name == name)
666668
}
667669

668-
/// Would `maybe_whole_expr` in `parser.rs` return `Ok(..)`?
670+
/// Would `maybe_reparse_metavar_expr` in `parser.rs` return `Ok(..)`?
669671
/// That is, is this a pre-parsed expression dropped into the token stream
670672
/// (which happens while parsing the result of macro expansion)?
671-
pub fn is_whole_expr(&self) -> bool {
672-
if let Interpolated(nt) = &self.kind
673-
&& let NtExpr(_) | NtLiteral(_) = **nt
674-
{
675-
true
676-
} else if matches!(
673+
pub fn is_metavar_expr(&self) -> bool {
674+
matches!(
677675
self.is_metavar_seq(),
678-
Some(NonterminalKind::Block | NonterminalKind::Path)
679-
) {
680-
true
681-
} else {
682-
false
683-
}
676+
Some(
677+
NonterminalKind::Expr
678+
| NonterminalKind::Literal
679+
| NonterminalKind::Block
680+
| NonterminalKind::Path
681+
)
682+
)
684683
}
685684

686685
/// Are we at a block from a metavar (`$b:block`)?
@@ -843,10 +842,8 @@ impl PartialEq<TokenKind> for Token {
843842
#[derive(Clone, Encodable, Decodable)]
844843
/// For interpolation during macro expansion.
845844
pub enum Nonterminal {
846-
NtExpr(P<ast::Expr>),
847845
NtIdent(Ident, /* is_raw */ bool),
848846
NtLifetime(Ident),
849-
NtLiteral(P<ast::Expr>),
850847
}
851848

852849
#[derive(Debug, Copy, Clone, PartialEq, Eq, Encodable, Decodable, Hash, HashStable_Generic)]
@@ -930,7 +927,6 @@ impl fmt::Display for NonterminalKind {
930927
impl Nonterminal {
931928
pub fn span(&self) -> Span {
932929
match self {
933-
NtExpr(expr) | NtLiteral(expr) => expr.span,
934930
NtIdent(ident, _) | NtLifetime(ident) => ident.span,
935931
}
936932
}
@@ -955,9 +951,7 @@ impl PartialEq for Nonterminal {
955951
impl fmt::Debug for Nonterminal {
956952
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
957953
match *self {
958-
NtExpr(..) => f.pad("NtExpr(..)"),
959954
NtIdent(..) => f.pad("NtIdent(..)"),
960-
NtLiteral(..) => f.pad("NtLiteral(..)"),
961955
NtLifetime(..) => f.pad("NtLifetime(..)"),
962956
}
963957
}

compiler/rustc_ast/src/tokenstream.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,7 @@ impl TokenStream {
444444
let Some(tokens) = node.tokens() else {
445445
panic!("missing tokens for node at {:?}: {:?}", node.span(), node);
446446
};
447+
//eprintln!("from_ast: {tokens:#?}");
447448
let attrs = node.attrs();
448449
let attr_stream = if attrs.is_empty() {
449450
tokens.to_attr_token_stream()
@@ -463,7 +464,6 @@ impl TokenStream {
463464
Nonterminal::NtLifetime(ident) => {
464465
TokenStream::token_alone(token::Lifetime(ident.name), ident.span)
465466
}
466-
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => TokenStream::from_ast(expr),
467467
}
468468
}
469469

compiler/rustc_ast_pretty/src/pprust/state.rs

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

727727
fn nonterminal_to_string(&self, nt: &Nonterminal) -> String {
728728
match nt {
729-
token::NtExpr(e) => self.expr_to_string(e),
730729
token::NtIdent(e, is_raw) => IdentPrinter::for_ast_ident(*e, *is_raw).to_string(),
731730
token::NtLifetime(e) => e.to_string(),
732-
token::NtLiteral(e) => self.expr_to_string(e),
733731
}
734732
}
735733

compiler/rustc_expand/src/mbe/transcribe.rs

+9
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,9 @@ pub(super) fn transcribe<'a>(
221221
// Emit as a token stream within `Delimiter::Invisible` to maintain parsing
222222
// priorities.
223223
marker.visit_span(&mut sp);
224+
// njn: `sp` usage here means that both the open delim
225+
// and close delim end up with the same span, which
226+
// covers the `$foo` in the decl macro RHS
224227
TokenTree::Delimited(
225228
DelimSpan::from_single(sp),
226229
Delimiter::Invisible(InvisibleSource::MetaVar(nt_kind)),
@@ -255,6 +258,12 @@ pub(super) fn transcribe<'a>(
255258
MatchedSingle(ParseNtResult::PatWithOr(ref pat)) => {
256259
mk_delimited(NonterminalKind::PatWithOr, TokenStream::from_ast(pat))
257260
}
261+
MatchedSingle(ParseNtResult::Expr(ref expr)) => {
262+
mk_delimited(NonterminalKind::Expr, TokenStream::from_ast(expr))
263+
}
264+
MatchedSingle(ParseNtResult::Literal(ref expr)) => {
265+
mk_delimited(NonterminalKind::Literal, TokenStream::from_ast(expr))
266+
}
258267
MatchedSingle(ParseNtResult::Ty(ref ty)) => {
259268
mk_delimited(NonterminalKind::Ty, TokenStream::from_ast(ty))
260269
}

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+
]
340340
);
341341
let mut b10000b = ChunkedBitSet::<usize>::new_empty(10000);
342342
b10000b.clone_from(&b10000);

0 commit comments

Comments
 (0)