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