Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 78b3a8f

Browse files
committed
Remove NtPat.
XXX: tests/ui/macros/trace_faulty_macros.rs regresses because I haven't fixed `expected_expression_found` properly yet.
1 parent 191fc6f commit 78b3a8f

16 files changed

+70
-49
lines changed

compiler/rustc_ast/src/ast_traits.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,6 @@ impl HasTokens for Nonterminal {
234234
Nonterminal::NtItem(item) => item.tokens(),
235235
Nonterminal::NtStmt(stmt) => stmt.tokens(),
236236
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => expr.tokens(),
237-
Nonterminal::NtPat(pat) => pat.tokens(),
238237
Nonterminal::NtMeta(attr_item) => attr_item.tokens(),
239238
Nonterminal::NtPath(path) => path.tokens(),
240239
Nonterminal::NtBlock(block) => block.tokens(),
@@ -245,7 +244,6 @@ impl HasTokens for Nonterminal {
245244
Nonterminal::NtItem(item) => item.tokens_mut(),
246245
Nonterminal::NtStmt(stmt) => stmt.tokens_mut(),
247246
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => expr.tokens_mut(),
248-
Nonterminal::NtPat(pat) => pat.tokens_mut(),
249247
Nonterminal::NtMeta(attr_item) => attr_item.tokens_mut(),
250248
Nonterminal::NtPath(path) => path.tokens_mut(),
251249
Nonterminal::NtBlock(block) => block.tokens_mut(),

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -832,7 +832,6 @@ fn visit_nonterminal<T: MutVisitor>(nt: &mut token::Nonterminal, vis: &mut T) {
832832
vis.flat_map_stmt(stmt).expect_one("expected visitor to produce exactly one item")
833833
})
834834
}),
835-
token::NtPat(pat) => vis.visit_pat(pat),
836835
token::NtExpr(expr) => vis.visit_expr(expr),
837836
token::NtLiteral(expr) => vis.visit_expr(expr),
838837
token::NtMeta(item) => {

compiler/rustc_ast/src/token.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,6 @@ impl Token {
563563
| Lt | BinOp(Shl) // associated path
564564
| PathSep => true, // global path
565565
Interpolated(ref nt) => matches!(&**nt, NtLiteral(..) |
566-
NtPat(..) |
567566
NtBlock(..) |
568567
NtPath(..)),
569568
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
@@ -943,7 +942,6 @@ pub enum Nonterminal {
943942
NtItem(P<ast::Item>),
944943
NtBlock(P<ast::Block>),
945944
NtStmt(P<ast::Stmt>),
946-
NtPat(P<ast::Pat>),
947945
NtExpr(P<ast::Expr>),
948946
NtLiteral(P<ast::Expr>),
949947
/// Stuff inside brackets for attributes
@@ -1048,7 +1046,6 @@ impl Nonterminal {
10481046
NtItem(item) => item.span,
10491047
NtBlock(block) => block.span,
10501048
NtStmt(stmt) => stmt.span,
1051-
NtPat(pat) => pat.span,
10521049
NtExpr(expr) | NtLiteral(expr) => expr.span,
10531050
NtMeta(attr_item) => attr_item.span(),
10541051
NtPath(path) => path.span,
@@ -1060,7 +1057,6 @@ impl Nonterminal {
10601057
NtItem(..) => "item",
10611058
NtBlock(..) => "block",
10621059
NtStmt(..) => "statement",
1063-
NtPat(..) => "pattern",
10641060
NtExpr(..) => "expression",
10651061
NtLiteral(..) => "literal",
10661062
NtMeta(..) => "attribute",
@@ -1085,7 +1081,6 @@ impl fmt::Debug for Nonterminal {
10851081
NtItem(..) => f.pad("NtItem(..)"),
10861082
NtBlock(..) => f.pad("NtBlock(..)"),
10871083
NtStmt(..) => f.pad("NtStmt(..)"),
1088-
NtPat(..) => f.pad("NtPat(..)"),
10891084
NtExpr(..) => f.pad("NtExpr(..)"),
10901085
NtLiteral(..) => f.pad("NtLiteral(..)"),
10911086
NtMeta(..) => f.pad("NtMeta(..)"),

compiler/rustc_ast/src/tokenstream.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,6 @@ impl TokenStream {
473473
TokenStream::token_alone(token::Semi, stmt.span)
474474
}
475475
Nonterminal::NtStmt(stmt) => TokenStream::from_ast(stmt),
476-
Nonterminal::NtPat(pat) => TokenStream::from_ast(pat),
477476
Nonterminal::NtMeta(attr) => TokenStream::from_ast(attr),
478477
Nonterminal::NtPath(path) => TokenStream::from_ast(path),
479478
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => TokenStream::from_ast(expr),

compiler/rustc_expand/src/mbe/transcribe.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,13 @@ pub(super) fn transcribe<'a>(
296296
let kind = token::NtLifetime(*ident);
297297
TokenTree::token_alone(kind, sp)
298298
}
299+
MatchedSingle(ParseNtResult::PatParam(ref pat, inferred)) => mk_delimited(
300+
NonterminalKind::PatParam { inferred: *inferred },
301+
TokenStream::from_ast(pat),
302+
),
303+
MatchedSingle(ParseNtResult::PatWithOr(ref pat)) => {
304+
mk_delimited(NonterminalKind::PatWithOr, TokenStream::from_ast(pat))
305+
}
299306
MatchedSingle(ParseNtResult::Ty(ref ty)) => {
300307
mk_delimited(NonterminalKind::Ty, TokenStream::from_ast(ty))
301308
}

compiler/rustc_parse/src/parser/diagnostics.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,11 @@ use ast::token::IdentIsRaw;
2424
use rustc_ast as ast;
2525
use rustc_ast::ptr::P;
2626
use rustc_ast::token::{self, Delimiter, Lit, LitKind, Token, TokenKind};
27-
use rustc_ast::tokenstream::AttrTokenTree;
2827
use rustc_ast::util::parser::AssocOp;
2928
use rustc_ast::{
3029
AngleBracketedArg, AngleBracketedArgs, AnonConst, AttrVec, BinOpKind, BindingMode, Block,
31-
BlockCheckMode, Expr, ExprKind, GenericArg, Generics, HasTokens, Item, ItemKind, Param, Pat,
32-
PatKind, Path, PathSegment, QSelf, Recovered, Ty, TyKind,
30+
BlockCheckMode, Expr, ExprKind, GenericArg, Generics, Item, ItemKind, Param, Pat, PatKind,
31+
Path, PathSegment, QSelf, Recovered, Ty, TyKind,
3332
};
3433
use rustc_ast_pretty::pprust;
3534
use rustc_data_structures::fx::FxHashSet;
@@ -2361,6 +2360,8 @@ impl<'a> Parser<'a> {
23612360
}
23622361
err.span_label(span, "expected expression");
23632362

2363+
/* njn: temp disabled, which hurts tests/ui/macros/trace_faulty_macros.rs
2364+
23642365
// Walk the chain of macro expansions for the current token to point at how the original
23652366
// code was interpreted. This helps the user realize when a macro argument of one type is
23662367
// later reinterpreted as a different type, like `$x:expr` being reinterpreted as `$x:pat`
@@ -2406,6 +2407,7 @@ impl<'a> Parser<'a> {
24062407
tokens",
24072408
);
24082409
}
2410+
*/
24092411
err
24102412
}
24112413

compiler/rustc_parse/src/parser/item.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::maybe_whole;
99
use ast::token::IdentIsRaw;
1010
use rustc_ast::ast::*;
1111
use rustc_ast::ptr::P;
12-
use rustc_ast::token::{self, Delimiter, TokenKind};
12+
use rustc_ast::token::{self, Delimiter, InvisibleOrigin, NonterminalKind, TokenKind};
1313
use rustc_ast::tokenstream::{DelimSpan, TokenStream, TokenTree};
1414
use rustc_ast::util::case::Case;
1515
use rustc_ast::{self as ast};
@@ -2895,8 +2895,10 @@ impl<'a> Parser<'a> {
28952895

28962896
fn is_named_param(&self) -> bool {
28972897
let offset = match &self.token.kind {
2898-
token::Interpolated(nt) => match &**nt {
2899-
token::NtPat(..) => return self.look_ahead(1, |t| t == &token::Colon),
2898+
token::OpenDelim(Delimiter::Invisible(origin)) => match origin {
2899+
InvisibleOrigin::MetaVar(
2900+
NonterminalKind::PatParam { .. } | NonterminalKind::PatWithOr,
2901+
) => return self.check_noexpect_past_close_delim(&token::Colon),
29002902
_ => 0,
29012903
},
29022904
token::BinOp(token::And) | token::AndAnd => 1,

compiler/rustc_parse/src/parser/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1701,6 +1701,8 @@ pub enum ParseNtResult {
17011701
Tt(TokenTree),
17021702
Ident(Ident, IdentIsRaw),
17031703
Lifetime(Ident),
1704+
PatParam(P<ast::Pat>, /* inferred */ bool),
1705+
PatWithOr(P<ast::Pat>),
17041706
Ty(P<ast::Ty>),
17051707
Vis(P<ast::Visibility>),
17061708

compiler/rustc_parse/src/parser/nonterminal.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ impl<'a> Parser<'a> {
4848
fn nt_may_be_ident(nt: &Nonterminal) -> bool {
4949
match nt {
5050
NtStmt(_)
51-
| NtPat(_)
5251
| NtExpr(_)
5352
| NtLiteral(_) // `true`, `false`
5453
| NtMeta(_)
@@ -89,7 +88,7 @@ impl<'a> Parser<'a> {
8988
token::NtLifetime(..) => true,
9089
token::Interpolated(nt) => match &**nt {
9190
NtBlock(_) | NtStmt(_) | NtExpr(_) | NtLiteral(_) => true,
92-
NtItem(_) | NtPat(_) | NtMeta(_) | NtPath(_) => false,
91+
NtItem(_) | NtMeta(_) | NtPath(_) => false,
9392
},
9493
token::OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(k))) => match k {
9594
NonterminalKind::Block
@@ -182,17 +181,21 @@ impl<'a> Parser<'a> {
182181
.create_err(UnexpectedNonterminal::Statement(self.token.span)));
183182
}
184183
},
185-
NonterminalKind::PatParam { .. } | NonterminalKind::PatWithOr => {
186-
NtPat(self.collect_tokens_no_attrs(|this| match kind {
187-
NonterminalKind::PatParam { .. } => this.parse_pat_no_top_alt(None, None),
188-
NonterminalKind::PatWithOr => this.parse_pat_allow_top_alt(
184+
NonterminalKind::PatParam { inferred } => {
185+
return Ok(ParseNtResult::PatParam(
186+
self.collect_tokens_no_attrs(|this| this.parse_pat_no_top_alt(None, None))?,
187+
inferred,
188+
));
189+
}
190+
NonterminalKind::PatWithOr => {
191+
return Ok(ParseNtResult::PatWithOr(self.collect_tokens_no_attrs(|this| {
192+
this.parse_pat_allow_top_alt(
189193
None,
190194
RecoverComma::No,
191195
RecoverColon::No,
192196
CommaRecoveryMode::EitherTupleOrPipe,
193-
),
194-
_ => unreachable!(),
195-
})?)
197+
)
198+
})?));
196199
}
197200

198201
NonterminalKind::Expr | NonterminalKind::Expr2021 { inferred: _ } => {

compiler/rustc_parse/src/parser/pat.rs

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use super::{ForceCollect, Parser, PathStyle, Restrictions, Trailing, TrailingToken};
1+
use super::{
2+
ForceCollect, ParseNtResult, Parser, PathStyle, Restrictions, Trailing, TrailingToken,
3+
};
24
use crate::errors::{
35
self, AmbiguousRangePattern, DotDotDotForRemainingFields, DotDotDotRangeToPatternNotAllowed,
46
DotDotDotRestPattern, EnumPatternInsteadOfIdentifier, ExpectedBindingLeftOfAt,
@@ -11,10 +13,10 @@ use crate::errors::{
1113
UnexpectedVertVertInPattern,
1214
};
1315
use crate::parser::expr::{could_be_unclosed_char_literal, LhsExpr};
14-
use crate::{maybe_recover_from_interpolated_ty_qpath, maybe_whole};
16+
use crate::{maybe_recover_from_interpolated_ty_qpath, maybe_reparse_metavar_seq};
1517
use rustc_ast::mut_visit::{noop_visit_pat, MutVisitor};
1618
use rustc_ast::ptr::P;
17-
use rustc_ast::token::{self, BinOpToken, Delimiter, Token};
19+
use rustc_ast::token::{self, BinOpToken, Delimiter, NonterminalKind, Token};
1820
use rustc_ast::{
1921
self as ast, AttrVec, BindingMode, ByRef, Expr, ExprKind, MacCall, Mutability, Pat, PatField,
2022
PatFieldsRest, PatKind, Path, QSelf, RangeEnd, RangeSyntax,
@@ -434,7 +436,26 @@ impl<'a> Parser<'a> {
434436
syntax_loc: Option<PatternLocation>,
435437
) -> PResult<'a, P<Pat>> {
436438
maybe_recover_from_interpolated_ty_qpath!(self, true);
437-
maybe_whole!(self, NtPat, |pat| pat);
439+
440+
// Must try both kinds of pattern nonterminals.
441+
if let Some(pat) = maybe_reparse_metavar_seq!(
442+
self,
443+
NonterminalKind::PatParam { inferred },
444+
NonterminalKind::PatParam { inferred },
445+
ParseNtResult::PatParam(pat, _),
446+
pat
447+
) {
448+
return Ok(pat);
449+
}
450+
if let Some(pat) = maybe_reparse_metavar_seq!(
451+
self,
452+
NonterminalKind::PatWithOr,
453+
NonterminalKind::PatWithOr,
454+
ParseNtResult::PatWithOr(pat),
455+
pat
456+
) {
457+
return Ok(pat);
458+
}
438459

439460
let mut lo = self.token.span;
440461

@@ -755,10 +776,10 @@ impl<'a> Parser<'a> {
755776
self.recover_additional_muts();
756777

757778
// Make sure we don't allow e.g. `let mut $p;` where `$p:pat`.
758-
if let token::Interpolated(nt) = &self.token.kind {
759-
if let token::NtPat(..) = &**nt {
760-
self.expected_ident_found_err().emit();
761-
}
779+
if let Some(NonterminalKind::PatParam { .. } | NonterminalKind::PatWithOr) =
780+
self.token.is_metavar_seq()
781+
{
782+
self.expected_ident_found_err().emit();
762783
}
763784

764785
// Parse the pattern we hope to be an identifier.

0 commit comments

Comments
 (0)