Skip to content

Commit adb2c66

Browse files
committed
Remove NtPat.
1 parent f7df213 commit adb2c66

File tree

16 files changed

+91
-71
lines changed

16 files changed

+91
-71
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(),
@@ -246,7 +245,6 @@ impl HasTokens for Nonterminal {
246245
Nonterminal::NtItem(item) => item.tokens_mut(),
247246
Nonterminal::NtStmt(stmt) => stmt.tokens_mut(),
248247
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => expr.tokens_mut(),
249-
Nonterminal::NtPat(pat) => pat.tokens_mut(),
250248
Nonterminal::NtMeta(attr_item) => attr_item.tokens_mut(),
251249
Nonterminal::NtPath(path) => path.tokens_mut(),
252250
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
@@ -802,7 +802,6 @@ pub fn visit_nonterminal<T: MutVisitor>(nt: &mut token::Nonterminal, vis: &mut T
802802
vis.flat_map_stmt(stmt).expect_one("expected visitor to produce exactly one item")
803803
})
804804
}),
805-
token::NtPat(pat) => vis.visit_pat(pat),
806805
token::NtExpr(expr) => vis.visit_expr(expr),
807806
token::NtIdent(ident, _is_raw) => vis.visit_ident(ident),
808807
token::NtLifetime(ident) => vis.visit_ident(ident),

compiler/rustc_ast/src/token.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -477,12 +477,12 @@ impl Token {
477477
}
478478
}
479479

480-
/// Returns `true` if the token can appear at the start of an pattern.
480+
/// Returns `true` if the token can appear at the start of a pattern.
481481
///
482482
/// Shamelessly borrowed from `can_begin_expr`, only used for diagnostics right now.
483483
pub fn can_begin_pattern(&self) -> bool {
484484
match self.uninterpolate().kind {
485-
Ident(name, is_raw) =>
485+
Ident(name, is_raw) =>
486486
ident_can_begin_expr(name, self.span, is_raw), // value name or keyword
487487
| OpenDelim(Delimiter::Bracket | Delimiter::Parenthesis) // tuple or array
488488
| Literal(..) // literal
@@ -492,11 +492,11 @@ impl Token {
492492
// DotDotDot is no longer supported
493493
| DotDot | DotDotDot | DotDotEq // ranges
494494
| Lt | BinOp(Shl) // associated path
495-
| ModSep => true, // global path
496-
Interpolated(ref nt) => matches!(**nt, NtLiteral(..) |
497-
NtPat(..) |
498-
NtBlock(..) |
499-
NtPath(..)),
495+
| ModSep => true, // global path
496+
Interpolated(ref nt) => matches!(**nt, NtLiteral(..) | NtBlock(..) | NtPath(..)),
497+
| OpenDelim(Delimiter::Invisible(InvisibleSource::MetaVar(
498+
NonterminalKind::PatParam { .. } | NonterminalKind::PatWithOr
499+
))) => true,
500500
_ => false,
501501
}
502502
}
@@ -845,7 +845,6 @@ pub enum Nonterminal {
845845
NtItem(P<ast::Item>),
846846
NtBlock(P<ast::Block>),
847847
NtStmt(P<ast::Stmt>),
848-
NtPat(P<ast::Pat>),
849848
NtExpr(P<ast::Expr>),
850849
NtIdent(Ident, /* is_raw */ bool),
851850
NtLifetime(Ident),
@@ -939,7 +938,6 @@ impl Nonterminal {
939938
NtItem(item) => item.span,
940939
NtBlock(block) => block.span,
941940
NtStmt(stmt) => stmt.span,
942-
NtPat(pat) => pat.span,
943941
NtExpr(expr) | NtLiteral(expr) => expr.span,
944942
NtIdent(ident, _) | NtLifetime(ident) => ident.span,
945943
NtMeta(attr_item) => attr_item.span(),
@@ -970,7 +968,6 @@ impl fmt::Debug for Nonterminal {
970968
NtItem(..) => f.pad("NtItem(..)"),
971969
NtBlock(..) => f.pad("NtBlock(..)"),
972970
NtStmt(..) => f.pad("NtStmt(..)"),
973-
NtPat(..) => f.pad("NtPat(..)"),
974971
NtExpr(..) => f.pad("NtExpr(..)"),
975972
NtIdent(..) => f.pad("NtIdent(..)"),
976973
NtLiteral(..) => f.pad("NtLiteral(..)"),

compiler/rustc_ast/src/tokenstream.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,6 @@ impl TokenStream {
470470
TokenStream::token_alone(token::Semi, stmt.span)
471471
}
472472
Nonterminal::NtStmt(stmt) => TokenStream::from_ast(stmt),
473-
Nonterminal::NtPat(pat) => TokenStream::from_ast(pat),
474473
Nonterminal::NtMeta(attr) => TokenStream::from_ast(attr),
475474
Nonterminal::NtPath(path) => TokenStream::from_ast(path),
476475
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => TokenStream::from_ast(expr),

compiler/rustc_ast_pretty/src/pprust/state.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,6 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
732732
token::NtItem(e) => self.item_to_string(e),
733733
token::NtBlock(e) => self.block_to_string(e),
734734
token::NtStmt(e) => self.stmt_to_string(e),
735-
token::NtPat(e) => self.pat_to_string(e),
736735
token::NtIdent(e, is_raw) => IdentPrinter::for_ast_ident(*e, *is_raw).to_string(),
737736
token::NtLifetime(e) => e.to_string(),
738737
token::NtLiteral(e) => self.expr_to_string(e),

compiler/rustc_expand/src/mbe/transcribe.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,13 @@ pub(super) fn transcribe<'a>(
232232
// without wrapping them into groups.
233233
tt.clone()
234234
}
235+
MatchedSingle(ParseNtResult::PatParam(ref pat, inferred)) => mk_delimited(
236+
NonterminalKind::PatParam { inferred: *inferred },
237+
TokenStream::from_ast(pat),
238+
),
239+
MatchedSingle(ParseNtResult::PatWithOr(ref pat)) => {
240+
mk_delimited(NonterminalKind::PatWithOr, TokenStream::from_ast(pat))
241+
}
235242
MatchedSingle(ParseNtResult::Ty(ref ty)) => {
236243
mk_delimited(NonterminalKind::Ty, TokenStream::from_ast(ty))
237244
}

compiler/rustc_parse/src/parser/item.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::fluent_generated as fluent;
66
use ast::StaticItem;
77
use rustc_ast::ast::*;
88
use rustc_ast::ptr::P;
9-
use rustc_ast::token::{self, Delimiter, TokenKind};
9+
use rustc_ast::token::{self, Delimiter, InvisibleSource, NonterminalKind, TokenKind};
1010
use rustc_ast::tokenstream::{DelimSpan, TokenStream, TokenTree};
1111
use rustc_ast::util::case::Case;
1212
use rustc_ast::MacCall;
@@ -2680,8 +2680,10 @@ impl<'a> Parser<'a> {
26802680

26812681
fn is_named_param(&self) -> bool {
26822682
let offset = match &self.token.kind {
2683-
token::Interpolated(nt) => match **nt {
2684-
token::NtPat(..) => return self.look_ahead(1, |t| t == &token::Colon),
2683+
token::OpenDelim(Delimiter::Invisible(source)) => match source {
2684+
InvisibleSource::MetaVar(
2685+
NonterminalKind::PatParam { .. } | NonterminalKind::PatWithOr,
2686+
) => return self.check_noexpect_past_close_delim(&token::Colon),
26852687
_ => 0,
26862688
},
26872689
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
@@ -1581,6 +1581,8 @@ pub enum FlatToken {
15811581
#[derive(Clone, Debug)]
15821582
pub enum ParseNtResult {
15831583
Tt(TokenTree),
1584+
PatParam(P<ast::Pat>, /* inferred */ bool),
1585+
PatWithOr(P<ast::Pat>),
15841586
Ty(P<ast::Ty>),
15851587
Vis(P<ast::Visibility>),
15861588

compiler/rustc_parse/src/parser/nonterminal.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ impl<'a> Parser<'a> {
4646
fn nt_may_be_ident(nt: &token::Nonterminal) -> bool {
4747
match nt {
4848
NtStmt(_)
49-
| NtPat(_)
5049
| NtExpr(_)
5150
| NtIdent(..)
5251
| NtLiteral(_) // `true`, `false`
@@ -82,7 +81,7 @@ impl<'a> Parser<'a> {
8281
token::OpenDelim(Delimiter::Brace) => true,
8382
token::Interpolated(nt) => match **nt {
8483
NtBlock(_) | NtLifetime(_) | NtStmt(_) | NtExpr(_) | NtLiteral(_) => true,
85-
NtItem(_) | NtPat(_) | NtIdent(..) | NtMeta(_) | NtPath(_) => false,
84+
NtItem(_) | NtIdent(..) | NtMeta(_) | NtPath(_) => false,
8685
},
8786
token::OpenDelim(Delimiter::Invisible(InvisibleSource::MetaVar(k))) => match k {
8887
NonterminalKind::Block
@@ -110,8 +109,7 @@ impl<'a> Parser<'a> {
110109
}
111110
_ => false,
112111
},
113-
NonterminalKind::PatParam { .. } | NonterminalKind::PatWithOr => {
114-
match &token.kind {
112+
NonterminalKind::PatParam { .. } | NonterminalKind::PatWithOr => match &token.kind {
115113
token::Ident(..) | // box, ref, mut, and other identifiers (can stricten)
116114
token::OpenDelim(Delimiter::Parenthesis) | // tuple pattern
117115
token::OpenDelim(Delimiter::Bracket) | // slice pattern
@@ -131,8 +129,7 @@ impl<'a> Parser<'a> {
131129
may_be_ident(*kind)
132130
}
133131
_ => false,
134-
}
135-
}
132+
},
136133
NonterminalKind::Lifetime => match &token.kind {
137134
token::Lifetime(_) => true,
138135
token::Interpolated(nt) => {
@@ -179,19 +176,21 @@ impl<'a> Parser<'a> {
179176
.into_diagnostic(&self.sess.span_diagnostic));
180177
}
181178
},
182-
NonterminalKind::PatParam { .. } | NonterminalKind::PatWithOr => {
183-
NtPat(self.collect_tokens_no_attrs(|this| match kind {
184-
NonterminalKind::PatParam { .. } => this.parse_pat_no_top_alt(None, None),
185-
NonterminalKind::PatWithOr => this.parse_pat_allow_top_alt(
179+
NonterminalKind::PatParam { inferred } => {
180+
return Ok(ParseNtResult::PatParam(self.collect_tokens_no_attrs(|this|
181+
this.parse_pat_no_top_alt(None, None)
182+
)?, inferred))
183+
}
184+
NonterminalKind::PatWithOr => {
185+
return Ok(ParseNtResult::PatWithOr(self.collect_tokens_no_attrs(|this|
186+
this.parse_pat_allow_top_alt(
186187
None,
187188
RecoverComma::No,
188189
RecoverColon::No,
189190
CommaRecoveryMode::EitherTupleOrPipe,
190-
),
191-
_ => unreachable!(),
192-
})?)
191+
)
192+
)?))
193193
}
194-
195194
NonterminalKind::Expr => NtExpr(self.parse_expr_force_collect()?),
196195
NonterminalKind::Literal => {
197196
// The `:literal` matcher does not support attributes

compiler/rustc_parse/src/parser/pat.rs

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{ForceCollect, Parser, PathStyle, TrailingToken};
1+
use super::{ForceCollect, ParseNtResult, Parser, PathStyle, TrailingToken};
22
use crate::errors::{
33
self, AmbiguousRangePattern, DotDotDotForRemainingFields, DotDotDotRangeToPatternNotAllowed,
44
DotDotDotRestPattern, EnumPatternInsteadOfIdentifier, ExpectedBindingLeftOfAt,
@@ -9,10 +9,10 @@ use crate::errors::{
99
UnexpectedLifetimeInPattern, UnexpectedVertVertBeforeFunctionParam,
1010
UnexpectedVertVertInPattern,
1111
};
12-
use crate::{maybe_recover_from_interpolated_ty_qpath, maybe_whole};
12+
use crate::{maybe_recover_from_interpolated_ty_qpath, maybe_reparse_metavar_seq};
1313
use rustc_ast::mut_visit::{noop_visit_pat, MutVisitor};
1414
use rustc_ast::ptr::P;
15-
use rustc_ast::token::{self, Delimiter};
15+
use rustc_ast::token::{self, Delimiter, NonterminalKind};
1616
use rustc_ast::{
1717
self as ast, AttrVec, BindingAnnotation, ByRef, Expr, ExprKind, MacCall, Mutability, Pat,
1818
PatField, PatKind, Path, QSelf, RangeEnd, RangeSyntax,
@@ -334,7 +334,26 @@ impl<'a> Parser<'a> {
334334
syntax_loc: Option<PatternLocation>,
335335
) -> PResult<'a, P<Pat>> {
336336
maybe_recover_from_interpolated_ty_qpath!(self, true);
337-
maybe_whole!(self, NtPat, |x| x);
337+
338+
// Need to try both kinds of pattern nonterminals.
339+
if let Some(pat) = maybe_reparse_metavar_seq!(
340+
self,
341+
NonterminalKind::PatParam { inferred },
342+
NonterminalKind::PatParam { inferred },
343+
ParseNtResult::PatParam(pat, _),
344+
pat
345+
) {
346+
return Ok(pat);
347+
}
348+
if let Some(pat) = maybe_reparse_metavar_seq!(
349+
self,
350+
NonterminalKind::PatWithOr,
351+
NonterminalKind::PatWithOr,
352+
ParseNtResult::PatWithOr(pat),
353+
pat
354+
) {
355+
return Ok(pat);
356+
}
338357

339358
let mut lo = self.token.span;
340359

@@ -589,11 +608,10 @@ impl<'a> Parser<'a> {
589608

590609
self.recover_additional_muts();
591610

592-
// Make sure we don't allow e.g. `let mut $p;` where `$p:pat`.
593-
if let token::Interpolated(nt) = &self.token.kind {
594-
if let token::NtPat(_) = **nt {
595-
self.expected_ident_found_err().emit();
596-
}
611+
if let Some(NonterminalKind::PatParam { .. } | NonterminalKind::PatWithOr) =
612+
self.token.is_metavar_seq()
613+
{
614+
self.expected_ident_found_err().emit();
597615
}
598616

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

0 commit comments

Comments
 (0)