Skip to main content

rustc_hir_typeck/
coercion.rs

1//! # Type Coercion
2//!
3//! Under certain circumstances we will coerce from one type to another,
4//! for example by auto-borrowing. This occurs in situations where the
5//! compiler has a firm 'expected type' that was supplied from the user,
6//! and where the actual type is similar to that expected type in purpose
7//! but not in representation (so actual subtyping is inappropriate).
8//!
9//! ## Reborrowing
10//!
11//! Note that if we are expecting a reference, we will *reborrow*
12//! even if the argument provided was already a reference. This is
13//! useful for freezing mut things (that is, when the expected type is &T
14//! but you have &mut T) and also for avoiding the linearity
15//! of mut things (when the expected is &mut T and you have &mut T). See
16//! the various `tests/ui/coerce/*.rs` tests for
17//! examples of where this is useful.
18//!
19//! ## Subtle note
20//!
21//! When inferring the generic arguments of functions, the argument
22//! order is relevant, which can lead to the following edge case:
23//!
24//! ```ignore (illustrative)
25//! fn foo<T>(a: T, b: T) {
26//!     // ...
27//! }
28//!
29//! foo(&7i32, &mut 7i32);
30//! // This compiles, as we first infer `T` to be `&i32`,
31//! // and then coerce `&mut 7i32` to `&7i32`.
32//!
33//! foo(&mut 7i32, &7i32);
34//! // This does not compile, as we first infer `T` to be `&mut i32`
35//! // and are then unable to coerce `&7i32` to `&mut i32`.
36//! ```
37
38use std::ops::{ControlFlow, Deref};
39
40use rustc_attr_ir::InlineAttr;
41use rustc_attr_ir::lang_items::LangItem;
42use rustc_errors::codes::*;
43use rustc_errors::{Applicability, Diag, struct_span_code_err};
44use rustc_hir as hir;
45use rustc_hir::def_id::{DefId, LocalDefId};
46use rustc_hir_analysis::hir_ty_lowering::HirTyLowerer;
47use rustc_infer::infer::relate::RelateResult;
48use rustc_infer::infer::{DefineOpaqueTypes, InferOk, InferResult, RegionVariableOrigin};
49use rustc_infer::traits::{
50    MatchExpressionArmCause, Obligation, PredicateObligation, PredicateObligations, SelectionError,
51};
52use rustc_middle::ty::adjustment::{
53    Adjust, Adjustment, AllowTwoPhase, AutoBorrow, AutoBorrowMutability, DerefAdjustKind,
54    PointerCoercion,
55};
56use rustc_middle::ty::error::TypeError;
57use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt, Unnormalized};
58use rustc_span::{BytePos, DUMMY_SP, Span, span_bug};
59use rustc_trait_selection::infer::InferCtxtExt as _;
60use rustc_trait_selection::solve::inspect::{self, InferCtxtProofTreeExt, ProofTreeVisitor};
61use rustc_trait_selection::solve::{Certainty, Goal, NoSolution};
62use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt;
63use rustc_trait_selection::traits::{
64    self, ImplSource, NormalizeExt, ObligationCause, ObligationCauseCode, ObligationCtxt,
65};
66use smallvec::{SmallVec, smallvec};
67use tracing::{debug, instrument};
68
69use crate::FnCtxt;
70use crate::diagnostics::SuggestBoxingForReturnImplTrait;
71
72struct Coerce<'a, 'tcx> {
73    fcx: &'a FnCtxt<'a, 'tcx>,
74    cause: ObligationCause<'tcx>,
75    use_lub: bool,
76    /// Determines whether or not allow_two_phase_borrow is set on any
77    /// autoref adjustments we create while coercing. We don't want to
78    /// allow deref coercions to create two-phase borrows, at least initially,
79    /// but we do need two-phase borrows for function argument reborrows.
80    /// See #47489 and #48598
81    /// See docs on the "AllowTwoPhase" type for a more detailed discussion
82    allow_two_phase: AllowTwoPhase,
83    /// Whether we allow `NeverToAny` coercions. This is unsound if we're
84    /// coercing a place expression without it counting as a read in the MIR.
85    /// This is a side-effect of HIR not really having a great distinction
86    /// between places and values.
87    coerce_never: bool,
88}
89
90impl<'a, 'tcx> Deref for Coerce<'a, 'tcx> {
91    type Target = FnCtxt<'a, 'tcx>;
92    fn deref(&self) -> &Self::Target {
93        self.fcx
94    }
95}
96
97type CoerceResult<'tcx> = InferResult<'tcx, (Vec<Adjustment<'tcx>>, Ty<'tcx>)>;
98
99/// Coercing a mutable reference to an immutable works, while
100/// coercing `&T` to `&mut T` should be forbidden.
101fn coerce_mutbls<'tcx>(
102    from_mutbl: hir::Mutability,
103    to_mutbl: hir::Mutability,
104) -> RelateResult<'tcx, ()> {
105    if from_mutbl >= to_mutbl { Ok(()) } else { Err(TypeError::Mutability) }
106}
107
108/// This always returns `Ok(...)`.
109fn success<'tcx>(
110    adj: Vec<Adjustment<'tcx>>,
111    target: Ty<'tcx>,
112    obligations: PredicateObligations<'tcx>,
113) -> CoerceResult<'tcx> {
114    Ok(InferOk { value: (adj, target), obligations })
115}
116
117/// Data extracted from a reference (pinned or not) for coercion to a reference (pinned or not).
118struct CoerceMaybePinnedRef<'tcx> {
119    /// coercion source, must be a pinned (i.e. `Pin<&T>` or `Pin<&mut T>`) or normal reference (`&T` or `&mut T`)
120    a: Ty<'tcx>,
121    /// coercion target, must be a pinned (i.e. `Pin<&T>` or `Pin<&mut T>`) or normal reference (`&T` or `&mut T`)
122    b: Ty<'tcx>,
123    /// referent type of the source
124    a_ty: Ty<'tcx>,
125    /// pinnedness of the source
126    a_pin: ty::Pinnedness,
127    /// mutability of the source
128    a_mut: ty::Mutability,
129    /// region of the source
130    a_r: ty::Region<'tcx>,
131    /// pinnedness of the target
132    b_pin: ty::Pinnedness,
133    /// mutability of the target
134    b_mut: ty::Mutability,
135}
136
137/// Whether to force a leak check to occur in `Coerce::unify_raw`.
138/// Note that leak checks may still occur evn with `ForceLeakCheck::No`.
139///
140/// FIXME: We may want to change type relations to always leak-check
141/// after exiting a binder, at which point we will always do so and
142/// no longer need to handle this explicitly
143enum ForceLeakCheck {
144    Yes,
145    No,
146}
147
148impl<'f, 'tcx> Coerce<'f, 'tcx> {
149    fn new(
150        fcx: &'f FnCtxt<'f, 'tcx>,
151        cause: ObligationCause<'tcx>,
152        allow_two_phase: AllowTwoPhase,
153        coerce_never: bool,
154    ) -> Self {
155        Coerce { fcx, cause, allow_two_phase, use_lub: false, coerce_never }
156    }
157
158    fn unify_raw(
159        &self,
160        a: Ty<'tcx>,
161        b: Ty<'tcx>,
162        leak_check: ForceLeakCheck,
163    ) -> InferResult<'tcx, Ty<'tcx>> {
164        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:164",
                        "rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
                        ::tracing_core::__macro_support::Option::Some(164u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
                        ::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};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("unify(a: {0:?}, b: {1:?}, use_lub: {2})",
                                                    a, b, self.use_lub) as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!("unify(a: {:?}, b: {:?}, use_lub: {})", a, b, self.use_lub);
165        self.commit_if_ok(|snapshot| {
166            let outer_universe = self.infcx.universe();
167
168            let at = self.at(&self.cause, self.fcx.param_env);
169
170            let res = if self.use_lub {
171                at.lub(b, a)
172            } else {
173                self.sup_raw(&self.cause, self.param_env, DefineOpaqueTypes::Yes, b, a)
174                    .map(|InferOk { value: (), obligations }| InferOk { value: b, obligations })
175            };
176
177            // In the new solver, lazy norm may allow us to shallowly equate
178            // more types, but we emit possibly impossible-to-satisfy obligations.
179            // Filter these cases out to make sure our coercion is more accurate.
180            let res = match res {
181                Ok(InferOk { value, obligations }) if self.next_trait_solver() => {
182                    let ocx = ObligationCtxt::new(self);
183                    ocx.register_obligations(obligations);
184                    if ocx.try_evaluate_obligations().no_errors() {
185                        Ok(InferOk { value, obligations: ocx.into_pending_obligations() })
186                    } else {
187                        Err(TypeError::Mismatch)
188                    }
189                }
190                res => res,
191            };
192
193            // We leak check here mostly because lub operations are
194            // kind of scuffed around binders. Instead of computing an actual
195            // lub'd binder we instead:
196            // - Equate the binders
197            // - Return the lhs of the lub operation
198            //
199            // This may lead to incomplete type inference for the resulting type
200            // of a `match` or `if .. else`, etc. This is a backwards compat
201            // hazard for if/when we start handling `lub` more correctly.
202            //
203            // In order to actually ensure that equating the binders *does*
204            // result in equal binders, and that the lhs is actually a supertype
205            // of the rhs, we must perform a leak check here.
206            if #[allow(non_exhaustive_omitted_patterns)] match leak_check {
    ForceLeakCheck::Yes => true,
    _ => false,
}matches!(leak_check, ForceLeakCheck::Yes) {
207                self.leak_check(outer_universe, Some(snapshot))?;
208            }
209
210            res
211        })
212    }
213
214    /// Unify two types (using sub or lub).
215    fn unify(&self, a: Ty<'tcx>, b: Ty<'tcx>, leak_check: ForceLeakCheck) -> CoerceResult<'tcx> {
216        self.unify_raw(a, b, leak_check)
217            .and_then(|InferOk { value: ty, obligations }| success(::alloc::vec::Vec::new()vec![], ty, obligations))
218    }
219
220    /// Unify two types (using sub or lub) and produce a specific coercion.
221    fn unify_and(
222        &self,
223        a: Ty<'tcx>,
224        b: Ty<'tcx>,
225        adjustments: impl IntoIterator<Item = Adjustment<'tcx>>,
226        final_adjustment: Adjust,
227        leak_check: ForceLeakCheck,
228    ) -> CoerceResult<'tcx> {
229        self.unify_raw(a, b, leak_check).and_then(|InferOk { value: ty, obligations }| {
230            success(
231                adjustments
232                    .into_iter()
233                    .chain(std::iter::once(Adjustment { target: ty, kind: final_adjustment }))
234                    .collect(),
235                ty,
236                obligations,
237            )
238        })
239    }
240
241    {}
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::INFO <= ::tracing::level_filters::STATIC_MAX_LEVEL &&
            ::tracing::Level::INFO <=
                ::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("coerce",
                                "rustc_hir_typeck::coercion", ::tracing::Level::INFO,
                                ::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
                                ::tracing_core::__macro_support::Option::Some(241u32),
                                ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
                                ::tracing_core::field::FieldSet::new(&[{
                                                    const NAME:
                                                        ::tracing::__macro_support::FieldName<{
                                                            ::tracing::__macro_support::FieldName::len("a")
                                                        }> =
                                                        ::tracing::__macro_support::FieldName::new("a");
                                                    NAME.as_str()
                                                },
                                                {
                                                    const NAME:
                                                        ::tracing::__macro_support::FieldName<{
                                                            ::tracing::__macro_support::FieldName::len("b")
                                                        }> =
                                                        ::tracing::__macro_support::FieldName::new("b");
                                                    NAME.as_str()
                                                }], ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                ::tracing::metadata::Kind::SPAN)
                        };
                    ::tracing::callsite::DefaultCallsite::new(&META)
                };
            let mut interest = ::tracing::subscriber::Interest::never();
            if ::tracing::Level::INFO <=
                                ::tracing::level_filters::STATIC_MAX_LEVEL &&
                            ::tracing::Level::INFO <=
                                ::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};
                            meta.fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&a)
                                                        as &dyn ::tracing::field::Value)),
                                            (::tracing::__macro_support::Option::Some(&::tracing::field::debug(&b)
                                                        as &dyn ::tracing::field::Value))])
                        })
            } else {
                let span =
                    ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                {};
                span
            }
        };
    __tracing_attr_guard = __tracing_attr_span.enter();
}
#[allow(clippy :: redundant_closure_call)]
let x =
    (move ||
                {

                    #[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: CoerceResult<'tcx> =
                            loop {};
                        return __tracing_attr_fake_return;
                    }
                    {
                        let a = self.shallow_resolve(a);
                        let b = self.shallow_resolve(b);
                        {
                            use ::tracing::__macro_support::Callsite as _;
                            static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                                {
                                    static META: ::tracing::Metadata<'static> =
                                        {
                                            ::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:246",
                                                "rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
                                                ::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
                                                ::tracing_core::__macro_support::Option::Some(246u32),
                                                ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
                                                ::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};
                                        __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("Coerce.tys({0:?} => {1:?})",
                                                                            a, b) as &dyn ::tracing::field::Value))])
                                    });
                            } else { ; }
                        };
                        if a.is_never() {
                            if self.coerce_never {
                                return success(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
                                                [Adjustment { kind: Adjust::NeverToAny, target: b }])), b,
                                        PredicateObligations::new());
                            } else { return self.unify(a, b, ForceLeakCheck::No); }
                        }
                        if a.is_ty_var() {
                            return self.coerce_from_inference_variable(a, b);
                        }
                        let unsize = self.coerce_unsized(a, b);
                        match unsize {
                            Ok(_) => {
                                {
                                    use ::tracing::__macro_support::Callsite as _;
                                    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                                        {
                                            static META: ::tracing::Metadata<'static> =
                                                {
                                                    ::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:273",
                                                        "rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
                                                        ::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
                                                        ::tracing_core::__macro_support::Option::Some(273u32),
                                                        ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
                                                        ::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};
                                                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("coerce: unsize successful")
                                                                            as &dyn ::tracing::field::Value))])
                                            });
                                    } else { ; }
                                };
                                return unsize;
                            }
                            Err(error) => {
                                {
                                    use ::tracing::__macro_support::Callsite as _;
                                    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                                        {
                                            static META: ::tracing::Metadata<'static> =
                                                {
                                                    ::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:277",
                                                        "rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
                                                        ::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
                                                        ::tracing_core::__macro_support::Option::Some(277u32),
                                                        ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
                                                        ::tracing_core::field::FieldSet::new(&["message",
                                                                        {
                                                                            const NAME:
                                                                                ::tracing::__macro_support::FieldName<{
                                                                                    ::tracing::__macro_support::FieldName::len("error")
                                                                                }> =
                                                                                ::tracing::__macro_support::FieldName::new("error");
                                                                            NAME.as_str()
                                                                        }], ::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};
                                                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("coerce: unsize failed")
                                                                            as &dyn ::tracing::field::Value)),
                                                                (::tracing::__macro_support::Option::Some(&::tracing::field::debug(&error)
                                                                            as &dyn ::tracing::field::Value))])
                                            });
                                    } else { ; }
                                };
                            }
                        }
                        match *b.kind() {
                            ty::RawPtr(_, b_mutbl) => {
                                return self.coerce_to_raw_ptr(a, b, b_mutbl);
                            }
                            ty::Ref(r_b, _, mutbl_b) => {
                                if let Some(pin_ref_to_ref) =
                                        self.maybe_pin_ref_to_ref(a, b) {
                                    return self.coerce_pin_ref_to_ref(pin_ref_to_ref);
                                }
                                return self.coerce_to_ref(a, b, r_b, mutbl_b);
                            }
                            _ if let Some(to_pin_ref) = self.maybe_to_pin_ref(a, b) => {
                                return self.coerce_to_pin_ref(to_pin_ref);
                            }
                            ty::Adt(_, _) if
                                self.tcx.features().reborrow() &&
                                    self.fcx.infcx.type_implements_trait(self.tcx.lang_items().reborrow().expect("Unexpectedly using core/std without reborrow"),
                                            [b], self.fcx.param_env).must_apply_modulo_regions() => {
                                let reborrow_coerce =
                                    self.commit_if_ok(|_| self.coerce_reborrow(a, b));
                                if reborrow_coerce.is_ok() { return reborrow_coerce; }
                            }
                            ty::Int(_) | ty::Uint(_) | ty::Float(_) |
                                ty::Infer(ty::IntVar(_) | ty::FloatVar(_)) => {}
                            _ => {}
                        }
                        match *a.kind() {
                            ty::FnDef(..) => { self.coerce_from_fn_item(a, b) }
                            ty::FnPtr(a_sig_tys, a_hdr) => {
                                self.coerce_from_fn_pointer(a, a_sig_tys.with(a_hdr), b)
                            }
                            ty::Closure(..) => { self.coerce_closure_to_fn(a, b) }
                            ty::Adt(_, _) if self.tcx.features().reborrow() => {
                                let reborrow_coerce =
                                    self.commit_if_ok(|_| self.coerce_shared_reborrow(a, b));
                                if reborrow_coerce.is_ok() {
                                    reborrow_coerce
                                } else { self.unify(a, b, ForceLeakCheck::No) }
                            }
                            ty::Int(_) | ty::Uint(_) | ty::Float(_) |
                                ty::Infer(ty::IntVar(_) | ty::FloatVar(_)) => {
                                self.unify(a, b, ForceLeakCheck::No)
                            }
                            _ => { self.unify(a, b, ForceLeakCheck::No) }
                        }
                    }
                })();
{
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:241",
                        "rustc_hir_typeck::coercion", ::tracing::Level::INFO,
                        ::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
                        ::tracing_core::__macro_support::Option::Some(241u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
                        ::tracing_core::field::FieldSet::new(&[{
                                            const NAME:
                                                ::tracing::__macro_support::FieldName<{
                                                    ::tracing::__macro_support::FieldName::len("return")
                                                }> =
                                                ::tracing::__macro_support::FieldName::new("return");
                                            NAME.as_str()
                                        }], ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::INFO <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::INFO <=
                    ::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};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&x)
                                            as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};
x;#[instrument(skip(self), ret)]
242    fn coerce(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> CoerceResult<'tcx> {
243        // First, remove any resolved type variables (at the top level, at least):
244        let a = self.shallow_resolve(a);
245        let b = self.shallow_resolve(b);
246        debug!("Coerce.tys({:?} => {:?})", a, b);
247
248        // Coercing from `!` to any type is allowed:
249        if a.is_never() {
250            if self.coerce_never {
251                return success(
252                    vec![Adjustment { kind: Adjust::NeverToAny, target: b }],
253                    b,
254                    PredicateObligations::new(),
255                );
256            } else {
257                // Otherwise the only coercion we can do is unification.
258                return self.unify(a, b, ForceLeakCheck::No);
259            }
260        }
261
262        // Coercing *from* an unresolved inference variable means that
263        // we have no information about the source type. This will always
264        // ultimately fall back to some form of subtyping.
265        if a.is_ty_var() {
266            return self.coerce_from_inference_variable(a, b);
267        }
268
269        // Consider coercing the subtype to a DST
270        let unsize = self.coerce_unsized(a, b);
271        match unsize {
272            Ok(_) => {
273                debug!("coerce: unsize successful");
274                return unsize;
275            }
276            Err(error) => {
277                debug!(?error, "coerce: unsize failed");
278            }
279        }
280
281        // Examine the target type and consider type-specific coercions, such
282        // as auto-borrowing, coercing pointer mutability, pin-ergonomics, or
283        // generic reborrow.
284        match *b.kind() {
285            ty::RawPtr(_, b_mutbl) => {
286                return self.coerce_to_raw_ptr(a, b, b_mutbl);
287            }
288            ty::Ref(r_b, _, mutbl_b) => {
289                if let Some(pin_ref_to_ref) = self.maybe_pin_ref_to_ref(a, b) {
290                    return self.coerce_pin_ref_to_ref(pin_ref_to_ref);
291                }
292                return self.coerce_to_ref(a, b, r_b, mutbl_b);
293            }
294            _ if let Some(to_pin_ref) = self.maybe_to_pin_ref(a, b) => {
295                return self.coerce_to_pin_ref(to_pin_ref);
296            }
297            ty::Adt(_, _)
298                if self.tcx.features().reborrow()
299                    && self
300                        .fcx
301                        .infcx
302                        .type_implements_trait(
303                            self.tcx
304                                .lang_items()
305                                .reborrow()
306                                .expect("Unexpectedly using core/std without reborrow"),
307                            [b],
308                            self.fcx.param_env,
309                        )
310                        .must_apply_modulo_regions() =>
311            {
312                let reborrow_coerce = self.commit_if_ok(|_| self.coerce_reborrow(a, b));
313                if reborrow_coerce.is_ok() {
314                    return reborrow_coerce;
315                }
316            }
317            ty::Int(_)
318            | ty::Uint(_)
319            | ty::Float(_)
320            | ty::Infer(ty::IntVar(_) | ty::FloatVar(_)) => {
321                // For now, do nothing. If coercions are ever added for ints/floats then the arms
322                // handling these types in `check_expr_lit` will need re-evaluation.
323            }
324            _ => {}
325        }
326
327        match *a.kind() {
328            ty::FnDef(..) => {
329                // Function items are coercible to any closure
330                // type; function pointers are not (that would
331                // require double indirection).
332                // Additionally, we permit coercion of function
333                // items to drop the unsafe qualifier.
334                self.coerce_from_fn_item(a, b)
335            }
336            ty::FnPtr(a_sig_tys, a_hdr) => {
337                // We permit coercion of fn pointers to drop the
338                // unsafe qualifier.
339                self.coerce_from_fn_pointer(a, a_sig_tys.with(a_hdr), b)
340            }
341            ty::Closure(..) => {
342                // Non-capturing closures are coercible to
343                // function pointers or unsafe function pointers.
344                // It cannot convert closures that require unsafe.
345                self.coerce_closure_to_fn(a, b)
346            }
347            ty::Adt(_, _) if self.tcx.features().reborrow() => {
348                let reborrow_coerce = self.commit_if_ok(|_| self.coerce_shared_reborrow(a, b));
349                if reborrow_coerce.is_ok() {
350                    reborrow_coerce
351                } else {
352                    self.unify(a, b, ForceLeakCheck::No)
353                }
354            }
355            ty::Int(_)
356            | ty::Uint(_)
357            | ty::Float(_)
358            | ty::Infer(ty::IntVar(_) | ty::FloatVar(_)) => {
359                // For now, just use unification rules. If coercions are ever added for ints/floats
360                // then the arms handling these types in `check_expr_lit` will need re-evaluation.
361                self.unify(a, b, ForceLeakCheck::No)
362            }
363            _ => {
364                // Otherwise, just use unification rules.
365                self.unify(a, b, ForceLeakCheck::No)
366            }
367        }
368    }
369
370    /// Coercing *from* an inference variable. In this case, we have no information
371    /// about the source type, so we can't really do a true coercion and we always
372    /// fall back to subtyping (`unify_and`).
373    fn coerce_from_inference_variable(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> CoerceResult<'tcx> {
374        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:374",
                        "rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
                        ::tracing_core::__macro_support::Option::Some(374u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
                        ::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};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("coerce_from_inference_variable(a={0:?}, b={1:?})",
                                                    a, b) as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!("coerce_from_inference_variable(a={:?}, b={:?})", a, b);
375        if true {
    if !(a.is_ty_var() && self.shallow_resolve(a) == a) {
        ::core::panicking::panic("assertion failed: a.is_ty_var() && self.shallow_resolve(a) == a")
    };
};debug_assert!(a.is_ty_var() && self.shallow_resolve(a) == a);
376        if true {
    if !(self.shallow_resolve(b) == b) {
        ::core::panicking::panic("assertion failed: self.shallow_resolve(b) == b")
    };
};debug_assert!(self.shallow_resolve(b) == b);
377
378        if b.is_ty_var() {
379            let mut obligations = PredicateObligations::with_capacity(2);
380            let mut push_coerce_obligation = |a, b| {
381                obligations.push(Obligation::new(
382                    self.tcx(),
383                    self.cause.clone(),
384                    self.param_env,
385                    ty::Binder::dummy(ty::PredicateKind::Coerce(ty::CoercePredicate { a, b })),
386                ));
387            };
388
389            let target_ty = if self.use_lub {
390                // When computing the lub, we create a new target
391                // and coerce both `a` and `b` to it.
392                let target_ty = self.next_ty_var(self.cause.span);
393                push_coerce_obligation(a, target_ty);
394                push_coerce_obligation(b, target_ty);
395                target_ty
396            } else {
397                // When subtyping, we don't need to create a new target
398                // as we only coerce `a` to `b`.
399                push_coerce_obligation(a, b);
400                b
401            };
402
403            {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:403",
                        "rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
                        ::tracing_core::__macro_support::Option::Some(403u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
                        ::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};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("coerce_from_inference_variable: two inference variables, target_ty={0:?}, obligations={1:?}",
                                                    target_ty, obligations) as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!(
404                "coerce_from_inference_variable: two inference variables, target_ty={:?}, obligations={:?}",
405                target_ty, obligations
406            );
407            success(::alloc::vec::Vec::new()vec![], target_ty, obligations)
408        } else {
409            // One unresolved type variable: just apply subtyping, we may be able
410            // to do something useful.
411            self.unify(a, b, ForceLeakCheck::No)
412        }
413    }
414
415    /// Handles coercing some arbitrary type `a` to some reference (`b`). This
416    /// handles a few cases:
417    /// - Introducing reborrows to give more flexible lifetimes
418    /// - Deref coercions to allow `&T` to coerce to `&T::Target`
419    /// - Coercing mutable references to immutable references
420    /// These coercions can be freely intermixed, for example we are able to
421    /// coerce `&mut T` to `&mut T::Target`.
422    fn coerce_to_ref(
423        &self,
424        a: Ty<'tcx>,
425        b: Ty<'tcx>,
426        r_b: ty::Region<'tcx>,
427        mutbl_b: hir::Mutability,
428    ) -> CoerceResult<'tcx> {
429        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:429",
                        "rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
                        ::tracing_core::__macro_support::Option::Some(429u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
                        ::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};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("coerce_to_ref(a={0:?}, b={1:?})",
                                                    a, b) as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!("coerce_to_ref(a={:?}, b={:?})", a, b);
430        if true {
    if !(self.shallow_resolve(a) == a) {
        ::core::panicking::panic("assertion failed: self.shallow_resolve(a) == a")
    };
};debug_assert!(self.shallow_resolve(a) == a);
431        if true {
    if !(self.shallow_resolve(b) == b) {
        ::core::panicking::panic("assertion failed: self.shallow_resolve(b) == b")
    };
};debug_assert!(self.shallow_resolve(b) == b);
432
433        let (r_a, mt_a) = match *a.kind() {
434            ty::Ref(r_a, ty, mutbl) => {
435                coerce_mutbls(mutbl, mutbl_b)?;
436                (r_a, ty::TypeAndMut { ty, mutbl })
437            }
438            _ => return self.unify(a, b, ForceLeakCheck::No),
439        };
440
441        // Look at each step in the `Deref` chain and check if
442        // any of the autoref'd `Target` types unify with the
443        // coercion target.
444        //
445        // For example when coercing from `&mut Vec<T>` to `&M [T]` we
446        // have three deref steps:
447        // 1. `&mut Vec<T>`, skip autoref
448        // 2. `Vec<T>`, autoref'd ty: `&M Vec<T>`
449        //     - `&M Vec<T>` does not unify with `&M [T]`
450        // 3. `[T]`, autoref'd ty: `&M [T]`
451        //     - `&M [T]` does unify with `&M [T]`
452        let mut first_error = None;
453        let mut r_borrow_var = None;
454        let mut autoderef = self.autoderef(self.cause.span, a);
455        let found = autoderef.by_ref().find_map(|(deref_ty, autoderefs)| {
456            if autoderefs == 0 {
457                // Don't autoref the first step as otherwise we'd allow
458                // coercing `&T` to `&&T`.
459                return None;
460            }
461
462            // The logic here really shouldn't exist. We don't care about free
463            // lifetimes during HIR typeck. Unfortunately later parts of this
464            // function rely on structural identity of the autoref'd deref'd ty.
465            //
466            // This means that what region we use here actually impacts whether
467            // we emit a reborrow coercion or not which can affect diagnostics
468            // and capture analysis (which in turn affects borrowck).
469            let r = if !self.use_lub {
470                r_b
471            } else if autoderefs == 1 {
472                r_a
473            } else {
474                if r_borrow_var.is_none() {
475                    // create var lazily, at most once
476                    let coercion = RegionVariableOrigin::Coercion(self.cause.span);
477                    let r = self.next_region_var(coercion);
478                    r_borrow_var = Some(r);
479                }
480                r_borrow_var.unwrap()
481            };
482
483            let autorefd_deref_ty = Ty::new_ref(self.tcx, r, deref_ty, mutbl_b);
484
485            // Note that we unify the autoref'd `Target` type with `b` rather than
486            // the `Target` type with the pointee of `b`. This is necessary
487            // to properly account for the differing variances of the pointees
488            // of `&` vs `&mut` references.
489            match self.unify_raw(autorefd_deref_ty, b, ForceLeakCheck::No) {
490                Ok(ok) => Some(ok),
491                Err(err) => {
492                    if first_error.is_none() {
493                        first_error = Some(err);
494                    }
495                    None
496                }
497            }
498        });
499
500        // Extract type or return an error. We return the first error
501        // we got, which should be from relating the "base" type
502        // (e.g., in example above, the failure from relating `Vec<T>`
503        // to the target type), since that should be the least
504        // confusing.
505        let Some(InferOk { value: coerced_a, mut obligations }) = found else {
506            if let Some(first_error) = first_error {
507                {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:507",
                        "rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
                        ::tracing_core::__macro_support::Option::Some(507u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
                        ::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};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("coerce_to_ref: failed with err = {0:?}",
                                                    first_error) as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!("coerce_to_ref: failed with err = {:?}", first_error);
508                return Err(first_error);
509            } else {
510                // This may happen in the new trait solver since autoderef requires
511                // the pointee to be structurally normalizable, or else it'll just bail.
512                // So when we have a type like `&<not well formed>`, then we get no
513                // autoderef steps (even though there should be at least one). That means
514                // we get no type mismatches, since the loop above just exits early.
515                return Err(TypeError::Mismatch);
516            }
517        };
518
519        if coerced_a == a && mt_a.mutbl.is_not() && autoderef.step_count() == 1 {
520            // As a special case, if we would produce `&'a *x`, that's
521            // a total no-op. We end up with the type `&'a T` just as
522            // we started with. In that case, just skip it altogether.
523            //
524            // Unfortunately, this can actually effect capture analysis
525            // which in turn means this effects borrow checking. This can
526            // also effect diagnostics.
527            // FIXME(BoxyUwU): we should always emit reborrow coercions
528            //
529            // Note that for `&mut`, we DO want to reborrow --
530            // otherwise, this would be a move, which might be an
531            // error. For example `foo(self.x)` where `self` and
532            // `self.x` both have `&mut `type would be a move of
533            // `self.x`, but we auto-coerce it to `foo(&mut *self.x)`,
534            // which is a borrow.
535            if !mutbl_b.is_not() {
    ::core::panicking::panic("assertion failed: mutbl_b.is_not()")
};assert!(mutbl_b.is_not()); // can only coerce &T -> &U
536            return success(::alloc::vec::Vec::new()vec![], coerced_a, obligations);
537        }
538
539        let InferOk { value: mut adjustments, obligations: o } =
540            self.adjust_steps_as_infer_ok(&autoderef);
541        obligations.extend(o);
542        obligations.extend(autoderef.into_obligations());
543
544        if !#[allow(non_exhaustive_omitted_patterns)] match coerced_a.kind() {
            ty::Ref(..) => true,
            _ => false,
        } {
    {
        ::core::panicking::panic_fmt(format_args!("expected a ref type, got {0:?}",
                coerced_a));
    }
};assert!(
545            matches!(coerced_a.kind(), ty::Ref(..)),
546            "expected a ref type, got {:?}",
547            coerced_a
548        );
549
550        // Now apply the autoref
551        let mutbl = AutoBorrowMutability::new(mutbl_b, self.allow_two_phase);
552        adjustments
553            .push(Adjustment { kind: Adjust::Borrow(AutoBorrow::Ref(mutbl)), target: coerced_a });
554
555        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:555",
                        "rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
                        ::tracing_core::__macro_support::Option::Some(555u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
                        ::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};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("coerce_to_ref: succeeded coerced_a={0:?} adjustments={1:?}",
                                                    coerced_a, adjustments) as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!("coerce_to_ref: succeeded coerced_a={:?} adjustments={:?}", coerced_a, adjustments);
556
557        success(adjustments, coerced_a, obligations)
558    }
559
560    /// Performs [unsized coercion] by emulating a fulfillment loop on a
561    /// `CoerceUnsized` goal until all `CoerceUnsized` and `Unsize` goals
562    /// are successfully selected.
563    ///
564    /// [unsized coercion](https://doc.rust-lang.org/reference/type-coercions.html#unsized-coercions)
565    {}
#[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("coerce_unsized",
                                    "rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
                                    ::tracing_core::__macro_support::Option::Some(565u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
                                    ::tracing_core::field::FieldSet::new(&[{
                                                        const NAME:
                                                            ::tracing::__macro_support::FieldName<{
                                                                ::tracing::__macro_support::FieldName::len("source")
                                                            }> =
                                                            ::tracing::__macro_support::FieldName::new("source");
                                                        NAME.as_str()
                                                    },
                                                    {
                                                        const NAME:
                                                            ::tracing::__macro_support::FieldName<{
                                                                ::tracing::__macro_support::FieldName::len("target")
                                                            }> =
                                                            ::tracing::__macro_support::FieldName::new("target");
                                                        NAME.as_str()
                                                    }], ::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};
                                meta.fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&source)
                                                            as &dyn ::tracing::field::Value)),
                                                (::tracing::__macro_support::Option::Some(&::tracing::field::debug(&target)
                                                            as &dyn ::tracing::field::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: CoerceResult<'tcx> = loop {};
            return __tracing_attr_fake_return;
        }
        {
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:567",
                                    "rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
                                    ::tracing_core::__macro_support::Option::Some(567u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
                                    ::tracing_core::field::FieldSet::new(&[{
                                                        const NAME:
                                                            ::tracing::__macro_support::FieldName<{
                                                                ::tracing::__macro_support::FieldName::len("source")
                                                            }> =
                                                            ::tracing::__macro_support::FieldName::new("source");
                                                        NAME.as_str()
                                                    },
                                                    {
                                                        const NAME:
                                                            ::tracing::__macro_support::FieldName<{
                                                                ::tracing::__macro_support::FieldName::len("target")
                                                            }> =
                                                            ::tracing::__macro_support::FieldName::new("target");
                                                        NAME.as_str()
                                                    }], ::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};
                            __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&source)
                                                        as &dyn ::tracing::field::Value)),
                                            (::tracing::__macro_support::Option::Some(&::tracing::field::debug(&target)
                                                        as &dyn ::tracing::field::Value))])
                        });
                } else { ; }
            };
            if true {
                if !(self.shallow_resolve(source) == source) {
                    ::core::panicking::panic("assertion failed: self.shallow_resolve(source) == source")
                };
            };
            if true {
                if !(self.shallow_resolve(target) == target) {
                    ::core::panicking::panic("assertion failed: self.shallow_resolve(target) == target")
                };
            };
            if source.is_ty_var() {
                {
                    use ::tracing::__macro_support::Callsite as _;
                    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                        {
                            static META: ::tracing::Metadata<'static> =
                                {
                                    ::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:575",
                                        "rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
                                        ::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
                                        ::tracing_core::__macro_support::Option::Some(575u32),
                                        ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
                                        ::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};
                                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("coerce_unsized: source is a TyVar, bailing out")
                                                            as &dyn ::tracing::field::Value))])
                            });
                    } else { ; }
                };
                return Err(TypeError::Mismatch);
            }
            if target.is_ty_var() {
                {
                    use ::tracing::__macro_support::Callsite as _;
                    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                        {
                            static META: ::tracing::Metadata<'static> =
                                {
                                    ::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:579",
                                        "rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
                                        ::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
                                        ::tracing_core::__macro_support::Option::Some(579u32),
                                        ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
                                        ::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};
                                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("coerce_unsized: target is a TyVar, bailing out")
                                                            as &dyn ::tracing::field::Value))])
                            });
                    } else { ; }
                };
                return Err(TypeError::Mismatch);
            }
            match target.kind() {
                ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::Float(_)
                    | ty::Infer(ty::IntVar(_) | ty::FloatVar(_)) | ty::Str |
                    ty::Array(_, _) | ty::Slice(_) | ty::FnDef(_, _) |
                    ty::FnPtr(_, _) | ty::Dynamic(_, _) | ty::Closure(_, _) |
                    ty::CoroutineClosure(_, _) | ty::Coroutine(_, _) |
                    ty::CoroutineWitness(_, _) | ty::Never | ty::Tuple(_) =>
                    return Err(TypeError::Mismatch),
                ty::Adt(adt, args) =>
                    match adt.adt_kind() {
                        ty::AdtKind::Enum | ty::AdtKind::Union =>
                            return Err(TypeError::Mismatch),
                        ty::AdtKind::Struct if args.is_empty() =>
                            return Err(TypeError::Mismatch),
                        _ => {}
                    },
                _ => {}
            }
            if let ty::Ref(_, source_pointee, ty::Mutability::Not) =
                                *source.kind() && source_pointee.is_str() &&
                        let ty::Ref(_, target_pointee, ty::Mutability::Not) =
                            *target.kind() && target_pointee.is_str() {
                return Err(TypeError::Mismatch);
            }
            let traits =
                (self.tcx.lang_items().unsize_trait(),
                    self.tcx.lang_items().coerce_unsized_trait());
            let (Some(unsize_did), Some(coerce_unsized_did)) =
                traits else {
                    {
                        use ::tracing::__macro_support::Callsite as _;
                        static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                            {
                                static META: ::tracing::Metadata<'static> =
                                    {
                                        ::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:645",
                                            "rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
                                            ::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
                                            ::tracing_core::__macro_support::Option::Some(645u32),
                                            ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
                                            ::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};
                                    __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("missing Unsize or CoerceUnsized traits")
                                                                as &dyn ::tracing::field::Value))])
                                });
                        } else { ; }
                    };
                    return Err(TypeError::Mismatch);
                };
            self.commit_if_ok(|_|
                    {
                        let reborrow =
                            match (source.kind(), target.kind()) {
                                (&ty::Ref(_, ty_a, mutbl_a), &ty::Ref(_, _, mutbl_b)) => {
                                    coerce_mutbls(mutbl_a, mutbl_b)?;
                                    let coercion =
                                        RegionVariableOrigin::Coercion(self.cause.span);
                                    let r_borrow = self.next_region_var(coercion);
                                    let mutbl =
                                        AutoBorrowMutability::new(mutbl_b, AllowTwoPhase::No);
                                    Some((Adjustment {
                                                kind: Adjust::Deref(DerefAdjustKind::Builtin),
                                                target: ty_a,
                                            },
                                            Adjustment {
                                                kind: Adjust::Borrow(AutoBorrow::Ref(mutbl)),
                                                target: Ty::new_ref(self.tcx, r_borrow, ty_a, mutbl_b),
                                            }))
                                }
                                (&ty::Ref(_, ty_a, mt_a), &ty::RawPtr(_, mt_b)) => {
                                    coerce_mutbls(mt_a, mt_b)?;
                                    Some((Adjustment {
                                                kind: Adjust::Deref(DerefAdjustKind::Builtin),
                                                target: ty_a,
                                            },
                                            Adjustment {
                                                kind: Adjust::Borrow(AutoBorrow::RawPtr(mt_b)),
                                                target: Ty::new_ptr(self.tcx, ty_a, mt_b),
                                            }))
                                }
                                _ => None,
                            };
                        let coerce_source =
                            reborrow.as_ref().map_or(source, |(_, r)| r.target);
                        let coerce_target = self.next_ty_var(self.cause.span);
                        let mut coercion =
                            self.unify_and(coerce_target, target,
                                    reborrow.map(|(deref, autoref)|
                                                [deref, autoref]).into_flat_iter(),
                                    Adjust::Pointer(PointerCoercion::Unsize),
                                    ForceLeakCheck::No)?;
                        let cause =
                            self.cause(self.cause.span,
                                ObligationCauseCode::Coercion { source, target });
                        let pred =
                            ty::TraitRef::new(self.tcx, coerce_unsized_did,
                                [coerce_source, coerce_target]);
                        let obligation =
                            Obligation::new(self.tcx, cause, self.fcx.param_env, pred);
                        if self.next_trait_solver() {
                            coercion.obligations.push(obligation);
                            if self.infcx.visit_proof_tree(Goal::new(self.tcx,
                                            self.param_env, pred),
                                        &mut CoerceVisitor {
                                                fcx: self.fcx,
                                                span: self.cause.span,
                                                errored: false,
                                            }).is_break() {
                                return Err(TypeError::Mismatch);
                            }
                        } else {
                            self.coerce_unsized_old_solver(obligation, &mut coercion,
                                    coerce_unsized_did, unsize_did)?;
                        }
                        Ok(coercion)
                    })
        }
    }
}#[instrument(skip(self), level = "debug")]
566    fn coerce_unsized(&self, source: Ty<'tcx>, target: Ty<'tcx>) -> CoerceResult<'tcx> {
567        debug!(?source, ?target);
568        debug_assert!(self.shallow_resolve(source) == source);
569        debug_assert!(self.shallow_resolve(target) == target);
570
571        // We don't apply any coercions incase either the source or target
572        // aren't sufficiently well known but tend to instead just equate
573        // them both.
574        if source.is_ty_var() {
575            debug!("coerce_unsized: source is a TyVar, bailing out");
576            return Err(TypeError::Mismatch);
577        }
578        if target.is_ty_var() {
579            debug!("coerce_unsized: target is a TyVar, bailing out");
580            return Err(TypeError::Mismatch);
581        }
582
583        // This is an optimization because coercion is one of the most common
584        // operations that we do in typeck, since it happens at every assignment
585        // and call arg (among other positions).
586        //
587        // These targets are known to never be RHS in `LHS: CoerceUnsized<RHS>`.
588        // That's because one of the following is true.
589        // - For the built-in types, a core-provided impl doesn't exist and a
590        //   user-written impl is invalid.
591        // - For enums and unions, they can never be the target of a
592        //   `CoerceUnsized` impl.
593        // - For structs, they can only be the target of a `CoerceUnsized` impl
594        //   if `LHS` and `RHS` are instances of the same struct type but with
595        //   at least one differing field type. This is impossible when we
596        //   exclude structs with any generic args.
597        //
598        // This is technically incomplete when users write impossible bounds like
599        // `where T: CoerceUnsized<usize>`, for example, but that trait is unstable
600        // and coercion is allowed to be incomplete. The only case where this matters
601        // is impossible bounds.
602        //
603        // Note that some of these types implement `LHS: Unsize<RHS>`, but they
604        // do not implement *`CoerceUnsized`* which is the root obligation of the
605        // check below.
606        match target.kind() {
607            ty::Bool
608            | ty::Char
609            | ty::Int(_)
610            | ty::Uint(_)
611            | ty::Float(_)
612            | ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
613            | ty::Str
614            | ty::Array(_, _)
615            | ty::Slice(_)
616            | ty::FnDef(_, _)
617            | ty::FnPtr(_, _)
618            | ty::Dynamic(_, _)
619            | ty::Closure(_, _)
620            | ty::CoroutineClosure(_, _)
621            | ty::Coroutine(_, _)
622            | ty::CoroutineWitness(_, _)
623            | ty::Never
624            | ty::Tuple(_) => return Err(TypeError::Mismatch),
625            ty::Adt(adt, args) => match adt.adt_kind() {
626                ty::AdtKind::Enum | ty::AdtKind::Union => return Err(TypeError::Mismatch),
627                ty::AdtKind::Struct if args.is_empty() => return Err(TypeError::Mismatch),
628                _ => {}
629            },
630            _ => {}
631        }
632        // `&str: CoerceUnsized<&str>` does not hold but is encountered frequently
633        // so we fast path bail out here
634        if let ty::Ref(_, source_pointee, ty::Mutability::Not) = *source.kind()
635            && source_pointee.is_str()
636            && let ty::Ref(_, target_pointee, ty::Mutability::Not) = *target.kind()
637            && target_pointee.is_str()
638        {
639            return Err(TypeError::Mismatch);
640        }
641
642        let traits =
643            (self.tcx.lang_items().unsize_trait(), self.tcx.lang_items().coerce_unsized_trait());
644        let (Some(unsize_did), Some(coerce_unsized_did)) = traits else {
645            debug!("missing Unsize or CoerceUnsized traits");
646            return Err(TypeError::Mismatch);
647        };
648
649        // Note, we want to avoid unnecessary unsizing. We don't want to coerce to
650        // a DST unless we have to. This currently comes out in the wash since
651        // we can't unify [T] with U. But to properly support DST, we need to allow
652        // that, at which point we will need extra checks on the target here.
653        //
654        // NOTE: this is wrapped in a `commit_if_ok` because it creates
655        // a "spurious" type variable, and we don't want to have that
656        // type variable in memory if the coercion fails.
657        self.commit_if_ok(|_| {
658            // Handle reborrows before selecting `Source: CoerceUnsized<Target>`.
659            let reborrow = match (source.kind(), target.kind()) {
660                (&ty::Ref(_, ty_a, mutbl_a), &ty::Ref(_, _, mutbl_b)) => {
661                    coerce_mutbls(mutbl_a, mutbl_b)?;
662
663                    let coercion = RegionVariableOrigin::Coercion(self.cause.span);
664                    let r_borrow = self.next_region_var(coercion);
665
666                    // We don't allow two-phase borrows here, at least for initial
667                    // implementation. If it happens that this coercion is a function argument,
668                    // the reborrow in coerce_borrowed_ptr will pick it up.
669                    let mutbl = AutoBorrowMutability::new(mutbl_b, AllowTwoPhase::No);
670
671                    Some((
672                        Adjustment { kind: Adjust::Deref(DerefAdjustKind::Builtin), target: ty_a },
673                        Adjustment {
674                            kind: Adjust::Borrow(AutoBorrow::Ref(mutbl)),
675                            target: Ty::new_ref(self.tcx, r_borrow, ty_a, mutbl_b),
676                        },
677                    ))
678                }
679                (&ty::Ref(_, ty_a, mt_a), &ty::RawPtr(_, mt_b)) => {
680                    coerce_mutbls(mt_a, mt_b)?;
681
682                    Some((
683                        Adjustment { kind: Adjust::Deref(DerefAdjustKind::Builtin), target: ty_a },
684                        Adjustment {
685                            kind: Adjust::Borrow(AutoBorrow::RawPtr(mt_b)),
686                            target: Ty::new_ptr(self.tcx, ty_a, mt_b),
687                        },
688                    ))
689                }
690                _ => None,
691            };
692            let coerce_source = reborrow.as_ref().map_or(source, |(_, r)| r.target);
693
694            // Setup either a subtyping or a LUB relationship between
695            // the `CoerceUnsized` target type and the expected type.
696            // We only have the latter, so we use an inference variable
697            // for the former and let type inference do the rest.
698            let coerce_target = self.next_ty_var(self.cause.span);
699
700            let mut coercion = self.unify_and(
701                coerce_target,
702                target,
703                reborrow.map(|(deref, autoref)| [deref, autoref]).into_flat_iter(),
704                Adjust::Pointer(PointerCoercion::Unsize),
705                ForceLeakCheck::No,
706            )?;
707
708            // Create an obligation for `Source: CoerceUnsized<Target>`.
709            let cause =
710                self.cause(self.cause.span, ObligationCauseCode::Coercion { source, target });
711            let pred =
712                ty::TraitRef::new(self.tcx, coerce_unsized_did, [coerce_source, coerce_target]);
713            let obligation = Obligation::new(self.tcx, cause, self.fcx.param_env, pred);
714
715            if self.next_trait_solver() {
716                coercion.obligations.push(obligation);
717
718                if self
719                    .infcx
720                    .visit_proof_tree(
721                        Goal::new(self.tcx, self.param_env, pred),
722                        &mut CoerceVisitor { fcx: self.fcx, span: self.cause.span, errored: false },
723                    )
724                    .is_break()
725                {
726                    return Err(TypeError::Mismatch);
727                }
728            } else {
729                self.coerce_unsized_old_solver(
730                    obligation,
731                    &mut coercion,
732                    coerce_unsized_did,
733                    unsize_did,
734                )?;
735            }
736
737            Ok(coercion)
738        })
739    }
740
741    fn coerce_unsized_old_solver(
742        &self,
743        obligation: Obligation<'tcx, ty::Predicate<'tcx>>,
744        coercion: &mut InferOk<'tcx, (Vec<Adjustment<'tcx>>, Ty<'tcx>)>,
745        coerce_unsized_did: DefId,
746        unsize_did: DefId,
747    ) -> Result<(), TypeError<'tcx>> {
748        let mut selcx = traits::SelectionContext::new(self);
749        // Use a FIFO queue for this custom fulfillment procedure.
750        //
751        // A Vec (or SmallVec) is not a natural choice for a queue. However,
752        // this code path is hot, and this queue usually has a max length of 1
753        // and almost never more than 3. By using a SmallVec we avoid an
754        // allocation, at the (very small) cost of (occasionally) having to
755        // shift subsequent elements down when removing the front element.
756        let mut queue: SmallVec<[PredicateObligation<'tcx>; 4]> = {
    let count = 0usize + 1usize;
    let mut vec = ::smallvec::SmallVec::new();
    if count <= vec.inline_size() {
        vec.push(obligation);
        vec
    } else {
        ::smallvec::SmallVec::from_vec(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
                    [obligation])))
    }
}smallvec![obligation];
757
758        // Keep resolving `CoerceUnsized` and `Unsize` predicates to avoid
759        // emitting a coercion in cases like `Foo<$1>` -> `Foo<$2>`, where
760        // inference might unify those two inner type variables later.
761        let traits = [coerce_unsized_did, unsize_did];
762        while !queue.is_empty() {
763            let obligation = queue.remove(0);
764            let trait_pred = match obligation.predicate.kind().no_bound_vars() {
765                Some(ty::PredicateKind::Clause(ty::ClauseKind::Trait(trait_pred)))
766                    if traits.contains(&trait_pred.def_id()) =>
767                {
768                    self.deeply_resolve_ignoring_regions(trait_pred)
769                }
770                _ => {
771                    coercion.obligations.push(obligation);
772                    continue;
773                }
774            };
775            {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:775",
                        "rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
                        ::tracing_core::__macro_support::Option::Some(775u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
                        ::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};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("coerce_unsized resolve step: {0:?}",
                                                    trait_pred) as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!("coerce_unsized resolve step: {:?}", trait_pred);
776            match selcx.select(&obligation.with(selcx.tcx(), trait_pred)) {
777                // Uncertain or unimplemented.
778                Ok(None) => {
779                    if trait_pred.def_id() == unsize_did {
780                        let self_ty = trait_pred.self_ty();
781                        let unsize_ty = trait_pred.trait_ref.args[1].expect_ty();
782                        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:782",
                        "rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
                        ::tracing_core::__macro_support::Option::Some(782u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
                        ::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};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("coerce_unsized: ambiguous unsize case for {0:?}",
                                                    trait_pred) as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!("coerce_unsized: ambiguous unsize case for {:?}", trait_pred);
783                        match (self_ty.kind(), unsize_ty.kind()) {
784                            (&ty::Infer(ty::TyVar(v)), ty::Dynamic(..))
785                                if self.type_var_is_sized(v) =>
786                            {
787                                {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:787",
                        "rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
                        ::tracing_core::__macro_support::Option::Some(787u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
                        ::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};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("coerce_unsized: have sized infer {0:?}",
                                                    v) as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!("coerce_unsized: have sized infer {:?}", v);
788                                coercion.obligations.push(obligation);
789                                // `$0: Unsize<dyn Trait>` where we know that `$0: Sized`, try going
790                                // for unsizing.
791                            }
792                            _ => {
793                                // Some other case for `$0: Unsize<Something>`. Note that we
794                                // hit this case even if `Something` is a sized type, so just
795                                // don't do the coercion.
796                                {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:796",
                        "rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
                        ::tracing_core::__macro_support::Option::Some(796u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
                        ::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};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("coerce_unsized: ambiguous unsize")
                                            as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!("coerce_unsized: ambiguous unsize");
797                                return Err(TypeError::Mismatch);
798                            }
799                        }
800                    } else {
801                        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:801",
                        "rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
                        ::tracing_core::__macro_support::Option::Some(801u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
                        ::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};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("coerce_unsized: early return - ambiguous")
                                            as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!("coerce_unsized: early return - ambiguous");
802                        return Err(TypeError::Mismatch);
803                    }
804                }
805                Err(SelectionError::Unimplemented) => {
806                    {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:806",
                        "rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
                        ::tracing_core::__macro_support::Option::Some(806u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
                        ::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};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("coerce_unsized: early return - can\'t prove obligation")
                                            as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!("coerce_unsized: early return - can't prove obligation");
807                    return Err(TypeError::Mismatch);
808                }
809
810                Err(SelectionError::TraitDynIncompatible(_)) => {
811                    // Dyn compatibility errors in coercion will *always* be due to the
812                    // fact that the RHS of the coercion is a non-dyn compatible `dyn Trait`
813                    // written in source somewhere (otherwise we will never have lowered
814                    // the dyn trait from HIR to middle).
815                    //
816                    // There's no reason to emit yet another dyn compatibility error,
817                    // especially since the span will differ slightly and thus not be
818                    // deduplicated at all!
819                    self.fcx.set_tainted_by_errors(
820                        self.fcx
821                            .dcx()
822                            .span_delayed_bug(self.cause.span, "dyn compatibility during coercion"),
823                    );
824                }
825                Err(err) => {
826                    let guar = self.err_ctxt().report_selection_error(
827                        obligation.clone(),
828                        &obligation,
829                        &err,
830                    );
831                    self.fcx.set_tainted_by_errors(guar);
832                    // Treat this like an obligation and follow through
833                    // with the unsizing - the lack of a coercion should
834                    // be silent, as it causes a type mismatch later.
835                }
836                Ok(Some(ImplSource::UserDefined(impl_source))) => {
837                    queue.extend(impl_source.nested);
838                    // Certain incoherent `CoerceUnsized` implementations may cause ICEs,
839                    // so check the impl's validity. Taint the body so that we don't try
840                    // to evaluate these invalid coercions in CTFE. We only need to do this
841                    // for local impls, since upstream impls should be valid.
842                    if impl_source.impl_def_id.is_local()
843                        && let Err(guar) =
844                            self.tcx.ensure_result().coerce_unsized_info(impl_source.impl_def_id)
845                    {
846                        self.fcx.set_tainted_by_errors(guar);
847                    }
848                }
849                Ok(Some(impl_source)) => queue.extend(impl_source.nested_obligations()),
850            }
851        }
852
853        Ok(())
854    }
855
856    /// Create an obligation for `ty: Unpin`, where .
857    fn unpin_obligation(
858        &self,
859        source: Ty<'tcx>,
860        target: Ty<'tcx>,
861        ty: Ty<'tcx>,
862    ) -> PredicateObligation<'tcx> {
863        let pred = ty::TraitRef::new(
864            self.tcx,
865            self.tcx.require_lang_item(LangItem::Unpin, self.cause.span),
866            [ty],
867        );
868        let cause = self.cause(self.cause.span, ObligationCauseCode::Coercion { source, target });
869        PredicateObligation::new(self.tcx, cause, self.param_env, pred)
870    }
871
872    /// Checks if the given types are compatible for coercion from a pinned reference to a normal reference.
873    fn maybe_pin_ref_to_ref(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> Option<CoerceMaybePinnedRef<'tcx>> {
874        if !self.tcx.features().pin_ergonomics() {
875            return None;
876        }
877        if let Some((a_ty, a_pin @ ty::Pinnedness::Pinned, a_mut, a_r)) = a.maybe_pinned_ref()
878            && let Some((_, b_pin @ ty::Pinnedness::Not, b_mut, _)) = b.maybe_pinned_ref()
879        {
880            return Some(CoerceMaybePinnedRef { a, b, a_ty, a_pin, a_mut, a_r, b_pin, b_mut });
881        }
882        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:882",
                        "rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
                        ::tracing_core::__macro_support::Option::Some(882u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
                        ::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};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("not fitting pinned ref to ref coercion (`{0:?}` -> `{1:?}`)",
                                                    a, b) as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!("not fitting pinned ref to ref coercion (`{:?}` -> `{:?}`)", a, b);
883        None
884    }
885
886    /// Coerces from a pinned reference to a normal reference.
887    {}
#[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::TRACE <=
                    ::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("coerce_pin_ref_to_ref",
                                    "rustc_hir_typeck::coercion", ::tracing::Level::TRACE,
                                    ::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
                                    ::tracing_core::__macro_support::Option::Some(887u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
                                    ::tracing_core::field::FieldSet::new(&[{
                                                        const NAME:
                                                            ::tracing::__macro_support::FieldName<{
                                                                ::tracing::__macro_support::FieldName::len("a")
                                                            }> =
                                                            ::tracing::__macro_support::FieldName::new("a");
                                                        NAME.as_str()
                                                    },
                                                    {
                                                        const NAME:
                                                            ::tracing::__macro_support::FieldName<{
                                                                ::tracing::__macro_support::FieldName::len("b")
                                                            }> =
                                                            ::tracing::__macro_support::FieldName::new("b");
                                                        NAME.as_str()
                                                    },
                                                    {
                                                        const NAME:
                                                            ::tracing::__macro_support::FieldName<{
                                                                ::tracing::__macro_support::FieldName::len("a_ty")
                                                            }> =
                                                            ::tracing::__macro_support::FieldName::new("a_ty");
                                                        NAME.as_str()
                                                    },
                                                    {
                                                        const NAME:
                                                            ::tracing::__macro_support::FieldName<{
                                                                ::tracing::__macro_support::FieldName::len("a_pin")
                                                            }> =
                                                            ::tracing::__macro_support::FieldName::new("a_pin");
                                                        NAME.as_str()
                                                    },
                                                    {
                                                        const NAME:
                                                            ::tracing::__macro_support::FieldName<{
                                                                ::tracing::__macro_support::FieldName::len("a_mut")
                                                            }> =
                                                            ::tracing::__macro_support::FieldName::new("a_mut");
                                                        NAME.as_str()
                                                    },
                                                    {
                                                        const NAME:
                                                            ::tracing::__macro_support::FieldName<{
                                                                ::tracing::__macro_support::FieldName::len("a_r")
                                                            }> =
                                                            ::tracing::__macro_support::FieldName::new("a_r");
                                                        NAME.as_str()
                                                    },
                                                    {
                                                        const NAME:
                                                            ::tracing::__macro_support::FieldName<{
                                                                ::tracing::__macro_support::FieldName::len("b_pin")
                                                            }> =
                                                            ::tracing::__macro_support::FieldName::new("b_pin");
                                                        NAME.as_str()
                                                    },
                                                    {
                                                        const NAME:
                                                            ::tracing::__macro_support::FieldName<{
                                                                ::tracing::__macro_support::FieldName::len("b_mut")
                                                            }> =
                                                            ::tracing::__macro_support::FieldName::new("b_mut");
                                                        NAME.as_str()
                                                    }], ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::TRACE <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::TRACE <=
                                    ::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};
                                meta.fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&a)
                                                            as &dyn ::tracing::field::Value)),
                                                (::tracing::__macro_support::Option::Some(&::tracing::field::debug(&b)
                                                            as &dyn ::tracing::field::Value)),
                                                (::tracing::__macro_support::Option::Some(&::tracing::field::debug(&a_ty)
                                                            as &dyn ::tracing::field::Value)),
                                                (::tracing::__macro_support::Option::Some(&::tracing::field::debug(&a_pin)
                                                            as &dyn ::tracing::field::Value)),
                                                (::tracing::__macro_support::Option::Some(&::tracing::field::debug(&a_mut)
                                                            as &dyn ::tracing::field::Value)),
                                                (::tracing::__macro_support::Option::Some(&::tracing::field::debug(&a_r)
                                                            as &dyn ::tracing::field::Value)),
                                                (::tracing::__macro_support::Option::Some(&::tracing::field::debug(&b_pin)
                                                            as &dyn ::tracing::field::Value)),
                                                (::tracing::__macro_support::Option::Some(&::tracing::field::debug(&b_mut)
                                                            as &dyn ::tracing::field::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: CoerceResult<'tcx> = loop {};
            return __tracing_attr_fake_return;
        }
        {
            if true {
                if !(self.shallow_resolve(a) == a) {
                    ::core::panicking::panic("assertion failed: self.shallow_resolve(a) == a")
                };
            };
            if true {
                if !(self.shallow_resolve(b) == b) {
                    ::core::panicking::panic("assertion failed: self.shallow_resolve(b) == b")
                };
            };
            if true {
                if !self.tcx.features().pin_ergonomics() {
                    ::core::panicking::panic("assertion failed: self.tcx.features().pin_ergonomics()")
                };
            };
            if true {
                {
                    match (&a_pin, &ty::Pinnedness::Pinned) {
                        (left_val, right_val) => {
                            if !(*left_val == *right_val) {
                                let kind = ::core::panicking::AssertKind::Eq;
                                ::core::panicking::assert_failed(kind, &*left_val,
                                    &*right_val, ::core::option::Option::None);
                            }
                        }
                    }
                };
            };
            if true {
                {
                    match (&b_pin, &ty::Pinnedness::Not) {
                        (left_val, right_val) => {
                            if !(*left_val == *right_val) {
                                let kind = ::core::panicking::AssertKind::Eq;
                                ::core::panicking::assert_failed(kind, &*left_val,
                                    &*right_val, ::core::option::Option::None);
                            }
                        }
                    }
                };
            };
            coerce_mutbls(a_mut, b_mut)?;
            let unpin_obligation = self.unpin_obligation(a, b, a_ty);
            let a = Ty::new_ref(self.tcx, a_r, a_ty, b_mut);
            let mut coerce =
                self.unify_and(a, b,
                        [Adjustment {
                                    kind: Adjust::Deref(DerefAdjustKind::Pin),
                                    target: a_ty,
                                }],
                        Adjust::Borrow(AutoBorrow::Ref(AutoBorrowMutability::new(b_mut,
                                    self.allow_two_phase))), ForceLeakCheck::No)?;
            coerce.obligations.push(unpin_obligation);
            Ok(coerce)
        }
    }
}#[instrument(skip(self), level = "trace")]
888    fn coerce_pin_ref_to_ref(
889        &self,
890        CoerceMaybePinnedRef { a, b, a_ty, a_pin, a_mut, a_r, b_pin, b_mut }: CoerceMaybePinnedRef<
891            'tcx,
892        >,
893    ) -> CoerceResult<'tcx> {
894        debug_assert!(self.shallow_resolve(a) == a);
895        debug_assert!(self.shallow_resolve(b) == b);
896        debug_assert!(self.tcx.features().pin_ergonomics());
897        debug_assert_eq!(a_pin, ty::Pinnedness::Pinned);
898        debug_assert_eq!(b_pin, ty::Pinnedness::Not);
899
900        coerce_mutbls(a_mut, b_mut)?;
901
902        let unpin_obligation = self.unpin_obligation(a, b, a_ty);
903
904        let a = Ty::new_ref(self.tcx, a_r, a_ty, b_mut);
905        let mut coerce = self.unify_and(
906            a,
907            b,
908            [Adjustment { kind: Adjust::Deref(DerefAdjustKind::Pin), target: a_ty }],
909            Adjust::Borrow(AutoBorrow::Ref(AutoBorrowMutability::new(b_mut, self.allow_two_phase))),
910            ForceLeakCheck::No,
911        )?;
912        coerce.obligations.push(unpin_obligation);
913        Ok(coerce)
914    }
915
916    /// Checks if the given types are compatible for coercion to a pinned reference.
917    fn maybe_to_pin_ref(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> Option<CoerceMaybePinnedRef<'tcx>> {
918        if !self.tcx.features().pin_ergonomics() {
919            return None;
920        }
921        if let Some((a_ty, a_pin, a_mut, a_r)) = a.maybe_pinned_ref()
922            && let Some((_, b_pin @ ty::Pinnedness::Pinned, b_mut, _)) = b.maybe_pinned_ref()
923        {
924            return Some(CoerceMaybePinnedRef { a, b, a_ty, a_pin, a_mut, a_r, b_pin, b_mut });
925        }
926        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:926",
                        "rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
                        ::tracing_core::__macro_support::Option::Some(926u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
                        ::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};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("not fitting ref to pinned ref coercion (`{0:?}` -> `{1:?}`)",
                                                    a, b) as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!("not fitting ref to pinned ref coercion (`{:?}` -> `{:?}`)", a, b);
927        None
928    }
929
930    /// Applies reborrowing and auto-borrowing that results to `Pin<&T>` or `Pin<&mut T>`:
931    ///
932    /// Currently we only support the following coercions:
933    /// - Reborrowing `Pin<&mut T>` -> `Pin<&mut T>`
934    /// - Reborrowing `Pin<&T>` -> `Pin<&T>`
935    /// - Auto-borrowing `&mut T` -> `Pin<&mut T>` where `T: Unpin`
936    /// - Auto-borrowing `&mut T` -> `Pin<&T>` where `T: Unpin`
937    /// - Auto-borrowing `&T` -> `Pin<&T>` where `T: Unpin`
938    ///
939    /// In the future we might want to support other reborrowing coercions, such as:
940    /// - `Pin<Box<T>>` as `Pin<&T>`
941    /// - `Pin<Box<T>>` as `Pin<&mut T>`
942    {}
#[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::TRACE <=
                    ::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("coerce_to_pin_ref",
                                    "rustc_hir_typeck::coercion", ::tracing::Level::TRACE,
                                    ::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
                                    ::tracing_core::__macro_support::Option::Some(942u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
                                    ::tracing_core::field::FieldSet::new(&[{
                                                        const NAME:
                                                            ::tracing::__macro_support::FieldName<{
                                                                ::tracing::__macro_support::FieldName::len("a")
                                                            }> =
                                                            ::tracing::__macro_support::FieldName::new("a");
                                                        NAME.as_str()
                                                    },
                                                    {
                                                        const NAME:
                                                            ::tracing::__macro_support::FieldName<{
                                                                ::tracing::__macro_support::FieldName::len("b")
                                                            }> =
                                                            ::tracing::__macro_support::FieldName::new("b");
                                                        NAME.as_str()
                                                    },
                                                    {
                                                        const NAME:
                                                            ::tracing::__macro_support::FieldName<{
                                                                ::tracing::__macro_support::FieldName::len("a_ty")
                                                            }> =
                                                            ::tracing::__macro_support::FieldName::new("a_ty");
                                                        NAME.as_str()
                                                    },
                                                    {
                                                        const NAME:
                                                            ::tracing::__macro_support::FieldName<{
                                                                ::tracing::__macro_support::FieldName::len("a_pin")
                                                            }> =
                                                            ::tracing::__macro_support::FieldName::new("a_pin");
                                                        NAME.as_str()
                                                    },
                                                    {
                                                        const NAME:
                                                            ::tracing::__macro_support::FieldName<{
                                                                ::tracing::__macro_support::FieldName::len("a_mut")
                                                            }> =
                                                            ::tracing::__macro_support::FieldName::new("a_mut");
                                                        NAME.as_str()
                                                    },
                                                    {
                                                        const NAME:
                                                            ::tracing::__macro_support::FieldName<{
                                                                ::tracing::__macro_support::FieldName::len("a_r")
                                                            }> =
                                                            ::tracing::__macro_support::FieldName::new("a_r");
                                                        NAME.as_str()
                                                    },
                                                    {
                                                        const NAME:
                                                            ::tracing::__macro_support::FieldName<{
                                                                ::tracing::__macro_support::FieldName::len("b_pin")
                                                            }> =
                                                            ::tracing::__macro_support::FieldName::new("b_pin");
                                                        NAME.as_str()
                                                    },
                                                    {
                                                        const NAME:
                                                            ::tracing::__macro_support::FieldName<{
                                                                ::tracing::__macro_support::FieldName::len("b_mut")
                                                            }> =
                                                            ::tracing::__macro_support::FieldName::new("b_mut");
                                                        NAME.as_str()
                                                    }], ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::TRACE <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::TRACE <=
                                    ::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};
                                meta.fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&a)
                                                            as &dyn ::tracing::field::Value)),
                                                (::tracing::__macro_support::Option::Some(&::tracing::field::debug(&b)
                                                            as &dyn ::tracing::field::Value)),
                                                (::tracing::__macro_support::Option::Some(&::tracing::field::debug(&a_ty)
                                                            as &dyn ::tracing::field::Value)),
                                                (::tracing::__macro_support::Option::Some(&::tracing::field::debug(&a_pin)
                                                            as &dyn ::tracing::field::Value)),
                                                (::tracing::__macro_support::Option::Some(&::tracing::field::debug(&a_mut)
                                                            as &dyn ::tracing::field::Value)),
                                                (::tracing::__macro_support::Option::Some(&::tracing::field::debug(&a_r)
                                                            as &dyn ::tracing::field::Value)),
                                                (::tracing::__macro_support::Option::Some(&::tracing::field::debug(&b_pin)
                                                            as &dyn ::tracing::field::Value)),
                                                (::tracing::__macro_support::Option::Some(&::tracing::field::debug(&b_mut)
                                                            as &dyn ::tracing::field::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: CoerceResult<'tcx> = loop {};
            return __tracing_attr_fake_return;
        }
        {
            if true {
                if !(self.shallow_resolve(a) == a) {
                    ::core::panicking::panic("assertion failed: self.shallow_resolve(a) == a")
                };
            };
            if true {
                if !(self.shallow_resolve(b) == b) {
                    ::core::panicking::panic("assertion failed: self.shallow_resolve(b) == b")
                };
            };
            if true {
                if !self.tcx.features().pin_ergonomics() {
                    ::core::panicking::panic("assertion failed: self.tcx.features().pin_ergonomics()")
                };
            };
            if true {
                {
                    match (&b_pin, &ty::Pinnedness::Pinned) {
                        (left_val, right_val) => {
                            if !(*left_val == *right_val) {
                                let kind = ::core::panicking::AssertKind::Eq;
                                ::core::panicking::assert_failed(kind, &*left_val,
                                    &*right_val, ::core::option::Option::None);
                            }
                        }
                    }
                };
            };
            let (deref, unpin_obligation) =
                match a_pin {
                    ty::Pinnedness::Pinned => (DerefAdjustKind::Pin, None),
                    ty::Pinnedness::Not => {
                        (DerefAdjustKind::Builtin,
                            Some(self.unpin_obligation(a, b, a_ty)))
                    }
                };
            coerce_mutbls(a_mut, b_mut)?;
            let a = Ty::new_pinned_ref(self.tcx, a_r, a_ty, b_mut);
            let mut coerce =
                self.unify_and(a, b,
                        [Adjustment { kind: Adjust::Deref(deref), target: a_ty }],
                        Adjust::Borrow(AutoBorrow::Pin(b_mut)),
                        ForceLeakCheck::No)?;
            coerce.obligations.extend(unpin_obligation);
            Ok(coerce)
        }
    }
}#[instrument(skip(self), level = "trace")]
943    fn coerce_to_pin_ref(
944        &self,
945        CoerceMaybePinnedRef { a, b, a_ty, a_pin, a_mut, a_r, b_pin, b_mut }: CoerceMaybePinnedRef<
946            'tcx,
947        >,
948    ) -> CoerceResult<'tcx> {
949        debug_assert!(self.shallow_resolve(a) == a);
950        debug_assert!(self.shallow_resolve(b) == b);
951        debug_assert!(self.tcx.features().pin_ergonomics());
952        debug_assert_eq!(b_pin, ty::Pinnedness::Pinned);
953
954        // We need to deref the reference first before we reborrow it to a pinned reference.
955        let (deref, unpin_obligation) = match a_pin {
956            // no `Unpin` required when reborrowing a pinned reference to a pinned reference
957            ty::Pinnedness::Pinned => (DerefAdjustKind::Pin, None),
958            // `Unpin` required when reborrowing a non-pinned reference to a pinned reference
959            ty::Pinnedness::Not => {
960                (DerefAdjustKind::Builtin, Some(self.unpin_obligation(a, b, a_ty)))
961            }
962        };
963
964        coerce_mutbls(a_mut, b_mut)?;
965
966        // update a with b's mutability since we'll be coercing mutability
967        let a = Ty::new_pinned_ref(self.tcx, a_r, a_ty, b_mut);
968
969        // To complete the reborrow, we need to make sure we can unify the inner types, and if so we
970        // add the adjustments.
971        let mut coerce = self.unify_and(
972            a,
973            b,
974            [Adjustment { kind: Adjust::Deref(deref), target: a_ty }],
975            Adjust::Borrow(AutoBorrow::Pin(b_mut)),
976            ForceLeakCheck::No,
977        )?;
978
979        coerce.obligations.extend(unpin_obligation);
980        Ok(coerce)
981    }
982
983    /// Applies generic exclusive reborrowing on type implementing `Reborrow`.
984    {}
#[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::TRACE <=
                    ::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("coerce_reborrow",
                                    "rustc_hir_typeck::coercion", ::tracing::Level::TRACE,
                                    ::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
                                    ::tracing_core::__macro_support::Option::Some(984u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
                                    ::tracing_core::field::FieldSet::new(&[{
                                                        const NAME:
                                                            ::tracing::__macro_support::FieldName<{
                                                                ::tracing::__macro_support::FieldName::len("a")
                                                            }> =
                                                            ::tracing::__macro_support::FieldName::new("a");
                                                        NAME.as_str()
                                                    },
                                                    {
                                                        const NAME:
                                                            ::tracing::__macro_support::FieldName<{
                                                                ::tracing::__macro_support::FieldName::len("b")
                                                            }> =
                                                            ::tracing::__macro_support::FieldName::new("b");
                                                        NAME.as_str()
                                                    }], ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::TRACE <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::TRACE <=
                                    ::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};
                                meta.fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&a)
                                                            as &dyn ::tracing::field::Value)),
                                                (::tracing::__macro_support::Option::Some(&::tracing::field::debug(&b)
                                                            as &dyn ::tracing::field::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: CoerceResult<'tcx> = loop {};
            return __tracing_attr_fake_return;
        }
        {
            if true {
                if !(self.shallow_resolve(a) == a) {
                    ::core::panicking::panic("assertion failed: self.shallow_resolve(a) == a")
                };
            };
            if true {
                if !(self.shallow_resolve(b) == b) {
                    ::core::panicking::panic("assertion failed: self.shallow_resolve(b) == b")
                };
            };
            let (ty::Adt(a_def, _), ty::Adt(b_def, _)) =
                (a.kind(),
                    b.kind()) else { return Err(TypeError::Mismatch); };
            if a_def.did() == b_def.did() {
                let coerce =
                    self.unify_and(a, b, [],
                            Adjust::GenericReborrow(ty::Mutability::Mut),
                            ForceLeakCheck::No)?;
                self.taint_if_reborrow_impls_are_invalid(ty::Mutability::Mut);
                Ok(coerce)
            } else { Err(TypeError::Mismatch) }
        }
    }
}#[instrument(skip(self), level = "trace")]
985    fn coerce_reborrow(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> CoerceResult<'tcx> {
986        debug_assert!(self.shallow_resolve(a) == a);
987        debug_assert!(self.shallow_resolve(b) == b);
988
989        // We need to make sure the two types are compatible for reborrow.
990        let (ty::Adt(a_def, _), ty::Adt(b_def, _)) = (a.kind(), b.kind()) else {
991            return Err(TypeError::Mismatch);
992        };
993        if a_def.did() == b_def.did() {
994            // Reborrow is applicable here
995            let coerce = self.unify_and(
996                a,
997                b,
998                [],
999                Adjust::GenericReborrow(ty::Mutability::Mut),
1000                ForceLeakCheck::No,
1001            )?;
1002            self.taint_if_reborrow_impls_are_invalid(ty::Mutability::Mut);
1003            Ok(coerce)
1004        } else {
1005            // FIXME: CoerceShared check goes here, error for now
1006            Err(TypeError::Mismatch)
1007        }
1008    }
1009
1010    /// Applies generic exclusive reborrowing on type implementing `Reborrow`.
1011    {}
#[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::TRACE <=
                    ::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("coerce_shared_reborrow",
                                    "rustc_hir_typeck::coercion", ::tracing::Level::TRACE,
                                    ::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
                                    ::tracing_core::__macro_support::Option::Some(1011u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
                                    ::tracing_core::field::FieldSet::new(&[{
                                                        const NAME:
                                                            ::tracing::__macro_support::FieldName<{
                                                                ::tracing::__macro_support::FieldName::len("a")
                                                            }> =
                                                            ::tracing::__macro_support::FieldName::new("a");
                                                        NAME.as_str()
                                                    },
                                                    {
                                                        const NAME:
                                                            ::tracing::__macro_support::FieldName<{
                                                                ::tracing::__macro_support::FieldName::len("b")
                                                            }> =
                                                            ::tracing::__macro_support::FieldName::new("b");
                                                        NAME.as_str()
                                                    }], ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::TRACE <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::TRACE <=
                                    ::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};
                                meta.fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&a)
                                                            as &dyn ::tracing::field::Value)),
                                                (::tracing::__macro_support::Option::Some(&::tracing::field::debug(&b)
                                                            as &dyn ::tracing::field::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: CoerceResult<'tcx> = loop {};
            return __tracing_attr_fake_return;
        }
        {
            if true {
                if !(self.shallow_resolve(a) == a) {
                    ::core::panicking::panic("assertion failed: self.shallow_resolve(a) == a")
                };
            };
            if true {
                if !(self.shallow_resolve(b) == b) {
                    ::core::panicking::panic("assertion failed: self.shallow_resolve(b) == b")
                };
            };
            let (ty::Adt(a_def, _), ty::Adt(b_def, _)) =
                (a.kind(),
                    b.kind()) else { return Err(TypeError::Mismatch); };
            if a_def.did() == b_def.did() { return Err(TypeError::Mismatch); }
            let Some(coerce_shared_trait_did) =
                self.tcx.lang_items().coerce_shared() else {
                    return Err(TypeError::Mismatch);
                };
            let coerce_shared_trait_ref =
                ty::TraitRef::new(self.tcx, coerce_shared_trait_did, [a, b]);
            let obligation =
                traits::Obligation::new(self.tcx, ObligationCause::dummy(),
                    self.param_env, ty::Binder::dummy(coerce_shared_trait_ref));
            let ocx = ObligationCtxt::new(&self.infcx);
            ocx.register_obligation(obligation);
            let errs = ocx.evaluate_obligations_error_on_ambiguity();
            if errs.no_errors() {
                self.taint_if_reborrow_impls_are_invalid(ty::Mutability::Not);
                Ok(InferOk {
                        value: (::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
                                    [Adjustment {
                                                kind: Adjust::GenericReborrow(ty::Mutability::Not),
                                                target: b,
                                            }])), b),
                        obligations: ocx.into_pending_obligations(),
                    })
            } else { Err(TypeError::Mismatch) }
        }
    }
}#[instrument(skip(self), level = "trace")]
1012    fn coerce_shared_reborrow(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> CoerceResult<'tcx> {
1013        debug_assert!(self.shallow_resolve(a) == a);
1014        debug_assert!(self.shallow_resolve(b) == b);
1015
1016        // We need to make sure the two types are compatible for reborrow.
1017        let (ty::Adt(a_def, _), ty::Adt(b_def, _)) = (a.kind(), b.kind()) else {
1018            return Err(TypeError::Mismatch);
1019        };
1020        if a_def.did() == b_def.did() {
1021            // CoerceShared cannot be T -> T.
1022            return Err(TypeError::Mismatch);
1023        }
1024        let Some(coerce_shared_trait_did) = self.tcx.lang_items().coerce_shared() else {
1025            return Err(TypeError::Mismatch);
1026        };
1027        let coerce_shared_trait_ref = ty::TraitRef::new(self.tcx, coerce_shared_trait_did, [a, b]);
1028        let obligation = traits::Obligation::new(
1029            self.tcx,
1030            ObligationCause::dummy(),
1031            self.param_env,
1032            ty::Binder::dummy(coerce_shared_trait_ref),
1033        );
1034        let ocx = ObligationCtxt::new(&self.infcx);
1035        ocx.register_obligation(obligation);
1036        let errs = ocx.evaluate_obligations_error_on_ambiguity();
1037        if errs.no_errors() {
1038            self.taint_if_reborrow_impls_are_invalid(ty::Mutability::Not);
1039            Ok(InferOk {
1040                value: (
1041                    vec![Adjustment {
1042                        kind: Adjust::GenericReborrow(ty::Mutability::Not),
1043                        target: b,
1044                    }],
1045                    b,
1046                ),
1047                obligations: ocx.into_pending_obligations(),
1048            })
1049        } else {
1050            Err(TypeError::Mismatch)
1051        }
1052    }
1053
1054    /// Borrowck and const eval lower a generic reborrow by walking the fields of the source and
1055    /// target types, trusting that coherence has validated the `Reborrow` and `CoerceShared` impls
1056    /// that permit it. If coherence rejected any of those impls, that walk can hit shapes it does
1057    /// not expect, so we taint the body to keep it away from them.
1058    fn taint_if_reborrow_impls_are_invalid(&self, mutability: ty::Mutability) {
1059        let lang_items = self.tcx.lang_items();
1060        let trait_def_ids = match mutability {
1061            ty::Mutability::Mut => [lang_items.reborrow(), None],
1062            // A shared reborrow also reborrows any fields of the source that implement `Reborrow`.
1063            ty::Mutability::Not => [lang_items.coerce_shared(), lang_items.reborrow()],
1064        };
1065        for trait_def_id in trait_def_ids.into_iter().flatten() {
1066            if let Err(guar) = self.tcx.ensure_result().coherent_trait(trait_def_id) {
1067                self.set_tainted_by_errors(guar);
1068            }
1069        }
1070    }
1071
1072    fn coerce_from_fn_pointer(
1073        &self,
1074        a: Ty<'tcx>,
1075        a_sig: ty::PolyFnSig<'tcx>,
1076        b: Ty<'tcx>,
1077    ) -> CoerceResult<'tcx> {
1078        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:1078",
                        "rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
                        ::tracing_core::__macro_support::Option::Some(1078u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
                        ::tracing_core::field::FieldSet::new(&["message",
                                        {
                                            const NAME:
                                                ::tracing::__macro_support::FieldName<{
                                                    ::tracing::__macro_support::FieldName::len("a_sig")
                                                }> =
                                                ::tracing::__macro_support::FieldName::new("a_sig");
                                            NAME.as_str()
                                        },
                                        {
                                            const NAME:
                                                ::tracing::__macro_support::FieldName<{
                                                    ::tracing::__macro_support::FieldName::len("b")
                                                }> =
                                                ::tracing::__macro_support::FieldName::new("b");
                                            NAME.as_str()
                                        }], ::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};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("coerce_from_fn_pointer")
                                            as &dyn ::tracing::field::Value)),
                                (::tracing::__macro_support::Option::Some(&::tracing::field::debug(&a_sig)
                                            as &dyn ::tracing::field::Value)),
                                (::tracing::__macro_support::Option::Some(&::tracing::field::debug(&b)
                                            as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!(?a_sig, ?b, "coerce_from_fn_pointer");
1079        if true {
    if !(self.shallow_resolve(b) == b) {
        ::core::panicking::panic("assertion failed: self.shallow_resolve(b) == b")
    };
};debug_assert!(self.shallow_resolve(b) == b);
1080
1081        match b.kind() {
1082            ty::FnPtr(_, b_hdr) if a_sig.safety().is_safe() && b_hdr.safety().is_unsafe() => {
1083                let a = self.tcx.safe_to_unsafe_fn_ty(a_sig);
1084                let adjust = Adjust::Pointer(PointerCoercion::UnsafeFnPointer);
1085                self.unify_and(a, b, [], adjust, ForceLeakCheck::Yes)
1086            }
1087            _ => self.unify(a, b, ForceLeakCheck::Yes),
1088        }
1089    }
1090
1091    fn coerce_from_fn_item(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> CoerceResult<'tcx> {
1092        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:1092",
                        "rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
                        ::tracing_core::__macro_support::Option::Some(1092u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
                        ::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};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("coerce_from_fn_item(a={0:?}, b={1:?})",
                                                    a, b) as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!("coerce_from_fn_item(a={:?}, b={:?})", a, b);
1093        if true {
    if !(self.shallow_resolve(a) == a) {
        ::core::panicking::panic("assertion failed: self.shallow_resolve(a) == a")
    };
};debug_assert!(self.shallow_resolve(a) == a);
1094        if true {
    if !(self.shallow_resolve(b) == b) {
        ::core::panicking::panic("assertion failed: self.shallow_resolve(b) == b")
    };
};debug_assert!(self.shallow_resolve(b) == b);
1095
1096        match b.kind() {
1097            ty::FnPtr(_, b_hdr) => {
1098                let a_sig = self.sig_for_fn_def_coercion(a, Some(b_hdr.safety()))?;
1099
1100                let InferOk { value: a_sig, mut obligations } =
1101                    self.at(&self.cause, self.param_env).normalize(Unnormalized::new_wip(a_sig));
1102                let a = Ty::new_fn_ptr(self.tcx, a_sig);
1103
1104                let adjust = Adjust::Pointer(PointerCoercion::ReifyFnPointer(b_hdr.safety()));
1105                let InferOk { value, obligations: o2 } =
1106                    self.unify_and(a, b, [], adjust, ForceLeakCheck::Yes)?;
1107
1108                obligations.extend(o2);
1109                Ok(InferOk { value, obligations })
1110            }
1111            _ => self.unify(a, b, ForceLeakCheck::No),
1112        }
1113    }
1114
1115    /// Attempts to coerce from a closure to a function pointer. Fails
1116    /// if the closure has any upvars.
1117    fn coerce_closure_to_fn(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> CoerceResult<'tcx> {
1118        if true {
    if !(self.shallow_resolve(a) == a) {
        ::core::panicking::panic("assertion failed: self.shallow_resolve(a) == a")
    };
};debug_assert!(self.shallow_resolve(a) == a);
1119        if true {
    if !(self.shallow_resolve(b) == b) {
        ::core::panicking::panic("assertion failed: self.shallow_resolve(b) == b")
    };
};debug_assert!(self.shallow_resolve(b) == b);
1120
1121        match b.kind() {
1122            ty::FnPtr(_, hdr) => {
1123                let safety = hdr.safety();
1124                let terr = TypeError::Sorts(ty::error::ExpectedFound::new(a, b));
1125                let closure_sig = self.sig_for_closure_coercion(a, Some(hdr.safety()), terr)?;
1126                let pointer_ty = Ty::new_fn_ptr(self.tcx, closure_sig);
1127                {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:1127",
                        "rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
                        ::tracing_core::__macro_support::Option::Some(1127u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
                        ::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};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("coerce_closure_to_fn(a={0:?}, b={1:?}, pty={2:?})",
                                                    a, b, pointer_ty) as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!("coerce_closure_to_fn(a={:?}, b={:?}, pty={:?})", a, b, pointer_ty);
1128
1129                let adjust = Adjust::Pointer(PointerCoercion::ClosureFnPointer(safety));
1130                self.unify_and(pointer_ty, b, [], adjust, ForceLeakCheck::No)
1131            }
1132            _ => self.unify(a, b, ForceLeakCheck::No),
1133        }
1134    }
1135
1136    fn coerce_to_raw_ptr(
1137        &self,
1138        a: Ty<'tcx>,
1139        b: Ty<'tcx>,
1140        mutbl_b: hir::Mutability,
1141    ) -> CoerceResult<'tcx> {
1142        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:1142",
                        "rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
                        ::tracing_core::__macro_support::Option::Some(1142u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
                        ::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};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("coerce_to_raw_ptr(a={0:?}, b={1:?})",
                                                    a, b) as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!("coerce_to_raw_ptr(a={:?}, b={:?})", a, b);
1143        if true {
    if !(self.shallow_resolve(a) == a) {
        ::core::panicking::panic("assertion failed: self.shallow_resolve(a) == a")
    };
};debug_assert!(self.shallow_resolve(a) == a);
1144        if true {
    if !(self.shallow_resolve(b) == b) {
        ::core::panicking::panic("assertion failed: self.shallow_resolve(b) == b")
    };
};debug_assert!(self.shallow_resolve(b) == b);
1145
1146        let (is_ref, mt_a) = match *a.kind() {
1147            ty::Ref(_, ty, mutbl) => (true, ty::TypeAndMut { ty, mutbl }),
1148            ty::RawPtr(ty, mutbl) => (false, ty::TypeAndMut { ty, mutbl }),
1149            _ => return self.unify(a, b, ForceLeakCheck::No),
1150        };
1151        coerce_mutbls(mt_a.mutbl, mutbl_b)?;
1152
1153        // Check that the types which they point at are compatible.
1154        let a_raw = Ty::new_ptr(self.tcx, mt_a.ty, mutbl_b);
1155        // Although references and raw ptrs have the same
1156        // representation, we still register an Adjust::DerefRef so that
1157        // regionck knows that the region for `a` must be valid here.
1158        if is_ref {
1159            self.unify_and(
1160                a_raw,
1161                b,
1162                [Adjustment { kind: Adjust::Deref(DerefAdjustKind::Builtin), target: mt_a.ty }],
1163                Adjust::Borrow(AutoBorrow::RawPtr(mutbl_b)),
1164                ForceLeakCheck::No,
1165            )
1166        } else if mt_a.mutbl != mutbl_b {
1167            self.unify_and(
1168                a_raw,
1169                b,
1170                [],
1171                Adjust::Pointer(PointerCoercion::MutToConstPointer),
1172                ForceLeakCheck::No,
1173            )
1174        } else {
1175            self.unify(a_raw, b, ForceLeakCheck::No)
1176        }
1177    }
1178}
1179
1180impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1181    /// Attempt to coerce an expression to a type, and return the
1182    /// adjusted type of the expression, if successful.
1183    /// Adjustments are only recorded if the coercion succeeded.
1184    /// The expressions *must not* have any preexisting adjustments.
1185    pub(crate) fn coerce(
1186        &self,
1187        expr: &'tcx hir::Expr<'tcx>,
1188        expr_ty: Ty<'tcx>,
1189        target: Ty<'tcx>,
1190        allow_two_phase: AllowTwoPhase,
1191        cause: Option<ObligationCause<'tcx>>,
1192    ) -> RelateResult<'tcx, Ty<'tcx>> {
1193        let source = self.deeply_resolve_ignoring_regions_with_obligations(expr_ty);
1194        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:1194",
                        "rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
                        ::tracing_core::__macro_support::Option::Some(1194u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
                        ::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};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("coercion::try({0:?}: {1:?} -> {2:?})",
                                                    expr, source, target) as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!("coercion::try({:?}: {:?} -> {:?})", expr, source, target);
1195
1196        let cause =
1197            cause.unwrap_or_else(|| self.cause(expr.span, ObligationCauseCode::ExprAssignable));
1198        let coerce = Coerce::new(
1199            self,
1200            cause,
1201            allow_two_phase,
1202            self.tcx.expr_guaranteed_to_constitute_read_for_never(expr),
1203        );
1204        let ok = self.commit_if_ok(|_| coerce.coerce(source, target))?;
1205
1206        let (adjustments, _) = self.register_infer_ok_obligations(ok);
1207        self.apply_adjustments(expr, adjustments);
1208        Ok(if let Err(guar) = expr_ty.error_reported() {
1209            Ty::new_error(self.tcx, guar)
1210        } else {
1211            target
1212        })
1213    }
1214
1215    /// Probe whether `expr_ty` can be coerced to `target_ty`. This has no side-effects,
1216    /// and may return false positives if types are not yet fully constrained by inference.
1217    ///
1218    /// Returns false if the coercion is not possible, or if the coercion creates any
1219    /// sub-obligations that result in errors.
1220    ///
1221    /// This should only be used for diagnostics.
1222    pub(crate) fn may_coerce(&self, expr_ty: Ty<'tcx>, target_ty: Ty<'tcx>) -> bool {
1223        let cause = self.cause(DUMMY_SP, ObligationCauseCode::ExprAssignable);
1224        // We don't ever need two-phase here since we throw out the result of the coercion.
1225        // We also just always set `coerce_never` to true, since this is a heuristic.
1226        let coerce = Coerce::new(self, cause.clone(), AllowTwoPhase::No, true);
1227        self.probe(|_| {
1228            // Make sure to structurally resolve the types, since we use
1229            // the `TyKind`s heavily in coercion.
1230            let ocx = ObligationCtxt::new(self);
1231            let Ok(ok) = coerce.coerce(expr_ty, target_ty) else {
1232                return false;
1233            };
1234            ocx.register_obligations(ok.obligations);
1235            ocx.try_evaluate_obligations().no_errors()
1236        })
1237    }
1238
1239    /// Like [`Self::may_coerce`], but for suggestions whose replacement must complete with a
1240    /// value of the target type. A coercion from `!` to another type does not provide such a
1241    /// value, so it should not by itself justify these suggestions.
1242    ///
1243    /// This should only be used for suggestions.
1244    pub(crate) fn may_coerce_except_never(&self, expr_ty: Ty<'tcx>, target_ty: Ty<'tcx>) -> bool {
1245        if expr_ty.is_never() && !target_ty.is_never() {
1246            return false;
1247        }
1248        self.may_coerce(expr_ty, target_ty)
1249    }
1250
1251    /// Given a type and a target type, this function will calculate and return
1252    /// how many dereference steps needed to coerce `expr_ty` to `target`. If
1253    /// it's not possible, return `None`.
1254    pub(crate) fn deref_steps_for_suggestion(
1255        &self,
1256        expr_ty: Ty<'tcx>,
1257        target: Ty<'tcx>,
1258    ) -> Option<usize> {
1259        let cause = self.cause(DUMMY_SP, ObligationCauseCode::ExprAssignable);
1260        // We don't ever need two-phase here since we throw out the result of the coercion.
1261        let coerce = Coerce::new(self, cause, AllowTwoPhase::No, true);
1262        coerce.autoderef(DUMMY_SP, expr_ty).find_map(|(ty, steps)| {
1263            self.probe(|_| coerce.unify_raw(ty, target, ForceLeakCheck::No)).ok().map(|_| steps)
1264        })
1265    }
1266
1267    /// Given a type, this function will calculate and return the type given
1268    /// for `<Ty as Deref>::Target` only if `Ty` also implements `DerefMut`.
1269    ///
1270    /// This function is for diagnostics only, since it does not register
1271    /// trait or region sub-obligations. (presumably we could, but it's not
1272    /// particularly important for diagnostics...)
1273    pub(crate) fn deref_once_mutably_for_diagnostic(&self, expr_ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
1274        self.autoderef(DUMMY_SP, expr_ty).silence_errors().nth(1).and_then(|(deref_ty, _)| {
1275            self.infcx
1276                .type_implements_trait(
1277                    self.tcx.lang_items().deref_mut_trait()?,
1278                    [expr_ty],
1279                    self.param_env,
1280                )
1281                .may_apply()
1282                .then_some(deref_ty)
1283        })
1284    }
1285
1286    {}
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("sig_for_coerce_lub",
                                "rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
                                ::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
                                ::tracing_core::__macro_support::Option::Some(1286u32),
                                ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
                                ::tracing_core::field::FieldSet::new(&[{
                                                    const NAME:
                                                        ::tracing::__macro_support::FieldName<{
                                                            ::tracing::__macro_support::FieldName::len("ty")
                                                        }> =
                                                        ::tracing::__macro_support::FieldName::new("ty");
                                                    NAME.as_str()
                                                },
                                                {
                                                    const NAME:
                                                        ::tracing::__macro_support::FieldName<{
                                                            ::tracing::__macro_support::FieldName::len("closure_upvars_terr")
                                                        }> =
                                                        ::tracing::__macro_support::FieldName::new("closure_upvars_terr");
                                                    NAME.as_str()
                                                }], ::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};
                            meta.fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&ty)
                                                        as &dyn ::tracing::field::Value)),
                                            (::tracing::__macro_support::Option::Some(&::tracing::field::debug(&closure_upvars_terr)
                                                        as &dyn ::tracing::field::Value))])
                        })
            } else {
                let span =
                    ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                {};
                span
            }
        };
    __tracing_attr_guard = __tracing_attr_span.enter();
}
#[allow(clippy :: redundant_closure_call)]
let x =
    (move ||
                {

                    #[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:
                                Result<ty::PolyFnSig<'tcx>, TypeError<'tcx>> = loop {};
                        return __tracing_attr_fake_return;
                    }
                    {
                        match ty.kind() {
                            ty::FnDef(..) => self.sig_for_fn_def_coercion(ty, None),
                            ty::Closure(..) =>
                                self.sig_for_closure_coercion(ty, None,
                                    closure_upvars_terr),
                            _ => {
                                ::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
                                        format_args!("`sig_for_fn_def_closure_coerce_lub` called with wrong ty: {0:?}",
                                            ty)));
                            }
                        }
                    }
                })();
{
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:1286",
                        "rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
                        ::tracing_core::__macro_support::Option::Some(1286u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
                        ::tracing_core::field::FieldSet::new(&[{
                                            const NAME:
                                                ::tracing::__macro_support::FieldName<{
                                                    ::tracing::__macro_support::FieldName::len("return")
                                                }> =
                                                ::tracing::__macro_support::FieldName::new("return");
                                            NAME.as_str()
                                        }], ::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};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&x)
                                            as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};
x;#[instrument(level = "debug", skip(self), ret)]
1287    fn sig_for_coerce_lub(
1288        &self,
1289        ty: Ty<'tcx>,
1290        closure_upvars_terr: TypeError<'tcx>,
1291    ) -> Result<ty::PolyFnSig<'tcx>, TypeError<'tcx>> {
1292        match ty.kind() {
1293            ty::FnDef(..) => self.sig_for_fn_def_coercion(ty, None),
1294            ty::Closure(..) => self.sig_for_closure_coercion(ty, None, closure_upvars_terr),
1295            _ => unreachable!("`sig_for_fn_def_closure_coerce_lub` called with wrong ty: {:?}", ty),
1296        }
1297    }
1298
1299    fn sig_for_fn_def_coercion(
1300        &self,
1301        fndef: Ty<'tcx>,
1302        expected_safety: Option<hir::Safety>,
1303    ) -> Result<ty::PolyFnSig<'tcx>, TypeError<'tcx>> {
1304        let tcx = self.tcx;
1305
1306        let &ty::FnDef(def_id, _) = fndef.kind() else {
1307            {
    ::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
            format_args!("`sig_for_fn_def_coercion` called with non-fndef: {0:?}",
                fndef)));
};unreachable!("`sig_for_fn_def_coercion` called with non-fndef: {:?}", fndef);
1308        };
1309
1310        // Intrinsics are not coercible to function pointers
1311        if tcx.intrinsic(def_id).is_some() {
1312            return Err(TypeError::IntrinsicCast);
1313        }
1314
1315        let fn_attrs = tcx.codegen_fn_attrs(def_id);
1316        if #[allow(non_exhaustive_omitted_patterns)] match fn_attrs.inline {
    InlineAttr::Force { .. } => true,
    _ => false,
}matches!(fn_attrs.inline, InlineAttr::Force { .. }) {
1317            return Err(TypeError::ForceInlineCast);
1318        }
1319
1320        let sig = fndef.fn_sig(tcx);
1321        let sig = if fn_attrs.safe_target_features {
1322            // Allow the coercion if the current function has all the features that would be
1323            // needed to call the coercee safely.
1324            match tcx.adjust_target_feature_sig(def_id, sig, self.body_def_id.into()) {
1325                Some(adjusted_sig) => adjusted_sig,
1326                None if #[allow(non_exhaustive_omitted_patterns)] match expected_safety {
    Some(hir::Safety::Safe) => true,
    _ => false,
}matches!(expected_safety, Some(hir::Safety::Safe)) => {
1327                    return Err(TypeError::TargetFeatureCast(def_id));
1328                }
1329                None => sig,
1330            }
1331        } else {
1332            sig
1333        };
1334
1335        if sig.safety().is_safe() && #[allow(non_exhaustive_omitted_patterns)] match expected_safety {
    Some(hir::Safety::Unsafe) => true,
    _ => false,
}matches!(expected_safety, Some(hir::Safety::Unsafe)) {
1336            Ok(tcx.safe_to_unsafe_sig(sig))
1337        } else {
1338            Ok(sig)
1339        }
1340    }
1341
1342    fn sig_for_closure_coercion(
1343        &self,
1344        closure: Ty<'tcx>,
1345        expected_safety: Option<hir::Safety>,
1346        closure_upvars_terr: TypeError<'tcx>,
1347    ) -> Result<ty::PolyFnSig<'tcx>, TypeError<'tcx>> {
1348        let tcx = self.tcx;
1349
1350        let ty::Closure(closure_def, closure_args) = closure.kind() else {
1351            {
    ::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
            format_args!("`sig_for_closure_coercion` called with non closure ty: {0:?}",
                closure)));
};unreachable!("`sig_for_closure_coercion` called with non closure ty: {:?}", closure);
1352        };
1353
1354        // At this point we haven't done capture analysis, which means
1355        // that the ClosureArgs just contains an inference variable instead
1356        // of tuple of captured types.
1357        //
1358        // All we care here is if any variable is being captured and not the exact paths,
1359        // so we check `upvars_mentioned` for root variables being captured.
1360        if !tcx.upvars_mentioned(closure_def.expect_local()).is_none_or(|u| u.is_empty()) {
1361            return Err(closure_upvars_terr);
1362        }
1363
1364        // We coerce the closure, which has fn type
1365        //     `extern "rust-call" fn((arg0,arg1,...)) -> _`
1366        // to
1367        //     `fn(arg0,arg1,...) -> _`
1368        // or
1369        //     `unsafe fn(arg0,arg1,...) -> _`
1370        let closure_sig = closure_args.as_closure().sig();
1371        Ok(tcx.signature_unclosure(closure_sig, expected_safety.unwrap_or(hir::Safety::Safe)))
1372    }
1373
1374    /// Given some expressions, their known unified type and another expression,
1375    /// tries to unify the types, potentially inserting coercions on any of the
1376    /// provided expressions and returns their LUB (aka "common supertype").
1377    ///
1378    /// This is really an internal helper. From outside the coercion
1379    /// module, you should instantiate a `CoerceMany` instance.
1380    fn try_find_coercion_lub(
1381        &self,
1382        cause: &ObligationCause<'tcx>,
1383        exprs: &[&'tcx hir::Expr<'tcx>],
1384        prev_ty: Ty<'tcx>,
1385        new: &hir::Expr<'_>,
1386        new_ty: Ty<'tcx>,
1387    ) -> RelateResult<'tcx, Ty<'tcx>> {
1388        let prev_ty = self.deeply_resolve_ignoring_regions_with_obligations(prev_ty);
1389        let new_ty = self.deeply_resolve_ignoring_regions_with_obligations(new_ty);
1390        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:1390",
                        "rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
                        ::tracing_core::__macro_support::Option::Some(1390u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
                        ::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};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("coercion::try_find_coercion_lub({0:?}, {1:?}, exprs={2:?} exprs)",
                                                    prev_ty, new_ty, exprs.len()) as
                                            &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!(
1391            "coercion::try_find_coercion_lub({:?}, {:?}, exprs={:?} exprs)",
1392            prev_ty,
1393            new_ty,
1394            exprs.len()
1395        );
1396
1397        // Fast Path: don't go through the coercion logic if we're coercing
1398        // a type to itself. This is unfortunately quite perf relevant so
1399        // we do it even though it may mask bugs in the coercion logic.
1400        if prev_ty == new_ty {
1401            return Ok(prev_ty);
1402        }
1403
1404        let terr = TypeError::Sorts(ty::error::ExpectedFound::new(prev_ty, new_ty));
1405        let opt_sigs = match (prev_ty.kind(), new_ty.kind()) {
1406            // Don't coerce pairs of fndefs or pairs of closures to fn ptrs
1407            // if they can just be lubbed.
1408            //
1409            // See #88097 or `lub_closures_before_fnptr_coercion.rs` for where
1410            // we would erroneously coerce closures to fnptrs when attempting to
1411            // coerce a closure to itself.
1412            (ty::FnDef(..), ty::FnDef(..)) | (ty::Closure(..), ty::Closure(..)) => {
1413                let lubbed_ty = self.commit_if_ok(|snapshot| {
1414                    let outer_universe = self.infcx.universe();
1415
1416                    // We need to eagerly handle nested obligations due to lazy norm.
1417                    let result = if self.next_trait_solver() {
1418                        let ocx = ObligationCtxt::new(self);
1419                        let value = ocx.lub(cause, self.param_env, prev_ty, new_ty)?;
1420                        if ocx.try_evaluate_obligations().no_errors() {
1421                            Ok(InferOk { value, obligations: ocx.into_pending_obligations() })
1422                        } else {
1423                            Err(TypeError::Mismatch)
1424                        }
1425                    } else {
1426                        self.at(cause, self.param_env).lub(prev_ty, new_ty)
1427                    };
1428
1429                    self.leak_check(outer_universe, Some(snapshot))?;
1430                    result
1431                });
1432
1433                match lubbed_ty {
1434                    Ok(ok) => return Ok(self.register_infer_ok_obligations(ok)),
1435                    Err(_) => {
1436                        let a_sig = self.sig_for_coerce_lub(prev_ty, terr)?;
1437                        let b_sig = self.sig_for_coerce_lub(new_ty, terr)?;
1438                        Some((a_sig, b_sig))
1439                    }
1440                }
1441            }
1442
1443            (ty::Closure(..), ty::FnDef(..)) | (ty::FnDef(..), ty::Closure(..)) => {
1444                let a_sig = self.sig_for_coerce_lub(prev_ty, terr)?;
1445                let b_sig = self.sig_for_coerce_lub(new_ty, terr)?;
1446                Some((a_sig, b_sig))
1447            }
1448            // ty::FnPtr x ty::FnPtr is fine to just be handled through a normal `unify`
1449            // call using `lub` which is what will happen on the normal path.
1450            (ty::FnPtr(..), ty::FnPtr(..)) => None,
1451            _ => None,
1452        };
1453
1454        if let Some((mut a_sig, mut b_sig)) = opt_sigs {
1455            // Allow coercing safe sigs to unsafe sigs
1456            if a_sig.safety().is_safe() && b_sig.safety().is_unsafe() {
1457                a_sig = self.tcx.safe_to_unsafe_sig(a_sig);
1458            } else if b_sig.safety().is_safe() && a_sig.safety().is_unsafe() {
1459                b_sig = self.tcx.safe_to_unsafe_sig(b_sig);
1460            };
1461
1462            // The signature must match.
1463            let (a_sig, b_sig) = self.normalize(new.span, Unnormalized::new_wip((a_sig, b_sig)));
1464            let sig = self
1465                .at(cause, self.param_env)
1466                .lub(a_sig, b_sig)
1467                .map(|ok| self.register_infer_ok_obligations(ok))?;
1468
1469            // Reify both sides and return the reified fn pointer type.
1470            let fn_ptr = Ty::new_fn_ptr(self.tcx, sig);
1471            let prev_adjustment = match prev_ty.kind() {
1472                ty::Closure(..) => Adjust::Pointer(PointerCoercion::ClosureFnPointer(sig.safety())),
1473                ty::FnDef(..) => Adjust::Pointer(PointerCoercion::ReifyFnPointer(sig.safety())),
1474                _ => ::rustc_span::macros::bug_impl(Some(cause.span),
    format_args!("should not try to coerce a {0} to a fn pointer", prev_ty),
    Location::caller())span_bug!(cause.span, "should not try to coerce a {prev_ty} to a fn pointer"),
1475            };
1476            let next_adjustment = match new_ty.kind() {
1477                ty::Closure(..) => Adjust::Pointer(PointerCoercion::ClosureFnPointer(sig.safety())),
1478                ty::FnDef(..) => Adjust::Pointer(PointerCoercion::ReifyFnPointer(sig.safety())),
1479                _ => ::rustc_span::macros::bug_impl(Some(new.span),
    format_args!("should not try to coerce a {0} to a fn pointer", new_ty),
    Location::caller())span_bug!(new.span, "should not try to coerce a {new_ty} to a fn pointer"),
1480            };
1481            for expr in exprs.iter() {
1482                self.apply_adjustments(
1483                    expr,
1484                    ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [Adjustment { kind: prev_adjustment.clone(), target: fn_ptr }]))vec![Adjustment { kind: prev_adjustment.clone(), target: fn_ptr }],
1485                );
1486            }
1487            self.apply_adjustments(new, ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [Adjustment { kind: next_adjustment, target: fn_ptr }]))vec![Adjustment { kind: next_adjustment, target: fn_ptr }]);
1488            return Ok(fn_ptr);
1489        }
1490
1491        // Configure a Coerce instance to compute the LUB.
1492        // We don't allow two-phase borrows on any autorefs this creates since we
1493        // probably aren't processing function arguments here and even if we were,
1494        // they're going to get autorefed again anyway and we can apply 2-phase borrows
1495        // at that time.
1496        //
1497        // NOTE: we set `coerce_never` to `true` here because coercion LUBs only
1498        // operate on values and not places, so a never coercion is valid.
1499        let mut coerce = Coerce::new(self, cause.clone(), AllowTwoPhase::No, true);
1500        coerce.use_lub = true;
1501
1502        // First try to coerce the new expression to the type of the previous ones,
1503        // but only if the new expression has no coercion already applied to it.
1504        let mut first_error = None;
1505        if !self.typeck_results.borrow().adjustments().contains_key(new.hir_id) {
1506            let result = self.commit_if_ok(|_| coerce.coerce(new_ty, prev_ty));
1507            match result {
1508                Ok(ok) => {
1509                    let (adjustments, target) = self.register_infer_ok_obligations(ok);
1510                    self.apply_adjustments(new, adjustments);
1511                    {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:1511",
                        "rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
                        ::tracing_core::__macro_support::Option::Some(1511u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
                        ::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};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("coercion::try_find_coercion_lub: was able to coerce from new type {0:?} to previous type {1:?} ({2:?})",
                                                    new_ty, prev_ty, target) as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!(
1512                        "coercion::try_find_coercion_lub: was able to coerce from new type {:?} to previous type {:?} ({:?})",
1513                        new_ty, prev_ty, target
1514                    );
1515                    return Ok(target);
1516                }
1517                Err(e) => first_error = Some(e),
1518            }
1519        }
1520
1521        let ok = self
1522            .commit_if_ok(|_| coerce.coerce(prev_ty, new_ty))
1523            // Avoid giving strange errors on failed attempts.
1524            .map_err(|e| first_error.unwrap_or(e))?;
1525
1526        let (adjustments, target) = self.register_infer_ok_obligations(ok);
1527        for expr in exprs {
1528            self.apply_adjustments(expr, adjustments.clone());
1529        }
1530        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:1530",
                        "rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
                        ::tracing_core::__macro_support::Option::Some(1530u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
                        ::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};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("coercion::try_find_coercion_lub: was able to coerce previous type {0:?} to new type {1:?} ({2:?})",
                                                    prev_ty, new_ty, target) as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!(
1531            "coercion::try_find_coercion_lub: was able to coerce previous type {:?} to new type {:?} ({:?})",
1532            prev_ty, new_ty, target
1533        );
1534        Ok(target)
1535    }
1536}
1537
1538/// Check whether `ty` can be coerced to `output_ty`.
1539/// Used from clippy.
1540pub fn can_coerce<'tcx>(
1541    tcx: TyCtxt<'tcx>,
1542    param_env: ty::ParamEnv<'tcx>,
1543    body_def_id: LocalDefId,
1544    ty: Ty<'tcx>,
1545    output_ty: Ty<'tcx>,
1546) -> bool {
1547    let root_ctxt = crate::typeck_root_ctxt::TypeckRootCtxt::new(tcx, body_def_id);
1548    let fn_ctxt = FnCtxt::new(&root_ctxt, param_env, body_def_id);
1549    fn_ctxt.may_coerce(ty, output_ty)
1550}
1551
1552/// CoerceMany encapsulates the pattern you should use when you have
1553/// many expressions that are all getting coerced to a common
1554/// type. This arises, for example, when you have a match (the result
1555/// of each arm is coerced to a common type). It also arises in less
1556/// obvious places, such as when you have many `break foo` expressions
1557/// that target the same loop, or the various `return` expressions in
1558/// a function.
1559///
1560/// The basic protocol is as follows:
1561///
1562/// - Instantiate the `CoerceMany` with an initial `expected_ty`.
1563///   This will also serve as the "starting LUB". The expectation is
1564///   that this type is something which all of the expressions *must*
1565///   be coercible to. Use a fresh type variable if needed.
1566/// - For each expression whose result is to be coerced, invoke `coerce()` with.
1567///   - In some cases we wish to coerce "non-expressions" whose types are implicitly
1568///     unit. This happens for example if you have a `break` with no expression,
1569///     or an `if` with no `else`. In that case, invoke `coerce_forced_unit()`.
1570///   - `coerce()` and `coerce_forced_unit()` may report errors. They hide this
1571///     from you so that you don't have to worry your pretty head about it.
1572///     But if an error is reported, the final type will be `err`.
1573///   - Invoking `coerce()` may cause us to go and adjust the "adjustments" on
1574///     previously coerced expressions.
1575/// - When all done, invoke `complete()`. This will return the LUB of
1576///   all your expressions.
1577///   - WARNING: I don't believe this final type is guaranteed to be
1578///     related to your initial `expected_ty` in any particular way,
1579///     although it will typically be a subtype, so you should check it.
1580///     Check the note below for more details.
1581///   - Invoking `complete()` may cause us to go and adjust the "adjustments" on
1582///     previously coerced expressions.
1583///
1584/// Example:
1585///
1586/// ```ignore (illustrative)
1587/// let mut coerce = CoerceMany::new(expected_ty);
1588/// for expr in exprs {
1589///     let expr_ty = fcx.check_expr_with_expectation(expr, expected);
1590///     coerce.coerce(fcx, &cause, expr, expr_ty);
1591/// }
1592/// let final_ty = coerce.complete(fcx);
1593/// ```
1594///
1595/// NOTE: Why does the `expected_ty` participate in the LUB?
1596/// When coercing, each branch should use the following expectations for type inference:
1597/// - The branch can be coerced to the expected type of the match/if/whatever.
1598/// - The branch can be coercion lub'd with the types of the previous branches.
1599/// Ideally we'd have some sort of `Expectation::ParticipatesInCoerceLub(ongoing_lub_ty, final_ty)`,
1600/// but adding and using this feels very challenging.
1601/// What we instead do is to use the expected type of the match/if/whatever as
1602/// the initial coercion lub. This allows us to use the lub of "expected type of match" with
1603/// "types from previous branches" as the coercion target, which can contains both expectations.
1604///
1605/// Two concerns with this approach:
1606/// - We may have incompatible `final_ty` if that lub is different from the expected
1607///   type of the match. However, in this case coercing the final type of the
1608///   `CoerceMany` to its expected type would have error'd anyways, so we don't care.
1609/// - We may constrain the `expected_ty` too early. For some branches with
1610///   type `a` and `b`, we end up with `(a lub expected_ty) lub b` instead of
1611///   `(a lub b) lub expected_ty`. They should be the same type. However,
1612///   `a lub expected_ty` may constrain inference variables in `expected_ty`.
1613///   In this case the difference does matter and we get actually incorrect results.
1614/// FIXME: Ideally we'd compute the final type without unnecessarily constraining
1615/// the expected type of the match when computing the types of its branches.
1616pub(crate) struct CoerceMany<'tcx> {
1617    expected_ty: Ty<'tcx>,
1618    final_ty: Option<Ty<'tcx>>,
1619    expressions: Vec<&'tcx hir::Expr<'tcx>>,
1620}
1621
1622impl<'tcx> CoerceMany<'tcx> {
1623    /// Creates a `CoerceMany` with a default capacity of 1. If the full set of
1624    /// coercion sites is known before hand, consider `with_capacity()` instead
1625    /// to avoid allocation.
1626    pub(crate) fn new(expected_ty: Ty<'tcx>) -> Self {
1627        Self::with_capacity(expected_ty, 1)
1628    }
1629
1630    /// Creates a `CoerceMany` with a given capacity.
1631    pub(crate) fn with_capacity(expected_ty: Ty<'tcx>, capacity: usize) -> Self {
1632        CoerceMany { expected_ty, final_ty: None, expressions: Vec::with_capacity(capacity) }
1633    }
1634
1635    /// Returns the "expected type" with which this coercion was
1636    /// constructed. This represents the "downward propagated" type
1637    /// that was given to us at the start of typing whatever construct
1638    /// we are typing (e.g., the match expression).
1639    ///
1640    /// Typically, this is used as the expected type when
1641    /// type-checking each of the alternative expressions whose types
1642    /// we are trying to merge.
1643    pub(crate) fn expected_ty(&self) -> Ty<'tcx> {
1644        self.expected_ty
1645    }
1646
1647    /// Returns the current "merged type", representing our best-guess
1648    /// at the LUB of the expressions we've seen so far (if any). This
1649    /// isn't *final* until you call `self.complete()`, which will return
1650    /// the merged type.
1651    pub(crate) fn merged_ty(&self) -> Ty<'tcx> {
1652        self.final_ty.unwrap_or(self.expected_ty)
1653    }
1654
1655    /// Indicates that the value generated by `expression`, which is
1656    /// of type `expression_ty`, is one of the possibilities that we
1657    /// could coerce from. This will record `expression`, and later
1658    /// calls to `coerce` may come back and add adjustments and things
1659    /// if necessary.
1660    pub(crate) fn coerce<'a>(
1661        &mut self,
1662        fcx: &FnCtxt<'a, 'tcx>,
1663        cause: &ObligationCause<'tcx>,
1664        expression: &'tcx hir::Expr<'tcx>,
1665        expression_ty: Ty<'tcx>,
1666    ) {
1667        self.coerce_inner(fcx, cause, Some(expression), expression_ty, |_| {}, false)
1668    }
1669
1670    /// Indicates that one of the inputs is a "forced unit". This
1671    /// occurs in a case like `if foo { ... };`, where the missing else
1672    /// generates a "forced unit". Another example is a `loop { break;
1673    /// }`, where the `break` has no argument expression. We treat
1674    /// these cases slightly differently for error-reporting
1675    /// purposes. Note that these tend to correspond to cases where
1676    /// the `()` expression is implicit in the source, and hence we do
1677    /// not take an expression argument.
1678    ///
1679    /// The `augment_error` gives you a chance to extend the error
1680    /// message, in case any results (e.g., we use this to suggest
1681    /// removing a `;`).
1682    pub(crate) fn coerce_forced_unit<'a>(
1683        &mut self,
1684        fcx: &FnCtxt<'a, 'tcx>,
1685        cause: &ObligationCause<'tcx>,
1686        augment_error: impl FnOnce(&mut Diag<'_>),
1687        label_unit_as_expected: bool,
1688    ) {
1689        self.coerce_inner(
1690            fcx,
1691            cause,
1692            None,
1693            fcx.tcx.types.unit,
1694            augment_error,
1695            label_unit_as_expected,
1696        )
1697    }
1698
1699    /// The inner coercion "engine". If `expression` is `None`, this
1700    /// is a forced-unit case, and hence `expression_ty` must be
1701    /// `Nil`.
1702    {}
#[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("coerce_inner",
                                    "rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
                                    ::tracing_core::__macro_support::Option::Some(1702u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
                                    ::tracing_core::field::FieldSet::new(&[{
                                                        const NAME:
                                                            ::tracing::__macro_support::FieldName<{
                                                                ::tracing::__macro_support::FieldName::len("cause")
                                                            }> =
                                                            ::tracing::__macro_support::FieldName::new("cause");
                                                        NAME.as_str()
                                                    },
                                                    {
                                                        const NAME:
                                                            ::tracing::__macro_support::FieldName<{
                                                                ::tracing::__macro_support::FieldName::len("expression")
                                                            }> =
                                                            ::tracing::__macro_support::FieldName::new("expression");
                                                        NAME.as_str()
                                                    },
                                                    {
                                                        const NAME:
                                                            ::tracing::__macro_support::FieldName<{
                                                                ::tracing::__macro_support::FieldName::len("expression_ty")
                                                            }> =
                                                            ::tracing::__macro_support::FieldName::new("expression_ty");
                                                        NAME.as_str()
                                                    }], ::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};
                                meta.fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&cause)
                                                            as &dyn ::tracing::field::Value)),
                                                (::tracing::__macro_support::Option::Some(&::tracing::field::debug(&expression)
                                                            as &dyn ::tracing::field::Value)),
                                                (::tracing::__macro_support::Option::Some(&::tracing::field::debug(&expression_ty)
                                                            as &dyn ::tracing::field::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;
        }
        {
            if expression_ty.is_ty_var() {
                expression_ty = fcx.infcx.shallow_resolve(expression_ty);
            }
            if let Err(guar) =
                    (expression_ty, self.merged_ty()).error_reported() {
                self.final_ty = Some(Ty::new_error(fcx.tcx, guar));
                return;
            }
            let (expected, found) =
                if label_expression_as_expected {
                    (expression_ty, self.merged_ty())
                } else { (self.merged_ty(), expression_ty) };
            let result =
                if let Some(expression) = expression {
                    if self.expressions.is_empty() {
                        fcx.coerce(expression, expression_ty, self.expected_ty,
                            AllowTwoPhase::No, Some(cause.clone()))
                    } else {
                        fcx.try_find_coercion_lub(cause, &self.expressions,
                            self.merged_ty(), expression, expression_ty)
                    }
                } else {
                    if !expression_ty.is_unit() {
                        {
                            ::core::panicking::panic_fmt(format_args!("if let hack without unit type"));
                        }
                    };
                    fcx.at(cause,
                                fcx.param_env).eq(DefineOpaqueTypes::Yes, expected,
                            found).map(|infer_ok|
                            {
                                fcx.register_infer_ok_obligations(infer_ok);
                                expression_ty
                            })
                };
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:1793",
                                    "rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
                                    ::tracing_core::__macro_support::Option::Some(1793u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
                                    ::tracing_core::field::FieldSet::new(&[{
                                                        const NAME:
                                                            ::tracing::__macro_support::FieldName<{
                                                                ::tracing::__macro_support::FieldName::len("result")
                                                            }> =
                                                            ::tracing::__macro_support::FieldName::new("result");
                                                        NAME.as_str()
                                                    }], ::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};
                            __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&result)
                                                        as &dyn ::tracing::field::Value))])
                        });
                } else { ; }
            };
            match result {
                Ok(v) => {
                    self.final_ty = Some(v);
                    if let Some(e) = expression { self.expressions.push(e); }
                }
                Err(coercion_error) => {
                    fcx.set_tainted_by_errors(fcx.dcx().span_delayed_bug(cause.span,
                            "coercion error but no error emitted"));
                    let (expected, found) =
                        fcx.deeply_resolve_ignoring_regions((expected, found));
                    let mut err;
                    let mut unsized_return = false;
                    match *cause.code() {
                        ObligationCauseCode::ReturnNoExpression => {
                            err =
                                {
                                    fcx.dcx().struct_span_err(cause.span,
                                            ::alloc::__export::must_use({
                                                    ::alloc::fmt::format(format_args!("`return;` in a function whose return type is not `()`"))
                                                })).with_code(E0069)
                                };
                            if let Some(value) =
                                    fcx.err_ctxt().ty_kind_suggestion(fcx.param_env, found) {
                                err.span_suggestion_verbose(cause.span.shrink_to_hi(),
                                    "give the `return` a value of the expected type",
                                    ::alloc::__export::must_use({
                                            ::alloc::fmt::format(format_args!(" {0}", value))
                                        }), Applicability::HasPlaceholders);
                            }
                            err.span_label(cause.span, "return type is not `()`");
                        }
                        ObligationCauseCode::BlockTailExpression(blk_id, ..) => {
                            err =
                                self.report_return_mismatched_types(cause, expected, found,
                                    coercion_error, fcx, blk_id, expression);
                            unsized_return = self.is_return_ty_definitely_unsized(fcx);
                        }
                        ObligationCauseCode::ReturnValue(return_expr_id) => {
                            err =
                                self.report_return_mismatched_types(cause, expected, found,
                                    coercion_error, fcx, return_expr_id, expression);
                            unsized_return = self.is_return_ty_definitely_unsized(fcx);
                        }
                        ObligationCauseCode::MatchExpressionArm(MatchExpressionArmCause {
                            arm_span,
                            arm_ty,
                            prior_arm_ty,
                            ref prior_non_diverging_arms,
                            tail_defines_return_position_impl_trait: Some(rpit_def_id),
                            .. }) => {
                            err =
                                fcx.err_ctxt().report_mismatched_types(cause, fcx.param_env,
                                    expected, found, coercion_error);
                            if prior_non_diverging_arms.len() > 0 {
                                self.suggest_boxing_tail_for_return_position_impl_trait(fcx,
                                    &mut err, rpit_def_id, arm_ty, prior_arm_ty,
                                    prior_non_diverging_arms.iter().chain(std::iter::once(&arm_span)).copied());
                            }
                        }
                        ObligationCauseCode::IfExpression {
                            expr_id,
                            tail_defines_return_position_impl_trait: Some(rpit_def_id) }
                            => {
                            let hir::Node::Expr(hir::Expr {
                                    kind: hir::ExprKind::If(_, then_expr, Some(else_expr)), ..
                                    }) =
                                fcx.tcx.hir_node(expr_id) else {
                                    ::core::panicking::unreachable_default_msg();
                                };
                            err =
                                fcx.err_ctxt().report_mismatched_types(cause, fcx.param_env,
                                    expected, found, coercion_error);
                            let then_span =
                                fcx.find_block_span_from_hir_id(then_expr.hir_id);
                            let else_span =
                                fcx.find_block_span_from_hir_id(else_expr.hir_id);
                            if then_span != then_expr.span &&
                                    else_span != else_expr.span {
                                let then_ty =
                                    fcx.typeck_results.borrow().expr_ty(then_expr);
                                let else_ty =
                                    fcx.typeck_results.borrow().expr_ty(else_expr);
                                self.suggest_boxing_tail_for_return_position_impl_trait(fcx,
                                    &mut err, rpit_def_id, then_ty, else_ty,
                                    [then_span, else_span].into_iter());
                            }
                        }
                        _ => {
                            err =
                                fcx.err_ctxt().report_mismatched_types(cause, fcx.param_env,
                                    expected, found, coercion_error);
                        }
                    }
                    augment_error(&mut err);
                    if let Some(expr) = expression {
                        if let hir::ExprKind::Loop(block, _, loop_src @
                                (hir::LoopSource::While | hir::LoopSource::ForLoop), _) =
                                expr.kind {
                            let loop_type =
                                if loop_src == hir::LoopSource::While {
                                    "`while` loops"
                                } else { "`for` loops" };
                            err.note(::alloc::__export::must_use({
                                        ::alloc::fmt::format(format_args!("{0} evaluate to unit type `()`",
                                                loop_type))
                                    }));
                            if loop_src == hir::LoopSource::While &&
                                    let Some(pat) = irrefutable_if_let_expr(block) {
                                err.span_label(pat.span,
                                    "this pattern always matches, consider using `loop` instead");
                            }
                        }
                        fcx.emit_coerce_suggestions(&mut err, expr, found, expected,
                            None, Some(coercion_error));
                    }
                    let guar = err.emit_err_unless_delay(unsized_return);
                    self.final_ty = Some(Ty::new_error(fcx.tcx, guar));
                }
            }
        }
    }
}#[instrument(skip(self, fcx, augment_error, label_expression_as_expected), level = "debug")]
1703    pub(crate) fn coerce_inner<'a>(
1704        &mut self,
1705        fcx: &FnCtxt<'a, 'tcx>,
1706        cause: &ObligationCause<'tcx>,
1707        expression: Option<&'tcx hir::Expr<'tcx>>,
1708        mut expression_ty: Ty<'tcx>,
1709        augment_error: impl FnOnce(&mut Diag<'_>),
1710        label_expression_as_expected: bool,
1711    ) {
1712        // Incorporate whatever type inference information we have
1713        // until now; in principle we might also want to process
1714        // pending obligations, but doing so should only improve
1715        // compatibility (hopefully that is true) by helping us
1716        // uncover never types better.
1717        if expression_ty.is_ty_var() {
1718            expression_ty = fcx.infcx.shallow_resolve(expression_ty);
1719        }
1720
1721        // If we see any error types, just propagate that error
1722        // upwards.
1723        if let Err(guar) = (expression_ty, self.merged_ty()).error_reported() {
1724            self.final_ty = Some(Ty::new_error(fcx.tcx, guar));
1725            return;
1726        }
1727
1728        let (expected, found) = if label_expression_as_expected {
1729            // In the case where this is a "forced unit", like
1730            // `break`, we want to call the `()` "expected"
1731            // since it is implied by the syntax.
1732            // (Note: not all force-units work this way.)"
1733            (expression_ty, self.merged_ty())
1734        } else {
1735            // Otherwise, the "expected" type for error
1736            // reporting is the current unification type,
1737            // which is basically the LUB of the expressions
1738            // we've seen so far (combined with the expected
1739            // type)
1740            (self.merged_ty(), expression_ty)
1741        };
1742
1743        // Handle the actual type unification etc.
1744        let result = if let Some(expression) = expression {
1745            if self.expressions.is_empty() {
1746                // Special-case the first expression we are coercing.
1747                // To be honest, I'm not entirely sure why we do this.
1748                // We don't allow two-phase borrows, see comment in try_find_coercion_lub for why
1749                fcx.coerce(
1750                    expression,
1751                    expression_ty,
1752                    self.expected_ty,
1753                    AllowTwoPhase::No,
1754                    Some(cause.clone()),
1755                )
1756            } else {
1757                fcx.try_find_coercion_lub(
1758                    cause,
1759                    &self.expressions,
1760                    self.merged_ty(),
1761                    expression,
1762                    expression_ty,
1763                )
1764            }
1765        } else {
1766            // this is a hack for cases where we default to `()` because
1767            // the expression etc has been omitted from the source. An
1768            // example is an `if let` without an else:
1769            //
1770            //     if let Some(x) = ... { }
1771            //
1772            // we wind up with a second match arm that is like `_ =>
1773            // ()`. That is the case we are considering here. We take
1774            // a different path to get the right "expected, found"
1775            // message and so forth (and because we know that
1776            // `expression_ty` will be unit).
1777            //
1778            // Another example is `break` with no argument expression.
1779            assert!(expression_ty.is_unit(), "if let hack without unit type");
1780            fcx.at(cause, fcx.param_env)
1781                .eq(
1782                    // needed for tests/ui/type-alias-impl-trait/issue-65679-inst-opaque-ty-from-val-twice.rs
1783                    DefineOpaqueTypes::Yes,
1784                    expected,
1785                    found,
1786                )
1787                .map(|infer_ok| {
1788                    fcx.register_infer_ok_obligations(infer_ok);
1789                    expression_ty
1790                })
1791        };
1792
1793        debug!(?result);
1794        match result {
1795            Ok(v) => {
1796                self.final_ty = Some(v);
1797                if let Some(e) = expression {
1798                    self.expressions.push(e);
1799                }
1800            }
1801            Err(coercion_error) => {
1802                // Mark that we've failed to coerce the types here to suppress
1803                // any superfluous errors we might encounter while trying to
1804                // emit or provide suggestions on how to fix the initial error.
1805                fcx.set_tainted_by_errors(
1806                    fcx.dcx().span_delayed_bug(cause.span, "coercion error but no error emitted"),
1807                );
1808                let (expected, found) = fcx.deeply_resolve_ignoring_regions((expected, found));
1809
1810                let mut err;
1811                let mut unsized_return = false;
1812                match *cause.code() {
1813                    ObligationCauseCode::ReturnNoExpression => {
1814                        err = struct_span_code_err!(
1815                            fcx.dcx(),
1816                            cause.span,
1817                            E0069,
1818                            "`return;` in a function whose return type is not `()`"
1819                        );
1820                        if let Some(value) = fcx.err_ctxt().ty_kind_suggestion(fcx.param_env, found)
1821                        {
1822                            err.span_suggestion_verbose(
1823                                cause.span.shrink_to_hi(),
1824                                "give the `return` a value of the expected type",
1825                                format!(" {value}"),
1826                                Applicability::HasPlaceholders,
1827                            );
1828                        }
1829                        err.span_label(cause.span, "return type is not `()`");
1830                    }
1831                    ObligationCauseCode::BlockTailExpression(blk_id, ..) => {
1832                        err = self.report_return_mismatched_types(
1833                            cause,
1834                            expected,
1835                            found,
1836                            coercion_error,
1837                            fcx,
1838                            blk_id,
1839                            expression,
1840                        );
1841                        unsized_return = self.is_return_ty_definitely_unsized(fcx);
1842                    }
1843                    ObligationCauseCode::ReturnValue(return_expr_id) => {
1844                        err = self.report_return_mismatched_types(
1845                            cause,
1846                            expected,
1847                            found,
1848                            coercion_error,
1849                            fcx,
1850                            return_expr_id,
1851                            expression,
1852                        );
1853                        unsized_return = self.is_return_ty_definitely_unsized(fcx);
1854                    }
1855                    ObligationCauseCode::MatchExpressionArm(MatchExpressionArmCause {
1856                        arm_span,
1857                        arm_ty,
1858                        prior_arm_ty,
1859                        ref prior_non_diverging_arms,
1860                        tail_defines_return_position_impl_trait: Some(rpit_def_id),
1861                        ..
1862                    }) => {
1863                        err = fcx.err_ctxt().report_mismatched_types(
1864                            cause,
1865                            fcx.param_env,
1866                            expected,
1867                            found,
1868                            coercion_error,
1869                        );
1870                        // Check that we're actually in the second or later arm
1871                        if prior_non_diverging_arms.len() > 0 {
1872                            self.suggest_boxing_tail_for_return_position_impl_trait(
1873                                fcx,
1874                                &mut err,
1875                                rpit_def_id,
1876                                arm_ty,
1877                                prior_arm_ty,
1878                                prior_non_diverging_arms
1879                                    .iter()
1880                                    .chain(std::iter::once(&arm_span))
1881                                    .copied(),
1882                            );
1883                        }
1884                    }
1885                    ObligationCauseCode::IfExpression {
1886                        expr_id,
1887                        tail_defines_return_position_impl_trait: Some(rpit_def_id),
1888                    } => {
1889                        let hir::Node::Expr(hir::Expr {
1890                            kind: hir::ExprKind::If(_, then_expr, Some(else_expr)),
1891                            ..
1892                        }) = fcx.tcx.hir_node(expr_id)
1893                        else {
1894                            unreachable!();
1895                        };
1896                        err = fcx.err_ctxt().report_mismatched_types(
1897                            cause,
1898                            fcx.param_env,
1899                            expected,
1900                            found,
1901                            coercion_error,
1902                        );
1903                        let then_span = fcx.find_block_span_from_hir_id(then_expr.hir_id);
1904                        let else_span = fcx.find_block_span_from_hir_id(else_expr.hir_id);
1905                        // Don't suggest wrapping whole block in `Box::new`.
1906                        if then_span != then_expr.span && else_span != else_expr.span {
1907                            let then_ty = fcx.typeck_results.borrow().expr_ty(then_expr);
1908                            let else_ty = fcx.typeck_results.borrow().expr_ty(else_expr);
1909                            self.suggest_boxing_tail_for_return_position_impl_trait(
1910                                fcx,
1911                                &mut err,
1912                                rpit_def_id,
1913                                then_ty,
1914                                else_ty,
1915                                [then_span, else_span].into_iter(),
1916                            );
1917                        }
1918                    }
1919                    _ => {
1920                        err = fcx.err_ctxt().report_mismatched_types(
1921                            cause,
1922                            fcx.param_env,
1923                            expected,
1924                            found,
1925                            coercion_error,
1926                        );
1927                    }
1928                }
1929
1930                augment_error(&mut err);
1931
1932                if let Some(expr) = expression {
1933                    if let hir::ExprKind::Loop(
1934                        block,
1935                        _,
1936                        loop_src @ (hir::LoopSource::While | hir::LoopSource::ForLoop),
1937                        _,
1938                    ) = expr.kind
1939                    {
1940                        let loop_type = if loop_src == hir::LoopSource::While {
1941                            "`while` loops"
1942                        } else {
1943                            "`for` loops"
1944                        };
1945
1946                        err.note(format!("{loop_type} evaluate to unit type `()`"));
1947                        if loop_src == hir::LoopSource::While
1948                            && let Some(pat) = irrefutable_if_let_expr(block)
1949                        {
1950                            err.span_label(
1951                                pat.span,
1952                                "this pattern always matches, consider using `loop` instead",
1953                            );
1954                        }
1955                    }
1956
1957                    fcx.emit_coerce_suggestions(
1958                        &mut err,
1959                        expr,
1960                        found,
1961                        expected,
1962                        None,
1963                        Some(coercion_error),
1964                    );
1965                }
1966
1967                let guar = err.emit_err_unless_delay(unsized_return);
1968
1969                self.final_ty = Some(Ty::new_error(fcx.tcx, guar));
1970            }
1971        }
1972    }
1973
1974    fn suggest_boxing_tail_for_return_position_impl_trait(
1975        &self,
1976        fcx: &FnCtxt<'_, 'tcx>,
1977        err: &mut Diag<'_>,
1978        rpit_def_id: LocalDefId,
1979        a_ty: Ty<'tcx>,
1980        b_ty: Ty<'tcx>,
1981        arm_spans: impl Iterator<Item = Span>,
1982    ) {
1983        let compatible = |ty: Ty<'tcx>| {
1984            fcx.probe(|_| {
1985                let ocx = ObligationCtxt::new(fcx);
1986                ocx.register_obligations(
1987                    fcx.tcx
1988                        .item_self_bounds(rpit_def_id)
1989                        .iter_identity()
1990                        .map(Unnormalized::skip_norm_wip)
1991                        .filter_map(|clause| {
1992                            let predicate = clause
1993                                .kind()
1994                                .map_bound(|clause| match clause {
1995                                    ty::ClauseKind::Trait(trait_pred) => {
1996                                        Some(ty::ClauseKind::Trait(
1997                                            trait_pred.with_replaced_self_ty(fcx.tcx, ty),
1998                                        ))
1999                                    }
2000                                    ty::ClauseKind::Projection(proj_pred) => {
2001                                        Some(ty::ClauseKind::Projection(
2002                                            proj_pred.with_replaced_self_ty(fcx.tcx, ty),
2003                                        ))
2004                                    }
2005                                    _ => None,
2006                                })
2007                                .transpose()?;
2008                            Some(Obligation::new(
2009                                fcx.tcx,
2010                                ObligationCause::dummy(),
2011                                fcx.param_env,
2012                                predicate,
2013                            ))
2014                        }),
2015                );
2016                ocx.try_evaluate_obligations().no_errors()
2017            })
2018        };
2019
2020        if !compatible(a_ty) || !compatible(b_ty) {
2021            return;
2022        }
2023
2024        let rpid_def_span = fcx.tcx.def_span(rpit_def_id);
2025        err.subdiagnostic(SuggestBoxingForReturnImplTrait::ChangeReturnType {
2026            start_sp: rpid_def_span.with_hi(rpid_def_span.lo() + BytePos(4)),
2027            end_sp: rpid_def_span.shrink_to_hi(),
2028        });
2029
2030        let (starts, ends) =
2031            arm_spans.map(|span| (span.shrink_to_lo(), span.shrink_to_hi())).unzip();
2032        err.subdiagnostic(SuggestBoxingForReturnImplTrait::BoxReturnExpr { starts, ends });
2033    }
2034
2035    fn report_return_mismatched_types<'infcx>(
2036        &self,
2037        cause: &ObligationCause<'tcx>,
2038        expected: Ty<'tcx>,
2039        found: Ty<'tcx>,
2040        ty_err: TypeError<'tcx>,
2041        fcx: &'infcx FnCtxt<'_, 'tcx>,
2042        block_or_return_id: hir::HirId,
2043        expression: Option<&'tcx hir::Expr<'tcx>>,
2044    ) -> Diag<'infcx> {
2045        let mut err =
2046            fcx.err_ctxt().report_mismatched_types(cause, fcx.param_env, expected, found, ty_err);
2047
2048        let due_to_block = #[allow(non_exhaustive_omitted_patterns)] match fcx.tcx.hir_node(block_or_return_id)
    {
    hir::Node::Block(..) => true,
    _ => false,
}matches!(fcx.tcx.hir_node(block_or_return_id), hir::Node::Block(..));
2049        let parent = fcx.tcx.parent_hir_node(block_or_return_id);
2050        if let Some(expr) = expression
2051            && let hir::Node::Expr(&hir::Expr {
2052                kind: hir::ExprKind::Closure(&hir::Closure { body, .. }),
2053                ..
2054            }) = parent
2055        {
2056            let needs_block =
2057                !#[allow(non_exhaustive_omitted_patterns)] match fcx.tcx.hir_body(body).value.kind
    {
    hir::ExprKind::Block(..) => true,
    _ => false,
}matches!(fcx.tcx.hir_body(body).value.kind, hir::ExprKind::Block(..));
2058            fcx.suggest_missing_semicolon(&mut err, expr, expected, needs_block, true);
2059        }
2060        // Verify that this is a tail expression of a function, otherwise the
2061        // label pointing out the cause for the type coercion will be wrong
2062        // as prior return coercions would not be relevant (#57664).
2063        if let Some(expr) = expression
2064            && due_to_block
2065        {
2066            fcx.suggest_missing_semicolon(&mut err, expr, expected, false, false);
2067            let pointing_at_return_type = fcx.suggest_mismatched_types_on_tail(
2068                &mut err,
2069                expr,
2070                expected,
2071                found,
2072                block_or_return_id,
2073            );
2074            if let Some(cond_expr) = fcx.tcx.hir_get_if_cause(expr.hir_id)
2075                && expected.is_unit()
2076                && !pointing_at_return_type
2077                // If the block is from an external macro or try (`?`) desugaring, then
2078                // do not suggest adding a semicolon, because there's nowhere to put it.
2079                // See issues #81943 and #87051.
2080                // Similarly, if the block is from a loop desugaring, then also do not
2081                // suggest adding a semicolon. See issue #150850.
2082                && cond_expr.span.desugaring_kind().is_none()
2083                && !cond_expr.span.in_external_macro(fcx.tcx.sess.source_map())
2084                && !#[allow(non_exhaustive_omitted_patterns)] match cond_expr.kind {
    hir::ExprKind::Match(.., hir::MatchSource::TryDesugar(_)) => true,
    _ => false,
}matches!(
2085                    cond_expr.kind,
2086                    hir::ExprKind::Match(.., hir::MatchSource::TryDesugar(_))
2087                )
2088            {
2089                if let ObligationCauseCode::BlockTailExpression(hir_id, hir::MatchSource::Normal) =
2090                    cause.code()
2091                    && let hir::Node::Block(block) = fcx.tcx.hir_node(*hir_id)
2092                    && let hir::Node::Expr(expr) = fcx.tcx.parent_hir_node(block.hir_id)
2093                    && let hir::Node::Expr(if_expr) = fcx.tcx.parent_hir_node(expr.hir_id)
2094                    && let hir::ExprKind::If(_cond, _then, None) = if_expr.kind
2095                {
2096                    err.span_label(
2097                        cond_expr.span,
2098                        "`if` expressions without `else` arms expect their inner expression to be `()`",
2099                    );
2100                } else {
2101                    err.span_label(cond_expr.span, "expected this to be `()`");
2102                }
2103                if expr.can_have_side_effects() {
2104                    // Don't suggest semicolon after if expressions as it does not fix the issue
2105                    if !#[allow(non_exhaustive_omitted_patterns)] match cond_expr.kind {
    hir::ExprKind::If(..) => true,
    _ => false,
}matches!(cond_expr.kind, hir::ExprKind::If(..)) {
2106                        fcx.suggest_semicolon_at_end(cond_expr.span, &mut err);
2107                    }
2108                }
2109            }
2110        }
2111
2112        // If this is due to an explicit `return`, suggest adding a return type.
2113        if let Some((fn_id, fn_decl)) = fcx.get_fn_decl(block_or_return_id)
2114            && !due_to_block
2115        {
2116            fcx.suggest_missing_return_type(&mut err, fn_decl, expected, found, fn_id);
2117        }
2118
2119        let is_return_position = fcx
2120            .tcx
2121            .hir_get_fn_id_for_return_block(block_or_return_id)
2122            .is_some_and(|fn_id| fn_id == fcx.tcx.local_def_id_to_hir_id(fcx.body_def_id));
2123
2124        // If this is due to a block, then maybe we forgot a `return`/`break`.
2125        if due_to_block
2126            && let Some(expr) = expression
2127            && let Some(parent_fn_decl) =
2128                fcx.tcx.hir_fn_decl_by_hir_id(fcx.tcx.local_def_id_to_hir_id(fcx.body_def_id))
2129            && !is_return_position
2130        {
2131            fcx.suggest_missing_break_or_return_expr(
2132                &mut err,
2133                expr,
2134                parent_fn_decl,
2135                expected,
2136                found,
2137                block_or_return_id,
2138                fcx.body_def_id,
2139            );
2140        }
2141
2142        if is_return_position
2143            && let Some(sp) = fcx.ret_coercion_span.get()
2144            // If the closure has an explicit return type annotation, or if
2145            // the closure's return type has been inferred from outside
2146            // requirements (such as an Fn* trait bound), then a type error
2147            // may occur at the first return expression we see in the closure
2148            // (if it conflicts with the declared return type). Skip adding a
2149            // note in this case, since it would be incorrect.
2150            && let Some(fn_sig) = fcx.fn_sig()
2151            && fn_sig.output().is_ty_var()
2152        {
2153            err.span_note(sp, ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("return type inferred to be `{0}` here",
                expected))
    })format!("return type inferred to be `{expected}` here"));
2154        }
2155
2156        err
2157    }
2158
2159    /// Checks whether the return type is unsized via an obligation, which makes
2160    /// sure we consider `dyn Trait: Sized` where clauses, which are trivially
2161    /// false but technically valid for typeck.
2162    fn is_return_ty_definitely_unsized(&self, fcx: &FnCtxt<'_, 'tcx>) -> bool {
2163        if let Some(sig) = fcx.fn_sig() {
2164            !fcx.predicate_may_hold(&Obligation::new(
2165                fcx.tcx,
2166                ObligationCause::dummy(),
2167                fcx.param_env,
2168                ty::TraitRef::new(
2169                    fcx.tcx,
2170                    fcx.tcx.require_lang_item(LangItem::Sized, DUMMY_SP),
2171                    [sig.output()],
2172                ),
2173            ))
2174        } else {
2175            false
2176        }
2177    }
2178
2179    pub(crate) fn complete<'a>(self, fcx: &FnCtxt<'a, 'tcx>) -> Ty<'tcx> {
2180        if let Some(final_ty) = self.final_ty {
2181            final_ty
2182        } else {
2183            // If we only had inputs that were of type `!` (or no
2184            // inputs at all), then the final type is `!`.
2185            if !self.expressions.is_empty() {
    ::core::panicking::panic("assertion failed: self.expressions.is_empty()")
};assert!(self.expressions.is_empty());
2186            fcx.tcx.types.never
2187        }
2188    }
2189}
2190
2191fn irrefutable_if_let_expr<'hir>(block: &hir::Block<'hir>) -> Option<&'hir hir::Pat<'hir>> {
2192    let hir::ExprKind::If(cond, _, _) = block.expr?.kind else {
2193        return None;
2194    };
2195    let hir::ExprKind::Let(let_expr) = cond.kind else {
2196        return None;
2197    };
2198    simple_irrefutable_pattern(let_expr.pat).then_some(let_expr.pat)
2199}
2200
2201fn simple_irrefutable_pattern(pat: &hir::Pat<'_>) -> bool {
2202    match pat.kind {
2203        hir::PatKind::Wild | hir::PatKind::Binding(_, _, _, None) => true,
2204        hir::PatKind::Tuple(pats, _) => pats.iter().all(simple_irrefutable_pattern),
2205        _ => false,
2206    }
2207}
2208
2209/// Recursively visit goals to decide whether an unsizing is possible.
2210/// `Break`s when it isn't, and an error should be raised.
2211/// `Continue`s when an unsizing ok based on an implementation of the `Unsize` trait / lang item.
2212struct CoerceVisitor<'a, 'tcx> {
2213    fcx: &'a FnCtxt<'a, 'tcx>,
2214    span: Span,
2215    /// Whether the coercion is impossible. If so we sometimes still try to
2216    /// coerce in these cases to emit better errors. This changes the behavior
2217    /// when hitting the recursion limit.
2218    errored: bool,
2219}
2220
2221impl<'tcx> ProofTreeVisitor<'tcx> for CoerceVisitor<'_, 'tcx> {
2222    type Result = ControlFlow<()>;
2223
2224    fn span(&self) -> Span {
2225        self.span
2226    }
2227
2228    fn visit_goal(&mut self, goal: &inspect::InspectGoal<'_, 'tcx>) -> Self::Result {
2229        let Some(pred) = goal.goal().predicate.as_trait_clause() else {
2230            return ControlFlow::Continue(());
2231        };
2232
2233        // Make sure this predicate is referring to either an `Unsize` or `CoerceUnsized` trait,
2234        // Otherwise there's nothing to do.
2235        if !self.fcx.tcx.is_lang_item(pred.def_id(), LangItem::Unsize)
2236            && !self.fcx.tcx.is_lang_item(pred.def_id(), LangItem::CoerceUnsized)
2237        {
2238            return ControlFlow::Continue(());
2239        }
2240
2241        match goal.result() {
2242            // If we prove the `Unsize` or `CoerceUnsized` goal, continue recursing.
2243            Ok(Certainty::Yes) => ControlFlow::Continue(()),
2244            Err(NoSolution) => {
2245                self.errored = true;
2246                // Even if we find no solution, continue recursing if we find a single candidate
2247                // for which we're shallowly certain it holds to get the right error source.
2248                if let [only_candidate] = &goal.candidates()[..]
2249                    && only_candidate.shallow_certainty() == Certainty::Yes
2250                {
2251                    only_candidate.visit_nested_no_probe(self)
2252                } else {
2253                    ControlFlow::Break(())
2254                }
2255            }
2256            Ok(Certainty::Maybe(_)) => {
2257                // FIXME: structurally normalize?
2258                if self.fcx.tcx.is_lang_item(pred.def_id(), LangItem::Unsize)
2259                    && let ty::Dynamic(..) = pred.skip_binder().trait_ref.args.type_at(1).kind()
2260                    && let ty::Infer(ty::TyVar(vid)) = *pred.self_ty().skip_binder().kind()
2261                    && self.fcx.type_var_is_sized(vid)
2262                {
2263                    // We get here when trying to unsize a type variable to a `dyn Trait`,
2264                    // knowing that that variable is sized. Unsizing definitely has to happen in that case.
2265                    // If the variable weren't sized, we may not need an unsizing coercion.
2266                    // In general, we don't want to add coercions too eagerly since it makes error messages much worse.
2267                    ControlFlow::Continue(())
2268                } else if let Some(cand) = goal.unique_applicable_candidate()
2269                    && cand.shallow_certainty() == Certainty::Yes
2270                {
2271                    cand.visit_nested_no_probe(self)
2272                } else {
2273                    ControlFlow::Break(())
2274                }
2275            }
2276        }
2277    }
2278
2279    fn on_recursion_limit(&mut self) -> Self::Result {
2280        if self.errored {
2281            // This prevents accidentally committing unfulfilled unsized coercions while trying to
2282            // find the error source for diagnostics.
2283            // See https://github.com/rust-lang/trait-system-refactor-initiative/issues/266.
2284            ControlFlow::Break(())
2285        } else {
2286            ControlFlow::Continue(())
2287        }
2288    }
2289}