1use std::collections::hash_map::Entry::{Occupied, Vacant};
2use std::{assert_matches, cmp};
34use rustc_abi::FieldIdx;
5use rustc_astas ast;
6use rustc_data_structures::fx::FxHashMap;
7use rustc_errors::codes::*;
8use rustc_errors::{
9Applicability, Diag, DiagCtxtHandle, Diagnostic, ErrorGuaranteed, Level, MultiSpan, pluralize,
10struct_span_code_err,
11};
12use rustc_hir::def::{CtorKind, DefKind, Res};
13use rustc_hir::def_id::DefId;
14use rustc_hir::pat_util::EnumerateAndAdjustIterator;
15use rustc_hir::{
16selfas hir, BindingMode, ByRef, ExprKind, HirId, LangItem, Mutability, Pat, PatExpr,
17PatExprKind, PatKind, expr_needs_parens,
18};
19use rustc_hir_analysis::autoderef::report_autoderef_recursion_limit_error;
20use rustc_infer::infer::RegionVariableOrigin;
21use rustc_middle::traits::PatternOriginExpr;
22use rustc_middle::ty::{self, Pinnedness, Ty, TypeVisitableExt, Unnormalized};
23use rustc_middle::{bug, span_bug};
24use rustc_session::errors::feature_err;
25use rustc_session::lint::builtin::NON_EXHAUSTIVE_OMITTED_PATTERNS;
26use rustc_span::edit_distance::find_best_match_for_name;
27use rustc_span::edition::Edition;
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};
3435use super::report_unexpected_variant_res;
36use crate::expectation::Expectation;
37use crate::gather_locals::DeclOrigin;
38use crate::{FnCtxt, diagnostics};
3940const 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.
4546You can read more about trait objects in the Trait Objects section of the Reference: \
47https://doc.rust-lang.org/reference/types.html#trait-objects";
4849fn is_number(text: &str) -> bool {
50text.chars().all(|c: char| c.is_ascii_digit())
51}
5253/// 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(#[automatically_derived]
impl<'tcx> ::core::marker::Copy for TopInfo<'tcx> { }Copy, #[automatically_derived]
impl<'tcx> ::core::clone::Clone for TopInfo<'tcx> {
#[inline]
fn clone(&self) -> TopInfo<'tcx> {
let _: ::core::clone::AssertParamIsClone<Ty<'tcx>>;
let _:
::core::clone::AssertParamIsClone<Option<&'tcx hir::Expr<'tcx>>>;
let _: ::core::clone::AssertParamIsClone<Option<Span>>;
let _: ::core::clone::AssertParamIsClone<HirId>;
*self
}
}Clone)]
57struct TopInfo<'tcx> {
58/// The `expected` type at the top level of type checking a pattern.
59expected: 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.
63origin_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 /// ```
86span: Option<Span>,
87/// The [`HirId`] of the top-level pattern.
88hir_id: HirId,
89}
9091#[derive(#[automatically_derived]
impl<'tcx> ::core::marker::Copy for PatInfo<'tcx> { }Copy, #[automatically_derived]
impl<'tcx> ::core::clone::Clone for PatInfo<'tcx> {
#[inline]
fn clone(&self) -> PatInfo<'tcx> {
let _: ::core::clone::AssertParamIsClone<ByRef>;
let _: ::core::clone::AssertParamIsClone<PinnednessCap>;
let _: ::core::clone::AssertParamIsClone<MutblCap>;
let _: ::core::clone::AssertParamIsClone<TopInfo<'tcx>>;
let _: ::core::clone::AssertParamIsClone<Option<DeclOrigin<'tcx>>>;
let _: ::core::clone::AssertParamIsClone<u32>;
*self
}
}Clone)]
92struct PatInfo<'tcx> {
93 binding_mode: ByRef,
94 max_pinnedness: PinnednessCap,
95 max_ref_mutbl: MutblCap,
96 top_info: TopInfo<'tcx>,
97 decl_origin: Option<DeclOrigin<'tcx>>,
9899/// The depth of current pattern
100current_depth: u32,
101}
102103impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
104fn pattern_cause(&self, ti: &TopInfo<'tcx>, cause_span: Span) -> ObligationCause<'tcx> {
105// If origin_expr exists, then expected represents the type of origin_expr.
106 // If span also exists, then span == origin_expr.span (although it doesn't need to exist).
107 // In that case, we can peel away references from both and treat them
108 // as the same.
109let origin_expr_info = ti.origin_expr.map(|mut cur_expr| {
110let mut count = 0;
111112// cur_ty may have more layers of references than cur_expr.
113 // We can only make suggestions about cur_expr, however, so we'll
114 // use that as our condition for stopping.
115while let ExprKind::AddrOf(.., inner) = &cur_expr.kind {
116 cur_expr = inner;
117 count += 1;
118 }
119120PatternOriginExpr {
121 peeled_span: cur_expr.span,
122 peeled_count: count,
123 peeled_prefix_suggestion_parentheses: expr_needs_parens(cur_expr),
124 }
125 });
126127let code = ObligationCauseCode::Pattern {
128 span: ti.span,
129 root_ty: ti.expected,
130 origin_expr: origin_expr_info,
131 };
132self.cause(cause_span, code)
133 }
134135fn demand_eqtype_pat_diag(
136&'a self,
137 cause_span: Span,
138 expected: Ty<'tcx>,
139 actual: Ty<'tcx>,
140 ti: &TopInfo<'tcx>,
141 ) -> Result<(), Diag<'a>> {
142self.demand_eqtype_with_origin(&self.pattern_cause(ti, cause_span), expected, actual)
143 .map_err(|mut diag| {
144if let Some(expr) = ti.origin_expr {
145self.suggest_fn_call(&mut diag, expr, expected, |output| {
146self.can_eq(self.param_env, output, actual)
147 });
148 }
149diag150 })
151 }
152153fn demand_eqtype_pat(
154&self,
155 cause_span: Span,
156 expected: Ty<'tcx>,
157 actual: Ty<'tcx>,
158 ti: &TopInfo<'tcx>,
159 ) -> Result<(), ErrorGuaranteed> {
160self.demand_eqtype_pat_diag(cause_span, expected, actual, ti).map_err(|err| err.emit())
161 }
162}
163164/// Mode for adjusting the expected type and binding mode.
165#[derive(#[automatically_derived]
impl ::core::clone::Clone for AdjustMode {
#[inline]
fn clone(&self) -> AdjustMode {
let _: ::core::clone::AssertParamIsClone<PeelKind>;
*self
}
}Clone, #[automatically_derived]
impl ::core::marker::Copy for AdjustMode { }Copy, #[automatically_derived]
impl ::core::fmt::Debug for AdjustMode {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
AdjustMode::Peel { kind: __self_0 } =>
::core::fmt::Formatter::debug_struct_field1_finish(f, "Peel",
"kind", &__self_0),
AdjustMode::Pass => ::core::fmt::Formatter::write_str(f, "Pass"),
}
}
}Debug, #[automatically_derived]
impl ::core::cmp::PartialEq for AdjustMode {
#[inline]
fn eq(&self, other: &AdjustMode) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr &&
match (self, other) {
(AdjustMode::Peel { kind: __self_0 }, AdjustMode::Peel {
kind: __arg1_0 }) => __self_0 == __arg1_0,
_ => true,
}
}
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for AdjustMode {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<PeelKind>;
}
}Eq)]
166enum AdjustMode {
167/// Peel off all immediate reference types. If the `deref_patterns` feature is enabled, this
168 /// also peels smart pointer ADTs.
169Peel { kind: PeelKind },
170/// Pass on the input binding mode and expected type.
171Pass,
172}
173174/// Restrictions on what types to peel when adjusting the expected type and binding mode.
175#[derive(#[automatically_derived]
impl ::core::clone::Clone for PeelKind {
#[inline]
fn clone(&self) -> PeelKind {
let _: ::core::clone::AssertParamIsClone<Option<DefId>>;
let _: ::core::clone::AssertParamIsClone<usize>;
*self
}
}Clone, #[automatically_derived]
impl ::core::marker::Copy for PeelKind { }Copy, #[automatically_derived]
impl ::core::fmt::Debug for PeelKind {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
PeelKind::ExplicitDerefPat =>
::core::fmt::Formatter::write_str(f, "ExplicitDerefPat"),
PeelKind::Implicit { until_adt: __self_0, pat_ref_layers: __self_1
} =>
::core::fmt::Formatter::debug_struct_field2_finish(f,
"Implicit", "until_adt", __self_0, "pat_ref_layers",
&__self_1),
}
}
}Debug, #[automatically_derived]
impl ::core::cmp::PartialEq for PeelKind {
#[inline]
fn eq(&self, other: &PeelKind) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr &&
match (self, other) {
(PeelKind::Implicit {
until_adt: __self_0, pat_ref_layers: __self_1 },
PeelKind::Implicit {
until_adt: __arg1_0, pat_ref_layers: __arg1_1 }) =>
__self_0 == __arg1_0 && __self_1 == __arg1_1,
_ => true,
}
}
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for PeelKind {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<Option<DefId>>;
let _: ::core::cmp::AssertParamIsEq<usize>;
}
}Eq)]
176enum PeelKind {
177/// Only peel reference types. This is used for explicit `deref!(_)` patterns, which dereference
178 /// any number of `&`/`&mut` references, plus a single smart pointer.
179ExplicitDerefPat,
180/// Implicitly peel references, and if `deref_patterns` is enabled, smart pointer ADTs.
181Implicit {
182/// The ADT the pattern is a constructor for, if applicable, so that we don't peel it. See
183 /// [`ResolvedPat`] for more information.
184until_adt: Option<DefId>,
185/// The number of references at the head of the pattern's type, so we can leave that many
186 /// untouched. This is `1` for string literals, and `0` for most patterns.
187pat_ref_layers: usize,
188 },
189}
190191impl AdjustMode {
192const fn peel_until_adt(opt_adt_def: Option<DefId>) -> AdjustMode {
193 AdjustMode::Peel { kind: PeelKind::Implicit { until_adt: opt_adt_def, pat_ref_layers: 0 } }
194 }
195const fn peel_all() -> AdjustMode {
196AdjustMode::peel_until_adt(None)
197 }
198}
199200/// `ref mut` bindings (explicit or match-ergonomics) are not allowed behind an `&` reference.
201/// Normally, the borrow checker enforces this, but for (currently experimental) match ergonomics,
202/// we track this when typing patterns for two purposes:
203///
204/// - For RFC 3627's Rule 3, when this would prevent us from binding with `ref mut`, we limit the
205/// default binding mode to be by shared `ref` when it would otherwise be `ref mut`.
206///
207/// - For RFC 3627's Rule 5, we allow `&` patterns to match against `&mut` references, treating them
208/// as if they were shared references. Since the scrutinee is mutable in this case, the borrow
209/// checker won't catch if we bind with `ref mut`, so we need to throw an error ourselves.
210#[derive(#[automatically_derived]
impl ::core::clone::Clone for MutblCap {
#[inline]
fn clone(&self) -> MutblCap {
let _: ::core::clone::AssertParamIsClone<Option<Span>>;
*self
}
}Clone, #[automatically_derived]
impl ::core::marker::Copy for MutblCap { }Copy, #[automatically_derived]
impl ::core::fmt::Debug for MutblCap {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
MutblCap::Not => ::core::fmt::Formatter::write_str(f, "Not"),
MutblCap::WeaklyNot(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"WeaklyNot", &__self_0),
MutblCap::Mut => ::core::fmt::Formatter::write_str(f, "Mut"),
}
}
}Debug, #[automatically_derived]
impl ::core::cmp::PartialEq for MutblCap {
#[inline]
fn eq(&self, other: &MutblCap) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr &&
match (self, other) {
(MutblCap::WeaklyNot(__self_0), MutblCap::WeaklyNot(__arg1_0))
=> __self_0 == __arg1_0,
_ => true,
}
}
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for MutblCap {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<Option<Span>>;
}
}Eq)]
211enum MutblCap {
212/// Mutability restricted to immutable.
213Not,
214215/// Mutability restricted to immutable, but only because of the pattern
216 /// (not the scrutinee type).
217 ///
218 /// The contained span, if present, points to an `&` pattern
219 /// that is the reason for the restriction,
220 /// and which will be reported in a diagnostic.
221WeaklyNot(Option<Span>),
222223/// No restriction on mutability
224Mut,
225}
226227impl MutblCap {
228#[must_use]
229fn cap_to_weakly_not(self, span: Option<Span>) -> Self {
230match self {
231 MutblCap::Not => MutblCap::Not,
232_ => MutblCap::WeaklyNot(span),
233 }
234 }
235236#[must_use]
237fn as_mutbl(self) -> Mutability {
238match self {
239 MutblCap::Not | MutblCap::WeaklyNot(_) => Mutability::Not,
240 MutblCap::Mut => Mutability::Mut,
241 }
242 }
243}
244245/// `ref` or `ref mut` bindings (not pinned, explicitly or match-ergonomics) are only allowed behind
246/// an `&pin` reference if the binding's type is `Unpin`.
247///
248/// Normally, the borrow checker enforces this (not implemented yet), but we track it here for better
249/// diagnostics.
250#[derive(#[automatically_derived]
impl ::core::clone::Clone for PinnednessCap {
#[inline]
fn clone(&self) -> PinnednessCap { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for PinnednessCap { }Copy, #[automatically_derived]
impl ::core::fmt::Debug for PinnednessCap {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f,
match self {
PinnednessCap::Not => "Not",
PinnednessCap::Pinned => "Pinned",
})
}
}Debug, #[automatically_derived]
impl ::core::cmp::PartialEq for PinnednessCap {
#[inline]
fn eq(&self, other: &PinnednessCap) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr
}
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for PinnednessCap {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {}
}Eq)]
251enum PinnednessCap {
252/// No restriction on pinnedness.
253Not,
254/// Pinnedness restricted to pinned.
255Pinned,
256}
257258/// Variations on RFC 3627's Rule 4: when do reference patterns match against inherited references?
259///
260/// "Inherited reference" designates the `&`/`&mut` types that arise from using match ergonomics, i.e.
261/// from matching a reference type with a non-reference pattern. E.g. when `Some(x)` matches on
262/// `&mut Option<&T>`, `x` gets type `&mut &T` and the outer `&mut` is considered "inherited".
263#[derive(#[automatically_derived]
impl ::core::clone::Clone for InheritedRefMatchRule {
#[inline]
fn clone(&self) -> InheritedRefMatchRule {
let _: ::core::clone::AssertParamIsClone<bool>;
*self
}
}Clone, #[automatically_derived]
impl ::core::marker::Copy for InheritedRefMatchRule { }Copy, #[automatically_derived]
impl ::core::fmt::Debug for InheritedRefMatchRule {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
InheritedRefMatchRule::EatOuter =>
::core::fmt::Formatter::write_str(f, "EatOuter"),
InheritedRefMatchRule::EatInner =>
::core::fmt::Formatter::write_str(f, "EatInner"),
InheritedRefMatchRule::EatBoth { consider_inherited_ref: __self_0
} =>
::core::fmt::Formatter::debug_struct_field1_finish(f,
"EatBoth", "consider_inherited_ref", &__self_0),
}
}
}Debug, #[automatically_derived]
impl ::core::cmp::PartialEq for InheritedRefMatchRule {
#[inline]
fn eq(&self, other: &InheritedRefMatchRule) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr &&
match (self, other) {
(InheritedRefMatchRule::EatBoth {
consider_inherited_ref: __self_0 },
InheritedRefMatchRule::EatBoth {
consider_inherited_ref: __arg1_0 }) => __self_0 == __arg1_0,
_ => true,
}
}
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for InheritedRefMatchRule {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<bool>;
}
}Eq)]
264enum InheritedRefMatchRule {
265/// Reference patterns consume only the inherited reference if possible, regardless of whether
266 /// the underlying type being matched against is a reference type. If there is no inherited
267 /// reference, a reference will be consumed from the underlying type.
268EatOuter,
269/// Reference patterns consume only a reference from the underlying type if possible. If the
270 /// underlying type is not a reference type, the inherited reference will be consumed.
271EatInner,
272/// When the underlying type is a reference type, reference patterns consume both layers of
273 /// reference, i.e. they both reset the binding mode and consume the reference type.
274EatBoth {
275/// If `true`, an inherited reference will be considered when determining whether a reference
276 /// pattern matches a given type:
277 /// - If the underlying type is not a reference, a reference pattern may eat the inherited reference;
278 /// - If the underlying type is a reference, a reference pattern matches if it can eat either one
279 /// of the underlying and inherited references. E.g. a `&mut` pattern is allowed if either the
280 /// underlying type is `&mut` or the inherited reference is `&mut`.
281 ///
282 /// If `false`, a reference pattern is only matched against the underlying type.
283 /// This is `false` for stable Rust and `true` for both the `ref_pat_eat_one_layer_2024` and
284 /// `ref_pat_eat_one_layer_2024_structural` feature gates.
285consider_inherited_ref: bool,
286 },
287}
288289/// When checking patterns containing paths, we need to know the path's resolution to determine
290/// whether to apply match ergonomics and implicitly dereference the scrutinee. For instance, when
291/// the `deref_patterns` feature is enabled and we're matching against a scrutinee of type
292/// `Cow<'a, Option<u8>>`, we insert an implicit dereference to allow the pattern `Some(_)` to type,
293/// but we must not dereference it when checking the pattern `Cow::Borrowed(_)`.
294///
295/// `ResolvedPat` contains the information from resolution needed to determine match ergonomics
296/// adjustments, and to finish checking the pattern once we know its adjusted type.
297#[derive(#[automatically_derived]
impl<'tcx> ::core::clone::Clone for ResolvedPat<'tcx> {
#[inline]
fn clone(&self) -> ResolvedPat<'tcx> {
let _: ::core::clone::AssertParamIsClone<Ty<'tcx>>;
let _: ::core::clone::AssertParamIsClone<ResolvedPatKind<'tcx>>;
*self
}
}Clone, #[automatically_derived]
impl<'tcx> ::core::marker::Copy for ResolvedPat<'tcx> { }Copy, #[automatically_derived]
impl<'tcx> ::core::fmt::Debug for ResolvedPat<'tcx> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field2_finish(f, "ResolvedPat",
"ty", &self.ty, "kind", &&self.kind)
}
}Debug)]
298struct ResolvedPat<'tcx> {
299/// The type of the pattern, to be checked against the type of the scrutinee after peeling. This
300 /// is also used to avoid peeling the scrutinee's constructors (see the `Cow` example above).
301ty: Ty<'tcx>,
302 kind: ResolvedPatKind<'tcx>,
303}
304305#[derive(#[automatically_derived]
impl<'tcx> ::core::clone::Clone for ResolvedPatKind<'tcx> {
#[inline]
fn clone(&self) -> ResolvedPatKind<'tcx> {
let _: ::core::clone::AssertParamIsClone<Res>;
let _:
::core::clone::AssertParamIsClone<&'tcx [hir::PathSegment<'tcx>]>;
let _: ::core::clone::AssertParamIsClone<&'tcx VariantDef>;
let _: ::core::clone::AssertParamIsClone<&'tcx VariantDef>;
*self
}
}Clone, #[automatically_derived]
impl<'tcx> ::core::marker::Copy for ResolvedPatKind<'tcx> { }Copy, #[automatically_derived]
impl<'tcx> ::core::fmt::Debug for ResolvedPatKind<'tcx> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
ResolvedPatKind::Path {
res: __self_0, pat_res: __self_1, segments: __self_2 } =>
::core::fmt::Formatter::debug_struct_field3_finish(f, "Path",
"res", __self_0, "pat_res", __self_1, "segments",
&__self_2),
ResolvedPatKind::Struct { variant: __self_0 } =>
::core::fmt::Formatter::debug_struct_field1_finish(f,
"Struct", "variant", &__self_0),
ResolvedPatKind::TupleStruct { res: __self_0, variant: __self_1 }
=>
::core::fmt::Formatter::debug_struct_field2_finish(f,
"TupleStruct", "res", __self_0, "variant", &__self_1),
}
}
}Debug)]
306enum ResolvedPatKind<'tcx> {
307 Path { res: Res, pat_res: Res, segments: &'tcx [hir::PathSegment<'tcx>] },
308 Struct { variant: &'tcx VariantDef },
309 TupleStruct { res: Res, variant: &'tcx VariantDef },
310}
311312impl<'tcx> ResolvedPat<'tcx> {
313fn adjust_mode(&self) -> AdjustMode {
314if let ResolvedPatKind::Path { res, .. } = self.kind
315 && #[allow(non_exhaustive_omitted_patterns)] match res {
Res::Def(DefKind::Const { .. } | DefKind::AssocConst { .. }, _) => true,
_ => false,
}matches!(res, Res::Def(DefKind::Const { .. } | DefKind::AssocConst { .. }, _))316 {
317// These constants can be of a reference type, e.g. `const X: &u8 = &0;`.
318 // Peeling the reference types too early will cause type checking failures.
319 // Although it would be possible to *also* peel the types of the constants too.
320AdjustMode::Pass321 } else {
322// The remaining possible resolutions for path, struct, and tuple struct patterns are
323 // ADT constructors. As such, we may peel references freely, but we must not peel the
324 // ADT itself from the scrutinee if it's a smart pointer.
325AdjustMode::peel_until_adt(self.ty.ty_adt_def().map(|adt| adt.did()))
326 }
327 }
328}
329330impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
331/// Experimental pattern feature: after matching against a shared reference, do we limit the
332 /// default binding mode in subpatterns to be `ref` when it would otherwise be `ref mut`?
333 /// This corresponds to Rule 3 of RFC 3627.
334fn downgrade_mut_inside_shared(&self) -> bool {
335// NB: RFC 3627 proposes stabilizing Rule 3 in all editions. If we adopt the same behavior
336 // across all editions, this may be removed.
337self.tcx.features().ref_pat_eat_one_layer_2024_structural()
338 }
339340/// Experimental pattern feature: when do reference patterns match against inherited references?
341 /// This corresponds to variations on Rule 4 of RFC 3627.
342fn ref_pat_matches_inherited_ref(&self, edition: Edition) -> InheritedRefMatchRule {
343// NB: The particular rule used here is likely to differ across editions, so calls to this
344 // may need to become edition checks after match ergonomics stabilize.
345if edition.at_least_rust_2024() {
346if self.tcx.features().ref_pat_eat_one_layer_2024() {
347 InheritedRefMatchRule::EatOuter348 } else if self.tcx.features().ref_pat_eat_one_layer_2024_structural() {
349 InheritedRefMatchRule::EatInner350 } else {
351// Currently, matching against an inherited ref on edition 2024 is an error.
352 // Use `EatBoth` as a fallback to be similar to stable Rust.
353InheritedRefMatchRule::EatBoth { consider_inherited_ref: false }
354 }
355 } else {
356 InheritedRefMatchRule::EatBoth {
357 consider_inherited_ref: self.tcx.features().ref_pat_eat_one_layer_2024()
358 || self.tcx.features().ref_pat_eat_one_layer_2024_structural(),
359 }
360 }
361 }
362363/// Experimental pattern feature: do `&` patterns match against `&mut` references, treating them
364 /// as if they were shared references? This corresponds to Rule 5 of RFC 3627.
365fn ref_pat_matches_mut_ref(&self) -> bool {
366// NB: RFC 3627 proposes stabilizing Rule 5 in all editions. If we adopt the same behavior
367 // across all editions, this may be removed.
368self.tcx.features().ref_pat_eat_one_layer_2024()
369 || self.tcx.features().ref_pat_eat_one_layer_2024_structural()
370 }
371372/// Type check the given top level pattern against the `expected` type.
373 ///
374 /// If a `Some(span)` is provided and `origin_expr` holds,
375 /// then the `span` represents the scrutinee's span.
376 /// The scrutinee is found in e.g. `match scrutinee { ... }` and `let pat = scrutinee;`.
377 ///
378 /// Otherwise, `Some(span)` represents the span of a type expression
379 /// which originated the `expected` type.
380pub(crate) fn check_pat_top(
381&self,
382 pat: &'tcx Pat<'tcx>,
383 expected: Ty<'tcx>,
384 span: Option<Span>,
385 origin_expr: Option<&'tcx hir::Expr<'tcx>>,
386 decl_origin: Option<DeclOrigin<'tcx>>,
387 ) {
388let top_info = TopInfo { expected, origin_expr, span, hir_id: pat.hir_id };
389let pat_info = PatInfo {
390 binding_mode: ByRef::No,
391 max_pinnedness: PinnednessCap::Not,
392 max_ref_mutbl: MutblCap::Mut,
393top_info,
394decl_origin,
395 current_depth: 0,
396 };
397self.check_pat(pat, expected, pat_info);
398 }
399400/// Type check the given `pat` against the `expected` type
401 /// with the provided `binding_mode` (default binding mode).
402 ///
403 /// Outside of this module, `check_pat_top` should always be used.
404 /// Conversely, inside this module, `check_pat_top` should never be used.
405#[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("check_pat",
"rustc_hir_typeck::pat", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/pat.rs"),
::tracing_core::__macro_support::Option::Some(405u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::pat"),
::tracing_core::field::FieldSet::new(&["pat", "expected"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = meta.fields().iter();
meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&pat)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&expected)
as &dyn Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: () = loop {};
return __tracing_attr_fake_return;
}
{
let opt_path_res =
match pat.kind {
PatKind::Expr(PatExpr {
kind: PatExprKind::Path(qpath), hir_id, span }) => {
Some(self.resolve_pat_path(*hir_id, *span, qpath))
}
PatKind::Struct(ref qpath, ..) =>
Some(self.resolve_pat_struct(pat, qpath)),
PatKind::TupleStruct(ref qpath, ..) =>
Some(self.resolve_pat_tuple_struct(pat, qpath)),
_ => None,
};
let adjust_mode = self.calc_adjust_mode(pat, opt_path_res);
let ty =
self.check_pat_inner(pat, opt_path_res, adjust_mode, expected,
pat_info);
self.write_ty(pat.hir_id, ty);
if let Some(derefed_tys) =
self.typeck_results.borrow().pat_adjustments().get(pat.hir_id)
&&
derefed_tys.iter().any(|adjust|
adjust.kind == PatAdjust::OverloadedDeref) {
self.register_deref_mut_bounds_if_needed(pat.span, pat,
derefed_tys.iter().filter_map(|adjust|
match adjust.kind {
PatAdjust::OverloadedDeref => Some(adjust.source),
PatAdjust::BuiltinDeref | PatAdjust::PinDeref => None,
}));
}
}
}
}#[instrument(level = "debug", skip(self, pat_info))]406fn check_pat(&self, pat: &'tcx Pat<'tcx>, expected: Ty<'tcx>, pat_info: PatInfo<'tcx>) {
407// For patterns containing paths, we need the path's resolution to determine whether to
408 // implicitly dereference the scrutinee before matching.
409let opt_path_res = match pat.kind {
410 PatKind::Expr(PatExpr { kind: PatExprKind::Path(qpath), hir_id, span }) => {
411Some(self.resolve_pat_path(*hir_id, *span, qpath))
412 }
413 PatKind::Struct(ref qpath, ..) => Some(self.resolve_pat_struct(pat, qpath)),
414 PatKind::TupleStruct(ref qpath, ..) => Some(self.resolve_pat_tuple_struct(pat, qpath)),
415_ => None,
416 };
417let adjust_mode = self.calc_adjust_mode(pat, opt_path_res);
418let ty = self.check_pat_inner(pat, opt_path_res, adjust_mode, expected, pat_info);
419self.write_ty(pat.hir_id, ty);
420421// If we implicitly inserted overloaded dereferences before matching check the pattern to
422 // see if the dereferenced types need `DerefMut` bounds.
423if let Some(derefed_tys) = self.typeck_results.borrow().pat_adjustments().get(pat.hir_id)
424 && derefed_tys.iter().any(|adjust| adjust.kind == PatAdjust::OverloadedDeref)
425 {
426self.register_deref_mut_bounds_if_needed(
427 pat.span,
428 pat,
429 derefed_tys.iter().filter_map(|adjust| match adjust.kind {
430 PatAdjust::OverloadedDeref => Some(adjust.source),
431 PatAdjust::BuiltinDeref | PatAdjust::PinDeref => None,
432 }),
433 );
434 }
435436// (note_1): In most of the cases where (note_1) is referenced
437 // (literals and constants being the exception), we relate types
438 // using strict equality, even though subtyping would be sufficient.
439 // There are a few reasons for this, some of which are fairly subtle
440 // and which cost me (nmatsakis) an hour or two debugging to remember,
441 // so I thought I'd write them down this time.
442 //
443 // 1. There is no loss of expressiveness here, though it does
444 // cause some inconvenience. What we are saying is that the type
445 // of `x` becomes *exactly* what is expected. This can cause unnecessary
446 // errors in some cases, such as this one:
447 //
448 // ```
449 // fn foo<'x>(x: &'x i32) {
450 // let a = 1;
451 // let mut z = x;
452 // z = &a;
453 // }
454 // ```
455 //
456 // The reason we might get an error is that `z` might be
457 // assigned a type like `&'x i32`, and then we would have
458 // a problem when we try to assign `&a` to `z`, because
459 // the lifetime of `&a` (i.e., the enclosing block) is
460 // shorter than `'x`.
461 //
462 // HOWEVER, this code works fine. The reason is that the
463 // expected type here is whatever type the user wrote, not
464 // the initializer's type. In this case the user wrote
465 // nothing, so we are going to create a type variable `Z`.
466 // Then we will assign the type of the initializer (`&'x i32`)
467 // as a subtype of `Z`: `&'x i32 <: Z`. And hence we
468 // will instantiate `Z` as a type `&'0 i32` where `'0` is
469 // a fresh region variable, with the constraint that `'x : '0`.
470 // So basically we're all set.
471 //
472 // Note that there are two tests to check that this remains true
473 // (`regions-reassign-{match,let}-bound-pointer.rs`).
474 //
475 // 2. An outdated issue related to the old HIR borrowck. See the test
476 // `regions-relate-bound-regions-on-closures-to-inference-variables.rs`,
477}
478479// Helper to avoid resolving the same path pattern several times.
480fn check_pat_inner(
481&self,
482 pat: &'tcx Pat<'tcx>,
483 opt_path_res: Option<Result<ResolvedPat<'tcx>, ErrorGuaranteed>>,
484 adjust_mode: AdjustMode,
485 expected: Ty<'tcx>,
486 pat_info: PatInfo<'tcx>,
487 ) -> Ty<'tcx> {
488#[cfg(debug_assertions)]
489if #[allow(non_exhaustive_omitted_patterns)] match pat_info.binding_mode {
ByRef::Yes(_, Mutability::Mut) => true,
_ => false,
}matches!(pat_info.binding_mode, ByRef::Yes(_, Mutability::Mut))490 && pat_info.max_ref_mutbl != MutblCap::Mut491 && self.downgrade_mut_inside_shared()
492 {
493::rustc_middle::util::bug::span_bug_fmt(pat.span,
format_args!("Pattern mutability cap violated!"));span_bug!(pat.span, "Pattern mutability cap violated!");
494 }
495496// Resolve type if needed.
497let expected = if let AdjustMode::Peel { .. } = adjust_mode498 && pat.default_binding_modes
499 {
500self.resolve_vars_with_obligations(expected)
501 } else {
502expected503 };
504let old_pat_info = pat_info;
505let pat_info = PatInfo { current_depth: old_pat_info.current_depth + 1, ..old_pat_info };
506507match pat.kind {
508// Peel off a `&` or `&mut`from the scrutinee type. See the examples in
509 // `tests/ui/rfcs/rfc-2005-default-binding-mode`.
510_ if let AdjustMode::Peel { kind: peel_kind } = adjust_mode511 && pat.default_binding_modes
512 && let &ty::Ref(_, inner_ty, inner_mutability) = expected.kind()
513 && self.should_peel_ref(peel_kind, expected) =>
514 {
515{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/pat.rs:515",
"rustc_hir_typeck::pat", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/pat.rs"),
::tracing_core::__macro_support::Option::Some(515u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::pat"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("inspecting {0:?}",
expected) as &dyn Value))])
});
} else { ; }
};debug!("inspecting {:?}", expected);
516517{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/pat.rs:517",
"rustc_hir_typeck::pat", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/pat.rs"),
::tracing_core::__macro_support::Option::Some(517u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::pat"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("current discriminant is Ref, inserting implicit deref")
as &dyn Value))])
});
} else { ; }
};debug!("current discriminant is Ref, inserting implicit deref");
518// Preserve the reference type. We'll need it later during THIR lowering.
519self.typeck_results
520 .borrow_mut()
521 .pat_adjustments_mut()
522 .entry(pat.hir_id)
523 .or_default()
524 .push(PatAdjustment { kind: PatAdjust::BuiltinDeref, source: expected });
525526// Use the old pat info to keep `current_depth` to its old value.
527let new_pat_info =
528self.adjust_pat_info(Pinnedness::Not, inner_mutability, old_pat_info);
529530// Recurse with the new expected type.
531self.check_pat_inner(pat, opt_path_res, adjust_mode, inner_ty, new_pat_info)
532 }
533// If `pin_ergonomics` is enabled, peel the `&pin` from the pinned reference type. See the
534 // examples in `tests/ui/async-await/pin-ergonomics/`.
535_ if self.tcx.features().pin_ergonomics()
536 && let AdjustMode::Peel { kind: peel_kind } = adjust_mode537 && pat.default_binding_modes
538 && self.should_peel_smart_pointer(peel_kind, expected)
539 && let Some(pinned_ty) = expected.pinned_ty()
540// Currently, only pinned reference is specially handled, leaving other
541 // pinned types (e.g. `Pin<Box<T>>` to deref patterns) handled as a
542 // deref pattern.
543&& let &ty::Ref(_, inner_ty, inner_mutability) = pinned_ty.kind() =>
544 {
545{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/pat.rs:545",
"rustc_hir_typeck::pat", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/pat.rs"),
::tracing_core::__macro_support::Option::Some(545u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::pat"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("scrutinee ty {0:?} is a pinned reference, inserting pin deref",
expected) as &dyn Value))])
});
} else { ; }
};debug!("scrutinee ty {expected:?} is a pinned reference, inserting pin deref");
546547// Use the old pat info to keep `current_depth` to its old value.
548let new_pat_info =
549self.adjust_pat_info(Pinnedness::Pinned, inner_mutability, old_pat_info);
550551self.check_deref_pattern(
552pat,
553opt_path_res,
554adjust_mode,
555expected,
556inner_ty,
557 PatAdjust::PinDeref,
558new_pat_info,
559 )
560 }
561// If `deref_patterns` is enabled, peel a smart pointer from the scrutinee type. See the
562 // examples in `tests/ui/pattern/deref_patterns/`.
563_ if self.tcx.features().deref_patterns()
564 && let AdjustMode::Peel { kind: peel_kind } = adjust_mode565 && pat.default_binding_modes
566 && self.should_peel_smart_pointer(peel_kind, expected) =>
567 {
568{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/pat.rs:568",
"rustc_hir_typeck::pat", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/pat.rs"),
::tracing_core::__macro_support::Option::Some(568u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::pat"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("scrutinee ty {0:?} is a smart pointer, inserting pin deref",
expected) as &dyn Value))])
});
} else { ; }
};debug!("scrutinee ty {expected:?} is a smart pointer, inserting pin deref");
569570// The scrutinee is a smart pointer; implicitly dereference it. This adds a
571 // requirement that `expected: DerefPure`.
572let inner_ty = self.deref_pat_target(pat.span, expected);
573// Once we've checked `pat`, we'll add a `DerefMut` bound if it contains any
574 // `ref mut` bindings. See `Self::register_deref_mut_bounds_if_needed`.
575576self.check_deref_pattern(
577pat,
578opt_path_res,
579adjust_mode,
580expected,
581inner_ty,
582 PatAdjust::OverloadedDeref,
583old_pat_info,
584 )
585 }
586 PatKind::Missing | PatKind::Wild | PatKind::Err(_) => expected,
587// We allow any type here; we ensure that the type is uninhabited during match checking.
588PatKind::Never => expected,
589 PatKind::Expr(PatExpr { kind: PatExprKind::Path(_), hir_id, .. }) => {
590let ty = match opt_path_res.unwrap() {
591Ok(ref pr) => {
592self.check_pat_path(pat.hir_id, pat.span, pr, expected, &pat_info.top_info)
593 }
594Err(guar) => Ty::new_error(self.tcx, guar),
595 };
596self.write_ty(*hir_id, ty);
597ty598 }
599 PatKind::Expr(expr @ PatExpr { kind: PatExprKind::Lit { lit, .. }, .. }) => {
600self.check_pat_lit(pat.span, expr, &lit.node, expected, &pat_info.top_info)
601 }
602 PatKind::Range(lhs, rhs, _) => {
603self.check_pat_range(pat.span, lhs, rhs, expected, &pat_info.top_info)
604 }
605 PatKind::Binding(ba, var_id, ident, sub) => {
606self.check_pat_ident(pat, ba, var_id, ident, sub, expected, pat_info)
607 }
608 PatKind::TupleStruct(ref qpath, subpats, ddpos) => match opt_path_res.unwrap() {
609Ok(ResolvedPat { ty, kind: ResolvedPatKind::TupleStruct { res, variant } }) => self610 .check_pat_tuple_struct(
611pat, qpath, subpats, ddpos, res, ty, variant, expected, pat_info,
612 ),
613Err(guar) => {
614let ty_err = Ty::new_error(self.tcx, guar);
615for subpat in subpats {
616self.check_pat(subpat, ty_err, pat_info);
617 }
618ty_err619 }
620Ok(pr) => ::rustc_middle::util::bug::span_bug_fmt(pat.span,
format_args!("tuple struct pattern resolved to {0:?}", pr))span_bug!(pat.span, "tuple struct pattern resolved to {pr:?}"),
621 },
622 PatKind::Struct(_, fields, has_rest_pat) => match opt_path_res.unwrap() {
623Ok(ResolvedPat { ty, kind: ResolvedPatKind::Struct { variant } }) => self624 .check_pat_struct(
625pat,
626fields,
627has_rest_pat.is_some(),
628ty,
629variant,
630expected,
631pat_info,
632 ),
633Err(guar) => {
634let ty_err = Ty::new_error(self.tcx, guar);
635for field in fields {
636self.check_pat(field.pat, ty_err, pat_info);
637 }
638ty_err639 }
640Ok(pr) => ::rustc_middle::util::bug::span_bug_fmt(pat.span,
format_args!("struct pattern resolved to {0:?}", pr))span_bug!(pat.span, "struct pattern resolved to {pr:?}"),
641 },
642 PatKind::Guard(pat, cond) => {
643self.check_pat(pat, expected, pat_info);
644self.check_expr_has_type_or_error(cond, self.tcx.types.bool, |_| {});
645expected646 }
647 PatKind::Or(pats) => {
648for pat in pats {
649self.check_pat(pat, expected, pat_info);
650 }
651expected652 }
653 PatKind::Tuple(elements, ddpos) => {
654self.check_pat_tuple(pat.span, elements, ddpos, expected, pat_info)
655 }
656 PatKind::Box(inner) => self.check_pat_box(pat.span, inner, expected, pat_info),
657 PatKind::Deref(inner) => self.check_pat_deref(pat.span, inner, expected, pat_info),
658 PatKind::Ref(inner, pinned, mutbl) => {
659self.check_pat_ref(pat, inner, pinned, mutbl, expected, pat_info)
660 }
661 PatKind::Slice(before, slice, after) => {
662self.check_pat_slice(pat.span, before, slice, after, expected, pat_info)
663 }
664 }
665 }
666667fn adjust_pat_info(
668&self,
669 inner_pinnedness: Pinnedness,
670 inner_mutability: Mutability,
671 pat_info: PatInfo<'tcx>,
672 ) -> PatInfo<'tcx> {
673let mut binding_mode = match pat_info.binding_mode {
674// If default binding mode is by value, make it `ref`, `ref mut`, `ref pin const`
675 // or `ref pin mut` (depending on whether we observe `&`, `&mut`, `&pin const` or
676 // `&pin mut`).
677ByRef::No => ByRef::Yes(inner_pinnedness, inner_mutability),
678 ByRef::Yes(pinnedness, mutability) => {
679let pinnedness = match pinnedness {
680// When `ref`, stay a `ref` (on `&`) or downgrade to `ref pin` (on `&pin`).
681Pinnedness::Not => inner_pinnedness,
682// When `ref pin`, stay a `ref pin`.
683 // This is because we cannot get an `&mut T` from `&mut &pin mut T` unless `T: Unpin`.
684 // Note that `&T` and `&mut T` are `Unpin`, which implies
685 // `& &pin const T` <-> `&pin const &T` and `&mut &pin mut T` <-> `&pin mut &mut T`
686 // (i.e. mutually coercible).
687Pinnedness::Pinned => Pinnedness::Pinned,
688 };
689690let mutability = match mutability {
691// When `ref mut`, stay a `ref mut` (on `&mut`) or downgrade to `ref` (on `&`).
692Mutability::Mut => inner_mutability,
693// Once a `ref`, always a `ref`.
694 // This is because a `& &mut` cannot mutate the underlying value.
695Mutability::Not => Mutability::Not,
696 };
697 ByRef::Yes(pinnedness, mutability)
698 }
699 };
700701let PatInfo { mut max_ref_mutbl, mut max_pinnedness, .. } = pat_info;
702if self.downgrade_mut_inside_shared() {
703binding_mode = binding_mode.cap_ref_mutability(max_ref_mutbl.as_mutbl());
704 }
705match binding_mode {
706 ByRef::Yes(_, Mutability::Not) => max_ref_mutbl = MutblCap::Not,
707 ByRef::Yes(Pinnedness::Pinned, _) => max_pinnedness = PinnednessCap::Pinned,
708_ => {}
709 }
710{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/pat.rs:710",
"rustc_hir_typeck::pat", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/pat.rs"),
::tracing_core::__macro_support::Option::Some(710u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::pat"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("default binding mode is now {0:?}",
binding_mode) as &dyn Value))])
});
} else { ; }
};debug!("default binding mode is now {:?}", binding_mode);
711PatInfo { binding_mode, max_pinnedness, max_ref_mutbl, ..pat_info }
712 }
713714fn check_deref_pattern(
715&self,
716 pat: &'tcx Pat<'tcx>,
717 opt_path_res: Option<Result<ResolvedPat<'tcx>, ErrorGuaranteed>>,
718 adjust_mode: AdjustMode,
719 expected: Ty<'tcx>,
720mut inner_ty: Ty<'tcx>,
721 pat_adjust_kind: PatAdjust,
722 pat_info: PatInfo<'tcx>,
723 ) -> Ty<'tcx> {
724if true {
if !!#[allow(non_exhaustive_omitted_patterns)] match pat_adjust_kind {
PatAdjust::BuiltinDeref => true,
_ => false,
} {
{
::core::panicking::panic_fmt(format_args!("unexpected deref pattern for builtin reference type {0:?}",
expected));
}
};
};debug_assert!(
725 !matches!(pat_adjust_kind, PatAdjust::BuiltinDeref),
726"unexpected deref pattern for builtin reference type {expected:?}",
727 );
728729let mut typeck_results = self.typeck_results.borrow_mut();
730let mut pat_adjustments_table = typeck_results.pat_adjustments_mut();
731let pat_adjustments = pat_adjustments_table.entry(pat.hir_id).or_default();
732// We may reach the recursion limit if a user matches on a type `T` satisfying
733 // `T: Deref<Target = T>`; error gracefully in this case.
734 // FIXME(deref_patterns): If `deref_patterns` stabilizes, it may make sense to move
735 // this check out of this branch. Alternatively, this loop could be implemented with
736 // autoderef and this check removed. For now though, don't break code compiling on
737 // stable with lots of `&`s and a low recursion limit, if anyone's done that.
738if self.tcx.recursion_limit().value_within_limit(pat_adjustments.len()) {
739// Preserve the smart pointer type for THIR lowering and closure upvar analysis.
740pat_adjustments.push(PatAdjustment { kind: pat_adjust_kind, source: expected });
741 } else {
742let guar = report_autoderef_recursion_limit_error(self.tcx, pat.span, expected);
743inner_ty = Ty::new_error(self.tcx, guar);
744 }
745drop(typeck_results);
746747// Recurse, using the old pat info to keep `current_depth` to its old value.
748 // Peeling smart pointers does not update the default binding mode.
749self.check_pat_inner(pat, opt_path_res, adjust_mode, inner_ty, pat_info)
750 }
751752/// How should the binding mode and expected type be adjusted?
753 ///
754 /// When the pattern contains a path, `opt_path_res` must be `Some(path_res)`.
755fn calc_adjust_mode(
756&self,
757 pat: &'tcx Pat<'tcx>,
758 opt_path_res: Option<Result<ResolvedPat<'tcx>, ErrorGuaranteed>>,
759 ) -> AdjustMode {
760match &pat.kind {
761// Type checking these product-like types successfully always require
762 // that the expected type be of those types and not reference types.
763PatKind::Tuple(..) | PatKind::Range(..) | PatKind::Slice(..) => AdjustMode::peel_all(),
764// When checking an explicit deref pattern, only peel reference types.
765 // FIXME(deref_patterns): If box patterns and deref patterns need to coexist, box
766 // patterns may want `PeelKind::Implicit`, stopping on encountering a box.
767PatKind::Box(_) | PatKind::Deref(_) => {
768 AdjustMode::Peel { kind: PeelKind::ExplicitDerefPat }
769 }
770// A never pattern behaves somewhat like a literal or unit variant.
771PatKind::Never => AdjustMode::peel_all(),
772// For patterns with paths, how we peel the scrutinee depends on the path's resolution.
773PatKind::Struct(..)
774 | PatKind::TupleStruct(..)
775 | PatKind::Expr(PatExpr { kind: PatExprKind::Path(_), .. }) => {
776// If there was an error resolving the path, default to peeling everything.
777opt_path_res.unwrap().map_or(AdjustMode::peel_all(), |pr| pr.adjust_mode())
778 }
779780// String and byte-string literals result in types `&str` and `&[u8]` respectively.
781 // All other literals result in non-reference types.
782 // As a result, we allow `if let 0 = &&0 {}` but not `if let "foo" = &&"foo" {}` unless
783 // `deref_patterns` is enabled.
784PatKind::Expr(lt) => {
785// Path patterns have already been handled, and inline const blocks currently
786 // aren't possible to write, so any handling for them would be untested.
787if truecfg!(debug_assertions)788 && self.tcx.features().deref_patterns()
789 && !#[allow(non_exhaustive_omitted_patterns)] match lt.kind {
PatExprKind::Lit { .. } => true,
_ => false,
}matches!(lt.kind, PatExprKind::Lit { .. })790 {
791::rustc_middle::util::bug::span_bug_fmt(lt.span,
format_args!("FIXME(deref_patterns): adjust mode unimplemented for {0:?}",
lt.kind));span_bug!(
792 lt.span,
793"FIXME(deref_patterns): adjust mode unimplemented for {:?}",
794 lt.kind
795 );
796 }
797// Call `resolve_vars_if_possible` here for inline const blocks.
798let lit_ty = self.resolve_vars_if_possible(self.check_pat_expr_unadjusted(lt));
799// If `deref_patterns` is enabled, allow `if let "foo" = &&"foo" {}`.
800if self.tcx.features().deref_patterns() {
801let mut peeled_ty = lit_ty;
802let mut pat_ref_layers = 0;
803while let ty::Ref(_, inner_ty, mutbl) =
804*self.resolve_vars_with_obligations(peeled_ty).kind()
805 {
806// We rely on references at the head of constants being immutable.
807if true {
if !mutbl.is_not() {
::core::panicking::panic("assertion failed: mutbl.is_not()")
};
};debug_assert!(mutbl.is_not());
808 pat_ref_layers += 1;
809 peeled_ty = inner_ty;
810 }
811 AdjustMode::Peel {
812 kind: PeelKind::Implicit { until_adt: None, pat_ref_layers },
813 }
814 } else {
815if lit_ty.is_ref() { AdjustMode::Pass } else { AdjustMode::peel_all() }
816 }
817 }
818819// Ref patterns are complicated, we handle them in `check_pat_ref`.
820PatKind::Ref(..)
821// No need to do anything on a missing pattern.
822| PatKind::Missing823// A `_` pattern works with any expected type, so there's no need to do anything.
824| PatKind::Wild825// A malformed pattern doesn't have an expected type, so let's just accept any type.
826| PatKind::Err(_)
827// Bindings also work with whatever the expected type is,
828 // and moreover if we peel references off, that will give us the wrong binding type.
829 // Also, we can have a subpattern `binding @ pat`.
830 // Each side of the `@` should be treated independently (like with OR-patterns).
831| PatKind::Binding(..)
832// An OR-pattern just propagates to each individual alternative.
833 // This is maximally flexible, allowing e.g., `Some(mut x) | &Some(mut x)`.
834 // In that example, `Some(mut x)` results in `Peel` whereas `&Some(mut x)` in `Reset`.
835| PatKind::Or(_)
836// Like or-patterns, guard patterns just propagate to their subpatterns.
837| PatKind::Guard(..) => AdjustMode::Pass,
838 }
839 }
840841/// Assuming `expected` is a reference type, determine whether to peel it before matching.
842fn should_peel_ref(&self, peel_kind: PeelKind, mut expected: Ty<'tcx>) -> bool {
843if true {
if !expected.is_ref() {
::core::panicking::panic("assertion failed: expected.is_ref()")
};
};debug_assert!(expected.is_ref());
844let pat_ref_layers = match peel_kind {
845 PeelKind::ExplicitDerefPat => 0,
846 PeelKind::Implicit { pat_ref_layers, .. } => pat_ref_layers,
847 };
848849// Most patterns don't have reference types, so we'll want to peel all references from the
850 // scrutinee before matching. To optimize for the common case, return early.
851if pat_ref_layers == 0 {
852return true;
853 }
854if true {
if !self.tcx.features().deref_patterns() {
{
::core::panicking::panic_fmt(format_args!("Peeling for patterns with reference types is gated by `deref_patterns`."));
}
};
};debug_assert!(
855self.tcx.features().deref_patterns(),
856"Peeling for patterns with reference types is gated by `deref_patterns`."
857);
858859// If the pattern has as many or more layers of reference as the expected type, we can match
860 // without peeling more, unless we find a smart pointer or `&mut` that we also need to peel.
861 // We don't treat `&` and `&mut` as interchangeable, but by peeling `&mut`s before matching,
862 // we can still, e.g., match on a `&mut str` with a string literal pattern. This is because
863 // string literal patterns may be used where `str` is expected.
864let mut expected_ref_layers = 0;
865while let ty::Ref(_, inner_ty, mutbl) = *expected.kind() {
866if mutbl.is_mut() {
867// Mutable references can't be in the final value of constants, thus they can't be
868 // at the head of their types, thus we should always peel `&mut`.
869return true;
870 }
871 expected_ref_layers += 1;
872 expected = inner_ty;
873 }
874pat_ref_layers < expected_ref_layers || self.should_peel_smart_pointer(peel_kind, expected)
875 }
876877/// Determine whether `expected` is a smart pointer type that should be peeled before matching.
878fn should_peel_smart_pointer(&self, peel_kind: PeelKind, expected: Ty<'tcx>) -> bool {
879// Explicit `deref!(_)` patterns match against smart pointers; don't peel in that case.
880if let PeelKind::Implicit { until_adt, .. } = peel_kind881// For simplicity, only apply overloaded derefs if `expected` is a known ADT.
882 // FIXME(deref_patterns): we'll get better diagnostics for users trying to
883 // implicitly deref generics if we allow them here, but primitives, tuples, and
884 // inference vars definitely should be stopped. Figure out what makes most sense.
885&& let ty::Adt(scrutinee_adt, _) = *expected.kind()
886// Don't peel if the pattern type already matches the scrutinee. E.g., stop here if
887 // matching on a `Cow<'a, T>` scrutinee with a `Cow::Owned(_)` pattern.
888&& until_adt != Some(scrutinee_adt.did())
889// At this point, the pattern isn't able to match `expected` without peeling. Check
890 // that it implements `Deref` before assuming it's a smart pointer, to get a normal
891 // type error instead of a missing impl error if not. This only checks for `Deref`,
892 // not `DerefPure`: we require that too, but we want a trait error if it's missing.
893&& let Some(deref_trait) = self.tcx.lang_items().deref_trait()
894 && self.type_implements_trait(deref_trait, [expected], self.param_env).may_apply()
895 {
896true
897} else {
898false
899}
900 }
901902fn check_pat_expr_unadjusted(&self, lt: &'tcx hir::PatExpr<'tcx>) -> Ty<'tcx> {
903let ty = match <.kind {
904 rustc_hir::PatExprKind::Lit { lit, negated } => {
905let ty = self.check_expr_lit(lit, lt.hir_id, Expectation::NoExpectation);
906if *negated {
907self.register_bound(
908ty,
909self.tcx.require_lang_item(LangItem::Neg, lt.span),
910ObligationCause::dummy_with_span(lt.span),
911 );
912 }
913ty914 }
915 rustc_hir::PatExprKind::Path(qpath) => {
916let (res, opt_ty, segments) =
917self.resolve_ty_and_res_fully_qualified_call(qpath, lt.hir_id, lt.span);
918self.instantiate_value_path(segments, opt_ty, res, lt.span, lt.span, lt.hir_id).0
919}
920 };
921self.write_ty(lt.hir_id, ty);
922ty923 }
924925fn check_pat_lit(
926&self,
927 span: Span,
928 expr: &hir::PatExpr<'tcx>,
929 lit_kind: &ast::LitKind,
930 expected: Ty<'tcx>,
931 ti: &TopInfo<'tcx>,
932 ) -> Ty<'tcx> {
933{
match expr.kind {
hir::PatExprKind::Lit { .. } => {}
ref left_val => {
::core::panicking::assert_matches_failed(left_val,
"hir::PatExprKind::Lit { .. }", ::core::option::Option::None);
}
}
};assert_matches!(expr.kind, hir::PatExprKind::Lit { .. });
934935// We've already computed the type above (when checking for a non-ref pat),
936 // so avoid computing it again.
937let ty = self.node_ty(expr.hir_id);
938939// Byte string patterns behave the same way as array patterns
940 // They can denote both statically and dynamically-sized byte arrays.
941 // Additionally, when `deref_patterns` is enabled, byte string literal patterns may have
942 // types `[u8]` or `[u8; N]`, in order to type, e.g., `deref!(b"..."): Vec<u8>`.
943let mut pat_ty = ty;
944if #[allow(non_exhaustive_omitted_patterns)] match lit_kind {
ast::LitKind::ByteStr(..) => true,
_ => false,
}matches!(lit_kind, ast::LitKind::ByteStr(..)) {
945let tcx = self.tcx;
946let expected = self.structurally_resolve_type(span, expected);
947match *expected.kind() {
948// Allow `b"...": &[u8]`
949ty::Ref(_, inner_ty, _)
950if self.resolve_vars_with_obligations(inner_ty).is_slice() =>
951 {
952{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/pat.rs:952",
"rustc_hir_typeck::pat", ::tracing::Level::TRACE,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/pat.rs"),
::tracing_core::__macro_support::Option::Some(952u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::pat"),
::tracing_core::field::FieldSet::new(&["message",
"expr.hir_id.local_id"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::TRACE <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("polymorphic byte string lit")
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&expr.hir_id.local_id)
as &dyn Value))])
});
} else { ; }
};trace!(?expr.hir_id.local_id, "polymorphic byte string lit");
953pat_ty = Ty::new_imm_ref(
954tcx,
955tcx.lifetimes.re_static,
956Ty::new_slice(tcx, tcx.types.u8),
957 );
958 }
959// Allow `b"...": [u8; 3]` for `deref_patterns`
960ty::Array(..) if tcx.features().deref_patterns() => {
961pat_ty = match *ty.kind() {
962 ty::Ref(_, inner_ty, _) => inner_ty,
963_ => ::rustc_middle::util::bug::span_bug_fmt(span,
format_args!("found byte string literal with non-ref type {0:?}", ty))span_bug!(span, "found byte string literal with non-ref type {ty:?}"),
964 }
965 }
966// Allow `b"...": [u8]` for `deref_patterns`
967ty::Slice(..) if tcx.features().deref_patterns() => {
968pat_ty = Ty::new_slice(tcx, tcx.types.u8);
969 }
970// Otherwise, `b"...": &[u8; 3]`
971_ => {}
972 }
973 }
974975// When `deref_patterns` is enabled, in order to allow `deref!("..."): String`, we allow
976 // string literal patterns to have type `str`. This is accounted for when lowering to MIR.
977if self.tcx.features().deref_patterns()
978 && #[allow(non_exhaustive_omitted_patterns)] match lit_kind {
ast::LitKind::Str(..) => true,
_ => false,
}matches!(lit_kind, ast::LitKind::Str(..))979 && self.resolve_vars_with_obligations(expected).is_str()
980 {
981pat_ty = self.tcx.types.str_;
982 }
983984// Somewhat surprising: in this case, the subtyping relation goes the
985 // opposite way as the other cases. Actually what we really want is not
986 // a subtyping relation at all but rather that there exists a LUB
987 // (so that they can be compared). However, in practice, constants are
988 // always scalars or strings. For scalars subtyping is irrelevant,
989 // and for strings `ty` is type is `&'static str`, so if we say that
990 //
991 // &'static str <: expected
992 //
993 // then that's equivalent to there existing a LUB.
994let cause = self.pattern_cause(ti, span);
995if let Err(mut err) = self.demand_suptype_with_origin(&cause, expected, pat_ty) {
996// If scrutinee is String and pattern is &str, suggest .as_str()
997let expected = self.resolve_vars_with_obligations(expected);
998if let ty::Adt(adt, _) = expected.kind()
999 && self.tcx.is_lang_item(adt.did(), LangItem::String)
1000 && pat_ty.is_ref()
1001 && pat_ty.peel_refs().is_str()
1002 && let Some(origin_expr) = ti.origin_expr
1003 {
1004err.span_suggestion_verbose(
1005origin_expr.span.shrink_to_hi(),
1006"consider converting the `String` to a `&str` using `.as_str()`",
1007".as_str()",
1008 Applicability::MachineApplicable,
1009 );
1010 }
1011err.emit();
1012 }
10131014pat_ty1015 }
10161017fn check_pat_range(
1018&self,
1019 span: Span,
1020 lhs: Option<&'tcx hir::PatExpr<'tcx>>,
1021 rhs: Option<&'tcx hir::PatExpr<'tcx>>,
1022 expected: Ty<'tcx>,
1023 ti: &TopInfo<'tcx>,
1024 ) -> Ty<'tcx> {
1025let calc_side = |opt_expr: Option<&'tcx hir::PatExpr<'tcx>>| match opt_expr {
1026None => None,
1027Some(expr) => {
1028let ty = self.check_pat_expr_unadjusted(expr);
1029// Check that the end-point is possibly of numeric or char type.
1030 // The early check here is not for correctness, but rather better
1031 // diagnostics (e.g. when `&str` is being matched, `expected` will
1032 // be peeled to `str` while ty here is still `&str`, if we don't
1033 // err early here, a rather confusing unification error will be
1034 // emitted instead).
1035let ty = self.resolve_vars_with_obligations(ty);
1036let fail =
1037 !(ty.is_numeric() || ty.is_char() || ty.is_ty_var() || ty.references_error());
1038Some((fail, ty, expr.span))
1039 }
1040 };
1041let mut lhs = calc_side(lhs);
1042let mut rhs = calc_side(rhs);
10431044if let (Some((true, ..)), _) | (_, Some((true, ..))) = (lhs, rhs) {
1045// There exists a side that didn't meet our criteria that the end-point
1046 // be of a numeric or char type, as checked in `calc_side` above.
1047let guar = self.emit_err_pat_range(span, lhs, rhs);
1048return Ty::new_error(self.tcx, guar);
1049 }
10501051// Unify each side with `expected`.
1052 // Subtyping doesn't matter here, as the value is some kind of scalar.
1053let demand_eqtype = |x: &mut _, y| {
1054if let Some((ref mut fail, x_ty, x_span)) = *x1055 && let Err(mut err) = self.demand_eqtype_pat_diag(x_span, expected, x_ty, ti)
1056 {
1057if let Some((_, y_ty, y_span)) = y {
1058self.endpoint_has_type(&mut err, y_span, y_ty);
1059 }
1060err.emit();
1061*fail = true;
1062 }
1063 };
1064demand_eqtype(&mut lhs, rhs);
1065demand_eqtype(&mut rhs, lhs);
10661067if let (Some((true, ..)), _) | (_, Some((true, ..))) = (lhs, rhs) {
1068return Ty::new_misc_error(self.tcx);
1069 }
10701071// Find the unified type and check if it's of numeric or char type again.
1072 // This check is needed if both sides are inference variables.
1073 // We require types to be resolved here so that we emit inference failure
1074 // rather than "_ is not a char or numeric".
1075let ty = self.structurally_resolve_type(span, expected);
1076if !(ty.is_numeric() || ty.is_char() || ty.references_error()) {
1077if let Some((ref mut fail, _, _)) = lhs {
1078*fail = true;
1079 }
1080if let Some((ref mut fail, _, _)) = rhs {
1081*fail = true;
1082 }
1083let guar = self.emit_err_pat_range(span, lhs, rhs);
1084return Ty::new_error(self.tcx, guar);
1085 }
1086ty1087 }
10881089fn endpoint_has_type(&self, err: &mut Diag<'_>, span: Span, ty: Ty<'_>) {
1090if !ty.references_error() {
1091err.span_label(span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("this is of type `{0}`", ty))
})format!("this is of type `{ty}`"));
1092 }
1093 }
10941095fn emit_err_pat_range(
1096&self,
1097 span: Span,
1098 lhs: Option<(bool, Ty<'tcx>, Span)>,
1099 rhs: Option<(bool, Ty<'tcx>, Span)>,
1100 ) -> ErrorGuaranteed {
1101let span = match (lhs, rhs) {
1102 (Some((true, ..)), Some((true, ..))) => span,
1103 (Some((true, _, sp)), _) => sp,
1104 (_, Some((true, _, sp))) => sp,
1105_ => ::rustc_middle::util::bug::span_bug_fmt(span,
format_args!("emit_err_pat_range: no side failed or exists but still error?"))span_bug!(span, "emit_err_pat_range: no side failed or exists but still error?"),
1106 };
1107let mut err = {
self.dcx().struct_span_err(span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("only `char` and numeric types are allowed in range patterns"))
})).with_code(E0029)
}struct_span_code_err!(
1108self.dcx(),
1109 span,
1110 E0029,
1111"only `char` and numeric types are allowed in range patterns"
1112);
1113let msg = |ty| {
1114let ty = self.resolve_vars_if_possible(ty);
1115::alloc::__export::must_use({
::alloc::fmt::format(format_args!("this is of type `{0}` but it should be `char` or numeric",
ty))
})format!("this is of type `{ty}` but it should be `char` or numeric")1116 };
1117let mut one_side_err = |first_span, first_ty, second: Option<(bool, Ty<'tcx>, Span)>| {
1118err.span_label(first_span, msg(first_ty));
1119if let Some((_, ty, sp)) = second {
1120let ty = self.resolve_vars_if_possible(ty);
1121self.endpoint_has_type(&mut err, sp, ty);
1122 }
1123 };
1124match (lhs, rhs) {
1125 (Some((true, lhs_ty, lhs_sp)), Some((true, rhs_ty, rhs_sp))) => {
1126err.span_label(lhs_sp, msg(lhs_ty));
1127err.span_label(rhs_sp, msg(rhs_ty));
1128 }
1129 (Some((true, lhs_ty, lhs_sp)), rhs) => one_side_err(lhs_sp, lhs_ty, rhs),
1130 (lhs, Some((true, rhs_ty, rhs_sp))) => one_side_err(rhs_sp, rhs_ty, lhs),
1131_ => ::rustc_middle::util::bug::span_bug_fmt(span,
format_args!("Impossible, verified above."))span_bug!(span, "Impossible, verified above."),
1132 }
1133if (lhs, rhs).references_error() {
1134err.downgrade_to_delayed_bug();
1135 }
1136if self.tcx.sess.teach(err.code.unwrap()) {
1137err.note(
1138"In a match expression, only numbers and characters can be matched \
1139 against a range. This is because the compiler checks that the range \
1140 is non-empty at compile-time, and is unable to evaluate arbitrary \
1141 comparison functions. If you want to capture values of an orderable \
1142 type between two end-points, you can use a guard.",
1143 );
1144 }
1145err.emit()
1146 }
11471148fn check_pat_ident(
1149&self,
1150 pat: &'tcx Pat<'tcx>,
1151 user_bind_annot: BindingMode,
1152 var_id: HirId,
1153 ident: Ident,
1154 sub: Option<&'tcx Pat<'tcx>>,
1155 expected: Ty<'tcx>,
1156 pat_info: PatInfo<'tcx>,
1157 ) -> Ty<'tcx> {
1158let PatInfo { binding_mode: def_br, top_info: ti, .. } = pat_info;
11591160// Determine the binding mode...
1161let bm = match user_bind_annot {
1162BindingMode(ByRef::No, Mutability::Mut) if let ByRef::Yes(_, def_br_mutbl) = def_br => {
1163// Only mention the experimental `mut_ref` feature if if we're in edition 2024 and
1164 // using other experimental matching features compatible with it.
1165if pat.span.at_least_rust_2024()
1166 && (self.tcx.features().ref_pat_eat_one_layer_2024()
1167 || self.tcx.features().ref_pat_eat_one_layer_2024_structural())
1168 {
1169if !self.tcx.features().mut_ref() {
1170feature_err(
1171self.tcx.sess,
1172 sym::mut_ref,
1173pat.span.until(ident.span),
1174"binding cannot be both mutable and by-reference",
1175 )
1176 .emit();
1177 }
11781179BindingMode(def_br, Mutability::Mut)
1180 } else {
1181// `mut` resets the binding mode on edition <= 2021
1182self.add_rust_2024_migration_desugared_pat(
1183pat_info.top_info.hir_id,
1184pat,
1185't', // last char of `mut`
1186def_br_mutbl,
1187 );
1188BindingMode(ByRef::No, Mutability::Mut)
1189 }
1190 }
1191BindingMode(ByRef::No, mutbl) => BindingMode(def_br, mutbl),
1192BindingMode(ByRef::Yes(_, user_br_mutbl), _) => {
1193if let ByRef::Yes(_, def_br_mutbl) = def_br {
1194// `ref`/`ref mut` overrides the binding mode on edition <= 2021
1195self.add_rust_2024_migration_desugared_pat(
1196pat_info.top_info.hir_id,
1197pat,
1198match user_br_mutbl {
1199 Mutability::Not => 'f', // last char of `ref`
1200Mutability::Mut => 't', // last char of `ref mut`
1201},
1202def_br_mutbl,
1203 );
1204 }
1205user_bind_annot1206 }
1207 };
12081209// If there exists a pinned reference in the pattern but the binding is not pinned,
1210 // it means the binding is unpinned and thus requires an `Unpin` bound.
1211if pat_info.max_pinnedness == PinnednessCap::Pinned1212 && #[allow(non_exhaustive_omitted_patterns)] match bm.0 {
ByRef::Yes(Pinnedness::Not, _) => true,
_ => false,
}matches!(bm.0, ByRef::Yes(Pinnedness::Not, _))1213 {
1214self.register_bound(
1215expected,
1216self.tcx.require_lang_item(hir::LangItem::Unpin, pat.span),
1217self.misc(pat.span),
1218 )
1219 }
12201221if #[allow(non_exhaustive_omitted_patterns)] match bm.0 {
ByRef::Yes(_, Mutability::Mut) => true,
_ => false,
}matches!(bm.0, ByRef::Yes(_, Mutability::Mut))1222 && let MutblCap::WeaklyNot(and_pat_span) = pat_info.max_ref_mutbl
1223 {
1224let mut err = {
self.dcx().struct_span_err(ident.span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("cannot borrow as mutable inside an `&` pattern"))
})).with_code(E0596)
}struct_span_code_err!(
1225self.dcx(),
1226 ident.span,
1227 E0596,
1228"cannot borrow as mutable inside an `&` pattern"
1229);
12301231if let Some(span) = and_pat_span {
1232err.span_suggestion(
1233span,
1234"replace this `&` with `&mut`",
1235"&mut ",
1236 Applicability::MachineApplicable,
1237 );
1238 }
1239err.emit();
1240 }
12411242// ...and store it in a side table:
1243self.typeck_results.borrow_mut().pat_binding_modes_mut().insert(pat.hir_id, bm);
12441245{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/pat.rs:1245",
"rustc_hir_typeck::pat", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/pat.rs"),
::tracing_core::__macro_support::Option::Some(1245u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::pat"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("check_pat_ident: pat.hir_id={0:?} bm={1:?}",
pat.hir_id, bm) as &dyn Value))])
});
} else { ; }
};debug!("check_pat_ident: pat.hir_id={:?} bm={:?}", pat.hir_id, bm);
12461247let local_ty = self.local_ty(pat.span, pat.hir_id);
1248let eq_ty = match bm.0 {
1249 ByRef::Yes(pinnedness, mutbl) => {
1250// If the binding is like `ref x | ref mut x`,
1251 // then `x` is assigned a value of type `&M T` where M is the
1252 // mutability and T is the expected type.
1253 //
1254 // Under pin ergonomics, if the binding is like `ref pin const|mut x`,
1255 // then `x` is assigned a value of type `&pin M T` where M is the
1256 // mutability and T is the expected type.
1257 //
1258 // `x` is assigned a value of type `&M T`, hence `&M T <: typeof(x)`
1259 // is required. However, we use equality, which is stronger.
1260 // See (note_1) for an explanation.
1261self.new_ref_ty(pat.span, pinnedness, mutbl, expected)
1262 }
1263// Otherwise, the type of x is the expected type `T`.
1264ByRef::No => expected, // As above, `T <: typeof(x)` is required, but we use equality, see (note_1).
1265};
12661267// We have a concrete type for the local, so we do not need to taint it and hide follow up errors *using* the local.
1268let _ = self.demand_eqtype_pat(pat.span, eq_ty, local_ty, &ti);
12691270// If there are multiple arms, make sure they all agree on
1271 // what the type of the binding `x` ought to be.
1272if var_id != pat.hir_id {
1273self.check_binding_alt_eq_ty(user_bind_annot, pat.span, var_id, local_ty, &ti);
1274 }
12751276if let Some(p) = sub {
1277self.check_pat(p, expected, pat_info);
1278 }
12791280local_ty1281 }
12821283/// When a variable is bound several times in a `PatKind::Or`, it'll resolve all of the
1284 /// subsequent bindings of the same name to the first usage. Verify that all of these
1285 /// bindings have the same type by comparing them all against the type of that first pat.
1286fn check_binding_alt_eq_ty(
1287&self,
1288 ba: BindingMode,
1289 span: Span,
1290 var_id: HirId,
1291 ty: Ty<'tcx>,
1292 ti: &TopInfo<'tcx>,
1293 ) {
1294let var_ty = self.local_ty(span, var_id);
1295if let Err(mut err) = self.demand_eqtype_pat_diag(span, var_ty, ty, ti) {
1296let var_ty = self.resolve_vars_if_possible(var_ty);
1297let msg = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("first introduced with type `{0}` here",
var_ty))
})format!("first introduced with type `{var_ty}` here");
1298err.span_label(self.tcx.hir_span(var_id), msg);
1299let in_match = self.tcx.hir_parent_iter(var_id).any(|(_, n)| {
1300#[allow(non_exhaustive_omitted_patterns)] match n {
hir::Node::Expr(hir::Expr {
kind: hir::ExprKind::Match(.., hir::MatchSource::Normal), .. }) =>
true,
_ => false,
}matches!(
1301 n,
1302 hir::Node::Expr(hir::Expr {
1303 kind: hir::ExprKind::Match(.., hir::MatchSource::Normal),
1304 ..
1305 })
1306 )1307 });
1308let pre = if in_match { "in the same arm, " } else { "" };
1309err.note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}a binding must have the same type in all alternatives",
pre))
})format!("{pre}a binding must have the same type in all alternatives"));
1310self.suggest_adding_missing_ref_or_removing_ref(
1311&mut err,
1312span,
1313var_ty,
1314self.resolve_vars_if_possible(ty),
1315ba,
1316 );
1317err.emit();
1318 }
1319 }
13201321fn suggest_adding_missing_ref_or_removing_ref(
1322&self,
1323 err: &mut Diag<'_>,
1324 span: Span,
1325 expected: Ty<'tcx>,
1326 actual: Ty<'tcx>,
1327 ba: BindingMode,
1328 ) {
1329match (expected.kind(), actual.kind(), ba) {
1330 (ty::Ref(_, inner_ty, _), _, BindingMode::NONE)
1331if self.can_eq(self.param_env, *inner_ty, actual) =>
1332 {
1333err.span_suggestion_verbose(
1334span.shrink_to_lo(),
1335"consider adding `ref`",
1336"ref ",
1337 Applicability::MaybeIncorrect,
1338 );
1339 }
1340 (_, ty::Ref(_, inner_ty, _), BindingMode::REF)
1341if self.can_eq(self.param_env, expected, *inner_ty) =>
1342 {
1343err.span_suggestion_verbose(
1344span.with_hi(span.lo() + BytePos(4)),
1345"consider removing `ref`",
1346"",
1347 Applicability::MaybeIncorrect,
1348 );
1349 }
1350_ => (),
1351 }
1352 }
13531354/// Precondition: pat is a `Ref(_)` pattern
1355fn borrow_pat_suggestion(&self, err: &mut Diag<'_>, pat: &Pat<'_>) {
1356let tcx = self.tcx;
1357if let PatKind::Ref(inner, pinned, mutbl) = pat.kind
1358 && let PatKind::Binding(_, _, binding, ..) = inner.kind
1359 {
1360let binding_parent = tcx.parent_hir_node(pat.hir_id);
1361{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/pat.rs:1361",
"rustc_hir_typeck::pat", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/pat.rs"),
::tracing_core::__macro_support::Option::Some(1361u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::pat"),
::tracing_core::field::FieldSet::new(&["inner", "pat",
"binding_parent"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&inner) as
&dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&pat) as
&dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&binding_parent)
as &dyn Value))])
});
} else { ; }
};debug!(?inner, ?pat, ?binding_parent);
13621363let pin_and_mut = pinned.prefix_str(mutbl).trim_end();
13641365let mut_var_suggestion = 'block: {
1366if mutbl.is_not() {
1367break 'block None;
1368 }
13691370let ident_kind = match binding_parent {
1371 hir::Node::Param(_) => "parameter",
1372 hir::Node::LetStmt(_) => "variable",
1373 hir::Node::Arm(_) => "binding",
13741375// Provide diagnostics only if the parent pattern is struct-like,
1376 // i.e. where `mut binding` makes sense
1377hir::Node::Pat(Pat { kind, .. }) => match kind {
1378 PatKind::Struct(..)
1379 | PatKind::TupleStruct(..)
1380 | PatKind::Or(..)
1381 | PatKind::Guard(..)
1382 | PatKind::Tuple(..)
1383 | PatKind::Slice(..) => "binding",
13841385 PatKind::Missing1386 | PatKind::Wild1387 | PatKind::Never1388 | PatKind::Binding(..)
1389 | PatKind::Box(..)
1390 | PatKind::Deref(_)
1391 | PatKind::Ref(..)
1392 | PatKind::Expr(..)
1393 | PatKind::Range(..)
1394 | PatKind::Err(_) => break 'block None,
1395 },
13961397// Don't provide suggestions in other cases
1398_ => break 'block None,
1399 };
14001401Some((
1402pat.span,
1403::alloc::__export::must_use({
::alloc::fmt::format(format_args!("to declare a mutable {0} use",
ident_kind))
})format!("to declare a mutable {ident_kind} use"),
1404::alloc::__export::must_use({
::alloc::fmt::format(format_args!("mut {0}", binding))
})format!("mut {binding}"),
1405 ))
1406 };
14071408match binding_parent {
1409 hir::Node::Param(hir::Param { ty_span, pat, .. })
1410if pat.span != *ty_span1411 && pinned.is_pinned()
1412 && !tcx.features().pin_ergonomics() =>
1413 {
1414// FIXME(pin_ergonomics): Once `pin_ergonomics` is stabilized, remove this
1415 // gate and allow the pinned reference type-position suggestion unconditionally.
1416}
1417// Check that there is explicit type (ie this is not a closure param with inferred type)
1418 // so we don't suggest moving something to the type that does not exist
1419hir::Node::Param(hir::Param { ty_span, pat, .. }) if pat.span != *ty_span => {
1420err.multipart_suggestion(
1421::alloc::__export::must_use({
::alloc::fmt::format(format_args!("to take parameter `{0}` by reference, move `&{1}` to the type",
binding, pin_and_mut))
})format!("to take parameter `{binding}` by reference, move `&{pin_and_mut}` to the type"),
1422::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(pat.span.until(inner.span), "".to_owned()),
(ty_span.shrink_to_lo(),
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("&{0}",
pinned.prefix_str(mutbl)))
}))]))vec![
1423 (pat.span.until(inner.span), "".to_owned()),
1424 (ty_span.shrink_to_lo(), format!("&{}", pinned.prefix_str(mutbl))),
1425 ],
1426 Applicability::MachineApplicable1427 );
14281429if let Some((sp, msg, sugg)) = mut_var_suggestion {
1430err.span_note(sp, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}: `{1}`", msg, sugg))
})format!("{msg}: `{sugg}`"));
1431 }
1432 }
1433 hir::Node::Pat(pt) if let PatKind::TupleStruct(_, pat_arr, _) = pt.kind => {
1434for i in pat_arr.iter() {
1435if let PatKind::Ref(the_ref, _, _) = i.kind
1436 && let PatKind::Binding(mt, _, ident, _) = the_ref.kind
1437 {
1438let BindingMode(_, mtblty) = mt;
1439 err.span_suggestion_verbose(
1440 i.span,
1441::alloc::__export::must_use({
::alloc::fmt::format(format_args!("consider removing `&{0}` from the pattern",
pin_and_mut))
})format!("consider removing `&{pin_and_mut}` from the pattern"),
1442 mtblty.prefix_str().to_string() + &ident.name.to_string(),
1443 Applicability::MaybeIncorrect,
1444 );
1445 }
1446 }
1447if let Some((sp, msg, sugg)) = mut_var_suggestion {
1448err.span_note(sp, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}: `{1}`", msg, sugg))
})format!("{msg}: `{sugg}`"));
1449 }
1450 }
1451 hir::Node::Param(_) | hir::Node::Arm(_) | hir::Node::Pat(_) => {
1452// rely on match ergonomics or it might be nested `&&pat`
1453err.span_suggestion_verbose(
1454pat.span.until(inner.span),
1455::alloc::__export::must_use({
::alloc::fmt::format(format_args!("consider removing `&{0}` from the pattern",
pin_and_mut))
})format!("consider removing `&{pin_and_mut}` from the pattern"),
1456"",
1457 Applicability::MaybeIncorrect,
1458 );
14591460if let Some((sp, msg, sugg)) = mut_var_suggestion {
1461err.span_note(sp, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}: `{1}`", msg, sugg))
})format!("{msg}: `{sugg}`"));
1462 }
1463 }
1464_ if let Some((sp, msg, sugg)) = mut_var_suggestion => {
1465err.span_suggestion(sp, msg, sugg, Applicability::MachineApplicable);
1466 }
1467_ => {} // don't provide suggestions in other cases #55175
1468}
1469 }
1470 }
14711472fn check_dereferenceable(
1473&self,
1474 span: Span,
1475 expected: Ty<'tcx>,
1476 inner: &Pat<'_>,
1477 ) -> Result<(), ErrorGuaranteed> {
1478if let PatKind::Binding(..) = inner.kind
1479 && let Some(pointee_ty) = self.shallow_resolve(expected).builtin_deref(true)
1480 && let ty::Dynamic(..) = pointee_ty.kind()
1481 {
1482// This is "x = dyn SomeTrait" being reduced from
1483 // "let &x = &dyn SomeTrait" or "let box x = Box<dyn SomeTrait>", an error.
1484let type_str = self.ty_to_string(expected);
1485let mut err = {
self.dcx().struct_span_err(span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("type `{0}` cannot be dereferenced",
type_str))
})).with_code(E0033)
}struct_span_code_err!(
1486self.dcx(),
1487 span,
1488 E0033,
1489"type `{}` cannot be dereferenced",
1490 type_str
1491 );
1492err.span_label(span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("type `{0}` cannot be dereferenced",
type_str))
})format!("type `{type_str}` cannot be dereferenced"));
1493if self.tcx.sess.teach(err.code.unwrap()) {
1494err.note(CANNOT_IMPLICITLY_DEREF_POINTER_TRAIT_OBJ);
1495 }
1496return Err(err.emit());
1497 }
1498Ok(())
1499 }
15001501fn resolve_pat_struct(
1502&self,
1503 pat: &'tcx Pat<'tcx>,
1504 qpath: &hir::QPath<'tcx>,
1505 ) -> Result<ResolvedPat<'tcx>, ErrorGuaranteed> {
1506// Resolve the path and check the definition for errors.
1507let (variant, pat_ty) = self.check_struct_path(qpath, pat.hir_id)?;
1508Ok(ResolvedPat { ty: pat_ty, kind: ResolvedPatKind::Struct { variant } })
1509 }
15101511/// Reject pin-projection through a type that isn't structurally pinnable.
1512 ///
1513 /// Destructuring an ADT underneath a `&pin` reference projects its fields as pinned references.
1514 /// This is only sound if the type opted into structural pinning with `#[pin_v2]`; otherwise it
1515 /// would let safe code form a `Pin<&mut Field>` for a type that should never be pinned, breaking
1516 /// the `Pin` guarantee (see #157634).
1517 ///
1518 /// This covers both explicit (`&pin mut`/`&pin const`) and implicit (match-ergonomics)
1519 /// projection. `max_pinnedness` is only set for `&pin mut`, so the implicit shared (`&pin
1520 /// const`) case is instead recognized through its pinned binding mode, hence both are checked.
1521fn check_pin_projection(
1522&self,
1523 pat: &'tcx Pat<'tcx>,
1524 pat_ty: Ty<'tcx>,
1525 pat_info: PatInfo<'tcx>,
1526 ) {
1527let through_pin = pat_info.max_pinnedness == PinnednessCap::Pinned1528 || #[allow(non_exhaustive_omitted_patterns)] match pat_info.binding_mode {
ByRef::Yes(Pinnedness::Pinned, _) => true,
_ => false,
}matches!(pat_info.binding_mode, ByRef::Yes(Pinnedness::Pinned, _));
1529if through_pin1530 && let Some(adt) = pat_ty.ty_adt_def()
1531 && !adt.is_pin_project()
1532 && !adt.is_pin()
1533 {
1534let def_span: Option<Span> = self.tcx.hir_span_if_local(adt.did());
1535let sugg_span = def_span.map(|span| span.shrink_to_lo());
1536self.dcx().emit_err(crate::diagnostics::ProjectOnNonPinProjectType {
1537 span: pat.span,
1538def_span,
1539sugg_span,
1540 });
1541 }
1542 }
15431544fn check_pat_struct(
1545&self,
1546 pat: &'tcx Pat<'tcx>,
1547 fields: &'tcx [hir::PatField<'tcx>],
1548 has_rest_pat: bool,
1549 pat_ty: Ty<'tcx>,
1550 variant: &'tcx VariantDef,
1551 expected: Ty<'tcx>,
1552 pat_info: PatInfo<'tcx>,
1553 ) -> Ty<'tcx> {
1554self.check_pin_projection(pat, pat_ty, pat_info);
15551556// Type-check the path.
1557let had_err = self.demand_eqtype_pat(pat.span, expected, pat_ty, &pat_info.top_info);
15581559// Type-check subpatterns.
1560match self.check_struct_pat_fields(pat_ty, pat, variant, fields, has_rest_pat, pat_info) {
1561Ok(()) => match had_err {
1562Ok(()) => pat_ty,
1563Err(guar) => Ty::new_error(self.tcx, guar),
1564 },
1565Err(guar) => Ty::new_error(self.tcx, guar),
1566 }
1567 }
15681569fn resolve_pat_path(
1570&self,
1571 path_id: HirId,
1572 span: Span,
1573 qpath: &'tcx hir::QPath<'_>,
1574 ) -> Result<ResolvedPat<'tcx>, ErrorGuaranteed> {
1575let tcx = self.tcx;
15761577let (res, opt_ty, segments) =
1578self.resolve_ty_and_res_fully_qualified_call(qpath, path_id, span);
1579match res {
1580 Res::Err => {
1581let e =
1582self.dcx().span_delayed_bug(qpath.span(), "`Res::Err` but no error emitted");
1583self.set_tainted_by_errors(e);
1584return Err(e);
1585 }
1586 Res::Def(DefKind::AssocFn | DefKind::Ctor(_, CtorKind::Fn) | DefKind::Variant, _) => {
1587let expected = "unit struct, unit variant or constant";
1588let e = report_unexpected_variant_res(tcx, res, None, qpath, span, E0533, expected);
1589return Err(e);
1590 }
1591 Res::SelfCtor(def_id) => {
1592if let ty::Adt(adt_def, _) = *tcx.type_of(def_id).skip_binder().kind()
1593 && adt_def.is_struct()
1594 && let Some((CtorKind::Const, _)) = adt_def.non_enum_variant().ctor
1595 {
1596// Ok, we allow unit struct ctors in patterns only.
1597} else {
1598let e = report_unexpected_variant_res(
1599tcx,
1600res,
1601None,
1602qpath,
1603span,
1604E0533,
1605"unit struct",
1606 );
1607return Err(e);
1608 }
1609 }
1610 Res::Def(
1611 DefKind::Ctor(_, CtorKind::Const)
1612 | DefKind::Const { .. }
1613 | DefKind::AssocConst { .. }
1614 | DefKind::ConstParam,
1615_,
1616 ) => {} // OK
1617_ => ::rustc_middle::util::bug::bug_fmt(format_args!("unexpected pattern resolution: {0:?}",
res))bug!("unexpected pattern resolution: {:?}", res),
1618 }
16191620// Find the type of the path pattern, for later checking.
1621let (pat_ty, pat_res) =
1622self.instantiate_value_path(segments, opt_ty, res, span, span, path_id);
1623Ok(ResolvedPat { ty: pat_ty, kind: ResolvedPatKind::Path { res, pat_res, segments } })
1624 }
16251626fn check_pat_path(
1627&self,
1628 pat_id_for_diag: HirId,
1629 span: Span,
1630 resolved: &ResolvedPat<'tcx>,
1631 expected: Ty<'tcx>,
1632 ti: &TopInfo<'tcx>,
1633 ) -> Ty<'tcx> {
1634if let Err(err) =
1635self.demand_suptype_with_origin(&self.pattern_cause(ti, span), expected, resolved.ty)
1636 {
1637self.emit_bad_pat_path(err, pat_id_for_diag, span, resolved);
1638 }
1639resolved.ty
1640 }
16411642fn maybe_suggest_range_literal(
1643&self,
1644 e: &mut Diag<'_>,
1645 opt_def_id: Option<hir::def_id::DefId>,
1646 ident: Ident,
1647 ) -> bool {
1648if let Some(def_id) = opt_def_id1649 && let Some(hir::Node::Item(hir::Item {
1650 kind: hir::ItemKind::Const(_, _, _, ct_rhs),
1651 ..
1652 })) = self.tcx.hir_get_if_local(def_id)
1653 && let hir::Node::Expr(expr) = self.tcx.hir_node(ct_rhs.hir_id())
1654 && hir::is_range_literal(expr)
1655 {
1656let span = self.tcx.hir_span(ct_rhs.hir_id());
1657if let Ok(snip) = self.tcx.sess.source_map().span_to_snippet(span) {
1658e.span_suggestion_verbose(
1659ident.span,
1660"you may want to move the range into the match block",
1661snip,
1662 Applicability::MachineApplicable,
1663 );
1664return true;
1665 }
1666 }
1667false
1668}
16691670fn emit_bad_pat_path(
1671&self,
1672mut e: Diag<'_>,
1673 hir_id: HirId,
1674 pat_span: Span,
1675 resolved_pat: &ResolvedPat<'tcx>,
1676 ) {
1677let ResolvedPatKind::Path { res, pat_res, segments } = resolved_pat.kind else {
1678::rustc_middle::util::bug::span_bug_fmt(pat_span,
format_args!("unexpected resolution for path pattern: {0:?}",
resolved_pat));span_bug!(pat_span, "unexpected resolution for path pattern: {resolved_pat:?}");
1679 };
16801681let span = match (self.tcx.hir_res_span(pat_res), res.opt_def_id()) {
1682 (Some(span), _) => span,
1683 (None, Some(def_id)) => self.tcx.def_span(def_id),
1684 (None, None) => {
1685e.emit();
1686return;
1687 }
1688 };
1689if let [hir::PathSegment { ident, args: None, .. }] = segments1690 && e.suggestions.len() == 0
1691{
1692e.span_label(span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} defined here", res.descr()))
})format!("{} defined here", res.descr()));
1693e.span_label(
1694pat_span,
1695::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}` is interpreted as {1} {2}, not a new binding",
ident, res.article(), res.descr()))
})format!(
1696"`{}` is interpreted as {} {}, not a new binding",
1697 ident,
1698 res.article(),
1699 res.descr(),
1700 ),
1701 );
1702match self.tcx.parent_hir_node(hir_id) {
1703 hir::Node::PatField(..) => {
1704e.span_suggestion_verbose(
1705ident.span.shrink_to_hi(),
1706"bind the struct field to a different name instead",
1707::alloc::__export::must_use({
::alloc::fmt::format(format_args!(": other_{0}",
ident.as_str().to_lowercase()))
})format!(": other_{}", ident.as_str().to_lowercase()),
1708 Applicability::HasPlaceholders,
1709 );
1710 }
1711_ => {
1712let (type_def_id, item_def_id) = match resolved_pat.ty.kind() {
1713 ty::Adt(def, _) => match res {
1714 Res::Def(DefKind::Const { .. }, def_id) => {
1715 (Some(def.did()), Some(def_id))
1716 }
1717_ => (None, None),
1718 },
1719_ => (None, None),
1720 };
17211722let is_range = #[allow(non_exhaustive_omitted_patterns)] match type_def_id.and_then(|id|
self.tcx.as_lang_item(id)) {
Some(LangItem::Range | LangItem::RangeFrom | LangItem::RangeTo |
LangItem::RangeFull | LangItem::RangeInclusiveStruct |
LangItem::RangeToInclusive) => true,
_ => false,
}matches!(
1723 type_def_id.and_then(|id| self.tcx.as_lang_item(id)),
1724Some(
1725 LangItem::Range
1726 | LangItem::RangeFrom
1727 | LangItem::RangeTo
1728 | LangItem::RangeFull
1729 | LangItem::RangeInclusiveStruct
1730 | LangItem::RangeToInclusive,
1731 )
1732 );
1733if is_range {
1734if !self.maybe_suggest_range_literal(&mut e, item_def_id, *ident) {
1735let msg = "constants only support matching by type, \
1736 if you meant to match against a range of values, \
1737 consider using a range pattern like `min ..= max` in the match block";
1738e.note(msg);
1739 }
1740 } else {
1741let msg = "introduce a new binding instead";
1742let sugg = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("other_{0}",
ident.as_str().to_lowercase()))
})format!("other_{}", ident.as_str().to_lowercase());
1743e.span_suggestion_verbose(
1744ident.span,
1745msg,
1746sugg,
1747 Applicability::HasPlaceholders,
1748 );
1749 }
1750 }
1751 };
1752 }
1753e.emit();
1754 }
17551756fn resolve_pat_tuple_struct(
1757&self,
1758 pat: &'tcx Pat<'tcx>,
1759 qpath: &'tcx hir::QPath<'tcx>,
1760 ) -> Result<ResolvedPat<'tcx>, ErrorGuaranteed> {
1761let tcx = self.tcx;
1762let report_unexpected_res = |res: Res| {
1763let expected = "tuple struct or tuple variant";
1764let e = report_unexpected_variant_res(tcx, res, None, qpath, pat.span, E0164, expected);
1765Err(e)
1766 };
17671768// Resolve the path and check the definition for errors.
1769let (res, opt_ty, segments) =
1770self.resolve_ty_and_res_fully_qualified_call(qpath, pat.hir_id, pat.span);
1771if res == Res::Err {
1772let e = self.dcx().span_delayed_bug(pat.span, "`Res::Err` but no error emitted");
1773self.set_tainted_by_errors(e);
1774return Err(e);
1775 }
17761777// Type-check the path.
1778let (pat_ty, res) =
1779self.instantiate_value_path(segments, opt_ty, res, pat.span, pat.span, pat.hir_id);
1780if !pat_ty.is_fn() {
1781return report_unexpected_res(res);
1782 }
17831784let variant = match res {
1785 Res::Err => {
1786self.dcx().span_bug(pat.span, "`Res::Err` but no error emitted");
1787 }
1788 Res::Def(DefKind::AssocConst { .. } | DefKind::AssocFn, _) => {
1789return report_unexpected_res(res);
1790 }
1791 Res::Def(DefKind::Ctor(_, CtorKind::Fn), _) => tcx.expect_variant_res(res),
1792_ => ::rustc_middle::util::bug::bug_fmt(format_args!("unexpected pattern resolution: {0:?}",
res))bug!("unexpected pattern resolution: {:?}", res),
1793 };
17941795// Replace constructor type with constructed type for tuple struct patterns.
1796let pat_ty = pat_ty.fn_sig(tcx).output();
1797let pat_ty = pat_ty.no_bound_vars().expect("expected fn type");
17981799Ok(ResolvedPat { ty: pat_ty, kind: ResolvedPatKind::TupleStruct { res, variant } })
1800 }
18011802fn check_pat_tuple_struct(
1803&self,
1804 pat: &'tcx Pat<'tcx>,
1805 qpath: &'tcx hir::QPath<'tcx>,
1806 subpats: &'tcx [Pat<'tcx>],
1807 ddpos: hir::DotDotPos,
1808 res: Res,
1809 pat_ty: Ty<'tcx>,
1810 variant: &'tcx VariantDef,
1811 expected: Ty<'tcx>,
1812 pat_info: PatInfo<'tcx>,
1813 ) -> Ty<'tcx> {
1814self.check_pin_projection(pat, pat_ty, pat_info);
18151816let tcx = self.tcx;
1817let on_error = |e| {
1818for pat in subpats {
1819self.check_pat(pat, Ty::new_error(tcx, e), pat_info);
1820 }
1821 };
18221823// Type-check the tuple struct pattern against the expected type.
1824let had_err = self.demand_eqtype_pat(pat.span, expected, pat_ty, &pat_info.top_info);
18251826// Type-check subpatterns.
1827if subpats.len() == variant.fields.len()
1828 || subpats.len() < variant.fields.len() && ddpos.as_opt_usize().is_some()
1829 {
1830let ty::Adt(_, args) = pat_ty.kind() else {
1831::rustc_middle::util::bug::bug_fmt(format_args!("unexpected pattern type {0:?}",
pat_ty));bug!("unexpected pattern type {:?}", pat_ty);
1832 };
1833for (i, subpat) in subpats.iter().enumerate_and_adjust(variant.fields.len(), ddpos) {
1834let field = &variant.fields[FieldIdx::from_usize(i)];
1835let field_ty = self.field_ty(subpat.span, field, args);
1836self.check_pat(subpat, field_ty, pat_info);
18371838self.tcx.check_stability(
1839 variant.fields[FieldIdx::from_usize(i)].did,
1840Some(subpat.hir_id),
1841 subpat.span,
1842None,
1843 );
1844 }
1845if let Err(e) = had_err {
1846on_error(e);
1847return Ty::new_error(tcx, e);
1848 }
1849 } else {
1850let e = self.emit_err_pat_wrong_number_of_fields(
1851pat.span,
1852res,
1853qpath,
1854subpats,
1855&variant.fields.raw,
1856expected,
1857had_err,
1858 );
1859on_error(e);
1860return Ty::new_error(tcx, e);
1861 }
1862pat_ty1863 }
18641865fn emit_err_pat_wrong_number_of_fields(
1866&self,
1867 pat_span: Span,
1868 res: Res,
1869 qpath: &hir::QPath<'_>,
1870 subpats: &'tcx [Pat<'tcx>],
1871 fields: &'tcx [ty::FieldDef],
1872 expected: Ty<'tcx>,
1873 had_err: Result<(), ErrorGuaranteed>,
1874 ) -> ErrorGuaranteed {
1875let subpats_ending = if subpats.len() == 1 { "" } else { "s" }pluralize!(subpats.len());
1876let fields_ending = if fields.len() == 1 { "" } else { "s" }pluralize!(fields.len());
18771878let subpat_spans = if subpats.is_empty() {
1879::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[pat_span]))vec![pat_span]1880 } else {
1881subpats.iter().map(|p| p.span).collect()
1882 };
1883let last_subpat_span = *subpat_spans.last().unwrap();
1884let res_span = self.tcx.def_span(res.def_id());
1885let def_ident_span = self.tcx.def_ident_span(res.def_id()).unwrap_or(res_span);
1886let field_def_spans = if fields.is_empty() {
1887::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[res_span]))vec![res_span]1888 } else {
1889fields.iter().map(|f| f.ident(self.tcx).span).collect()
1890 };
1891let last_field_def_span = *field_def_spans.last().unwrap();
18921893let mut err = {
self.dcx().struct_span_err(MultiSpan::from_spans(subpat_spans),
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("this pattern has {0} field{1}, but the corresponding {2} has {3} field{4}",
subpats.len(), subpats_ending, res.descr(), fields.len(),
fields_ending))
})).with_code(E0023)
}struct_span_code_err!(
1894self.dcx(),
1895 MultiSpan::from_spans(subpat_spans),
1896 E0023,
1897"this pattern has {} field{}, but the corresponding {} has {} field{}",
1898 subpats.len(),
1899 subpats_ending,
1900 res.descr(),
1901 fields.len(),
1902 fields_ending,
1903 );
1904err.span_label(
1905last_subpat_span,
1906::alloc::__export::must_use({
::alloc::fmt::format(format_args!("expected {0} field{1}, found {2}",
fields.len(), fields_ending, subpats.len()))
})format!("expected {} field{}, found {}", fields.len(), fields_ending, subpats.len()),
1907 );
1908if self.tcx.sess.source_map().is_multiline(qpath.span().between(last_subpat_span)) {
1909err.span_label(qpath.span(), "");
1910 }
1911if self.tcx.sess.source_map().is_multiline(def_ident_span.between(last_field_def_span)) {
1912err.span_label(def_ident_span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} defined here", res.descr()))
})format!("{} defined here", res.descr()));
1913 }
1914for span in &field_def_spans[..field_def_spans.len() - 1] {
1915 err.span_label(*span, "");
1916 }
1917err.span_label(
1918last_field_def_span,
1919::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} has {1} field{2}", res.descr(),
fields.len(), fields_ending))
})format!("{} has {} field{}", res.descr(), fields.len(), fields_ending),
1920 );
19211922// Identify the case `Some(x, y)` where the expected type is e.g. `Option<(T, U)>`.
1923 // More generally, the expected type wants a tuple variant with one field of an
1924 // N-arity-tuple, e.g., `V_i((p_0, .., p_N))`. Meanwhile, the user supplied a pattern
1925 // with the subpatterns directly in the tuple variant pattern, e.g., `V_i(p_0, .., p_N)`.
1926let missing_parentheses = match (expected.kind(), fields, had_err) {
1927// #67037: only do this if we could successfully type-check the expected type against
1928 // the tuple struct pattern. Otherwise the args could get out of range on e.g.,
1929 // `let P() = U;` where `P != U` with `struct Box<T>(T);`.
1930(ty::Adt(_, args), [field], Ok(())) => {
1931let field_ty = self.field_ty(pat_span, field, args);
1932match field_ty.kind() {
1933 ty::Tuple(fields) => fields.len() == subpats.len(),
1934_ => false,
1935 }
1936 }
1937_ => false,
1938 };
1939if missing_parentheses {
1940let (left, right) = match subpats {
1941// This is the zero case; we aim to get the "hi" part of the `QPath`'s
1942 // span as the "lo" and then the "hi" part of the pattern's span as the "hi".
1943 // This looks like:
1944 //
1945 // help: missing parentheses
1946 // |
1947 // L | let A(()) = A(());
1948 // | ^ ^
1949[] => (qpath.span().shrink_to_hi(), pat_span),
1950// Easy case. Just take the "lo" of the first sub-pattern and the "hi" of the
1951 // last sub-pattern. In the case of `A(x)` the first and last may coincide.
1952 // This looks like:
1953 //
1954 // help: missing parentheses
1955 // |
1956 // L | let A((x, y)) = A((1, 2));
1957 // | ^ ^
1958[first, ..] => (first.span.shrink_to_lo(), subpats.last().unwrap().span),
1959 };
1960err.multipart_suggestion(
1961"missing parentheses",
1962::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(left, "(".to_string()), (right.shrink_to_hi(), ")".to_string())]))vec![(left, "(".to_string()), (right.shrink_to_hi(), ")".to_string())],
1963 Applicability::MachineApplicable,
1964 );
1965 } else if fields.len() > subpats.len() && pat_span != DUMMY_SP {
1966let after_fields_span = pat_span.with_hi(pat_span.hi() - BytePos(1)).shrink_to_hi();
1967let all_fields_span = match subpats {
1968 [] => after_fields_span,
1969 [field] => field.span,
1970 [first, .., last] => first.span.to(last.span),
1971 };
19721973// Check if all the fields in the pattern are wildcards.
1974let all_wildcards = subpats.iter().all(|pat| #[allow(non_exhaustive_omitted_patterns)] match pat.kind {
PatKind::Wild => true,
_ => false,
}matches!(pat.kind, PatKind::Wild));
1975let first_tail_wildcard =
1976subpats.iter().enumerate().fold(None, |acc, (pos, pat)| match (acc, &pat.kind) {
1977 (None, PatKind::Wild) => Some(pos),
1978 (Some(_), PatKind::Wild) => acc,
1979_ => None,
1980 });
1981let tail_span = match first_tail_wildcard {
1982None => after_fields_span,
1983Some(0) => subpats[0].span.to(after_fields_span),
1984Some(pos) => subpats[pos - 1].span.shrink_to_hi().to(after_fields_span),
1985 };
19861987// FIXME: heuristic-based suggestion to check current types for where to add `_`.
1988let mut wildcard_sugg = ::alloc::vec::from_elem("_", fields.len() - subpats.len())vec!["_"; fields.len() - subpats.len()].join(", ");
1989if !subpats.is_empty() {
1990wildcard_sugg = String::from(", ") + &wildcard_sugg;
1991 }
19921993err.span_suggestion_verbose(
1994after_fields_span,
1995"use `_` to explicitly ignore each field",
1996wildcard_sugg,
1997 Applicability::MaybeIncorrect,
1998 );
19992000// Only suggest `..` if more than one field is missing
2001 // or the pattern consists of all wildcards.
2002if fields.len() - subpats.len() > 1 || all_wildcards {
2003if subpats.is_empty() || all_wildcards {
2004err.span_suggestion_verbose(
2005all_fields_span,
2006"use `..` to ignore all fields",
2007"..",
2008 Applicability::MaybeIncorrect,
2009 );
2010 } else {
2011err.span_suggestion_verbose(
2012tail_span,
2013"use `..` to ignore the rest of the fields",
2014", ..",
2015 Applicability::MaybeIncorrect,
2016 );
2017 }
2018 }
2019 }
20202021err.emit()
2022 }
20232024fn check_pat_tuple(
2025&self,
2026 span: Span,
2027 elements: &'tcx [Pat<'tcx>],
2028 ddpos: hir::DotDotPos,
2029 expected: Ty<'tcx>,
2030 pat_info: PatInfo<'tcx>,
2031 ) -> Ty<'tcx> {
2032let tcx = self.tcx;
2033let mut expected_len = elements.len();
2034if ddpos.as_opt_usize().is_some() {
2035// Require known type only when `..` is present.
2036if let ty::Tuple(tys) = self.structurally_resolve_type(span, expected).kind() {
2037expected_len = tys.len();
2038 }
2039 }
2040let max_len = cmp::max(expected_len, elements.len());
20412042let element_tys_iter = (0..max_len).map(|_| self.next_ty_var(span));
2043let element_tys = tcx.mk_type_list_from_iter(element_tys_iter);
2044let pat_ty = Ty::new_tup(tcx, element_tys);
2045if let Err(reported) = self.demand_eqtype_pat(span, expected, pat_ty, &pat_info.top_info) {
2046// Walk subpatterns with an expected type of `err` in this case to silence
2047 // further errors being emitted when using the bindings. #50333
2048for (_, elem) in elements.iter().enumerate_and_adjust(max_len, ddpos) {
2049self.check_pat(elem, Ty::new_error(tcx, reported), pat_info);
2050 }
2051Ty::new_error(tcx, reported)
2052 } else {
2053for (i, elem) in elements.iter().enumerate_and_adjust(max_len, ddpos) {
2054self.check_pat(elem, element_tys[i], pat_info);
2055 }
2056pat_ty2057 }
2058 }
20592060fn check_struct_pat_fields(
2061&self,
2062 adt_ty: Ty<'tcx>,
2063 pat: &'tcx Pat<'tcx>,
2064 variant: &'tcx ty::VariantDef,
2065 fields: &'tcx [hir::PatField<'tcx>],
2066 has_rest_pat: bool,
2067 pat_info: PatInfo<'tcx>,
2068 ) -> Result<(), ErrorGuaranteed> {
2069let tcx = self.tcx;
20702071let ty::Adt(adt, args) = adt_ty.kind() else {
2072::rustc_middle::util::bug::span_bug_fmt(pat.span,
format_args!("struct pattern is not an ADT"));span_bug!(pat.span, "struct pattern is not an ADT");
2073 };
20742075// Index the struct fields' types.
2076let field_map = variant2077 .fields
2078 .iter_enumerated()
2079 .map(|(i, field)| (field.ident(self.tcx).normalize_to_macros_2_0(), (i, field)))
2080 .collect::<FxHashMap<_, _>>();
20812082// Keep track of which fields have already appeared in the pattern.
2083let mut used_fields = FxHashMap::default();
2084let mut result = Ok(());
20852086let mut inexistent_fields = ::alloc::vec::Vec::new()vec![];
2087// Typecheck each field.
2088for field in fields {
2089let span = field.span;
2090let ident = tcx.adjust_ident(field.ident, variant.def_id);
2091let field_ty = match used_fields.entry(ident) {
2092 Occupied(occupied) => {
2093let guar = self.error_field_already_bound(span, field.ident, *occupied.get());
2094 result = Err(guar);
2095 Ty::new_error(tcx, guar)
2096 }
2097 Vacant(vacant) => {
2098 vacant.insert(span);
2099 field_map
2100 .get(&ident)
2101 .map(|(i, f)| {
2102self.write_field_index(field.hir_id, *i);
2103self.tcx.check_stability(f.did, Some(field.hir_id), span, None);
2104self.field_ty(span, f, args)
2105 })
2106 .unwrap_or_else(|| {
2107 inexistent_fields.push(field);
2108 Ty::new_misc_error(tcx)
2109 })
2110 }
2111 };
21122113self.check_pat(field.pat, field_ty, pat_info);
2114 }
21152116let mut unmentioned_fields = variant2117 .fields
2118 .iter()
2119 .map(|field| (field, field.ident(self.tcx).normalize_to_macros_2_0()))
2120 .filter(|(_, ident)| !used_fields.contains_key(ident))
2121 .collect::<Vec<_>>();
21222123let inexistent_fields_err = if !inexistent_fields.is_empty()
2124 && !inexistent_fields.iter().any(|field| field.ident.name == kw::Underscore)
2125 {
2126// we don't care to report errors for a struct if the struct itself is tainted
2127variant.has_errors()?;
2128Some(self.error_inexistent_fields(
2129adt.variant_descr(),
2130&inexistent_fields,
2131&mut unmentioned_fields,
2132pat,
2133variant,
2134args,
2135 ))
2136 } else {
2137None2138 };
21392140// Require `..` if struct has non_exhaustive attribute.
2141let non_exhaustive = variant.field_list_has_applicable_non_exhaustive();
2142if non_exhaustive && !has_rest_pat {
2143self.error_foreign_non_exhaustive_spat(pat, adt.variant_descr(), fields.is_empty());
2144 }
21452146let mut unmentioned_err = None;
2147// Report an error if an incorrect number of fields was specified.
2148if adt.is_union() {
2149if fields.len() != 1 {
2150self.dcx().emit_err(diagnostics::UnionPatMultipleFields { span: pat.span });
2151 }
2152if has_rest_pat {
2153self.dcx().emit_err(diagnostics::UnionPatDotDot { span: pat.span });
2154 }
2155 } else if !unmentioned_fields.is_empty() {
2156let accessible_unmentioned_fields: Vec<_> = unmentioned_fields2157 .iter()
2158 .copied()
2159 .filter(|(field, _)| self.is_field_suggestable(field, pat.hir_id, pat.span))
2160 .collect();
21612162if !has_rest_pat {
2163if accessible_unmentioned_fields.is_empty() {
2164unmentioned_err = Some(self.error_no_accessible_fields(pat, fields));
2165 } else {
2166unmentioned_err = Some(self.error_unmentioned_fields(
2167pat,
2168&accessible_unmentioned_fields,
2169accessible_unmentioned_fields.len() != unmentioned_fields.len(),
2170fields,
2171 ));
2172 }
2173 } else if non_exhaustive && !accessible_unmentioned_fields.is_empty() {
2174self.lint_non_exhaustive_omitted_patterns(
2175pat,
2176&accessible_unmentioned_fields,
2177adt_ty,
2178 )
2179 }
2180 }
2181match (inexistent_fields_err, unmentioned_err) {
2182 (Some(i), Some(u)) => {
2183if let Err(e) = self.error_tuple_variant_as_struct_pat(pat, fields, variant) {
2184// We don't want to show the nonexistent fields error when this was
2185 // `Foo { a, b }` when it should have been `Foo(a, b)`.
2186i.delay_as_bug();
2187u.delay_as_bug();
2188Err(e)
2189 } else {
2190i.emit();
2191Err(u.emit())
2192 }
2193 }
2194 (None, Some(u)) => {
2195if let Err(e) = self.error_tuple_variant_as_struct_pat(pat, fields, variant) {
2196u.delay_as_bug();
2197Err(e)
2198 } else {
2199Err(u.emit())
2200 }
2201 }
2202 (Some(err), None) => Err(err.emit()),
2203 (None, None) => {
2204self.error_tuple_variant_index_shorthand(variant, pat, fields)?;
2205result2206 }
2207 }
2208 }
22092210fn error_tuple_variant_index_shorthand(
2211&self,
2212 variant: &VariantDef,
2213 pat: &'_ Pat<'_>,
2214 fields: &[hir::PatField<'_>],
2215 ) -> Result<(), ErrorGuaranteed> {
2216// if this is a tuple struct, then all field names will be numbers
2217 // so if any fields in a struct pattern use shorthand syntax, they will
2218 // be invalid identifiers (for example, Foo { 0, 1 }).
2219if let (Some(CtorKind::Fn), PatKind::Struct(qpath, field_patterns, ..)) =
2220 (variant.ctor_kind(), &pat.kind)
2221 {
2222let has_shorthand_field_name = field_patterns.iter().any(|field| field.is_shorthand);
2223if has_shorthand_field_name {
2224let path = rustc_hir_pretty::qpath_to_string(&self.tcx, qpath);
2225let mut err = {
self.dcx().struct_span_err(pat.span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("tuple variant `{0}` written as struct variant",
path))
})).with_code(E0769)
}struct_span_code_err!(
2226self.dcx(),
2227 pat.span,
2228 E0769,
2229"tuple variant `{path}` written as struct variant",
2230 );
2231err.span_suggestion_verbose(
2232qpath.span().shrink_to_hi().to(pat.span.shrink_to_hi()),
2233"use the tuple variant pattern syntax instead",
2234::alloc::__export::must_use({
::alloc::fmt::format(format_args!("({0})",
self.get_suggested_tuple_struct_pattern(fields, variant)))
})format!("({})", self.get_suggested_tuple_struct_pattern(fields, variant)),
2235 Applicability::MaybeIncorrect,
2236 );
2237return Err(err.emit());
2238 }
2239 }
2240Ok(())
2241 }
22422243fn error_foreign_non_exhaustive_spat(&self, pat: &Pat<'_>, descr: &str, no_fields: bool) {
2244let sess = self.tcx.sess;
2245let sm = sess.source_map();
2246let sp_brace = sm.end_point(pat.span);
2247let sp_comma = sm.end_point(pat.span.with_hi(sp_brace.hi()));
2248let sugg = if no_fields || sp_brace != sp_comma { ".. }" } else { ", .. }" };
22492250{
self.dcx().struct_span_err(pat.span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`..` required with {0} marked as non-exhaustive",
descr))
})).with_code(E0638)
}struct_span_code_err!(
2251self.dcx(),
2252 pat.span,
2253 E0638,
2254"`..` required with {descr} marked as non-exhaustive",
2255 )2256 .with_span_suggestion_verbose(
2257sp_comma,
2258"add `..` at the end of the field list to ignore all other fields",
2259sugg,
2260 Applicability::MachineApplicable,
2261 )
2262 .emit();
2263 }
22642265fn error_field_already_bound(
2266&self,
2267 span: Span,
2268 ident: Ident,
2269 other_field: Span,
2270 ) -> ErrorGuaranteed {
2271{
self.dcx().struct_span_err(span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("field `{0}` bound multiple times in the pattern",
ident))
})).with_code(E0025)
}struct_span_code_err!(
2272self.dcx(),
2273 span,
2274 E0025,
2275"field `{}` bound multiple times in the pattern",
2276 ident
2277 )2278 .with_span_label(span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("multiple uses of `{0}` in pattern",
ident))
})format!("multiple uses of `{ident}` in pattern"))
2279 .with_span_label(other_field, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("first use of `{0}`", ident))
})format!("first use of `{ident}`"))
2280 .emit()
2281 }
22822283fn error_inexistent_fields(
2284&self,
2285 kind_name: &str,
2286 inexistent_fields: &[&hir::PatField<'tcx>],
2287 unmentioned_fields: &mut Vec<(&'tcx ty::FieldDef, Ident)>,
2288 pat: &'tcx Pat<'tcx>,
2289 variant: &ty::VariantDef,
2290 args: ty::GenericArgsRef<'tcx>,
2291 ) -> Diag<'a> {
2292let tcx = self.tcx;
2293let (field_names, t, plural) = if let [field] = inexistent_fields {
2294 (::alloc::__export::must_use({
::alloc::fmt::format(format_args!("a field named `{0}`", field.ident))
})format!("a field named `{}`", field.ident), "this", "")
2295 } else {
2296 (
2297::alloc::__export::must_use({
::alloc::fmt::format(format_args!("fields named {0}",
inexistent_fields.iter().map(|field|
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}`", field.ident))
})).collect::<Vec<String>>().join(", ")))
})format!(
2298"fields named {}",
2299 inexistent_fields
2300 .iter()
2301 .map(|field| format!("`{}`", field.ident))
2302 .collect::<Vec<String>>()
2303 .join(", ")
2304 ),
2305"these",
2306"s",
2307 )
2308 };
2309let spans = inexistent_fields.iter().map(|field| field.ident.span).collect::<Vec<_>>();
2310let mut err = {
self.dcx().struct_span_err(spans,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} `{1}` does not have {2}",
kind_name, tcx.def_path_str(variant.def_id), field_names))
})).with_code(E0026)
}struct_span_code_err!(
2311self.dcx(),
2312 spans,
2313 E0026,
2314"{} `{}` does not have {}",
2315 kind_name,
2316 tcx.def_path_str(variant.def_id),
2317 field_names
2318 );
2319if let Some(pat_field) = inexistent_fields.last() {
2320err.span_label(
2321pat_field.ident.span,
2322::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} `{1}` does not have {2} field{3}",
kind_name, tcx.def_path_str(variant.def_id), t, plural))
})format!(
2323"{} `{}` does not have {} field{}",
2324 kind_name,
2325 tcx.def_path_str(variant.def_id),
2326 t,
2327 plural
2328 ),
2329 );
23302331if let [(field_def, field)] = unmentioned_fields.as_slice()
2332 && self.is_field_suggestable(field_def, pat.hir_id, pat.span)
2333 {
2334let suggested_name =
2335find_best_match_for_name(&[field.name], pat_field.ident.name, None);
2336if let Some(suggested_name) = suggested_name {
2337err.span_suggestion_verbose(
2338pat_field.ident.span,
2339"a field with a similar name exists",
2340suggested_name,
2341 Applicability::MaybeIncorrect,
2342 );
23432344// When we have a tuple struct used with struct we don't want to suggest using
2345 // the (valid) struct syntax with numeric field names. Instead we want to
2346 // suggest the expected syntax. We infer that this is the case by parsing the
2347 // `Ident` into an unsized integer. The suggestion will be emitted elsewhere in
2348 // `smart_resolve_context_dependent_help`.
2349if suggested_name.to_ident_string().parse::<usize>().is_err() {
2350// We don't want to throw `E0027` in case we have thrown `E0026` for them.
2351unmentioned_fields.retain(|&(_, x)| x.name != suggested_name);
2352 }
2353 } else if inexistent_fields.len() == 1 {
2354match pat_field.pat.kind {
2355 PatKind::Expr(_)
2356if !self.may_coerce(
2357self.typeck_results.borrow().node_type(pat_field.pat.hir_id),
2358self.field_ty(field.span, field_def, args),
2359 ) => {}
2360_ => {
2361err.span_suggestion_short(
2362pat_field.ident.span,
2363::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}` has a field named `{1}`",
tcx.def_path_str(variant.def_id), field.name))
})format!(
2364"`{}` has a field named `{}`",
2365 tcx.def_path_str(variant.def_id),
2366 field.name,
2367 ),
2368field.name,
2369 Applicability::MaybeIncorrect,
2370 );
2371 }
2372 }
2373 }
2374 }
2375 }
2376if tcx.sess.teach(err.code.unwrap()) {
2377err.note(
2378"This error indicates that a struct pattern attempted to \
2379 extract a nonexistent field from a struct. Struct fields \
2380 are identified by the name used before the colon : so struct \
2381 patterns should resemble the declaration of the struct type \
2382 being matched.\n\n\
2383 If you are using shorthand field patterns but want to refer \
2384 to the struct field by a different name, you should rename \
2385 it explicitly.",
2386 );
2387 }
2388err2389 }
23902391fn error_tuple_variant_as_struct_pat(
2392&self,
2393 pat: &Pat<'_>,
2394 fields: &'tcx [hir::PatField<'tcx>],
2395 variant: &ty::VariantDef,
2396 ) -> Result<(), ErrorGuaranteed> {
2397if let (Some(CtorKind::Fn), PatKind::Struct(qpath, pattern_fields, ..)) =
2398 (variant.ctor_kind(), &pat.kind)
2399 {
2400let is_tuple_struct_match = !pattern_fields.is_empty()
2401 && pattern_fields.iter().map(|field| field.ident.name.as_str()).all(is_number);
2402if is_tuple_struct_match {
2403return Ok(());
2404 }
24052406// we don't care to report errors for a struct if the struct itself is tainted
2407variant.has_errors()?;
24082409let path = rustc_hir_pretty::qpath_to_string(&self.tcx, qpath);
2410let mut err = {
self.dcx().struct_span_err(pat.span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("tuple variant `{0}` written as struct variant",
path))
})).with_code(E0769)
}struct_span_code_err!(
2411self.dcx(),
2412 pat.span,
2413 E0769,
2414"tuple variant `{}` written as struct variant",
2415 path
2416 );
2417let (sugg, appl) = if fields.len() == variant.fields.len() {
2418 (
2419self.get_suggested_tuple_struct_pattern(fields, variant),
2420 Applicability::MachineApplicable,
2421 )
2422 } else {
2423 (
2424variant.fields.iter().map(|_| "_").collect::<Vec<&str>>().join(", "),
2425 Applicability::MaybeIncorrect,
2426 )
2427 };
2428err.span_suggestion_verbose(
2429qpath.span().shrink_to_hi().to(pat.span.shrink_to_hi()),
2430"use the tuple variant pattern syntax instead",
2431::alloc::__export::must_use({
::alloc::fmt::format(format_args!("({0})", sugg))
})format!("({sugg})"),
2432appl,
2433 );
2434return Err(err.emit());
2435 }
2436Ok(())
2437 }
24382439fn get_suggested_tuple_struct_pattern(
2440&self,
2441 fields: &[hir::PatField<'_>],
2442 variant: &VariantDef,
2443 ) -> String {
2444let variant_field_idents =
2445variant.fields.iter().map(|f| f.ident(self.tcx)).collect::<Vec<Ident>>();
2446fields2447 .iter()
2448 .map(|field| {
2449match self.tcx.sess.source_map().span_to_snippet(field.pat.span) {
2450Ok(f) => {
2451// Field names are numbers, but numbers
2452 // are not valid identifiers
2453if variant_field_idents.contains(&field.ident) {
2454String::from("_")
2455 } else {
2456f2457 }
2458 }
2459Err(_) => rustc_hir_pretty::pat_to_string(&self.tcx, field.pat),
2460 }
2461 })
2462 .collect::<Vec<String>>()
2463 .join(", ")
2464 }
24652466/// Returns a diagnostic reporting a struct pattern which is missing an `..` due to
2467 /// inaccessible fields.
2468 ///
2469 /// ```text
2470 /// error: pattern requires `..` due to inaccessible fields
2471 /// --> src/main.rs:10:9
2472 /// |
2473 /// LL | let foo::Foo {} = foo::Foo::default();
2474 /// | ^^^^^^^^^^^
2475 /// |
2476 /// help: add a `..`
2477 /// |
2478 /// LL | let foo::Foo { .. } = foo::Foo::default();
2479 /// | ^^^^^^
2480 /// ```
2481fn error_no_accessible_fields(
2482&self,
2483 pat: &Pat<'_>,
2484 fields: &'tcx [hir::PatField<'tcx>],
2485 ) -> Diag<'a> {
2486let mut err = self2487 .dcx()
2488 .struct_span_err(pat.span, "pattern requires `..` due to inaccessible fields");
24892490if let Some(field) = fields.last() {
2491let tail_span = field.span.shrink_to_hi().to(pat.span.shrink_to_hi());
2492let comma_hi_offset =
2493self.tcx.sess.source_map().span_to_snippet(tail_span).ok().and_then(|snippet| {
2494let trimmed = snippet.trim_start();
2495trimmed.starts_with(',').then(|| (snippet.len() - trimmed.len() + 1) as u32)
2496 });
2497err.span_suggestion_verbose(
2498if let Some(comma_hi_offset) = comma_hi_offset {
2499tail_span.with_hi(tail_span.lo() + BytePos(comma_hi_offset)).shrink_to_hi()
2500 } else {
2501field.span.shrink_to_hi()
2502 },
2503"ignore the inaccessible and unused fields",
2504if comma_hi_offset.is_some() { " .." } else { ", .." },
2505 Applicability::MachineApplicable,
2506 );
2507 } else {
2508let qpath_span = if let PatKind::Struct(qpath, ..) = &pat.kind {
2509qpath.span()
2510 } else {
2511::rustc_middle::util::bug::bug_fmt(format_args!("`error_no_accessible_fields` called on non-struct pattern"));bug!("`error_no_accessible_fields` called on non-struct pattern");
2512 };
25132514// Shrink the span to exclude the `foo:Foo` in `foo::Foo { }`.
2515let span = pat.span.with_lo(qpath_span.shrink_to_hi().hi());
2516err.span_suggestion_verbose(
2517span,
2518"ignore the inaccessible and unused fields",
2519" { .. }",
2520 Applicability::MachineApplicable,
2521 );
2522 }
2523err2524 }
25252526/// Report that a pattern for a `#[non_exhaustive]` struct marked with `non_exhaustive_omitted_patterns`
2527 /// is not exhaustive enough.
2528 ///
2529 /// Nb: the partner lint for enums lives in `compiler/rustc_mir_build/src/thir/pattern/usefulness.rs`.
2530fn lint_non_exhaustive_omitted_patterns(
2531&self,
2532 pat: &Pat<'_>,
2533 unmentioned_fields: &[(&ty::FieldDef, Ident)],
2534 ty: Ty<'tcx>,
2535 ) {
2536struct FieldsNotListed<'a, 'b, 'tcx> {
2537 pat_span: Span,
2538 unmentioned_fields: &'a [(&'b ty::FieldDef, Ident)],
2539 joined_patterns: String,
2540 ty: Ty<'tcx>,
2541 }
25422543impl<'a, 'b, 'c, 'tcx> Diagnostic<'a, ()> for FieldsNotListed<'b, 'c, 'tcx> {
2544fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, ()> {
2545let Self { pat_span, unmentioned_fields, joined_patterns, ty } = self;
2546Diag::new(dcx, level, "some fields are not explicitly listed")
2547 .with_span_label(pat_span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("field{0} {1} not listed",
if unmentioned_fields.len() == 1 { "" } else { "s" },
joined_patterns))
})format!("field{} {} not listed", rustc_errors::pluralize!(unmentioned_fields.len()), joined_patterns))
2548 .with_help(
2549"ensure that all fields are mentioned explicitly by adding the suggested fields",
2550 )
2551 .with_note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("the pattern is of type `{0}` and the `non_exhaustive_omitted_patterns` attribute was found",
ty))
})format!(
2552"the pattern is of type `{ty}` and the `non_exhaustive_omitted_patterns` attribute was found",
2553 ))
2554 }
2555 }
25562557fn joined_uncovered_patterns(witnesses: &[&Ident]) -> String {
2558const LIMIT: usize = 3;
2559match witnesses {
2560 [] => {
2561{
::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
format_args!("expected an uncovered pattern, otherwise why are we emitting an error?")));
}unreachable!(
2562"expected an uncovered pattern, otherwise why are we emitting an error?"
2563)2564 }
2565 [witness] => ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}`", witness))
})format!("`{witness}`"),
2566 [head @ .., tail] if head.len() < LIMIT => {
2567let head: Vec<_> = head.iter().map(<_>::to_string).collect();
2568::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}` and `{1}`",
head.join("`, `"), tail))
})format!("`{}` and `{}`", head.join("`, `"), tail)2569 }
2570_ => {
2571let (head, tail) = witnesses.split_at(LIMIT);
2572let head: Vec<_> = head.iter().map(<_>::to_string).collect();
2573::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}` and {1} more",
head.join("`, `"), tail.len()))
})format!("`{}` and {} more", head.join("`, `"), tail.len())2574 }
2575 }
2576 }
2577let joined_patterns = joined_uncovered_patterns(
2578&unmentioned_fields.iter().map(|(_, i)| i).collect::<Vec<_>>(),
2579 );
25802581self.tcx.emit_node_span_lint(
2582NON_EXHAUSTIVE_OMITTED_PATTERNS,
2583pat.hir_id,
2584pat.span,
2585FieldsNotListed { pat_span: pat.span, unmentioned_fields, joined_patterns, ty },
2586 );
2587 }
25882589/// Returns a diagnostic reporting a struct pattern which does not mention some fields.
2590 ///
2591 /// ```text
2592 /// error[E0027]: pattern does not mention field `bar`
2593 /// --> src/main.rs:15:9
2594 /// |
2595 /// LL | let foo::Foo {} = foo::Foo::new();
2596 /// | ^^^^^^^^^^^ missing field `bar`
2597 /// ```
2598fn error_unmentioned_fields(
2599&self,
2600 pat: &Pat<'_>,
2601 unmentioned_fields: &[(&ty::FieldDef, Ident)],
2602 have_inaccessible_fields: bool,
2603 fields: &'tcx [hir::PatField<'tcx>],
2604 ) -> Diag<'a> {
2605let inaccessible = if have_inaccessible_fields { " and inaccessible fields" } else { "" };
2606let field_names = if let [(_, field)] = unmentioned_fields {
2607::alloc::__export::must_use({
::alloc::fmt::format(format_args!("field `{0}`{1}", field,
inaccessible))
})format!("field `{field}`{inaccessible}")2608 } else {
2609let fields = unmentioned_fields2610 .iter()
2611 .map(|(_, name)| ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}`", name))
})format!("`{name}`"))
2612 .collect::<Vec<String>>()
2613 .join(", ");
2614::alloc::__export::must_use({
::alloc::fmt::format(format_args!("fields {0}{1}", fields,
inaccessible))
})format!("fields {fields}{inaccessible}")2615 };
2616let mut err = {
self.dcx().struct_span_err(pat.span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("pattern does not mention {0}",
field_names))
})).with_code(E0027)
}struct_span_code_err!(
2617self.dcx(),
2618 pat.span,
2619 E0027,
2620"pattern does not mention {}",
2621 field_names
2622 );
2623err.span_label(pat.span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("missing {0}", field_names))
})format!("missing {field_names}"));
2624let len = unmentioned_fields.len();
2625let (prefix, postfix, sp) = match fields {
2626 [] => match &pat.kind {
2627 PatKind::Struct(path, [], None) => {
2628 (" { ", " }", path.span().shrink_to_hi().until(pat.span.shrink_to_hi()))
2629 }
2630_ => return err,
2631 },
2632 [.., field] => {
2633// Account for last field having a trailing comma or parse recovery at the tail of
2634 // the pattern to avoid invalid suggestion (#78511).
2635let tail = field.span.shrink_to_hi().with_hi(pat.span.hi());
2636match &pat.kind {
2637 PatKind::Struct(..) => (", ", " }", tail),
2638_ => return err,
2639 }
2640 }
2641 };
2642err.span_suggestion(
2643sp,
2644::alloc::__export::must_use({
::alloc::fmt::format(format_args!("include the missing field{0} in the pattern{1}",
if len == 1 { "" } else { "s" },
if have_inaccessible_fields {
" and ignore the inaccessible fields"
} else { "" }))
})format!(
2645"include the missing field{} in the pattern{}",
2646pluralize!(len),
2647if have_inaccessible_fields { " and ignore the inaccessible fields" } else { "" }
2648 ),
2649::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}{1}{2}{3}", prefix,
unmentioned_fields.iter().map(|(_, name)|
{
let field_name = name.to_string();
if is_number(&field_name) {
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}: _", field_name))
})
} else { field_name }
}).collect::<Vec<_>>().join(", "),
if have_inaccessible_fields { ", .." } else { "" }, postfix))
})format!(
2650"{}{}{}{}",
2651 prefix,
2652 unmentioned_fields
2653 .iter()
2654 .map(|(_, name)| {
2655let field_name = name.to_string();
2656if is_number(&field_name) { format!("{field_name}: _") } else { field_name }
2657 })
2658 .collect::<Vec<_>>()
2659 .join(", "),
2660if have_inaccessible_fields { ", .." } else { "" },
2661 postfix,
2662 ),
2663 Applicability::MachineApplicable,
2664 );
2665err.span_suggestion(
2666sp,
2667::alloc::__export::must_use({
::alloc::fmt::format(format_args!("if you don\'t care about {0} missing field{1}, you can explicitly ignore {2}",
if len == 1 { "this" } else { "these" },
if len == 1 { "" } else { "s" },
if len == 1 { "it" } else { "them" }))
})format!(
2668"if you don't care about {these} missing field{s}, you can explicitly ignore {them}",
2669 these = pluralize!("this", len),
2670 s = pluralize!(len),
2671 them = if len == 1 { "it" } else { "them" },
2672 ),
2673::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}{1}{2}{3}", prefix,
unmentioned_fields.iter().map(|(_, name)|
{
let field_name = name.to_string();
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}: _", field_name))
})
}).collect::<Vec<_>>().join(", "),
if have_inaccessible_fields { ", .." } else { "" }, postfix))
})format!(
2674"{}{}{}{}",
2675 prefix,
2676 unmentioned_fields
2677 .iter()
2678 .map(|(_, name)| {
2679let field_name = name.to_string();
2680format!("{field_name}: _")
2681 })
2682 .collect::<Vec<_>>()
2683 .join(", "),
2684if have_inaccessible_fields { ", .." } else { "" },
2685 postfix,
2686 ),
2687 Applicability::MachineApplicable,
2688 );
2689err.span_suggestion(
2690sp,
2691"or always ignore missing fields here",
2692::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}..{1}", prefix, postfix))
})format!("{prefix}..{postfix}"),
2693 Applicability::MachineApplicable,
2694 );
2695err2696 }
26972698fn check_pat_box(
2699&self,
2700 span: Span,
2701 inner: &'tcx Pat<'tcx>,
2702 expected: Ty<'tcx>,
2703 pat_info: PatInfo<'tcx>,
2704 ) -> Ty<'tcx> {
2705let tcx = self.tcx;
2706let (box_ty, inner_ty) = self2707 .check_dereferenceable(span, expected, inner)
2708 .and_then(|()| {
2709// Here, `demand::subtype` is good enough, but I don't
2710 // think any errors can be introduced by using `demand::eqtype`.
2711let inner_ty = self.next_ty_var(inner.span);
2712let box_ty = Ty::new_box(tcx, inner_ty);
2713self.demand_eqtype_pat(span, expected, box_ty, &pat_info.top_info)?;
2714Ok((box_ty, inner_ty))
2715 })
2716 .unwrap_or_else(|guar| {
2717let err = Ty::new_error(tcx, guar);
2718 (err, err)
2719 });
2720self.check_pat(inner, inner_ty, pat_info);
2721box_ty2722 }
27232724fn check_pat_deref(
2725&self,
2726 span: Span,
2727 inner: &'tcx Pat<'tcx>,
2728 expected: Ty<'tcx>,
2729 pat_info: PatInfo<'tcx>,
2730 ) -> Ty<'tcx> {
2731let target_ty = self.deref_pat_target(span, expected);
2732self.check_pat(inner, target_ty, pat_info);
2733self.register_deref_mut_bounds_if_needed(span, inner, [expected]);
2734expected2735 }
27362737fn deref_pat_target(&self, span: Span, source_ty: Ty<'tcx>) -> Ty<'tcx> {
2738// Register a `DerefPure` bound, which is required by all `deref!()` pats.
2739let tcx = self.tcx;
2740self.register_bound(
2741source_ty,
2742tcx.require_lang_item(hir::LangItem::DerefPure, span),
2743self.misc(span),
2744 );
2745// The expected type for the deref pat's inner pattern is `<expected as Deref>::Target`.
2746let target_ty = Ty::new_projection(
2747tcx,
2748tcx.require_lang_item(hir::LangItem::DerefTarget, span),
2749 [source_ty],
2750 );
2751let target_ty = self.normalize(span, Unnormalized::new_wip(target_ty));
2752self.resolve_vars_with_obligations(target_ty)
2753 }
27542755/// Check if the interior of a deref pattern (either explicit or implicit) has any `ref mut`
2756 /// bindings, which would require `DerefMut` to be emitted in MIR building instead of just
2757 /// `Deref`. We do this *after* checking the inner pattern, since we want to make sure to
2758 /// account for `ref mut` binding modes inherited from implicitly dereferencing `&mut` refs.
2759fn register_deref_mut_bounds_if_needed(
2760&self,
2761 span: Span,
2762 inner: &'tcx Pat<'tcx>,
2763 derefed_tys: impl IntoIterator<Item = Ty<'tcx>>,
2764 ) {
2765if self.typeck_results.borrow().pat_has_ref_mut_binding(inner) {
2766for mutably_derefed_ty in derefed_tys {
2767self.register_bound(
2768 mutably_derefed_ty,
2769self.tcx.require_lang_item(hir::LangItem::DerefMut, span),
2770self.misc(span),
2771 );
2772 }
2773 }
2774 }
27752776// Precondition: Pat is Ref(inner)
2777fn check_pat_ref(
2778&self,
2779 pat: &'tcx Pat<'tcx>,
2780 inner: &'tcx Pat<'tcx>,
2781 pat_pinned: Pinnedness,
2782 pat_mutbl: Mutability,
2783mut expected: Ty<'tcx>,
2784mut pat_info: PatInfo<'tcx>,
2785 ) -> Ty<'tcx> {
2786let tcx = self.tcx;
27872788let pat_prefix_span =
2789inner.span.find_ancestor_inside(pat.span).map(|end| pat.span.until(end));
27902791let ref_pat_matches_mut_ref = self.ref_pat_matches_mut_ref();
2792if ref_pat_matches_mut_ref && pat_mutbl == Mutability::Not {
2793// If `&` patterns can match against mutable reference types (RFC 3627, Rule 5), we need
2794 // to prevent subpatterns from binding with `ref mut`. Subpatterns of a shared reference
2795 // pattern should have read-only access to the scrutinee, and the borrow checker won't
2796 // catch it in this case.
2797pat_info.max_ref_mutbl = pat_info.max_ref_mutbl.cap_to_weakly_not(pat_prefix_span);
2798 }
27992800expected = self.resolve_vars_with_obligations(expected);
2801// Determine whether we're consuming an inherited reference and resetting the default
2802 // binding mode, based on edition and enabled experimental features.
2803if let ByRef::Yes(inh_pin, inh_mut) = pat_info.binding_mode
2804 && pat_pinned == inh_pin2805 {
2806match self.ref_pat_matches_inherited_ref(pat.span.edition()) {
2807 InheritedRefMatchRule::EatOuter => {
2808// ref pattern attempts to consume inherited reference
2809if pat_mutbl > inh_mut {
2810// Tried to match inherited `ref` with `&mut`
2811 // NB: This assumes that `&` patterns can match against mutable references
2812 // (RFC 3627, Rule 5). If we implement a pattern typing ruleset with Rule 4E
2813 // but not Rule 5, we'll need to check that here.
2814if true {
if !ref_pat_matches_mut_ref {
::core::panicking::panic("assertion failed: ref_pat_matches_mut_ref")
};
};debug_assert!(ref_pat_matches_mut_ref);
2815self.error_inherited_ref_mutability_mismatch(pat, pat_prefix_span);
2816 }
28172818pat_info.binding_mode = ByRef::No;
2819self.typeck_results.borrow_mut().skipped_ref_pats_mut().insert(pat.hir_id);
2820self.check_pat(inner, expected, pat_info);
2821return expected;
2822 }
2823 InheritedRefMatchRule::EatInner => {
2824if let ty::Ref(_, _, r_mutbl) = *expected.kind()
2825 && pat_mutbl <= r_mutbl2826 {
2827// Match against the reference type; don't consume the inherited ref.
2828 // NB: The check for compatible pattern and ref type mutability assumes that
2829 // `&` patterns can match against mutable references (RFC 3627, Rule 5). If
2830 // we implement a pattern typing ruleset with Rule 4 (including the fallback
2831 // to matching the inherited ref when the inner ref can't match) but not
2832 // Rule 5, we'll need to check that here.
2833if true {
if !ref_pat_matches_mut_ref {
::core::panicking::panic("assertion failed: ref_pat_matches_mut_ref")
};
};debug_assert!(ref_pat_matches_mut_ref);
2834// NB: For RFC 3627's Rule 3, we limit the default binding mode's ref
2835 // mutability to `pat_info.max_ref_mutbl`. If we implement a pattern typing
2836 // ruleset with Rule 4 but not Rule 3, we'll need to check that here.
2837if true {
if !self.downgrade_mut_inside_shared() {
::core::panicking::panic("assertion failed: self.downgrade_mut_inside_shared()")
};
};debug_assert!(self.downgrade_mut_inside_shared());
2838let mutbl_cap = cmp::min(r_mutbl, pat_info.max_ref_mutbl.as_mutbl());
2839pat_info.binding_mode = pat_info.binding_mode.cap_ref_mutability(mutbl_cap);
2840 } else {
2841// The reference pattern can't match against the expected type, so try
2842 // matching against the inherited ref instead.
2843if pat_mutbl > inh_mut {
2844// We can't match an inherited shared reference with `&mut`.
2845 // NB: This assumes that `&` patterns can match against mutable
2846 // references (RFC 3627, Rule 5). If we implement a pattern typing
2847 // ruleset with Rule 4 but not Rule 5, we'll need to check that here.
2848 // FIXME(ref_pat_eat_one_layer_2024_structural): If we already tried
2849 // matching the real reference, the error message should explain that
2850 // falling back to the inherited reference didn't work. This should be
2851 // the same error as the old-Edition version below.
2852if true {
if !ref_pat_matches_mut_ref {
::core::panicking::panic("assertion failed: ref_pat_matches_mut_ref")
};
};debug_assert!(ref_pat_matches_mut_ref);
2853self.error_inherited_ref_mutability_mismatch(pat, pat_prefix_span);
2854 }
28552856pat_info.binding_mode = ByRef::No;
2857self.typeck_results.borrow_mut().skipped_ref_pats_mut().insert(pat.hir_id);
2858self.check_pat(inner, expected, pat_info);
2859return expected;
2860 }
2861 }
2862 InheritedRefMatchRule::EatBoth { consider_inherited_ref: true } => {
2863// Reset binding mode on old editions
2864pat_info.binding_mode = ByRef::No;
28652866if let ty::Ref(_, inner_ty, _) = *expected.kind() {
2867// Consume both the inherited and inner references.
2868if pat_mutbl.is_mut() && inh_mut.is_mut() {
2869// As a special case, a `&mut` reference pattern will be able to match
2870 // against a reference type of any mutability if the inherited ref is
2871 // mutable. Since this allows us to match against a shared reference
2872 // type, we refer to this as "falling back" to matching the inherited
2873 // reference, though we consume the real reference as well. We handle
2874 // this here to avoid adding this case to the common logic below.
2875self.check_pat(inner, inner_ty, pat_info);
2876return expected;
2877 } else {
2878// Otherwise, use the common logic below for matching the inner
2879 // reference type.
2880 // FIXME(ref_pat_eat_one_layer_2024_structural): If this results in a
2881 // mutability mismatch, the error message should explain that falling
2882 // back to the inherited reference didn't work. This should be the same
2883 // error as the Edition 2024 version above.
2884}
2885 } else {
2886// The expected type isn't a reference type, so only match against the
2887 // inherited reference.
2888if pat_mutbl > inh_mut {
2889// We can't match a lone inherited shared reference with `&mut`.
2890self.error_inherited_ref_mutability_mismatch(pat, pat_prefix_span);
2891 }
28922893self.typeck_results.borrow_mut().skipped_ref_pats_mut().insert(pat.hir_id);
2894self.check_pat(inner, expected, pat_info);
2895return expected;
2896 }
2897 }
2898 InheritedRefMatchRule::EatBoth { consider_inherited_ref: false } => {
2899// Reset binding mode on stable Rust. This will be a type error below if
2900 // `expected` is not a reference type.
2901pat_info.binding_mode = ByRef::No;
2902self.add_rust_2024_migration_desugared_pat(
2903pat_info.top_info.hir_id,
2904pat,
2905match pat_mutbl {
2906 Mutability::Not => '&', // last char of `&`
2907Mutability::Mut => 't', // last char of `&mut`
2908},
2909inh_mut,
2910 )
2911 }
2912 }
2913 }
29142915let (ref_ty, inner_ty) = match self.check_dereferenceable(pat.span, expected, inner) {
2916Ok(()) => {
2917// `demand::subtype` would be good enough, but using `eqtype` turns
2918 // out to be equally general. See (note_1) for details.
29192920 // Take region, inner-type from expected type if we can,
2921 // to avoid creating needless variables. This also helps with
2922 // the bad interactions of the given hack detailed in (note_1).
2923{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/pat.rs:2923",
"rustc_hir_typeck::pat", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/pat.rs"),
::tracing_core::__macro_support::Option::Some(2923u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::pat"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("check_pat_ref: expected={0:?}",
expected) as &dyn Value))])
});
} else { ; }
};debug!("check_pat_ref: expected={:?}", expected);
2924match expected.maybe_pinned_ref() {
2925Some((r_ty, r_pinned, r_mutbl, _))
2926if ((ref_pat_matches_mut_ref && r_mutbl >= pat_mutbl)
2927 || r_mutbl == pat_mutbl)
2928 && pat_pinned == r_pinned =>
2929 {
2930if r_mutbl == Mutability::Not {
2931pat_info.max_ref_mutbl = MutblCap::Not;
2932 }
2933if r_pinned == Pinnedness::Pinned {
2934pat_info.max_pinnedness = PinnednessCap::Pinned;
2935 }
29362937 (expected, r_ty)
2938 }
2939_ => {
2940let inner_ty = self.next_ty_var(inner.span);
2941let ref_ty = self.new_ref_ty(pat.span, pat_pinned, pat_mutbl, inner_ty);
2942{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/pat.rs:2942",
"rustc_hir_typeck::pat", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/pat.rs"),
::tracing_core::__macro_support::Option::Some(2942u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::pat"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("check_pat_ref: demanding {0:?} = {1:?}",
expected, ref_ty) as &dyn Value))])
});
} else { ; }
};debug!("check_pat_ref: demanding {:?} = {:?}", expected, ref_ty);
2943let err = self.demand_eqtype_pat_diag(
2944pat.span,
2945expected,
2946ref_ty,
2947&pat_info.top_info,
2948 );
29492950// Look for a case like `fn foo(&foo: u32)` and suggest
2951 // `fn foo(foo: &u32)`
2952if let Err(mut err) = err {
2953self.borrow_pat_suggestion(&mut err, pat);
2954err.emit();
2955 }
2956 (ref_ty, inner_ty)
2957 }
2958 }
2959 }
2960Err(guar) => {
2961let err = Ty::new_error(tcx, guar);
2962 (err, err)
2963 }
2964 };
29652966self.check_pat(inner, inner_ty, pat_info);
2967ref_ty2968 }
29692970/// Create a reference or pinned reference type with a fresh region variable.
2971fn new_ref_ty(
2972&self,
2973 span: Span,
2974 pinnedness: Pinnedness,
2975 mutbl: Mutability,
2976 ty: Ty<'tcx>,
2977 ) -> Ty<'tcx> {
2978let region = self.next_region_var(RegionVariableOrigin::PatternRegion(span));
2979let ref_ty = Ty::new_ref(self.tcx, region, ty, mutbl);
2980if pinnedness.is_pinned() {
2981return self.new_pinned_ty(span, ref_ty);
2982 }
2983ref_ty2984 }
29852986/// Create a pinned type.
2987fn new_pinned_ty(&self, span: Span, ty: Ty<'tcx>) -> Ty<'tcx> {
2988Ty::new_adt(
2989self.tcx,
2990self.tcx.adt_def(self.tcx.require_lang_item(LangItem::Pin, span)),
2991self.tcx.mk_args(&[ty.into()]),
2992 )
2993 }
29942995fn error_inherited_ref_mutability_mismatch(
2996&self,
2997 pat: &'tcx Pat<'tcx>,
2998 pat_prefix_span: Option<Span>,
2999 ) -> ErrorGuaranteed {
3000let err_msg = "mismatched types";
3001let err = if let Some(span) = pat_prefix_span {
3002let mut err = self.dcx().struct_span_err(span, err_msg);
3003err.code(E0308);
3004err.note("cannot match inherited `&` with `&mut` pattern");
3005err.span_suggestion_verbose(
3006span,
3007"replace this `&mut` pattern with `&`",
3008"&",
3009 Applicability::MachineApplicable,
3010 );
3011err3012 } else {
3013self.dcx().struct_span_err(pat.span, err_msg)
3014 };
3015err.emit()
3016 }
30173018fn try_resolve_slice_ty_to_array_ty(
3019&self,
3020 before: &'tcx [Pat<'tcx>],
3021 slice: Option<&'tcx Pat<'tcx>>,
3022 span: Span,
3023 ) -> Option<Ty<'tcx>> {
3024if slice.is_some() {
3025return None;
3026 }
30273028let tcx = self.tcx;
3029let len = before.len();
3030let inner_ty = self.next_ty_var(span);
30313032Some(Ty::new_array(tcx, inner_ty, len.try_into().unwrap()))
3033 }
30343035/// Used to determines whether we can infer the expected type in the slice pattern to be of type array.
3036 /// This is only possible if we're in an irrefutable pattern. If we were to allow this in refutable
3037 /// patterns we wouldn't e.g. report ambiguity in the following situation:
3038 ///
3039 /// ```ignore(rust)
3040 /// struct Zeroes;
3041 /// const ARR: [usize; 2] = [0; 2];
3042 /// const ARR2: [usize; 2] = [2; 2];
3043 ///
3044 /// impl Into<&'static [usize; 2]> for Zeroes {
3045 /// fn into(self) -> &'static [usize; 2] {
3046 /// &ARR
3047 /// }
3048 /// }
3049 ///
3050 /// impl Into<&'static [usize]> for Zeroes {
3051 /// fn into(self) -> &'static [usize] {
3052 /// &ARR2
3053 /// }
3054 /// }
3055 ///
3056 /// fn main() {
3057 /// let &[a, b]: &[usize] = Zeroes.into() else {
3058 /// ..
3059 /// };
3060 /// }
3061 /// ```
3062 ///
3063 /// If we're in an irrefutable pattern we prefer the array impl candidate given that
3064 /// the slice impl candidate would be rejected anyway (if no ambiguity existed).
3065fn pat_is_irrefutable(&self, decl_origin: Option<DeclOrigin<'_>>) -> bool {
3066match decl_origin {
3067Some(DeclOrigin::LocalDecl { els: None }) => true,
3068Some(DeclOrigin::LocalDecl { els: Some(_) } | DeclOrigin::LetExpr) | None => false,
3069 }
3070 }
30713072/// Type check a slice pattern.
3073 ///
3074 /// Syntactically, these look like `[pat_0, ..., pat_n]`.
3075 /// Semantically, we are type checking a pattern with structure:
3076 /// ```ignore (not-rust)
3077 /// [before_0, ..., before_n, (slice, after_0, ... after_n)?]
3078 /// ```
3079 /// The type of `slice`, if it is present, depends on the `expected` type.
3080 /// If `slice` is missing, then so is `after_i`.
3081 /// If `slice` is present, it can still represent 0 elements.
3082fn check_pat_slice(
3083&self,
3084 span: Span,
3085 before: &'tcx [Pat<'tcx>],
3086 slice: Option<&'tcx Pat<'tcx>>,
3087 after: &'tcx [Pat<'tcx>],
3088 expected: Ty<'tcx>,
3089 pat_info: PatInfo<'tcx>,
3090 ) -> Ty<'tcx> {
3091let expected = self.resolve_vars_with_obligations(expected);
30923093// If the pattern is irrefutable and `expected` is an infer ty, we try to equate it
3094 // to an array if the given pattern allows it. See issue #76342
3095if self.pat_is_irrefutable(pat_info.decl_origin) && expected.is_ty_var() {
3096if let Some(resolved_arr_ty) =
3097self.try_resolve_slice_ty_to_array_ty(before, slice, span)
3098 {
3099{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/pat.rs:3099",
"rustc_hir_typeck::pat", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/pat.rs"),
::tracing_core::__macro_support::Option::Some(3099u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::pat"),
::tracing_core::field::FieldSet::new(&["resolved_arr_ty"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&resolved_arr_ty)
as &dyn Value))])
});
} else { ; }
};debug!(?resolved_arr_ty);
3100let _ = self.demand_eqtype(span, expected, resolved_arr_ty);
3101 }
3102 }
31033104let expected = self.structurally_resolve_type(span, expected);
3105{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/pat.rs:3105",
"rustc_hir_typeck::pat", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/pat.rs"),
::tracing_core::__macro_support::Option::Some(3105u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::pat"),
::tracing_core::field::FieldSet::new(&["expected"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&expected)
as &dyn Value))])
});
} else { ; }
};debug!(?expected);
31063107let (element_ty, opt_slice_ty, inferred) = match *expected.kind() {
3108// An array, so we might have something like `let [a, b, c] = [0, 1, 2];`.
3109ty::Array(element_ty, len) => {
3110let min = before.len() as u64 + after.len() as u64;
3111let (opt_slice_ty, expected) =
3112self.check_array_pat_len(span, element_ty, expected, slice, len, min);
3113// `opt_slice_ty.is_none()` => `slice.is_none()`.
3114 // Note, though, that opt_slice_ty could be `Some(error_ty)`.
3115if !(opt_slice_ty.is_some() || slice.is_none()) {
::core::panicking::panic("assertion failed: opt_slice_ty.is_some() || slice.is_none()")
};assert!(opt_slice_ty.is_some() || slice.is_none());
3116 (element_ty, opt_slice_ty, expected)
3117 }
3118 ty::Slice(element_ty) => (element_ty, Some(expected), expected),
3119// The expected type must be an array or slice, but was neither, so error.
3120_ => {
3121let guar = expected.error_reported().err().unwrap_or_else(|| {
3122self.error_expected_array_or_slice(span, expected, pat_info)
3123 });
3124let err = Ty::new_error(self.tcx, guar);
3125 (err, Some(err), err)
3126 }
3127 };
31283129// Type check all the patterns before `slice`.
3130for elt in before {
3131self.check_pat(elt, element_ty, pat_info);
3132 }
3133// Type check the `slice`, if present, against its expected type.
3134if let Some(slice) = slice {
3135self.check_pat(slice, opt_slice_ty.unwrap(), pat_info);
3136 }
3137// Type check the elements after `slice`, if present.
3138for elt in after {
3139self.check_pat(elt, element_ty, pat_info);
3140 }
3141inferred3142 }
31433144/// Type check the length of an array pattern.
3145 ///
3146 /// Returns both the type of the variable length pattern (or `None`), and the potentially
3147 /// inferred array type. We only return `None` for the slice type if `slice.is_none()`.
3148fn check_array_pat_len(
3149&self,
3150 span: Span,
3151 element_ty: Ty<'tcx>,
3152 arr_ty: Ty<'tcx>,
3153 slice: Option<&'tcx Pat<'tcx>>,
3154 len: ty::Const<'tcx>,
3155 min_len: u64,
3156 ) -> (Option<Ty<'tcx>>, Ty<'tcx>) {
3157let len = self.try_structurally_resolve_const(span, len).try_to_target_usize(self.tcx);
31583159let guar = if let Some(len) = len {
3160// Now we know the length...
3161if slice.is_none() {
3162// ...and since there is no variable-length pattern,
3163 // we require an exact match between the number of elements
3164 // in the array pattern and as provided by the matched type.
3165if min_len == len {
3166return (None, arr_ty);
3167 }
31683169self.error_scrutinee_inconsistent_length(span, min_len, len)
3170 } else if let Some(pat_len) = len.checked_sub(min_len) {
3171// The variable-length pattern was there,
3172 // so it has an array type with the remaining elements left as its size...
3173return (Some(Ty::new_array(self.tcx, element_ty, pat_len)), arr_ty);
3174 } else {
3175// ...however, in this case, there were no remaining elements.
3176 // That is, the slice pattern requires more than the array type offers.
3177self.error_scrutinee_with_rest_inconsistent_length(span, min_len, len)
3178 }
3179 } else if slice.is_none() {
3180// We have a pattern with a fixed length,
3181 // which we can use to infer the length of the array.
3182let updated_arr_ty = Ty::new_array(self.tcx, element_ty, min_len);
3183self.demand_eqtype(span, updated_arr_ty, arr_ty);
3184return (None, updated_arr_ty);
3185 } else {
3186// We have a variable-length pattern and don't know the array length.
3187 // This happens if we have e.g.,
3188 // `let [a, b, ..] = arr` where `arr: [T; N]` where `const N: usize`.
3189self.error_scrutinee_unfixed_length(span)
3190 };
31913192// If we get here, we must have emitted an error.
3193(Some(Ty::new_error(self.tcx, guar)), arr_ty)
3194 }
31953196fn error_scrutinee_inconsistent_length(
3197&self,
3198 span: Span,
3199 min_len: u64,
3200 size: u64,
3201 ) -> ErrorGuaranteed {
3202{
self.dcx().struct_span_err(span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("pattern requires {0} element{1} but array has {2}",
min_len, if min_len == 1 { "" } else { "s" }, size))
})).with_code(E0527)
}struct_span_code_err!(
3203self.dcx(),
3204 span,
3205 E0527,
3206"pattern requires {} element{} but array has {}",
3207 min_len,
3208pluralize!(min_len),
3209 size,
3210 )3211 .with_span_label(span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("expected {0} element{1}", size,
if size == 1 { "" } else { "s" }))
})format!("expected {} element{}", size, pluralize!(size)))
3212 .emit()
3213 }
32143215fn error_scrutinee_with_rest_inconsistent_length(
3216&self,
3217 span: Span,
3218 min_len: u64,
3219 size: u64,
3220 ) -> ErrorGuaranteed {
3221{
self.dcx().struct_span_err(span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("pattern requires at least {0} element{1} but array has {2}",
min_len, if min_len == 1 { "" } else { "s" }, size))
})).with_code(E0528)
}struct_span_code_err!(
3222self.dcx(),
3223 span,
3224 E0528,
3225"pattern requires at least {} element{} but array has {}",
3226 min_len,
3227pluralize!(min_len),
3228 size,
3229 )3230 .with_span_label(
3231span,
3232::alloc::__export::must_use({
::alloc::fmt::format(format_args!("pattern cannot match array of {0} element{1}",
size, if size == 1 { "" } else { "s" }))
})format!("pattern cannot match array of {} element{}", size, pluralize!(size),),
3233 )
3234 .emit()
3235 }
32363237fn error_scrutinee_unfixed_length(&self, span: Span) -> ErrorGuaranteed {
3238{
self.dcx().struct_span_err(span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("cannot pattern-match on an array without a fixed length"))
})).with_code(E0730)
}struct_span_code_err!(
3239self.dcx(),
3240 span,
3241 E0730,
3242"cannot pattern-match on an array without a fixed length",
3243 )3244 .emit()
3245 }
32463247fn error_expected_array_or_slice(
3248&self,
3249 span: Span,
3250 expected_ty: Ty<'tcx>,
3251 pat_info: PatInfo<'tcx>,
3252 ) -> ErrorGuaranteed {
3253let PatInfo { top_info: ti, current_depth, .. } = pat_info;
32543255let mut slice_pat_semantics = false;
3256let mut as_deref = None;
3257let mut slicing = None;
3258if let ty::Ref(_, ty, _) = expected_ty.kind()
3259 && let ty::Array(..) | ty::Slice(..) = ty.kind()
3260 {
3261slice_pat_semantics = true;
3262 } else if self3263 .autoderef(span, expected_ty)
3264 .silence_errors()
3265 .any(|(ty, _)| #[allow(non_exhaustive_omitted_patterns)] match ty.kind() {
ty::Slice(..) | ty::Array(..) => true,
_ => false,
}matches!(ty.kind(), ty::Slice(..) | ty::Array(..)))
3266 && let Some(span) = ti.span
3267 && let Some(_) = ti.origin_expr
3268 {
3269let resolved_ty = self.resolve_vars_if_possible(ti.expected);
3270let (is_slice_or_array_or_vector, resolved_ty) =
3271self.is_slice_or_array_or_vector(resolved_ty);
3272match resolved_ty.kind() {
3273 ty::Adt(adt_def, _)
3274if self.tcx.is_diagnostic_item(sym::Option, adt_def.did())
3275 || self.tcx.is_diagnostic_item(sym::Result, adt_def.did()) =>
3276 {
3277// Slicing won't work here, but `.as_deref()` might (issue #91328).
3278as_deref = Some(diagnostics::AsDerefSuggestion { span: span.shrink_to_hi() });
3279 }
3280_ => (),
3281 }
32823283let is_top_level = current_depth <= 1;
3284if is_slice_or_array_or_vector && is_top_level {
3285slicing = Some(diagnostics::SlicingSuggestion { span: span.shrink_to_hi() });
3286 }
3287 }
3288self.dcx().emit_err(diagnostics::ExpectedArrayOrSlice {
3289span,
3290 ty: expected_ty,
3291slice_pat_semantics,
3292as_deref,
3293slicing,
3294 })
3295 }
32963297fn is_slice_or_array_or_vector(&self, ty: Ty<'tcx>) -> (bool, Ty<'tcx>) {
3298match ty.kind() {
3299 ty::Adt(adt_def, _) if self.tcx.is_diagnostic_item(sym::Vec, adt_def.did()) => {
3300 (true, ty)
3301 }
3302 ty::Ref(_, ty, _) => self.is_slice_or_array_or_vector(*ty),
3303 ty::Slice(..) | ty::Array(..) => (true, ty),
3304_ => (false, ty),
3305 }
3306 }
33073308/// Record a pattern that's invalid under Rust 2024 match ergonomics, along with a problematic
3309 /// span, so that the pattern migration lint can desugar it during THIR construction.
3310fn add_rust_2024_migration_desugared_pat(
3311&self,
3312 pat_id: HirId,
3313 subpat: &'tcx Pat<'tcx>,
3314 final_char: char,
3315 def_br_mutbl: Mutability,
3316 ) {
3317// Try to trim the span we're labeling to just the `&` or binding mode that's an issue.
3318let from_expansion = subpat.span.from_expansion();
3319let trimmed_span = if from_expansion {
3320// If the subpattern is from an expansion, highlight the whole macro call instead.
3321subpat.span
3322 } else {
3323let trimmed = self.tcx.sess.source_map().span_through_char(subpat.span, final_char);
3324// The edition of the trimmed span should be the same as `subpat.span`; this will be a
3325 // a hard error if the subpattern is of edition >= 2024. We set it manually to be sure:
3326trimmed.with_ctxt(subpat.span.ctxt())
3327 };
33283329let mut typeck_results = self.typeck_results.borrow_mut();
3330let mut table = typeck_results.rust_2024_migration_desugared_pats_mut();
3331// FIXME(ref_pat_eat_one_layer_2024): The migration diagnostic doesn't know how to track the
3332 // default binding mode in the presence of Rule 3 or Rule 5. As a consequence, the labels it
3333 // gives for default binding modes are wrong, as well as suggestions based on the default
3334 // binding mode. This keeps it from making those suggestions, as doing so could panic.
3335let info = table.entry(pat_id).or_insert_with(|| ty::Rust2024IncompatiblePatInfo {
3336 primary_labels: Vec::new(),
3337 bad_ref_modifiers: false,
3338 bad_mut_modifiers: false,
3339 bad_ref_pats: false,
3340 suggest_eliding_modes: !self.tcx.features().ref_pat_eat_one_layer_2024()
3341 && !self.tcx.features().ref_pat_eat_one_layer_2024_structural(),
3342 });
33433344let pat_kind = if let PatKind::Binding(user_bind_annot, _, _, _) = subpat.kind {
3345// If the user-provided binding modifier doesn't match the default binding mode, we'll
3346 // need to suggest reference patterns, which can affect other bindings.
3347 // For simplicity, we opt to suggest making the pattern fully explicit.
3348info.suggest_eliding_modes &= #[allow(non_exhaustive_omitted_patterns)] match user_bind_annot {
BindingMode(ByRef::Yes(_, mutbl), Mutability::Not) if
mutbl == def_br_mutbl => true,
_ => false,
}matches!(
3349 user_bind_annot,
3350 BindingMode(ByRef::Yes(_, mutbl), Mutability::Not) if mutbl == def_br_mutbl
3351 );
3352if user_bind_annot == BindingMode(ByRef::No, Mutability::Mut) {
3353info.bad_mut_modifiers = true;
3354"`mut` binding modifier"
3355} else {
3356info.bad_ref_modifiers = true;
3357match user_bind_annot.1 {
3358 Mutability::Not => "explicit `ref` binding modifier",
3359 Mutability::Mut => "explicit `ref mut` binding modifier",
3360 }
3361 }
3362 } else {
3363info.bad_ref_pats = true;
3364// For simplicity, we don't try to suggest eliding reference patterns. Thus, we'll
3365 // suggest adding them instead, which can affect the types assigned to bindings.
3366 // As such, we opt to suggest making the pattern fully explicit.
3367info.suggest_eliding_modes = false;
3368"reference pattern"
3369};
3370// Only provide a detailed label if the problematic subpattern isn't from an expansion.
3371 // In the case that it's from a macro, we'll add a more detailed note in the emitter.
3372let primary_label = if from_expansion {
3373// We can't suggest eliding modifiers within expansions.
3374info.suggest_eliding_modes = false;
3375// NB: This wording assumes the only expansions that can produce problematic reference
3376 // patterns and bindings are macros. If a desugaring or AST pass is added that can do
3377 // so, we may want to inspect the span's source callee or macro backtrace.
3378"occurs within macro expansion".to_owned()
3379 } else {
3380::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} not allowed when implicitly borrowing",
pat_kind))
})format!("{pat_kind} not allowed when implicitly borrowing")3381 };
3382info.primary_labels.push((trimmed_span, primary_label));
3383 }
3384}