Skip to content

Commit 141719f

Browse files
committed
Remove NtItem and NtStmt.
This involves replacing `nt_pretty_printing_compatibility_hack` with `stream_pretty_printing_compatibility_hack`. The handling of statements in `transcribe` is slightly different to other nonterminal kinds, due to the lack of `from_ast` implementation for empty statements. Notable test changes: - `tests/ui/proc-macro/expand-to-derive.rs`: the diff looks large but the only difference is the insertion of a single invisible-delimited group around a metavar.
1 parent 98a4878 commit 141719f

File tree

18 files changed

+194
-141
lines changed

18 files changed

+194
-141
lines changed

Diff for: compiler/rustc_ast/src/ast_traits.rs

-4
Original file line numberDiff line numberDiff line change
@@ -209,16 +209,12 @@ impl HasTokens for Attribute {
209209
impl HasTokens for Nonterminal {
210210
fn tokens(&self) -> Option<&LazyAttrTokenStream> {
211211
match self {
212-
Nonterminal::NtItem(item) => item.tokens(),
213-
Nonterminal::NtStmt(stmt) => stmt.tokens(),
214212
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => expr.tokens(),
215213
Nonterminal::NtBlock(block) => block.tokens(),
216214
}
217215
}
218216
fn tokens_mut(&mut self) -> Option<&mut Option<LazyAttrTokenStream>> {
219217
match self {
220-
Nonterminal::NtItem(item) => item.tokens_mut(),
221-
Nonterminal::NtStmt(stmt) => stmt.tokens_mut(),
222218
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => expr.tokens_mut(),
223219
Nonterminal::NtBlock(block) => block.tokens_mut(),
224220
}

Diff for: compiler/rustc_ast/src/mut_visit.rs

-12
Original file line numberDiff line numberDiff line change
@@ -895,19 +895,7 @@ pub fn visit_token<T: MutVisitor>(vis: &mut T, t: &mut Token) {
895895
// multiple items there....
896896
fn visit_nonterminal<T: MutVisitor>(vis: &mut T, nt: &mut token::Nonterminal) {
897897
match nt {
898-
token::NtItem(item) => visit_clobber(item, |item| {
899-
// This is probably okay, because the only visitors likely to
900-
// peek inside interpolated nodes will be renamings/markings,
901-
// which map single items to single items.
902-
vis.flat_map_item(item).expect_one("expected visitor to produce exactly one item")
903-
}),
904898
token::NtBlock(block) => vis.visit_block(block),
905-
token::NtStmt(stmt) => visit_clobber(stmt, |stmt| {
906-
// See reasoning above.
907-
stmt.map(|stmt| {
908-
vis.flat_map_stmt(stmt).expect_one("expected visitor to produce exactly one item")
909-
})
910-
}),
911899
token::NtExpr(expr) => vis.visit_expr(expr),
912900
token::NtLiteral(expr) => vis.visit_expr(expr),
913901
}

Diff for: compiler/rustc_ast/src/token.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,7 @@ impl Token {
870870
/// Is this a pre-parsed expression dropped into the token stream
871871
/// (which happens while parsing the result of macro expansion)?
872872
pub fn is_whole_expr(&self) -> bool {
873+
#[allow(irrefutable_let_patterns)] // FIXME: temporary
873874
if let Interpolated(nt) = &self.kind
874875
&& let NtExpr(_) | NtLiteral(_) | NtBlock(_) = &**nt
875876
{
@@ -1103,9 +1104,7 @@ pub enum NtExprKind {
11031104
#[derive(Clone, Encodable, Decodable)]
11041105
/// For interpolation during macro expansion.
11051106
pub enum Nonterminal {
1106-
NtItem(P<ast::Item>),
11071107
NtBlock(P<ast::Block>),
1108-
NtStmt(P<ast::Stmt>),
11091108
NtExpr(P<ast::Expr>),
11101109
NtLiteral(P<ast::Expr>),
11111110
}
@@ -1196,18 +1195,14 @@ impl fmt::Display for NonterminalKind {
11961195
impl Nonterminal {
11971196
pub fn use_span(&self) -> Span {
11981197
match self {
1199-
NtItem(item) => item.span,
12001198
NtBlock(block) => block.span,
1201-
NtStmt(stmt) => stmt.span,
12021199
NtExpr(expr) | NtLiteral(expr) => expr.span,
12031200
}
12041201
}
12051202

12061203
pub fn descr(&self) -> &'static str {
12071204
match self {
1208-
NtItem(..) => "item",
12091205
NtBlock(..) => "block",
1210-
NtStmt(..) => "statement",
12111206
NtExpr(..) => "expression",
12121207
NtLiteral(..) => "literal",
12131208
}
@@ -1227,9 +1222,7 @@ impl PartialEq for Nonterminal {
12271222
impl fmt::Debug for Nonterminal {
12281223
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
12291224
match *self {
1230-
NtItem(..) => f.pad("NtItem(..)"),
12311225
NtBlock(..) => f.pad("NtBlock(..)"),
1232-
NtStmt(..) => f.pad("NtStmt(..)"),
12331226
NtExpr(..) => f.pad("NtExpr(..)"),
12341227
NtLiteral(..) => f.pad("NtLiteral(..)"),
12351228
}

Diff for: compiler/rustc_ast/src/tokenstream.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use rustc_macros::{Decodable, Encodable, HashStable_Generic};
2323
use rustc_serialize::{Decodable, Encodable};
2424
use rustc_span::{DUMMY_SP, Span, SpanDecoder, SpanEncoder, Symbol, sym};
2525

26-
use crate::ast::{AttrStyle, StmtKind};
26+
use crate::ast::AttrStyle;
2727
use crate::ast_traits::{HasAttrs, HasTokens};
2828
use crate::token::{self, Delimiter, InvisibleOrigin, Nonterminal, Token, TokenKind};
2929
use crate::{AttrVec, Attribute};
@@ -461,13 +461,7 @@ impl TokenStream {
461461

462462
pub fn from_nonterminal_ast(nt: &Nonterminal) -> TokenStream {
463463
match nt {
464-
Nonterminal::NtItem(item) => TokenStream::from_ast(item),
465464
Nonterminal::NtBlock(block) => TokenStream::from_ast(block),
466-
Nonterminal::NtStmt(stmt) if let StmtKind::Empty = stmt.kind => {
467-
// FIXME: Properly collect tokens for empty statements.
468-
TokenStream::token_alone(token::Semi, stmt.span)
469-
}
470-
Nonterminal::NtStmt(stmt) => TokenStream::from_ast(stmt),
471465
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => TokenStream::from_ast(expr),
472466
}
473467
}

Diff for: compiler/rustc_builtin_macros/src/cfg_eval.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,9 @@ impl CfgEval<'_> {
140140
Annotatable::ForeignItem(self.flat_map_foreign_item(item).pop().unwrap())
141141
}
142142
Annotatable::Stmt(_) => {
143-
let stmt =
144-
parser.parse_stmt_without_recovery(false, ForceCollect::Yes)?.unwrap();
143+
let stmt = parser
144+
.parse_stmt_without_recovery(false, ForceCollect::Yes, false)?
145+
.unwrap();
145146
Annotatable::Stmt(P(self.flat_map_stmt(stmt).pop().unwrap()))
146147
}
147148
Annotatable::Expr(_) => {

Diff for: compiler/rustc_expand/src/base.rs

+34-15
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::sync::Arc;
77

88
use rustc_ast::attr::{AttributeExt, MarkedAttrs};
99
use rustc_ast::ptr::P;
10-
use rustc_ast::token::Nonterminal;
10+
use rustc_ast::token::MetaVarKind;
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};
@@ -19,7 +19,7 @@ use rustc_feature::Features;
1919
use rustc_hir as hir;
2020
use rustc_lint_defs::{BufferedEarlyLint, RegisteredTools};
2121
use rustc_parse::MACRO_ARGUMENTS;
22-
use rustc_parse::parser::Parser;
22+
use rustc_parse::parser::{ForceCollect, Parser};
2323
use rustc_session::config::CollapseMacroDebuginfo;
2424
use rustc_session::parse::ParseSess;
2525
use rustc_session::{Limit, Session};
@@ -1405,13 +1405,13 @@ pub fn parse_macro_name_and_helper_attrs(
14051405
/// If this item looks like a specific enums from `rental`, emit a fatal error.
14061406
/// See #73345 and #83125 for more details.
14071407
/// FIXME(#73933): Remove this eventually.
1408-
fn pretty_printing_compatibility_hack(item: &Item, sess: &Session) {
1408+
fn pretty_printing_compatibility_hack(item: &Item, psess: &ParseSess) {
14091409
let name = item.ident.name;
14101410
if name == sym::ProceduralMasqueradeDummyType
14111411
&& let ast::ItemKind::Enum(enum_def, _) = &item.kind
14121412
&& let [variant] = &*enum_def.variants
14131413
&& variant.ident.name == sym::Input
1414-
&& let FileName::Real(real) = sess.source_map().span_to_filename(item.ident.span)
1414+
&& let FileName::Real(real) = psess.source_map().span_to_filename(item.ident.span)
14151415
&& let Some(c) = real
14161416
.local_path()
14171417
.unwrap_or(Path::new(""))
@@ -1429,15 +1429,15 @@ fn pretty_printing_compatibility_hack(item: &Item, sess: &Session) {
14291429
};
14301430

14311431
if crate_matches {
1432-
sess.dcx().emit_fatal(errors::ProcMacroBackCompat {
1432+
psess.dcx().emit_fatal(errors::ProcMacroBackCompat {
14331433
crate_name: "rental".to_string(),
14341434
fixed_version: "0.5.6".to_string(),
14351435
});
14361436
}
14371437
}
14381438
}
14391439

1440-
pub(crate) fn ann_pretty_printing_compatibility_hack(ann: &Annotatable, sess: &Session) {
1440+
pub(crate) fn ann_pretty_printing_compatibility_hack(ann: &Annotatable, psess: &ParseSess) {
14411441
let item = match ann {
14421442
Annotatable::Item(item) => item,
14431443
Annotatable::Stmt(stmt) => match &stmt.kind {
@@ -1446,17 +1446,36 @@ pub(crate) fn ann_pretty_printing_compatibility_hack(ann: &Annotatable, sess: &S
14461446
},
14471447
_ => return,
14481448
};
1449-
pretty_printing_compatibility_hack(item, sess)
1449+
pretty_printing_compatibility_hack(item, psess)
14501450
}
14511451

1452-
pub(crate) fn nt_pretty_printing_compatibility_hack(nt: &Nonterminal, sess: &Session) {
1453-
let item = match nt {
1454-
Nonterminal::NtItem(item) => item,
1455-
Nonterminal::NtStmt(stmt) => match &stmt.kind {
1456-
ast::StmtKind::Item(item) => item,
1457-
_ => return,
1458-
},
1452+
pub(crate) fn stream_pretty_printing_compatibility_hack(
1453+
kind: MetaVarKind,
1454+
stream: &TokenStream,
1455+
psess: &ParseSess,
1456+
) {
1457+
let item = match kind {
1458+
MetaVarKind::Item => {
1459+
let mut parser = Parser::new(psess, stream.clone(), None);
1460+
// No need to collect tokens for this simple check.
1461+
parser
1462+
.parse_item(ForceCollect::No)
1463+
.expect("failed to reparse item")
1464+
.expect("an actual item")
1465+
}
1466+
MetaVarKind::Stmt => {
1467+
let mut parser = Parser::new(psess, stream.clone(), None);
1468+
// No need to collect tokens for this simple check.
1469+
let stmt = parser
1470+
.parse_stmt(ForceCollect::No)
1471+
.expect("failed to reparse")
1472+
.expect("an actual stmt");
1473+
match &stmt.kind {
1474+
ast::StmtKind::Item(item) => item.clone(),
1475+
_ => return,
1476+
}
1477+
}
14591478
_ => return,
14601479
};
1461-
pretty_printing_compatibility_hack(item, sess)
1480+
pretty_printing_compatibility_hack(&item, psess)
14621481
}

Diff for: compiler/rustc_expand/src/mbe/transcribe.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_ast::token::{
77
TokenKind,
88
};
99
use rustc_ast::tokenstream::{DelimSpacing, DelimSpan, Spacing, TokenStream, TokenTree};
10-
use rustc_ast::{ExprKind, TyKind};
10+
use rustc_ast::{ExprKind, StmtKind, TyKind};
1111
use rustc_data_structures::fx::FxHashMap;
1212
use rustc_errors::{Diag, DiagCtxtHandle, PResult, pluralize};
1313
use rustc_parse::lexer::nfc_normalize;
@@ -323,6 +323,18 @@ pub(super) fn transcribe<'a>(
323323
let kind = token::NtLifetime(*ident, *is_raw);
324324
TokenTree::token_alone(kind, sp)
325325
}
326+
MatchedSingle(ParseNtResult::Item(item)) => {
327+
mk_delimited(item.span, MetaVarKind::Item, TokenStream::from_ast(item))
328+
}
329+
MatchedSingle(ParseNtResult::Stmt(stmt)) => {
330+
let stream = if let StmtKind::Empty = stmt.kind {
331+
// FIXME: Properly collect tokens for empty statements.
332+
TokenStream::token_alone(token::Semi, stmt.span)
333+
} else {
334+
TokenStream::from_ast(stmt)
335+
};
336+
mk_delimited(stmt.span, MetaVarKind::Stmt, stream)
337+
}
326338
MatchedSingle(ParseNtResult::Pat(pat, pat_kind)) => mk_delimited(
327339
pat.span,
328340
MetaVarKind::Pat(*pat_kind),

Diff for: compiler/rustc_expand/src/proc_macro.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ impl MultiItemModifier for DeriveProcMacro {
122122
// We had a lint for a long time, but now we just emit a hard error.
123123
// Eventually we might remove the special case hard error check
124124
// altogether. See #73345.
125-
crate::base::ann_pretty_printing_compatibility_hack(&item, &ecx.sess);
125+
crate::base::ann_pretty_printing_compatibility_hack(&item, &ecx.sess.psess);
126126
let input = item.to_tokens();
127127
let stream = {
128128
let _timer =

Diff for: compiler/rustc_expand/src/proc_macro_server.rs

+18-13
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,25 @@ impl FromInternal<(TokenStream, &mut Rustc<'_, '_>)> for Vec<TokenTree<TokenStre
115115

116116
while let Some(tree) = iter.next() {
117117
let (Token { kind, span }, joint) = match tree.clone() {
118-
tokenstream::TokenTree::Delimited(span, _, delim, tts) => {
119-
let delimiter = pm::Delimiter::from_internal(delim);
118+
tokenstream::TokenTree::Delimited(span, _, delim, stream) => {
119+
// We used to have an alternative behaviour for crates that
120+
// needed it: a hack used to pass AST fragments to
121+
// attribute and derive macros as a single nonterminal
122+
// token instead of a token stream. Such token needs to be
123+
// "unwrapped" and not represented as a delimited group. We
124+
// had a lint for a long time, but now we just emit a hard
125+
// error. Eventually we might remove the special case hard
126+
// error check altogether. See #73345.
127+
if let Delimiter::Invisible(InvisibleOrigin::MetaVar(kind)) = delim {
128+
crate::base::stream_pretty_printing_compatibility_hack(
129+
kind,
130+
&stream,
131+
rustc.psess(),
132+
);
133+
}
120134
trees.push(TokenTree::Group(Group {
121-
delimiter,
122-
stream: Some(tts),
135+
delimiter: pm::Delimiter::from_internal(delim),
136+
stream: Some(stream),
123137
span: DelimSpan {
124138
open: span.open,
125139
close: span.close,
@@ -279,15 +293,6 @@ impl FromInternal<(TokenStream, &mut Rustc<'_, '_>)> for Vec<TokenTree<TokenStre
279293

280294
Interpolated(nt) => {
281295
let stream = TokenStream::from_nonterminal_ast(&nt);
282-
// We used to have an alternative behaviour for crates that
283-
// needed it: a hack used to pass AST fragments to
284-
// attribute and derive macros as a single nonterminal
285-
// token instead of a token stream. Such token needs to be
286-
// "unwrapped" and not represented as a delimited group. We
287-
// had a lint for a long time, but now we just emit a hard
288-
// error. Eventually we might remove the special case hard
289-
// error check altogether. See #73345.
290-
crate::base::nt_pretty_printing_compatibility_hack(&nt, rustc.ecx.sess);
291296
trees.push(TokenTree::Group(Group {
292297
delimiter: pm::Delimiter::None,
293298
stream: Some(stream),

Diff for: compiler/rustc_parse/src/parser/expr.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1357,7 +1357,6 @@ impl<'a> Parser<'a> {
13571357
self.bump();
13581358
return Ok(self.mk_expr(self.prev_token.span, ExprKind::Block(block, None)));
13591359
}
1360-
_ => {}
13611360
};
13621361
} else if let Some(path) = self.eat_metavar_seq(MetaVarKind::Path, |this| {
13631362
this.collect_tokens_no_attrs(|this| this.parse_path(PathStyle::Type))
@@ -3046,7 +3045,7 @@ impl<'a> Parser<'a> {
30463045
}
30473046

30483047
self.restore_snapshot(pre_pat_snapshot);
3049-
match self.parse_stmt_without_recovery(true, ForceCollect::No) {
3048+
match self.parse_stmt_without_recovery(true, ForceCollect::No, false) {
30503049
// Consume statements for as long as possible.
30513050
Ok(Some(stmt)) => {
30523051
stmts.push(stmt);

Diff for: compiler/rustc_parse/src/parser/item.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ use super::diagnostics::{ConsumeClosingDelim, dummy_arg};
2121
use super::ty::{AllowPlus, RecoverQPath, RecoverReturnSign};
2222
use super::{
2323
AttrWrapper, ExpKeywordPair, ExpTokenPair, FollowedByType, ForceCollect, Parser, PathStyle,
24-
Trailing, UsePreAttrPos,
24+
Recovered, Trailing, UsePreAttrPos,
2525
};
2626
use crate::errors::{self, MacroExpandsToAdtField};
27-
use crate::{exp, fluent_generated as fluent, maybe_whole};
27+
use crate::{exp, fluent_generated as fluent};
2828

2929
impl<'a> Parser<'a> {
3030
/// Parses a source module as a crate. This is the main entry point for the parser.
@@ -142,10 +142,13 @@ impl<'a> Parser<'a> {
142142
fn_parse_mode: FnParseMode,
143143
force_collect: ForceCollect,
144144
) -> PResult<'a, Option<Item>> {
145-
maybe_whole!(self, NtItem, |item| {
145+
if let Some(item) =
146+
self.eat_metavar_seq(MetaVarKind::Item, |this| this.parse_item(ForceCollect::Yes))
147+
{
148+
let mut item = item.expect("an actual item");
146149
attrs.prepend_to_nt_inner(&mut item.attrs);
147-
Some(item.into_inner())
148-
});
150+
return Ok(Some(item.into_inner()));
151+
}
149152

150153
self.collect_tokens(None, attrs, force_collect, |this, mut attrs| {
151154
let lo = this.token.span;

Diff for: compiler/rustc_parse/src/parser/mod.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -1076,10 +1076,12 @@ impl<'a> Parser<'a> {
10761076
let initial_semicolon = self.token.span;
10771077

10781078
while self.eat(exp!(Semi)) {
1079-
let _ = self.parse_stmt_without_recovery(false, ForceCollect::No).unwrap_or_else(|e| {
1080-
e.cancel();
1081-
None
1082-
});
1079+
let _ = self
1080+
.parse_stmt_without_recovery(false, ForceCollect::No, false)
1081+
.unwrap_or_else(|e| {
1082+
e.cancel();
1083+
None
1084+
});
10831085
}
10841086

10851087
expect_err
@@ -1746,6 +1748,8 @@ pub enum ParseNtResult {
17461748
Tt(TokenTree),
17471749
Ident(Ident, IdentIsRaw),
17481750
Lifetime(Ident, IdentIsRaw),
1751+
Item(P<ast::Item>),
1752+
Stmt(P<ast::Stmt>),
17491753
Pat(P<ast::Pat>, NtPatKind),
17501754
Ty(P<ast::Ty>),
17511755
Meta(P<ast::AttrItem>),

0 commit comments

Comments
 (0)