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

Commit 8d24526

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 aa6bd27 commit 8d24526

16 files changed

+67
-52
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
@@ -564,7 +564,6 @@ impl Token {
564564
| Lt | BinOp(Shl) // associated path
565565
| PathSep => true, // global path
566566
Interpolated(ref nt) => matches!(&**nt, NtLiteral(..) |
567-
NtPat(..) |
568567
NtBlock(..) |
569568
NtPath(..)),
570569
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
@@ -962,7 +961,6 @@ pub enum Nonterminal {
962961
NtItem(P<ast::Item>),
963962
NtBlock(P<ast::Block>),
964963
NtStmt(P<ast::Stmt>),
965-
NtPat(P<ast::Pat>),
966964
NtExpr(P<ast::Expr>),
967965
NtLiteral(P<ast::Expr>),
968966
/// Stuff inside brackets for attributes
@@ -1059,7 +1057,6 @@ impl Nonterminal {
10591057
NtItem(item) => item.span,
10601058
NtBlock(block) => block.span,
10611059
NtStmt(stmt) => stmt.span,
1062-
NtPat(pat) => pat.span,
10631060
NtExpr(expr) | NtLiteral(expr) => expr.span,
10641061
NtMeta(attr_item) => attr_item.span(),
10651062
NtPath(path) => path.span,
@@ -1071,7 +1068,6 @@ impl Nonterminal {
10711068
NtItem(..) => "item",
10721069
NtBlock(..) => "block",
10731070
NtStmt(..) => "statement",
1074-
NtPat(..) => "pattern",
10751071
NtExpr(..) => "expression",
10761072
NtLiteral(..) => "literal",
10771073
NtMeta(..) => "attribute",
@@ -1096,7 +1092,6 @@ impl fmt::Debug for Nonterminal {
10961092
NtItem(..) => f.pad("NtItem(..)"),
10971093
NtBlock(..) => f.pad("NtBlock(..)"),
10981094
NtStmt(..) => f.pad("NtStmt(..)"),
1099-
NtPat(..) => f.pad("NtPat(..)"),
11001095
NtExpr(..) => f.pad("NtExpr(..)"),
11011096
NtLiteral(..) => f.pad("NtLiteral(..)"),
11021097
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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,10 @@ pub(super) fn transcribe<'a>(
296296
let kind = token::NtLifetime(*ident);
297297
TokenTree::token_alone(kind, sp)
298298
}
299+
MatchedSingle(ParseNtResult::Pat(pat, pat_kind)) => mk_delimited(
300+
NonterminalKind::Pat(*pat_kind),
301+
TokenStream::from_ast(pat),
302+
),
299303
MatchedSingle(ParseNtResult::Ty(ref ty)) => {
300304
mk_delimited(NonterminalKind::Ty, TokenStream::from_ast(ty))
301305
}

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(NonterminalKind::Pat(_)) => {
2900+
return self.check_noexpect_past_close_delim(&token::Colon);
2901+
}
29002902
_ => 0,
29012903
},
29022904
token::BinOp(token::And) | token::AndAnd => 1,

compiler/rustc_parse/src/parser/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ use path::PathStyle;
2020

2121
use rustc_ast::ptr::P;
2222
use rustc_ast::token::{
23-
self, Delimiter, IdentIsRaw, InvisibleOrigin, Nonterminal, NonterminalKind, Token, TokenKind,
23+
self, Delimiter, IdentIsRaw, InvisibleOrigin, Nonterminal, NonterminalKind, NtPatKind, Token,
24+
TokenKind,
2425
};
2526
use rustc_ast::tokenstream::{AttributesData, DelimSpacing, DelimSpan, Spacing};
2627
use rustc_ast::tokenstream::{TokenStream, TokenTree, TokenTreeCursor};
@@ -1701,6 +1702,7 @@ pub enum ParseNtResult {
17011702
Tt(TokenTree),
17021703
Ident(Ident, IdentIsRaw),
17031704
Lifetime(Ident),
1705+
Pat(P<ast::Pat>, NtPatKind),
17041706
Ty(P<ast::Ty>),
17051707
Vis(P<ast::Visibility>),
17061708

compiler/rustc_parse/src/parser/nonterminal.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ impl<'a> Parser<'a> {
4747
fn nt_may_be_ident(nt: &Nonterminal) -> bool {
4848
match nt {
4949
NtStmt(_)
50-
| NtPat(_)
5150
| NtExpr(_)
5251
| NtLiteral(_) // `true`, `false`
5352
| NtMeta(_)
@@ -88,7 +87,7 @@ impl<'a> Parser<'a> {
8887
token::NtLifetime(..) => true,
8988
token::Interpolated(nt) => match &**nt {
9089
NtBlock(_) | NtStmt(_) | NtExpr(_) | NtLiteral(_) => true,
91-
NtItem(_) | NtPat(_) | NtMeta(_) | NtPath(_) => false,
90+
NtItem(_) | NtMeta(_) | NtPath(_) => false,
9291
},
9392
token::OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(k))) => match k {
9493
NonterminalKind::Block
@@ -180,15 +179,18 @@ impl<'a> Parser<'a> {
180179
}
181180
},
182181
NonterminalKind::Pat(pat_kind) => {
183-
NtPat(self.collect_tokens_no_attrs(|this| match pat_kind {
184-
PatParam { .. } => this.parse_pat_no_top_alt(None, None),
185-
PatWithOr => this.parse_pat_allow_top_alt(
186-
None,
187-
RecoverComma::No,
188-
RecoverColon::No,
189-
CommaRecoveryMode::EitherTupleOrPipe,
190-
),
191-
})?)
182+
return Ok(ParseNtResult::Pat(
183+
self.collect_tokens_no_attrs(|this| match pat_kind {
184+
PatParam { .. } => this.parse_pat_no_top_alt(None, None),
185+
PatWithOr => this.parse_pat_allow_top_alt(
186+
None,
187+
RecoverComma::No,
188+
RecoverColon::No,
189+
CommaRecoveryMode::EitherTupleOrPipe,
190+
),
191+
})?,
192+
pat_kind,
193+
));
192194
}
193195
NonterminalKind::Expr(_) => NtExpr(self.parse_expr_force_collect()?),
194196
NonterminalKind::Literal => {

compiler/rustc_parse/src/parser/pat.rs

Lines changed: 27 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, NtPatKind::*, 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::Pat(PatParam { inferred }),
444+
NonterminalKind::Pat(PatParam { inferred }),
445+
ParseNtResult::Pat(pat, PatParam { inferred: _ }),
446+
pat
447+
) {
448+
return Ok(pat);
449+
}
450+
if let Some(pat) = maybe_reparse_metavar_seq!(
451+
self,
452+
NonterminalKind::Pat(PatWithOr),
453+
NonterminalKind::Pat(PatWithOr),
454+
ParseNtResult::Pat(pat, PatWithOr),
455+
pat
456+
) {
457+
return Ok(pat);
458+
}
438459

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

@@ -755,10 +776,8 @@ 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::Pat(_)) = self.token.is_metavar_seq() {
780+
self.expected_ident_found_err().emit();
762781
}
763782

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

0 commit comments

Comments
 (0)