Skip to main content

rustc_parse/parser/
pat.rs

1use std::ops::Bound;
2
3use rustc_ast::mut_visit::{self, MutVisitor};
4use rustc_ast::token::NtPatKind::*;
5use rustc_ast::token::{self, IdentIsRaw, MetaVarKind, Token};
6use rustc_ast::util::parser::ExprPrecedence;
7use rustc_ast::visit::{self, Visitor};
8use rustc_ast::{
9    self as ast, Arm, AttrVec, BindingMode, ByRef, Expr, ExprKind, LocalKind, MacCall, Mutability,
10    Pat, PatField, PatFieldsRest, PatKind, Path, QSelf, RangeEnd, RangeSyntax, Stmt, StmtKind,
11};
12use rustc_ast_pretty::pprust;
13use rustc_errors::{Applicability, Diag, DiagArgValue, PResult, StashKey};
14use rustc_session::errors::ExprParenthesesNeeded;
15use rustc_span::source_map::{Spanned, respan};
16use rustc_span::{BytePos, ErrorGuaranteed, Ident, Span, kw, sym};
17use thin_vec::{ThinVec, thin_vec};
18
19use super::{ForceCollect, Parser, PathStyle, Restrictions, Trailing, UsePreAttrPos};
20use crate::errors::{
21    self, AmbiguousRangePattern, AtDotDotInStructPattern, AtInStructPattern,
22    DotDotDotForRemainingFields, DotDotDotRangeToPatternNotAllowed, DotDotDotRestPattern,
23    EnumPatternInsteadOfIdentifier, ExpectedBindingLeftOfAt, ExpectedCommaAfterPatternField,
24    GenericArgsInPatRequireTurbofishSyntax, InclusiveRangeExtraEquals, InclusiveRangeMatchArrow,
25    InclusiveRangeNoEnd, InvalidMutInPattern, ParenRangeSuggestion, PatternOnWrongSideOfAt,
26    RemoveLet, RepeatedMutInPattern, SwitchRefBoxOrder, TopLevelOrPatternNotAllowed,
27    TopLevelOrPatternNotAllowedSugg, TrailingVertNotAllowed, TrailingVertSuggestion,
28    UnexpectedExpressionInPattern, UnexpectedExpressionInPatternSugg, UnexpectedLifetimeInPattern,
29    UnexpectedParenInRangePat, UnexpectedParenInRangePatSugg,
30    UnexpectedVertVertBeforeFunctionParam, UnexpectedVertVertInPattern, WrapInParens,
31};
32use crate::parser::expr::{DestructuredFloat, could_be_unclosed_char_literal};
33use crate::{exp, maybe_recover_from_interpolated_ty_qpath};
34
35#[derive(#[automatically_derived]
impl ::core::cmp::PartialEq for Expected {
    #[inline]
    fn eq(&self, other: &Expected) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr
    }
}PartialEq, #[automatically_derived]
impl ::core::marker::Copy for Expected { }Copy, #[automatically_derived]
impl ::core::clone::Clone for Expected {
    #[inline]
    fn clone(&self) -> Expected { *self }
}Clone)]
36pub enum Expected {
37    ParameterName,
38    ArgumentName,
39    Identifier,
40    BindingPattern,
41}
42
43impl Expected {
44    // FIXME(#100717): migrate users of this to proper localization
45    fn to_string_or_fallback(expected: Option<Expected>) -> &'static str {
46        match expected {
47            Some(Expected::ParameterName) => "parameter name",
48            Some(Expected::ArgumentName) => "argument name",
49            Some(Expected::Identifier) => "identifier",
50            Some(Expected::BindingPattern) => "binding pattern",
51            None => "pattern",
52        }
53    }
54}
55
56const WHILE_PARSING_OR_MSG: &str = "while parsing this or-pattern starting here";
57
58/// Whether or not to recover a `,` when parsing or-patterns.
59#[derive(#[automatically_derived]
impl ::core::cmp::PartialEq for RecoverComma {
    #[inline]
    fn eq(&self, other: &RecoverComma) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr
    }
}PartialEq, #[automatically_derived]
impl ::core::marker::Copy for RecoverComma { }Copy, #[automatically_derived]
impl ::core::clone::Clone for RecoverComma {
    #[inline]
    fn clone(&self) -> RecoverComma { *self }
}Clone)]
60pub enum RecoverComma {
61    Yes,
62    No,
63}
64
65/// Whether or not to recover a `:` when parsing patterns that were meant to be paths.
66#[derive(#[automatically_derived]
impl ::core::cmp::PartialEq for RecoverColon {
    #[inline]
    fn eq(&self, other: &RecoverColon) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr
    }
}PartialEq, #[automatically_derived]
impl ::core::marker::Copy for RecoverColon { }Copy, #[automatically_derived]
impl ::core::clone::Clone for RecoverColon {
    #[inline]
    fn clone(&self) -> RecoverColon { *self }
}Clone)]
67pub enum RecoverColon {
68    Yes,
69    No,
70}
71
72/// Whether or not to recover a `a, b` when parsing patterns as `(a, b)` or that *and* `a | b`.
73#[derive(#[automatically_derived]
impl ::core::cmp::PartialEq for CommaRecoveryMode {
    #[inline]
    fn eq(&self, other: &CommaRecoveryMode) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr
    }
}PartialEq, #[automatically_derived]
impl ::core::marker::Copy for CommaRecoveryMode { }Copy, #[automatically_derived]
impl ::core::clone::Clone for CommaRecoveryMode {
    #[inline]
    fn clone(&self) -> CommaRecoveryMode { *self }
}Clone)]
74pub enum CommaRecoveryMode {
75    LikelyTuple,
76    EitherTupleOrPipe,
77}
78
79/// The result of `eat_or_separator`. We want to distinguish which case we are in to avoid
80/// emitting duplicate diagnostics.
81#[derive(#[automatically_derived]
impl ::core::fmt::Debug for EatOrResult {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f,
            match self {
                EatOrResult::TrailingVert => "TrailingVert",
                EatOrResult::AteOr => "AteOr",
                EatOrResult::None => "None",
            })
    }
}Debug, #[automatically_derived]
impl ::core::clone::Clone for EatOrResult {
    #[inline]
    fn clone(&self) -> EatOrResult { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for EatOrResult { }Copy)]
82enum EatOrResult {
83    /// We recovered from a trailing vert.
84    TrailingVert,
85    /// We ate an `|` (or `||` and recovered).
86    AteOr,
87    /// We did not eat anything (i.e. the current token is not `|` or `||`).
88    None,
89}
90
91/// The syntax location of a given pattern. Used for diagnostics.
92#[derive(#[automatically_derived]
impl ::core::clone::Clone for PatternLocation {
    #[inline]
    fn clone(&self) -> PatternLocation { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for PatternLocation { }Copy)]
93pub enum PatternLocation {
94    LetBinding,
95    FunctionParameter,
96}
97
98impl<'a> Parser<'a> {
99    /// Parses a pattern.
100    ///
101    /// Corresponds to `Pattern` in RFC 3637 and admits guard patterns at the top level.
102    /// Used when parsing patterns in all cases where neither `PatternNoTopGuard` nor
103    /// `PatternNoTopAlt` (see below) are used.
104    pub fn parse_pat_allow_top_guard(
105        &mut self,
106        expected: Option<Expected>,
107        rc: RecoverComma,
108        ra: RecoverColon,
109        rt: CommaRecoveryMode,
110    ) -> PResult<'a, Pat> {
111        let pat = self.parse_pat_no_top_guard(expected, rc, ra, rt)?;
112
113        if self.eat_keyword(crate::parser::token_type::ExpKeywordPair {
    kw: rustc_span::symbol::kw::If,
    token_type: crate::parser::token_type::TokenType::KwIf,
}exp!(If)) {
114            let cond = self.parse_expr()?;
115            // Feature-gate guard patterns
116            self.psess.gated_spans.gate(sym::guard_patterns, cond.span);
117            let span = pat.span.to(cond.span);
118            Ok(self.mk_pat(span, PatKind::Guard(Box::new(pat), cond)))
119        } else {
120            Ok(pat)
121        }
122    }
123
124    /// Parses a pattern.
125    ///
126    /// Corresponds to `PatternNoTopAlt` in RFC 3637 and does not admit or-patterns
127    /// or guard patterns at the top level. Used when parsing the parameters of lambda
128    /// expressions, functions, function pointers, and `pat_param` macro fragments.
129    pub fn parse_pat_no_top_alt(
130        &mut self,
131        expected: Option<Expected>,
132        syntax_loc: Option<PatternLocation>,
133    ) -> PResult<'a, Pat> {
134        self.parse_pat_with_range_pat(true, expected, syntax_loc)
135    }
136
137    /// Parses a pattern.
138    ///
139    /// Corresponds to `PatternNoTopGuard` in RFC 3637 and allows or-patterns, but not
140    /// guard patterns, at the top level. Used for parsing patterns in `pat` fragments (until
141    /// the next edition) and `let`, `if let`, and `while let` expressions.
142    ///
143    /// Note that after the FCP in <https://github.com/rust-lang/rust/issues/81415>,
144    /// a leading vert is allowed in nested or-patterns, too. This allows us to
145    /// simplify the grammar somewhat.
146    pub fn parse_pat_no_top_guard(
147        &mut self,
148        expected: Option<Expected>,
149        rc: RecoverComma,
150        ra: RecoverColon,
151        rt: CommaRecoveryMode,
152    ) -> PResult<'a, Pat> {
153        self.parse_pat_no_top_guard_inner(expected, rc, ra, rt, None).map(|(pat, _)| pat)
154    }
155
156    /// Returns the pattern and a bool indicating whether we recovered from a trailing vert (true =
157    /// recovered).
158    fn parse_pat_no_top_guard_inner(
159        &mut self,
160        expected: Option<Expected>,
161        rc: RecoverComma,
162        ra: RecoverColon,
163        rt: CommaRecoveryMode,
164        syntax_loc: Option<PatternLocation>,
165    ) -> PResult<'a, (Pat, bool)> {
166        // Keep track of whether we recovered from a trailing vert so that we can avoid duplicated
167        // suggestions (which bothers rustfix).
168        //
169        // Allow a '|' before the pats (RFCs 1925, 2530, and 2535).
170        let (leading_vert_span, mut trailing_vert) = match self.eat_or_separator(None) {
171            EatOrResult::AteOr => (Some(self.prev_token.span), false),
172            EatOrResult::TrailingVert => (None, true),
173            EatOrResult::None => (None, false),
174        };
175
176        // Parse the first pattern (`p_0`).
177        let mut first_pat = match self.parse_pat_no_top_alt(expected, syntax_loc) {
178            Ok(pat) => pat,
179            Err(err)
180                if self.token.is_reserved_ident()
181                    && !self.token.is_keyword(kw::In)
182                    && !self.token.is_keyword(kw::If) =>
183            {
184                err.emit();
185                self.bump();
186                self.mk_pat(self.token.span, PatKind::Wild)
187            }
188            Err(err) => return Err(err),
189        };
190        if rc == RecoverComma::Yes && !first_pat.could_be_never_pattern() {
191            self.maybe_recover_unexpected_comma(first_pat.span, rt)?;
192        }
193
194        // If the next token is not a `|`,
195        // this is not an or-pattern and we should exit here.
196        if !self.check(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::Or,
    token_type: crate::parser::token_type::TokenType::Or,
}exp!(Or)) && self.token != token::OrOr {
197            // If we parsed a leading `|` which should be gated,
198            // then we should really gate the leading `|`.
199            // This complicated procedure is done purely for diagnostics UX.
200
201            // Check if the user wrote `foo:bar` instead of `foo::bar`.
202            if ra == RecoverColon::Yes && token::Colon == self.token.kind {
203                first_pat = self.recover_colon_colon_in_pat_typo(first_pat, expected);
204            }
205
206            if let Some(leading_vert_span) = leading_vert_span {
207                // If there was a leading vert, treat this as an or-pattern. This improves
208                // diagnostics.
209                let span = leading_vert_span.to(self.prev_token.span);
210                return Ok((self.mk_pat(span, PatKind::Or({
    let len = [()].len();
    let mut vec = ::thin_vec::ThinVec::with_capacity(len);
    vec.push(first_pat);
    vec
}thin_vec![first_pat])), trailing_vert));
211            }
212
213            return Ok((first_pat, trailing_vert));
214        }
215
216        // Parse the patterns `p_1 | ... | p_n` where `n > 0`.
217        let lo = leading_vert_span.unwrap_or(first_pat.span);
218        let mut pats = {
    let len = [()].len();
    let mut vec = ::thin_vec::ThinVec::with_capacity(len);
    vec.push(first_pat);
    vec
}thin_vec![first_pat];
219        loop {
220            match self.eat_or_separator(Some(lo)) {
221                EatOrResult::AteOr => {}
222                EatOrResult::None => break,
223                EatOrResult::TrailingVert => {
224                    trailing_vert = true;
225                    break;
226                }
227            }
228            let pat = self.parse_pat_no_top_alt(expected, syntax_loc).map_err(|mut err| {
229                err.span_label(lo, WHILE_PARSING_OR_MSG);
230                err
231            })?;
232            if rc == RecoverComma::Yes && !pat.could_be_never_pattern() {
233                self.maybe_recover_unexpected_comma(pat.span, rt)?;
234            }
235            pats.push(pat);
236        }
237        let or_pattern_span = lo.to(self.prev_token.span);
238
239        Ok((self.mk_pat(or_pattern_span, PatKind::Or(pats)), trailing_vert))
240    }
241
242    /// Parse a pattern and (maybe) a `Colon` in positions where a pattern may be followed by a
243    /// type annotation (e.g. for `let` bindings or `fn` params).
244    ///
245    /// Generally, this corresponds to `pat_no_top_alt` followed by an optional `Colon`. It will
246    /// eat the `Colon` token if one is present.
247    ///
248    /// The return value represents the parsed pattern and `true` if a `Colon` was parsed (`false`
249    /// otherwise).
250    pub(super) fn parse_pat_before_ty(
251        &mut self,
252        expected: Option<Expected>,
253        rc: RecoverComma,
254        syntax_loc: PatternLocation,
255    ) -> PResult<'a, (Box<Pat>, bool)> {
256        // We use `parse_pat_allow_top_alt` regardless of whether we actually want top-level
257        // or-patterns so that we can detect when a user tries to use it. This allows us to print a
258        // better error message.
259        let (pat, trailing_vert) = self.parse_pat_no_top_guard_inner(
260            expected,
261            rc,
262            RecoverColon::No,
263            CommaRecoveryMode::LikelyTuple,
264            Some(syntax_loc),
265        )?;
266        let pat = Box::new(pat);
267        let colon = self.eat(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::Colon,
    token_type: crate::parser::token_type::TokenType::Colon,
}exp!(Colon));
268
269        if let PatKind::Or(pats) = &pat.kind {
270            let span = pat.span;
271            let sub = if let [_] = &pats[..] {
272                let span = span.with_hi(span.lo() + BytePos(1));
273                Some(TopLevelOrPatternNotAllowedSugg::RemoveLeadingVert { span })
274            } else {
275                Some(TopLevelOrPatternNotAllowedSugg::WrapInParens {
276                    span,
277                    suggestion: WrapInParens { lo: span.shrink_to_lo(), hi: span.shrink_to_hi() },
278                })
279            };
280
281            let err = self.dcx().create_err(match syntax_loc {
282                PatternLocation::LetBinding => {
283                    TopLevelOrPatternNotAllowed::LetBinding { span, sub }
284                }
285                PatternLocation::FunctionParameter => {
286                    TopLevelOrPatternNotAllowed::FunctionParameter { span, sub }
287                }
288            });
289            if trailing_vert {
290                err.delay_as_bug();
291            } else {
292                err.emit();
293            }
294        }
295
296        Ok((pat, colon))
297    }
298
299    /// Parse the pattern for a function or function pointer parameter, followed by a colon.
300    ///
301    /// The return value represents the parsed pattern and `true` if a `Colon` was parsed (`false`
302    /// otherwise).
303    pub(super) fn parse_fn_param_pat_colon(&mut self) -> PResult<'a, (Box<Pat>, bool)> {
304        // In order to get good UX, we first recover in the case of a leading vert for an illegal
305        // top-level or-pat. Normally, this means recovering both `|` and `||`, but in this case,
306        // a leading `||` probably doesn't indicate an or-pattern attempt, so we handle that
307        // separately.
308        if let token::OrOr = self.token.kind {
309            self.dcx().emit_err(UnexpectedVertVertBeforeFunctionParam { span: self.token.span });
310            self.bump();
311        }
312
313        self.parse_pat_before_ty(
314            Some(Expected::ParameterName),
315            RecoverComma::No,
316            PatternLocation::FunctionParameter,
317        )
318    }
319
320    /// Eat the or-pattern `|` separator.
321    /// If instead a `||` token is encountered, recover and pretend we parsed `|`.
322    fn eat_or_separator(&mut self, lo: Option<Span>) -> EatOrResult {
323        if self.recover_trailing_vert(lo) {
324            EatOrResult::TrailingVert
325        } else if self.token.kind == token::OrOr {
326            // Found `||`; Recover and pretend we parsed `|`.
327            self.dcx().emit_err(UnexpectedVertVertInPattern { span: self.token.span, start: lo });
328            self.bump();
329            EatOrResult::AteOr
330        } else if self.eat(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::Or,
    token_type: crate::parser::token_type::TokenType::Or,
}exp!(Or)) {
331            EatOrResult::AteOr
332        } else {
333            EatOrResult::None
334        }
335    }
336
337    /// Recover if `|` or `||` is the current token and we have one of the
338    /// tokens `=>`, `if`, `=`, `:`, `;`, `,`, `]`, `)`, or `}` ahead of us.
339    ///
340    /// These tokens all indicate that we reached the end of the or-pattern
341    /// list and can now reliably say that the `|` was an illegal trailing vert.
342    /// Note that there are more tokens such as `@` for which we know that the `|`
343    /// is an illegal parse. However, the user's intent is less clear in that case.
344    fn recover_trailing_vert(&mut self, lo: Option<Span>) -> bool {
345        let is_end_ahead = self.look_ahead(1, |token| {
346            #[allow(non_exhaustive_omitted_patterns)] match &token.uninterpolate().kind {
    token::FatArrow | token::Ident(kw::If, token::IdentIsRaw::No) | token::Eq
        | token::Semi | token::Colon | token::Comma | token::CloseBracket |
        token::CloseParen | token::CloseBrace => true,
    _ => false,
}matches!(
347                &token.uninterpolate().kind,
348                token::FatArrow // e.g. `a | => 0,`.
349                | token::Ident(kw::If, token::IdentIsRaw::No) // e.g. `a | if expr`.
350                | token::Eq // e.g. `let a | = 0`.
351                | token::Semi // e.g. `let a |;`.
352                | token::Colon // e.g. `let a | :`.
353                | token::Comma // e.g. `let (a |,)`.
354                | token::CloseBracket // e.g. `let [a | ]`.
355                | token::CloseParen // e.g. `let (a | )`.
356                | token::CloseBrace // e.g. `let A { f: a | }`.
357            )
358        });
359        match (is_end_ahead, &self.token.kind) {
360            (true, token::Or | token::OrOr) => {
361                // A `|` or possibly `||` token shouldn't be here. Ban it.
362                self.dcx().emit_err(TrailingVertNotAllowed {
363                    span: self.token.span,
364                    start: lo,
365                    suggestion: TrailingVertSuggestion {
366                        span: self.prev_token.span.shrink_to_hi().with_hi(self.token.span.hi()),
367                        token: self.token,
368                    },
369                    token: self.token,
370                    note_double_vert: self.token.kind == token::OrOr,
371                });
372                self.bump();
373                true
374            }
375            _ => false,
376        }
377    }
378
379    /// Ensures that the last parsed pattern (or pattern range bound) is not followed by an expression.
380    ///
381    /// `is_end_bound` indicates whether the last parsed thing was the end bound of a range pattern (see [`parse_pat_range_end`](Self::parse_pat_range_end))
382    /// in order to say "expected a pattern range bound" instead of "expected a pattern";
383    /// ```text
384    /// 0..=1 + 2
385    ///     ^^^^^
386    /// ```
387    /// Only the end bound is spanned in this case, and this function has no idea if there was a `..=` before `pat_span`, hence the parameter.
388    ///
389    /// This function returns `Some` if a trailing expression was recovered, and said expression's span.
390    #[must_use = "the pattern must be discarded as `PatKind::Err` if this function returns Some"]
391    fn maybe_recover_trailing_expr(
392        &mut self,
393        pat_span: Span,
394        is_end_bound: bool,
395    ) -> Option<(ErrorGuaranteed, Span)> {
396        if self.prev_token.is_keyword(kw::Underscore) || !self.may_recover() {
397            // Don't recover anything after an `_` or if recovery is disabled.
398            return None;
399        }
400
401        // Returns `true` iff `token` is an unsuffixed integer.
402        let is_one_tuple_index = |_: &Self, token: &Token| -> bool {
403            use token::{Lit, LitKind};
404
405            #[allow(non_exhaustive_omitted_patterns)] match token.kind {
    token::Literal(Lit { kind: LitKind::Integer, symbol: _, suffix: None }) =>
        true,
    _ => false,
}matches!(
406                token.kind,
407                token::Literal(Lit { kind: LitKind::Integer, symbol: _, suffix: None })
408            )
409        };
410
411        // Returns `true` iff `token` is an unsuffixed `x.y` float.
412        let is_two_tuple_indexes = |this: &Self, token: &Token| -> bool {
413            use token::{Lit, LitKind};
414
415            if let token::Literal(Lit { kind: LitKind::Float, symbol, suffix: None }) = token.kind
416                && let DestructuredFloat::MiddleDot(..) = this.break_up_float(symbol, token.span)
417            {
418                true
419            } else {
420                false
421            }
422        };
423
424        // Check for `.hello` or `.0`.
425        let has_dot_expr = self.check_noexpect(&token::Dot) // `.`
426            && self.look_ahead(1, |tok| {
427                tok.is_ident() // `hello`
428                || is_one_tuple_index(&self, &tok) // `0`
429                || is_two_tuple_indexes(&self, &tok) // `0.0`
430            });
431
432        // Check for operators.
433        // `|` is excluded as it is used in pattern alternatives and lambdas,
434        // `?` is included for error propagation,
435        // `[` is included for indexing operations,
436        // `[]` is excluded as `a[]` isn't an expression and should be recovered as `a, []` (cf. `tests/ui/parser/pat-lt-bracket-7.rs`),
437        // `as` is included for type casts
438        let has_trailing_operator = #[allow(non_exhaustive_omitted_patterns)] match self.token.kind {
    token::Plus | token::Minus | token::Star | token::Slash | token::Percent |
        token::Caret | token::And | token::Shl | token::Shr => true,
    _ => false,
}matches!(
439                self.token.kind,
440                token::Plus | token::Minus | token::Star | token::Slash | token::Percent
441                | token::Caret | token::And | token::Shl | token::Shr // excludes `Or`
442            )
443            || self.token == token::Question
444            || (self.token == token::OpenBracket
445                && self.look_ahead(1, |t| *t != token::CloseBracket)) // excludes `[]`
446            || self.token.is_keyword(kw::As);
447
448        if !has_dot_expr && !has_trailing_operator {
449            // Nothing to recover here.
450            return None;
451        }
452
453        // Let's try to parse an expression to emit a better diagnostic.
454        let mut snapshot = self.create_snapshot_for_diagnostic();
455        snapshot.restrictions.insert(Restrictions::IS_PAT);
456
457        // Parse `?`, `.f`, `(arg0, arg1, ...)` or `[expr]` until they've all been eaten.
458        let Ok(expr) = snapshot
459            .parse_expr_dot_or_call_with(
460                AttrVec::new(),
461                self.mk_expr(pat_span, ExprKind::Dummy), // equivalent to transforming the parsed pattern into an `Expr`
462                pat_span,
463            )
464            .map_err(|err| err.cancel())
465        else {
466            // We got a trailing method/operator, but that wasn't an expression.
467            return None;
468        };
469
470        // Parse an associative expression such as `+ expr`, `% expr`, ...
471        // Assignments, ranges and `|` are disabled by [`Restrictions::IS_PAT`].
472        let Ok((expr, _)) = snapshot
473            .parse_expr_assoc_rest_with(Bound::Unbounded, false, expr)
474            .map_err(|err| err.cancel())
475        else {
476            // We got a trailing method/operator, but that wasn't an expression.
477            return None;
478        };
479
480        // We got a valid expression.
481        self.restore_snapshot(snapshot);
482        self.restrictions.remove(Restrictions::IS_PAT);
483
484        let is_bound = is_end_bound
485            // is_start_bound: either `..` or `)..`
486            || self.token.is_range_separator()
487            || self.token == token::CloseParen
488                && self.look_ahead(1, Token::is_range_separator);
489
490        let span = expr.span;
491
492        Some((
493            self.dcx()
494                .create_err(UnexpectedExpressionInPattern {
495                    span,
496                    is_bound,
497                    expr_precedence: expr.precedence(),
498                })
499                .stash(span, StashKey::ExprInPat)
500                .unwrap(),
501            span,
502        ))
503    }
504
505    /// Called by [`Parser::parse_stmt_without_recovery`], used to add statement-aware subdiagnostics to the errors stashed
506    /// by [`Parser::maybe_recover_trailing_expr`].
507    pub(super) fn maybe_augment_stashed_expr_in_pats_with_suggestions(&mut self, stmt: &Stmt) {
508        if self.dcx().has_errors().is_none() {
509            // No need to walk the statement if there's no stashed errors.
510            return;
511        }
512
513        struct PatVisitor<'a> {
514            /// `self`
515            parser: &'a Parser<'a>,
516            /// The freshly-parsed statement.
517            stmt: &'a Stmt,
518            /// The current match arm (for arm guard suggestions).
519            arm: Option<&'a Arm>,
520            /// The current struct field (for variable name suggestions).
521            field: Option<&'a PatField>,
522        }
523
524        impl<'a> PatVisitor<'a> {
525            /// Looks for stashed [`StashKey::ExprInPat`] errors in `stash_span`, and emit them with suggestions.
526            /// `stash_span` is contained in `expr_span`, the latter being larger in borrow patterns;
527            /// ```txt
528            /// &mut x.y
529            /// -----^^^ `stash_span`
530            /// |
531            /// `expr_span`
532            /// ```
533            /// `is_range_bound` is used to exclude arm guard suggestions in range pattern bounds.
534            fn maybe_add_suggestions_then_emit(
535                &self,
536                stash_span: Span,
537                expr_span: Span,
538                is_range_bound: bool,
539            ) {
540                self.parser.dcx().try_steal_modify_and_emit_err(
541                    stash_span,
542                    StashKey::ExprInPat,
543                    |err| {
544                        // Includes pre-pats (e.g. `&mut <err>`) in the diagnostic.
545                        err.span.replace(stash_span, expr_span);
546
547                        let sm = self.parser.psess.source_map();
548                        let stmt = self.stmt;
549                        let line_lo = sm.span_extend_to_line(stmt.span).shrink_to_lo();
550                        let indentation = sm.indentation_before(stmt.span).unwrap_or_default();
551                        let Ok(expr) = self.parser.span_to_snippet(expr_span) else {
552                            // FIXME: some suggestions don't actually need the snippet; see PR #123877's unresolved conversations.
553                            return;
554                        };
555
556                        if let StmtKind::Let(local) = &stmt.kind {
557                            match &local.kind {
558                                LocalKind::Decl | LocalKind::Init(_) => {
559                                    // It's kinda hard to guess what the user intended, so don't make suggestions.
560                                    return;
561                                }
562
563                                LocalKind::InitElse(_, _) => {}
564                            }
565                        }
566
567                        // help: use an arm guard `if val == expr`
568                        // FIXME(guard_patterns): suggest this regardless of a match arm.
569                        if let Some(arm) = &self.arm
570                            && !is_range_bound
571                        {
572                            let (ident, ident_span) = match self.field {
573                                Some(field) => {
574                                    (field.ident.to_string(), field.ident.span.to(expr_span))
575                                }
576                                None => ("val".to_owned(), expr_span),
577                            };
578
579                            // Are parentheses required around `expr`?
580                            // HACK: a neater way would be preferable.
581                            let expr = match &err.args["expr_precedence"] {
582                                DiagArgValue::Number(expr_precedence) => {
583                                    if *expr_precedence <= ExprPrecedence::Compare as i32 {
584                                        ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("({0})", expr))
    })format!("({expr})")
585                                    } else {
586                                        ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}", expr))
    })format!("{expr}")
587                                    }
588                                }
589                                _ => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
590                            };
591
592                            match &arm.guard {
593                                None => {
594                                    err.subdiagnostic(
595                                        UnexpectedExpressionInPatternSugg::CreateGuard {
596                                            ident_span,
597                                            pat_hi: arm.pat.span.shrink_to_hi(),
598                                            ident,
599                                            expr,
600                                        },
601                                    );
602                                }
603                                Some(guard) => {
604                                    // Are parentheses required around the old guard?
605                                    let wrap_guard = guard.precedence() <= ExprPrecedence::LAnd;
606
607                                    err.subdiagnostic(
608                                        UnexpectedExpressionInPatternSugg::UpdateGuard {
609                                            ident_span,
610                                            guard_lo: if wrap_guard {
611                                                Some(guard.span.shrink_to_lo())
612                                            } else {
613                                                None
614                                            },
615                                            guard_hi: guard.span.shrink_to_hi(),
616                                            guard_hi_paren: if wrap_guard { ")" } else { "" },
617                                            ident,
618                                            expr,
619                                        },
620                                    );
621                                }
622                            }
623                        }
624
625                        // help: extract the expr into a `const VAL: _ = expr`
626                        let ident = match self.field {
627                            Some(field) => field.ident.as_str().to_uppercase(),
628                            None => "VAL".to_owned(),
629                        };
630                        err.subdiagnostic(UnexpectedExpressionInPatternSugg::Const {
631                            stmt_lo: line_lo,
632                            ident_span: expr_span,
633                            expr,
634                            ident,
635                            indentation,
636                        });
637                    },
638                );
639            }
640        }
641
642        impl<'a> Visitor<'a> for PatVisitor<'a> {
643            fn visit_arm(&mut self, a: &'a Arm) -> Self::Result {
644                self.arm = Some(a);
645                visit::walk_arm(self, a);
646                self.arm = None;
647            }
648
649            fn visit_pat_field(&mut self, fp: &'a PatField) -> Self::Result {
650                self.field = Some(fp);
651                visit::walk_pat_field(self, fp);
652                self.field = None;
653            }
654
655            fn visit_pat(&mut self, p: &'a Pat) -> Self::Result {
656                match &p.kind {
657                    // Base expression
658                    PatKind::Err(_) | PatKind::Expr(_) => {
659                        self.maybe_add_suggestions_then_emit(p.span, p.span, false)
660                    }
661
662                    // Sub-patterns
663                    // FIXME: this doesn't work with recursive subpats (`&mut &mut <err>`)
664                    PatKind::Box(subpat) | PatKind::Ref(subpat, _, _)
665                        if #[allow(non_exhaustive_omitted_patterns)] match subpat.kind {
    PatKind::Err(_) | PatKind::Expr(_) => true,
    _ => false,
}matches!(subpat.kind, PatKind::Err(_) | PatKind::Expr(_)) =>
666                    {
667                        self.maybe_add_suggestions_then_emit(subpat.span, p.span, false)
668                    }
669
670                    // Sub-expressions
671                    PatKind::Range(start, end, _) => {
672                        if let Some(start) = start {
673                            self.maybe_add_suggestions_then_emit(start.span, start.span, true);
674                        }
675
676                        if let Some(end) = end {
677                            self.maybe_add_suggestions_then_emit(end.span, end.span, true);
678                        }
679                    }
680
681                    // Walk continuation
682                    _ => visit::walk_pat(self, p),
683                }
684            }
685        }
686
687        // Starts the visit.
688        PatVisitor { parser: self, stmt, arm: None, field: None }.visit_stmt(stmt);
689    }
690
691    fn eat_metavar_pat(&mut self) -> Option<Pat> {
692        // Must try both kinds of pattern nonterminals.
693        if let Some(pat) = self.eat_metavar_seq_with_matcher(
694            |mv_kind| #[allow(non_exhaustive_omitted_patterns)] match mv_kind {
    MetaVarKind::Pat(PatParam { .. }) => true,
    _ => false,
}matches!(mv_kind, MetaVarKind::Pat(PatParam { .. })),
695            |this| this.parse_pat_no_top_alt(None, None),
696        ) {
697            Some(pat)
698        } else if let Some(pat) = self.eat_metavar_seq(MetaVarKind::Pat(PatWithOr), |this| {
699            this.parse_pat_no_top_guard(
700                None,
701                RecoverComma::No,
702                RecoverColon::No,
703                CommaRecoveryMode::EitherTupleOrPipe,
704            )
705        }) {
706            Some(pat)
707        } else {
708            None
709        }
710    }
711
712    /// Parses a pattern, with a setting whether modern range patterns (e.g., `a..=b`, `a..b` are
713    /// allowed).
714    fn parse_pat_with_range_pat(
715        &mut self,
716        allow_range_pat: bool,
717        expected: Option<Expected>,
718        syntax_loc: Option<PatternLocation>,
719    ) -> PResult<'a, Pat> {
720        if true && self.may_recover() &&
                let Some(mv_kind) = self.token.is_metavar_seq() &&
            let token::MetaVarKind::Ty { .. } = mv_kind &&
        self.check_noexpect_past_close_delim(&token::PathSep) {
    let ty =
        self.eat_metavar_seq(mv_kind,
                |this|
                    this.parse_ty_no_question_mark_recover()).expect("metavar seq ty");
    return self.maybe_recover_from_bad_qpath_stage_2(self.prev_token.span,
            ty);
};maybe_recover_from_interpolated_ty_qpath!(self, true);
721
722        if let Some(pat) = self.eat_metavar_pat() {
723            return Ok(pat);
724        }
725
726        let mut lo = self.token.span;
727
728        if self.token.is_keyword(kw::Let)
729            && self.look_ahead(1, |tok| {
730                tok.can_begin_pattern(token::NtPatKind::PatParam { inferred: false })
731            })
732        {
733            self.bump();
734            // Trim extra space after the `let`
735            let span = lo.with_hi(self.token.span.lo());
736            self.dcx().emit_err(RemoveLet { span: lo, suggestion: span });
737            lo = self.token.span;
738        }
739
740        let pat = if self.check(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::And,
    token_type: crate::parser::token_type::TokenType::And,
}exp!(And)) || self.token == token::AndAnd {
741            self.parse_pat_deref(expected)?
742        } else if self.check(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::OpenParen,
    token_type: crate::parser::token_type::TokenType::OpenParen,
}exp!(OpenParen)) {
743            self.parse_pat_tuple_or_parens()?
744        } else if self.check(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::OpenBracket,
    token_type: crate::parser::token_type::TokenType::OpenBracket,
}exp!(OpenBracket)) {
745            // Parse `[pat, pat,...]` as a slice pattern.
746            let (pats, _) =
747                self.parse_delim_comma_seq(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::OpenBracket,
    token_type: crate::parser::token_type::TokenType::OpenBracket,
}exp!(OpenBracket), crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::CloseBracket,
    token_type: crate::parser::token_type::TokenType::CloseBracket,
}exp!(CloseBracket), |p| {
748                    p.parse_pat_allow_top_guard(
749                        None,
750                        RecoverComma::No,
751                        RecoverColon::No,
752                        CommaRecoveryMode::EitherTupleOrPipe,
753                    )
754                })?;
755            PatKind::Slice(pats)
756        } else if self.check(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::DotDot,
    token_type: crate::parser::token_type::TokenType::DotDot,
}exp!(DotDot)) && !self.is_pat_range_end_start(1) {
757            // A rest pattern `..`.
758            self.bump(); // `..`
759            PatKind::Rest
760        } else if self.check(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::DotDotDot,
    token_type: crate::parser::token_type::TokenType::DotDotDot,
}exp!(DotDotDot)) && !self.is_pat_range_end_start(1) {
761            self.recover_dotdotdot_rest_pat(lo, expected)
762        } else if let Some(form) = self.parse_range_end() {
763            self.parse_pat_range_to(form)? // `..=X`, `...X`, or `..X`.
764        } else if self.eat(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::Bang,
    token_type: crate::parser::token_type::TokenType::Bang,
}exp!(Bang)) {
765            // Parse `!`
766            self.psess.gated_spans.gate(sym::never_patterns, self.prev_token.span);
767            PatKind::Never
768        } else if self.eat_keyword(crate::parser::token_type::ExpKeywordPair {
    kw: rustc_span::symbol::kw::Underscore,
    token_type: crate::parser::token_type::TokenType::KwUnderscore,
}exp!(Underscore)) {
769            // Parse `_`
770            PatKind::Wild
771        } else if self.eat_keyword(crate::parser::token_type::ExpKeywordPair {
    kw: rustc_span::symbol::kw::Mut,
    token_type: crate::parser::token_type::TokenType::KwMut,
}exp!(Mut)) {
772            self.parse_pat_ident_mut()?
773        } else if self.eat_keyword(crate::parser::token_type::ExpKeywordPair {
    kw: rustc_span::symbol::kw::Ref,
    token_type: crate::parser::token_type::TokenType::KwRef,
}exp!(Ref)) {
774            if self.check_keyword(crate::parser::token_type::ExpKeywordPair {
    kw: rustc_span::symbol::kw::Box,
    token_type: crate::parser::token_type::TokenType::KwBox,
}exp!(Box)) {
775                // Suggest `box ref`.
776                let span = self.prev_token.span.to(self.token.span);
777                self.bump();
778                self.dcx().emit_err(SwitchRefBoxOrder { span });
779            }
780            // Parse ref ident @ pat / ref mut ident @ pat / ref pin const|mut ident @ pat
781            let (pinned, mutbl) = self.parse_pin_and_mut();
782            self.parse_pat_ident(
783                BindingMode(ByRef::Yes(pinned, mutbl), Mutability::Not),
784                syntax_loc,
785            )?
786        } else if self.eat_keyword(crate::parser::token_type::ExpKeywordPair {
    kw: rustc_span::symbol::kw::Box,
    token_type: crate::parser::token_type::TokenType::KwBox,
}exp!(Box)) {
787            self.parse_pat_box()?
788        } else if self.check_inline_const(0) {
789            // Parse `const pat`
790            let const_expr = self.parse_const_block(lo.to(self.token.span), true)?;
791
792            if let Some(re) = self.parse_range_end() {
793                self.parse_pat_range_begin_with(const_expr, re)?
794            } else {
795                PatKind::Expr(const_expr)
796            }
797        } else if self.is_builtin() {
798            self.parse_pat_builtin()?
799        }
800        // Don't eagerly error on semantically invalid tokens when matching
801        // declarative macros, as the input to those doesn't have to be
802        // semantically valid. For attribute/derive proc macros this is not the
803        // case, so doing the recovery for them is fine.
804        else if self.can_be_ident_pat()
805            || (self.is_lit_bad_ident().is_some() && self.may_recover())
806        {
807            // Parse `ident @ pat`
808            // This can give false positives and parse nullary enums,
809            // they are dealt with later in resolve.
810            self.parse_pat_ident(BindingMode::NONE, syntax_loc)?
811        } else if self.is_start_of_pat_with_path() {
812            // Parse pattern starting with a path
813            let (qself, path) = if self.eat_lt() {
814                // Parse a qualified path
815                let (qself, path) = self.parse_qpath(PathStyle::Pat)?;
816                (Some(qself), path)
817            } else {
818                // Parse an unqualified path
819                (None, self.parse_path(PathStyle::Pat)?)
820            };
821            let span = lo.to(self.prev_token.span);
822
823            if qself.is_none() && self.check(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::Bang,
    token_type: crate::parser::token_type::TokenType::Bang,
}exp!(Bang)) {
824                self.parse_pat_mac_invoc(path)?
825            } else if let Some(form) = self.parse_range_end() {
826                let begin = self.mk_expr(span, ExprKind::Path(qself, path));
827                self.parse_pat_range_begin_with(begin, form)?
828            } else if self.check(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::OpenBrace,
    token_type: crate::parser::token_type::TokenType::OpenBrace,
}exp!(OpenBrace)) {
829                self.parse_pat_struct(qself, path)?
830            } else if self.check(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::OpenParen,
    token_type: crate::parser::token_type::TokenType::OpenParen,
}exp!(OpenParen)) {
831                self.parse_pat_tuple_struct(qself, path)?
832            } else {
833                match self.maybe_recover_trailing_expr(span, false) {
834                    Some((guar, _)) => PatKind::Err(guar),
835                    None => PatKind::Path(qself, path),
836                }
837            }
838        } else if let Some((lt, IdentIsRaw::No)) = self.token.lifetime()
839            // In pattern position, we're totally fine with using "next token isn't colon"
840            // as a heuristic. We could probably just always try to recover if it's a lifetime,
841            // because we never have `'a: label {}` in a pattern position anyways, but it does
842            // keep us from suggesting something like `let 'a: Ty = ..` => `let 'a': Ty = ..`
843            && could_be_unclosed_char_literal(lt)
844            && !self.look_ahead(1, |token| token.kind == token::Colon)
845        {
846            // Recover a `'a` as a `'a'` literal
847            let lt = self.expect_lifetime();
848            let (lit, _) =
849                self.recover_unclosed_char(lt.ident, Parser::mk_token_lit_char, |self_| {
850                    let expected = Expected::to_string_or_fallback(expected);
851                    let msg = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("expected {0}, found {1}", expected,
                super::token_descr(&self_.token)))
    })format!(
852                        "expected {}, found {}",
853                        expected,
854                        super::token_descr(&self_.token)
855                    );
856
857                    self_
858                        .dcx()
859                        .struct_span_err(self_.token.span, msg)
860                        .with_span_label(self_.token.span, ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("expected {0}", expected))
    })format!("expected {expected}"))
861                });
862            PatKind::Expr(self.mk_expr(lo, ExprKind::Lit(lit)))
863        } else {
864            // Try to parse everything else as literal with optional minus
865            match self.parse_literal_maybe_minus() {
866                Ok(begin) => {
867                    let begin = self
868                        .maybe_recover_trailing_expr(begin.span, false)
869                        .map(|(guar, sp)| self.mk_expr_err(sp, guar))
870                        .unwrap_or(begin);
871
872                    match self.parse_range_end() {
873                        Some(form) => self.parse_pat_range_begin_with(begin, form)?,
874                        None => PatKind::Expr(begin),
875                    }
876                }
877                Err(err) => return self.fatal_unexpected_non_pat(err, expected),
878            }
879        };
880
881        let mut pat = self.mk_pat(lo.to(self.prev_token.span), pat);
882
883        pat = self.maybe_recover_from_bad_qpath(pat)?;
884        if self.eat_noexpect(&token::At) {
885            pat = self.recover_intersection_pat(pat)?;
886        }
887
888        if !allow_range_pat {
889            self.ban_pat_range_if_ambiguous(&pat)
890        }
891
892        Ok(pat)
893    }
894
895    /// Recover from a typoed `...` pattern that was encountered
896    /// Ref: Issue #70388
897    fn recover_dotdotdot_rest_pat(&mut self, lo: Span, expected: Option<Expected>) -> PatKind {
898        // A typoed rest pattern `...`.
899        self.bump(); // `...`
900
901        if let Some(Expected::ParameterName) = expected {
902            // We have `...` in a closure argument, likely meant to be var-arg, which aren't
903            // supported in closures (#146489).
904            PatKind::Err(self.dcx().emit_err(DotDotDotRestPattern {
905                span: lo,
906                suggestion: None,
907                var_args: Some(()),
908            }))
909        } else {
910            // The user probably mistook `...` for a rest pattern `..`.
911            self.dcx().emit_err(DotDotDotRestPattern {
912                span: lo,
913                suggestion: Some(lo.with_lo(lo.hi() - BytePos(1))),
914                var_args: None,
915            });
916            PatKind::Rest
917        }
918    }
919
920    /// Try to recover the more general form `intersect ::= $pat_lhs @ $pat_rhs`.
921    ///
922    /// Allowed binding patterns generated by `binding ::= ref? mut? $ident @ $pat_rhs`
923    /// should already have been parsed by now at this point,
924    /// if the next token is `@` then we can try to parse the more general form.
925    ///
926    /// Consult `parse_pat_ident` for the `binding` grammar.
927    ///
928    /// The notion of intersection patterns are found in
929    /// e.g. [F#][and] where they are called AND-patterns.
930    ///
931    /// [and]: https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/pattern-matching
932    #[cold]
933    fn recover_intersection_pat(&mut self, lhs: Pat) -> PResult<'a, Pat> {
934        let mut rhs = self.parse_pat_no_top_alt(None, None)?;
935        let whole_span = lhs.span.to(rhs.span);
936
937        if let PatKind::Ident(_, _, sub @ None) = &mut rhs.kind {
938            // The user inverted the order, so help them fix that.
939            let lhs_span = lhs.span;
940            // Move the LHS into the RHS as a subpattern.
941            // The RHS is now the full pattern.
942            *sub = Some(Box::new(lhs));
943
944            self.dcx().emit_err(PatternOnWrongSideOfAt {
945                whole_span,
946                whole_pat: pprust::pat_to_string(&rhs),
947                pattern: lhs_span,
948                binding: rhs.span,
949            });
950        } else {
951            // The special case above doesn't apply so we may have e.g. `A(x) @ B(y)`.
952            rhs.kind = PatKind::Wild;
953            self.dcx().emit_err(ExpectedBindingLeftOfAt {
954                whole_span,
955                lhs: lhs.span,
956                rhs: rhs.span,
957            });
958        }
959
960        rhs.span = whole_span;
961        Ok(rhs)
962    }
963
964    /// Ban a range pattern if it has an ambiguous interpretation.
965    fn ban_pat_range_if_ambiguous(&self, pat: &Pat) {
966        match pat.kind {
967            PatKind::Range(
968                ..,
969                Spanned { node: RangeEnd::Included(RangeSyntax::DotDotDot), .. },
970            ) => return,
971            PatKind::Range(..) => {}
972            _ => return,
973        }
974
975        self.dcx().emit_err(AmbiguousRangePattern {
976            span: pat.span,
977            suggestion: ParenRangeSuggestion {
978                lo: pat.span.shrink_to_lo(),
979                hi: pat.span.shrink_to_hi(),
980            },
981        });
982    }
983
984    /// Parse `&pat` / `&mut pat` / `&pin const pat` / `&pin mut pat`.
985    fn parse_pat_deref(&mut self, expected: Option<Expected>) -> PResult<'a, PatKind> {
986        self.expect_and()?;
987        if let Some((lifetime, _)) = self.token.lifetime() {
988            self.bump(); // `'a`
989
990            self.dcx().emit_err(UnexpectedLifetimeInPattern {
991                span: self.prev_token.span,
992                symbol: lifetime.name,
993                suggestion: self.prev_token.span.until(self.token.span),
994            });
995        }
996
997        let (pinned, mutbl) = self.parse_pin_and_mut();
998        let subpat = self.parse_pat_with_range_pat(false, expected, None)?;
999        Ok(PatKind::Ref(Box::new(subpat), pinned, mutbl))
1000    }
1001
1002    /// Parse a tuple or parenthesis pattern.
1003    fn parse_pat_tuple_or_parens(&mut self) -> PResult<'a, PatKind> {
1004        let open_paren = self.token.span;
1005
1006        let (fields, trailing_comma) = self.parse_paren_comma_seq(|p| {
1007            p.parse_pat_allow_top_guard(
1008                None,
1009                RecoverComma::No,
1010                RecoverColon::No,
1011                CommaRecoveryMode::LikelyTuple,
1012            )
1013        })?;
1014
1015        // Here, `(pat,)` is a tuple pattern.
1016        // For backward compatibility, `(..)` is a tuple pattern as well.
1017        let paren_pattern =
1018            fields.len() == 1 && !(#[allow(non_exhaustive_omitted_patterns)] match trailing_comma {
    Trailing::Yes => true,
    _ => false,
}matches!(trailing_comma, Trailing::Yes) || fields[0].is_rest());
1019
1020        let pat = if paren_pattern {
1021            let pat = fields.into_iter().next().unwrap();
1022            let close_paren = self.prev_token.span;
1023
1024            match &pat.kind {
1025                // recover ranges with parentheses around the `(start)..`
1026                PatKind::Expr(begin)
1027                    if self.may_recover()
1028                        && let Some(form) = self.parse_range_end() =>
1029                {
1030                    self.dcx().emit_err(UnexpectedParenInRangePat {
1031                        span: ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [open_paren, close_paren]))vec![open_paren, close_paren],
1032                        sugg: UnexpectedParenInRangePatSugg {
1033                            start_span: open_paren,
1034                            end_span: close_paren,
1035                        },
1036                    });
1037
1038                    self.parse_pat_range_begin_with(begin.clone(), form)?
1039                }
1040                // recover ranges with parentheses around the `(start)..`
1041                PatKind::Err(guar)
1042                    if self.may_recover()
1043                        && let Some(form) = self.parse_range_end() =>
1044                {
1045                    self.dcx().emit_err(UnexpectedParenInRangePat {
1046                        span: ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [open_paren, close_paren]))vec![open_paren, close_paren],
1047                        sugg: UnexpectedParenInRangePatSugg {
1048                            start_span: open_paren,
1049                            end_span: close_paren,
1050                        },
1051                    });
1052
1053                    self.parse_pat_range_begin_with(self.mk_expr_err(pat.span, *guar), form)?
1054                }
1055
1056                // (pat) with optional parentheses
1057                _ => PatKind::Paren(Box::new(pat)),
1058            }
1059        } else {
1060            PatKind::Tuple(fields)
1061        };
1062
1063        Ok(match self.maybe_recover_trailing_expr(open_paren.to(self.prev_token.span), false) {
1064            None => pat,
1065            Some((guar, _)) => PatKind::Err(guar),
1066        })
1067    }
1068
1069    /// Parse a mutable binding with the `mut` token already eaten.
1070    fn parse_pat_ident_mut(&mut self) -> PResult<'a, PatKind> {
1071        let mut_span = self.prev_token.span;
1072
1073        self.recover_additional_muts();
1074
1075        let byref = self.parse_byref();
1076
1077        self.recover_additional_muts();
1078
1079        // Make sure we don't allow e.g. `let mut $p;` where `$p:pat`.
1080        if let Some(MetaVarKind::Pat(_)) = self.token.is_metavar_seq() {
1081            self.expected_ident_found_err().emit();
1082        }
1083
1084        // Parse the pattern we hope to be an identifier.
1085        let mut pat = self.parse_pat_no_top_alt(Some(Expected::Identifier), None)?;
1086
1087        // If we don't have `mut $ident (@ pat)?`, error.
1088        if let PatKind::Ident(BindingMode(br @ ByRef::No, m @ Mutability::Not), ..) = &mut pat.kind
1089        {
1090            // Don't recurse into the subpattern.
1091            // `mut` on the outer binding doesn't affect the inner bindings.
1092            *br = byref;
1093            *m = Mutability::Mut;
1094        } else {
1095            // Add `mut` to any binding in the parsed pattern.
1096            let changed_any_binding = Self::make_all_value_bindings_mutable(&mut pat);
1097            self.ban_mut_general_pat(mut_span, &pat, changed_any_binding);
1098        }
1099
1100        if #[allow(non_exhaustive_omitted_patterns)] match pat.kind {
    PatKind::Ident(BindingMode(ByRef::Yes(..), Mutability::Mut), ..) => true,
    _ => false,
}matches!(pat.kind, PatKind::Ident(BindingMode(ByRef::Yes(..), Mutability::Mut), ..)) {
1101            self.psess.gated_spans.gate(sym::mut_ref, pat.span);
1102        }
1103        Ok(pat.kind)
1104    }
1105
1106    /// Turn all by-value immutable bindings in a pattern into mutable bindings.
1107    /// Returns `true` if any change was made.
1108    fn make_all_value_bindings_mutable(pat: &mut Pat) -> bool {
1109        struct AddMut(bool);
1110        impl MutVisitor for AddMut {
1111            fn visit_pat(&mut self, pat: &mut Pat) {
1112                if let PatKind::Ident(BindingMode(ByRef::No, m @ Mutability::Not), ..) =
1113                    &mut pat.kind
1114                {
1115                    self.0 = true;
1116                    *m = Mutability::Mut;
1117                }
1118                mut_visit::walk_pat(self, pat);
1119            }
1120        }
1121
1122        let mut add_mut = AddMut(false);
1123        add_mut.visit_pat(pat);
1124        add_mut.0
1125    }
1126
1127    /// Error on `mut $pat` where `$pat` is not an ident.
1128    fn ban_mut_general_pat(&self, lo: Span, pat: &Pat, changed_any_binding: bool) {
1129        self.dcx().emit_err(if changed_any_binding {
1130            InvalidMutInPattern::NestedIdent {
1131                span: lo.to(pat.span),
1132                pat: pprust::pat_to_string(pat),
1133            }
1134        } else {
1135            InvalidMutInPattern::NonIdent { span: lo.until(pat.span) }
1136        });
1137    }
1138
1139    /// Eat any extraneous `mut`s and error + recover if we ate any.
1140    fn recover_additional_muts(&mut self) {
1141        let lo = self.token.span;
1142        while self.eat_keyword(crate::parser::token_type::ExpKeywordPair {
    kw: rustc_span::symbol::kw::Mut,
    token_type: crate::parser::token_type::TokenType::KwMut,
}exp!(Mut)) {}
1143        if lo == self.token.span {
1144            return;
1145        }
1146
1147        let span = lo.to(self.prev_token.span);
1148        let suggestion = span.with_hi(self.token.span.lo());
1149        self.dcx().emit_err(RepeatedMutInPattern { span, suggestion });
1150    }
1151
1152    /// Parse macro invocation
1153    fn parse_pat_mac_invoc(&mut self, path: Path) -> PResult<'a, PatKind> {
1154        self.bump();
1155        let args = self.parse_delim_args()?;
1156        let mac = Box::new(MacCall { path, args });
1157        Ok(PatKind::MacCall(mac))
1158    }
1159
1160    fn fatal_unexpected_non_pat(
1161        &mut self,
1162        err: Diag<'a>,
1163        expected: Option<Expected>,
1164    ) -> PResult<'a, Pat> {
1165        err.cancel();
1166
1167        let expected = Expected::to_string_or_fallback(expected);
1168        let msg = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("expected {0}, found {1}", expected,
                super::token_descr(&self.token)))
    })format!("expected {}, found {}", expected, super::token_descr(&self.token));
1169
1170        let mut err = self.dcx().struct_span_err(self.token.span, msg);
1171        err.span_label(self.token.span, ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("expected {0}", expected))
    })format!("expected {expected}"));
1172
1173        let sp = self.psess.source_map().start_point(self.token.span);
1174        if let Some(sp) = self.psess.ambiguous_block_expr_parse.borrow().get(&sp) {
1175            err.subdiagnostic(ExprParenthesesNeeded::surrounding(*sp));
1176        }
1177
1178        Err(err)
1179    }
1180
1181    /// Parses the range pattern end form `".." | "..." | "..=" ;`.
1182    fn parse_range_end(&mut self) -> Option<Spanned<RangeEnd>> {
1183        let re = if self.eat(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::DotDotDot,
    token_type: crate::parser::token_type::TokenType::DotDotDot,
}exp!(DotDotDot)) {
1184            RangeEnd::Included(RangeSyntax::DotDotDot)
1185        } else if self.eat(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::DotDotEq,
    token_type: crate::parser::token_type::TokenType::DotDotEq,
}exp!(DotDotEq)) {
1186            RangeEnd::Included(RangeSyntax::DotDotEq)
1187        } else if self.eat(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::DotDot,
    token_type: crate::parser::token_type::TokenType::DotDot,
}exp!(DotDot)) {
1188            RangeEnd::Excluded
1189        } else {
1190            return None;
1191        };
1192        Some(respan(self.prev_token.span, re))
1193    }
1194
1195    /// Parse a range pattern `$begin $form $end?` where `$form = ".." | "..." | "..=" ;`.
1196    /// `$begin $form` has already been parsed.
1197    fn parse_pat_range_begin_with(
1198        &mut self,
1199        begin: Box<Expr>,
1200        re: Spanned<RangeEnd>,
1201    ) -> PResult<'a, PatKind> {
1202        let end = if self.is_pat_range_end_start(0) {
1203            // Parsing e.g. `X..=Y`.
1204            Some(self.parse_pat_range_end()?)
1205        } else {
1206            // Parsing e.g. `X..`.
1207            if let RangeEnd::Included(_) = re.node {
1208                // FIXME(Centril): Consider semantic errors instead in `ast_validation`.
1209                self.inclusive_range_with_incorrect_end();
1210            }
1211            None
1212        };
1213        Ok(PatKind::Range(Some(begin), end, re))
1214    }
1215
1216    pub(super) fn inclusive_range_with_incorrect_end(&mut self) -> ErrorGuaranteed {
1217        let tok = &self.token;
1218        let span = self.prev_token.span;
1219        // If the user typed "..==" instead of "..=", we want to give them
1220        // a specific error message telling them to use "..=".
1221        // If they typed "..=>", suggest they use ".. =>".
1222        // Otherwise, we assume that they meant to type a half open exclusive
1223        // range and give them an error telling them to do that instead.
1224        let no_space = tok.span.lo() == span.hi();
1225        match tok.kind {
1226            token::Eq if no_space => {
1227                let span_with_eq = span.to(tok.span);
1228
1229                // Ensure the user doesn't receive unhelpful unexpected token errors
1230                self.bump();
1231                if self.is_pat_range_end_start(0) {
1232                    let _ = self.parse_pat_range_end().map_err(|e| e.cancel());
1233                }
1234
1235                self.dcx().emit_err(InclusiveRangeExtraEquals { span: span_with_eq })
1236            }
1237            token::Gt if no_space => {
1238                let after_pat = span.with_hi(span.hi() - BytePos(1)).shrink_to_hi();
1239                self.dcx().emit_err(InclusiveRangeMatchArrow { span, arrow: tok.span, after_pat })
1240            }
1241            _ => self.dcx().emit_err(InclusiveRangeNoEnd {
1242                span,
1243                suggestion: span.with_lo(span.hi() - BytePos(1)),
1244            }),
1245        }
1246    }
1247
1248    /// Parse a range-to pattern, `..X` or `..=X` where `X` remains to be parsed.
1249    ///
1250    /// The form `...X` is prohibited to reduce confusion with the potential
1251    /// expression syntax `...expr` for splatting in expressions.
1252    fn parse_pat_range_to(&mut self, mut re: Spanned<RangeEnd>) -> PResult<'a, PatKind> {
1253        let end = self.parse_pat_range_end()?;
1254        if let RangeEnd::Included(syn @ RangeSyntax::DotDotDot) = &mut re.node {
1255            *syn = RangeSyntax::DotDotEq;
1256            self.dcx().emit_err(DotDotDotRangeToPatternNotAllowed { span: re.span });
1257        }
1258        Ok(PatKind::Range(None, Some(end), re))
1259    }
1260
1261    /// Is the token `dist` away from the current suitable as the start of a range patterns end?
1262    fn is_pat_range_end_start(&self, dist: usize) -> bool {
1263        self.check_inline_const(dist)
1264            || self.look_ahead(dist, |t| {
1265                t.is_path_start() // e.g. `MY_CONST`;
1266                || *t == token::Dot // e.g. `.5` for recovery;
1267                || #[allow(non_exhaustive_omitted_patterns)] match t.kind {
    token::Literal(..) | token::Minus => true,
    _ => false,
}matches!(t.kind, token::Literal(..) | token::Minus)
1268                || t.is_bool_lit()
1269                || t.is_metavar_expr()
1270                || t.is_lifetime() // recover `'a` instead of `'a'`
1271                || (self.may_recover() // recover leading `(`
1272                    && *t == token::OpenParen
1273                    && self.look_ahead(dist + 1, |t| *t != token::OpenParen)
1274                    && self.is_pat_range_end_start(dist + 1))
1275            })
1276    }
1277
1278    /// Parse a range pattern end bound
1279    fn parse_pat_range_end(&mut self) -> PResult<'a, Box<Expr>> {
1280        // recover leading `(`
1281        let open_paren = (self.may_recover() && self.eat_noexpect(&token::OpenParen))
1282            .then_some(self.prev_token.span);
1283
1284        let bound = if self.check_inline_const(0) {
1285            self.parse_const_block(self.token.span, true)
1286        } else if self.check_path() {
1287            let lo = self.token.span;
1288            let (qself, path) = if self.eat_lt() {
1289                // Parse a qualified path
1290                let (qself, path) = self.parse_qpath(PathStyle::Pat)?;
1291                (Some(qself), path)
1292            } else {
1293                // Parse an unqualified path
1294                (None, self.parse_path(PathStyle::Pat)?)
1295            };
1296            let hi = self.prev_token.span;
1297            Ok(self.mk_expr(lo.to(hi), ExprKind::Path(qself, path)))
1298        } else {
1299            self.parse_literal_maybe_minus()
1300        }?;
1301
1302        let recovered = self.maybe_recover_trailing_expr(bound.span, true);
1303
1304        // recover trailing `)`
1305        if let Some(open_paren) = open_paren {
1306            self.expect(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::CloseParen,
    token_type: crate::parser::token_type::TokenType::CloseParen,
}exp!(CloseParen))?;
1307
1308            self.dcx().emit_err(UnexpectedParenInRangePat {
1309                span: ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [open_paren, self.prev_token.span]))vec![open_paren, self.prev_token.span],
1310                sugg: UnexpectedParenInRangePatSugg {
1311                    start_span: open_paren,
1312                    end_span: self.prev_token.span,
1313                },
1314            });
1315        }
1316
1317        Ok(match recovered {
1318            Some((guar, sp)) => self.mk_expr_err(sp, guar),
1319            None => bound,
1320        })
1321    }
1322
1323    /// Is this the start of a pattern beginning with a path?
1324    fn is_start_of_pat_with_path(&mut self) -> bool {
1325        self.check_path()
1326        // Just for recovery (see `can_be_ident`).
1327        || self.token.is_ident() && !self.token.is_bool_lit() && !self.token.is_keyword(kw::In)
1328    }
1329
1330    /// Would `parse_pat_ident` be appropriate here?
1331    fn can_be_ident_pat(&mut self) -> bool {
1332        self.check_ident()
1333        && !self.token.is_bool_lit() // Avoid `true` or `false` as a binding as it is a literal.
1334        && !self.token.is_path_segment_keyword() // Avoid e.g. `Self` as it is a path.
1335        // Avoid `in`. Due to recovery in the list parser this messes with `for ( $pat in $expr )`.
1336        && !self.token.is_keyword(kw::In)
1337        // Try to do something more complex?
1338        && self.look_ahead(1, |t| !#[allow(non_exhaustive_omitted_patterns)] match t.kind {
    token::OpenParen | token::OpenBrace | token::DotDotDot | token::DotDotEq |
        token::DotDot | token::PathSep | token::Bang => true,
    _ => false,
}matches!(t.kind, token::OpenParen // A tuple struct pattern.
1339            | token::OpenBrace // A struct pattern.
1340            | token::DotDotDot | token::DotDotEq | token::DotDot // A range pattern.
1341            | token::PathSep // A tuple / struct variant pattern.
1342            | token::Bang)) // A macro expanding to a pattern.
1343    }
1344
1345    /// Parses `ident` or `ident @ pat`.
1346    /// Used by the copy foo and ref foo patterns to give a good
1347    /// error message when parsing mistakes like `ref foo(a, b)`.
1348    fn parse_pat_ident(
1349        &mut self,
1350        binding_annotation: BindingMode,
1351        syntax_loc: Option<PatternLocation>,
1352    ) -> PResult<'a, PatKind> {
1353        let ident = self.parse_ident_common(false)?;
1354
1355        if self.may_recover()
1356            && !#[allow(non_exhaustive_omitted_patterns)] match syntax_loc {
    Some(PatternLocation::FunctionParameter) => true,
    _ => false,
}matches!(syntax_loc, Some(PatternLocation::FunctionParameter))
1357            && self.check_noexpect(&token::Lt)
1358            && self.look_ahead(1, |t| t.can_begin_type())
1359        {
1360            return Err(self.dcx().create_err(GenericArgsInPatRequireTurbofishSyntax {
1361                span: self.token.span,
1362                suggest_turbofish: self.token.span.shrink_to_lo(),
1363            }));
1364        }
1365
1366        let sub = if self.eat(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::At,
    token_type: crate::parser::token_type::TokenType::At,
}exp!(At)) {
1367            Some(Box::new(self.parse_pat_no_top_alt(Some(Expected::BindingPattern), None)?))
1368        } else {
1369            None
1370        };
1371
1372        // Just to be friendly, if they write something like `ref Some(i)`,
1373        // we end up here with `(` as the current token.
1374        // This shortly leads to a parse error. Note that if there is no explicit
1375        // binding mode then we do not end up here, because the lookahead
1376        // will direct us over to `parse_enum_variant()`.
1377        if self.token == token::OpenParen {
1378            return Err(self
1379                .dcx()
1380                .create_err(EnumPatternInsteadOfIdentifier { span: self.prev_token.span }));
1381        }
1382
1383        // Check for method calls after the `ident`,
1384        // but not `ident @ subpat` as `subpat` was already checked and `ident` continues with `@`.
1385
1386        let pat = if sub.is_none()
1387            && let Some((guar, _)) = self.maybe_recover_trailing_expr(ident.span, false)
1388        {
1389            PatKind::Err(guar)
1390        } else {
1391            PatKind::Ident(binding_annotation, ident, sub)
1392        };
1393        Ok(pat)
1394    }
1395
1396    /// Parse a struct ("record") pattern (e.g. `Foo { ... }` or `Foo::Bar { ... }`).
1397    fn parse_pat_struct(&mut self, qself: Option<Box<QSelf>>, path: Path) -> PResult<'a, PatKind> {
1398        if qself.is_some() {
1399            // Feature gate the use of qualified paths in patterns
1400            self.psess.gated_spans.gate(sym::more_qualified_paths, path.span);
1401        }
1402        self.bump();
1403        let (fields, etc) = self.parse_pat_fields().unwrap_or_else(|mut e| {
1404            e.span_label(path.span, "while parsing the fields for this pattern");
1405            let guar = e.emit();
1406            self.recover_stmt();
1407            // When recovering, pretend we had `Foo { .. }`, to avoid cascading errors.
1408            (ThinVec::new(), PatFieldsRest::Recovered(guar))
1409        });
1410        self.bump();
1411        Ok(PatKind::Struct(qself, path, fields, etc))
1412    }
1413
1414    /// Parse tuple struct or tuple variant pattern (e.g. `Foo(...)` or `Foo::Bar(...)`).
1415    fn parse_pat_tuple_struct(
1416        &mut self,
1417        qself: Option<Box<QSelf>>,
1418        path: Path,
1419    ) -> PResult<'a, PatKind> {
1420        let (fields, _) = self.parse_paren_comma_seq(|p| {
1421            p.parse_pat_allow_top_guard(
1422                None,
1423                RecoverComma::No,
1424                RecoverColon::No,
1425                CommaRecoveryMode::EitherTupleOrPipe,
1426            )
1427        })?;
1428        if qself.is_some() {
1429            self.psess.gated_spans.gate(sym::more_qualified_paths, path.span);
1430        }
1431        Ok(PatKind::TupleStruct(qself, path, fields))
1432    }
1433
1434    /// Are we sure this could not possibly be the start of a pattern?
1435    ///
1436    /// Currently, this only accounts for tokens that can follow identifiers
1437    /// in patterns, but this can be extended as necessary.
1438    fn isnt_pattern_start(&self) -> bool {
1439        [
1440            token::Eq,
1441            token::Colon,
1442            token::Comma,
1443            token::Semi,
1444            token::At,
1445            token::OpenBrace,
1446            token::CloseBrace,
1447            token::CloseParen,
1448        ]
1449        .contains(&self.token.kind)
1450    }
1451
1452    fn parse_pat_builtin(&mut self) -> PResult<'a, PatKind> {
1453        self.parse_builtin(|self_, _lo, ident| {
1454            Ok(match ident.name {
1455                // builtin#deref(PAT)
1456                sym::deref => {
1457                    Some(ast::PatKind::Deref(Box::new(self_.parse_pat_allow_top_guard(
1458                        None,
1459                        RecoverComma::Yes,
1460                        RecoverColon::Yes,
1461                        CommaRecoveryMode::LikelyTuple,
1462                    )?)))
1463                }
1464                _ => None,
1465            })
1466        })
1467    }
1468
1469    /// Parses `box pat`
1470    fn parse_pat_box(&mut self) -> PResult<'a, PatKind> {
1471        let box_span = self.prev_token.span;
1472
1473        if self.isnt_pattern_start() {
1474            let descr = super::token_descr(&self.token);
1475            self.dcx().emit_err(errors::BoxNotPat {
1476                span: self.token.span,
1477                kw: box_span,
1478                lo: box_span.shrink_to_lo(),
1479                descr,
1480            });
1481
1482            // We cannot use `parse_pat_ident()` since it will complain `box`
1483            // is not an identifier.
1484            let sub = if self.eat(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::At,
    token_type: crate::parser::token_type::TokenType::At,
}exp!(At)) {
1485                Some(Box::new(self.parse_pat_no_top_alt(Some(Expected::BindingPattern), None)?))
1486            } else {
1487                None
1488            };
1489
1490            Ok(PatKind::Ident(BindingMode::NONE, Ident::new(kw::Box, box_span), sub))
1491        } else {
1492            let pat = Box::new(self.parse_pat_with_range_pat(false, None, None)?);
1493            self.psess.gated_spans.gate(sym::box_patterns, box_span.to(self.prev_token.span));
1494            Ok(PatKind::Box(pat))
1495        }
1496    }
1497
1498    /// Parses the fields of a struct-like pattern.
1499    fn parse_pat_fields(&mut self) -> PResult<'a, (ThinVec<PatField>, PatFieldsRest)> {
1500        let mut fields: ThinVec<PatField> = ThinVec::new();
1501        let mut etc = PatFieldsRest::None;
1502        let mut ate_comma = true;
1503        let mut delayed_err: Option<Diag<'a>> = None;
1504        let mut first_etc_and_maybe_comma_span = None;
1505        let mut last_non_comma_dotdot_span = None;
1506
1507        while self.token != token::CloseBrace {
1508            // check that a comma comes after every field
1509            if !ate_comma {
1510                let err = if self.token == token::At {
1511                    let prev_field = fields
1512                        .last()
1513                        .expect("Unreachable on first iteration, not empty otherwise")
1514                        .ident;
1515                    self.report_misplaced_at_in_struct_pat(prev_field)
1516                } else {
1517                    let mut err = self
1518                        .dcx()
1519                        .create_err(ExpectedCommaAfterPatternField { span: self.token.span });
1520                    self.recover_misplaced_pattern_modifiers(&fields, &mut err);
1521                    err
1522                };
1523                if let Some(delayed) = delayed_err {
1524                    delayed.emit();
1525                }
1526                return Err(err);
1527            }
1528            ate_comma = false;
1529
1530            if self.check(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::DotDot,
    token_type: crate::parser::token_type::TokenType::DotDot,
}exp!(DotDot))
1531                || self.check_noexpect(&token::DotDotDot)
1532                || self.check_keyword(crate::parser::token_type::ExpKeywordPair {
    kw: rustc_span::symbol::kw::Underscore,
    token_type: crate::parser::token_type::TokenType::KwUnderscore,
}exp!(Underscore))
1533            {
1534                etc = PatFieldsRest::Rest(self.token.span);
1535                let mut etc_sp = self.token.span;
1536                if first_etc_and_maybe_comma_span.is_none() {
1537                    if let Some(comma_tok) =
1538                        self.look_ahead(1, |&t| if t == token::Comma { Some(t) } else { None })
1539                    {
1540                        let nw_span = self
1541                            .psess
1542                            .source_map()
1543                            .span_extend_to_line(comma_tok.span)
1544                            .trim_start(comma_tok.span.shrink_to_lo())
1545                            .map(|s| self.psess.source_map().span_until_non_whitespace(s));
1546                        first_etc_and_maybe_comma_span = nw_span.map(|s| etc_sp.to(s));
1547                    } else {
1548                        first_etc_and_maybe_comma_span =
1549                            Some(self.psess.source_map().span_until_non_whitespace(etc_sp));
1550                    }
1551                }
1552
1553                self.recover_bad_dot_dot();
1554                self.bump(); // `..` || `...` || `_`
1555
1556                if self.token == token::CloseBrace {
1557                    break;
1558                }
1559                let token_str = super::token_descr(&self.token);
1560                let msg = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("expected `}}`, found {0}",
                token_str))
    })format!("expected `}}`, found {token_str}");
1561                let mut err = self.dcx().struct_span_err(self.token.span, msg);
1562
1563                err.span_label(self.token.span, "expected `}`");
1564                let mut comma_sp = None;
1565                if self.token == token::Comma {
1566                    // Issue #49257
1567                    let nw_span =
1568                        self.psess.source_map().span_until_non_whitespace(self.token.span);
1569                    etc_sp = etc_sp.to(nw_span);
1570                    err.span_label(
1571                        etc_sp,
1572                        "`..` must be at the end and cannot have a trailing comma",
1573                    );
1574                    comma_sp = Some(self.token.span);
1575                    self.bump();
1576                    ate_comma = true;
1577                }
1578
1579                if self.token == token::CloseBrace {
1580                    // If the struct looks otherwise well formed, recover and continue.
1581                    if let Some(sp) = comma_sp {
1582                        err.span_suggestion_short(
1583                            sp,
1584                            "remove this comma",
1585                            "",
1586                            Applicability::MachineApplicable,
1587                        );
1588                    }
1589                    err.emit();
1590                    break;
1591                } else if self.token.is_ident() && ate_comma {
1592                    // Accept fields coming after `..,`.
1593                    // This way we avoid "pattern missing fields" errors afterwards.
1594                    // We delay this error until the end in order to have a span for a
1595                    // suggested fix.
1596                    if let Some(delayed_err) = delayed_err {
1597                        delayed_err.emit();
1598                        return Err(err);
1599                    } else {
1600                        delayed_err = Some(err);
1601                    }
1602                } else {
1603                    if let Some(err) = delayed_err {
1604                        err.emit();
1605                    }
1606                    return Err(err);
1607                }
1608            }
1609
1610            let attrs = match self.parse_outer_attributes() {
1611                Ok(attrs) => attrs,
1612                Err(err) => {
1613                    if let Some(delayed) = delayed_err {
1614                        delayed.emit();
1615                    }
1616                    return Err(err);
1617                }
1618            };
1619            let lo = self.token.span;
1620
1621            let field = self.collect_tokens(None, attrs, ForceCollect::No, |this, attrs| {
1622                let field = match this.parse_pat_field(lo, attrs) {
1623                    Ok(field) => Ok(field),
1624                    Err(err) => {
1625                        if let Some(delayed_err) = delayed_err.take() {
1626                            delayed_err.emit();
1627                        }
1628                        return Err(err);
1629                    }
1630                }?;
1631                ate_comma = this.eat(crate::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::Comma,
    token_type: crate::parser::token_type::TokenType::Comma,
}exp!(Comma));
1632
1633                last_non_comma_dotdot_span = Some(this.prev_token.span);
1634
1635                // We just ate a comma, so there's no need to capture a trailing token.
1636                Ok((field, Trailing::No, UsePreAttrPos::No))
1637            })?;
1638
1639            fields.push(field)
1640        }
1641
1642        if let Some(mut err) = delayed_err {
1643            if let Some(first_etc_span) = first_etc_and_maybe_comma_span {
1644                if self.prev_token == token::DotDot {
1645                    // We have `.., x, ..`.
1646                    err.multipart_suggestion(
1647                        "remove the starting `..`",
1648                        ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(first_etc_span, String::new())]))vec![(first_etc_span, String::new())],
1649                        Applicability::MachineApplicable,
1650                    );
1651                } else if let Some(last_non_comma_dotdot_span) = last_non_comma_dotdot_span {
1652                    // We have `.., x`.
1653                    err.multipart_suggestion(
1654                        "move the `..` to the end of the field list",
1655                        ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(first_etc_span, String::new()),
                (self.token.span.to(last_non_comma_dotdot_span.shrink_to_hi()),
                    ::alloc::__export::must_use({
                            ::alloc::fmt::format(format_args!("{0} .. }}",
                                    if ate_comma { "" } else { "," }))
                        }))]))vec![
1656                            (first_etc_span, String::new()),
1657                            (
1658                                self.token.span.to(last_non_comma_dotdot_span.shrink_to_hi()),
1659                                format!("{} .. }}", if ate_comma { "" } else { "," }),
1660                            ),
1661                        ],
1662                        Applicability::MachineApplicable,
1663                    );
1664                }
1665            }
1666            err.emit();
1667        }
1668        Ok((fields, etc))
1669    }
1670
1671    fn report_misplaced_at_in_struct_pat(&self, prev_field: Ident) -> Diag<'a> {
1672        if true {
    match (&self.token, &token::At) {
        (left_val, right_val) => {
            if !(*left_val == *right_val) {
                let kind = ::core::panicking::AssertKind::Eq;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val, ::core::option::Option::None);
            }
        }
    };
};debug_assert_eq!(self.token, token::At);
1673        let span = prev_field.span.to(self.token.span);
1674        if let Some(dot_dot_span) =
1675            self.look_ahead(1, |t| if t == &token::DotDot { Some(t.span) } else { None })
1676        {
1677            self.dcx().create_err(AtDotDotInStructPattern {
1678                span: span.to(dot_dot_span),
1679                remove: span.until(dot_dot_span),
1680                ident: prev_field,
1681            })
1682        } else {
1683            self.dcx().create_err(AtInStructPattern { span })
1684        }
1685    }
1686
1687    /// If the user writes `S { ref field: name }` instead of `S { field: ref name }`, we suggest
1688    /// the correct code.
1689    fn recover_misplaced_pattern_modifiers(&self, fields: &ThinVec<PatField>, err: &mut Diag<'a>) {
1690        if let Some(last) = fields.iter().last()
1691            && last.is_shorthand
1692            && let PatKind::Ident(binding, ident, None) = last.pat.kind
1693            && binding != BindingMode::NONE
1694            && self.token == token::Colon
1695            // We found `ref mut? ident:`, try to parse a `name,` or `name }`.
1696            && let Some(name_span) = self.look_ahead(1, |t| t.is_ident().then(|| t.span))
1697            && self.look_ahead(2, |t| {
1698                t == &token::Comma || t == &token::CloseBrace
1699            })
1700        {
1701            let span = last.pat.span.with_hi(ident.span.lo());
1702            // We have `S { ref field: name }` instead of `S { field: ref name }`
1703            err.multipart_suggestion(
1704                "the pattern modifiers belong after the `:`",
1705                ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(span, String::new()),
                (name_span.shrink_to_lo(),
                    binding.prefix_str().to_string())]))vec![
1706                    (span, String::new()),
1707                    (name_span.shrink_to_lo(), binding.prefix_str().to_string()),
1708                ],
1709                Applicability::MachineApplicable,
1710            );
1711        }
1712    }
1713
1714    /// Recover on `...` or `_` as if it were `..` to avoid further errors.
1715    /// See issue #46718.
1716    fn recover_bad_dot_dot(&self) {
1717        if self.token == token::DotDot {
1718            return;
1719        }
1720
1721        let token_str = pprust::token_to_string(&self.token);
1722        self.dcx().emit_err(DotDotDotForRemainingFields { span: self.token.span, token_str });
1723    }
1724
1725    fn parse_pat_field(&mut self, lo: Span, attrs: AttrVec) -> PResult<'a, PatField> {
1726        // Check if a colon exists one ahead. This means we're parsing a fieldname.
1727        let hi;
1728        let (subpat, fieldname, is_shorthand) = if self.look_ahead(1, |t| t == &token::Colon) {
1729            // Parsing a pattern of the form `fieldname: pat`.
1730            let fieldname = self.parse_field_name()?;
1731            self.bump();
1732            let pat = self.parse_pat_allow_top_guard(
1733                None,
1734                RecoverComma::No,
1735                RecoverColon::No,
1736                CommaRecoveryMode::EitherTupleOrPipe,
1737            )?;
1738            hi = pat.span;
1739            (pat, fieldname, false)
1740        } else {
1741            // Parsing a pattern of the form `(box) (ref) (mut) fieldname`.
1742            let is_box = self.eat_keyword(crate::parser::token_type::ExpKeywordPair {
    kw: rustc_span::symbol::kw::Box,
    token_type: crate::parser::token_type::TokenType::KwBox,
}exp!(Box));
1743            let boxed_span = self.token.span;
1744            let mutability = self.parse_mutability();
1745            let by_ref = self.parse_byref();
1746
1747            let fieldname = self.parse_field_name()?;
1748            hi = self.prev_token.span;
1749            let ann = BindingMode(by_ref, mutability);
1750            let fieldpat = self.mk_pat_ident(boxed_span.to(hi), ann, fieldname);
1751            if #[allow(non_exhaustive_omitted_patterns)] match fieldpat.kind {
    PatKind::Ident(BindingMode(ByRef::Yes(..), Mutability::Mut), ..) => true,
    _ => false,
}matches!(
1752                fieldpat.kind,
1753                PatKind::Ident(BindingMode(ByRef::Yes(..), Mutability::Mut), ..)
1754            ) {
1755                self.psess.gated_spans.gate(sym::mut_ref, fieldpat.span);
1756            }
1757            let subpat = if is_box {
1758                self.mk_pat(lo.to(hi), PatKind::Box(Box::new(fieldpat)))
1759            } else {
1760                fieldpat
1761            };
1762            (subpat, fieldname, true)
1763        };
1764
1765        Ok(PatField {
1766            ident: fieldname,
1767            pat: Box::new(subpat),
1768            is_shorthand,
1769            attrs,
1770            id: ast::DUMMY_NODE_ID,
1771            span: lo.to(hi),
1772            is_placeholder: false,
1773        })
1774    }
1775
1776    pub(super) fn mk_pat_ident(&self, span: Span, ann: BindingMode, ident: Ident) -> Pat {
1777        self.mk_pat(span, PatKind::Ident(ann, ident, None))
1778    }
1779
1780    pub(super) fn mk_pat(&self, span: Span, kind: PatKind) -> Pat {
1781        Pat { kind, span, id: ast::DUMMY_NODE_ID, tokens: None }
1782    }
1783}