Skip to content

Commit 3977ae0

Browse files
committed
Remove NtBlock.
1 parent e783775 commit 3977ae0

File tree

11 files changed

+51
-61
lines changed

11 files changed

+51
-61
lines changed

compiler/rustc_ast/src/ast_traits.rs

-2
Original file line numberDiff line numberDiff line change
@@ -232,14 +232,12 @@ impl HasTokens for Nonterminal {
232232
fn tokens(&self) -> Option<&LazyAttrTokenStream> {
233233
match self {
234234
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => expr.tokens(),
235-
Nonterminal::NtBlock(block) => block.tokens(),
236235
Nonterminal::NtIdent(..) | Nonterminal::NtLifetime(..) => None,
237236
}
238237
}
239238
fn tokens_mut(&mut self) -> Option<&mut Option<LazyAttrTokenStream>> {
240239
match self {
241240
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => expr.tokens_mut(),
242-
Nonterminal::NtBlock(block) => block.tokens_mut(),
243241
Nonterminal::NtIdent(..) | Nonterminal::NtLifetime(..) => None,
244242
}
245243
}

compiler/rustc_ast/src/mut_visit.rs

-1
Original file line numberDiff line numberDiff line change
@@ -823,7 +823,6 @@ 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::NtBlock(block) => vis.visit_block(block),
827826
token::NtExpr(expr) => vis.visit_expr(expr),
828827
token::NtIdent(ident, _is_raw) => vis.visit_ident(ident),
829828
token::NtLifetime(ident) => vis.visit_ident(ident),

compiler/rustc_ast/src/token.rs

+11-20
Original file line numberDiff line numberDiff line change
@@ -516,9 +516,7 @@ impl Token {
516516
PathSep | // global path
517517
Lifetime(..) | // labeled loop
518518
Pound => true, // expression attributes
519-
Interpolated(ref nt) => matches!(&nt.0, NtLiteral(..) |
520-
NtExpr(..) |
521-
NtBlock(..)),
519+
Interpolated(ref nt) => matches!(&nt.0, NtLiteral(..) | NtExpr(..)),
522520
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
523521
NonterminalKind::Block |
524522
NonterminalKind::Expr |
@@ -545,7 +543,7 @@ impl Token {
545543
| DotDot | DotDotDot | DotDotEq // ranges
546544
| Lt | BinOp(Shl) // associated path
547545
| PathSep => true, // global path
548-
Interpolated(ref nt) => matches!(&nt.0, NtLiteral(..) | NtBlock(..)),
546+
Interpolated(ref nt) => matches!(&nt.0, NtLiteral(..)),
549547
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
550548
NonterminalKind::Block |
551549
NonterminalKind::PatParam { .. } |
@@ -586,7 +584,7 @@ impl Token {
586584
pub fn can_begin_const_arg(&self) -> bool {
587585
match self.kind {
588586
OpenDelim(Delimiter::Brace) => true,
589-
Interpolated(ref nt) => matches!(&nt.0, NtExpr(..) | NtBlock(..) | NtLiteral(..)),
587+
Interpolated(ref nt) => matches!(&nt.0, NtExpr(..) | NtLiteral(..)),
590588
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
591589
NonterminalKind::Expr | NonterminalKind::Block | NonterminalKind::Literal,
592590
))) => true,
@@ -719,25 +717,22 @@ impl Token {
719717
/// (which happens while parsing the result of macro expansion)?
720718
pub fn is_whole_expr(&self) -> bool {
721719
if let Interpolated(nt) = &self.kind
722-
&& let NtExpr(_) | NtLiteral(_) | NtBlock(_) = &nt.0
720+
&& let NtExpr(_) | NtLiteral(_) = &nt.0
723721
{
724722
true
725-
} else if matches!(self.is_metavar_seq(), Some(NonterminalKind::Path)) {
723+
} else if matches!(
724+
self.is_metavar_seq(),
725+
Some(NonterminalKind::Block | NonterminalKind::Path)
726+
) {
726727
true
727728
} else {
728729
false
729730
}
730731
}
731732

732-
/// Is the token an interpolated block (`$b:block`)?
733-
pub fn is_whole_block(&self) -> bool {
734-
if let Interpolated(nt) = &self.kind
735-
&& let NtBlock(..) = &nt.0
736-
{
737-
return true;
738-
}
739-
740-
false
733+
/// Are we at a block from a metavar (`$b:block`)?
734+
pub fn is_metavar_block(&self) -> bool {
735+
matches!(self.is_metavar_seq(), Some(NonterminalKind::Block))
741736
}
742737

743738
/// Returns `true` if the token is either the `mut` or `const` keyword.
@@ -900,7 +895,6 @@ impl PartialEq<TokenKind> for Token {
900895
#[derive(Clone, Encodable, Decodable)]
901896
/// For interpolation during macro expansion.
902897
pub enum Nonterminal {
903-
NtBlock(P<ast::Block>),
904898
NtExpr(P<ast::Expr>),
905899
NtIdent(Ident, IdentIsRaw),
906900
NtLifetime(Ident),
@@ -988,15 +982,13 @@ impl fmt::Display for NonterminalKind {
988982
impl Nonterminal {
989983
pub fn use_span(&self) -> Span {
990984
match self {
991-
NtBlock(block) => block.span,
992985
NtExpr(expr) | NtLiteral(expr) => expr.span,
993986
NtIdent(ident, _) | NtLifetime(ident) => ident.span,
994987
}
995988
}
996989

997990
pub fn descr(&self) -> &'static str {
998991
match self {
999-
NtBlock(..) => "block",
1000992
NtExpr(..) => "expression",
1001993
NtLiteral(..) => "literal",
1002994
NtIdent(..) => "identifier",
@@ -1024,7 +1016,6 @@ impl PartialEq for Nonterminal {
10241016
impl fmt::Debug for Nonterminal {
10251017
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
10261018
match *self {
1027-
NtBlock(..) => f.pad("NtBlock(..)"),
10281019
NtExpr(..) => f.pad("NtExpr(..)"),
10291020
NtIdent(..) => f.pad("NtIdent(..)"),
10301021
NtLiteral(..) => f.pad("NtLiteral(..)"),

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::NtBlock(block) => TokenStream::from_ast(block),
487486
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => TokenStream::from_ast(expr),
488487
}
489488
}

compiler/rustc_ast_pretty/src/pprust/state.rs

-1
Original file line numberDiff line numberDiff line change
@@ -845,7 +845,6 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
845845
fn nonterminal_to_string(&self, nt: &Nonterminal) -> String {
846846
match nt {
847847
token::NtExpr(e) => self.expr_to_string(e),
848-
token::NtBlock(e) => self.block_to_string(e),
849848
&token::NtIdent(e, is_raw) => IdentPrinter::for_ast_ident(e, is_raw.into()).to_string(),
850849
token::NtLifetime(e) => e.to_string(),
851850
token::NtLiteral(e) => self.expr_to_string(e),

compiler/rustc_expand/src/mbe/transcribe.rs

+3
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,9 @@ pub(super) fn transcribe<'a>(
270270
MatchedSingle(ParseNtResult::Item(ref item)) => {
271271
mk_delimited(NonterminalKind::Item, TokenStream::from_ast(item))
272272
}
273+
MatchedSingle(ParseNtResult::Block(ref block)) => {
274+
mk_delimited(NonterminalKind::Block, TokenStream::from_ast(block))
275+
}
273276
MatchedSingle(ParseNtResult::Stmt(ref stmt)) => {
274277
let stream = if let StmtKind::Empty = stmt.kind {
275278
// FIXME: Properly collect tokens for empty statements.

compiler/rustc_parse/src/parser/expr.rs

+14-11
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,16 @@ macro_rules! maybe_whole_expr {
5252
$p.bump();
5353
return Ok(e);
5454
}
55-
token::NtBlock(block) => {
56-
let block = block.clone();
57-
$p.bump();
58-
return Ok($p.mk_expr($p.prev_token.span, ExprKind::Block(block, None)));
59-
}
6055
_ => {}
6156
};
57+
} else if let Some(block) = crate::maybe_reparse_metavar_seq!(
58+
$p,
59+
token::NonterminalKind::Block,
60+
token::NonterminalKind::Block,
61+
super::ParseNtResult::Block(block),
62+
block
63+
) {
64+
return Ok($p.mk_expr(span, ExprKind::Block(block, None)));
6265
} else if let Some(path) = crate::maybe_reparse_metavar_seq!(
6366
$p,
6467
token::NonterminalKind::Path,
@@ -1682,7 +1685,7 @@ impl<'a> Parser<'a> {
16821685
} else if self.eat_keyword(kw::Loop) {
16831686
self.parse_expr_loop(label, lo)
16841687
} else if self.check_noexpect(&token::OpenDelim(Delimiter::Brace))
1685-
|| self.token.is_whole_block()
1688+
|| self.token.is_metavar_block()
16861689
{
16871690
self.parse_expr_block(label, lo, BlockCheckMode::Default)
16881691
} else if !ate_colon
@@ -2303,7 +2306,7 @@ impl<'a> Parser<'a> {
23032306
}
23042307
}
23052308

2306-
if self.token.is_whole_block() {
2309+
if self.token.is_metavar_block() {
23072310
self.dcx().emit_err(errors::InvalidBlockMacroSegment {
23082311
span: self.token.span,
23092312
context: lo.to(self.token.span),
@@ -3364,7 +3367,7 @@ impl<'a> Parser<'a> {
33643367
self.token.is_keyword(kw::Do)
33653368
&& self.is_keyword_ahead(1, &[kw::Catch])
33663369
&& self
3367-
.look_ahead(2, |t| *t == token::OpenDelim(Delimiter::Brace) || t.is_whole_block())
3370+
.look_ahead(2, |t| *t == token::OpenDelim(Delimiter::Brace) || t.is_metavar_block())
33683371
&& !self.restrictions.contains(Restrictions::NO_STRUCT_LITERAL)
33693372
}
33703373

@@ -3375,7 +3378,7 @@ impl<'a> Parser<'a> {
33753378
fn is_try_block(&self) -> bool {
33763379
self.token.is_keyword(kw::Try)
33773380
&& self
3378-
.look_ahead(1, |t| *t == token::OpenDelim(Delimiter::Brace) || t.is_whole_block())
3381+
.look_ahead(1, |t| *t == token::OpenDelim(Delimiter::Brace) || t.is_metavar_block())
33793382
&& self.token.uninterpolated_span().at_least_rust_2018()
33803383
}
33813384

@@ -3408,12 +3411,12 @@ impl<'a> Parser<'a> {
34083411
// `async move {`
34093412
self.is_keyword_ahead(lookahead + 1, &[kw::Move])
34103413
&& self.look_ahead(lookahead + 2, |t| {
3411-
*t == token::OpenDelim(Delimiter::Brace) || t.is_whole_block()
3414+
*t == token::OpenDelim(Delimiter::Brace) || t.is_metavar_block()
34123415
})
34133416
) || (
34143417
// `async {`
34153418
self.look_ahead(lookahead + 1, |t| {
3416-
*t == token::OpenDelim(Delimiter::Brace) || t.is_whole_block()
3419+
*t == token::OpenDelim(Delimiter::Brace) || t.is_metavar_block()
34173420
})
34183421
))
34193422
}

compiler/rustc_parse/src/parser/item.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2323,7 +2323,7 @@ impl<'a> Parser<'a> {
23232323
self.expect_semi()?;
23242324
*sig_hi = self.prev_token.span;
23252325
(AttrVec::new(), None)
2326-
} else if self.check(&token::OpenDelim(Delimiter::Brace)) || self.token.is_whole_block() {
2326+
} else if self.check(&token::OpenDelim(Delimiter::Brace)) || self.token.is_metavar_block() {
23272327
self.parse_block_common(self.token.span, BlockCheckMode::Default, false)
23282328
.map(|(attrs, body)| (attrs, Some(body)))?
23292329
} else if self.token.kind == token::Eq {

compiler/rustc_parse/src/parser/mod.rs

+6-17
Original file line numberDiff line numberDiff line change
@@ -88,21 +88,6 @@ pub enum TrailingToken {
8888
MaybeComma,
8989
}
9090

91-
/// Like `maybe_whole_expr`, but for things other than expressions.
92-
#[macro_export]
93-
macro_rules! maybe_whole {
94-
($p:expr, $constructor:ident, |$x:ident| $e:expr) => {
95-
if let token::Interpolated(nt) = &$p.token.kind
96-
&& let token::$constructor(x) = &nt.0
97-
{
98-
#[allow(unused_mut)]
99-
let mut $x = x.clone();
100-
$p.bump();
101-
return Ok($e);
102-
}
103-
};
104-
}
105-
10691
/// Reparses an invisible-delimited sequence produced by expansion of a
10792
/// declarative macro metavariable. Will panic if called with a `self.token`
10893
/// that is not an `InvisibleOrigin::Metavar` invisible open delimiter.
@@ -775,8 +760,10 @@ impl<'a> Parser<'a> {
775760
fn check_inline_const(&self, dist: usize) -> bool {
776761
self.is_keyword_ahead(dist, &[kw::Const])
777762
&& self.look_ahead(dist + 1, |t| match &t.kind {
778-
token::Interpolated(nt) => matches!(&nt.0, token::NtBlock(..)),
779763
token::OpenDelim(Delimiter::Brace) => true,
764+
token::OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
765+
NonterminalKind::Block,
766+
))) => true,
780767
_ => false,
781768
})
782769
}
@@ -1309,7 +1296,7 @@ impl<'a> Parser<'a> {
13091296
// Avoid const blocks and const closures to be parsed as const items
13101297
if (self.check_const_closure() == is_closure)
13111298
&& !self
1312-
.look_ahead(1, |t| *t == token::OpenDelim(Delimiter::Brace) || t.is_whole_block())
1299+
.look_ahead(1, |t| *t == token::OpenDelim(Delimiter::Brace) || t.is_metavar_block())
13131300
&& self.eat_keyword_case(kw::Const, case)
13141301
{
13151302
Const::Yes(self.prev_token.uninterpolated_span())
@@ -1671,6 +1658,7 @@ pub enum ParseNtResult<NtType> {
16711658
Tt(TokenTree),
16721659

16731660
Item(P<ast::Item>),
1661+
Block(P<ast::Block>),
16741662
Stmt(P<ast::Stmt>),
16751663
PatParam(P<ast::Pat>, /* inferred */ bool),
16761664
PatWithOr(P<ast::Pat>),
@@ -1691,6 +1679,7 @@ impl<T> ParseNtResult<T> {
16911679
match self {
16921680
ParseNtResult::Tt(tt) => ParseNtResult::Tt(tt),
16931681
ParseNtResult::Item(x) => ParseNtResult::Item(x),
1682+
ParseNtResult::Block(x) => ParseNtResult::Block(x),
16941683
ParseNtResult::Stmt(x) => ParseNtResult::Stmt(x),
16951684
ParseNtResult::PatParam(x, inf) => ParseNtResult::PatParam(x, inf),
16961685
ParseNtResult::PatWithOr(x) => ParseNtResult::PatWithOr(x),

compiler/rustc_parse/src/parser/nonterminal.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ impl<'a> Parser<'a> {
5050
| NtLiteral(_) // `true`, `false`
5151
=> true,
5252

53-
NtBlock(_)
54-
| NtLifetime(_) => false,
53+
NtLifetime(_) => false,
5554
}
5655
}
5756

@@ -77,7 +76,7 @@ impl<'a> Parser<'a> {
7776
NonterminalKind::Block => match &token.kind {
7877
token::OpenDelim(Delimiter::Brace) => true,
7978
token::Interpolated(nt) => match &nt.0 {
80-
NtBlock(_) | NtLifetime(_) | NtExpr(_) | NtLiteral(_) => true,
79+
NtLifetime(_) | NtExpr(_) | NtLiteral(_) => true,
8180
NtIdent(..) => false,
8281
},
8382
token::OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(k))) => match k {
@@ -168,7 +167,9 @@ impl<'a> Parser<'a> {
168167
NonterminalKind::Block => {
169168
// While a block *expression* may have attributes (e.g. `#[my_attr] { ... }`),
170169
// the ':block' matcher does not support them
171-
NtBlock(self.collect_tokens_no_attrs(|this| this.parse_block())?)
170+
return Ok(ParseNtResult::Block(
171+
self.collect_tokens_no_attrs(|this| this.parse_block())?)
172+
)
172173
}
173174
NonterminalKind::Stmt => match self.parse_stmt(ForceCollect::Yes)? {
174175
Some(stmt) => return Ok(ParseNtResult::Stmt(P(stmt))),

compiler/rustc_parse/src/parser/stmt.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use super::{
88
SemiColonMode, TrailingToken,
99
};
1010
use crate::errors::{self, MalformedLoopLabel};
11-
use crate::maybe_whole;
11+
use crate::maybe_reparse_metavar_seq;
1212
use crate::parser::Recovered;
1313
use ast::Label;
1414
use rustc_ast as ast;
@@ -537,7 +537,15 @@ impl<'a> Parser<'a> {
537537
blk_mode: BlockCheckMode,
538538
can_be_struct_literal: bool,
539539
) -> PResult<'a, (AttrVec, P<Block>)> {
540-
maybe_whole!(self, NtBlock, |block| (AttrVec::new(), block));
540+
if let Some(block) = maybe_reparse_metavar_seq!(
541+
self,
542+
NonterminalKind::Block,
543+
NonterminalKind::Block,
544+
ParseNtResult::Block(block),
545+
block
546+
) {
547+
return Ok((AttrVec::new(), block));
548+
}
541549

542550
let maybe_ident = self.prev_token.clone();
543551
self.maybe_recover_unexpected_block_label();
@@ -710,7 +718,7 @@ impl<'a> Parser<'a> {
710718
{
711719
if self.token == token::Colon
712720
&& self.look_ahead(1, |token| {
713-
token.is_whole_block()
721+
token.is_metavar_block()
714722
|| matches!(
715723
token.kind,
716724
token::Ident(

0 commit comments

Comments
 (0)