Skip to content

Commit 4a67d7d

Browse files
committed
Auto merge of rust-lang#138478 - nnethercote:rm-NtExpr-NtLiteral, r=petrochenkov
Remove `NtExpr` and `NtLiteral` The next part of rust-lang#124141. r? `@petrochenkov`
2 parents 19f42cb + bf56100 commit 4a67d7d

32 files changed

+897
-646
lines changed

compiler/rustc_ast/src/ast_traits.rs

-2
Original file line numberDiff line numberDiff line change
@@ -209,13 +209,11 @@ impl HasTokens for Attribute {
209209
impl HasTokens for Nonterminal {
210210
fn tokens(&self) -> Option<&LazyAttrTokenStream> {
211211
match self {
212-
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => expr.tokens(),
213212
Nonterminal::NtBlock(block) => block.tokens(),
214213
}
215214
}
216215
fn tokens_mut(&mut self) -> Option<&mut Option<LazyAttrTokenStream>> {
217216
match self {
218-
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => expr.tokens_mut(),
219217
Nonterminal::NtBlock(block) => block.tokens_mut(),
220218
}
221219
}

compiler/rustc_ast/src/mut_visit.rs

-2
Original file line numberDiff line numberDiff line change
@@ -900,8 +900,6 @@ pub fn visit_token<T: MutVisitor>(vis: &mut T, t: &mut Token) {
900900
fn visit_nonterminal<T: MutVisitor>(vis: &mut T, nt: &mut token::Nonterminal) {
901901
match nt {
902902
token::NtBlock(block) => vis.visit_block(block),
903-
token::NtExpr(expr) => vis.visit_expr(expr),
904-
token::NtLiteral(expr) => vis.visit_expr(expr),
905903
}
906904
}
907905

compiler/rustc_ast/src/token.rs

+26-66
Original file line numberDiff line numberDiff line change
@@ -198,16 +198,17 @@ impl Lit {
198198
}
199199
}
200200

201-
/// Keep this in sync with `Token::can_begin_literal_maybe_minus` excluding unary negation.
201+
/// Keep this in sync with `Token::can_begin_literal_maybe_minus` and
202+
/// `Parser::eat_token_lit` (excluding unary negation).
202203
pub fn from_token(token: &Token) -> Option<Lit> {
203204
match token.uninterpolate().kind {
204205
Ident(name, IdentIsRaw::No) if name.is_bool_lit() => Some(Lit::new(Bool, name, None)),
205206
Literal(token_lit) => Some(token_lit),
206-
Interpolated(ref nt)
207-
if let NtExpr(expr) | NtLiteral(expr) = &**nt
208-
&& let ast::ExprKind::Lit(token_lit) = expr.kind =>
209-
{
210-
Some(token_lit)
207+
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
208+
MetaVarKind::Literal | MetaVarKind::Expr { .. },
209+
))) => {
210+
// Unreachable with the current test suite.
211+
panic!("from_token metavar");
211212
}
212213
_ => None,
213214
}
@@ -447,8 +448,9 @@ pub enum TokenKind {
447448

448449
/// Identifier token.
449450
/// Do not forget about `NtIdent` when you want to match on identifiers.
450-
/// It's recommended to use `Token::(ident,uninterpolate,uninterpolated_span)` to
451-
/// treat regular and interpolated identifiers in the same way.
451+
/// It's recommended to use `Token::{ident,uninterpolate}` and
452+
/// `Parser::token_uninterpolated_span` to treat regular and interpolated
453+
/// identifiers in the same way.
452454
Ident(Symbol, IdentIsRaw),
453455
/// This identifier (and its span) is the identifier passed to the
454456
/// declarative macro. The span in the surrounding `Token` is the span of
@@ -457,8 +459,9 @@ pub enum TokenKind {
457459

458460
/// Lifetime identifier token.
459461
/// Do not forget about `NtLifetime` when you want to match on lifetime identifiers.
460-
/// It's recommended to use `Token::(lifetime,uninterpolate,uninterpolated_span)` to
461-
/// treat regular and interpolated lifetime identifiers in the same way.
462+
/// It's recommended to use `Token::{ident,uninterpolate}` and
463+
/// `Parser::token_uninterpolated_span` to treat regular and interpolated
464+
/// identifiers in the same way.
462465
Lifetime(Symbol, IdentIsRaw),
463466
/// This identifier (and its span) is the lifetime passed to the
464467
/// declarative macro. The span in the surrounding `Token` is the span of
@@ -584,20 +587,6 @@ impl Token {
584587
Token::new(Ident(ident.name, ident.is_raw_guess().into()), ident.span)
585588
}
586589

587-
/// For interpolated tokens, returns a span of the fragment to which the interpolated
588-
/// token refers. For all other tokens this is just a regular span.
589-
/// It is particularly important to use this for identifiers and lifetimes
590-
/// for which spans affect name resolution and edition checks.
591-
/// Note that keywords are also identifiers, so they should use this
592-
/// if they keep spans or perform edition checks.
593-
pub fn uninterpolated_span(&self) -> Span {
594-
match self.kind {
595-
NtIdent(ident, _) | NtLifetime(ident, _) => ident.span,
596-
Interpolated(ref nt) => nt.use_span(),
597-
_ => self.span,
598-
}
599-
}
600-
601590
pub fn is_range_separator(&self) -> bool {
602591
[DotDot, DotDotDot, DotDotEq].contains(&self.kind)
603592
}
@@ -642,12 +631,7 @@ impl Token {
642631
PathSep | // global path
643632
Lifetime(..) | // labeled loop
644633
Pound => true, // expression attributes
645-
Interpolated(ref nt) =>
646-
matches!(&**nt,
647-
NtBlock(..) |
648-
NtExpr(..) |
649-
NtLiteral(..)
650-
),
634+
Interpolated(ref nt) => matches!(&**nt, NtBlock(..)),
651635
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
652636
MetaVarKind::Block |
653637
MetaVarKind::Expr { .. } |
@@ -677,11 +661,6 @@ impl Token {
677661
Lt | // path (UFCS constant)
678662
Shl => true, // path (double UFCS)
679663
Or => matches!(pat_kind, PatWithOr), // leading vert `|` or-pattern
680-
Interpolated(nt) =>
681-
matches!(&**nt,
682-
| NtExpr(..)
683-
| NtLiteral(..)
684-
),
685664
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
686665
MetaVarKind::Expr { .. } |
687666
MetaVarKind::Literal |
@@ -724,7 +703,7 @@ impl Token {
724703
match self.kind {
725704
OpenDelim(Delimiter::Brace) | Literal(..) | Minus => true,
726705
Ident(name, IdentIsRaw::No) if name.is_bool_lit() => true,
727-
Interpolated(ref nt) => matches!(&**nt, NtExpr(..) | NtBlock(..) | NtLiteral(..)),
706+
Interpolated(ref nt) => matches!(&**nt, NtBlock(..)),
728707
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
729708
MetaVarKind::Expr { .. } | MetaVarKind::Block | MetaVarKind::Literal,
730709
))) => true,
@@ -768,22 +747,12 @@ impl Token {
768747
///
769748
/// In other words, would this token be a valid start of `parse_literal_maybe_minus`?
770749
///
771-
/// Keep this in sync with and `Lit::from_token`, excluding unary negation.
750+
/// Keep this in sync with `Lit::from_token` and `Parser::eat_token_lit`
751+
/// (excluding unary negation).
772752
pub fn can_begin_literal_maybe_minus(&self) -> bool {
773753
match self.uninterpolate().kind {
774754
Literal(..) | Minus => true,
775755
Ident(name, IdentIsRaw::No) if name.is_bool_lit() => true,
776-
Interpolated(ref nt) => match &**nt {
777-
NtLiteral(_) => true,
778-
NtExpr(e) => match &e.kind {
779-
ast::ExprKind::Lit(_) => true,
780-
ast::ExprKind::Unary(ast::UnOp::Neg, e) => {
781-
matches!(&e.kind, ast::ExprKind::Lit(_))
782-
}
783-
_ => false,
784-
},
785-
_ => false,
786-
},
787756
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(mv_kind))) => match mv_kind {
788757
MetaVarKind::Literal => true,
789758
MetaVarKind::Expr { can_begin_literal_maybe_minus, .. } => {
@@ -798,14 +767,6 @@ impl Token {
798767
pub fn can_begin_string_literal(&self) -> bool {
799768
match self.uninterpolate().kind {
800769
Literal(..) => true,
801-
Interpolated(ref nt) => match &**nt {
802-
NtLiteral(_) => true,
803-
NtExpr(e) => match &e.kind {
804-
ast::ExprKind::Lit(_) => true,
805-
_ => false,
806-
},
807-
_ => false,
808-
},
809770
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(mv_kind))) => match mv_kind {
810771
MetaVarKind::Literal => true,
811772
MetaVarKind::Expr { can_begin_string_literal, .. } => can_begin_string_literal,
@@ -869,19 +830,25 @@ impl Token {
869830

870831
/// Is this a pre-parsed expression dropped into the token stream
871832
/// (which happens while parsing the result of macro expansion)?
872-
pub fn is_whole_expr(&self) -> bool {
833+
pub fn is_metavar_expr(&self) -> bool {
873834
#[allow(irrefutable_let_patterns)] // FIXME: temporary
874835
if let Interpolated(nt) = &self.kind
875-
&& let NtExpr(_) | NtLiteral(_) | NtBlock(_) = &**nt
836+
&& let NtBlock(_) = &**nt
876837
{
877838
true
839+
} else if matches!(
840+
self.is_metavar_seq(),
841+
Some(MetaVarKind::Expr { .. } | MetaVarKind::Literal | MetaVarKind::Path)
842+
) {
843+
true
878844
} else {
879845
matches!(self.is_metavar_seq(), Some(MetaVarKind::Path))
880846
}
881847
}
882848

883849
/// Is the token an interpolated block (`$b:block`)?
884850
pub fn is_whole_block(&self) -> bool {
851+
#[allow(irrefutable_let_patterns)] // FIXME: temporary
885852
if let Interpolated(nt) = &self.kind
886853
&& let NtBlock(..) = &**nt
887854
{
@@ -1100,8 +1067,6 @@ pub enum NtExprKind {
11001067
/// For interpolation during macro expansion.
11011068
pub enum Nonterminal {
11021069
NtBlock(P<ast::Block>),
1103-
NtExpr(P<ast::Expr>),
1104-
NtLiteral(P<ast::Expr>),
11051070
}
11061071

11071072
#[derive(Debug, Copy, Clone, PartialEq, Eq, Encodable, Decodable, Hash, HashStable_Generic)]
@@ -1191,15 +1156,12 @@ impl Nonterminal {
11911156
pub fn use_span(&self) -> Span {
11921157
match self {
11931158
NtBlock(block) => block.span,
1194-
NtExpr(expr) | NtLiteral(expr) => expr.span,
11951159
}
11961160
}
11971161

11981162
pub fn descr(&self) -> &'static str {
11991163
match self {
12001164
NtBlock(..) => "block",
1201-
NtExpr(..) => "expression",
1202-
NtLiteral(..) => "literal",
12031165
}
12041166
}
12051167
}
@@ -1218,8 +1180,6 @@ impl fmt::Debug for Nonterminal {
12181180
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
12191181
match *self {
12201182
NtBlock(..) => f.pad("NtBlock(..)"),
1221-
NtExpr(..) => f.pad("NtExpr(..)"),
1222-
NtLiteral(..) => f.pad("NtLiteral(..)"),
12231183
}
12241184
}
12251185
}
@@ -1242,7 +1202,7 @@ mod size_asserts {
12421202
// tidy-alphabetical-start
12431203
static_assert_size!(Lit, 12);
12441204
static_assert_size!(LitKind, 2);
1245-
static_assert_size!(Nonterminal, 16);
1205+
static_assert_size!(Nonterminal, 8);
12461206
static_assert_size!(Token, 24);
12471207
static_assert_size!(TokenKind, 16);
12481208
// tidy-alphabetical-end

compiler/rustc_ast/src/tokenstream.rs

+39-23
Original file line numberDiff line numberDiff line change
@@ -233,35 +233,52 @@ fn attrs_and_tokens_to_token_trees(
233233

234234
// Insert inner attribute tokens.
235235
if !inner_attrs.is_empty() {
236-
let mut found = false;
237-
// Check the last two trees (to account for a trailing semi)
238-
for tree in res.iter_mut().rev().take(2) {
239-
if let TokenTree::Delimited(span, spacing, delim, delim_tokens) = tree {
240-
// Inner attributes are only supported on extern blocks, functions,
241-
// impls, and modules. All of these have their inner attributes
242-
// placed at the beginning of the rightmost outermost braced group:
243-
// e.g. fn foo() { #![my_attr] }
244-
//
245-
// Therefore, we can insert them back into the right location
246-
// without needing to do any extra position tracking.
247-
//
248-
// Note: Outline modules are an exception - they can
249-
// have attributes like `#![my_attr]` at the start of a file.
250-
// Support for custom attributes in this position is not
251-
// properly implemented - we always synthesize fake tokens,
252-
// so we never reach this code.
236+
let found = insert_inner_attrs(inner_attrs, res);
237+
assert!(found, "Failed to find trailing delimited group in: {res:?}");
238+
}
239+
240+
// Inner attributes are only supported on blocks, functions, impls, and
241+
// modules. All of these have their inner attributes placed at the
242+
// beginning of the rightmost outermost braced group:
243+
// e.g. `fn foo() { #![my_attr] }`. (Note: the braces may be within
244+
// invisible delimiters.)
245+
//
246+
// Therefore, we can insert them back into the right location without
247+
// needing to do any extra position tracking.
248+
//
249+
// Note: Outline modules are an exception - they can have attributes like
250+
// `#![my_attr]` at the start of a file. Support for custom attributes in
251+
// this position is not properly implemented - we always synthesize fake
252+
// tokens, so we never reach this code.
253+
fn insert_inner_attrs(inner_attrs: &[Attribute], tts: &mut Vec<TokenTree>) -> bool {
254+
for tree in tts.iter_mut().rev() {
255+
if let TokenTree::Delimited(span, spacing, Delimiter::Brace, stream) = tree {
256+
// Found it: the rightmost, outermost braced group.
253257
let mut tts = vec![];
254258
for inner_attr in inner_attrs {
255259
tts.extend(inner_attr.token_trees());
256260
}
257-
tts.extend(delim_tokens.0.iter().cloned());
261+
tts.extend(stream.0.iter().cloned());
258262
let stream = TokenStream::new(tts);
259-
*tree = TokenTree::Delimited(*span, *spacing, *delim, stream);
260-
found = true;
261-
break;
263+
*tree = TokenTree::Delimited(*span, *spacing, Delimiter::Brace, stream);
264+
return true;
265+
} else if let TokenTree::Delimited(span, spacing, Delimiter::Invisible(src), stream) =
266+
tree
267+
{
268+
// Recurse inside invisible delimiters.
269+
let mut vec: Vec<_> = stream.iter().cloned().collect();
270+
if insert_inner_attrs(inner_attrs, &mut vec) {
271+
*tree = TokenTree::Delimited(
272+
*span,
273+
*spacing,
274+
Delimiter::Invisible(*src),
275+
TokenStream::new(vec),
276+
);
277+
return true;
278+
}
262279
}
263280
}
264-
assert!(found, "Failed to find trailing delimited group in: {res:?}");
281+
false
265282
}
266283
}
267284

@@ -462,7 +479,6 @@ impl TokenStream {
462479
pub fn from_nonterminal_ast(nt: &Nonterminal) -> TokenStream {
463480
match nt {
464481
Nonterminal::NtBlock(block) => TokenStream::from_ast(block),
465-
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => TokenStream::from_ast(expr),
466482
}
467483
}
468484

compiler/rustc_expand/src/mbe/transcribe.rs

+28-7
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@ use std::sync::Arc;
33

44
use rustc_ast::mut_visit::{self, MutVisitor};
55
use rustc_ast::token::{
6-
self, Delimiter, IdentIsRaw, InvisibleOrigin, Lit, LitKind, MetaVarKind, Nonterminal, Token,
7-
TokenKind,
6+
self, Delimiter, IdentIsRaw, InvisibleOrigin, Lit, LitKind, MetaVarKind, Token, TokenKind,
87
};
98
use rustc_ast::tokenstream::{DelimSpacing, DelimSpan, Spacing, TokenStream, TokenTree};
10-
use rustc_ast::{ExprKind, StmtKind, TyKind};
9+
use rustc_ast::{ExprKind, StmtKind, TyKind, UnOp};
1110
use rustc_data_structures::fx::FxHashMap;
1211
use rustc_errors::{Diag, DiagCtxtHandle, PResult, pluralize};
1312
use rustc_parse::lexer::nfc_normalize;
@@ -340,6 +339,30 @@ pub(super) fn transcribe<'a>(
340339
MetaVarKind::Pat(*pat_kind),
341340
TokenStream::from_ast(pat),
342341
),
342+
MatchedSingle(ParseNtResult::Expr(expr, kind)) => {
343+
let (can_begin_literal_maybe_minus, can_begin_string_literal) =
344+
match &expr.kind {
345+
ExprKind::Lit(_) => (true, true),
346+
ExprKind::Unary(UnOp::Neg, e)
347+
if matches!(&e.kind, ExprKind::Lit(_)) =>
348+
{
349+
(true, false)
350+
}
351+
_ => (false, false),
352+
};
353+
mk_delimited(
354+
expr.span,
355+
MetaVarKind::Expr {
356+
kind: *kind,
357+
can_begin_literal_maybe_minus,
358+
can_begin_string_literal,
359+
},
360+
TokenStream::from_ast(expr),
361+
)
362+
}
363+
MatchedSingle(ParseNtResult::Literal(lit)) => {
364+
mk_delimited(lit.span, MetaVarKind::Literal, TokenStream::from_ast(lit))
365+
}
343366
MatchedSingle(ParseNtResult::Ty(ty)) => {
344367
let is_path = matches!(&ty.kind, TyKind::Path(None, _path));
345368
mk_delimited(
@@ -869,10 +892,8 @@ fn extract_symbol_from_pnr<'a>(
869892
},
870893
_,
871894
)) => Ok(*symbol),
872-
ParseNtResult::Nt(nt)
873-
if let Nonterminal::NtLiteral(expr) = &**nt
874-
&& let ExprKind::Lit(Lit { kind: LitKind::Str, symbol, suffix: None }) =
875-
&expr.kind =>
895+
ParseNtResult::Literal(expr)
896+
if let ExprKind::Lit(Lit { kind: LitKind::Str, symbol, suffix: None }) = &expr.kind =>
876897
{
877898
Ok(*symbol)
878899
}

compiler/rustc_parse/messages.ftl

+1-1
Original file line numberDiff line numberDiff line change
@@ -860,7 +860,7 @@ parse_unexpected_parentheses_in_match_arm_pattern = unexpected parentheses surro
860860
parse_unexpected_self_in_generic_parameters = unexpected keyword `Self` in generic parameters
861861
.note = you cannot use `Self` as a generic parameter because it is reserved for associated items
862862
863-
parse_unexpected_token_after_dot = unexpected token: `{$actual}`
863+
parse_unexpected_token_after_dot = unexpected token: {$actual}
864864
865865
parse_unexpected_token_after_label = expected `while`, `for`, `loop` or `{"{"}` after a label
866866
.suggestion_remove_label = consider removing the label

0 commit comments

Comments
 (0)