Skip to content

Commit 06ff446

Browse files
committed
Remove NtStmt.
This means `nt_pretty_printing_compatibility_hack` can also be removed. The handling of statements in `transcribe` is slightly different to other nonterminal kinds, due to the lack of `from_ast` implementation for empty statements.
1 parent 1a07e0c commit 06ff446

File tree

15 files changed

+73
-80
lines changed

15 files changed

+73
-80
lines changed

compiler/rustc_ast/src/ast_traits.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,6 @@ impl HasTokens for Attribute {
231231
impl HasTokens for Nonterminal {
232232
fn tokens(&self) -> Option<&LazyAttrTokenStream> {
233233
match self {
234-
Nonterminal::NtStmt(stmt) => stmt.tokens(),
235234
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => expr.tokens(),
236235
Nonterminal::NtMeta(attr_item) => attr_item.tokens(),
237236
Nonterminal::NtPath(path) => path.tokens(),
@@ -241,7 +240,6 @@ impl HasTokens for Nonterminal {
241240
}
242241
fn tokens_mut(&mut self) -> Option<&mut Option<LazyAttrTokenStream>> {
243242
match self {
244-
Nonterminal::NtStmt(stmt) => stmt.tokens_mut(),
245243
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => expr.tokens_mut(),
246244
Nonterminal::NtMeta(attr_item) => attr_item.tokens_mut(),
247245
Nonterminal::NtPath(path) => path.tokens_mut(),

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -791,12 +791,6 @@ pub fn visit_token<T: MutVisitor>(t: &mut Token, vis: &mut T) {
791791
pub fn visit_nonterminal<T: MutVisitor>(nt: &mut token::Nonterminal, vis: &mut T) {
792792
match nt {
793793
token::NtBlock(block) => vis.visit_block(block),
794-
token::NtStmt(stmt) => visit_clobber(stmt, |stmt| {
795-
// See reasoning above.
796-
stmt.map(|stmt| {
797-
vis.flat_map_stmt(stmt).expect_one("expected visitor to produce exactly one item")
798-
})
799-
}),
800794
token::NtExpr(expr) => vis.visit_expr(expr),
801795
token::NtIdent(ident, _is_raw) => vis.visit_ident(ident),
802796
token::NtLifetime(ident) => vis.visit_ident(ident),

compiler/rustc_ast/src/token.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -843,7 +843,6 @@ impl PartialEq<TokenKind> for Token {
843843
/// For interpolation during macro expansion.
844844
pub enum Nonterminal {
845845
NtBlock(P<ast::Block>),
846-
NtStmt(P<ast::Stmt>),
847846
NtExpr(P<ast::Expr>),
848847
NtIdent(Ident, /* is_raw */ bool),
849848
NtLifetime(Ident),
@@ -935,7 +934,6 @@ impl Nonterminal {
935934
pub fn span(&self) -> Span {
936935
match self {
937936
NtBlock(block) => block.span,
938-
NtStmt(stmt) => stmt.span,
939937
NtExpr(expr) | NtLiteral(expr) => expr.span,
940938
NtIdent(ident, _) | NtLifetime(ident) => ident.span,
941939
NtMeta(attr_item) => attr_item.span(),
@@ -964,7 +962,6 @@ impl fmt::Debug for Nonterminal {
964962
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
965963
match *self {
966964
NtBlock(..) => f.pad("NtBlock(..)"),
967-
NtStmt(..) => f.pad("NtStmt(..)"),
968965
NtExpr(..) => f.pad("NtExpr(..)"),
969966
NtIdent(..) => f.pad("NtIdent(..)"),
970967
NtLiteral(..) => f.pad("NtLiteral(..)"),

compiler/rustc_ast/src/tokenstream.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
//! and a borrowed `TokenStream` is sufficient to build an owned `TokenStream` without taking
1414
//! ownership of the original.
1515
16-
use crate::ast::{AttrStyle, StmtKind};
16+
use crate::ast::AttrStyle;
1717
use crate::ast_traits::{HasAttrs, HasSpan, HasTokens};
1818
use crate::token::{self, Delimiter, InvisibleSource, Nonterminal, Token, TokenKind};
1919
use crate::AttrVec;
@@ -464,11 +464,6 @@ impl TokenStream {
464464
TokenStream::token_alone(token::Lifetime(ident.name), ident.span)
465465
}
466466
Nonterminal::NtBlock(block) => TokenStream::from_ast(block),
467-
Nonterminal::NtStmt(stmt) if let StmtKind::Empty = stmt.kind => {
468-
// FIXME: Properly collect tokens for empty statements.
469-
TokenStream::token_alone(token::Semi, stmt.span)
470-
}
471-
Nonterminal::NtStmt(stmt) => TokenStream::from_ast(stmt),
472467
Nonterminal::NtMeta(attr) => TokenStream::from_ast(attr),
473468
Nonterminal::NtPath(path) => TokenStream::from_ast(path),
474469
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => TokenStream::from_ast(expr),

compiler/rustc_ast_pretty/src/pprust/state.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,6 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
735735
token::NtMeta(e) => self.attr_item_to_string(e),
736736
token::NtPath(e) => self.path_to_string(e),
737737
token::NtBlock(e) => self.block_to_string(e),
738-
token::NtStmt(e) => self.stmt_to_string(e),
739738
token::NtIdent(e, is_raw) => IdentPrinter::for_ast_ident(*e, *is_raw).to_string(),
740739
token::NtLifetime(e) => e.to_string(),
741740
token::NtLiteral(e) => self.expr_to_string(e),

compiler/rustc_expand/src/base.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::module::DirOwnership;
77
use rustc_ast::attr::MarkedAttrs;
88
use rustc_ast::mut_visit::DummyAstNode;
99
use rustc_ast::ptr::P;
10-
use rustc_ast::token::{self, Nonterminal, NonterminalKind};
10+
use rustc_ast::token::{self, NonterminalKind};
1111
use rustc_ast::tokenstream::TokenStream;
1212
use rustc_ast::visit::{AssocCtxt, Visitor};
1313
use rustc_ast::{self as ast, AttrVec, Attribute, HasAttrs, Item, NodeId, PatKind};
@@ -1496,17 +1496,6 @@ pub(crate) fn ann_pretty_printing_compatibility_hack(ann: &Annotatable, sess: &P
14961496
pretty_printing_compatibility_hack(item, sess)
14971497
}
14981498

1499-
pub(crate) fn nt_pretty_printing_compatibility_hack(nt: &Nonterminal, sess: &ParseSess) -> bool {
1500-
let item = match nt {
1501-
Nonterminal::NtStmt(stmt) => match &stmt.kind {
1502-
ast::StmtKind::Item(item) => item,
1503-
_ => return false,
1504-
},
1505-
_ => return false,
1506-
};
1507-
pretty_printing_compatibility_hack(item, sess)
1508-
}
1509-
15101499
pub(crate) fn stream_pretty_printing_compatibility_hack(
15111500
kind: NonterminalKind,
15121501
stream: &TokenStream,
@@ -1520,6 +1509,17 @@ pub(crate) fn stream_pretty_printing_compatibility_hack(
15201509
};
15211510
item
15221511
}
1512+
NonterminalKind::Stmt => {
1513+
// njn: reparsing and then checking for StmtKind::Item sucks, hmm
1514+
let mut parser = parser::Parser::new(sess, stream.clone(), None);
1515+
let Ok(parser::ParseNtResult::Stmt(stmt)) = parser.parse_nonterminal(kind) else {
1516+
panic!("failed to reparse");
1517+
};
1518+
match &stmt.kind {
1519+
ast::StmtKind::Item(item) => item.clone(),
1520+
_ => return false,
1521+
}
1522+
}
15231523
_ => return false,
15241524
};
15251525
pretty_printing_compatibility_hack(&item, sess)

compiler/rustc_expand/src/mbe/transcribe.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::mbe::{self, MetaVarExpr};
88
use rustc_ast::mut_visit::{self, MutVisitor};
99
use rustc_ast::token::{self, Delimiter, InvisibleSource, NonterminalKind, Token, TokenKind};
1010
use rustc_ast::tokenstream::{DelimSpan, Spacing, TokenStream, TokenTree};
11+
use rustc_ast::StmtKind;
1112
use rustc_data_structures::fx::FxHashMap;
1213
use rustc_errors::{pluralize, PResult};
1314
use rustc_errors::{DiagnosticBuilder, ErrorGuaranteed};
@@ -235,6 +236,15 @@ pub(super) fn transcribe<'a>(
235236
MatchedSingle(ParseNtResult::Item(ref item)) => {
236237
mk_delimited(NonterminalKind::Item, TokenStream::from_ast(item))
237238
}
239+
MatchedSingle(ParseNtResult::Stmt(ref stmt)) => {
240+
let stream = if let StmtKind::Empty = stmt.kind {
241+
// FIXME: Properly collect tokens for empty statements.
242+
TokenStream::token_alone(token::Semi, stmt.span)
243+
} else {
244+
TokenStream::from_ast(stmt)
245+
};
246+
mk_delimited(NonterminalKind::Stmt, stream)
247+
}
238248
MatchedSingle(ParseNtResult::PatParam(ref pat, inferred)) => mk_delimited(
239249
NonterminalKind::PatParam { inferred: *inferred },
240250
TokenStream::from_ast(pat),

compiler/rustc_expand/src/proc_macro.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ use crate::proc_macro_server;
44

55
use rustc_ast as ast;
66
use rustc_ast::ptr::P;
7-
use rustc_ast::token;
87
use rustc_ast::token::{Delimiter, InvisibleSource, NonterminalKind};
98
use rustc_ast::tokenstream::{DelimSpan, TokenStream};
10-
use rustc_data_structures::sync::Lrc;
119
use rustc_errors::ErrorGuaranteed;
1210
use rustc_parse::parser::ForceCollect;
1311
use rustc_session::config::ProcMacroExecutionStrategy;
@@ -122,16 +120,18 @@ impl MultiItemModifier for DeriveProcMacro {
122120
let is_stmt = matches!(item, Annotatable::Stmt(..));
123121
let hack = crate::base::ann_pretty_printing_compatibility_hack(&item, &ecx.sess.parse_sess);
124122
let input = if hack {
123+
let delim_span = DelimSpan::from_single(DUMMY_SP);
125124
match item {
126125
Annotatable::Item(item) => TokenStream::delimited(
127-
DelimSpan::from_single(DUMMY_SP),
126+
delim_span,
128127
Delimiter::Invisible(InvisibleSource::MetaVar(NonterminalKind::Item)),
129128
TokenStream::from_ast(&item),
130129
),
131-
Annotatable::Stmt(stmt) => {
132-
let nt = token::NtStmt(stmt);
133-
TokenStream::token_alone(token::Interpolated(Lrc::new(nt)), DUMMY_SP)
134-
}
130+
Annotatable::Stmt(stmt) => TokenStream::delimited(
131+
delim_span,
132+
Delimiter::Invisible(InvisibleSource::MetaVar(NonterminalKind::Stmt)),
133+
TokenStream::from_ast(&stmt),
134+
),
135135
_ => unreachable!(),
136136
}
137137
} else {

compiler/rustc_expand/src/proc_macro_server.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -246,17 +246,12 @@ impl FromInternal<(TokenStream, &mut Rustc<'_, '_>)> for Vec<TokenTree<TokenStre
246246
}
247247

248248
Interpolated(nt) => {
249-
// See the "hack" comment above.
250249
let stream = TokenStream::from_nonterminal_ast(&nt);
251-
if crate::base::nt_pretty_printing_compatibility_hack(&nt, rustc.sess()) {
252-
trees.extend(Self::from_internal((stream, rustc)));
253-
} else {
254-
trees.push(TokenTree::Group(Group {
255-
delimiter: pm::Delimiter::None,
256-
stream: Some(stream),
257-
span: DelimSpan::from_single(span),
258-
}))
259-
}
250+
trees.push(TokenTree::Group(Group {
251+
delimiter: pm::Delimiter::None,
252+
stream: Some(stream),
253+
span: DelimSpan::from_single(span),
254+
}))
260255
}
261256

262257
OpenDelim(..) | CloseDelim(..) => unreachable!(),

compiler/rustc_parse/src/parser/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1583,6 +1583,7 @@ pub enum ParseNtResult {
15831583
Tt(TokenTree),
15841584

15851585
Item(P<ast::Item>),
1586+
Stmt(P<ast::Stmt>),
15861587
PatParam(P<ast::Pat>, /* inferred */ bool),
15871588
PatWithOr(P<ast::Pat>),
15881589
Ty(P<ast::Ty>),

0 commit comments

Comments
 (0)