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