Skip to content

Commit e6d1e42

Browse files
committed
Auto merge of #137517 - nnethercote:rm-NtPat-NtItem-NtStmt, r=<try>
Remove `NtPat`, `NtItem`, and `NtStmt` Another part of #124141. r? `@petrochenkov`
2 parents e0be1a0 + 96229ed commit e6d1e42

26 files changed

+270
-241
lines changed

compiler/rustc_ast/src/ast_traits.rs

-6
Original file line numberDiff line numberDiff line change
@@ -199,21 +199,15 @@ impl HasTokens for Attribute {
199199
impl HasTokens for Nonterminal {
200200
fn tokens(&self) -> Option<&LazyAttrTokenStream> {
201201
match self {
202-
Nonterminal::NtItem(item) => item.tokens(),
203-
Nonterminal::NtStmt(stmt) => stmt.tokens(),
204202
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => expr.tokens(),
205-
Nonterminal::NtPat(pat) => pat.tokens(),
206203
Nonterminal::NtMeta(attr_item) => attr_item.tokens(),
207204
Nonterminal::NtPath(path) => path.tokens(),
208205
Nonterminal::NtBlock(block) => block.tokens(),
209206
}
210207
}
211208
fn tokens_mut(&mut self) -> Option<&mut Option<LazyAttrTokenStream>> {
212209
match self {
213-
Nonterminal::NtItem(item) => item.tokens_mut(),
214-
Nonterminal::NtStmt(stmt) => stmt.tokens_mut(),
215210
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => expr.tokens_mut(),
216-
Nonterminal::NtPat(pat) => pat.tokens_mut(),
217211
Nonterminal::NtMeta(attr_item) => attr_item.tokens_mut(),
218212
Nonterminal::NtPath(path) => path.tokens_mut(),
219213
Nonterminal::NtBlock(block) => block.tokens_mut(),

compiler/rustc_ast/src/mut_visit.rs

-13
Original file line numberDiff line numberDiff line change
@@ -892,20 +892,7 @@ pub fn visit_token<T: MutVisitor>(vis: &mut T, t: &mut Token) {
892892
// multiple items there....
893893
fn visit_nonterminal<T: MutVisitor>(vis: &mut T, nt: &mut token::Nonterminal) {
894894
match nt {
895-
token::NtItem(item) => visit_clobber(item, |item| {
896-
// This is probably okay, because the only visitors likely to
897-
// peek inside interpolated nodes will be renamings/markings,
898-
// which map single items to single items.
899-
vis.flat_map_item(item).expect_one("expected visitor to produce exactly one item")
900-
}),
901895
token::NtBlock(block) => vis.visit_block(block),
902-
token::NtStmt(stmt) => visit_clobber(stmt, |stmt| {
903-
// See reasoning above.
904-
stmt.map(|stmt| {
905-
vis.flat_map_stmt(stmt).expect_one("expected visitor to produce exactly one item")
906-
})
907-
}),
908-
token::NtPat(pat) => vis.visit_pat(pat),
909896
token::NtExpr(expr) => vis.visit_expr(expr),
910897
token::NtLiteral(expr) => vis.visit_expr(expr),
911898
token::NtMeta(item) => {

compiler/rustc_ast/src/token.rs

-13
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,6 @@ impl Token {
659659
| NtExpr(..)
660660
| NtLiteral(..)
661661
| NtMeta(..)
662-
| NtPat(..)
663662
| NtPath(..)
664663
),
665664
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
@@ -1072,10 +1071,7 @@ pub enum NtExprKind {
10721071
#[derive(Clone, Encodable, Decodable)]
10731072
/// For interpolation during macro expansion.
10741073
pub enum Nonterminal {
1075-
NtItem(P<ast::Item>),
10761074
NtBlock(P<ast::Block>),
1077-
NtStmt(P<ast::Stmt>),
1078-
NtPat(P<ast::Pat>),
10791075
NtExpr(P<ast::Expr>),
10801076
NtLiteral(P<ast::Expr>),
10811077
/// Stuff inside brackets for attributes
@@ -1169,10 +1165,7 @@ impl fmt::Display for NonterminalKind {
11691165
impl Nonterminal {
11701166
pub fn use_span(&self) -> Span {
11711167
match self {
1172-
NtItem(item) => item.span,
11731168
NtBlock(block) => block.span,
1174-
NtStmt(stmt) => stmt.span,
1175-
NtPat(pat) => pat.span,
11761169
NtExpr(expr) | NtLiteral(expr) => expr.span,
11771170
NtMeta(attr_item) => attr_item.span(),
11781171
NtPath(path) => path.span,
@@ -1181,10 +1174,7 @@ impl Nonterminal {
11811174

11821175
pub fn descr(&self) -> &'static str {
11831176
match self {
1184-
NtItem(..) => "item",
11851177
NtBlock(..) => "block",
1186-
NtStmt(..) => "statement",
1187-
NtPat(..) => "pattern",
11881178
NtExpr(..) => "expression",
11891179
NtLiteral(..) => "literal",
11901180
NtMeta(..) => "attribute",
@@ -1206,10 +1196,7 @@ impl PartialEq for Nonterminal {
12061196
impl fmt::Debug for Nonterminal {
12071197
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
12081198
match *self {
1209-
NtItem(..) => f.pad("NtItem(..)"),
12101199
NtBlock(..) => f.pad("NtBlock(..)"),
1211-
NtStmt(..) => f.pad("NtStmt(..)"),
1212-
NtPat(..) => f.pad("NtPat(..)"),
12131200
NtExpr(..) => f.pad("NtExpr(..)"),
12141201
NtLiteral(..) => f.pad("NtLiteral(..)"),
12151202
NtMeta(..) => f.pad("NtMeta(..)"),

compiler/rustc_ast/src/tokenstream.rs

+1-8
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,14 +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),
471-
Nonterminal::NtPat(pat) => TokenStream::from_ast(pat),
472465
Nonterminal::NtMeta(attr) => TokenStream::from_ast(attr),
473466
Nonterminal::NtPath(path) => TokenStream::from_ast(path),
474467
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => TokenStream::from_ast(expr),

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(_) => {

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};
@@ -18,7 +18,7 @@ use rustc_errors::{DiagCtxtHandle, ErrorGuaranteed, PResult};
1818
use rustc_feature::Features;
1919
use rustc_lint_defs::{BufferedEarlyLint, RegisteredTools};
2020
use rustc_parse::MACRO_ARGUMENTS;
21-
use rustc_parse::parser::Parser;
21+
use rustc_parse::parser::{ForceCollect, Parser};
2222
use rustc_session::config::CollapseMacroDebuginfo;
2323
use rustc_session::parse::ParseSess;
2424
use rustc_session::{Limit, Session};
@@ -1382,13 +1382,13 @@ pub fn parse_macro_name_and_helper_attrs(
13821382
/// If this item looks like a specific enums from `rental`, emit a fatal error.
13831383
/// See #73345 and #83125 for more details.
13841384
/// FIXME(#73933): Remove this eventually.
1385-
fn pretty_printing_compatibility_hack(item: &Item, sess: &Session) {
1385+
fn pretty_printing_compatibility_hack(item: &Item, psess: &ParseSess) {
13861386
let name = item.ident.name;
13871387
if name == sym::ProceduralMasqueradeDummyType
13881388
&& let ast::ItemKind::Enum(enum_def, _) = &item.kind
13891389
&& let [variant] = &*enum_def.variants
13901390
&& variant.ident.name == sym::Input
1391-
&& let FileName::Real(real) = sess.source_map().span_to_filename(item.ident.span)
1391+
&& let FileName::Real(real) = psess.source_map().span_to_filename(item.ident.span)
13921392
&& let Some(c) = real
13931393
.local_path()
13941394
.unwrap_or(Path::new(""))
@@ -1406,15 +1406,15 @@ fn pretty_printing_compatibility_hack(item: &Item, sess: &Session) {
14061406
};
14071407

14081408
if crate_matches {
1409-
sess.dcx().emit_fatal(errors::ProcMacroBackCompat {
1409+
psess.dcx().emit_fatal(errors::ProcMacroBackCompat {
14101410
crate_name: "rental".to_string(),
14111411
fixed_version: "0.5.6".to_string(),
14121412
});
14131413
}
14141414
}
14151415
}
14161416

1417-
pub(crate) fn ann_pretty_printing_compatibility_hack(ann: &Annotatable, sess: &Session) {
1417+
pub(crate) fn ann_pretty_printing_compatibility_hack(ann: &Annotatable, psess: &ParseSess) {
14181418
let item = match ann {
14191419
Annotatable::Item(item) => item,
14201420
Annotatable::Stmt(stmt) => match &stmt.kind {
@@ -1423,17 +1423,36 @@ pub(crate) fn ann_pretty_printing_compatibility_hack(ann: &Annotatable, sess: &S
14231423
},
14241424
_ => return,
14251425
};
1426-
pretty_printing_compatibility_hack(item, sess)
1426+
pretty_printing_compatibility_hack(item, psess)
14271427
}
14281428

1429-
pub(crate) fn nt_pretty_printing_compatibility_hack(nt: &Nonterminal, sess: &Session) {
1430-
let item = match nt {
1431-
Nonterminal::NtItem(item) => item,
1432-
Nonterminal::NtStmt(stmt) => match &stmt.kind {
1433-
ast::StmtKind::Item(item) => item,
1434-
_ => return,
1435-
},
1429+
pub(crate) fn stream_pretty_printing_compatibility_hack(
1430+
kind: MetaVarKind,
1431+
stream: &TokenStream,
1432+
psess: &ParseSess,
1433+
) {
1434+
let item = match kind {
1435+
MetaVarKind::Item => {
1436+
let mut parser = Parser::new(psess, stream.clone(), None);
1437+
// No need to collect tokens for this simple check.
1438+
parser
1439+
.parse_item(ForceCollect::No)
1440+
.expect("failed to reparse item")
1441+
.expect("an actual item")
1442+
}
1443+
MetaVarKind::Stmt => {
1444+
let mut parser = Parser::new(psess, stream.clone(), None);
1445+
// No need to collect tokens for this simple check.
1446+
let stmt = parser
1447+
.parse_stmt(ForceCollect::No)
1448+
.expect("failed to reparse")
1449+
.expect("an actual stmt");
1450+
match &stmt.kind {
1451+
ast::StmtKind::Item(item) => item.clone(),
1452+
_ => return,
1453+
}
1454+
}
14361455
_ => return,
14371456
};
1438-
pretty_printing_compatibility_hack(item, sess)
1457+
pretty_printing_compatibility_hack(&item, psess)
14391458
}

compiler/rustc_expand/src/mbe/transcribe.rs

+27-5
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;
@@ -279,9 +279,9 @@ pub(super) fn transcribe<'a>(
279279
if let Some(cur_matched) = lookup_cur_matched(ident, interp, &repeats) {
280280
// We wrap the tokens in invisible delimiters, unless they are already wrapped
281281
// in invisible delimiters with the same `MetaVarKind`. Because some proc
282-
// macros can't multiple layers of invisible delimiters of the same
282+
// macros can't handle multiple layers of invisible delimiters of the same
283283
// `MetaVarKind`. This loses some span info, though it hopefully won't matter.
284-
let mut mk_delimited = |mv_kind, mut stream: TokenStream| {
284+
let mut mk_delimited = |mk_span, mv_kind, mut stream: TokenStream| {
285285
if stream.len() == 1 {
286286
let tree = stream.iter().next().unwrap();
287287
if let TokenTree::Delimited(_, _, delim, inner) = tree
@@ -295,6 +295,7 @@ pub(super) fn transcribe<'a>(
295295
// Emit as a token stream within `Delimiter::Invisible` to maintain
296296
// parsing priorities.
297297
marker.visit_span(&mut sp);
298+
with_metavar_spans(|mspans| mspans.insert(mk_span, sp));
298299
// Both the open delim and close delim get the same span, which covers the
299300
// `$foo` in the decl macro RHS.
300301
TokenTree::Delimited(
@@ -322,12 +323,33 @@ pub(super) fn transcribe<'a>(
322323
let kind = token::NtLifetime(*ident, *is_raw);
323324
TokenTree::token_alone(kind, sp)
324325
}
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+
}
338+
MatchedSingle(ParseNtResult::Pat(pat, pat_kind)) => mk_delimited(
339+
pat.span,
340+
MetaVarKind::Pat(*pat_kind),
341+
TokenStream::from_ast(pat),
342+
),
325343
MatchedSingle(ParseNtResult::Ty(ty)) => {
326344
let is_path = matches!(&ty.kind, TyKind::Path(None, _path));
327-
mk_delimited(MetaVarKind::Ty { is_path }, TokenStream::from_ast(ty))
345+
mk_delimited(
346+
ty.span,
347+
MetaVarKind::Ty { is_path },
348+
TokenStream::from_ast(ty),
349+
)
328350
}
329351
MatchedSingle(ParseNtResult::Vis(vis)) => {
330-
mk_delimited(MetaVarKind::Vis, TokenStream::from_ast(vis))
352+
mk_delimited(vis.span, MetaVarKind::Vis, TokenStream::from_ast(vis))
331353
}
332354
MatchedSingle(ParseNtResult::Nt(nt)) => {
333355
// Other variables are emitted into the output stream as groups with

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 =

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),

0 commit comments

Comments
 (0)