Skip to content

Commit 51cc83a

Browse files
committed
Remove NtBlock.
1 parent 705e4fd commit 51cc83a

File tree

11 files changed

+51
-71
lines changed

11 files changed

+51
-71
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-21
Original file line numberDiff line numberDiff line change
@@ -520,9 +520,7 @@ impl Token {
520520
PathSep | // global path
521521
Lifetime(..) | // labeled loop
522522
Pound => true, // expression attributes
523-
Interpolated(ref nt) => matches!(&nt.0, NtLiteral(..) |
524-
NtExpr(..) |
525-
NtBlock(..)),
523+
Interpolated(ref nt) => matches!(&nt.0, NtLiteral(..) | NtExpr(..)),
526524
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
527525
NonterminalKind::Block |
528526
NonterminalKind::Expr |
@@ -549,7 +547,7 @@ impl Token {
549547
| DotDot | DotDotDot | DotDotEq // ranges
550548
| Lt | BinOp(Shl) // associated path
551549
| PathSep => true, // global path
552-
Interpolated(ref nt) => matches!(&nt.0, NtLiteral(..) | NtBlock(..)),
550+
Interpolated(ref nt) => matches!(&nt.0, NtLiteral(..)),
553551
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
554552
NonterminalKind::Block |
555553
NonterminalKind::PatParam { .. } |
@@ -590,7 +588,7 @@ impl Token {
590588
pub fn can_begin_const_arg(&self) -> bool {
591589
match self.kind {
592590
OpenDelim(Delimiter::Brace) => true,
593-
Interpolated(ref nt) => matches!(&nt.0, NtExpr(..) | NtBlock(..) | NtLiteral(..)),
591+
Interpolated(ref nt) => matches!(&nt.0, NtExpr(..) | NtLiteral(..)),
594592
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
595593
NonterminalKind::Expr | NonterminalKind::Block | NonterminalKind::Literal,
596594
))) => true,
@@ -727,26 +725,22 @@ impl Token {
727725
pub fn is_whole_expr(&self) -> bool {
728726
// njn: nothing needed, just deal with NtExpr/NtLiteral/NtPath/NtBlock later
729727
if let Interpolated(nt) = &self.kind
730-
&& let NtExpr(_) | NtLiteral(_) | NtBlock(_) = &nt.0
728+
&& let NtExpr(_) | NtLiteral(_) = &nt.0
731729
{
732730
true
733-
} else if matches!(self.is_metavar_seq(), Some(NonterminalKind::Path)) {
731+
} else if matches!(
732+
self.is_metavar_seq(),
733+
Some(NonterminalKind::Block | NonterminalKind::Path)
734+
) {
734735
true
735736
} else {
736737
false
737738
}
738739
}
739740

740-
/// Is the token an interpolated block (`$b:block`)?
741-
pub fn is_whole_block(&self) -> bool {
742-
// njn: nothing needed, just deal with NtBlock later
743-
if let Interpolated(nt) = &self.kind
744-
&& let NtBlock(..) = &nt.0
745-
{
746-
return true;
747-
}
748-
749-
false
741+
/// Are we at a block from a metavar (`$b:block`)?
742+
pub fn is_metavar_block(&self) -> bool {
743+
matches!(self.is_metavar_seq(), Some(NonterminalKind::Block))
750744
}
751745

752746
/// Returns `true` if the token is either the `mut` or `const` keyword.
@@ -910,7 +904,6 @@ impl PartialEq<TokenKind> for Token {
910904
#[derive(Clone, Encodable, Decodable)]
911905
/// For interpolation during macro expansion.
912906
pub enum Nonterminal {
913-
NtBlock(P<ast::Block>),
914907
NtExpr(P<ast::Expr>),
915908
NtIdent(Ident, IdentIsRaw),
916909
NtLifetime(Ident),
@@ -998,15 +991,13 @@ impl fmt::Display for NonterminalKind {
998991
impl Nonterminal {
999992
pub fn use_span(&self) -> Span {
1000993
match self {
1001-
NtBlock(block) => block.span,
1002994
NtExpr(expr) | NtLiteral(expr) => expr.span,
1003995
NtIdent(ident, _) | NtLifetime(ident) => ident.span,
1004996
}
1005997
}
1006998

1007999
pub fn descr(&self) -> &'static str {
10081000
match self {
1009-
NtBlock(..) => "block",
10101001
NtExpr(..) => "expression",
10111002
NtLiteral(..) => "literal",
10121003
NtIdent(..) => "identifier",
@@ -1034,7 +1025,6 @@ impl PartialEq for Nonterminal {
10341025
impl fmt::Debug for Nonterminal {
10351026
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
10361027
match *self {
1037-
NtBlock(..) => f.pad("NtBlock(..)"),
10381028
NtExpr(..) => f.pad("NtExpr(..)"),
10391029
NtIdent(..) => f.pad("NtIdent(..)"),
10401030
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
@@ -53,13 +53,16 @@ macro_rules! maybe_whole_expr {
5353
$p.bump();
5454
return Ok(e);
5555
}
56-
token::NtBlock(block) => {
57-
let block = block.clone();
58-
$p.bump();
59-
return Ok($p.mk_expr($p.prev_token.span, ExprKind::Block(block, None)));
60-
}
6156
_ => {}
6257
};
58+
} else if let Some(block) = crate::maybe_reparse_metavar_seq!(
59+
$p,
60+
token::NonterminalKind::Block,
61+
token::NonterminalKind::Block,
62+
super::ParseNtResult::Block(block),
63+
block
64+
) {
65+
return Ok($p.mk_expr(span, ExprKind::Block(block, None)));
6366
} else if let Some(path) = crate::maybe_reparse_metavar_seq!(
6467
$p,
6568
token::NonterminalKind::Path,
@@ -1683,7 +1686,7 @@ impl<'a> Parser<'a> {
16831686
} else if self.eat_keyword(kw::Loop) {
16841687
self.parse_expr_loop(label, lo)
16851688
} else if self.check_noexpect(&token::OpenDelim(Delimiter::Brace))
1686-
|| self.token.is_whole_block()
1689+
|| self.token.is_metavar_block()
16871690
{
16881691
self.parse_expr_block(label, lo, BlockCheckMode::Default)
16891692
} else if !ate_colon
@@ -2304,7 +2307,7 @@ impl<'a> Parser<'a> {
23042307
}
23052308
}
23062309

2307-
if self.token.is_whole_block() {
2310+
if self.token.is_metavar_block() {
23082311
self.dcx().emit_err(errors::InvalidBlockMacroSegment {
23092312
span: self.token.span,
23102313
context: lo.to(self.token.span),
@@ -3365,7 +3368,7 @@ impl<'a> Parser<'a> {
33653368
self.token.is_keyword(kw::Do)
33663369
&& self.is_keyword_ahead(1, &[kw::Catch])
33673370
&& self
3368-
.look_ahead(2, |t| *t == token::OpenDelim(Delimiter::Brace) || t.is_whole_block())
3371+
.look_ahead(2, |t| *t == token::OpenDelim(Delimiter::Brace) || t.is_metavar_block())
33693372
&& !self.restrictions.contains(Restrictions::NO_STRUCT_LITERAL)
33703373
}
33713374

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

@@ -3409,12 +3412,12 @@ impl<'a> Parser<'a> {
34093412
// `async move {`
34103413
self.is_keyword_ahead(lookahead + 1, &[kw::Move])
34113414
&& self.look_ahead(lookahead + 2, |t| {
3412-
*t == token::OpenDelim(Delimiter::Brace) || t.is_whole_block()
3415+
*t == token::OpenDelim(Delimiter::Brace) || t.is_metavar_block()
34133416
})
34143417
) || (
34153418
// `async {`
34163419
self.look_ahead(lookahead + 1, |t| {
3417-
*t == token::OpenDelim(Delimiter::Brace) || t.is_whole_block()
3420+
*t == token::OpenDelim(Delimiter::Brace) || t.is_metavar_block()
34183421
})
34193422
))
34203423
}

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-26
Original file line numberDiff line numberDiff line change
@@ -88,22 +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-
// njn: nothing needed here due to $constructor match
96-
if let token::Interpolated(nt) = &$p.token.kind
97-
&& let token::$constructor(x) = &nt.0
98-
{
99-
#[allow(unused_mut)]
100-
let mut $x = x.clone();
101-
$p.bump();
102-
return Ok($e);
103-
}
104-
};
105-
}
106-
10791
/// Reparses an invisible-delimited sequence produced by expansion of a
10892
/// declarative macro metavariable. Will panic if called with a `self.token`
10993
/// that is not an `InvisibleOrigin::Metavar` invisible open delimiter.
@@ -775,18 +759,12 @@ impl<'a> Parser<'a> {
775759
}
776760

777761
fn check_inline_const(&self, dist: usize) -> bool {
778-
if self.is_keyword_ahead(dist, &[kw::Const]) {
779-
self.look_ahead(dist + 1, |t| match &t.kind {
780-
token::OpenDelim(Delimiter::Invisible(_)) => {
781-
panic!("njn: invis-delim?");
782-
}
783-
_ => {}
784-
})
785-
}
786762
self.is_keyword_ahead(dist, &[kw::Const])
787763
&& self.look_ahead(dist + 1, |t| match &t.kind {
788-
token::Interpolated(nt) => matches!(&nt.0, token::NtBlock(..)),
789764
token::OpenDelim(Delimiter::Brace) => true,
765+
token::OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
766+
NonterminalKind::Block,
767+
))) => true,
790768
_ => false,
791769
})
792770
}
@@ -1319,7 +1297,7 @@ impl<'a> Parser<'a> {
13191297
// Avoid const blocks and const closures to be parsed as const items
13201298
if (self.check_const_closure() == is_closure)
13211299
&& !self
1322-
.look_ahead(1, |t| *t == token::OpenDelim(Delimiter::Brace) || t.is_whole_block())
1300+
.look_ahead(1, |t| *t == token::OpenDelim(Delimiter::Brace) || t.is_metavar_block())
13231301
&& self.eat_keyword_case(kw::Const, case)
13241302
{
13251303
Const::Yes(self.prev_token.uninterpolated_span())
@@ -1681,6 +1659,7 @@ pub enum ParseNtResult<NtType> {
16811659
Tt(TokenTree),
16821660

16831661
Item(P<ast::Item>),
1662+
Block(P<ast::Block>),
16841663
Stmt(P<ast::Stmt>),
16851664
PatParam(P<ast::Pat>, /* inferred */ bool),
16861665
PatWithOr(P<ast::Pat>),
@@ -1701,6 +1680,7 @@ impl<T> ParseNtResult<T> {
17011680
match self {
17021681
ParseNtResult::Tt(tt) => ParseNtResult::Tt(tt),
17031682
ParseNtResult::Item(x) => ParseNtResult::Item(x),
1683+
ParseNtResult::Block(x) => ParseNtResult::Block(x),
17041684
ParseNtResult::Stmt(x) => ParseNtResult::Stmt(x),
17051685
ParseNtResult::PatParam(x, inf) => ParseNtResult::PatParam(x, inf),
17061686
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)