1 use super::{ForceCollect, Parser, PathStyle, TrailingToken};
2 use crate::parser::diagnostics::RemoveLet;
3 use crate::{maybe_recover_from_interpolated_ty_qpath, maybe_whole};
4 use rustc_ast::mut_visit::{noop_visit_pat, MutVisitor};
6 use rustc_ast::token::{self, Delimiter};
8 self as ast, AttrVec, BindingAnnotation, ByRef, Expr, ExprKind, MacCall, Mutability, Pat,
9 PatField, PatKind, Path, QSelf, RangeEnd, RangeSyntax,
11 use rustc_ast_pretty::pprust;
12 use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder, ErrorGuaranteed, PResult};
13 use rustc_span::source_map::{respan, Span, Spanned};
14 use rustc_span::symbol::{kw, sym, Ident};
16 pub(super) type Expected = Option<&'static str>;
18 /// `Expected` for function and lambda parameter patterns.
19 pub(super) const PARAM_EXPECTED: Expected = Some("parameter name");
21 const WHILE_PARSING_OR_MSG: &str = "while parsing this or-pattern starting here";
23 /// Whether or not to recover a `,` when parsing or-patterns.
24 #[derive(PartialEq, Copy, Clone)]
25 pub enum RecoverComma {
30 /// Whether or not to recover a `:` when parsing patterns that were meant to be paths.
31 #[derive(PartialEq, Copy, Clone)]
32 pub enum RecoverColon {
37 /// Whether or not to recover a `a, b` when parsing patterns as `(a, b)` or that *and* `a | b`.
38 #[derive(PartialEq, Copy, Clone)]
39 pub enum CommaRecoveryMode {
44 /// The result of `eat_or_separator`. We want to distinguish which case we are in to avoid
45 /// emitting duplicate diagnostics.
46 #[derive(Debug, Clone, Copy)]
48 /// We recovered from a trailing vert.
50 /// We ate an `|` (or `||` and recovered).
52 /// We did not eat anything (i.e. the current token is not `|` or `||`).
59 /// Corresponds to `pat<no_top_alt>` in RFC 2535 and does not admit or-patterns
60 /// at the top level. Used when parsing the parameters of lambda expressions,
61 /// functions, function pointers, and `pat` macro fragments.
62 pub fn parse_pat_no_top_alt(&mut self, expected: Expected) -> PResult<'a, P<Pat>> {
63 self.parse_pat_with_range_pat(true, expected)
68 /// Corresponds to `top_pat` in RFC 2535 and allows or-pattern at the top level.
69 /// Used for parsing patterns in all cases when `pat<no_top_alt>` is not used.
71 /// Note that after the FCP in <https://github.com/rust-lang/rust/issues/81415>,
72 /// a leading vert is allowed in nested or-patterns, too. This allows us to
73 /// simplify the grammar somewhat.
74 pub fn parse_pat_allow_top_alt(
79 rt: CommaRecoveryMode,
80 ) -> PResult<'a, P<Pat>> {
81 self.parse_pat_allow_top_alt_inner(expected, rc, ra, rt).map(|(pat, _)| pat)
84 /// Returns the pattern and a bool indicating whether we recovered from a trailing vert (true =
86 fn parse_pat_allow_top_alt_inner(
91 rt: CommaRecoveryMode,
92 ) -> PResult<'a, (P<Pat>, bool)> {
93 // Keep track of whether we recovered from a trailing vert so that we can avoid duplicated
94 // suggestions (which bothers rustfix).
96 // Allow a '|' before the pats (RFCs 1925, 2530, and 2535).
97 let (leading_vert_span, mut trailing_vert) = match self.eat_or_separator(None) {
98 EatOrResult::AteOr => (Some(self.prev_token.span), false),
99 EatOrResult::TrailingVert => (None, true),
100 EatOrResult::None => (None, false),
103 // Parse the first pattern (`p_0`).
104 let mut first_pat = self.parse_pat_no_top_alt(expected)?;
105 if rc == RecoverComma::Yes {
106 self.maybe_recover_unexpected_comma(first_pat.span, rt)?;
109 // If the next token is not a `|`,
110 // this is not an or-pattern and we should exit here.
111 if !self.check(&token::BinOp(token::Or)) && self.token != token::OrOr {
112 // If we parsed a leading `|` which should be gated,
113 // then we should really gate the leading `|`.
114 // This complicated procedure is done purely for diagnostics UX.
116 // Check if the user wrote `foo:bar` instead of `foo::bar`.
117 if ra == RecoverColon::Yes {
118 first_pat = self.maybe_recover_colon_colon_in_pat_typo(first_pat, expected);
121 if let Some(leading_vert_span) = leading_vert_span {
122 // If there was a leading vert, treat this as an or-pattern. This improves
124 let span = leading_vert_span.to(self.prev_token.span);
125 return Ok((self.mk_pat(span, PatKind::Or(vec![first_pat])), trailing_vert));
128 return Ok((first_pat, trailing_vert));
131 // Parse the patterns `p_1 | ... | p_n` where `n > 0`.
132 let lo = leading_vert_span.unwrap_or(first_pat.span);
133 let mut pats = vec![first_pat];
135 match self.eat_or_separator(Some(lo)) {
136 EatOrResult::AteOr => {}
137 EatOrResult::None => break,
138 EatOrResult::TrailingVert => {
139 trailing_vert = true;
143 let pat = self.parse_pat_no_top_alt(expected).map_err(|mut err| {
144 err.span_label(lo, WHILE_PARSING_OR_MSG);
147 if rc == RecoverComma::Yes {
148 self.maybe_recover_unexpected_comma(pat.span, rt)?;
152 let or_pattern_span = lo.to(self.prev_token.span);
154 Ok((self.mk_pat(or_pattern_span, PatKind::Or(pats)), trailing_vert))
157 /// Parse a pattern and (maybe) a `Colon` in positions where a pattern may be followed by a
158 /// type annotation (e.g. for `let` bindings or `fn` params).
160 /// Generally, this corresponds to `pat_no_top_alt` followed by an optional `Colon`. It will
161 /// eat the `Colon` token if one is present.
163 /// The return value represents the parsed pattern and `true` if a `Colon` was parsed (`false`
165 pub(super) fn parse_pat_before_ty(
170 ) -> PResult<'a, (P<Pat>, bool)> {
171 // We use `parse_pat_allow_top_alt` regardless of whether we actually want top-level
172 // or-patterns so that we can detect when a user tries to use it. This allows us to print a
173 // better error message.
174 let (pat, trailing_vert) = self.parse_pat_allow_top_alt_inner(
178 CommaRecoveryMode::LikelyTuple,
180 let colon = self.eat(&token::Colon);
182 if let PatKind::Or(pats) = &pat.kind {
183 let msg = format!("top-level or-patterns are not allowed in {}", syntax_loc);
184 let (help, fix) = if pats.len() == 1 {
185 // If all we have is a leading vert, then print a special message. This is the case
186 // if `parse_pat_allow_top_alt` returns an or-pattern with one variant.
187 let msg = "remove the `|`";
188 let fix = pprust::pat_to_string(&pat);
191 let msg = "wrap the pattern in parentheses";
192 let fix = format!("({})", pprust::pat_to_string(&pat));
197 // We already emitted an error and suggestion to remove the trailing vert. Don't
199 self.sess.span_diagnostic.delay_span_bug(pat.span, &msg);
201 self.struct_span_err(pat.span, &msg)
202 .span_suggestion(pat.span, help, fix, Applicability::MachineApplicable)
210 /// Parse the pattern for a function or function pointer parameter, followed by a colon.
212 /// The return value represents the parsed pattern and `true` if a `Colon` was parsed (`false`
214 pub(super) fn parse_fn_param_pat_colon(&mut self) -> PResult<'a, (P<Pat>, bool)> {
215 // In order to get good UX, we first recover in the case of a leading vert for an illegal
216 // top-level or-pat. Normally, this means recovering both `|` and `||`, but in this case,
217 // a leading `||` probably doesn't indicate an or-pattern attempt, so we handle that
219 if let token::OrOr = self.token.kind {
220 let span = self.token.span;
221 let mut err = self.struct_span_err(span, "unexpected `||` before function parameter");
222 err.span_suggestion(span, "remove the `||`", "", Applicability::MachineApplicable);
223 err.note("alternatives in or-patterns are separated with `|`, not `||`");
228 self.parse_pat_before_ty(PARAM_EXPECTED, RecoverComma::No, "function parameters")
231 /// Eat the or-pattern `|` separator.
232 /// If instead a `||` token is encountered, recover and pretend we parsed `|`.
233 fn eat_or_separator(&mut self, lo: Option<Span>) -> EatOrResult {
234 if self.recover_trailing_vert(lo) {
235 EatOrResult::TrailingVert
236 } else if matches!(self.token.kind, token::OrOr) {
237 // Found `||`; Recover and pretend we parsed `|`.
238 self.ban_unexpected_or_or(lo);
241 } else if self.eat(&token::BinOp(token::Or)) {
248 /// Recover if `|` or `||` is the current token and we have one of the
249 /// tokens `=>`, `if`, `=`, `:`, `;`, `,`, `]`, `)`, or `}` ahead of us.
251 /// These tokens all indicate that we reached the end of the or-pattern
252 /// list and can now reliably say that the `|` was an illegal trailing vert.
253 /// Note that there are more tokens such as `@` for which we know that the `|`
254 /// is an illegal parse. However, the user's intent is less clear in that case.
255 fn recover_trailing_vert(&mut self, lo: Option<Span>) -> bool {
256 let is_end_ahead = self.look_ahead(1, |token| {
258 &token.uninterpolate().kind,
259 token::FatArrow // e.g. `a | => 0,`.
260 | token::Ident(kw::If, false) // e.g. `a | if expr`.
261 | token::Eq // e.g. `let a | = 0`.
262 | token::Semi // e.g. `let a |;`.
263 | token::Colon // e.g. `let a | :`.
264 | token::Comma // e.g. `let (a |,)`.
265 | token::CloseDelim(Delimiter::Bracket) // e.g. `let [a | ]`.
266 | token::CloseDelim(Delimiter::Parenthesis) // e.g. `let (a | )`.
267 | token::CloseDelim(Delimiter::Brace) // e.g. `let A { f: a | }`.
270 match (is_end_ahead, &self.token.kind) {
271 (true, token::BinOp(token::Or) | token::OrOr) => {
272 self.ban_illegal_vert(lo, "trailing", "not allowed in an or-pattern");
280 /// We have parsed `||` instead of `|`. Error and suggest `|` instead.
281 fn ban_unexpected_or_or(&mut self, lo: Option<Span>) {
282 let mut err = self.struct_span_err(self.token.span, "unexpected token `||` in pattern");
285 "use a single `|` to separate multiple alternative patterns",
287 Applicability::MachineApplicable,
289 if let Some(lo) = lo {
290 err.span_label(lo, WHILE_PARSING_OR_MSG);
295 /// A `|` or possibly `||` token shouldn't be here. Ban it.
296 fn ban_illegal_vert(&mut self, lo: Option<Span>, pos: &str, ctx: &str) {
297 let span = self.token.span;
298 let mut err = self.struct_span_err(span, &format!("a {} `|` is {}", pos, ctx));
301 &format!("remove the `{}`", pprust::token_to_string(&self.token)),
303 Applicability::MachineApplicable,
305 if let Some(lo) = lo {
306 err.span_label(lo, WHILE_PARSING_OR_MSG);
308 if let token::OrOr = self.token.kind {
309 err.note("alternatives in or-patterns are separated with `|`, not `||`");
314 /// Parses a pattern, with a setting whether modern range patterns (e.g., `a..=b`, `a..b` are
316 fn parse_pat_with_range_pat(
318 allow_range_pat: bool,
320 ) -> PResult<'a, P<Pat>> {
321 maybe_recover_from_interpolated_ty_qpath!(self, true);
322 maybe_whole!(self, NtPat, |x| x);
324 let mut lo = self.token.span;
326 if self.token.is_keyword(kw::Let) && self.look_ahead(1, |tok| tok.can_begin_pattern()) {
328 self.sess.emit_err(RemoveLet { span: lo });
329 lo = self.token.span;
332 let pat = if self.check(&token::BinOp(token::And)) || self.token.kind == token::AndAnd {
333 self.parse_pat_deref(expected)?
334 } else if self.check(&token::OpenDelim(Delimiter::Parenthesis)) {
335 self.parse_pat_tuple_or_parens()?
336 } else if self.check(&token::OpenDelim(Delimiter::Bracket)) {
337 // Parse `[pat, pat,...]` as a slice pattern.
338 let (pats, _) = self.parse_delim_comma_seq(Delimiter::Bracket, |p| {
339 p.parse_pat_allow_top_alt(
343 CommaRecoveryMode::EitherTupleOrPipe,
347 } else if self.check(&token::DotDot) && !self.is_pat_range_end_start(1) {
348 // A rest pattern `..`.
351 } else if self.check(&token::DotDotDot) && !self.is_pat_range_end_start(1) {
352 self.recover_dotdotdot_rest_pat(lo)
353 } else if let Some(form) = self.parse_range_end() {
354 self.parse_pat_range_to(form)? // `..=X`, `...X`, or `..X`.
355 } else if self.eat_keyword(kw::Underscore) {
358 } else if self.eat_keyword(kw::Mut) {
359 self.parse_pat_ident_mut()?
360 } else if self.eat_keyword(kw::Ref) {
361 // Parse ref ident @ pat / ref mut ident @ pat
362 let mutbl = self.parse_mutability();
363 self.parse_pat_ident(BindingAnnotation(ByRef::Yes, mutbl))?
364 } else if self.eat_keyword(kw::Box) {
365 self.parse_pat_box()?
366 } else if self.check_inline_const(0) {
368 let const_expr = self.parse_const_block(lo.to(self.token.span), true)?;
370 if let Some(re) = self.parse_range_end() {
371 self.parse_pat_range_begin_with(const_expr, re)?
373 PatKind::Lit(const_expr)
375 } else if self.can_be_ident_pat() {
376 // Parse `ident @ pat`
377 // This can give false positives and parse nullary enums,
378 // they are dealt with later in resolve.
379 self.parse_pat_ident(BindingAnnotation::NONE)?
380 } else if self.is_start_of_pat_with_path() {
381 // Parse pattern starting with a path
382 let (qself, path) = if self.eat_lt() {
383 // Parse a qualified path
384 let (qself, path) = self.parse_qpath(PathStyle::Expr)?;
387 // Parse an unqualified path
388 (None, self.parse_path(PathStyle::Expr)?)
390 let span = lo.to(self.prev_token.span);
392 if qself.is_none() && self.check(&token::Not) {
393 self.parse_pat_mac_invoc(path)?
394 } else if let Some(form) = self.parse_range_end() {
395 let begin = self.mk_expr(span, ExprKind::Path(qself, path));
396 self.parse_pat_range_begin_with(begin, form)?
397 } else if self.check(&token::OpenDelim(Delimiter::Brace)) {
398 self.parse_pat_struct(qself, path)?
399 } else if self.check(&token::OpenDelim(Delimiter::Parenthesis)) {
400 self.parse_pat_tuple_struct(qself, path)?
402 PatKind::Path(qself, path)
405 // Try to parse everything else as literal with optional minus
406 match self.parse_literal_maybe_minus() {
407 Ok(begin) => match self.parse_range_end() {
408 Some(form) => self.parse_pat_range_begin_with(begin, form)?,
409 None => PatKind::Lit(begin),
411 Err(err) => return self.fatal_unexpected_non_pat(err, expected),
415 let pat = self.mk_pat(lo.to(self.prev_token.span), pat);
416 let pat = self.maybe_recover_from_bad_qpath(pat)?;
417 let pat = self.recover_intersection_pat(pat)?;
419 if !allow_range_pat {
420 self.ban_pat_range_if_ambiguous(&pat)
426 /// Recover from a typoed `...` pattern that was encountered
427 /// Ref: Issue #70388
428 fn recover_dotdotdot_rest_pat(&mut self, lo: Span) -> PatKind {
429 // A typoed rest pattern `...`.
430 self.bump(); // `...`
432 // The user probably mistook `...` for a rest pattern `..`.
433 self.struct_span_err(lo, "unexpected `...`")
434 .span_label(lo, "not a valid pattern")
435 .span_suggestion_short(
437 "for a rest pattern, use `..` instead of `...`",
439 Applicability::MachineApplicable,
445 /// Try to recover the more general form `intersect ::= $pat_lhs @ $pat_rhs`.
447 /// Allowed binding patterns generated by `binding ::= ref? mut? $ident @ $pat_rhs`
448 /// should already have been parsed by now at this point,
449 /// if the next token is `@` then we can try to parse the more general form.
451 /// Consult `parse_pat_ident` for the `binding` grammar.
453 /// The notion of intersection patterns are found in
454 /// e.g. [F#][and] where they are called AND-patterns.
456 /// [and]: https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/pattern-matching
457 fn recover_intersection_pat(&mut self, lhs: P<Pat>) -> PResult<'a, P<Pat>> {
458 if self.token.kind != token::At {
459 // Next token is not `@` so it's not going to be an intersection pattern.
463 // At this point we attempt to parse `@ $pat_rhs` and emit an error.
465 let mut rhs = self.parse_pat_no_top_alt(None)?;
466 let sp = lhs.span.to(rhs.span);
468 if let PatKind::Ident(_, _, ref mut sub @ None) = rhs.kind {
469 // The user inverted the order, so help them fix that.
470 let mut applicability = Applicability::MachineApplicable;
471 // FIXME(bindings_after_at): Remove this code when stabilizing the feature.
472 lhs.walk(&mut |p| match p.kind {
473 // `check_match` is unhappy if the subpattern has a binding anywhere.
474 PatKind::Ident(..) => {
475 applicability = Applicability::MaybeIncorrect;
476 false // Short-circuit.
481 let lhs_span = lhs.span;
482 // Move the LHS into the RHS as a subpattern.
483 // The RHS is now the full pattern.
486 self.struct_span_err(sp, "pattern on wrong side of `@`")
487 .span_label(lhs_span, "pattern on the left, should be on the right")
488 .span_label(rhs.span, "binding on the right, should be on the left")
489 .span_suggestion(sp, "switch the order", pprust::pat_to_string(&rhs), applicability)
492 // The special case above doesn't apply so we may have e.g. `A(x) @ B(y)`.
493 rhs.kind = PatKind::Wild;
494 self.struct_span_err(sp, "left-hand side of `@` must be a binding")
495 .span_label(lhs.span, "interpreted as a pattern, not a binding")
496 .span_label(rhs.span, "also a pattern")
497 .note("bindings are `x`, `mut x`, `ref x`, and `ref mut x`")
505 /// Ban a range pattern if it has an ambiguous interpretation.
506 fn ban_pat_range_if_ambiguous(&self, pat: &Pat) {
510 Spanned { node: RangeEnd::Included(RangeSyntax::DotDotDot), .. },
512 PatKind::Range(..) => {}
516 self.struct_span_err(pat.span, "the range pattern here has ambiguous interpretation")
519 "add parentheses to clarify the precedence",
520 format!("({})", pprust::pat_to_string(&pat)),
521 // "ambiguous interpretation" implies that we have to be guessing
522 Applicability::MaybeIncorrect,
527 /// Parse `&pat` / `&mut pat`.
528 fn parse_pat_deref(&mut self, expected: Expected) -> PResult<'a, PatKind> {
530 self.recover_lifetime_in_deref_pat();
531 let mutbl = self.parse_mutability();
532 let subpat = self.parse_pat_with_range_pat(false, expected)?;
533 Ok(PatKind::Ref(subpat, mutbl))
536 fn recover_lifetime_in_deref_pat(&mut self) {
537 if let token::Lifetime(name) = self.token.kind {
540 let span = self.prev_token.span;
541 self.struct_span_err(span, &format!("unexpected lifetime `{}` in pattern", name))
542 .span_suggestion(span, "remove the lifetime", "", Applicability::MachineApplicable)
547 /// Parse a tuple or parenthesis pattern.
548 fn parse_pat_tuple_or_parens(&mut self) -> PResult<'a, PatKind> {
549 let (fields, trailing_comma) = self.parse_paren_comma_seq(|p| {
550 p.parse_pat_allow_top_alt(
554 CommaRecoveryMode::LikelyTuple,
558 // Here, `(pat,)` is a tuple pattern.
559 // For backward compatibility, `(..)` is a tuple pattern as well.
560 Ok(if fields.len() == 1 && !(trailing_comma || fields[0].is_rest()) {
561 PatKind::Paren(fields.into_iter().next().unwrap())
563 PatKind::Tuple(fields)
567 /// Parse a mutable binding with the `mut` token already eaten.
568 fn parse_pat_ident_mut(&mut self) -> PResult<'a, PatKind> {
569 let mut_span = self.prev_token.span;
571 if self.eat_keyword(kw::Ref) {
572 return self.recover_mut_ref_ident(mut_span);
575 self.recover_additional_muts();
577 // Make sure we don't allow e.g. `let mut $p;` where `$p:pat`.
578 if let token::Interpolated(ref nt) = self.token.kind {
579 if let token::NtPat(_) = **nt {
580 self.expected_ident_found().emit();
584 // Parse the pattern we hope to be an identifier.
585 let mut pat = self.parse_pat_no_top_alt(Some("identifier"))?;
587 // If we don't have `mut $ident (@ pat)?`, error.
588 if let PatKind::Ident(BindingAnnotation(ByRef::No, m @ Mutability::Not), ..) = &mut pat.kind
590 // Don't recurse into the subpattern.
591 // `mut` on the outer binding doesn't affect the inner bindings.
592 *m = Mutability::Mut;
594 // Add `mut` to any binding in the parsed pattern.
595 let changed_any_binding = Self::make_all_value_bindings_mutable(&mut pat);
596 self.ban_mut_general_pat(mut_span, &pat, changed_any_binding);
599 Ok(pat.into_inner().kind)
602 /// Recover on `mut ref? ident @ pat` and suggest
603 /// that the order of `mut` and `ref` is incorrect.
604 fn recover_mut_ref_ident(&mut self, lo: Span) -> PResult<'a, PatKind> {
605 let mutref_span = lo.to(self.prev_token.span);
606 self.struct_span_err(mutref_span, "the order of `mut` and `ref` is incorrect")
609 "try switching the order",
611 Applicability::MachineApplicable,
615 self.parse_pat_ident(BindingAnnotation::REF_MUT)
618 /// Turn all by-value immutable bindings in a pattern into mutable bindings.
619 /// Returns `true` if any change was made.
620 fn make_all_value_bindings_mutable(pat: &mut P<Pat>) -> bool {
622 impl MutVisitor for AddMut {
623 fn visit_pat(&mut self, pat: &mut P<Pat>) {
624 if let PatKind::Ident(BindingAnnotation(ByRef::No, m @ Mutability::Not), ..) =
628 *m = Mutability::Mut;
630 noop_visit_pat(pat, self);
634 let mut add_mut = AddMut(false);
635 add_mut.visit_pat(pat);
639 /// Error on `mut $pat` where `$pat` is not an ident.
640 fn ban_mut_general_pat(&self, lo: Span, pat: &Pat, changed_any_binding: bool) {
641 let span = lo.to(pat.span);
642 let fix = pprust::pat_to_string(&pat);
643 let (problem, suggestion) = if changed_any_binding {
644 ("`mut` must be attached to each individual binding", "add `mut` to each binding")
646 ("`mut` must be followed by a named binding", "remove the `mut` prefix")
648 self.struct_span_err(span, problem)
649 .span_suggestion(span, suggestion, fix, Applicability::MachineApplicable)
650 .note("`mut` may be followed by `variable` and `variable @ pattern`")
654 /// Eat any extraneous `mut`s and error + recover if we ate any.
655 fn recover_additional_muts(&mut self) {
656 let lo = self.token.span;
657 while self.eat_keyword(kw::Mut) {}
658 if lo == self.token.span {
662 let span = lo.to(self.prev_token.span);
663 self.struct_span_err(span, "`mut` on a binding may not be repeated")
666 "remove the additional `mut`s",
668 Applicability::MachineApplicable,
673 /// Parse macro invocation
674 fn parse_pat_mac_invoc(&mut self, path: Path) -> PResult<'a, PatKind> {
676 let args = self.parse_mac_args()?;
677 let mac = P(MacCall { path, args, prior_type_ascription: self.last_type_ascription });
678 Ok(PatKind::MacCall(mac))
681 fn fatal_unexpected_non_pat(
683 err: DiagnosticBuilder<'a, ErrorGuaranteed>,
685 ) -> PResult<'a, P<Pat>> {
688 let expected = expected.unwrap_or("pattern");
689 let msg = format!("expected {}, found {}", expected, super::token_descr(&self.token));
691 let mut err = self.struct_span_err(self.token.span, &msg);
692 err.span_label(self.token.span, format!("expected {}", expected));
694 let sp = self.sess.source_map().start_point(self.token.span);
695 if let Some(sp) = self.sess.ambiguous_block_expr_parse.borrow().get(&sp) {
696 self.sess.expr_parentheses_needed(&mut err, *sp);
702 /// Parses the range pattern end form `".." | "..." | "..=" ;`.
703 fn parse_range_end(&mut self) -> Option<Spanned<RangeEnd>> {
704 let re = if self.eat(&token::DotDotDot) {
705 RangeEnd::Included(RangeSyntax::DotDotDot)
706 } else if self.eat(&token::DotDotEq) {
707 RangeEnd::Included(RangeSyntax::DotDotEq)
708 } else if self.eat(&token::DotDot) {
713 Some(respan(self.prev_token.span, re))
716 /// Parse a range pattern `$begin $form $end?` where `$form = ".." | "..." | "..=" ;`.
717 /// `$begin $form` has already been parsed.
718 fn parse_pat_range_begin_with(
721 re: Spanned<RangeEnd>,
722 ) -> PResult<'a, PatKind> {
723 let end = if self.is_pat_range_end_start(0) {
724 // Parsing e.g. `X..=Y`.
725 Some(self.parse_pat_range_end()?)
727 // Parsing e.g. `X..`.
728 if let RangeEnd::Included(_) = re.node {
729 // FIXME(Centril): Consider semantic errors instead in `ast_validation`.
730 self.inclusive_range_with_incorrect_end(re.span);
734 Ok(PatKind::Range(Some(begin), end, re))
737 pub(super) fn inclusive_range_with_incorrect_end(&mut self, span: Span) {
738 let tok = &self.token;
740 // If the user typed "..==" instead of "..=", we want to give them
741 // a specific error message telling them to use "..=".
742 // Otherwise, we assume that they meant to type a half open exclusive
743 // range and give them an error telling them to do that instead.
744 if matches!(tok.kind, token::Eq) && tok.span.lo() == span.hi() {
745 let span_with_eq = span.to(tok.span);
747 // Ensure the user doesn't receive unhelpful unexpected token errors
749 if self.is_pat_range_end_start(0) {
750 let _ = self.parse_pat_range_end().map_err(|e| e.cancel());
753 self.error_inclusive_range_with_extra_equals(span_with_eq);
755 self.error_inclusive_range_with_no_end(span);
759 fn error_inclusive_range_with_extra_equals(&self, span: Span) {
760 self.struct_span_err(span, "unexpected `=` after inclusive range")
761 .span_suggestion_short(span, "use `..=` instead", "..=", Applicability::MaybeIncorrect)
762 .note("inclusive ranges end with a single equals sign (`..=`)")
766 fn error_inclusive_range_with_no_end(&self, span: Span) {
767 struct_span_err!(self.sess.span_diagnostic, span, E0586, "inclusive range with no end")
768 .span_suggestion_short(span, "use `..` instead", "..", Applicability::MachineApplicable)
769 .note("inclusive ranges must be bounded at the end (`..=b` or `a..=b`)")
773 /// Parse a range-to pattern, `..X` or `..=X` where `X` remains to be parsed.
775 /// The form `...X` is prohibited to reduce confusion with the potential
776 /// expression syntax `...expr` for splatting in expressions.
777 fn parse_pat_range_to(&mut self, mut re: Spanned<RangeEnd>) -> PResult<'a, PatKind> {
778 let end = self.parse_pat_range_end()?;
779 self.sess.gated_spans.gate(sym::half_open_range_patterns, re.span.to(self.prev_token.span));
780 if let RangeEnd::Included(ref mut syn @ RangeSyntax::DotDotDot) = &mut re.node {
781 *syn = RangeSyntax::DotDotEq;
782 self.struct_span_err(re.span, "range-to patterns with `...` are not allowed")
783 .span_suggestion_short(
787 Applicability::MachineApplicable,
791 Ok(PatKind::Range(None, Some(end), re))
794 /// Is the token `dist` away from the current suitable as the start of a range patterns end?
795 fn is_pat_range_end_start(&self, dist: usize) -> bool {
796 self.check_inline_const(dist)
797 || self.look_ahead(dist, |t| {
798 t.is_path_start() // e.g. `MY_CONST`;
799 || t.kind == token::Dot // e.g. `.5` for recovery;
800 || t.can_begin_literal_maybe_minus() // e.g. `42`.
805 fn parse_pat_range_end(&mut self) -> PResult<'a, P<Expr>> {
806 if self.check_inline_const(0) {
807 self.parse_const_block(self.token.span, true)
808 } else if self.check_path() {
809 let lo = self.token.span;
810 let (qself, path) = if self.eat_lt() {
811 // Parse a qualified path
812 let (qself, path) = self.parse_qpath(PathStyle::Expr)?;
815 // Parse an unqualified path
816 (None, self.parse_path(PathStyle::Expr)?)
818 let hi = self.prev_token.span;
819 Ok(self.mk_expr(lo.to(hi), ExprKind::Path(qself, path)))
821 self.parse_literal_maybe_minus()
825 /// Is this the start of a pattern beginning with a path?
826 fn is_start_of_pat_with_path(&mut self) -> bool {
828 // Just for recovery (see `can_be_ident`).
829 || self.token.is_ident() && !self.token.is_bool_lit() && !self.token.is_keyword(kw::In)
832 /// Would `parse_pat_ident` be appropriate here?
833 fn can_be_ident_pat(&mut self) -> bool {
835 && !self.token.is_bool_lit() // Avoid `true` or `false` as a binding as it is a literal.
836 && !self.token.is_path_segment_keyword() // Avoid e.g. `Self` as it is a path.
837 // Avoid `in`. Due to recovery in the list parser this messes with `for ( $pat in $expr )`.
838 && !self.token.is_keyword(kw::In)
839 // Try to do something more complex?
840 && self.look_ahead(1, |t| !matches!(t.kind, token::OpenDelim(Delimiter::Parenthesis) // A tuple struct pattern.
841 | token::OpenDelim(Delimiter::Brace) // A struct pattern.
842 | token::DotDotDot | token::DotDotEq | token::DotDot // A range pattern.
843 | token::ModSep // A tuple / struct variant pattern.
844 | token::Not)) // A macro expanding to a pattern.
847 /// Parses `ident` or `ident @ pat`.
848 /// Used by the copy foo and ref foo patterns to give a good
849 /// error message when parsing mistakes like `ref foo(a, b)`.
850 fn parse_pat_ident(&mut self, binding_annotation: BindingAnnotation) -> PResult<'a, PatKind> {
851 let ident = self.parse_ident()?;
852 let sub = if self.eat(&token::At) {
853 Some(self.parse_pat_no_top_alt(Some("binding pattern"))?)
858 // Just to be friendly, if they write something like `ref Some(i)`,
859 // we end up here with `(` as the current token.
860 // This shortly leads to a parse error. Note that if there is no explicit
861 // binding mode then we do not end up here, because the lookahead
862 // will direct us over to `parse_enum_variant()`.
863 if self.token == token::OpenDelim(Delimiter::Parenthesis) {
865 .struct_span_err(self.prev_token.span, "expected identifier, found enum pattern"));
868 Ok(PatKind::Ident(binding_annotation, ident, sub))
871 /// Parse a struct ("record") pattern (e.g. `Foo { ... }` or `Foo::Bar { ... }`).
872 fn parse_pat_struct(&mut self, qself: Option<QSelf>, path: Path) -> PResult<'a, PatKind> {
874 // Feature gate the use of qualified paths in patterns
875 self.sess.gated_spans.gate(sym::more_qualified_paths, path.span);
878 let (fields, etc) = self.parse_pat_fields().unwrap_or_else(|mut e| {
879 e.span_label(path.span, "while parsing the fields for this pattern");
885 Ok(PatKind::Struct(qself, path, fields, etc))
888 /// Parse tuple struct or tuple variant pattern (e.g. `Foo(...)` or `Foo::Bar(...)`).
889 fn parse_pat_tuple_struct(&mut self, qself: Option<QSelf>, path: Path) -> PResult<'a, PatKind> {
890 let (fields, _) = self.parse_paren_comma_seq(|p| {
891 p.parse_pat_allow_top_alt(
895 CommaRecoveryMode::EitherTupleOrPipe,
899 self.sess.gated_spans.gate(sym::more_qualified_paths, path.span);
901 Ok(PatKind::TupleStruct(qself, path, fields))
904 /// Are we sure this could not possibly be the start of a pattern?
906 /// Currently, this only accounts for tokens that can follow identifiers
907 /// in patterns, but this can be extended as necessary.
908 fn isnt_pattern_start(&self) -> bool {
915 token::OpenDelim(Delimiter::Brace),
916 token::CloseDelim(Delimiter::Brace),
917 token::CloseDelim(Delimiter::Parenthesis),
919 .contains(&self.token.kind)
923 fn parse_pat_box(&mut self) -> PResult<'a, PatKind> {
924 let box_span = self.prev_token.span;
926 if self.isnt_pattern_start() {
927 self.struct_span_err(
929 format!("expected pattern, found {}", super::token_descr(&self.token)),
931 .span_note(box_span, "`box` is a reserved keyword")
932 .span_suggestion_verbose(
933 box_span.shrink_to_lo(),
934 "escape `box` to use it as an identifier",
936 Applicability::MaybeIncorrect,
940 // We cannot use `parse_pat_ident()` since it will complain `box`
941 // is not an identifier.
942 let sub = if self.eat(&token::At) {
943 Some(self.parse_pat_no_top_alt(Some("binding pattern"))?)
948 Ok(PatKind::Ident(BindingAnnotation::NONE, Ident::new(kw::Box, box_span), sub))
950 let pat = self.parse_pat_with_range_pat(false, None)?;
951 self.sess.gated_spans.gate(sym::box_patterns, box_span.to(self.prev_token.span));
952 Ok(PatKind::Box(pat))
956 /// Parses the fields of a struct-like pattern.
957 fn parse_pat_fields(&mut self) -> PResult<'a, (Vec<PatField>, bool)> {
958 let mut fields = Vec::new();
960 let mut ate_comma = true;
961 let mut delayed_err: Option<DiagnosticBuilder<'a, ErrorGuaranteed>> = None;
962 let mut etc_span = None;
964 while self.token != token::CloseDelim(Delimiter::Brace) {
965 let attrs = match self.parse_outer_attributes() {
968 if let Some(mut delayed) = delayed_err {
974 let lo = self.token.span;
976 // check that a comma comes after every field
978 let err = self.struct_span_err(self.token.span, "expected `,`");
979 if let Some(mut delayed) = delayed_err {
986 if self.check(&token::DotDot) || self.token == token::DotDotDot {
988 let mut etc_sp = self.token.span;
990 self.recover_one_fewer_dotdot();
991 self.bump(); // `..` || `...`
993 if self.token == token::CloseDelim(Delimiter::Brace) {
994 etc_span = Some(etc_sp);
997 let token_str = super::token_descr(&self.token);
998 let msg = &format!("expected `}}`, found {}", token_str);
999 let mut err = self.struct_span_err(self.token.span, msg);
1001 err.span_label(self.token.span, "expected `}`");
1002 let mut comma_sp = None;
1003 if self.token == token::Comma {
1005 let nw_span = self.sess.source_map().span_until_non_whitespace(self.token.span);
1006 etc_sp = etc_sp.to(nw_span);
1009 "`..` must be at the end and cannot have a trailing comma",
1011 comma_sp = Some(self.token.span);
1016 etc_span = Some(etc_sp.until(self.token.span));
1017 if self.token == token::CloseDelim(Delimiter::Brace) {
1018 // If the struct looks otherwise well formed, recover and continue.
1019 if let Some(sp) = comma_sp {
1020 err.span_suggestion_short(
1022 "remove this comma",
1024 Applicability::MachineApplicable,
1029 } else if self.token.is_ident() && ate_comma {
1030 // Accept fields coming after `..,`.
1031 // This way we avoid "pattern missing fields" errors afterwards.
1032 // We delay this error until the end in order to have a span for a
1034 if let Some(mut delayed_err) = delayed_err {
1038 delayed_err = Some(err);
1041 if let Some(mut err) = delayed_err {
1049 self.collect_tokens_trailing_token(attrs, ForceCollect::No, |this, attrs| {
1050 let field = match this.parse_pat_field(lo, attrs) {
1051 Ok(field) => Ok(field),
1053 if let Some(mut delayed_err) = delayed_err.take() {
1059 ate_comma = this.eat(&token::Comma);
1060 // We just ate a comma, so there's no need to use
1061 // `TrailingToken::Comma`
1062 Ok((field, TrailingToken::None))
1068 if let Some(mut err) = delayed_err {
1069 if let Some(etc_span) = etc_span {
1070 err.multipart_suggestion(
1071 "move the `..` to the end of the field list",
1073 (etc_span, String::new()),
1074 (self.token.span, format!("{}.. }}", if ate_comma { "" } else { ", " })),
1076 Applicability::MachineApplicable,
1084 /// Recover on `...` as if it were `..` to avoid further errors.
1085 /// See issue #46718.
1086 fn recover_one_fewer_dotdot(&self) {
1087 if self.token != token::DotDotDot {
1091 self.struct_span_err(self.token.span, "expected field pattern, found `...`")
1094 "to omit remaining fields, use one fewer `.`",
1096 Applicability::MachineApplicable,
1101 fn parse_pat_field(&mut self, lo: Span, attrs: AttrVec) -> PResult<'a, PatField> {
1102 // Check if a colon exists one ahead. This means we're parsing a fieldname.
1104 let (subpat, fieldname, is_shorthand) = if self.look_ahead(1, |t| t == &token::Colon) {
1105 // Parsing a pattern of the form `fieldname: pat`.
1106 let fieldname = self.parse_field_name()?;
1108 let pat = self.parse_pat_allow_top_alt(
1112 CommaRecoveryMode::EitherTupleOrPipe,
1115 (pat, fieldname, false)
1117 // Parsing a pattern of the form `(box) (ref) (mut) fieldname`.
1118 let is_box = self.eat_keyword(kw::Box);
1119 let boxed_span = self.token.span;
1120 let is_ref = self.eat_keyword(kw::Ref);
1121 let is_mut = self.eat_keyword(kw::Mut);
1122 let fieldname = self.parse_field_name()?;
1123 hi = self.prev_token.span;
1125 let mutability = match is_mut {
1126 false => Mutability::Not,
1127 true => Mutability::Mut,
1129 let ann = BindingAnnotation(ByRef::from(is_ref), mutability);
1130 let fieldpat = self.mk_pat_ident(boxed_span.to(hi), ann, fieldname);
1132 if is_box { self.mk_pat(lo.to(hi), PatKind::Box(fieldpat)) } else { fieldpat };
1133 (subpat, fieldname, true)
1141 id: ast::DUMMY_NODE_ID,
1143 is_placeholder: false,
1147 pub(super) fn mk_pat_ident(&self, span: Span, ann: BindingAnnotation, ident: Ident) -> P<Pat> {
1148 self.mk_pat(span, PatKind::Ident(ann, ident, None))
1151 pub(super) fn mk_pat(&self, span: Span, kind: PatKind) -> P<Pat> {
1152 P(Pat { kind, span, id: ast::DUMMY_NODE_ID, tokens: None })