Skip to main content

rustc_mir_build/thir/pattern/
const_to_pat.rs

1use core::ops::ControlFlow;
2
3use rustc_abi::{FieldIdx, VariantIdx};
4use rustc_apfloat::Float;
5use rustc_data_structures::fx::FxHashSet;
6use rustc_errors::{Diag, msg};
7use rustc_hir as hir;
8use rustc_hir::find_attr;
9use rustc_index::Idx;
10use rustc_infer::infer::TyCtxtInferExt;
11use rustc_infer::traits::Obligation;
12use rustc_middle::mir::interpret::ErrorHandled;
13use rustc_middle::span_bug;
14use rustc_middle::thir::{FieldPat, Pat, PatKind};
15use rustc_middle::ty::{
16    self, Ty, TyCtxt, TypeSuperVisitable, TypeVisitableExt, TypeVisitor, Unnormalized,
17};
18use rustc_span::def_id::DefId;
19use rustc_span::{DUMMY_SP, Span};
20use rustc_trait_selection::error_reporting::traits::ambiguity::{
21    CandidateSource, compute_applicable_impls_for_diagnostics,
22};
23use rustc_trait_selection::traits::ObligationCause;
24use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt;
25use tracing::{debug, instrument, trace};
26
27use super::PatCtxt;
28use crate::diagnostics::{
29    ConstPatternDependsOnGenericParameter, CouldNotEvalConstPattern, InvalidPattern, NaNPattern,
30    PointerPattern, SuggestEq, TypeNotPartialEq, TypeNotStructural, UnionPattern, UnsizedPattern,
31};
32
33impl<'tcx, 'ptcx> PatCtxt<'tcx, 'ptcx> {
34    /// Converts a constant to a pattern (if possible).
35    /// This means aggregate values (like structs and enums) are converted
36    /// to a pattern that matches the value (as if you'd compared via structural equality).
37    ///
38    /// Only type system constants are supported, as we are using valtrees
39    /// as an intermediate step. Unfortunately those don't carry a type
40    /// so we have to carry one ourselves.
41    x;#[instrument(level = "debug", skip(self), ret)]
42    pub(super) fn const_to_pat(
43        &self,
44        c: ty::Const<'tcx>,
45        ty: Ty<'tcx>,
46        id: hir::HirId,
47        span: Span,
48    ) -> Box<Pat<'tcx>> {
49        let mut convert = ConstToPat::new(self, id, span, c);
50
51        match c.kind() {
52            ty::ConstKind::Unevaluated(uv) => convert.unevaluated_to_pat(uv, ty),
53            ty::ConstKind::Value(value) => convert.valtree_to_pat(value),
54            _ => span_bug!(span, "Invalid `ConstKind` for `const_to_pat`: {:?}", c),
55        }
56    }
57}
58
59struct ConstToPat<'tcx> {
60    tcx: TyCtxt<'tcx>,
61    typing_env: ty::TypingEnv<'tcx>,
62    span: Span,
63    id: hir::HirId,
64
65    c: ty::Const<'tcx>,
66}
67
68impl<'tcx> ConstToPat<'tcx> {
69    fn new(pat_ctxt: &PatCtxt<'tcx, '_>, id: hir::HirId, span: Span, c: ty::Const<'tcx>) -> Self {
70        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs:70",
                        "rustc_mir_build::thir::pattern::const_to_pat",
                        ::tracing::Level::TRACE,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs"),
                        ::tracing_core::__macro_support::Option::Some(70u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_mir_build::thir::pattern::const_to_pat"),
                        ::tracing_core::field::FieldSet::new(&["pat_ctxt.typeck_results.hir_owner"],
                            ::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(&debug(&pat_ctxt.typeck_results.hir_owner)
                                            as &dyn Value))])
            });
    } else { ; }
};trace!(?pat_ctxt.typeck_results.hir_owner);
71        ConstToPat { tcx: pat_ctxt.tcx, typing_env: pat_ctxt.typing_env, span, id, c }
72    }
73
74    fn type_marked_structural(&self, ty: Ty<'tcx>) -> bool {
75        ty.is_structural_eq_shallow(self.tcx)
76    }
77
78    /// We errored. Signal that in the pattern, so that follow up errors can be silenced.
79    fn mk_err(&self, mut err: Diag<'_>, ty: Ty<'tcx>) -> Box<Pat<'tcx>> {
80        if let ty::ConstKind::Unevaluated(uv) = self.c.kind() {
81            if let ty::UnevaluatedConstKind::Projection { def_id }
82            | ty::UnevaluatedConstKind::Inherent { def_id } = uv.kind
83                && let Some(def_id) = def_id.as_local()
84            {
85                // Include the container item in the output.
86                err.span_label(self.tcx.def_span(self.tcx.local_parent(def_id)), "");
87            }
88            if let ty::UnevaluatedConstKind::Projection { def_id }
89            | ty::UnevaluatedConstKind::Inherent { def_id }
90            | ty::UnevaluatedConstKind::Free { def_id } = uv.kind
91            {
92                err.span_label(self.tcx.def_span(def_id), rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("constant defined here"))msg!("constant defined here"));
93            }
94        }
95        Box::new(Pat { span: self.span, ty, kind: PatKind::Error(err.emit()), extra: None })
96    }
97
98    fn unevaluated_to_pat(
99        &mut self,
100        uv: ty::UnevaluatedConst<'tcx>,
101        ty: Ty<'tcx>,
102    ) -> Box<Pat<'tcx>> {
103        // It's not *technically* correct to be revealing opaque types here as borrowcheck has
104        // not run yet. However, CTFE itself uses `TypingMode::PostAnalysis` unconditionally even
105        // during typeck and not doing so has a lot of (undesirable) fallout (#101478, #119821).
106        // As a result we always use a revealed env when resolving the instance to evaluate.
107        //
108        // FIXME: `const_eval_resolve_for_typeck` should probably just modify the env itself
109        // instead of having this logic here
110        let typing_env =
111            self.tcx.erase_and_anonymize_regions(self.typing_env).with_codegen_normalized(self.tcx);
112        let uv = self.tcx.erase_and_anonymize_regions(uv);
113
114        // FIXME(gca): This will become insufficient once associated constants can be
115        // implemented as `type` consts (project-const-generics#76). At that point it'll
116        // become necessary to just use type system normalization for all const patterns
117        // but that's not yet possible.
118        let mut thir_pat = if uv.kind.is_type_const(self.tcx) {
119            let Ok(normalize) = self
120                .tcx
121                .try_normalize_erasing_regions(self.typing_env, Unnormalized::new_wip(self.c))
122            else {
123                let err = self.tcx.dcx().create_err(CouldNotEvalConstPattern { span: self.span });
124                return self.mk_err(err, ty);
125            };
126
127            let ty::ConstKind::Value(value) = normalize.kind() else {
128                let err = self.tcx.dcx().create_err(CouldNotEvalConstPattern { span: self.span });
129                return self.mk_err(err, ty);
130            };
131            self.valtree_to_pat(value)
132        } else {
133            // try to resolve e.g. associated constants to their definition on an impl, and then
134            // evaluate the const.
135            let valtree = match self.tcx.const_eval_resolve_for_typeck(typing_env, uv, self.span) {
136                Ok(Ok(c)) => c,
137                Err(ErrorHandled::Reported(_, _)) => {
138                    // Let's tell the use where this failing const occurs.
139                    let mut err =
140                        self.tcx.dcx().create_err(CouldNotEvalConstPattern { span: self.span });
141                    // We've emitted an error on the original const, it would be redundant to complain
142                    // on its use as well.
143                    if let ty::ConstKind::Unevaluated(uv) = self.c.kind()
144                        && let ty::UnevaluatedConstKind::Projection { .. }
145                        | ty::UnevaluatedConstKind::Inherent { .. }
146                        | ty::UnevaluatedConstKind::Free { .. } = uv.kind
147                    {
148                        err.downgrade_to_delayed_bug();
149                    }
150                    return self.mk_err(err, ty);
151                }
152                Err(ErrorHandled::TooGeneric(_)) => {
153                    let mut e = self
154                        .tcx
155                        .dcx()
156                        .create_err(ConstPatternDependsOnGenericParameter { span: self.span });
157                    for arg in uv.args {
158                        if let ty::GenericArgKind::Type(ty) = arg.kind()
159                            && let ty::Param(param_ty) = ty.kind()
160                        {
161                            let def_id = self.tcx.hir_enclosing_body_owner(self.id);
162                            let generics = self.tcx.generics_of(def_id);
163                            let param = generics.type_param(*param_ty, self.tcx);
164                            let span = self.tcx.def_span(param.def_id);
165                            e.span_label(span, "constant depends on this generic parameter");
166                            if let Some(ident) = self.tcx.def_ident_span(def_id)
167                                && self.tcx.sess.source_map().is_multiline(ident.between(span))
168                            {
169                                // Display the `fn` name as well in the diagnostic, as the generic isn't
170                                // in the same line and it could be confusing otherwise.
171                                e.span_label(ident, "");
172                            }
173                        }
174                    }
175                    return self.mk_err(e, ty);
176                }
177                Ok(Err(bad_ty)) => {
178                    // The pattern cannot be turned into a valtree.
179                    let e = match bad_ty.kind() {
180                        ty::Adt(def, ..) => {
181                            if !def.is_union() {
    ::core::panicking::panic("assertion failed: def.is_union()")
};assert!(def.is_union());
182                            self.tcx.dcx().create_err(UnionPattern { span: self.span })
183                        }
184                        ty::FnPtr(..) | ty::RawPtr(..) => {
185                            self.tcx.dcx().create_err(PointerPattern { span: self.span })
186                        }
187                        _ => self.tcx.dcx().create_err(InvalidPattern {
188                            span: self.span,
189                            non_sm_ty: bad_ty,
190                            prefix: bad_ty.prefix_string(self.tcx).to_string(),
191                        }),
192                    };
193                    return self.mk_err(e, ty);
194                }
195            };
196
197            // Lower the valtree to a THIR pattern.
198            self.valtree_to_pat(ty::Value { ty, valtree })
199        };
200
201        if !thir_pat.references_error() {
202            // Always check for `PartialEq` if we had no other errors yet.
203            if !type_has_partial_eq_impl(self.tcx, typing_env, ty).has_impl {
204                let mut err = self.tcx.dcx().create_err(TypeNotPartialEq { span: self.span, ty });
205                extend_type_not_partial_eq(self.tcx, typing_env, ty, &mut err);
206                return self.mk_err(err, ty);
207            }
208        }
209
210        // Mark the pattern to indicate that it is the result of lowering a named
211        // constant. This is used for diagnostics.
212        thir_pat.extra.get_or_insert_default().expanded_const = uv.kind.opt_def_id();
213        thir_pat
214    }
215
216    fn lower_field_values_to_fieldpats(
217        &self,
218        values: impl Iterator<Item = ty::Value<'tcx>>,
219    ) -> Vec<FieldPat<'tcx>> {
220        values
221            .enumerate()
222            .map(|(index, value)| FieldPat {
223                field: FieldIdx::new(index),
224                pattern: *self.valtree_to_pat(value),
225            })
226            .collect()
227    }
228
229    // Recursive helper for `to_pat`; invoke that (instead of calling this directly).
230    #[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("valtree_to_pat",
                                    "rustc_mir_build::thir::pattern::const_to_pat",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs"),
                                    ::tracing_core::__macro_support::Option::Some(230u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_mir_build::thir::pattern::const_to_pat"),
                                    ::tracing_core::field::FieldSet::new(&["value"],
                                        ::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(&value)
                                                            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: Box<Pat<'tcx>> = loop {};
            return __tracing_attr_fake_return;
        }
        {
            let span = self.span;
            let tcx = self.tcx;
            let ty::Value { ty, valtree } = value;
            let kind =
                match ty.kind() {
                    ty::Adt(adt_def, _) if !self.type_marked_structural(ty) => {
                        {
                            use ::tracing::__macro_support::Callsite as _;
                            static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                                {
                                    static META: ::tracing::Metadata<'static> =
                                        {
                                            ::tracing_core::metadata::Metadata::new("event compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs:242",
                                                "rustc_mir_build::thir::pattern::const_to_pat",
                                                ::tracing::Level::DEBUG,
                                                ::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs"),
                                                ::tracing_core::__macro_support::Option::Some(242u32),
                                                ::tracing_core::__macro_support::Option::Some("rustc_mir_build::thir::pattern::const_to_pat"),
                                                ::tracing_core::field::FieldSet::new(&["message", "adt_def",
                                                                "value.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(&format_args!("ADT type in pattern is not `type_marked_structural`")
                                                                    as &dyn Value)),
                                                        (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                            ::tracing::__macro_support::Option::Some(&debug(&adt_def) as
                                                                    &dyn Value)),
                                                        (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                            ::tracing::__macro_support::Option::Some(&debug(&value.ty)
                                                                    as &dyn Value))])
                                    });
                            } else { ; }
                        };
                        let PartialEqImplStatus {
                                is_derived,
                                possibly_inapplicable_structural_partial_eq,
                                non_blanket_impl,
                                possibly_inapplicable_derived_partial_eq,
                                has_impl, .. } =
                            type_has_partial_eq_impl(self.tcx, self.typing_env, ty);
                        if possibly_inapplicable_derived_partial_eq && !has_impl {
                            let mut err =
                                self.tcx.dcx().create_err(TypeNotPartialEq {
                                        span: self.span,
                                        ty,
                                    });
                            extend_type_not_partial_eq(self.tcx, self.typing_env, ty,
                                &mut err);
                            return self.mk_err(err, ty);
                        }
                        let (manual_partialeq_impl_span,
                                manual_partialeq_impl_note) =
                            match (possibly_inapplicable_structural_partial_eq,
                                    non_blanket_impl) {
                                (true, _) => (None, false),
                                (_, Some(def_id)) if def_id.is_local() && !is_derived => {
                                    (Some(tcx.def_span(def_id)), false)
                                }
                                _ => (None, true),
                            };
                        let manual_partialeq_impl =
                            manual_partialeq_impl_note ||
                                manual_partialeq_impl_span.is_some();
                        let is_local = adt_def.did().is_local();
                        let ty_def_span = tcx.def_span(adt_def.did());
                        let suggestion =
                            if let Ok(name) =
                                        tcx.sess.source_map().span_to_snippet(self.span) &&
                                    (is_local || manual_partialeq_impl) {
                                let mut hir_id = self.id;
                                while let hir::Node::Pat(pat) = tcx.parent_hir_node(hir_id)
                                    {
                                    hir_id = pat.hir_id;
                                }
                                match tcx.parent_hir_node(hir_id) {
                                    hir::Node::Arm(hir::Arm { pat, guard: None, .. }) => {
                                        Some(SuggestEq::AddIf {
                                                if_span: pat.span.shrink_to_hi(),
                                                pat_span: self.span,
                                                name,
                                                ty,
                                                manual_partialeq_impl,
                                            })
                                    }
                                    hir::Node::Arm(hir::Arm { guard: Some(guard), .. }) => {
                                        Some(SuggestEq::AddToIf {
                                                span: guard.span.shrink_to_hi(),
                                                pat_span: self.span,
                                                name,
                                                ty,
                                                manual_partialeq_impl,
                                            })
                                    }
                                    hir::Node::Expr(hir::Expr {
                                        kind: hir::ExprKind::Let(let_expr), span, .. }) => {
                                        if let_expr.pat.span == self.span {
                                            Some(SuggestEq::ReplaceWithEq {
                                                    removal: span.until(self.span),
                                                    eq: self.span.between(let_expr.init.span),
                                                    ty,
                                                    manual_partialeq_impl,
                                                })
                                        } else if tcx.sess.edition().at_least_rust_2024() {
                                            Some(SuggestEq::AddToLetChain {
                                                    span: span.shrink_to_hi(),
                                                    pat_span: self.span,
                                                    name,
                                                    ty,
                                                    manual_partialeq_impl,
                                                })
                                        } else { None }
                                    }
                                    hir::Node::LetStmt(let_stmt) if
                                        let Some(init) = let_stmt.init &&
                                                    let Some(els) = let_stmt.els && init.span.ctxt().is_root()
                                            && els.span.ctxt().is_root() => {
                                        Some(SuggestEq::ReplaceLetElseWithIf {
                                                if_span: let_stmt.span.until(let_stmt.pat.span),
                                                eq: let_stmt.pat.span.between(init.span),
                                                else_span: init.span.between(els.span),
                                                ty,
                                                manual_partialeq_impl,
                                            })
                                    }
                                    _ => None,
                                }
                            } else { None };
                        let err =
                            TypeNotStructural {
                                span,
                                ty,
                                ty_def_span,
                                manual_partialeq_impl_span,
                                manual_partialeq_impl_note,
                                is_local,
                                suggestion,
                            };
                        return self.mk_err(tcx.dcx().create_err(err), ty);
                    }
                    ty::Adt(adt_def, args) if adt_def.is_enum() => {
                        let (&variant_index, fields) =
                            valtree.to_branch().split_first().unwrap();
                        let variant_index =
                            VariantIdx::from_u32(variant_index.to_leaf().to_u32());
                        PatKind::Variant {
                            adt_def: *adt_def,
                            args,
                            variant_index,
                            subpatterns: self.lower_field_values_to_fieldpats(fields.iter().map(|ct|
                                        ct.to_value())),
                        }
                    }
                    ty::Adt(def, _) => {
                        if !!def.is_union() {
                            ::core::panicking::panic("assertion failed: !def.is_union()")
                        };
                        PatKind::Leaf {
                            subpatterns: self.lower_field_values_to_fieldpats(valtree.to_branch().iter().map(|ct|
                                        ct.to_value())),
                        }
                    }
                    ty::Tuple(_) =>
                        PatKind::Leaf {
                            subpatterns: self.lower_field_values_to_fieldpats(valtree.to_branch().iter().map(|ct|
                                        ct.to_value())),
                        },
                    ty::Slice(_) =>
                        PatKind::Slice {
                            prefix: valtree.to_branch().iter().map(|val|
                                        *self.valtree_to_pat(val.to_value())).collect(),
                            slice: None,
                            suffix: Box::new([]),
                        },
                    ty::Array(_, _) =>
                        PatKind::Array {
                            prefix: valtree.to_branch().iter().map(|val|
                                        *self.valtree_to_pat(val.to_value())).collect(),
                            slice: None,
                            suffix: Box::new([]),
                        },
                    ty::Str => { PatKind::Constant { value } }
                    ty::Ref(_, pointee_ty, ..) => {
                        if pointee_ty.is_str() || pointee_ty.is_slice() ||
                                pointee_ty.is_sized(tcx, self.typing_env) {
                            PatKind::Deref {
                                pin: hir::Pinnedness::Not,
                                subpattern: self.valtree_to_pat(ty::Value {
                                        ty: *pointee_ty,
                                        valtree,
                                    }),
                            }
                        } else {
                            return self.mk_err(tcx.dcx().create_err(UnsizedPattern {
                                            span,
                                            non_sm_ty: *pointee_ty,
                                        }), ty);
                        }
                    }
                    ty::Float(flt) => {
                        let v = valtree.to_leaf();
                        let is_nan =
                            match flt {
                                ty::FloatTy::F16 => v.to_f16().is_nan(),
                                ty::FloatTy::F32 => v.to_f32().is_nan(),
                                ty::FloatTy::F64 => v.to_f64().is_nan(),
                                ty::FloatTy::F128 => v.to_f128().is_nan(),
                            };
                        if is_nan {
                            return self.mk_err(tcx.dcx().create_err(NaNPattern {
                                            span,
                                        }), ty);
                        } else { PatKind::Constant { value } }
                    }
                    ty::Pat(..) | ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_)
                        | ty::RawPtr(..) => {
                        PatKind::Constant { value }
                    }
                    ty::FnPtr(..) => {
                        {
                            ::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
                                    format_args!("Valtree construction would never succeed for FnPtr, so this is unreachable.")));
                        }
                    }
                    _ => {
                        let err =
                            InvalidPattern {
                                span,
                                non_sm_ty: ty,
                                prefix: ty.prefix_string(tcx).to_string(),
                            };
                        return self.mk_err(tcx.dcx().create_err(err), ty);
                    }
                };
            Box::new(Pat { span, ty, kind, extra: None })
        }
    }
}#[instrument(skip(self), level = "debug")]
231    fn valtree_to_pat(&self, value: ty::Value<'tcx>) -> Box<Pat<'tcx>> {
232        let span = self.span;
233        let tcx = self.tcx;
234        let ty::Value { ty, valtree } = value;
235
236        let kind = match ty.kind() {
237            // Extremely important check for all ADTs!
238            // Make sure they are eligible to be used in patterns (structural), and if not, emit an
239            // error.
240            ty::Adt(adt_def, _) if !self.type_marked_structural(ty) => {
241                // This ADT cannot be used as a constant in patterns.
242                debug!(?adt_def, ?value.ty, "ADT type in pattern is not `type_marked_structural`");
243                let PartialEqImplStatus {
244                    is_derived,
245                    possibly_inapplicable_structural_partial_eq,
246                    non_blanket_impl,
247                    possibly_inapplicable_derived_partial_eq,
248                    has_impl,
249                    ..
250                } = type_has_partial_eq_impl(self.tcx, self.typing_env, ty);
251
252                // If we have a derived PartialEq impl but it does not apply,
253                // then error about that instead, because `TypeNotStructural` gives advice that is
254                // relevant only when the problem is that `ty` does not derive `PartialEq`.
255                //
256                // Note that this is a duplicate of a check in `unevaluated_to_pat()`,
257                // which we would run later if we weren’t emitting an error now.
258                if possibly_inapplicable_derived_partial_eq && !has_impl {
259                    let mut err =
260                        self.tcx.dcx().create_err(TypeNotPartialEq { span: self.span, ty });
261                    extend_type_not_partial_eq(self.tcx, self.typing_env, ty, &mut err);
262                    return self.mk_err(err, ty);
263                }
264
265                let (manual_partialeq_impl_span, manual_partialeq_impl_note) =
266                    match (possibly_inapplicable_structural_partial_eq, non_blanket_impl) {
267                        (true, _) => (None, false),
268                        (_, Some(def_id)) if def_id.is_local() && !is_derived => {
269                            (Some(tcx.def_span(def_id)), false)
270                        }
271                        _ => (None, true),
272                    };
273                let manual_partialeq_impl =
274                    manual_partialeq_impl_note || manual_partialeq_impl_span.is_some();
275                let is_local = adt_def.did().is_local();
276                let ty_def_span = tcx.def_span(adt_def.did());
277                let suggestion = if let Ok(name) = tcx.sess.source_map().span_to_snippet(self.span)
278                    && (is_local || manual_partialeq_impl)
279                {
280                    let mut hir_id = self.id;
281                    while let hir::Node::Pat(pat) = tcx.parent_hir_node(hir_id) {
282                        hir_id = pat.hir_id;
283                    }
284                    match tcx.parent_hir_node(hir_id) {
285                        hir::Node::Arm(hir::Arm { pat, guard: None, .. }) => {
286                            // Add an if condition to the match arm.
287                            Some(SuggestEq::AddIf {
288                                if_span: pat.span.shrink_to_hi(),
289                                pat_span: self.span,
290                                name,
291                                ty,
292                                manual_partialeq_impl,
293                            })
294                        }
295                        hir::Node::Arm(hir::Arm { guard: Some(guard), .. }) => {
296                            // Modify the the match arm if condition and add a check for equality.
297                            Some(SuggestEq::AddToIf {
298                                span: guard.span.shrink_to_hi(),
299                                pat_span: self.span,
300                                name,
301                                ty,
302                                manual_partialeq_impl,
303                            })
304                        }
305                        hir::Node::Expr(hir::Expr {
306                            kind: hir::ExprKind::Let(let_expr),
307                            span,
308                            ..
309                        }) => {
310                            if let_expr.pat.span == self.span {
311                                // `if let CONST = expr` -> `if CONST == expr`.
312                                Some(SuggestEq::ReplaceWithEq {
313                                    removal: span.until(self.span),
314                                    eq: self.span.between(let_expr.init.span),
315                                    ty,
316                                    manual_partialeq_impl,
317                                })
318                            } else if tcx.sess.edition().at_least_rust_2024() {
319                                // `if let Some(CONST) = expr` ->
320                                // `if let Some(binding) = expr && binding == CONST`.
321                                Some(SuggestEq::AddToLetChain {
322                                    span: span.shrink_to_hi(),
323                                    pat_span: self.span,
324                                    name,
325                                    ty,
326                                    manual_partialeq_impl,
327                                })
328                            } else {
329                                None
330                            }
331                        }
332                        hir::Node::LetStmt(let_stmt)
333                            if let Some(init) = let_stmt.init
334                                && let Some(els) = let_stmt.els
335                                && init.span.ctxt().is_root()
336                                && els.span.ctxt().is_root() =>
337                        {
338                            // `let PAT = expr else {` -> `if PAT == expr {`.
339                            Some(SuggestEq::ReplaceLetElseWithIf {
340                                if_span: let_stmt.span.until(let_stmt.pat.span),
341                                eq: let_stmt.pat.span.between(init.span),
342                                else_span: init.span.between(els.span),
343                                ty,
344                                manual_partialeq_impl,
345                            })
346                        }
347                        _ => None,
348                    }
349                } else {
350                    None
351                };
352                let err = TypeNotStructural {
353                    span,
354                    ty,
355                    ty_def_span,
356                    manual_partialeq_impl_span,
357                    manual_partialeq_impl_note,
358                    is_local,
359                    suggestion,
360                };
361                return self.mk_err(tcx.dcx().create_err(err), ty);
362            }
363            ty::Adt(adt_def, args) if adt_def.is_enum() => {
364                let (&variant_index, fields) = valtree.to_branch().split_first().unwrap();
365                let variant_index = VariantIdx::from_u32(variant_index.to_leaf().to_u32());
366                PatKind::Variant {
367                    adt_def: *adt_def,
368                    args,
369                    variant_index,
370                    subpatterns: self
371                        .lower_field_values_to_fieldpats(fields.iter().map(|ct| ct.to_value())),
372                }
373            }
374            ty::Adt(def, _) => {
375                assert!(!def.is_union()); // Valtree construction would never succeed for unions.
376                PatKind::Leaf {
377                    subpatterns: self.lower_field_values_to_fieldpats(
378                        valtree.to_branch().iter().map(|ct| ct.to_value()),
379                    ),
380                }
381            }
382            ty::Tuple(_) => PatKind::Leaf {
383                subpatterns: self.lower_field_values_to_fieldpats(
384                    valtree.to_branch().iter().map(|ct| ct.to_value()),
385                ),
386            },
387            ty::Slice(_) => PatKind::Slice {
388                prefix: valtree
389                    .to_branch()
390                    .iter()
391                    .map(|val| *self.valtree_to_pat(val.to_value()))
392                    .collect(),
393                slice: None,
394                suffix: Box::new([]),
395            },
396            ty::Array(_, _) => PatKind::Array {
397                prefix: valtree
398                    .to_branch()
399                    .iter()
400                    .map(|val| *self.valtree_to_pat(val.to_value()))
401                    .collect(),
402                slice: None,
403                suffix: Box::new([]),
404            },
405            ty::Str => {
406                // Constant/literal patterns of type `&str` are lowered to a
407                // `PatKind::Deref` wrapping a `PatKind::Constant` of type `str`.
408                // This pattern node is the `str` constant part.
409                //
410                // Under `feature(deref_patterns)`, string literal patterns can also
411                // have type `str` directly, without the `&`, in order to allow things
412                // like `deref!("...")` to work when the scrutinee is `String`.
413                PatKind::Constant { value }
414            }
415            ty::Ref(_, pointee_ty, ..) => {
416                if pointee_ty.is_str()
417                    || pointee_ty.is_slice()
418                    || pointee_ty.is_sized(tcx, self.typing_env)
419                {
420                    PatKind::Deref {
421                        // This node has type `ty::Ref`, so it's not a pin-deref.
422                        pin: hir::Pinnedness::Not,
423                        // Lower the valtree to a pattern as the pointee type.
424                        // This works because references have the same valtree
425                        // representation as their pointee.
426                        subpattern: self.valtree_to_pat(ty::Value { ty: *pointee_ty, valtree }),
427                    }
428                } else {
429                    return self.mk_err(
430                        tcx.dcx().create_err(UnsizedPattern { span, non_sm_ty: *pointee_ty }),
431                        ty,
432                    );
433                }
434            }
435            ty::Float(flt) => {
436                let v = valtree.to_leaf();
437                let is_nan = match flt {
438                    ty::FloatTy::F16 => v.to_f16().is_nan(),
439                    ty::FloatTy::F32 => v.to_f32().is_nan(),
440                    ty::FloatTy::F64 => v.to_f64().is_nan(),
441                    ty::FloatTy::F128 => v.to_f128().is_nan(),
442                };
443                if is_nan {
444                    // NaNs are not ever equal to anything so they make no sense as patterns.
445                    // Also see <https://github.com/rust-lang/rfcs/pull/3535>.
446                    return self.mk_err(tcx.dcx().create_err(NaNPattern { span }), ty);
447                } else {
448                    PatKind::Constant { value }
449                }
450            }
451            ty::Pat(..) | ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::RawPtr(..) => {
452                // The raw pointers we see here have been "vetted" by valtree construction to be
453                // just integers, so we simply allow them.
454                PatKind::Constant { value }
455            }
456            ty::FnPtr(..) => {
457                unreachable!(
458                    "Valtree construction would never succeed for FnPtr, so this is unreachable."
459                )
460            }
461            _ => {
462                let err = InvalidPattern {
463                    span,
464                    non_sm_ty: ty,
465                    prefix: ty.prefix_string(tcx).to_string(),
466                };
467                return self.mk_err(tcx.dcx().create_err(err), ty);
468            }
469        };
470
471        Box::new(Pat { span, ty, kind, extra: None })
472    }
473}
474
475/// Given a type with type parameters, visit every ADT looking for types that need to
476/// `#[derive(PartialEq)]` for it to be a structural type.
477fn extend_type_not_partial_eq<'tcx>(
478    tcx: TyCtxt<'tcx>,
479    typing_env: ty::TypingEnv<'tcx>,
480    ty: Ty<'tcx>,
481    err: &mut Diag<'_>,
482) {
483    /// Collect all types that need to be `StructuralPartialEq`.
484    struct UsedParamsNeedInstantiationVisitor<'tcx> {
485        tcx: TyCtxt<'tcx>,
486        typing_env: ty::TypingEnv<'tcx>,
487        /// The user has written `impl PartialEq for Ty` which means it's non-structural.
488        adts_with_manual_partialeq: FxHashSet<Span>,
489        /// The type has no `PartialEq` implementation, neither manual or derived.
490        adts_without_partialeq: FxHashSet<Span>,
491        /// The user has written `impl PartialEq for Ty` which means it's non-structural,
492        /// but we don't have a span to point at, so we'll just add them as a `note`.
493        manual: FxHashSet<Ty<'tcx>>,
494        /// The type has no `PartialEq` implementation, neither manual or derived, but
495        /// we don't have a span to point at, so we'll just add them as a `note`.
496        without: FxHashSet<Ty<'tcx>>,
497    }
498
499    impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for UsedParamsNeedInstantiationVisitor<'tcx> {
500        type Result = ControlFlow<()>;
501        fn visit_ty(&mut self, ty: Ty<'tcx>) -> Self::Result {
502            match ty.kind() {
503                ty::Dynamic(..) => return ControlFlow::Break(()),
504                // Unsafe binders never implement `PartialEq`, so avoid walking into them
505                // which would require instantiating its binder with placeholders too.
506                ty::UnsafeBinder(..) => return ControlFlow::Break(()),
507                ty::FnPtr(..) => return ControlFlow::Continue(()),
508                ty::Adt(def, _args) => {
509                    let ty_def_id = def.did();
510                    let ty_def_span = self.tcx.def_span(ty_def_id);
511                    let PartialEqImplStatus {
512                        has_impl,
513                        is_derived,
514                        possibly_inapplicable_structural_partial_eq: structural_partial_eq,
515                        non_blanket_impl,
516                        possibly_inapplicable_derived_partial_eq: _,
517                    } = type_has_partial_eq_impl(self.tcx, self.typing_env, ty);
518                    match (has_impl, is_derived, structural_partial_eq, non_blanket_impl) {
519                        (_, _, true, _) => {}
520                        (true, false, _, Some(def_id)) if def_id.is_local() => {
521                            self.adts_with_manual_partialeq.insert(self.tcx.def_span(def_id));
522                        }
523                        (true, false, _, _) if ty_def_id.is_local() => {
524                            self.adts_with_manual_partialeq.insert(ty_def_span);
525                        }
526                        (false, _, _, _) if ty_def_id.is_local() => {
527                            self.adts_without_partialeq.insert(ty_def_span);
528                        }
529                        (true, false, _, _) => {
530                            self.manual.insert(ty);
531                        }
532                        (false, _, _, _) => {
533                            self.without.insert(ty);
534                        }
535                        _ => {}
536                    };
537                    ty.super_visit_with(self)
538                }
539                _ => ty.super_visit_with(self),
540            }
541        }
542    }
543    let mut v = UsedParamsNeedInstantiationVisitor {
544        tcx,
545        typing_env,
546        adts_with_manual_partialeq: FxHashSet::default(),
547        adts_without_partialeq: FxHashSet::default(),
548        manual: FxHashSet::default(),
549        without: FxHashSet::default(),
550    };
551    if v.visit_ty(ty).is_break() {
552        return;
553    }
554    #[allow(rustc::potential_query_instability)] // Span labels will be sorted by the rendering
555    for span in v.adts_with_manual_partialeq {
556        err.span_note(span, "the `PartialEq` trait must be derived, manual `impl`s are not sufficient; see https://doc.rust-lang.org/stable/std/marker/trait.StructuralPartialEq.html for details");
557    }
558    #[allow(rustc::potential_query_instability)] // Span labels will be sorted by the rendering
559    for span in v.adts_without_partialeq {
560        err.span_label(
561            span,
562            "must be annotated with `#[derive(PartialEq)]` to be usable in patterns",
563        );
564    }
565    #[allow(rustc::potential_query_instability)]
566    let mut manual: Vec<_> = v.manual.into_iter().map(|t| t.to_string()).collect();
567    manual.sort();
568    for ty in manual {
569        err.note(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("`{0}` must be annotated with `#[derive(PartialEq)]` to be usable in patterns, manual `impl`s are not sufficient; see https://doc.rust-lang.org/stable/std/marker/trait.StructuralPartialEq.html for details",
                ty))
    })format!(
570            "`{ty}` must be annotated with `#[derive(PartialEq)]` to be usable in patterns, manual `impl`s are not sufficient; see https://doc.rust-lang.org/stable/std/marker/trait.StructuralPartialEq.html for details"
571        ));
572    }
573    #[allow(rustc::potential_query_instability)]
574    let mut without: Vec<_> = v.without.into_iter().map(|t| t.to_string()).collect();
575    without.sort();
576    for ty in without {
577        err.note(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("`{0}` must be annotated with `#[derive(PartialEq)]` to be usable in patterns",
                ty))
    })format!(
578            "`{ty}` must be annotated with `#[derive(PartialEq)]` to be usable in patterns"
579        ));
580    }
581}
582
583#[derive(#[automatically_derived]
impl ::core::fmt::Debug for PartialEqImplStatus {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field5_finish(f,
            "PartialEqImplStatus", "has_impl", &self.has_impl, "is_derived",
            &self.is_derived, "non_blanket_impl", &self.non_blanket_impl,
            "possibly_inapplicable_structural_partial_eq",
            &self.possibly_inapplicable_structural_partial_eq,
            "possibly_inapplicable_derived_partial_eq",
            &&self.possibly_inapplicable_derived_partial_eq)
    }
}Debug)]
584struct PartialEqImplStatus {
585    /// There is a `PartialEq` impl that applies to the type.
586    has_impl: bool,
587
588    /// The `PartialEq` impl is `#[automatically_derived]`.
589    is_derived: bool,
590    /// The `DefId` of the same impl that `is_derived` refers to.
591    non_blanket_impl: Option<DefId>,
592
593    /// If true, there is a `StructuralPartialEq` implementation,
594    /// but its bounds might not be satisfied.
595    possibly_inapplicable_structural_partial_eq: bool,
596    /// If true, there is a derived `PartialEq` implementation for the type,
597    /// but its bounds might not be satisfied.
598    possibly_inapplicable_derived_partial_eq: bool,
599}
600
601x;#[instrument(level = "trace", skip(tcx), ret)]
602fn type_has_partial_eq_impl<'tcx>(
603    tcx: TyCtxt<'tcx>,
604    typing_env: ty::TypingEnv<'tcx>,
605    ty: Ty<'tcx>,
606) -> PartialEqImplStatus {
607    let (infcx, param_env) = tcx.infer_ctxt().build_with_typing_env(typing_env);
608    // double-check there even *is* a semantic `PartialEq` to dispatch to.
609    //
610    // (If there isn't, then we can safely issue a hard
611    // error, because that's never worked, due to compiler
612    // using `PartialEq::eq` in this scenario in the past.)
613    let partial_eq_trait_id = tcx.require_lang_item(hir::LangItem::PartialEq, DUMMY_SP);
614    let structural_partial_eq_trait_id =
615        tcx.require_lang_item(hir::LangItem::StructuralPeq, DUMMY_SP);
616
617    // This *could* accept a type that isn't actually `PartialEq`, because region bounds get
618    // ignored. However that should be pretty much impossible since consts that do not depend on
619    // generics can only mention the `'static` lifetime, and how would one have a type that's
620    // `PartialEq` for some lifetime but *not* for `'static`? If this ever becomes a problem
621    // we'll need to leave some sort of trace of this requirement in the MIR so that borrowck
622    // can ensure that the type really implements `PartialEq`.
623    // We also do *not* require `const PartialEq`, not even in `const fn`. This violates the model
624    // that patterns can only do things that the code could also do without patterns, but it is
625    // needed for backwards compatibility. The actual pattern matching compares primitive values,
626    // `PartialEq::eq` never gets invoked, so there's no risk of us running non-const code.
627    let has_impl = {
628        let obligation = Obligation::new(
629            tcx,
630            ObligationCause::dummy(),
631            param_env,
632            ty::TraitRef::new(tcx, partial_eq_trait_id, [ty, ty]),
633        );
634        infcx.predicate_must_hold_modulo_regions(&obligation)
635    };
636
637    // Determine whether there are is a derived `PartialEq` implementation, whether or not its
638    // bounds are met.
639    let possibly_inapplicable_derived_partial_eq = {
640        let obligation = Obligation::new(
641            tcx,
642            ObligationCause::dummy(),
643            param_env,
644            ty::Binder::dummy(ty::TraitRef::new(tcx, partial_eq_trait_id, [ty, ty])),
645        );
646        compute_applicable_impls_for_diagnostics(&infcx, &obligation, true).iter().any(
647            |candidate_source| {
648                matches!(
649                    candidate_source,
650                    &CandidateSource::DefId(def_id)
651                    if find_attr!(tcx, def_id, AutomaticallyDerived)
652                )
653            },
654        )
655    };
656
657    let possibly_inapplicable_structural_partial_eq = {
658        let obligation = Obligation::new(
659            tcx,
660            ObligationCause::dummy(),
661            param_env,
662            ty::Binder::dummy(ty::TraitRef::new(tcx, structural_partial_eq_trait_id, [ty])),
663        );
664        compute_applicable_impls_for_diagnostics(&infcx, &obligation, true)
665            .iter()
666            .any(|candidate_source| matches!(candidate_source, CandidateSource::DefId(_)))
667    };
668
669    let mut automatically_derived = false;
670    let mut impl_def_id = None;
671    for def_id in tcx.non_blanket_impls_for_ty(partial_eq_trait_id, ty) {
672        automatically_derived = find_attr!(tcx, def_id, AutomaticallyDerived);
673        impl_def_id = Some(def_id);
674    }
675
676    PartialEqImplStatus {
677        has_impl,
678        is_derived: automatically_derived,
679        possibly_inapplicable_structural_partial_eq,
680        non_blanket_impl: impl_def_id,
681        possibly_inapplicable_derived_partial_eq,
682    }
683}