Skip to content

Commit 776faaa

Browse files
committed
Remove NtBlock.
1 parent 09d0cd3 commit 776faaa

File tree

12 files changed

+62
-61
lines changed

12 files changed

+62
-61
lines changed

compiler/rustc_ast/src/ast_traits.rs

Lines changed: 0 additions & 2 deletions
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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,6 @@ 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::NtBlock(block) => vis.visit_block(block),
793792
token::NtExpr(expr) => vis.visit_expr(expr),
794793
token::NtIdent(ident, _is_raw) => vis.visit_ident(ident),
795794
token::NtLifetime(ident) => vis.visit_ident(ident),

compiler/rustc_ast/src/token.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -469,10 +469,10 @@ impl Token {
469469
ModSep | // global path
470470
Lifetime(..) | // labeled loop
471471
Pound => true, // expression attributes
472-
Interpolated(ref nt) => matches!(**nt, NtLiteral(..) |
473-
NtExpr(..) |
474-
NtBlock(..)),
475-
OpenDelim(Delimiter::Invisible(InvisibleSource::MetaVar(NonterminalKind::Path)))
472+
Interpolated(ref nt) => matches!(**nt, NtLiteral(..) | NtExpr(..)),
473+
OpenDelim(Delimiter::Invisible(InvisibleSource::MetaVar(
474+
NonterminalKind::Block | NonterminalKind::Path
475+
)))
476476
=> true,
477477
_ => false,
478478
}
@@ -494,8 +494,9 @@ impl Token {
494494
| DotDot | DotDotDot | DotDotEq // ranges
495495
| Lt | BinOp(Shl) // associated path
496496
| ModSep => true, // global path
497-
Interpolated(ref nt) => matches!(**nt, NtLiteral(..) | NtBlock(..)),
497+
Interpolated(ref nt) => matches!(**nt, NtLiteral(..)),
498498
| OpenDelim(Delimiter::Invisible(InvisibleSource::MetaVar(
499+
NonterminalKind::Block |
499500
NonterminalKind::PatParam { .. } |
500501
NonterminalKind::PatWithOr |
501502
NonterminalKind::Path
@@ -531,7 +532,10 @@ impl Token {
531532
pub fn can_begin_const_arg(&self) -> bool {
532533
match self.kind {
533534
OpenDelim(Delimiter::Brace) => true,
534-
Interpolated(ref nt) => matches!(**nt, NtExpr(..) | NtBlock(..) | NtLiteral(..)),
535+
Interpolated(ref nt) => matches!(**nt, NtExpr(..) | NtLiteral(..)),
536+
OpenDelim(Delimiter::Invisible(InvisibleSource::MetaVar(NonterminalKind::Block))) => {
537+
true
538+
}
535539
_ => self.can_begin_literal_maybe_minus(),
536540
}
537541
}
@@ -666,23 +670,22 @@ impl Token {
666670
/// (which happens while parsing the result of macro expansion)?
667671
pub fn is_whole_expr(&self) -> bool {
668672
if let Interpolated(nt) = &self.kind
669-
&& let NtExpr(_) | NtLiteral(_) | NtBlock(_) = **nt
673+
&& let NtExpr(_) | NtLiteral(_) = **nt
670674
{
671675
true
672-
} else if matches!(self.is_metavar_seq(), Some(NonterminalKind::Path)) {
676+
} else if matches!(
677+
self.is_metavar_seq(),
678+
Some(NonterminalKind::Block | NonterminalKind::Path)
679+
) {
673680
true
674681
} else {
675682
false
676683
}
677684
}
678685

679-
/// Is the token an interpolated block (`$b:block`)?
680-
pub fn is_whole_block(&self) -> bool {
681-
if let Interpolated(nt) = &self.kind && let NtBlock(..) = **nt {
682-
return true;
683-
}
684-
685-
false
686+
/// Are we at a block from a metavar (`$b:block`)?
687+
pub fn is_metavar_block(&self) -> bool {
688+
matches!(self.is_metavar_seq(), Some(NonterminalKind::Block))
686689
}
687690

688691
/// Returns `true` if the token is either the `mut` or `const` keyword.
@@ -840,7 +843,6 @@ impl PartialEq<TokenKind> for Token {
840843
#[derive(Clone, Encodable, Decodable)]
841844
/// For interpolation during macro expansion.
842845
pub enum Nonterminal {
843-
NtBlock(P<ast::Block>),
844846
NtExpr(P<ast::Expr>),
845847
NtIdent(Ident, /* is_raw */ bool),
846848
NtLifetime(Ident),
@@ -928,7 +930,6 @@ impl fmt::Display for NonterminalKind {
928930
impl Nonterminal {
929931
pub fn span(&self) -> Span {
930932
match self {
931-
NtBlock(block) => block.span,
932933
NtExpr(expr) | NtLiteral(expr) => expr.span,
933934
NtIdent(ident, _) | NtLifetime(ident) => ident.span,
934935
}
@@ -954,7 +955,6 @@ impl PartialEq for Nonterminal {
954955
impl fmt::Debug for Nonterminal {
955956
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
956957
match *self {
957-
NtBlock(..) => f.pad("NtBlock(..)"),
958958
NtExpr(..) => f.pad("NtExpr(..)"),
959959
NtIdent(..) => f.pad("NtIdent(..)"),
960960
NtLiteral(..) => f.pad("NtLiteral(..)"),

compiler/rustc_ast/src/tokenstream.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,6 @@ impl TokenStream {
463463
Nonterminal::NtLifetime(ident) => {
464464
TokenStream::token_alone(token::Lifetime(ident.name), ident.span)
465465
}
466-
Nonterminal::NtBlock(block) => TokenStream::from_ast(block),
467466
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => TokenStream::from_ast(expr),
468467
}
469468
}

compiler/rustc_ast_pretty/src/pprust/state.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,6 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
727727
fn nonterminal_to_string(&self, nt: &Nonterminal) -> String {
728728
match nt {
729729
token::NtExpr(e) => self.expr_to_string(e),
730-
token::NtBlock(e) => self.block_to_string(e),
731730
token::NtIdent(e, is_raw) => IdentPrinter::for_ast_ident(*e, *is_raw).to_string(),
732731
token::NtLifetime(e) => e.to_string(),
733732
token::NtLiteral(e) => self.expr_to_string(e),

compiler/rustc_expand/src/mbe/transcribe.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,9 @@ pub(super) fn transcribe<'a>(
236236
MatchedSingle(ParseNtResult::Item(ref item)) => {
237237
mk_delimited(NonterminalKind::Item, TokenStream::from_ast(item))
238238
}
239+
MatchedSingle(ParseNtResult::Block(ref block)) => {
240+
mk_delimited(NonterminalKind::Block, TokenStream::from_ast(block))
241+
}
239242
MatchedSingle(ParseNtResult::Stmt(ref stmt)) => {
240243
let stream = if let StmtKind::Empty = stmt.kind {
241244
// FIXME: Properly collect tokens for empty statements.

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,16 @@ macro_rules! maybe_whole_expr {
5050
$p.bump();
5151
return Ok(e);
5252
}
53-
token::NtBlock(block) => {
54-
let block = block.clone();
55-
$p.bump();
56-
return Ok($p.mk_expr($p.prev_token.span, ExprKind::Block(block, None)));
57-
}
5853
_ => {}
5954
};
55+
} else if let Some(block) = crate::maybe_reparse_metavar_seq!(
56+
$p,
57+
token::NonterminalKind::Block,
58+
token::NonterminalKind::Block,
59+
super::ParseNtResult::Block(block),
60+
block
61+
) {
62+
return Ok($p.mk_expr(span, ExprKind::Block(block, None)));
6063
} else if let Some(path) = crate::maybe_reparse_metavar_seq!(
6164
$p,
6265
token::NonterminalKind::Path,
@@ -1584,7 +1587,7 @@ impl<'a> Parser<'a> {
15841587
} else if self.eat_keyword(kw::Loop) {
15851588
self.parse_expr_loop(label, lo)
15861589
} else if self.check_noexpect(&token::OpenDelim(Delimiter::Brace))
1587-
|| self.token.is_whole_block()
1590+
|| self.token.is_metavar_block()
15881591
{
15891592
self.parse_expr_block(label, lo, BlockCheckMode::Default)
15901593
} else if !ate_colon
@@ -2182,7 +2185,7 @@ impl<'a> Parser<'a> {
21822185
}
21832186
}
21842187

2185-
if self.token.is_whole_block() {
2188+
if self.token.is_metavar_block() {
21862189
self.sess.emit_err(errors::InvalidBlockMacroSegment {
21872190
span: self.token.span,
21882191
context: lo.to(self.token.span),
@@ -2998,7 +3001,7 @@ impl<'a> Parser<'a> {
29983001
self.token.is_keyword(kw::Do)
29993002
&& self.is_keyword_ahead(1, &[kw::Catch])
30003003
&& self
3001-
.look_ahead(2, |t| *t == token::OpenDelim(Delimiter::Brace) || t.is_whole_block())
3004+
.look_ahead(2, |t| *t == token::OpenDelim(Delimiter::Brace) || t.is_metavar_block())
30023005
&& !self.restrictions.contains(Restrictions::NO_STRUCT_LITERAL)
30033006
}
30043007

@@ -3009,7 +3012,7 @@ impl<'a> Parser<'a> {
30093012
fn is_try_block(&self) -> bool {
30103013
self.token.is_keyword(kw::Try)
30113014
&& self
3012-
.look_ahead(1, |t| *t == token::OpenDelim(Delimiter::Brace) || t.is_whole_block())
3015+
.look_ahead(1, |t| *t == token::OpenDelim(Delimiter::Brace) || t.is_metavar_block())
30133016
&& self.token.uninterpolated_span().at_least_rust_2018()
30143017
}
30153018

@@ -3029,12 +3032,12 @@ impl<'a> Parser<'a> {
30293032
// `async move {`
30303033
self.is_keyword_ahead(1, &[kw::Move])
30313034
&& self.look_ahead(2, |t| {
3032-
*t == token::OpenDelim(Delimiter::Brace) || t.is_whole_block()
3035+
*t == token::OpenDelim(Delimiter::Brace) || t.is_metavar_block()
30333036
})
30343037
) || (
30353038
// `async {`
30363039
self.look_ahead(1, |t| {
3037-
*t == token::OpenDelim(Delimiter::Brace) || t.is_whole_block()
3040+
*t == token::OpenDelim(Delimiter::Brace) || t.is_metavar_block()
30383041
})
30393042
))
30403043
}

compiler/rustc_parse/src/parser/item.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2249,7 +2249,7 @@ impl<'a> Parser<'a> {
22492249
self.expect_semi()?;
22502250
*sig_hi = self.prev_token.span;
22512251
(AttrVec::new(), None)
2252-
} else if self.check(&token::OpenDelim(Delimiter::Brace)) || self.token.is_whole_block() {
2252+
} else if self.check(&token::OpenDelim(Delimiter::Brace)) || self.token.is_metavar_block() {
22532253
self.parse_block_common(self.token.span, BlockCheckMode::Default, false)
22542254
.map(|(attrs, body)| (attrs, Some(body)))?
22552255
} else if self.token.kind == token::Eq {

compiler/rustc_parse/src/parser/mod.rs

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ pub use pat::{CommaRecoveryMode, RecoverColon, RecoverComma};
1818
pub use path::PathStyle;
1919

2020
use rustc_ast::ptr::P;
21-
use rustc_ast::token::{self, Delimiter, Nonterminal, NonterminalKind, Token, TokenKind};
21+
use rustc_ast::token::{
22+
self, Delimiter, InvisibleSource, Nonterminal, NonterminalKind, Token, TokenKind,
23+
};
2224
use rustc_ast::tokenstream::{AttributesData, DelimSpan, Spacing};
2325
use rustc_ast::tokenstream::{TokenStream, TokenTree, TokenTreeCursor};
2426
use rustc_ast::util::case::Case;
@@ -86,20 +88,6 @@ pub enum TrailingToken {
8688
MaybeComma,
8789
}
8890

89-
/// Like `maybe_whole_expr`, but for things other than expressions.
90-
#[macro_export]
91-
macro_rules! maybe_whole {
92-
($p:expr, $constructor:ident, |$x:ident| $e:expr) => {
93-
if let token::Interpolated(nt) = &$p.token.kind {
94-
if let token::$constructor(x) = &**nt {
95-
let $x = x.clone();
96-
$p.bump();
97-
return Ok($e);
98-
}
99-
}
100-
};
101-
}
102-
10391
/// Reparses an invisible-delimited sequence produced by expansion of a
10492
/// declarative macro metavariable. Will panic if called with a `self.token`
10593
/// that is not an `InvisibleSource::Metavar` invisible open delimiter.
@@ -733,8 +721,10 @@ impl<'a> Parser<'a> {
733721
fn check_inline_const(&self, dist: usize) -> bool {
734722
self.is_keyword_ahead(dist, &[kw::Const])
735723
&& self.look_ahead(dist + 1, |t| match &t.kind {
736-
token::Interpolated(nt) => matches!(**nt, token::NtBlock(..)),
737724
token::OpenDelim(Delimiter::Brace) => true,
725+
token::OpenDelim(Delimiter::Invisible(InvisibleSource::MetaVar(
726+
NonterminalKind::Block,
727+
))) => true,
738728
_ => false,
739729
})
740730
}
@@ -1227,7 +1217,7 @@ impl<'a> Parser<'a> {
12271217
// Avoid const blocks and const closures to be parsed as const items
12281218
if (self.check_const_closure() == is_closure)
12291219
&& !self
1230-
.look_ahead(1, |t| *t == token::OpenDelim(Delimiter::Brace) || t.is_whole_block())
1220+
.look_ahead(1, |t| *t == token::OpenDelim(Delimiter::Brace) || t.is_metavar_block())
12311221
&& self.eat_keyword_case(kw::Const, case)
12321222
{
12331223
Const::Yes(self.prev_token.uninterpolated_span())
@@ -1583,6 +1573,7 @@ pub enum ParseNtResult {
15831573
Tt(TokenTree),
15841574

15851575
Item(P<ast::Item>),
1576+
Block(P<ast::Block>),
15861577
Stmt(P<ast::Stmt>),
15871578
PatParam(P<ast::Pat>, /* inferred */ bool),
15881579
PatWithOr(P<ast::Pat>),

compiler/rustc_parse/src/parser/nonterminal.rs

Lines changed: 5 additions & 4 deletions
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 {
80-
NtBlock(_) | NtLifetime(_) | NtExpr(_) | NtLiteral(_) => true,
79+
NtLifetime(_) | NtExpr(_) | NtLiteral(_) => true,
8180
NtIdent(..) => false,
8281
},
8382
token::OpenDelim(Delimiter::Invisible(InvisibleSource::MetaVar(k))) => match k {
@@ -164,7 +163,9 @@ impl<'a> Parser<'a> {
164163
NonterminalKind::Block => {
165164
// While a block *expression* may have attributes (e.g. `#[my_attr] { ... }`),
166165
// the ':block' matcher does not support them
167-
NtBlock(self.collect_tokens_no_attrs(|this| this.parse_block())?)
166+
return Ok(ParseNtResult::Block(
167+
self.collect_tokens_no_attrs(|this| this.parse_block())?)
168+
)
168169
}
169170
NonterminalKind::Stmt => match self.parse_stmt(ForceCollect::Yes)? {
170171
Some(stmt) => return Ok(ParseNtResult::Stmt(P(stmt))),

compiler/rustc_parse/src/parser/stmt.rs

Lines changed: 11 additions & 3 deletions
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 ast::Label;
1313
use rustc_ast as ast;
1414
use rustc_ast::ptr::P;
@@ -517,7 +517,15 @@ impl<'a> Parser<'a> {
517517
blk_mode: BlockCheckMode,
518518
can_be_struct_literal: bool,
519519
) -> PResult<'a, (AttrVec, P<Block>)> {
520-
maybe_whole!(self, NtBlock, |x| (AttrVec::new(), x));
520+
if let Some(block) = maybe_reparse_metavar_seq!(
521+
self,
522+
NonterminalKind::Block,
523+
NonterminalKind::Block,
524+
ParseNtResult::Block(block),
525+
block
526+
) {
527+
return Ok((AttrVec::new(), block));
528+
}
521529

522530
let maybe_ident = self.prev_token.clone();
523531
self.maybe_recover_unexpected_block_label();
@@ -665,7 +673,7 @@ impl<'a> Parser<'a> {
665673
ExprKind::Path(None, ast::Path { segments, .. }) if segments.len() == 1 => {
666674
if self.token == token::Colon
667675
&& self.look_ahead(1, |token| {
668-
token.is_whole_block() || matches!(
676+
token.is_metavar_block() || matches!(
669677
token.kind,
670678
token::Ident(kw::For | kw::Loop | kw::While, false)
671679
| token::OpenDelim(Delimiter::Brace)

tests/ui/macros/stringify.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,14 @@ fn test_block() {
7777
stringify_block!({
7878
return;
7979
}),
80-
"{ return; }",
80+
"{ return ; }",
8181
);
8282
assert_eq!(
8383
stringify_block!({
8484
let _;
8585
true
8686
}),
87-
"{ let _; true }",
87+
"{ let _ ; true }",
8888
);
8989
}
9090

0 commit comments

Comments
 (0)