Skip to content

Commit aaa2d47

Browse files
committed
Auto merge of #138083 - nnethercote:rm-NtItem-NtStmt, r=petrochenkov
Remove `NtItem` and `NtStmt` Another piece of #124141. r? `@petrochenkov`
2 parents 0998d40 + 3378ee1 commit aaa2d47

File tree

49 files changed

+269
-141
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+269
-141
lines changed

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
}

compiler/rustc_ast/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
77
// tidy-alphabetical-start
88
#![allow(internal_features)]
9+
#![cfg_attr(doc, recursion_limit = "256")] // FIXME(nnethercote): will be removed by #124141
910
#![doc(
1011
html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/",
1112
test(attr(deny(warnings)))

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
}

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
}

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
}

compiler/rustc_ast_lowering/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
3333
// tidy-alphabetical-start
3434
#![allow(internal_features)]
35+
#![cfg_attr(doc, recursion_limit = "256")] // FIXME(nnethercote): will be removed by #124141
3536
#![doc(rust_logo)]
3637
#![feature(assert_matches)]
3738
#![feature(box_patterns)]

compiler/rustc_attr_parsing/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
7878
// tidy-alphabetical-start
7979
#![allow(internal_features)]
80+
#![cfg_attr(doc, recursion_limit = "256")] // FIXME(nnethercote): will be removed by #124141
8081
#![doc(rust_logo)]
8182
#![feature(let_chains)]
8283
#![feature(rustdoc_internals)]

compiler/rustc_borrowck/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
// tidy-alphabetical-start
44
#![allow(internal_features)]
5+
#![cfg_attr(doc, recursion_limit = "256")] // FIXME(nnethercote): will be removed by #124141
56
#![doc(rust_logo)]
67
#![feature(assert_matches)]
78
#![feature(box_patterns)]

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_builtin_macros/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#![allow(internal_features)]
66
#![allow(rustc::diagnostic_outside_of_impl)]
77
#![allow(rustc::untranslatable_diagnostic)]
8+
#![cfg_attr(doc, recursion_limit = "256")] // FIXME(nnethercote): will be removed by #124141
89
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
910
#![doc(rust_logo)]
1011
#![feature(assert_matches)]

compiler/rustc_codegen_ssa/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#![allow(internal_features)]
33
#![allow(rustc::diagnostic_outside_of_impl)]
44
#![allow(rustc::untranslatable_diagnostic)]
5+
#![cfg_attr(doc, recursion_limit = "256")] // FIXME(nnethercote): will be removed by #124141
56
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
67
#![doc(rust_logo)]
78
#![feature(assert_matches)]

compiler/rustc_const_eval/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// tidy-alphabetical-start
22
#![allow(internal_features)]
33
#![allow(rustc::diagnostic_outside_of_impl)]
4+
#![cfg_attr(doc, recursion_limit = "256")] // FIXME(nnethercote): will be removed by #124141
45
#![doc(rust_logo)]
56
#![feature(assert_matches)]
67
#![feature(box_patterns)]

compiler/rustc_driver/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
// tidy-alphabetical-start
55
#![allow(internal_features)]
6+
#![cfg_attr(doc, recursion_limit = "256")] // FIXME(nnethercote): will be removed by #124141
67
#![doc(rust_logo)]
78
#![feature(rustdoc_internals)]
89
// tidy-alphabetical-end

compiler/rustc_driver_impl/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// tidy-alphabetical-start
88
#![allow(internal_features)]
99
#![allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
10+
#![cfg_attr(doc, recursion_limit = "256")] // FIXME(nnethercote): will be removed by #124141
1011
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
1112
#![doc(rust_logo)]
1213
#![feature(decl_macro)]

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
}

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

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

+36-13
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,43 @@ 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, _, mut delim, mut 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+
}
134+
135+
// In `mk_delimited` we avoid nesting invisible delimited
136+
// of the same `MetaVarKind`. Here we do the same but
137+
// ignore the `MetaVarKind` because it is discarded when we
138+
// convert it to a `Group`.
139+
while let Delimiter::Invisible(InvisibleOrigin::MetaVar(_)) = delim {
140+
if stream.len() == 1
141+
&& let tree = stream.iter().next().unwrap()
142+
&& let tokenstream::TokenTree::Delimited(_, _, delim2, stream2) = tree
143+
&& let Delimiter::Invisible(InvisibleOrigin::MetaVar(_)) = delim2
144+
{
145+
delim = *delim2;
146+
stream = stream2.clone();
147+
} else {
148+
break;
149+
}
150+
}
151+
120152
trees.push(TokenTree::Group(Group {
121-
delimiter,
122-
stream: Some(tts),
153+
delimiter: pm::Delimiter::from_internal(delim),
154+
stream: Some(stream),
123155
span: DelimSpan {
124156
open: span.open,
125157
close: span.close,
@@ -279,15 +311,6 @@ impl FromInternal<(TokenStream, &mut Rustc<'_, '_>)> for Vec<TokenTree<TokenStre
279311

280312
Interpolated(nt) => {
281313
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);
291314
trees.push(TokenTree::Group(Group {
292315
delimiter: pm::Delimiter::None,
293316
stream: Some(stream),

compiler/rustc_hir/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
55
// tidy-alphabetical-start
66
#![allow(internal_features)]
7+
#![cfg_attr(doc, recursion_limit = "256")] // FIXME(nnethercote): will be removed by #124141
78
#![feature(associated_type_defaults)]
89
#![feature(box_patterns)]
910
#![feature(closure_track_caller)]

0 commit comments

Comments
 (0)