rustc_hir_typeck/
pat.rs

1use std::cmp;
2use std::collections::hash_map::Entry::{Occupied, Vacant};
3
4use rustc_abi::FieldIdx;
5use rustc_ast as ast;
6use rustc_data_structures::fx::FxHashMap;
7use rustc_errors::codes::*;
8use rustc_errors::{
9    Applicability, Diag, ErrorGuaranteed, MultiSpan, pluralize, struct_span_code_err,
10};
11use rustc_hir::def::{CtorKind, DefKind, Res};
12use rustc_hir::def_id::DefId;
13use rustc_hir::pat_util::EnumerateAndAdjustIterator;
14use rustc_hir::{
15    self as hir, BindingMode, ByRef, ExprKind, HirId, LangItem, Mutability, Pat, PatExpr,
16    PatExprKind, PatKind, expr_needs_parens,
17};
18use rustc_hir_analysis::autoderef::report_autoderef_recursion_limit_error;
19use rustc_infer::infer;
20use rustc_middle::traits::PatternOriginExpr;
21use rustc_middle::ty::{self, Ty, TypeVisitableExt};
22use rustc_middle::{bug, span_bug};
23use rustc_session::lint::builtin::NON_EXHAUSTIVE_OMITTED_PATTERNS;
24use rustc_session::parse::feature_err;
25use rustc_span::edit_distance::find_best_match_for_name;
26use rustc_span::edition::Edition;
27use rustc_span::hygiene::DesugaringKind;
28use rustc_span::source_map::Spanned;
29use rustc_span::{BytePos, DUMMY_SP, Ident, Span, kw, sym};
30use rustc_trait_selection::infer::InferCtxtExt;
31use rustc_trait_selection::traits::{ObligationCause, ObligationCauseCode};
32use tracing::{debug, instrument, trace};
33use ty::VariantDef;
34use ty::adjustment::{PatAdjust, PatAdjustment};
35
36use super::report_unexpected_variant_res;
37use crate::expectation::Expectation;
38use crate::gather_locals::DeclOrigin;
39use crate::{FnCtxt, errors};
40
41const CANNOT_IMPLICITLY_DEREF_POINTER_TRAIT_OBJ: &str = "\
42This error indicates that a pointer to a trait type cannot be implicitly dereferenced by a \
43pattern. Every trait defines a type, but because the size of trait implementors isn't fixed, \
44this type has no compile-time size. Therefore, all accesses to trait types must be through \
45pointers. If you encounter this error you should try to avoid dereferencing the pointer.
46
47You can read more about trait objects in the Trait Objects section of the Reference: \
48https://doc.rust-lang.org/reference/types.html#trait-objects";
49
50fn is_number(text: &str) -> bool {
51    text.chars().all(|c: char| c.is_digit(10))
52}
53
54/// Information about the expected type at the top level of type checking a pattern.
55///
56/// **NOTE:** This is only for use by diagnostics. Do NOT use for type checking logic!
57#[derive(Copy, Clone)]
58struct TopInfo<'tcx> {
59    /// The `expected` type at the top level of type checking a pattern.
60    expected: Ty<'tcx>,
61    /// Was the origin of the `span` from a scrutinee expression?
62    ///
63    /// Otherwise there is no scrutinee and it could be e.g. from the type of a formal parameter.
64    origin_expr: Option<&'tcx hir::Expr<'tcx>>,
65    /// The span giving rise to the `expected` type, if one could be provided.
66    ///
67    /// If `origin_expr` is `true`, then this is the span of the scrutinee as in:
68    ///
69    /// - `match scrutinee { ... }`
70    /// - `let _ = scrutinee;`
71    ///
72    /// This is used to point to add context in type errors.
73    /// In the following example, `span` corresponds to the `a + b` expression:
74    ///
75    /// ```text
76    /// error[E0308]: mismatched types
77    ///  --> src/main.rs:L:C
78    ///   |
79    /// L |    let temp: usize = match a + b {
80    ///   |                            ----- this expression has type `usize`
81    /// L |         Ok(num) => num,
82    ///   |         ^^^^^^^ expected `usize`, found enum `std::result::Result`
83    ///   |
84    ///   = note: expected type `usize`
85    ///              found type `std::result::Result<_, _>`
86    /// ```
87    span: Option<Span>,
88    /// The [`HirId`] of the top-level pattern.
89    hir_id: HirId,
90}
91
92#[derive(Copy, Clone)]
93struct PatInfo<'tcx> {
94    binding_mode: ByRef,
95    max_ref_mutbl: MutblCap,
96    top_info: TopInfo<'tcx>,
97    decl_origin: Option<DeclOrigin<'tcx>>,
98
99    /// The depth of current pattern
100    current_depth: u32,
101}
102
103impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
104    fn pattern_cause(&self, ti: &TopInfo<'tcx>, cause_span: Span) -> ObligationCause<'tcx> {
105        // If origin_expr exists, then expected represents the type of origin_expr.
106        // If span also exists, then span == origin_expr.span (although it doesn't need to exist).
107        // In that case, we can peel away references from both and treat them
108        // as the same.
109        let origin_expr_info = ti.origin_expr.map(|mut cur_expr| {
110            let mut count = 0;
111
112            // cur_ty may have more layers of references than cur_expr.
113            // We can only make suggestions about cur_expr, however, so we'll
114            // use that as our condition for stopping.
115            while let ExprKind::AddrOf(.., inner) = &cur_expr.kind {
116                cur_expr = inner;
117                count += 1;
118            }
119
120            PatternOriginExpr {
121                peeled_span: cur_expr.span,
122                peeled_count: count,
123                peeled_prefix_suggestion_parentheses: expr_needs_parens(cur_expr),
124            }
125        });
126
127        let code = ObligationCauseCode::Pattern {
128            span: ti.span,
129            root_ty: ti.expected,
130            origin_expr: origin_expr_info,
131        };
132        self.cause(cause_span, code)
133    }
134
135    fn demand_eqtype_pat_diag(
136        &'a self,
137        cause_span: Span,
138        expected: Ty<'tcx>,
139        actual: Ty<'tcx>,
140        ti: &TopInfo<'tcx>,
141    ) -> Result<(), Diag<'a>> {
142        self.demand_eqtype_with_origin(&self.pattern_cause(ti, cause_span), expected, actual)
143            .map_err(|mut diag| {
144                if let Some(expr) = ti.origin_expr {
145                    self.suggest_fn_call(&mut diag, expr, expected, |output| {
146                        self.can_eq(self.param_env, output, actual)
147                    });
148                }
149                diag
150            })
151    }
152
153    fn demand_eqtype_pat(
154        &self,
155        cause_span: Span,
156        expected: Ty<'tcx>,
157        actual: Ty<'tcx>,
158        ti: &TopInfo<'tcx>,
159    ) -> Result<(), ErrorGuaranteed> {
160        self.demand_eqtype_pat_diag(cause_span, expected, actual, ti).map_err(|err| err.emit())
161    }
162}
163
164/// Mode for adjusting the expected type and binding mode.
165#[derive(Clone, Copy, Debug, PartialEq, Eq)]
166enum AdjustMode {
167    /// Peel off all immediate reference types. If the `deref_patterns` feature is enabled, this
168    /// also peels smart pointer ADTs.
169    Peel { kind: PeelKind },
170    /// Pass on the input binding mode and expected type.
171    Pass,
172}
173
174/// Restrictions on what types to peel when adjusting the expected type and binding mode.
175#[derive(Clone, Copy, Debug, PartialEq, Eq)]
176enum PeelKind {
177    /// Only peel reference types. This is used for explicit `deref!(_)` patterns, which dereference
178    /// any number of `&`/`&mut` references, plus a single smart pointer.
179    ExplicitDerefPat,
180    /// Implicitly peel any number of references, and if `deref_patterns` is enabled, smart pointer
181    /// ADTs. In order to peel only as much as necessary for the pattern to match, the `until_adt`
182    /// field contains the ADT def that the pattern is a constructor for, if applicable, so that we
183    /// don't peel it. See [`ResolvedPat`] for more information.
184    Implicit { until_adt: Option<DefId> },
185}
186
187impl AdjustMode {
188    const fn peel_until_adt(opt_adt_def: Option<DefId>) -> AdjustMode {
189        AdjustMode::Peel { kind: PeelKind::Implicit { until_adt: opt_adt_def } }
190    }
191    const fn peel_all() -> AdjustMode {
192        AdjustMode::peel_until_adt(None)
193    }
194}
195
196/// `ref mut` bindings (explicit or match-ergonomics) are not allowed behind an `&` reference.
197/// Normally, the borrow checker enforces this, but for (currently experimental) match ergonomics,
198/// we track this when typing patterns for two purposes:
199///
200/// - For RFC 3627's Rule 3, when this would prevent us from binding with `ref mut`, we limit the
201///   default binding mode to be by shared `ref` when it would otherwise be `ref mut`.
202///
203/// - For RFC 3627's Rule 5, we allow `&` patterns to match against `&mut` references, treating them
204///   as if they were shared references. Since the scrutinee is mutable in this case, the borrow
205///   checker won't catch if we bind with `ref mut`, so we need to throw an error ourselves.
206#[derive(Clone, Copy, Debug, PartialEq, Eq)]
207enum MutblCap {
208    /// Mutability restricted to immutable.
209    Not,
210
211    /// Mutability restricted to immutable, but only because of the pattern
212    /// (not the scrutinee type).
213    ///
214    /// The contained span, if present, points to an `&` pattern
215    /// that is the reason for the restriction,
216    /// and which will be reported in a diagnostic.
217    WeaklyNot(Option<Span>),
218
219    /// No restriction on mutability
220    Mut,
221}
222
223impl MutblCap {
224    #[must_use]
225    fn cap_to_weakly_not(self, span: Option<Span>) -> Self {
226        match self {
227            MutblCap::Not => MutblCap::Not,
228            _ => MutblCap::WeaklyNot(span),
229        }
230    }
231
232    #[must_use]
233    fn as_mutbl(self) -> Mutability {
234        match self {
235            MutblCap::Not | MutblCap::WeaklyNot(_) => Mutability::Not,
236            MutblCap::Mut => Mutability::Mut,
237        }
238    }
239}
240
241/// Variations on RFC 3627's Rule 4: when do reference patterns match against inherited references?
242///
243/// "Inherited reference" designates the `&`/`&mut` types that arise from using match ergonomics, i.e.
244/// from matching a reference type with a non-reference pattern. E.g. when `Some(x)` matches on
245/// `&mut Option<&T>`, `x` gets type `&mut &T` and the outer `&mut` is considered "inherited".
246#[derive(Clone, Copy, Debug, PartialEq, Eq)]
247enum InheritedRefMatchRule {
248    /// Reference patterns consume only the inherited reference if possible, regardless of whether
249    /// the underlying type being matched against is a reference type. If there is no inherited
250    /// reference, a reference will be consumed from the underlying type.
251    EatOuter,
252    /// Reference patterns consume only a reference from the underlying type if possible. If the
253    /// underlying type is not a reference type, the inherited reference will be consumed.
254    EatInner,
255    /// When the underlying type is a reference type, reference patterns consume both layers of
256    /// reference, i.e. they both reset the binding mode and consume the reference type.
257    EatBoth {
258        /// If `true`, an inherited reference will be considered when determining whether a reference
259        /// pattern matches a given type:
260        /// - If the underlying type is not a reference, a reference pattern may eat the inherited reference;
261        /// - If the underlying type is a reference, a reference pattern matches if it can eat either one
262        ///    of the underlying and inherited references. E.g. a `&mut` pattern is allowed if either the
263        ///    underlying type is `&mut` or the inherited reference is `&mut`.
264        /// If `false`, a reference pattern is only matched against the underlying type.
265        /// This is `false` for stable Rust and `true` for both the `ref_pat_eat_one_layer_2024` and
266        /// `ref_pat_eat_one_layer_2024_structural` feature gates.
267        consider_inherited_ref: bool,
268    },
269}
270
271/// When checking patterns containing paths, we need to know the path's resolution to determine
272/// whether to apply match ergonomics and implicitly dereference the scrutinee. For instance, when
273/// the `deref_patterns` feature is enabled and we're matching against a scrutinee of type
274/// `Cow<'a, Option<u8>>`, we insert an implicit dereference to allow the pattern `Some(_)` to type,
275/// but we must not dereference it when checking the pattern `Cow::Borrowed(_)`.
276///
277/// `ResolvedPat` contains the information from resolution needed to determine match ergonomics
278/// adjustments, and to finish checking the pattern once we know its adjusted type.
279#[derive(Clone, Copy, Debug)]
280struct ResolvedPat<'tcx> {
281    /// The type of the pattern, to be checked against the type of the scrutinee after peeling. This
282    /// is also used to avoid peeling the scrutinee's constructors (see the `Cow` example above).
283    ty: Ty<'tcx>,
284    kind: ResolvedPatKind<'tcx>,
285}
286
287#[derive(Clone, Copy, Debug)]
288enum ResolvedPatKind<'tcx> {
289    Path { res: Res, pat_res: Res, segments: &'tcx [hir::PathSegment<'tcx>] },
290    Struct { variant: &'tcx VariantDef },
291    TupleStruct { res: Res, variant: &'tcx VariantDef },
292}
293
294impl<'tcx> ResolvedPat<'tcx> {
295    fn adjust_mode(&self) -> AdjustMode {
296        if let ResolvedPatKind::Path { res, .. } = self.kind
297            && matches!(res, Res::Def(DefKind::Const | DefKind::AssocConst, _))
298        {
299            // These constants can be of a reference type, e.g. `const X: &u8 = &0;`.
300            // Peeling the reference types too early will cause type checking failures.
301            // Although it would be possible to *also* peel the types of the constants too.
302            AdjustMode::Pass
303        } else {
304            // The remaining possible resolutions for path, struct, and tuple struct patterns are
305            // ADT constructors. As such, we may peel references freely, but we must not peel the
306            // ADT itself from the scrutinee if it's a smart pointer.
307            AdjustMode::peel_until_adt(self.ty.ty_adt_def().map(|adt| adt.did()))
308        }
309    }
310}
311
312impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
313    /// Experimental pattern feature: after matching against a shared reference, do we limit the
314    /// default binding mode in subpatterns to be `ref` when it would otherwise be `ref mut`?
315    /// This corresponds to Rule 3 of RFC 3627.
316    fn downgrade_mut_inside_shared(&self) -> bool {
317        // NB: RFC 3627 proposes stabilizing Rule 3 in all editions. If we adopt the same behavior
318        // across all editions, this may be removed.
319        self.tcx.features().ref_pat_eat_one_layer_2024_structural()
320    }
321
322    /// Experimental pattern feature: when do reference patterns match against inherited references?
323    /// This corresponds to variations on Rule 4 of RFC 3627.
324    fn ref_pat_matches_inherited_ref(&self, edition: Edition) -> InheritedRefMatchRule {
325        // NB: The particular rule used here is likely to differ across editions, so calls to this
326        // may need to become edition checks after match ergonomics stabilize.
327        if edition.at_least_rust_2024() {
328            if self.tcx.features().ref_pat_eat_one_layer_2024() {
329                InheritedRefMatchRule::EatOuter
330            } else if self.tcx.features().ref_pat_eat_one_layer_2024_structural() {
331                InheritedRefMatchRule::EatInner
332            } else {
333                // Currently, matching against an inherited ref on edition 2024 is an error.
334                // Use `EatBoth` as a fallback to be similar to stable Rust.
335                InheritedRefMatchRule::EatBoth { consider_inherited_ref: false }
336            }
337        } else {
338            InheritedRefMatchRule::EatBoth {
339                consider_inherited_ref: self.tcx.features().ref_pat_eat_one_layer_2024()
340                    || self.tcx.features().ref_pat_eat_one_layer_2024_structural(),
341            }
342        }
343    }
344
345    /// Experimental pattern feature: do `&` patterns match against `&mut` references, treating them
346    /// as if they were shared references? This corresponds to Rule 5 of RFC 3627.
347    fn ref_pat_matches_mut_ref(&self) -> bool {
348        // NB: RFC 3627 proposes stabilizing Rule 5 in all editions. If we adopt the same behavior
349        // across all editions, this may be removed.
350        self.tcx.features().ref_pat_eat_one_layer_2024()
351            || self.tcx.features().ref_pat_eat_one_layer_2024_structural()
352    }
353
354    /// Type check the given top level pattern against the `expected` type.
355    ///
356    /// If a `Some(span)` is provided and `origin_expr` holds,
357    /// then the `span` represents the scrutinee's span.
358    /// The scrutinee is found in e.g. `match scrutinee { ... }` and `let pat = scrutinee;`.
359    ///
360    /// Otherwise, `Some(span)` represents the span of a type expression
361    /// which originated the `expected` type.
362    pub(crate) fn check_pat_top(
363        &self,
364        pat: &'tcx Pat<'tcx>,
365        expected: Ty<'tcx>,
366        span: Option<Span>,
367        origin_expr: Option<&'tcx hir::Expr<'tcx>>,
368        decl_origin: Option<DeclOrigin<'tcx>>,
369    ) {
370        let top_info = TopInfo { expected, origin_expr, span, hir_id: pat.hir_id };
371        let pat_info = PatInfo {
372            binding_mode: ByRef::No,
373            max_ref_mutbl: MutblCap::Mut,
374            top_info,
375            decl_origin,
376            current_depth: 0,
377        };
378        self.check_pat(pat, expected, pat_info);
379    }
380
381    /// Type check the given `pat` against the `expected` type
382    /// with the provided `binding_mode` (default binding mode).
383    ///
384    /// Outside of this module, `check_pat_top` should always be used.
385    /// Conversely, inside this module, `check_pat_top` should never be used.
386    #[instrument(level = "debug", skip(self, pat_info))]
387    fn check_pat(&self, pat: &'tcx Pat<'tcx>, expected: Ty<'tcx>, pat_info: PatInfo<'tcx>) {
388        // For patterns containing paths, we need the path's resolution to determine whether to
389        // implicitly dereference the scrutinee before matching.
390        let opt_path_res = match pat.kind {
391            PatKind::Expr(PatExpr { kind: PatExprKind::Path(qpath), hir_id, span }) => {
392                Some(self.resolve_pat_path(*hir_id, *span, qpath))
393            }
394            PatKind::Struct(ref qpath, ..) => Some(self.resolve_pat_struct(pat, qpath)),
395            PatKind::TupleStruct(ref qpath, ..) => Some(self.resolve_pat_tuple_struct(pat, qpath)),
396            _ => None,
397        };
398        let adjust_mode = self.calc_adjust_mode(pat, opt_path_res);
399        let ty = self.check_pat_inner(pat, opt_path_res, adjust_mode, expected, pat_info);
400        self.write_ty(pat.hir_id, ty);
401
402        // If we implicitly inserted overloaded dereferences before matching, check the pattern to
403        // see if the dereferenced types need `DerefMut` bounds.
404        if let Some(derefed_tys) = self.typeck_results.borrow().pat_adjustments().get(pat.hir_id)
405            && derefed_tys.iter().any(|adjust| adjust.kind == PatAdjust::OverloadedDeref)
406        {
407            self.register_deref_mut_bounds_if_needed(
408                pat.span,
409                pat,
410                derefed_tys.iter().filter_map(|adjust| match adjust.kind {
411                    PatAdjust::OverloadedDeref => Some(adjust.source),
412                    PatAdjust::BuiltinDeref => None,
413                }),
414            );
415        }
416
417        // (note_1): In most of the cases where (note_1) is referenced
418        // (literals and constants being the exception), we relate types
419        // using strict equality, even though subtyping would be sufficient.
420        // There are a few reasons for this, some of which are fairly subtle
421        // and which cost me (nmatsakis) an hour or two debugging to remember,
422        // so I thought I'd write them down this time.
423        //
424        // 1. There is no loss of expressiveness here, though it does
425        // cause some inconvenience. What we are saying is that the type
426        // of `x` becomes *exactly* what is expected. This can cause unnecessary
427        // errors in some cases, such as this one:
428        //
429        // ```
430        // fn foo<'x>(x: &'x i32) {
431        //    let a = 1;
432        //    let mut z = x;
433        //    z = &a;
434        // }
435        // ```
436        //
437        // The reason we might get an error is that `z` might be
438        // assigned a type like `&'x i32`, and then we would have
439        // a problem when we try to assign `&a` to `z`, because
440        // the lifetime of `&a` (i.e., the enclosing block) is
441        // shorter than `'x`.
442        //
443        // HOWEVER, this code works fine. The reason is that the
444        // expected type here is whatever type the user wrote, not
445        // the initializer's type. In this case the user wrote
446        // nothing, so we are going to create a type variable `Z`.
447        // Then we will assign the type of the initializer (`&'x i32`)
448        // as a subtype of `Z`: `&'x i32 <: Z`. And hence we
449        // will instantiate `Z` as a type `&'0 i32` where `'0` is
450        // a fresh region variable, with the constraint that `'x : '0`.
451        // So basically we're all set.
452        //
453        // Note that there are two tests to check that this remains true
454        // (`regions-reassign-{match,let}-bound-pointer.rs`).
455        //
456        // 2. An outdated issue related to the old HIR borrowck. See the test
457        // `regions-relate-bound-regions-on-closures-to-inference-variables.rs`,
458    }
459
460    // Helper to avoid resolving the same path pattern several times.
461    fn check_pat_inner(
462        &self,
463        pat: &'tcx Pat<'tcx>,
464        opt_path_res: Option<Result<ResolvedPat<'tcx>, ErrorGuaranteed>>,
465        adjust_mode: AdjustMode,
466        expected: Ty<'tcx>,
467        pat_info: PatInfo<'tcx>,
468    ) -> Ty<'tcx> {
469        #[cfg(debug_assertions)]
470        if pat_info.binding_mode == ByRef::Yes(Mutability::Mut)
471            && pat_info.max_ref_mutbl != MutblCap::Mut
472            && self.downgrade_mut_inside_shared()
473        {
474            span_bug!(pat.span, "Pattern mutability cap violated!");
475        }
476
477        // Resolve type if needed.
478        let expected = if let AdjustMode::Peel { .. } = adjust_mode
479            && pat.default_binding_modes
480        {
481            self.try_structurally_resolve_type(pat.span, expected)
482        } else {
483            expected
484        };
485        let old_pat_info = pat_info;
486        let pat_info = PatInfo { current_depth: old_pat_info.current_depth + 1, ..old_pat_info };
487
488        match pat.kind {
489            // Peel off a `&` or `&mut` from the scrutinee type. See the examples in
490            // `tests/ui/rfcs/rfc-2005-default-binding-mode`.
491            _ if let AdjustMode::Peel { .. } = adjust_mode
492                && pat.default_binding_modes
493                && let ty::Ref(_, inner_ty, inner_mutability) = *expected.kind() =>
494            {
495                debug!("inspecting {:?}", expected);
496
497                debug!("current discriminant is Ref, inserting implicit deref");
498                // Preserve the reference type. We'll need it later during THIR lowering.
499                self.typeck_results
500                    .borrow_mut()
501                    .pat_adjustments_mut()
502                    .entry(pat.hir_id)
503                    .or_default()
504                    .push(PatAdjustment { kind: PatAdjust::BuiltinDeref, source: expected });
505
506                let mut binding_mode = ByRef::Yes(match pat_info.binding_mode {
507                    // If default binding mode is by value, make it `ref` or `ref mut`
508                    // (depending on whether we observe `&` or `&mut`).
509                    ByRef::No |
510                    // When `ref mut`, stay a `ref mut` (on `&mut`) or downgrade to `ref` (on `&`).
511                    ByRef::Yes(Mutability::Mut) => inner_mutability,
512                    // Once a `ref`, always a `ref`.
513                    // This is because a `& &mut` cannot mutate the underlying value.
514                    ByRef::Yes(Mutability::Not) => Mutability::Not,
515                });
516
517                let mut max_ref_mutbl = pat_info.max_ref_mutbl;
518                if self.downgrade_mut_inside_shared() {
519                    binding_mode = binding_mode.cap_ref_mutability(max_ref_mutbl.as_mutbl());
520                }
521                if binding_mode == ByRef::Yes(Mutability::Not) {
522                    max_ref_mutbl = MutblCap::Not;
523                }
524                debug!("default binding mode is now {:?}", binding_mode);
525
526                // Use the old pat info to keep `current_depth` to its old value.
527                let new_pat_info = PatInfo { binding_mode, max_ref_mutbl, ..old_pat_info };
528                // Recurse with the new expected type.
529                self.check_pat_inner(pat, opt_path_res, adjust_mode, inner_ty, new_pat_info)
530            }
531            // If `deref_patterns` is enabled, peel a smart pointer from the scrutinee type. See the
532            // examples in `tests/ui/pattern/deref_patterns/`.
533            _ if self.tcx.features().deref_patterns()
534                && let AdjustMode::Peel { kind: PeelKind::Implicit { until_adt } } = adjust_mode
535                && pat.default_binding_modes
536                // For simplicity, only apply overloaded derefs if `expected` is a known ADT.
537                // FIXME(deref_patterns): we'll get better diagnostics for users trying to
538                // implicitly deref generics if we allow them here, but primitives, tuples, and
539                // inference vars definitely should be stopped. Figure out what makes most sense.
540                && let ty::Adt(scrutinee_adt, _) = *expected.kind()
541                // Don't peel if the pattern type already matches the scrutinee. E.g., stop here if
542                // matching on a `Cow<'a, T>` scrutinee with a `Cow::Owned(_)` pattern.
543                && until_adt != Some(scrutinee_adt.did())
544                // At this point, the pattern isn't able to match `expected` without peeling. Check
545                // that it implements `Deref` before assuming it's a smart pointer, to get a normal
546                // type error instead of a missing impl error if not. This only checks for `Deref`,
547                // not `DerefPure`: we require that too, but we want a trait error if it's missing.
548                && let Some(deref_trait) = self.tcx.lang_items().deref_trait()
549                && self
550                    .type_implements_trait(deref_trait, [expected], self.param_env)
551                    .may_apply() =>
552            {
553                debug!("scrutinee ty {expected:?} is a smart pointer, inserting overloaded deref");
554                // The scrutinee is a smart pointer; implicitly dereference it. This adds a
555                // requirement that `expected: DerefPure`.
556                let mut inner_ty = self.deref_pat_target(pat.span, expected);
557                // Once we've checked `pat`, we'll add a `DerefMut` bound if it contains any
558                // `ref mut` bindings. See `Self::register_deref_mut_bounds_if_needed`.
559
560                let mut typeck_results = self.typeck_results.borrow_mut();
561                let mut pat_adjustments_table = typeck_results.pat_adjustments_mut();
562                let pat_adjustments = pat_adjustments_table.entry(pat.hir_id).or_default();
563                // We may reach the recursion limit if a user matches on a type `T` satisfying
564                // `T: Deref<Target = T>`; error gracefully in this case.
565                // FIXME(deref_patterns): If `deref_patterns` stabilizes, it may make sense to move
566                // this check out of this branch. Alternatively, this loop could be implemented with
567                // autoderef and this check removed. For now though, don't break code compiling on
568                // stable with lots of `&`s and a low recursion limit, if anyone's done that.
569                if self.tcx.recursion_limit().value_within_limit(pat_adjustments.len()) {
570                    // Preserve the smart pointer type for THIR lowering and closure upvar analysis.
571                    pat_adjustments
572                        .push(PatAdjustment { kind: PatAdjust::OverloadedDeref, source: expected });
573                } else {
574                    let guar = report_autoderef_recursion_limit_error(self.tcx, pat.span, expected);
575                    inner_ty = Ty::new_error(self.tcx, guar);
576                }
577                drop(typeck_results);
578
579                // Recurse, using the old pat info to keep `current_depth` to its old value.
580                // Peeling smart pointers does not update the default binding mode.
581                self.check_pat_inner(pat, opt_path_res, adjust_mode, inner_ty, old_pat_info)
582            }
583            PatKind::Missing | PatKind::Wild | PatKind::Err(_) => expected,
584            // We allow any type here; we ensure that the type is uninhabited during match checking.
585            PatKind::Never => expected,
586            PatKind::Expr(PatExpr { kind: PatExprKind::Path(_), hir_id, .. }) => {
587                let ty = match opt_path_res.unwrap() {
588                    Ok(ref pr) => {
589                        self.check_pat_path(pat.hir_id, pat.span, pr, expected, &pat_info.top_info)
590                    }
591                    Err(guar) => Ty::new_error(self.tcx, guar),
592                };
593                self.write_ty(*hir_id, ty);
594                ty
595            }
596            PatKind::Expr(lt) => self.check_pat_lit(pat.span, lt, expected, &pat_info.top_info),
597            PatKind::Range(lhs, rhs, _) => {
598                self.check_pat_range(pat.span, lhs, rhs, expected, &pat_info.top_info)
599            }
600            PatKind::Binding(ba, var_id, ident, sub) => {
601                self.check_pat_ident(pat, ba, var_id, ident, sub, expected, pat_info)
602            }
603            PatKind::TupleStruct(ref qpath, subpats, ddpos) => match opt_path_res.unwrap() {
604                Ok(ResolvedPat { ty, kind: ResolvedPatKind::TupleStruct { res, variant } }) => self
605                    .check_pat_tuple_struct(
606                        pat, qpath, subpats, ddpos, res, ty, variant, expected, pat_info,
607                    ),
608                Err(guar) => {
609                    let ty_err = Ty::new_error(self.tcx, guar);
610                    for subpat in subpats {
611                        self.check_pat(subpat, ty_err, pat_info);
612                    }
613                    ty_err
614                }
615                Ok(pr) => span_bug!(pat.span, "tuple struct pattern resolved to {pr:?}"),
616            },
617            PatKind::Struct(_, fields, has_rest_pat) => match opt_path_res.unwrap() {
618                Ok(ResolvedPat { ty, kind: ResolvedPatKind::Struct { variant } }) => self
619                    .check_pat_struct(pat, fields, has_rest_pat, ty, variant, expected, pat_info),
620                Err(guar) => {
621                    let ty_err = Ty::new_error(self.tcx, guar);
622                    for field in fields {
623                        self.check_pat(field.pat, ty_err, pat_info);
624                    }
625                    ty_err
626                }
627                Ok(pr) => span_bug!(pat.span, "struct pattern resolved to {pr:?}"),
628            },
629            PatKind::Guard(pat, cond) => {
630                self.check_pat(pat, expected, pat_info);
631                self.check_expr_has_type_or_error(cond, self.tcx.types.bool, |_| {});
632                expected
633            }
634            PatKind::Or(pats) => {
635                for pat in pats {
636                    self.check_pat(pat, expected, pat_info);
637                }
638                expected
639            }
640            PatKind::Tuple(elements, ddpos) => {
641                self.check_pat_tuple(pat.span, elements, ddpos, expected, pat_info)
642            }
643            PatKind::Box(inner) => self.check_pat_box(pat.span, inner, expected, pat_info),
644            PatKind::Deref(inner) => self.check_pat_deref(pat.span, inner, expected, pat_info),
645            PatKind::Ref(inner, mutbl) => self.check_pat_ref(pat, inner, mutbl, expected, pat_info),
646            PatKind::Slice(before, slice, after) => {
647                self.check_pat_slice(pat.span, before, slice, after, expected, pat_info)
648            }
649        }
650    }
651
652    /// How should the binding mode and expected type be adjusted?
653    ///
654    /// When the pattern contains a path, `opt_path_res` must be `Some(path_res)`.
655    fn calc_adjust_mode(
656        &self,
657        pat: &'tcx Pat<'tcx>,
658        opt_path_res: Option<Result<ResolvedPat<'tcx>, ErrorGuaranteed>>,
659    ) -> AdjustMode {
660        match &pat.kind {
661            // Type checking these product-like types successfully always require
662            // that the expected type be of those types and not reference types.
663            PatKind::Tuple(..)
664            | PatKind::Range(..)
665            | PatKind::Slice(..) => AdjustMode::peel_all(),
666            // When checking an explicit deref pattern, only peel reference types.
667            // FIXME(deref_patterns): If box patterns and deref patterns need to coexist, box
668            // patterns may want `PeelKind::Implicit`, stopping on encountering a box.
669            | PatKind::Box(_)
670            | PatKind::Deref(_) => AdjustMode::Peel { kind: PeelKind::ExplicitDerefPat },
671            // A never pattern behaves somewhat like a literal or unit variant.
672            PatKind::Never => AdjustMode::peel_all(),
673            // For patterns with paths, how we peel the scrutinee depends on the path's resolution.
674            PatKind::Struct(..)
675            | PatKind::TupleStruct(..)
676            | PatKind::Expr(PatExpr { kind: PatExprKind::Path(_), .. }) => {
677                // If there was an error resolving the path, default to peeling everything.
678                opt_path_res.unwrap().map_or(AdjustMode::peel_all(), |pr| pr.adjust_mode())
679            }
680
681            // String and byte-string literals result in types `&str` and `&[u8]` respectively.
682            // All other literals result in non-reference types.
683            // As a result, we allow `if let 0 = &&0 {}` but not `if let "foo" = &&"foo" {}`.
684            //
685            // Call `resolve_vars_if_possible` here for inline const blocks.
686            PatKind::Expr(lt) => match self.resolve_vars_if_possible(self.check_pat_expr_unadjusted(lt)).kind() {
687                ty::Ref(..) => AdjustMode::Pass,
688                _ => {
689                    // Path patterns have already been handled, and inline const blocks currently
690                    // aren't possible to write, so any handling for them would be untested.
691                    if cfg!(debug_assertions)
692                        && self.tcx.features().deref_patterns()
693                        && !matches!(lt.kind, PatExprKind::Lit { .. })
694                    {
695                        span_bug!(lt.span, "FIXME(deref_patterns): adjust mode unimplemented for {:?}", lt.kind);
696                    }
697                    AdjustMode::peel_all()
698                }
699            },
700
701            // Ref patterns are complicated, we handle them in `check_pat_ref`.
702            PatKind::Ref(..)
703            // No need to do anything on a missing pattern.
704            | PatKind::Missing
705            // A `_` pattern works with any expected type, so there's no need to do anything.
706            | PatKind::Wild
707            // A malformed pattern doesn't have an expected type, so let's just accept any type.
708            | PatKind::Err(_)
709            // Bindings also work with whatever the expected type is,
710            // and moreover if we peel references off, that will give us the wrong binding type.
711            // Also, we can have a subpattern `binding @ pat`.
712            // Each side of the `@` should be treated independently (like with OR-patterns).
713            | PatKind::Binding(..)
714            // An OR-pattern just propagates to each individual alternative.
715            // This is maximally flexible, allowing e.g., `Some(mut x) | &Some(mut x)`.
716            // In that example, `Some(mut x)` results in `Peel` whereas `&Some(mut x)` in `Reset`.
717            | PatKind::Or(_)
718            // Like or-patterns, guard patterns just propogate to their subpatterns.
719            | PatKind::Guard(..) => AdjustMode::Pass,
720        }
721    }
722
723    fn check_pat_expr_unadjusted(&self, lt: &'tcx hir::PatExpr<'tcx>) -> Ty<'tcx> {
724        let ty = match &lt.kind {
725            rustc_hir::PatExprKind::Lit { lit, negated } => {
726                let ty = self.check_expr_lit(lit, Expectation::NoExpectation);
727                if *negated {
728                    self.register_bound(
729                        ty,
730                        self.tcx.require_lang_item(LangItem::Neg, Some(lt.span)),
731                        ObligationCause::dummy_with_span(lt.span),
732                    );
733                }
734                ty
735            }
736            rustc_hir::PatExprKind::ConstBlock(c) => {
737                self.check_expr_const_block(c, Expectation::NoExpectation)
738            }
739            rustc_hir::PatExprKind::Path(qpath) => {
740                let (res, opt_ty, segments) =
741                    self.resolve_ty_and_res_fully_qualified_call(qpath, lt.hir_id, lt.span);
742                self.instantiate_value_path(segments, opt_ty, res, lt.span, lt.span, lt.hir_id).0
743            }
744        };
745        self.write_ty(lt.hir_id, ty);
746        ty
747    }
748
749    fn check_pat_lit(
750        &self,
751        span: Span,
752        lt: &hir::PatExpr<'tcx>,
753        expected: Ty<'tcx>,
754        ti: &TopInfo<'tcx>,
755    ) -> Ty<'tcx> {
756        // We've already computed the type above (when checking for a non-ref pat),
757        // so avoid computing it again.
758        let ty = self.node_ty(lt.hir_id);
759
760        // Byte string patterns behave the same way as array patterns
761        // They can denote both statically and dynamically-sized byte arrays.
762        let mut pat_ty = ty;
763        if let hir::PatExprKind::Lit {
764            lit: Spanned { node: ast::LitKind::ByteStr(..), .. }, ..
765        } = lt.kind
766        {
767            let expected = self.structurally_resolve_type(span, expected);
768            if let ty::Ref(_, inner_ty, _) = *expected.kind()
769                && self.try_structurally_resolve_type(span, inner_ty).is_slice()
770            {
771                let tcx = self.tcx;
772                trace!(?lt.hir_id.local_id, "polymorphic byte string lit");
773                pat_ty =
774                    Ty::new_imm_ref(tcx, tcx.lifetimes.re_static, Ty::new_slice(tcx, tcx.types.u8));
775            }
776        }
777
778        if self.tcx.features().string_deref_patterns()
779            && let hir::PatExprKind::Lit {
780                lit: Spanned { node: ast::LitKind::Str(..), .. }, ..
781            } = lt.kind
782        {
783            let tcx = self.tcx;
784            let expected = self.resolve_vars_if_possible(expected);
785            pat_ty = match expected.kind() {
786                ty::Adt(def, _) if tcx.is_lang_item(def.did(), LangItem::String) => expected,
787                ty::Str => Ty::new_static_str(tcx),
788                _ => pat_ty,
789            };
790        }
791
792        // Somewhat surprising: in this case, the subtyping relation goes the
793        // opposite way as the other cases. Actually what we really want is not
794        // a subtyping relation at all but rather that there exists a LUB
795        // (so that they can be compared). However, in practice, constants are
796        // always scalars or strings. For scalars subtyping is irrelevant,
797        // and for strings `ty` is type is `&'static str`, so if we say that
798        //
799        //     &'static str <: expected
800        //
801        // then that's equivalent to there existing a LUB.
802        let cause = self.pattern_cause(ti, span);
803        if let Err(err) = self.demand_suptype_with_origin(&cause, expected, pat_ty) {
804            err.emit_unless(
805                ti.span
806                    .filter(|&s| {
807                        // In the case of `if`- and `while`-expressions we've already checked
808                        // that `scrutinee: bool`. We know that the pattern is `true`,
809                        // so an error here would be a duplicate and from the wrong POV.
810                        s.is_desugaring(DesugaringKind::CondTemporary)
811                    })
812                    .is_some(),
813            );
814        }
815
816        pat_ty
817    }
818
819    fn check_pat_range(
820        &self,
821        span: Span,
822        lhs: Option<&'tcx hir::PatExpr<'tcx>>,
823        rhs: Option<&'tcx hir::PatExpr<'tcx>>,
824        expected: Ty<'tcx>,
825        ti: &TopInfo<'tcx>,
826    ) -> Ty<'tcx> {
827        let calc_side = |opt_expr: Option<&'tcx hir::PatExpr<'tcx>>| match opt_expr {
828            None => None,
829            Some(expr) => {
830                let ty = self.check_pat_expr_unadjusted(expr);
831                // Check that the end-point is possibly of numeric or char type.
832                // The early check here is not for correctness, but rather better
833                // diagnostics (e.g. when `&str` is being matched, `expected` will
834                // be peeled to `str` while ty here is still `&str`, if we don't
835                // err early here, a rather confusing unification error will be
836                // emitted instead).
837                let fail =
838                    !(ty.is_numeric() || ty.is_char() || ty.is_ty_var() || ty.references_error());
839                Some((fail, ty, expr.span))
840            }
841        };
842        let mut lhs = calc_side(lhs);
843        let mut rhs = calc_side(rhs);
844
845        if let (Some((true, ..)), _) | (_, Some((true, ..))) = (lhs, rhs) {
846            // There exists a side that didn't meet our criteria that the end-point
847            // be of a numeric or char type, as checked in `calc_side` above.
848            let guar = self.emit_err_pat_range(span, lhs, rhs);
849            return Ty::new_error(self.tcx, guar);
850        }
851
852        // Unify each side with `expected`.
853        // Subtyping doesn't matter here, as the value is some kind of scalar.
854        let demand_eqtype = |x: &mut _, y| {
855            if let Some((ref mut fail, x_ty, x_span)) = *x
856                && let Err(mut err) = self.demand_eqtype_pat_diag(x_span, expected, x_ty, ti)
857            {
858                if let Some((_, y_ty, y_span)) = y {
859                    self.endpoint_has_type(&mut err, y_span, y_ty);
860                }
861                err.emit();
862                *fail = true;
863            }
864        };
865        demand_eqtype(&mut lhs, rhs);
866        demand_eqtype(&mut rhs, lhs);
867
868        if let (Some((true, ..)), _) | (_, Some((true, ..))) = (lhs, rhs) {
869            return Ty::new_misc_error(self.tcx);
870        }
871
872        // Find the unified type and check if it's of numeric or char type again.
873        // This check is needed if both sides are inference variables.
874        // We require types to be resolved here so that we emit inference failure
875        // rather than "_ is not a char or numeric".
876        let ty = self.structurally_resolve_type(span, expected);
877        if !(ty.is_numeric() || ty.is_char() || ty.references_error()) {
878            if let Some((ref mut fail, _, _)) = lhs {
879                *fail = true;
880            }
881            if let Some((ref mut fail, _, _)) = rhs {
882                *fail = true;
883            }
884            let guar = self.emit_err_pat_range(span, lhs, rhs);
885            return Ty::new_error(self.tcx, guar);
886        }
887        ty
888    }
889
890    fn endpoint_has_type(&self, err: &mut Diag<'_>, span: Span, ty: Ty<'_>) {
891        if !ty.references_error() {
892            err.span_label(span, format!("this is of type `{ty}`"));
893        }
894    }
895
896    fn emit_err_pat_range(
897        &self,
898        span: Span,
899        lhs: Option<(bool, Ty<'tcx>, Span)>,
900        rhs: Option<(bool, Ty<'tcx>, Span)>,
901    ) -> ErrorGuaranteed {
902        let span = match (lhs, rhs) {
903            (Some((true, ..)), Some((true, ..))) => span,
904            (Some((true, _, sp)), _) => sp,
905            (_, Some((true, _, sp))) => sp,
906            _ => span_bug!(span, "emit_err_pat_range: no side failed or exists but still error?"),
907        };
908        let mut err = struct_span_code_err!(
909            self.dcx(),
910            span,
911            E0029,
912            "only `char` and numeric types are allowed in range patterns"
913        );
914        let msg = |ty| {
915            let ty = self.resolve_vars_if_possible(ty);
916            format!("this is of type `{ty}` but it should be `char` or numeric")
917        };
918        let mut one_side_err = |first_span, first_ty, second: Option<(bool, Ty<'tcx>, Span)>| {
919            err.span_label(first_span, msg(first_ty));
920            if let Some((_, ty, sp)) = second {
921                let ty = self.resolve_vars_if_possible(ty);
922                self.endpoint_has_type(&mut err, sp, ty);
923            }
924        };
925        match (lhs, rhs) {
926            (Some((true, lhs_ty, lhs_sp)), Some((true, rhs_ty, rhs_sp))) => {
927                err.span_label(lhs_sp, msg(lhs_ty));
928                err.span_label(rhs_sp, msg(rhs_ty));
929            }
930            (Some((true, lhs_ty, lhs_sp)), rhs) => one_side_err(lhs_sp, lhs_ty, rhs),
931            (lhs, Some((true, rhs_ty, rhs_sp))) => one_side_err(rhs_sp, rhs_ty, lhs),
932            _ => span_bug!(span, "Impossible, verified above."),
933        }
934        if (lhs, rhs).references_error() {
935            err.downgrade_to_delayed_bug();
936        }
937        if self.tcx.sess.teach(err.code.unwrap()) {
938            err.note(
939                "In a match expression, only numbers and characters can be matched \
940                    against a range. This is because the compiler checks that the range \
941                    is non-empty at compile-time, and is unable to evaluate arbitrary \
942                    comparison functions. If you want to capture values of an orderable \
943                    type between two end-points, you can use a guard.",
944            );
945        }
946        err.emit()
947    }
948
949    fn check_pat_ident(
950        &self,
951        pat: &'tcx Pat<'tcx>,
952        user_bind_annot: BindingMode,
953        var_id: HirId,
954        ident: Ident,
955        sub: Option<&'tcx Pat<'tcx>>,
956        expected: Ty<'tcx>,
957        pat_info: PatInfo<'tcx>,
958    ) -> Ty<'tcx> {
959        let PatInfo { binding_mode: def_br, top_info: ti, .. } = pat_info;
960
961        // Determine the binding mode...
962        let bm = match user_bind_annot {
963            BindingMode(ByRef::No, Mutability::Mut) if let ByRef::Yes(def_br_mutbl) = def_br => {
964                // Only mention the experimental `mut_ref` feature if if we're in edition 2024 and
965                // using other experimental matching features compatible with it.
966                if pat.span.at_least_rust_2024()
967                    && (self.tcx.features().ref_pat_eat_one_layer_2024()
968                        || self.tcx.features().ref_pat_eat_one_layer_2024_structural())
969                {
970                    if !self.tcx.features().mut_ref() {
971                        feature_err(
972                            &self.tcx.sess,
973                            sym::mut_ref,
974                            pat.span.until(ident.span),
975                            "binding cannot be both mutable and by-reference",
976                        )
977                        .emit();
978                    }
979
980                    BindingMode(def_br, Mutability::Mut)
981                } else {
982                    // `mut` resets the binding mode on edition <= 2021
983                    self.add_rust_2024_migration_desugared_pat(
984                        pat_info.top_info.hir_id,
985                        pat,
986                        't', // last char of `mut`
987                        def_br_mutbl,
988                    );
989                    BindingMode(ByRef::No, Mutability::Mut)
990                }
991            }
992            BindingMode(ByRef::No, mutbl) => BindingMode(def_br, mutbl),
993            BindingMode(ByRef::Yes(user_br_mutbl), _) => {
994                if let ByRef::Yes(def_br_mutbl) = def_br {
995                    // `ref`/`ref mut` overrides the binding mode on edition <= 2021
996                    self.add_rust_2024_migration_desugared_pat(
997                        pat_info.top_info.hir_id,
998                        pat,
999                        match user_br_mutbl {
1000                            Mutability::Not => 'f', // last char of `ref`
1001                            Mutability::Mut => 't', // last char of `ref mut`
1002                        },
1003                        def_br_mutbl,
1004                    );
1005                }
1006                user_bind_annot
1007            }
1008        };
1009
1010        if bm.0 == ByRef::Yes(Mutability::Mut)
1011            && let MutblCap::WeaklyNot(and_pat_span) = pat_info.max_ref_mutbl
1012        {
1013            let mut err = struct_span_code_err!(
1014                self.dcx(),
1015                ident.span,
1016                E0596,
1017                "cannot borrow as mutable inside an `&` pattern"
1018            );
1019
1020            if let Some(span) = and_pat_span {
1021                err.span_suggestion(
1022                    span,
1023                    "replace this `&` with `&mut`",
1024                    "&mut ",
1025                    Applicability::MachineApplicable,
1026                );
1027            }
1028            err.emit();
1029        }
1030
1031        // ...and store it in a side table:
1032        self.typeck_results.borrow_mut().pat_binding_modes_mut().insert(pat.hir_id, bm);
1033
1034        debug!("check_pat_ident: pat.hir_id={:?} bm={:?}", pat.hir_id, bm);
1035
1036        let local_ty = self.local_ty(pat.span, pat.hir_id);
1037        let eq_ty = match bm.0 {
1038            ByRef::Yes(mutbl) => {
1039                // If the binding is like `ref x | ref mut x`,
1040                // then `x` is assigned a value of type `&M T` where M is the
1041                // mutability and T is the expected type.
1042                //
1043                // `x` is assigned a value of type `&M T`, hence `&M T <: typeof(x)`
1044                // is required. However, we use equality, which is stronger.
1045                // See (note_1) for an explanation.
1046                self.new_ref_ty(pat.span, mutbl, expected)
1047            }
1048            // Otherwise, the type of x is the expected type `T`.
1049            ByRef::No => expected, // As above, `T <: typeof(x)` is required, but we use equality, see (note_1).
1050        };
1051
1052        // We have a concrete type for the local, so we do not need to taint it and hide follow up errors *using* the local.
1053        let _ = self.demand_eqtype_pat(pat.span, eq_ty, local_ty, &ti);
1054
1055        // If there are multiple arms, make sure they all agree on
1056        // what the type of the binding `x` ought to be.
1057        if var_id != pat.hir_id {
1058            self.check_binding_alt_eq_ty(user_bind_annot, pat.span, var_id, local_ty, &ti);
1059        }
1060
1061        if let Some(p) = sub {
1062            self.check_pat(p, expected, pat_info);
1063        }
1064
1065        local_ty
1066    }
1067
1068    /// When a variable is bound several times in a `PatKind::Or`, it'll resolve all of the
1069    /// subsequent bindings of the same name to the first usage. Verify that all of these
1070    /// bindings have the same type by comparing them all against the type of that first pat.
1071    fn check_binding_alt_eq_ty(
1072        &self,
1073        ba: BindingMode,
1074        span: Span,
1075        var_id: HirId,
1076        ty: Ty<'tcx>,
1077        ti: &TopInfo<'tcx>,
1078    ) {
1079        let var_ty = self.local_ty(span, var_id);
1080        if let Err(mut err) = self.demand_eqtype_pat_diag(span, var_ty, ty, ti) {
1081            let var_ty = self.resolve_vars_if_possible(var_ty);
1082            let msg = format!("first introduced with type `{var_ty}` here");
1083            err.span_label(self.tcx.hir_span(var_id), msg);
1084            let in_match = self.tcx.hir_parent_iter(var_id).any(|(_, n)| {
1085                matches!(
1086                    n,
1087                    hir::Node::Expr(hir::Expr {
1088                        kind: hir::ExprKind::Match(.., hir::MatchSource::Normal),
1089                        ..
1090                    })
1091                )
1092            });
1093            let pre = if in_match { "in the same arm, " } else { "" };
1094            err.note(format!("{pre}a binding must have the same type in all alternatives"));
1095            self.suggest_adding_missing_ref_or_removing_ref(
1096                &mut err,
1097                span,
1098                var_ty,
1099                self.resolve_vars_if_possible(ty),
1100                ba,
1101            );
1102            err.emit();
1103        }
1104    }
1105
1106    fn suggest_adding_missing_ref_or_removing_ref(
1107        &self,
1108        err: &mut Diag<'_>,
1109        span: Span,
1110        expected: Ty<'tcx>,
1111        actual: Ty<'tcx>,
1112        ba: BindingMode,
1113    ) {
1114        match (expected.kind(), actual.kind(), ba) {
1115            (ty::Ref(_, inner_ty, _), _, BindingMode::NONE)
1116                if self.can_eq(self.param_env, *inner_ty, actual) =>
1117            {
1118                err.span_suggestion_verbose(
1119                    span.shrink_to_lo(),
1120                    "consider adding `ref`",
1121                    "ref ",
1122                    Applicability::MaybeIncorrect,
1123                );
1124            }
1125            (_, ty::Ref(_, inner_ty, _), BindingMode::REF)
1126                if self.can_eq(self.param_env, expected, *inner_ty) =>
1127            {
1128                err.span_suggestion_verbose(
1129                    span.with_hi(span.lo() + BytePos(4)),
1130                    "consider removing `ref`",
1131                    "",
1132                    Applicability::MaybeIncorrect,
1133                );
1134            }
1135            _ => (),
1136        }
1137    }
1138
1139    /// Precondition: pat is a `Ref(_)` pattern
1140    fn borrow_pat_suggestion(&self, err: &mut Diag<'_>, pat: &Pat<'_>) {
1141        let tcx = self.tcx;
1142        if let PatKind::Ref(inner, mutbl) = pat.kind
1143            && let PatKind::Binding(_, _, binding, ..) = inner.kind
1144        {
1145            let binding_parent = tcx.parent_hir_node(pat.hir_id);
1146            debug!(?inner, ?pat, ?binding_parent);
1147
1148            let mutability = match mutbl {
1149                ast::Mutability::Mut => "mut",
1150                ast::Mutability::Not => "",
1151            };
1152
1153            let mut_var_suggestion = 'block: {
1154                if mutbl.is_not() {
1155                    break 'block None;
1156                }
1157
1158                let ident_kind = match binding_parent {
1159                    hir::Node::Param(_) => "parameter",
1160                    hir::Node::LetStmt(_) => "variable",
1161                    hir::Node::Arm(_) => "binding",
1162
1163                    // Provide diagnostics only if the parent pattern is struct-like,
1164                    // i.e. where `mut binding` makes sense
1165                    hir::Node::Pat(Pat { kind, .. }) => match kind {
1166                        PatKind::Struct(..)
1167                        | PatKind::TupleStruct(..)
1168                        | PatKind::Or(..)
1169                        | PatKind::Guard(..)
1170                        | PatKind::Tuple(..)
1171                        | PatKind::Slice(..) => "binding",
1172
1173                        PatKind::Missing
1174                        | PatKind::Wild
1175                        | PatKind::Never
1176                        | PatKind::Binding(..)
1177                        | PatKind::Box(..)
1178                        | PatKind::Deref(_)
1179                        | PatKind::Ref(..)
1180                        | PatKind::Expr(..)
1181                        | PatKind::Range(..)
1182                        | PatKind::Err(_) => break 'block None,
1183                    },
1184
1185                    // Don't provide suggestions in other cases
1186                    _ => break 'block None,
1187                };
1188
1189                Some((
1190                    pat.span,
1191                    format!("to declare a mutable {ident_kind} use"),
1192                    format!("mut {binding}"),
1193                ))
1194            };
1195
1196            match binding_parent {
1197                // Check that there is explicit type (ie this is not a closure param with inferred type)
1198                // so we don't suggest moving something to the type that does not exist
1199                hir::Node::Param(hir::Param { ty_span, pat, .. }) if pat.span != *ty_span => {
1200                    err.multipart_suggestion_verbose(
1201                        format!("to take parameter `{binding}` by reference, move `&{mutability}` to the type"),
1202                        vec![
1203                            (pat.span.until(inner.span), "".to_owned()),
1204                            (ty_span.shrink_to_lo(), mutbl.ref_prefix_str().to_owned()),
1205                        ],
1206                        Applicability::MachineApplicable
1207                    );
1208
1209                    if let Some((sp, msg, sugg)) = mut_var_suggestion {
1210                        err.span_note(sp, format!("{msg}: `{sugg}`"));
1211                    }
1212                }
1213                hir::Node::Pat(pt) if let PatKind::TupleStruct(_, pat_arr, _) = pt.kind => {
1214                    for i in pat_arr.iter() {
1215                        if let PatKind::Ref(the_ref, _) = i.kind
1216                            && let PatKind::Binding(mt, _, ident, _) = the_ref.kind
1217                        {
1218                            let BindingMode(_, mtblty) = mt;
1219                            err.span_suggestion_verbose(
1220                                i.span,
1221                                format!("consider removing `&{mutability}` from the pattern"),
1222                                mtblty.prefix_str().to_string() + &ident.name.to_string(),
1223                                Applicability::MaybeIncorrect,
1224                            );
1225                        }
1226                    }
1227                    if let Some((sp, msg, sugg)) = mut_var_suggestion {
1228                        err.span_note(sp, format!("{msg}: `{sugg}`"));
1229                    }
1230                }
1231                hir::Node::Param(_) | hir::Node::Arm(_) | hir::Node::Pat(_) => {
1232                    // rely on match ergonomics or it might be nested `&&pat`
1233                    err.span_suggestion_verbose(
1234                        pat.span.until(inner.span),
1235                        format!("consider removing `&{mutability}` from the pattern"),
1236                        "",
1237                        Applicability::MaybeIncorrect,
1238                    );
1239
1240                    if let Some((sp, msg, sugg)) = mut_var_suggestion {
1241                        err.span_note(sp, format!("{msg}: `{sugg}`"));
1242                    }
1243                }
1244                _ if let Some((sp, msg, sugg)) = mut_var_suggestion => {
1245                    err.span_suggestion(sp, msg, sugg, Applicability::MachineApplicable);
1246                }
1247                _ => {} // don't provide suggestions in other cases #55175
1248            }
1249        }
1250    }
1251
1252    fn check_dereferenceable(
1253        &self,
1254        span: Span,
1255        expected: Ty<'tcx>,
1256        inner: &Pat<'_>,
1257    ) -> Result<(), ErrorGuaranteed> {
1258        if let PatKind::Binding(..) = inner.kind
1259            && let Some(pointee_ty) = self.shallow_resolve(expected).builtin_deref(true)
1260            && let ty::Dynamic(..) = pointee_ty.kind()
1261        {
1262            // This is "x = dyn SomeTrait" being reduced from
1263            // "let &x = &dyn SomeTrait" or "let box x = Box<dyn SomeTrait>", an error.
1264            let type_str = self.ty_to_string(expected);
1265            let mut err = struct_span_code_err!(
1266                self.dcx(),
1267                span,
1268                E0033,
1269                "type `{}` cannot be dereferenced",
1270                type_str
1271            );
1272            err.span_label(span, format!("type `{type_str}` cannot be dereferenced"));
1273            if self.tcx.sess.teach(err.code.unwrap()) {
1274                err.note(CANNOT_IMPLICITLY_DEREF_POINTER_TRAIT_OBJ);
1275            }
1276            return Err(err.emit());
1277        }
1278        Ok(())
1279    }
1280
1281    fn resolve_pat_struct(
1282        &self,
1283        pat: &'tcx Pat<'tcx>,
1284        qpath: &hir::QPath<'tcx>,
1285    ) -> Result<ResolvedPat<'tcx>, ErrorGuaranteed> {
1286        // Resolve the path and check the definition for errors.
1287        let (variant, pat_ty) = self.check_struct_path(qpath, pat.hir_id)?;
1288        Ok(ResolvedPat { ty: pat_ty, kind: ResolvedPatKind::Struct { variant } })
1289    }
1290
1291    fn check_pat_struct(
1292        &self,
1293        pat: &'tcx Pat<'tcx>,
1294        fields: &'tcx [hir::PatField<'tcx>],
1295        has_rest_pat: bool,
1296        pat_ty: Ty<'tcx>,
1297        variant: &'tcx VariantDef,
1298        expected: Ty<'tcx>,
1299        pat_info: PatInfo<'tcx>,
1300    ) -> Ty<'tcx> {
1301        // Type-check the path.
1302        let _ = self.demand_eqtype_pat(pat.span, expected, pat_ty, &pat_info.top_info);
1303
1304        // Type-check subpatterns.
1305        match self.check_struct_pat_fields(pat_ty, pat, variant, fields, has_rest_pat, pat_info) {
1306            Ok(()) => pat_ty,
1307            Err(guar) => Ty::new_error(self.tcx, guar),
1308        }
1309    }
1310
1311    fn resolve_pat_path(
1312        &self,
1313        path_id: HirId,
1314        span: Span,
1315        qpath: &'tcx hir::QPath<'_>,
1316    ) -> Result<ResolvedPat<'tcx>, ErrorGuaranteed> {
1317        let tcx = self.tcx;
1318
1319        let (res, opt_ty, segments) =
1320            self.resolve_ty_and_res_fully_qualified_call(qpath, path_id, span);
1321        match res {
1322            Res::Err => {
1323                let e =
1324                    self.dcx().span_delayed_bug(qpath.span(), "`Res::Err` but no error emitted");
1325                self.set_tainted_by_errors(e);
1326                return Err(e);
1327            }
1328            Res::Def(DefKind::AssocFn | DefKind::Ctor(_, CtorKind::Fn) | DefKind::Variant, _) => {
1329                let expected = "unit struct, unit variant or constant";
1330                let e = report_unexpected_variant_res(tcx, res, None, qpath, span, E0533, expected);
1331                return Err(e);
1332            }
1333            Res::SelfCtor(def_id) => {
1334                if let ty::Adt(adt_def, _) = *tcx.type_of(def_id).skip_binder().kind()
1335                    && adt_def.is_struct()
1336                    && let Some((CtorKind::Const, _)) = adt_def.non_enum_variant().ctor
1337                {
1338                    // Ok, we allow unit struct ctors in patterns only.
1339                } else {
1340                    let e = report_unexpected_variant_res(
1341                        tcx,
1342                        res,
1343                        None,
1344                        qpath,
1345                        span,
1346                        E0533,
1347                        "unit struct",
1348                    );
1349                    return Err(e);
1350                }
1351            }
1352            Res::Def(
1353                DefKind::Ctor(_, CtorKind::Const)
1354                | DefKind::Const
1355                | DefKind::AssocConst
1356                | DefKind::ConstParam,
1357                _,
1358            ) => {} // OK
1359            _ => bug!("unexpected pattern resolution: {:?}", res),
1360        }
1361
1362        // Find the type of the path pattern, for later checking.
1363        let (pat_ty, pat_res) =
1364            self.instantiate_value_path(segments, opt_ty, res, span, span, path_id);
1365        Ok(ResolvedPat { ty: pat_ty, kind: ResolvedPatKind::Path { res, pat_res, segments } })
1366    }
1367
1368    fn check_pat_path(
1369        &self,
1370        pat_id_for_diag: HirId,
1371        span: Span,
1372        resolved: &ResolvedPat<'tcx>,
1373        expected: Ty<'tcx>,
1374        ti: &TopInfo<'tcx>,
1375    ) -> Ty<'tcx> {
1376        if let Err(err) =
1377            self.demand_suptype_with_origin(&self.pattern_cause(ti, span), expected, resolved.ty)
1378        {
1379            self.emit_bad_pat_path(err, pat_id_for_diag, span, resolved);
1380        }
1381        resolved.ty
1382    }
1383
1384    fn maybe_suggest_range_literal(
1385        &self,
1386        e: &mut Diag<'_>,
1387        opt_def_id: Option<hir::def_id::DefId>,
1388        ident: Ident,
1389    ) -> bool {
1390        match opt_def_id {
1391            Some(def_id) => match self.tcx.hir_get_if_local(def_id) {
1392                Some(hir::Node::Item(hir::Item {
1393                    kind: hir::ItemKind::Const(_, _, _, body_id),
1394                    ..
1395                })) => match self.tcx.hir_node(body_id.hir_id) {
1396                    hir::Node::Expr(expr) => {
1397                        if hir::is_range_literal(expr) {
1398                            let span = self.tcx.hir_span(body_id.hir_id);
1399                            if let Ok(snip) = self.tcx.sess.source_map().span_to_snippet(span) {
1400                                e.span_suggestion_verbose(
1401                                    ident.span,
1402                                    "you may want to move the range into the match block",
1403                                    snip,
1404                                    Applicability::MachineApplicable,
1405                                );
1406                                return true;
1407                            }
1408                        }
1409                    }
1410                    _ => (),
1411                },
1412                _ => (),
1413            },
1414            _ => (),
1415        }
1416        false
1417    }
1418
1419    fn emit_bad_pat_path(
1420        &self,
1421        mut e: Diag<'_>,
1422        hir_id: HirId,
1423        pat_span: Span,
1424        resolved_pat: &ResolvedPat<'tcx>,
1425    ) {
1426        let ResolvedPatKind::Path { res, pat_res, segments } = resolved_pat.kind else {
1427            span_bug!(pat_span, "unexpected resolution for path pattern: {resolved_pat:?}");
1428        };
1429
1430        if let Some(span) = self.tcx.hir_res_span(pat_res) {
1431            e.span_label(span, format!("{} defined here", res.descr()));
1432            if let [hir::PathSegment { ident, .. }] = &*segments {
1433                e.span_label(
1434                    pat_span,
1435                    format!(
1436                        "`{}` is interpreted as {} {}, not a new binding",
1437                        ident,
1438                        res.article(),
1439                        res.descr(),
1440                    ),
1441                );
1442                match self.tcx.parent_hir_node(hir_id) {
1443                    hir::Node::PatField(..) => {
1444                        e.span_suggestion_verbose(
1445                            ident.span.shrink_to_hi(),
1446                            "bind the struct field to a different name instead",
1447                            format!(": other_{}", ident.as_str().to_lowercase()),
1448                            Applicability::HasPlaceholders,
1449                        );
1450                    }
1451                    _ => {
1452                        let (type_def_id, item_def_id) = match resolved_pat.ty.kind() {
1453                            ty::Adt(def, _) => match res {
1454                                Res::Def(DefKind::Const, def_id) => (Some(def.did()), Some(def_id)),
1455                                _ => (None, None),
1456                            },
1457                            _ => (None, None),
1458                        };
1459
1460                        let ranges = &[
1461                            self.tcx.lang_items().range_struct(),
1462                            self.tcx.lang_items().range_from_struct(),
1463                            self.tcx.lang_items().range_to_struct(),
1464                            self.tcx.lang_items().range_full_struct(),
1465                            self.tcx.lang_items().range_inclusive_struct(),
1466                            self.tcx.lang_items().range_to_inclusive_struct(),
1467                        ];
1468                        if type_def_id != None && ranges.contains(&type_def_id) {
1469                            if !self.maybe_suggest_range_literal(&mut e, item_def_id, *ident) {
1470                                let msg = "constants only support matching by type, \
1471                                    if you meant to match against a range of values, \
1472                                    consider using a range pattern like `min ..= max` in the match block";
1473                                e.note(msg);
1474                            }
1475                        } else {
1476                            let msg = "introduce a new binding instead";
1477                            let sugg = format!("other_{}", ident.as_str().to_lowercase());
1478                            e.span_suggestion(
1479                                ident.span,
1480                                msg,
1481                                sugg,
1482                                Applicability::HasPlaceholders,
1483                            );
1484                        }
1485                    }
1486                };
1487            }
1488        }
1489        e.emit();
1490    }
1491
1492    fn resolve_pat_tuple_struct(
1493        &self,
1494        pat: &'tcx Pat<'tcx>,
1495        qpath: &'tcx hir::QPath<'tcx>,
1496    ) -> Result<ResolvedPat<'tcx>, ErrorGuaranteed> {
1497        let tcx = self.tcx;
1498        let report_unexpected_res = |res: Res| {
1499            let expected = "tuple struct or tuple variant";
1500            let e = report_unexpected_variant_res(tcx, res, None, qpath, pat.span, E0164, expected);
1501            Err(e)
1502        };
1503
1504        // Resolve the path and check the definition for errors.
1505        let (res, opt_ty, segments) =
1506            self.resolve_ty_and_res_fully_qualified_call(qpath, pat.hir_id, pat.span);
1507        if res == Res::Err {
1508            let e = self.dcx().span_delayed_bug(pat.span, "`Res::Err` but no error emitted");
1509            self.set_tainted_by_errors(e);
1510            return Err(e);
1511        }
1512
1513        // Type-check the path.
1514        let (pat_ty, res) =
1515            self.instantiate_value_path(segments, opt_ty, res, pat.span, pat.span, pat.hir_id);
1516        if !pat_ty.is_fn() {
1517            return report_unexpected_res(res);
1518        }
1519
1520        let variant = match res {
1521            Res::Err => {
1522                self.dcx().span_bug(pat.span, "`Res::Err` but no error emitted");
1523            }
1524            Res::Def(DefKind::AssocConst | DefKind::AssocFn, _) => {
1525                return report_unexpected_res(res);
1526            }
1527            Res::Def(DefKind::Ctor(_, CtorKind::Fn), _) => tcx.expect_variant_res(res),
1528            _ => bug!("unexpected pattern resolution: {:?}", res),
1529        };
1530
1531        // Replace constructor type with constructed type for tuple struct patterns.
1532        let pat_ty = pat_ty.fn_sig(tcx).output();
1533        let pat_ty = pat_ty.no_bound_vars().expect("expected fn type");
1534
1535        Ok(ResolvedPat { ty: pat_ty, kind: ResolvedPatKind::TupleStruct { res, variant } })
1536    }
1537
1538    fn check_pat_tuple_struct(
1539        &self,
1540        pat: &'tcx Pat<'tcx>,
1541        qpath: &'tcx hir::QPath<'tcx>,
1542        subpats: &'tcx [Pat<'tcx>],
1543        ddpos: hir::DotDotPos,
1544        res: Res,
1545        pat_ty: Ty<'tcx>,
1546        variant: &'tcx VariantDef,
1547        expected: Ty<'tcx>,
1548        pat_info: PatInfo<'tcx>,
1549    ) -> Ty<'tcx> {
1550        let tcx = self.tcx;
1551        let on_error = |e| {
1552            for pat in subpats {
1553                self.check_pat(pat, Ty::new_error(tcx, e), pat_info);
1554            }
1555        };
1556
1557        // Type-check the tuple struct pattern against the expected type.
1558        let diag = self.demand_eqtype_pat_diag(pat.span, expected, pat_ty, &pat_info.top_info);
1559        let had_err = diag.map_err(|diag| diag.emit());
1560
1561        // Type-check subpatterns.
1562        if subpats.len() == variant.fields.len()
1563            || subpats.len() < variant.fields.len() && ddpos.as_opt_usize().is_some()
1564        {
1565            let ty::Adt(_, args) = pat_ty.kind() else {
1566                bug!("unexpected pattern type {:?}", pat_ty);
1567            };
1568            for (i, subpat) in subpats.iter().enumerate_and_adjust(variant.fields.len(), ddpos) {
1569                let field = &variant.fields[FieldIdx::from_usize(i)];
1570                let field_ty = self.field_ty(subpat.span, field, args);
1571                self.check_pat(subpat, field_ty, pat_info);
1572
1573                self.tcx.check_stability(
1574                    variant.fields[FieldIdx::from_usize(i)].did,
1575                    Some(subpat.hir_id),
1576                    subpat.span,
1577                    None,
1578                );
1579            }
1580            if let Err(e) = had_err {
1581                on_error(e);
1582                return Ty::new_error(tcx, e);
1583            }
1584        } else {
1585            let e = self.emit_err_pat_wrong_number_of_fields(
1586                pat.span,
1587                res,
1588                qpath,
1589                subpats,
1590                &variant.fields.raw,
1591                expected,
1592                had_err,
1593            );
1594            on_error(e);
1595            return Ty::new_error(tcx, e);
1596        }
1597        pat_ty
1598    }
1599
1600    fn emit_err_pat_wrong_number_of_fields(
1601        &self,
1602        pat_span: Span,
1603        res: Res,
1604        qpath: &hir::QPath<'_>,
1605        subpats: &'tcx [Pat<'tcx>],
1606        fields: &'tcx [ty::FieldDef],
1607        expected: Ty<'tcx>,
1608        had_err: Result<(), ErrorGuaranteed>,
1609    ) -> ErrorGuaranteed {
1610        let subpats_ending = pluralize!(subpats.len());
1611        let fields_ending = pluralize!(fields.len());
1612
1613        let subpat_spans = if subpats.is_empty() {
1614            vec![pat_span]
1615        } else {
1616            subpats.iter().map(|p| p.span).collect()
1617        };
1618        let last_subpat_span = *subpat_spans.last().unwrap();
1619        let res_span = self.tcx.def_span(res.def_id());
1620        let def_ident_span = self.tcx.def_ident_span(res.def_id()).unwrap_or(res_span);
1621        let field_def_spans = if fields.is_empty() {
1622            vec![res_span]
1623        } else {
1624            fields.iter().map(|f| f.ident(self.tcx).span).collect()
1625        };
1626        let last_field_def_span = *field_def_spans.last().unwrap();
1627
1628        let mut err = struct_span_code_err!(
1629            self.dcx(),
1630            MultiSpan::from_spans(subpat_spans),
1631            E0023,
1632            "this pattern has {} field{}, but the corresponding {} has {} field{}",
1633            subpats.len(),
1634            subpats_ending,
1635            res.descr(),
1636            fields.len(),
1637            fields_ending,
1638        );
1639        err.span_label(
1640            last_subpat_span,
1641            format!("expected {} field{}, found {}", fields.len(), fields_ending, subpats.len()),
1642        );
1643        if self.tcx.sess.source_map().is_multiline(qpath.span().between(last_subpat_span)) {
1644            err.span_label(qpath.span(), "");
1645        }
1646        if self.tcx.sess.source_map().is_multiline(def_ident_span.between(last_field_def_span)) {
1647            err.span_label(def_ident_span, format!("{} defined here", res.descr()));
1648        }
1649        for span in &field_def_spans[..field_def_spans.len() - 1] {
1650            err.span_label(*span, "");
1651        }
1652        err.span_label(
1653            last_field_def_span,
1654            format!("{} has {} field{}", res.descr(), fields.len(), fields_ending),
1655        );
1656
1657        // Identify the case `Some(x, y)` where the expected type is e.g. `Option<(T, U)>`.
1658        // More generally, the expected type wants a tuple variant with one field of an
1659        // N-arity-tuple, e.g., `V_i((p_0, .., p_N))`. Meanwhile, the user supplied a pattern
1660        // with the subpatterns directly in the tuple variant pattern, e.g., `V_i(p_0, .., p_N)`.
1661        let missing_parentheses = match (expected.kind(), fields, had_err) {
1662            // #67037: only do this if we could successfully type-check the expected type against
1663            // the tuple struct pattern. Otherwise the args could get out of range on e.g.,
1664            // `let P() = U;` where `P != U` with `struct P<T>(T);`.
1665            (ty::Adt(_, args), [field], Ok(())) => {
1666                let field_ty = self.field_ty(pat_span, field, args);
1667                match field_ty.kind() {
1668                    ty::Tuple(fields) => fields.len() == subpats.len(),
1669                    _ => false,
1670                }
1671            }
1672            _ => false,
1673        };
1674        if missing_parentheses {
1675            let (left, right) = match subpats {
1676                // This is the zero case; we aim to get the "hi" part of the `QPath`'s
1677                // span as the "lo" and then the "hi" part of the pattern's span as the "hi".
1678                // This looks like:
1679                //
1680                // help: missing parentheses
1681                //   |
1682                // L |     let A(()) = A(());
1683                //   |          ^  ^
1684                [] => (qpath.span().shrink_to_hi(), pat_span),
1685                // Easy case. Just take the "lo" of the first sub-pattern and the "hi" of the
1686                // last sub-pattern. In the case of `A(x)` the first and last may coincide.
1687                // This looks like:
1688                //
1689                // help: missing parentheses
1690                //   |
1691                // L |     let A((x, y)) = A((1, 2));
1692                //   |           ^    ^
1693                [first, ..] => (first.span.shrink_to_lo(), subpats.last().unwrap().span),
1694            };
1695            err.multipart_suggestion(
1696                "missing parentheses",
1697                vec![(left, "(".to_string()), (right.shrink_to_hi(), ")".to_string())],
1698                Applicability::MachineApplicable,
1699            );
1700        } else if fields.len() > subpats.len() && pat_span != DUMMY_SP {
1701            let after_fields_span = pat_span.with_hi(pat_span.hi() - BytePos(1)).shrink_to_hi();
1702            let all_fields_span = match subpats {
1703                [] => after_fields_span,
1704                [field] => field.span,
1705                [first, .., last] => first.span.to(last.span),
1706            };
1707
1708            // Check if all the fields in the pattern are wildcards.
1709            let all_wildcards = subpats.iter().all(|pat| matches!(pat.kind, PatKind::Wild));
1710            let first_tail_wildcard =
1711                subpats.iter().enumerate().fold(None, |acc, (pos, pat)| match (acc, &pat.kind) {
1712                    (None, PatKind::Wild) => Some(pos),
1713                    (Some(_), PatKind::Wild) => acc,
1714                    _ => None,
1715                });
1716            let tail_span = match first_tail_wildcard {
1717                None => after_fields_span,
1718                Some(0) => subpats[0].span.to(after_fields_span),
1719                Some(pos) => subpats[pos - 1].span.shrink_to_hi().to(after_fields_span),
1720            };
1721
1722            // FIXME: heuristic-based suggestion to check current types for where to add `_`.
1723            let mut wildcard_sugg = vec!["_"; fields.len() - subpats.len()].join(", ");
1724            if !subpats.is_empty() {
1725                wildcard_sugg = String::from(", ") + &wildcard_sugg;
1726            }
1727
1728            err.span_suggestion_verbose(
1729                after_fields_span,
1730                "use `_` to explicitly ignore each field",
1731                wildcard_sugg,
1732                Applicability::MaybeIncorrect,
1733            );
1734
1735            // Only suggest `..` if more than one field is missing
1736            // or the pattern consists of all wildcards.
1737            if fields.len() - subpats.len() > 1 || all_wildcards {
1738                if subpats.is_empty() || all_wildcards {
1739                    err.span_suggestion_verbose(
1740                        all_fields_span,
1741                        "use `..` to ignore all fields",
1742                        "..",
1743                        Applicability::MaybeIncorrect,
1744                    );
1745                } else {
1746                    err.span_suggestion_verbose(
1747                        tail_span,
1748                        "use `..` to ignore the rest of the fields",
1749                        ", ..",
1750                        Applicability::MaybeIncorrect,
1751                    );
1752                }
1753            }
1754        }
1755
1756        err.emit()
1757    }
1758
1759    fn check_pat_tuple(
1760        &self,
1761        span: Span,
1762        elements: &'tcx [Pat<'tcx>],
1763        ddpos: hir::DotDotPos,
1764        expected: Ty<'tcx>,
1765        pat_info: PatInfo<'tcx>,
1766    ) -> Ty<'tcx> {
1767        let tcx = self.tcx;
1768        let mut expected_len = elements.len();
1769        if ddpos.as_opt_usize().is_some() {
1770            // Require known type only when `..` is present.
1771            if let ty::Tuple(tys) = self.structurally_resolve_type(span, expected).kind() {
1772                expected_len = tys.len();
1773            }
1774        }
1775        let max_len = cmp::max(expected_len, elements.len());
1776
1777        let element_tys_iter = (0..max_len).map(|_| self.next_ty_var(span));
1778        let element_tys = tcx.mk_type_list_from_iter(element_tys_iter);
1779        let pat_ty = Ty::new_tup(tcx, element_tys);
1780        if let Err(reported) = self.demand_eqtype_pat(span, expected, pat_ty, &pat_info.top_info) {
1781            // Walk subpatterns with an expected type of `err` in this case to silence
1782            // further errors being emitted when using the bindings. #50333
1783            let element_tys_iter = (0..max_len).map(|_| Ty::new_error(tcx, reported));
1784            for (_, elem) in elements.iter().enumerate_and_adjust(max_len, ddpos) {
1785                self.check_pat(elem, Ty::new_error(tcx, reported), pat_info);
1786            }
1787            Ty::new_tup_from_iter(tcx, element_tys_iter)
1788        } else {
1789            for (i, elem) in elements.iter().enumerate_and_adjust(max_len, ddpos) {
1790                self.check_pat(elem, element_tys[i], pat_info);
1791            }
1792            pat_ty
1793        }
1794    }
1795
1796    fn check_struct_pat_fields(
1797        &self,
1798        adt_ty: Ty<'tcx>,
1799        pat: &'tcx Pat<'tcx>,
1800        variant: &'tcx ty::VariantDef,
1801        fields: &'tcx [hir::PatField<'tcx>],
1802        has_rest_pat: bool,
1803        pat_info: PatInfo<'tcx>,
1804    ) -> Result<(), ErrorGuaranteed> {
1805        let tcx = self.tcx;
1806
1807        let ty::Adt(adt, args) = adt_ty.kind() else {
1808            span_bug!(pat.span, "struct pattern is not an ADT");
1809        };
1810
1811        // Index the struct fields' types.
1812        let field_map = variant
1813            .fields
1814            .iter_enumerated()
1815            .map(|(i, field)| (field.ident(self.tcx).normalize_to_macros_2_0(), (i, field)))
1816            .collect::<FxHashMap<_, _>>();
1817
1818        // Keep track of which fields have already appeared in the pattern.
1819        let mut used_fields = FxHashMap::default();
1820        let mut result = Ok(());
1821
1822        let mut inexistent_fields = vec![];
1823        // Typecheck each field.
1824        for field in fields {
1825            let span = field.span;
1826            let ident = tcx.adjust_ident(field.ident, variant.def_id);
1827            let field_ty = match used_fields.entry(ident) {
1828                Occupied(occupied) => {
1829                    let guar = self.error_field_already_bound(span, field.ident, *occupied.get());
1830                    result = Err(guar);
1831                    Ty::new_error(tcx, guar)
1832                }
1833                Vacant(vacant) => {
1834                    vacant.insert(span);
1835                    field_map
1836                        .get(&ident)
1837                        .map(|(i, f)| {
1838                            self.write_field_index(field.hir_id, *i);
1839                            self.tcx.check_stability(f.did, Some(field.hir_id), span, None);
1840                            self.field_ty(span, f, args)
1841                        })
1842                        .unwrap_or_else(|| {
1843                            inexistent_fields.push(field);
1844                            Ty::new_misc_error(tcx)
1845                        })
1846                }
1847            };
1848
1849            self.check_pat(field.pat, field_ty, pat_info);
1850        }
1851
1852        let mut unmentioned_fields = variant
1853            .fields
1854            .iter()
1855            .map(|field| (field, field.ident(self.tcx).normalize_to_macros_2_0()))
1856            .filter(|(_, ident)| !used_fields.contains_key(ident))
1857            .collect::<Vec<_>>();
1858
1859        let inexistent_fields_err = if !inexistent_fields.is_empty()
1860            && !inexistent_fields.iter().any(|field| field.ident.name == kw::Underscore)
1861        {
1862            // we don't care to report errors for a struct if the struct itself is tainted
1863            variant.has_errors()?;
1864            Some(self.error_inexistent_fields(
1865                adt.variant_descr(),
1866                &inexistent_fields,
1867                &mut unmentioned_fields,
1868                pat,
1869                variant,
1870                args,
1871            ))
1872        } else {
1873            None
1874        };
1875
1876        // Require `..` if struct has non_exhaustive attribute.
1877        let non_exhaustive = variant.field_list_has_applicable_non_exhaustive();
1878        if non_exhaustive && !has_rest_pat {
1879            self.error_foreign_non_exhaustive_spat(pat, adt.variant_descr(), fields.is_empty());
1880        }
1881
1882        let mut unmentioned_err = None;
1883        // Report an error if an incorrect number of fields was specified.
1884        if adt.is_union() {
1885            if fields.len() != 1 {
1886                self.dcx().emit_err(errors::UnionPatMultipleFields { span: pat.span });
1887            }
1888            if has_rest_pat {
1889                self.dcx().emit_err(errors::UnionPatDotDot { span: pat.span });
1890            }
1891        } else if !unmentioned_fields.is_empty() {
1892            let accessible_unmentioned_fields: Vec<_> = unmentioned_fields
1893                .iter()
1894                .copied()
1895                .filter(|(field, _)| self.is_field_suggestable(field, pat.hir_id, pat.span))
1896                .collect();
1897
1898            if !has_rest_pat {
1899                if accessible_unmentioned_fields.is_empty() {
1900                    unmentioned_err = Some(self.error_no_accessible_fields(pat, fields));
1901                } else {
1902                    unmentioned_err = Some(self.error_unmentioned_fields(
1903                        pat,
1904                        &accessible_unmentioned_fields,
1905                        accessible_unmentioned_fields.len() != unmentioned_fields.len(),
1906                        fields,
1907                    ));
1908                }
1909            } else if non_exhaustive && !accessible_unmentioned_fields.is_empty() {
1910                self.lint_non_exhaustive_omitted_patterns(
1911                    pat,
1912                    &accessible_unmentioned_fields,
1913                    adt_ty,
1914                )
1915            }
1916        }
1917        match (inexistent_fields_err, unmentioned_err) {
1918            (Some(i), Some(u)) => {
1919                if let Err(e) = self.error_tuple_variant_as_struct_pat(pat, fields, variant) {
1920                    // We don't want to show the nonexistent fields error when this was
1921                    // `Foo { a, b }` when it should have been `Foo(a, b)`.
1922                    i.delay_as_bug();
1923                    u.delay_as_bug();
1924                    Err(e)
1925                } else {
1926                    i.emit();
1927                    Err(u.emit())
1928                }
1929            }
1930            (None, Some(u)) => {
1931                if let Err(e) = self.error_tuple_variant_as_struct_pat(pat, fields, variant) {
1932                    u.delay_as_bug();
1933                    Err(e)
1934                } else {
1935                    Err(u.emit())
1936                }
1937            }
1938            (Some(err), None) => Err(err.emit()),
1939            (None, None) => {
1940                self.error_tuple_variant_index_shorthand(variant, pat, fields)?;
1941                result
1942            }
1943        }
1944    }
1945
1946    fn error_tuple_variant_index_shorthand(
1947        &self,
1948        variant: &VariantDef,
1949        pat: &'_ Pat<'_>,
1950        fields: &[hir::PatField<'_>],
1951    ) -> Result<(), ErrorGuaranteed> {
1952        // if this is a tuple struct, then all field names will be numbers
1953        // so if any fields in a struct pattern use shorthand syntax, they will
1954        // be invalid identifiers (for example, Foo { 0, 1 }).
1955        if let (Some(CtorKind::Fn), PatKind::Struct(qpath, field_patterns, ..)) =
1956            (variant.ctor_kind(), &pat.kind)
1957        {
1958            let has_shorthand_field_name = field_patterns.iter().any(|field| field.is_shorthand);
1959            if has_shorthand_field_name {
1960                let path = rustc_hir_pretty::qpath_to_string(&self.tcx, qpath);
1961                let mut err = struct_span_code_err!(
1962                    self.dcx(),
1963                    pat.span,
1964                    E0769,
1965                    "tuple variant `{path}` written as struct variant",
1966                );
1967                err.span_suggestion_verbose(
1968                    qpath.span().shrink_to_hi().to(pat.span.shrink_to_hi()),
1969                    "use the tuple variant pattern syntax instead",
1970                    format!("({})", self.get_suggested_tuple_struct_pattern(fields, variant)),
1971                    Applicability::MaybeIncorrect,
1972                );
1973                return Err(err.emit());
1974            }
1975        }
1976        Ok(())
1977    }
1978
1979    fn error_foreign_non_exhaustive_spat(&self, pat: &Pat<'_>, descr: &str, no_fields: bool) {
1980        let sess = self.tcx.sess;
1981        let sm = sess.source_map();
1982        let sp_brace = sm.end_point(pat.span);
1983        let sp_comma = sm.end_point(pat.span.with_hi(sp_brace.hi()));
1984        let sugg = if no_fields || sp_brace != sp_comma { ".. }" } else { ", .. }" };
1985
1986        struct_span_code_err!(
1987            self.dcx(),
1988            pat.span,
1989            E0638,
1990            "`..` required with {descr} marked as non-exhaustive",
1991        )
1992        .with_span_suggestion_verbose(
1993            sp_comma,
1994            "add `..` at the end of the field list to ignore all other fields",
1995            sugg,
1996            Applicability::MachineApplicable,
1997        )
1998        .emit();
1999    }
2000
2001    fn error_field_already_bound(
2002        &self,
2003        span: Span,
2004        ident: Ident,
2005        other_field: Span,
2006    ) -> ErrorGuaranteed {
2007        struct_span_code_err!(
2008            self.dcx(),
2009            span,
2010            E0025,
2011            "field `{}` bound multiple times in the pattern",
2012            ident
2013        )
2014        .with_span_label(span, format!("multiple uses of `{ident}` in pattern"))
2015        .with_span_label(other_field, format!("first use of `{ident}`"))
2016        .emit()
2017    }
2018
2019    fn error_inexistent_fields(
2020        &self,
2021        kind_name: &str,
2022        inexistent_fields: &[&hir::PatField<'tcx>],
2023        unmentioned_fields: &mut Vec<(&'tcx ty::FieldDef, Ident)>,
2024        pat: &'tcx Pat<'tcx>,
2025        variant: &ty::VariantDef,
2026        args: ty::GenericArgsRef<'tcx>,
2027    ) -> Diag<'a> {
2028        let tcx = self.tcx;
2029        let (field_names, t, plural) = if let [field] = inexistent_fields {
2030            (format!("a field named `{}`", field.ident), "this", "")
2031        } else {
2032            (
2033                format!(
2034                    "fields named {}",
2035                    inexistent_fields
2036                        .iter()
2037                        .map(|field| format!("`{}`", field.ident))
2038                        .collect::<Vec<String>>()
2039                        .join(", ")
2040                ),
2041                "these",
2042                "s",
2043            )
2044        };
2045        let spans = inexistent_fields.iter().map(|field| field.ident.span).collect::<Vec<_>>();
2046        let mut err = struct_span_code_err!(
2047            self.dcx(),
2048            spans,
2049            E0026,
2050            "{} `{}` does not have {}",
2051            kind_name,
2052            tcx.def_path_str(variant.def_id),
2053            field_names
2054        );
2055        if let Some(pat_field) = inexistent_fields.last() {
2056            err.span_label(
2057                pat_field.ident.span,
2058                format!(
2059                    "{} `{}` does not have {} field{}",
2060                    kind_name,
2061                    tcx.def_path_str(variant.def_id),
2062                    t,
2063                    plural
2064                ),
2065            );
2066
2067            if let [(field_def, field)] = unmentioned_fields.as_slice()
2068                && self.is_field_suggestable(field_def, pat.hir_id, pat.span)
2069            {
2070                let suggested_name =
2071                    find_best_match_for_name(&[field.name], pat_field.ident.name, None);
2072                if let Some(suggested_name) = suggested_name {
2073                    err.span_suggestion(
2074                        pat_field.ident.span,
2075                        "a field with a similar name exists",
2076                        suggested_name,
2077                        Applicability::MaybeIncorrect,
2078                    );
2079
2080                    // When we have a tuple struct used with struct we don't want to suggest using
2081                    // the (valid) struct syntax with numeric field names. Instead we want to
2082                    // suggest the expected syntax. We infer that this is the case by parsing the
2083                    // `Ident` into an unsized integer. The suggestion will be emitted elsewhere in
2084                    // `smart_resolve_context_dependent_help`.
2085                    if suggested_name.to_ident_string().parse::<usize>().is_err() {
2086                        // We don't want to throw `E0027` in case we have thrown `E0026` for them.
2087                        unmentioned_fields.retain(|&(_, x)| x.name != suggested_name);
2088                    }
2089                } else if inexistent_fields.len() == 1 {
2090                    match pat_field.pat.kind {
2091                        PatKind::Expr(_)
2092                            if !self.may_coerce(
2093                                self.typeck_results.borrow().node_type(pat_field.pat.hir_id),
2094                                self.field_ty(field.span, field_def, args),
2095                            ) => {}
2096                        _ => {
2097                            err.span_suggestion_short(
2098                                pat_field.ident.span,
2099                                format!(
2100                                    "`{}` has a field named `{}`",
2101                                    tcx.def_path_str(variant.def_id),
2102                                    field.name,
2103                                ),
2104                                field.name,
2105                                Applicability::MaybeIncorrect,
2106                            );
2107                        }
2108                    }
2109                }
2110            }
2111        }
2112        if tcx.sess.teach(err.code.unwrap()) {
2113            err.note(
2114                "This error indicates that a struct pattern attempted to \
2115                 extract a nonexistent field from a struct. Struct fields \
2116                 are identified by the name used before the colon : so struct \
2117                 patterns should resemble the declaration of the struct type \
2118                 being matched.\n\n\
2119                 If you are using shorthand field patterns but want to refer \
2120                 to the struct field by a different name, you should rename \
2121                 it explicitly.",
2122            );
2123        }
2124        err
2125    }
2126
2127    fn error_tuple_variant_as_struct_pat(
2128        &self,
2129        pat: &Pat<'_>,
2130        fields: &'tcx [hir::PatField<'tcx>],
2131        variant: &ty::VariantDef,
2132    ) -> Result<(), ErrorGuaranteed> {
2133        if let (Some(CtorKind::Fn), PatKind::Struct(qpath, pattern_fields, ..)) =
2134            (variant.ctor_kind(), &pat.kind)
2135        {
2136            let is_tuple_struct_match = !pattern_fields.is_empty()
2137                && pattern_fields.iter().map(|field| field.ident.name.as_str()).all(is_number);
2138            if is_tuple_struct_match {
2139                return Ok(());
2140            }
2141
2142            // we don't care to report errors for a struct if the struct itself is tainted
2143            variant.has_errors()?;
2144
2145            let path = rustc_hir_pretty::qpath_to_string(&self.tcx, qpath);
2146            let mut err = struct_span_code_err!(
2147                self.dcx(),
2148                pat.span,
2149                E0769,
2150                "tuple variant `{}` written as struct variant",
2151                path
2152            );
2153            let (sugg, appl) = if fields.len() == variant.fields.len() {
2154                (
2155                    self.get_suggested_tuple_struct_pattern(fields, variant),
2156                    Applicability::MachineApplicable,
2157                )
2158            } else {
2159                (
2160                    variant.fields.iter().map(|_| "_").collect::<Vec<&str>>().join(", "),
2161                    Applicability::MaybeIncorrect,
2162                )
2163            };
2164            err.span_suggestion_verbose(
2165                qpath.span().shrink_to_hi().to(pat.span.shrink_to_hi()),
2166                "use the tuple variant pattern syntax instead",
2167                format!("({sugg})"),
2168                appl,
2169            );
2170            return Err(err.emit());
2171        }
2172        Ok(())
2173    }
2174
2175    fn get_suggested_tuple_struct_pattern(
2176        &self,
2177        fields: &[hir::PatField<'_>],
2178        variant: &VariantDef,
2179    ) -> String {
2180        let variant_field_idents =
2181            variant.fields.iter().map(|f| f.ident(self.tcx)).collect::<Vec<Ident>>();
2182        fields
2183            .iter()
2184            .map(|field| {
2185                match self.tcx.sess.source_map().span_to_snippet(field.pat.span) {
2186                    Ok(f) => {
2187                        // Field names are numbers, but numbers
2188                        // are not valid identifiers
2189                        if variant_field_idents.contains(&field.ident) {
2190                            String::from("_")
2191                        } else {
2192                            f
2193                        }
2194                    }
2195                    Err(_) => rustc_hir_pretty::pat_to_string(&self.tcx, field.pat),
2196                }
2197            })
2198            .collect::<Vec<String>>()
2199            .join(", ")
2200    }
2201
2202    /// Returns a diagnostic reporting a struct pattern which is missing an `..` due to
2203    /// inaccessible fields.
2204    ///
2205    /// ```text
2206    /// error: pattern requires `..` due to inaccessible fields
2207    ///   --> src/main.rs:10:9
2208    ///    |
2209    /// LL |     let foo::Foo {} = foo::Foo::default();
2210    ///    |         ^^^^^^^^^^^
2211    ///    |
2212    /// help: add a `..`
2213    ///    |
2214    /// LL |     let foo::Foo { .. } = foo::Foo::default();
2215    ///    |                  ^^^^^^
2216    /// ```
2217    fn error_no_accessible_fields(
2218        &self,
2219        pat: &Pat<'_>,
2220        fields: &'tcx [hir::PatField<'tcx>],
2221    ) -> Diag<'a> {
2222        let mut err = self
2223            .dcx()
2224            .struct_span_err(pat.span, "pattern requires `..` due to inaccessible fields");
2225
2226        if let Some(field) = fields.last() {
2227            err.span_suggestion_verbose(
2228                field.span.shrink_to_hi(),
2229                "ignore the inaccessible and unused fields",
2230                ", ..",
2231                Applicability::MachineApplicable,
2232            );
2233        } else {
2234            let qpath_span = if let PatKind::Struct(qpath, ..) = &pat.kind {
2235                qpath.span()
2236            } else {
2237                bug!("`error_no_accessible_fields` called on non-struct pattern");
2238            };
2239
2240            // Shrink the span to exclude the `foo:Foo` in `foo::Foo { }`.
2241            let span = pat.span.with_lo(qpath_span.shrink_to_hi().hi());
2242            err.span_suggestion_verbose(
2243                span,
2244                "ignore the inaccessible and unused fields",
2245                " { .. }",
2246                Applicability::MachineApplicable,
2247            );
2248        }
2249        err
2250    }
2251
2252    /// Report that a pattern for a `#[non_exhaustive]` struct marked with `non_exhaustive_omitted_patterns`
2253    /// is not exhaustive enough.
2254    ///
2255    /// Nb: the partner lint for enums lives in `compiler/rustc_mir_build/src/thir/pattern/usefulness.rs`.
2256    fn lint_non_exhaustive_omitted_patterns(
2257        &self,
2258        pat: &Pat<'_>,
2259        unmentioned_fields: &[(&ty::FieldDef, Ident)],
2260        ty: Ty<'tcx>,
2261    ) {
2262        fn joined_uncovered_patterns(witnesses: &[&Ident]) -> String {
2263            const LIMIT: usize = 3;
2264            match witnesses {
2265                [] => {
2266                    unreachable!(
2267                        "expected an uncovered pattern, otherwise why are we emitting an error?"
2268                    )
2269                }
2270                [witness] => format!("`{witness}`"),
2271                [head @ .., tail] if head.len() < LIMIT => {
2272                    let head: Vec<_> = head.iter().map(<_>::to_string).collect();
2273                    format!("`{}` and `{}`", head.join("`, `"), tail)
2274                }
2275                _ => {
2276                    let (head, tail) = witnesses.split_at(LIMIT);
2277                    let head: Vec<_> = head.iter().map(<_>::to_string).collect();
2278                    format!("`{}` and {} more", head.join("`, `"), tail.len())
2279                }
2280            }
2281        }
2282        let joined_patterns = joined_uncovered_patterns(
2283            &unmentioned_fields.iter().map(|(_, i)| i).collect::<Vec<_>>(),
2284        );
2285
2286        self.tcx.node_span_lint(NON_EXHAUSTIVE_OMITTED_PATTERNS, pat.hir_id, pat.span, |lint| {
2287            lint.primary_message("some fields are not explicitly listed");
2288            lint.span_label(pat.span, format!("field{} {} not listed", rustc_errors::pluralize!(unmentioned_fields.len()), joined_patterns));
2289            lint.help(
2290                "ensure that all fields are mentioned explicitly by adding the suggested fields",
2291            );
2292            lint.note(format!(
2293                "the pattern is of type `{ty}` and the `non_exhaustive_omitted_patterns` attribute was found",
2294            ));
2295        });
2296    }
2297
2298    /// Returns a diagnostic reporting a struct pattern which does not mention some fields.
2299    ///
2300    /// ```text
2301    /// error[E0027]: pattern does not mention field `bar`
2302    ///   --> src/main.rs:15:9
2303    ///    |
2304    /// LL |     let foo::Foo {} = foo::Foo::new();
2305    ///    |         ^^^^^^^^^^^ missing field `bar`
2306    /// ```
2307    fn error_unmentioned_fields(
2308        &self,
2309        pat: &Pat<'_>,
2310        unmentioned_fields: &[(&ty::FieldDef, Ident)],
2311        have_inaccessible_fields: bool,
2312        fields: &'tcx [hir::PatField<'tcx>],
2313    ) -> Diag<'a> {
2314        let inaccessible = if have_inaccessible_fields { " and inaccessible fields" } else { "" };
2315        let field_names = if let [(_, field)] = unmentioned_fields {
2316            format!("field `{field}`{inaccessible}")
2317        } else {
2318            let fields = unmentioned_fields
2319                .iter()
2320                .map(|(_, name)| format!("`{name}`"))
2321                .collect::<Vec<String>>()
2322                .join(", ");
2323            format!("fields {fields}{inaccessible}")
2324        };
2325        let mut err = struct_span_code_err!(
2326            self.dcx(),
2327            pat.span,
2328            E0027,
2329            "pattern does not mention {}",
2330            field_names
2331        );
2332        err.span_label(pat.span, format!("missing {field_names}"));
2333        let len = unmentioned_fields.len();
2334        let (prefix, postfix, sp) = match fields {
2335            [] => match &pat.kind {
2336                PatKind::Struct(path, [], false) => {
2337                    (" { ", " }", path.span().shrink_to_hi().until(pat.span.shrink_to_hi()))
2338                }
2339                _ => return err,
2340            },
2341            [.., field] => {
2342                // Account for last field having a trailing comma or parse recovery at the tail of
2343                // the pattern to avoid invalid suggestion (#78511).
2344                let tail = field.span.shrink_to_hi().with_hi(pat.span.hi());
2345                match &pat.kind {
2346                    PatKind::Struct(..) => (", ", " }", tail),
2347                    _ => return err,
2348                }
2349            }
2350        };
2351        err.span_suggestion(
2352            sp,
2353            format!(
2354                "include the missing field{} in the pattern{}",
2355                pluralize!(len),
2356                if have_inaccessible_fields { " and ignore the inaccessible fields" } else { "" }
2357            ),
2358            format!(
2359                "{}{}{}{}",
2360                prefix,
2361                unmentioned_fields
2362                    .iter()
2363                    .map(|(_, name)| {
2364                        let field_name = name.to_string();
2365                        if is_number(&field_name) { format!("{field_name}: _") } else { field_name }
2366                    })
2367                    .collect::<Vec<_>>()
2368                    .join(", "),
2369                if have_inaccessible_fields { ", .." } else { "" },
2370                postfix,
2371            ),
2372            Applicability::MachineApplicable,
2373        );
2374        err.span_suggestion(
2375            sp,
2376            format!(
2377                "if you don't care about {these} missing field{s}, you can explicitly ignore {them}",
2378                these = pluralize!("this", len),
2379                s = pluralize!(len),
2380                them = if len == 1 { "it" } else { "them" },
2381            ),
2382            format!(
2383                "{}{}{}{}",
2384                prefix,
2385                unmentioned_fields
2386                    .iter()
2387                    .map(|(_, name)| {
2388                        let field_name = name.to_string();
2389                        format!("{field_name}: _")
2390                    })
2391                    .collect::<Vec<_>>()
2392                    .join(", "),
2393                if have_inaccessible_fields { ", .." } else { "" },
2394                postfix,
2395            ),
2396            Applicability::MachineApplicable,
2397        );
2398        err.span_suggestion(
2399            sp,
2400            "or always ignore missing fields here",
2401            format!("{prefix}..{postfix}"),
2402            Applicability::MachineApplicable,
2403        );
2404        err
2405    }
2406
2407    fn check_pat_box(
2408        &self,
2409        span: Span,
2410        inner: &'tcx Pat<'tcx>,
2411        expected: Ty<'tcx>,
2412        pat_info: PatInfo<'tcx>,
2413    ) -> Ty<'tcx> {
2414        let tcx = self.tcx;
2415        let (box_ty, inner_ty) = self
2416            .check_dereferenceable(span, expected, inner)
2417            .and_then(|()| {
2418                // Here, `demand::subtype` is good enough, but I don't
2419                // think any errors can be introduced by using `demand::eqtype`.
2420                let inner_ty = self.next_ty_var(inner.span);
2421                let box_ty = Ty::new_box(tcx, inner_ty);
2422                self.demand_eqtype_pat(span, expected, box_ty, &pat_info.top_info)?;
2423                Ok((box_ty, inner_ty))
2424            })
2425            .unwrap_or_else(|guar| {
2426                let err = Ty::new_error(tcx, guar);
2427                (err, err)
2428            });
2429        self.check_pat(inner, inner_ty, pat_info);
2430        box_ty
2431    }
2432
2433    fn check_pat_deref(
2434        &self,
2435        span: Span,
2436        inner: &'tcx Pat<'tcx>,
2437        expected: Ty<'tcx>,
2438        pat_info: PatInfo<'tcx>,
2439    ) -> Ty<'tcx> {
2440        let target_ty = self.deref_pat_target(span, expected);
2441        self.check_pat(inner, target_ty, pat_info);
2442        self.register_deref_mut_bounds_if_needed(span, inner, [expected]);
2443        expected
2444    }
2445
2446    fn deref_pat_target(&self, span: Span, source_ty: Ty<'tcx>) -> Ty<'tcx> {
2447        // Register a `DerefPure` bound, which is required by all `deref!()` pats.
2448        let tcx = self.tcx;
2449        self.register_bound(
2450            source_ty,
2451            tcx.require_lang_item(hir::LangItem::DerefPure, Some(span)),
2452            self.misc(span),
2453        );
2454        // The expected type for the deref pat's inner pattern is `<expected as Deref>::Target`.
2455        let target_ty = Ty::new_projection(
2456            tcx,
2457            tcx.require_lang_item(hir::LangItem::DerefTarget, Some(span)),
2458            [source_ty],
2459        );
2460        let target_ty = self.normalize(span, target_ty);
2461        self.try_structurally_resolve_type(span, target_ty)
2462    }
2463
2464    /// Check if the interior of a deref pattern (either explicit or implicit) has any `ref mut`
2465    /// bindings, which would require `DerefMut` to be emitted in MIR building instead of just
2466    /// `Deref`. We do this *after* checking the inner pattern, since we want to make sure to
2467    /// account for `ref mut` binding modes inherited from implicitly dereferencing `&mut` refs.
2468    fn register_deref_mut_bounds_if_needed(
2469        &self,
2470        span: Span,
2471        inner: &'tcx Pat<'tcx>,
2472        derefed_tys: impl IntoIterator<Item = Ty<'tcx>>,
2473    ) {
2474        if self.typeck_results.borrow().pat_has_ref_mut_binding(inner) {
2475            for mutably_derefed_ty in derefed_tys {
2476                self.register_bound(
2477                    mutably_derefed_ty,
2478                    self.tcx.require_lang_item(hir::LangItem::DerefMut, Some(span)),
2479                    self.misc(span),
2480                );
2481            }
2482        }
2483    }
2484
2485    // Precondition: Pat is Ref(inner)
2486    fn check_pat_ref(
2487        &self,
2488        pat: &'tcx Pat<'tcx>,
2489        inner: &'tcx Pat<'tcx>,
2490        pat_mutbl: Mutability,
2491        mut expected: Ty<'tcx>,
2492        mut pat_info: PatInfo<'tcx>,
2493    ) -> Ty<'tcx> {
2494        let tcx = self.tcx;
2495
2496        let pat_prefix_span =
2497            inner.span.find_ancestor_inside(pat.span).map(|end| pat.span.until(end));
2498
2499        let ref_pat_matches_mut_ref = self.ref_pat_matches_mut_ref();
2500        if ref_pat_matches_mut_ref && pat_mutbl == Mutability::Not {
2501            // If `&` patterns can match against mutable reference types (RFC 3627, Rule 5), we need
2502            // to prevent subpatterns from binding with `ref mut`. Subpatterns of a shared reference
2503            // pattern should have read-only access to the scrutinee, and the borrow checker won't
2504            // catch it in this case.
2505            pat_info.max_ref_mutbl = pat_info.max_ref_mutbl.cap_to_weakly_not(pat_prefix_span);
2506        }
2507
2508        expected = self.try_structurally_resolve_type(pat.span, expected);
2509        // Determine whether we're consuming an inherited reference and resetting the default
2510        // binding mode, based on edition and enabled experimental features.
2511        if let ByRef::Yes(inh_mut) = pat_info.binding_mode {
2512            match self.ref_pat_matches_inherited_ref(pat.span.edition()) {
2513                InheritedRefMatchRule::EatOuter => {
2514                    // ref pattern attempts to consume inherited reference
2515                    if pat_mutbl > inh_mut {
2516                        // Tried to match inherited `ref` with `&mut`
2517                        // NB: This assumes that `&` patterns can match against mutable references
2518                        // (RFC 3627, Rule 5). If we implement a pattern typing ruleset with Rule 4E
2519                        // but not Rule 5, we'll need to check that here.
2520                        debug_assert!(ref_pat_matches_mut_ref);
2521                        self.error_inherited_ref_mutability_mismatch(pat, pat_prefix_span);
2522                    }
2523
2524                    pat_info.binding_mode = ByRef::No;
2525                    self.typeck_results.borrow_mut().skipped_ref_pats_mut().insert(pat.hir_id);
2526                    self.check_pat(inner, expected, pat_info);
2527                    return expected;
2528                }
2529                InheritedRefMatchRule::EatInner => {
2530                    if let ty::Ref(_, _, r_mutbl) = *expected.kind()
2531                        && pat_mutbl <= r_mutbl
2532                    {
2533                        // Match against the reference type; don't consume the inherited ref.
2534                        // NB: The check for compatible pattern and ref type mutability assumes that
2535                        // `&` patterns can match against mutable references (RFC 3627, Rule 5). If
2536                        // we implement a pattern typing ruleset with Rule 4 (including the fallback
2537                        // to matching the inherited ref when the inner ref can't match) but not
2538                        // Rule 5, we'll need to check that here.
2539                        debug_assert!(ref_pat_matches_mut_ref);
2540                        // NB: For RFC 3627's Rule 3, we limit the default binding mode's ref
2541                        // mutability to `pat_info.max_ref_mutbl`. If we implement a pattern typing
2542                        // ruleset with Rule 4 but not Rule 3, we'll need to check that here.
2543                        debug_assert!(self.downgrade_mut_inside_shared());
2544                        let mutbl_cap = cmp::min(r_mutbl, pat_info.max_ref_mutbl.as_mutbl());
2545                        pat_info.binding_mode = pat_info.binding_mode.cap_ref_mutability(mutbl_cap);
2546                    } else {
2547                        // The reference pattern can't match against the expected type, so try
2548                        // matching against the inherited ref instead.
2549                        if pat_mutbl > inh_mut {
2550                            // We can't match an inherited shared reference with `&mut`.
2551                            // NB: This assumes that `&` patterns can match against mutable
2552                            // references (RFC 3627, Rule 5). If we implement a pattern typing
2553                            // ruleset with Rule 4 but not Rule 5, we'll need to check that here.
2554                            // FIXME(ref_pat_eat_one_layer_2024_structural): If we already tried
2555                            // matching the real reference, the error message should explain that
2556                            // falling back to the inherited reference didn't work. This should be
2557                            // the same error as the old-Edition version below.
2558                            debug_assert!(ref_pat_matches_mut_ref);
2559                            self.error_inherited_ref_mutability_mismatch(pat, pat_prefix_span);
2560                        }
2561
2562                        pat_info.binding_mode = ByRef::No;
2563                        self.typeck_results.borrow_mut().skipped_ref_pats_mut().insert(pat.hir_id);
2564                        self.check_pat(inner, expected, pat_info);
2565                        return expected;
2566                    }
2567                }
2568                InheritedRefMatchRule::EatBoth { consider_inherited_ref: true } => {
2569                    // Reset binding mode on old editions
2570                    pat_info.binding_mode = ByRef::No;
2571
2572                    if let ty::Ref(_, inner_ty, _) = *expected.kind() {
2573                        // Consume both the inherited and inner references.
2574                        if pat_mutbl.is_mut() && inh_mut.is_mut() {
2575                            // As a special case, a `&mut` reference pattern will be able to match
2576                            // against a reference type of any mutability if the inherited ref is
2577                            // mutable. Since this allows us to match against a shared reference
2578                            // type, we refer to this as "falling back" to matching the inherited
2579                            // reference, though we consume the real reference as well. We handle
2580                            // this here to avoid adding this case to the common logic below.
2581                            self.check_pat(inner, inner_ty, pat_info);
2582                            return expected;
2583                        } else {
2584                            // Otherwise, use the common logic below for matching the inner
2585                            // reference type.
2586                            // FIXME(ref_pat_eat_one_layer_2024_structural): If this results in a
2587                            // mutability mismatch, the error message should explain that falling
2588                            // back to the inherited reference didn't work. This should be the same
2589                            // error as the Edition 2024 version above.
2590                        }
2591                    } else {
2592                        // The expected type isn't a reference type, so only match against the
2593                        // inherited reference.
2594                        if pat_mutbl > inh_mut {
2595                            // We can't match a lone inherited shared reference with `&mut`.
2596                            self.error_inherited_ref_mutability_mismatch(pat, pat_prefix_span);
2597                        }
2598
2599                        self.typeck_results.borrow_mut().skipped_ref_pats_mut().insert(pat.hir_id);
2600                        self.check_pat(inner, expected, pat_info);
2601                        return expected;
2602                    }
2603                }
2604                InheritedRefMatchRule::EatBoth { consider_inherited_ref: false } => {
2605                    // Reset binding mode on stable Rust. This will be a type error below if
2606                    // `expected` is not a reference type.
2607                    pat_info.binding_mode = ByRef::No;
2608                    self.add_rust_2024_migration_desugared_pat(
2609                        pat_info.top_info.hir_id,
2610                        pat,
2611                        match pat_mutbl {
2612                            Mutability::Not => '&', // last char of `&`
2613                            Mutability::Mut => 't', // last char of `&mut`
2614                        },
2615                        inh_mut,
2616                    )
2617                }
2618            }
2619        }
2620
2621        let (ref_ty, inner_ty) = match self.check_dereferenceable(pat.span, expected, inner) {
2622            Ok(()) => {
2623                // `demand::subtype` would be good enough, but using `eqtype` turns
2624                // out to be equally general. See (note_1) for details.
2625
2626                // Take region, inner-type from expected type if we can,
2627                // to avoid creating needless variables. This also helps with
2628                // the bad interactions of the given hack detailed in (note_1).
2629                debug!("check_pat_ref: expected={:?}", expected);
2630                match *expected.kind() {
2631                    ty::Ref(_, r_ty, r_mutbl)
2632                        if (ref_pat_matches_mut_ref && r_mutbl >= pat_mutbl)
2633                            || r_mutbl == pat_mutbl =>
2634                    {
2635                        if r_mutbl == Mutability::Not {
2636                            pat_info.max_ref_mutbl = MutblCap::Not;
2637                        }
2638
2639                        (expected, r_ty)
2640                    }
2641
2642                    _ => {
2643                        let inner_ty = self.next_ty_var(inner.span);
2644                        let ref_ty = self.new_ref_ty(pat.span, pat_mutbl, inner_ty);
2645                        debug!("check_pat_ref: demanding {:?} = {:?}", expected, ref_ty);
2646                        let err = self.demand_eqtype_pat_diag(
2647                            pat.span,
2648                            expected,
2649                            ref_ty,
2650                            &pat_info.top_info,
2651                        );
2652
2653                        // Look for a case like `fn foo(&foo: u32)` and suggest
2654                        // `fn foo(foo: &u32)`
2655                        if let Err(mut err) = err {
2656                            self.borrow_pat_suggestion(&mut err, pat);
2657                            err.emit();
2658                        }
2659                        (ref_ty, inner_ty)
2660                    }
2661                }
2662            }
2663            Err(guar) => {
2664                let err = Ty::new_error(tcx, guar);
2665                (err, err)
2666            }
2667        };
2668
2669        self.check_pat(inner, inner_ty, pat_info);
2670        ref_ty
2671    }
2672
2673    /// Create a reference type with a fresh region variable.
2674    fn new_ref_ty(&self, span: Span, mutbl: Mutability, ty: Ty<'tcx>) -> Ty<'tcx> {
2675        let region = self.next_region_var(infer::PatternRegion(span));
2676        Ty::new_ref(self.tcx, region, ty, mutbl)
2677    }
2678
2679    fn error_inherited_ref_mutability_mismatch(
2680        &self,
2681        pat: &'tcx Pat<'tcx>,
2682        pat_prefix_span: Option<Span>,
2683    ) -> ErrorGuaranteed {
2684        let err_msg = "mismatched types";
2685        let err = if let Some(span) = pat_prefix_span {
2686            let mut err = self.dcx().struct_span_err(span, err_msg);
2687            err.code(E0308);
2688            err.note("cannot match inherited `&` with `&mut` pattern");
2689            err.span_suggestion_verbose(
2690                span,
2691                "replace this `&mut` pattern with `&`",
2692                "&",
2693                Applicability::MachineApplicable,
2694            );
2695            err
2696        } else {
2697            self.dcx().struct_span_err(pat.span, err_msg)
2698        };
2699        err.emit()
2700    }
2701
2702    fn try_resolve_slice_ty_to_array_ty(
2703        &self,
2704        before: &'tcx [Pat<'tcx>],
2705        slice: Option<&'tcx Pat<'tcx>>,
2706        span: Span,
2707    ) -> Option<Ty<'tcx>> {
2708        if slice.is_some() {
2709            return None;
2710        }
2711
2712        let tcx = self.tcx;
2713        let len = before.len();
2714        let inner_ty = self.next_ty_var(span);
2715
2716        Some(Ty::new_array(tcx, inner_ty, len.try_into().unwrap()))
2717    }
2718
2719    /// Used to determines whether we can infer the expected type in the slice pattern to be of type array.
2720    /// This is only possible if we're in an irrefutable pattern. If we were to allow this in refutable
2721    /// patterns we wouldn't e.g. report ambiguity in the following situation:
2722    ///
2723    /// ```ignore(rust)
2724    /// struct Zeroes;
2725    ///    const ARR: [usize; 2] = [0; 2];
2726    ///    const ARR2: [usize; 2] = [2; 2];
2727    ///
2728    ///    impl Into<&'static [usize; 2]> for Zeroes {
2729    ///        fn into(self) -> &'static [usize; 2] {
2730    ///            &ARR
2731    ///        }
2732    ///    }
2733    ///
2734    ///    impl Into<&'static [usize]> for Zeroes {
2735    ///        fn into(self) -> &'static [usize] {
2736    ///            &ARR2
2737    ///        }
2738    ///    }
2739    ///
2740    ///    fn main() {
2741    ///        let &[a, b]: &[usize] = Zeroes.into() else {
2742    ///           ..
2743    ///        };
2744    ///    }
2745    /// ```
2746    ///
2747    /// If we're in an irrefutable pattern we prefer the array impl candidate given that
2748    /// the slice impl candidate would be rejected anyway (if no ambiguity existed).
2749    fn pat_is_irrefutable(&self, decl_origin: Option<DeclOrigin<'_>>) -> bool {
2750        match decl_origin {
2751            Some(DeclOrigin::LocalDecl { els: None }) => true,
2752            Some(DeclOrigin::LocalDecl { els: Some(_) } | DeclOrigin::LetExpr) | None => false,
2753        }
2754    }
2755
2756    /// Type check a slice pattern.
2757    ///
2758    /// Syntactically, these look like `[pat_0, ..., pat_n]`.
2759    /// Semantically, we are type checking a pattern with structure:
2760    /// ```ignore (not-rust)
2761    /// [before_0, ..., before_n, (slice, after_0, ... after_n)?]
2762    /// ```
2763    /// The type of `slice`, if it is present, depends on the `expected` type.
2764    /// If `slice` is missing, then so is `after_i`.
2765    /// If `slice` is present, it can still represent 0 elements.
2766    fn check_pat_slice(
2767        &self,
2768        span: Span,
2769        before: &'tcx [Pat<'tcx>],
2770        slice: Option<&'tcx Pat<'tcx>>,
2771        after: &'tcx [Pat<'tcx>],
2772        expected: Ty<'tcx>,
2773        pat_info: PatInfo<'tcx>,
2774    ) -> Ty<'tcx> {
2775        let expected = self.try_structurally_resolve_type(span, expected);
2776
2777        // If the pattern is irrefutable and `expected` is an infer ty, we try to equate it
2778        // to an array if the given pattern allows it. See issue #76342
2779        if self.pat_is_irrefutable(pat_info.decl_origin) && expected.is_ty_var() {
2780            if let Some(resolved_arr_ty) =
2781                self.try_resolve_slice_ty_to_array_ty(before, slice, span)
2782            {
2783                debug!(?resolved_arr_ty);
2784                let _ = self.demand_eqtype(span, expected, resolved_arr_ty);
2785            }
2786        }
2787
2788        let expected = self.structurally_resolve_type(span, expected);
2789        debug!(?expected);
2790
2791        let (element_ty, opt_slice_ty, inferred) = match *expected.kind() {
2792            // An array, so we might have something like `let [a, b, c] = [0, 1, 2];`.
2793            ty::Array(element_ty, len) => {
2794                let min = before.len() as u64 + after.len() as u64;
2795                let (opt_slice_ty, expected) =
2796                    self.check_array_pat_len(span, element_ty, expected, slice, len, min);
2797                // `opt_slice_ty.is_none()` => `slice.is_none()`.
2798                // Note, though, that opt_slice_ty could be `Some(error_ty)`.
2799                assert!(opt_slice_ty.is_some() || slice.is_none());
2800                (element_ty, opt_slice_ty, expected)
2801            }
2802            ty::Slice(element_ty) => (element_ty, Some(expected), expected),
2803            // The expected type must be an array or slice, but was neither, so error.
2804            _ => {
2805                let guar = expected.error_reported().err().unwrap_or_else(|| {
2806                    self.error_expected_array_or_slice(span, expected, pat_info)
2807                });
2808                let err = Ty::new_error(self.tcx, guar);
2809                (err, Some(err), err)
2810            }
2811        };
2812
2813        // Type check all the patterns before `slice`.
2814        for elt in before {
2815            self.check_pat(elt, element_ty, pat_info);
2816        }
2817        // Type check the `slice`, if present, against its expected type.
2818        if let Some(slice) = slice {
2819            self.check_pat(slice, opt_slice_ty.unwrap(), pat_info);
2820        }
2821        // Type check the elements after `slice`, if present.
2822        for elt in after {
2823            self.check_pat(elt, element_ty, pat_info);
2824        }
2825        inferred
2826    }
2827
2828    /// Type check the length of an array pattern.
2829    ///
2830    /// Returns both the type of the variable length pattern (or `None`), and the potentially
2831    /// inferred array type. We only return `None` for the slice type if `slice.is_none()`.
2832    fn check_array_pat_len(
2833        &self,
2834        span: Span,
2835        element_ty: Ty<'tcx>,
2836        arr_ty: Ty<'tcx>,
2837        slice: Option<&'tcx Pat<'tcx>>,
2838        len: ty::Const<'tcx>,
2839        min_len: u64,
2840    ) -> (Option<Ty<'tcx>>, Ty<'tcx>) {
2841        let len = self.try_structurally_resolve_const(span, len).try_to_target_usize(self.tcx);
2842
2843        let guar = if let Some(len) = len {
2844            // Now we know the length...
2845            if slice.is_none() {
2846                // ...and since there is no variable-length pattern,
2847                // we require an exact match between the number of elements
2848                // in the array pattern and as provided by the matched type.
2849                if min_len == len {
2850                    return (None, arr_ty);
2851                }
2852
2853                self.error_scrutinee_inconsistent_length(span, min_len, len)
2854            } else if let Some(pat_len) = len.checked_sub(min_len) {
2855                // The variable-length pattern was there,
2856                // so it has an array type with the remaining elements left as its size...
2857                return (Some(Ty::new_array(self.tcx, element_ty, pat_len)), arr_ty);
2858            } else {
2859                // ...however, in this case, there were no remaining elements.
2860                // That is, the slice pattern requires more than the array type offers.
2861                self.error_scrutinee_with_rest_inconsistent_length(span, min_len, len)
2862            }
2863        } else if slice.is_none() {
2864            // We have a pattern with a fixed length,
2865            // which we can use to infer the length of the array.
2866            let updated_arr_ty = Ty::new_array(self.tcx, element_ty, min_len);
2867            self.demand_eqtype(span, updated_arr_ty, arr_ty);
2868            return (None, updated_arr_ty);
2869        } else {
2870            // We have a variable-length pattern and don't know the array length.
2871            // This happens if we have e.g.,
2872            // `let [a, b, ..] = arr` where `arr: [T; N]` where `const N: usize`.
2873            self.error_scrutinee_unfixed_length(span)
2874        };
2875
2876        // If we get here, we must have emitted an error.
2877        (Some(Ty::new_error(self.tcx, guar)), arr_ty)
2878    }
2879
2880    fn error_scrutinee_inconsistent_length(
2881        &self,
2882        span: Span,
2883        min_len: u64,
2884        size: u64,
2885    ) -> ErrorGuaranteed {
2886        struct_span_code_err!(
2887            self.dcx(),
2888            span,
2889            E0527,
2890            "pattern requires {} element{} but array has {}",
2891            min_len,
2892            pluralize!(min_len),
2893            size,
2894        )
2895        .with_span_label(span, format!("expected {} element{}", size, pluralize!(size)))
2896        .emit()
2897    }
2898
2899    fn error_scrutinee_with_rest_inconsistent_length(
2900        &self,
2901        span: Span,
2902        min_len: u64,
2903        size: u64,
2904    ) -> ErrorGuaranteed {
2905        struct_span_code_err!(
2906            self.dcx(),
2907            span,
2908            E0528,
2909            "pattern requires at least {} element{} but array has {}",
2910            min_len,
2911            pluralize!(min_len),
2912            size,
2913        )
2914        .with_span_label(
2915            span,
2916            format!("pattern cannot match array of {} element{}", size, pluralize!(size),),
2917        )
2918        .emit()
2919    }
2920
2921    fn error_scrutinee_unfixed_length(&self, span: Span) -> ErrorGuaranteed {
2922        struct_span_code_err!(
2923            self.dcx(),
2924            span,
2925            E0730,
2926            "cannot pattern-match on an array without a fixed length",
2927        )
2928        .emit()
2929    }
2930
2931    fn error_expected_array_or_slice(
2932        &self,
2933        span: Span,
2934        expected_ty: Ty<'tcx>,
2935        pat_info: PatInfo<'tcx>,
2936    ) -> ErrorGuaranteed {
2937        let PatInfo { top_info: ti, current_depth, .. } = pat_info;
2938
2939        let mut slice_pat_semantics = false;
2940        let mut as_deref = None;
2941        let mut slicing = None;
2942        if let ty::Ref(_, ty, _) = expected_ty.kind()
2943            && let ty::Array(..) | ty::Slice(..) = ty.kind()
2944        {
2945            slice_pat_semantics = true;
2946        } else if self
2947            .autoderef(span, expected_ty)
2948            .silence_errors()
2949            .any(|(ty, _)| matches!(ty.kind(), ty::Slice(..) | ty::Array(..)))
2950            && let Some(span) = ti.span
2951            && let Some(_) = ti.origin_expr
2952        {
2953            let resolved_ty = self.resolve_vars_if_possible(ti.expected);
2954            let (is_slice_or_array_or_vector, resolved_ty) =
2955                self.is_slice_or_array_or_vector(resolved_ty);
2956            match resolved_ty.kind() {
2957                ty::Adt(adt_def, _)
2958                    if self.tcx.is_diagnostic_item(sym::Option, adt_def.did())
2959                        || self.tcx.is_diagnostic_item(sym::Result, adt_def.did()) =>
2960                {
2961                    // Slicing won't work here, but `.as_deref()` might (issue #91328).
2962                    as_deref = Some(errors::AsDerefSuggestion { span: span.shrink_to_hi() });
2963                }
2964                _ => (),
2965            }
2966
2967            let is_top_level = current_depth <= 1;
2968            if is_slice_or_array_or_vector && is_top_level {
2969                slicing = Some(errors::SlicingSuggestion { span: span.shrink_to_hi() });
2970            }
2971        }
2972        self.dcx().emit_err(errors::ExpectedArrayOrSlice {
2973            span,
2974            ty: expected_ty,
2975            slice_pat_semantics,
2976            as_deref,
2977            slicing,
2978        })
2979    }
2980
2981    fn is_slice_or_array_or_vector(&self, ty: Ty<'tcx>) -> (bool, Ty<'tcx>) {
2982        match ty.kind() {
2983            ty::Adt(adt_def, _) if self.tcx.is_diagnostic_item(sym::Vec, adt_def.did()) => {
2984                (true, ty)
2985            }
2986            ty::Ref(_, ty, _) => self.is_slice_or_array_or_vector(*ty),
2987            ty::Slice(..) | ty::Array(..) => (true, ty),
2988            _ => (false, ty),
2989        }
2990    }
2991
2992    /// Record a pattern that's invalid under Rust 2024 match ergonomics, along with a problematic
2993    /// span, so that the pattern migration lint can desugar it during THIR construction.
2994    fn add_rust_2024_migration_desugared_pat(
2995        &self,
2996        pat_id: HirId,
2997        subpat: &'tcx Pat<'tcx>,
2998        final_char: char,
2999        def_br_mutbl: Mutability,
3000    ) {
3001        // Try to trim the span we're labeling to just the `&` or binding mode that's an issue.
3002        let from_expansion = subpat.span.from_expansion();
3003        let trimmed_span = if from_expansion {
3004            // If the subpattern is from an expansion, highlight the whole macro call instead.
3005            subpat.span
3006        } else {
3007            let trimmed = self.tcx.sess.source_map().span_through_char(subpat.span, final_char);
3008            // The edition of the trimmed span should be the same as `subpat.span`; this will be a
3009            // a hard error if the subpattern is of edition >= 2024. We set it manually to be sure:
3010            trimmed.with_ctxt(subpat.span.ctxt())
3011        };
3012
3013        let mut typeck_results = self.typeck_results.borrow_mut();
3014        let mut table = typeck_results.rust_2024_migration_desugared_pats_mut();
3015        // FIXME(ref_pat_eat_one_layer_2024): The migration diagnostic doesn't know how to track the
3016        // default binding mode in the presence of Rule 3 or Rule 5. As a consequence, the labels it
3017        // gives for default binding modes are wrong, as well as suggestions based on the default
3018        // binding mode. This keeps it from making those suggestions, as doing so could panic.
3019        let info = table.entry(pat_id).or_insert_with(|| ty::Rust2024IncompatiblePatInfo {
3020            primary_labels: Vec::new(),
3021            bad_modifiers: false,
3022            bad_ref_pats: false,
3023            suggest_eliding_modes: !self.tcx.features().ref_pat_eat_one_layer_2024()
3024                && !self.tcx.features().ref_pat_eat_one_layer_2024_structural(),
3025        });
3026
3027        let pat_kind = if let PatKind::Binding(user_bind_annot, _, _, _) = subpat.kind {
3028            info.bad_modifiers = true;
3029            // If the user-provided binding modifier doesn't match the default binding mode, we'll
3030            // need to suggest reference patterns, which can affect other bindings.
3031            // For simplicity, we opt to suggest making the pattern fully explicit.
3032            info.suggest_eliding_modes &=
3033                user_bind_annot == BindingMode(ByRef::Yes(def_br_mutbl), Mutability::Not);
3034            "binding modifier"
3035        } else {
3036            info.bad_ref_pats = true;
3037            // For simplicity, we don't try to suggest eliding reference patterns. Thus, we'll
3038            // suggest adding them instead, which can affect the types assigned to bindings.
3039            // As such, we opt to suggest making the pattern fully explicit.
3040            info.suggest_eliding_modes = false;
3041            "reference pattern"
3042        };
3043        // Only provide a detailed label if the problematic subpattern isn't from an expansion.
3044        // In the case that it's from a macro, we'll add a more detailed note in the emitter.
3045        let primary_label = if from_expansion {
3046            // We can't suggest eliding modifiers within expansions.
3047            info.suggest_eliding_modes = false;
3048            // NB: This wording assumes the only expansions that can produce problematic reference
3049            // patterns and bindings are macros. If a desugaring or AST pass is added that can do
3050            // so, we may want to inspect the span's source callee or macro backtrace.
3051            "occurs within macro expansion".to_owned()
3052        } else {
3053            let dbm_str = match def_br_mutbl {
3054                Mutability::Not => "ref",
3055                Mutability::Mut => "ref mut",
3056            };
3057            format!("{pat_kind} not allowed under `{dbm_str}` default binding mode")
3058        };
3059        info.primary_labels.push((trimmed_span, primary_label));
3060    }
3061}