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::Deref;
39
40use rustc_attr_data_structures::InlineAttr;
41use rustc_errors::codes::*;
42use rustc_errors::{Applicability, Diag, struct_span_code_err};
43use rustc_hir as hir;
44use rustc_hir::def_id::{DefId, LocalDefId};
45use rustc_hir_analysis::hir_ty_lowering::HirTyLowerer;
46use rustc_infer::infer::relate::RelateResult;
47use rustc_infer::infer::{DefineOpaqueTypes, InferOk, InferResult, RegionVariableOrigin};
48use rustc_infer::traits::{
49    MatchExpressionArmCause, Obligation, PredicateObligation, PredicateObligations, SelectionError,
50};
51use rustc_middle::span_bug;
52use rustc_middle::ty::adjustment::{
53    Adjust, Adjustment, AllowTwoPhase, AutoBorrow, AutoBorrowMutability, PointerCoercion,
54};
55use rustc_middle::ty::error::TypeError;
56use rustc_middle::ty::{self, GenericArgsRef, Ty, TyCtxt, TypeVisitableExt};
57use rustc_span::{BytePos, DUMMY_SP, DesugaringKind, Span};
58use rustc_trait_selection::infer::InferCtxtExt as _;
59use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt;
60use rustc_trait_selection::traits::{
61    self, ImplSource, NormalizeExt, ObligationCause, ObligationCauseCode, ObligationCtxt,
62};
63use smallvec::{SmallVec, smallvec};
64use tracing::{debug, instrument};
65
66use crate::FnCtxt;
67use crate::errors::SuggestBoxingForReturnImplTrait;
68
69struct Coerce<'a, 'tcx> {
70    fcx: &'a FnCtxt<'a, 'tcx>,
71    cause: ObligationCause<'tcx>,
72    use_lub: bool,
73    /// Determines whether or not allow_two_phase_borrow is set on any
74    /// autoref adjustments we create while coercing. We don't want to
75    /// allow deref coercions to create two-phase borrows, at least initially,
76    /// but we do need two-phase borrows for function argument reborrows.
77    /// See #47489 and #48598
78    /// See docs on the "AllowTwoPhase" type for a more detailed discussion
79    allow_two_phase: AllowTwoPhase,
80    /// Whether we allow `NeverToAny` coercions. This is unsound if we're
81    /// coercing a place expression without it counting as a read in the MIR.
82    /// This is a side-effect of HIR not really having a great distinction
83    /// between places and values.
84    coerce_never: bool,
85}
86
87impl<'a, 'tcx> Deref for Coerce<'a, 'tcx> {
88    type Target = FnCtxt<'a, 'tcx>;
89    fn deref(&self) -> &Self::Target {
90        self.fcx
91    }
92}
93
94type CoerceResult<'tcx> = InferResult<'tcx, (Vec<Adjustment<'tcx>>, Ty<'tcx>)>;
95
96/// Coercing a mutable reference to an immutable works, while
97/// coercing `&T` to `&mut T` should be forbidden.
98fn coerce_mutbls<'tcx>(
99    from_mutbl: hir::Mutability,
100    to_mutbl: hir::Mutability,
101) -> RelateResult<'tcx, ()> {
102    if from_mutbl >= to_mutbl { Ok(()) } else { Err(TypeError::Mutability) }
103}
104
105/// This always returns `Ok(...)`.
106fn success<'tcx>(
107    adj: Vec<Adjustment<'tcx>>,
108    target: Ty<'tcx>,
109    obligations: PredicateObligations<'tcx>,
110) -> CoerceResult<'tcx> {
111    Ok(InferOk { value: (adj, target), obligations })
112}
113
114impl<'f, 'tcx> Coerce<'f, 'tcx> {
115    fn new(
116        fcx: &'f FnCtxt<'f, 'tcx>,
117        cause: ObligationCause<'tcx>,
118        allow_two_phase: AllowTwoPhase,
119        coerce_never: bool,
120    ) -> Self {
121        Coerce { fcx, cause, allow_two_phase, use_lub: false, coerce_never }
122    }
123
124    fn unify_raw(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> InferResult<'tcx, Ty<'tcx>> {
125        debug!("unify(a: {:?}, b: {:?}, use_lub: {})", a, b, self.use_lub);
126        self.commit_if_ok(|_| {
127            let at = self.at(&self.cause, self.fcx.param_env);
128
129            let res = if self.use_lub {
130                at.lub(b, a)
131            } else {
132                at.sup(DefineOpaqueTypes::Yes, b, a)
133                    .map(|InferOk { value: (), obligations }| InferOk { value: b, obligations })
134            };
135
136            // In the new solver, lazy norm may allow us to shallowly equate
137            // more types, but we emit possibly impossible-to-satisfy obligations.
138            // Filter these cases out to make sure our coercion is more accurate.
139            match res {
140                Ok(InferOk { value, obligations }) if self.next_trait_solver() => {
141                    let ocx = ObligationCtxt::new(self);
142                    ocx.register_obligations(obligations);
143                    if ocx.select_where_possible().is_empty() {
144                        Ok(InferOk { value, obligations: ocx.into_pending_obligations() })
145                    } else {
146                        Err(TypeError::Mismatch)
147                    }
148                }
149                res => res,
150            }
151        })
152    }
153
154    /// Unify two types (using sub or lub).
155    fn unify(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> CoerceResult<'tcx> {
156        self.unify_raw(a, b)
157            .and_then(|InferOk { value: ty, obligations }| success(vec![], ty, obligations))
158    }
159
160    /// Unify two types (using sub or lub) and produce a specific coercion.
161    fn unify_and(
162        &self,
163        a: Ty<'tcx>,
164        b: Ty<'tcx>,
165        adjustments: impl IntoIterator<Item = Adjustment<'tcx>>,
166        final_adjustment: Adjust,
167    ) -> CoerceResult<'tcx> {
168        self.unify_raw(a, b).and_then(|InferOk { value: ty, obligations }| {
169            success(
170                adjustments
171                    .into_iter()
172                    .chain(std::iter::once(Adjustment { target: ty, kind: final_adjustment }))
173                    .collect(),
174                ty,
175                obligations,
176            )
177        })
178    }
179
180    #[instrument(skip(self))]
181    fn coerce(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> CoerceResult<'tcx> {
182        // First, remove any resolved type variables (at the top level, at least):
183        let a = self.shallow_resolve(a);
184        let b = self.shallow_resolve(b);
185        debug!("Coerce.tys({:?} => {:?})", a, b);
186
187        // Coercing from `!` to any type is allowed:
188        if a.is_never() {
189            if self.coerce_never {
190                return success(
191                    vec![Adjustment { kind: Adjust::NeverToAny, target: b }],
192                    b,
193                    PredicateObligations::new(),
194                );
195            } else {
196                // Otherwise the only coercion we can do is unification.
197                return self.unify(a, b);
198            }
199        }
200
201        // Coercing *from* an unresolved inference variable means that
202        // we have no information about the source type. This will always
203        // ultimately fall back to some form of subtyping.
204        if a.is_ty_var() {
205            return self.coerce_from_inference_variable(a, b);
206        }
207
208        // Consider coercing the subtype to a DST
209        //
210        // NOTE: this is wrapped in a `commit_if_ok` because it creates
211        // a "spurious" type variable, and we don't want to have that
212        // type variable in memory if the coercion fails.
213        let unsize = self.commit_if_ok(|_| self.coerce_unsized(a, b));
214        match unsize {
215            Ok(_) => {
216                debug!("coerce: unsize successful");
217                return unsize;
218            }
219            Err(error) => {
220                debug!(?error, "coerce: unsize failed");
221            }
222        }
223
224        // Examine the supertype and consider type-specific coercions, such
225        // as auto-borrowing, coercing pointer mutability, a `dyn*` coercion,
226        // or pin-ergonomics.
227        match *b.kind() {
228            ty::RawPtr(_, b_mutbl) => {
229                return self.coerce_raw_ptr(a, b, b_mutbl);
230            }
231            ty::Ref(r_b, _, mutbl_b) => {
232                return self.coerce_borrowed_pointer(a, b, r_b, mutbl_b);
233            }
234            ty::Adt(pin, _)
235                if self.tcx.features().pin_ergonomics()
236                    && self.tcx.is_lang_item(pin.did(), hir::LangItem::Pin) =>
237            {
238                let pin_coerce = self.commit_if_ok(|_| self.coerce_pin_ref(a, b));
239                if pin_coerce.is_ok() {
240                    return pin_coerce;
241                }
242            }
243            _ => {}
244        }
245
246        match *a.kind() {
247            ty::FnDef(..) => {
248                // Function items are coercible to any closure
249                // type; function pointers are not (that would
250                // require double indirection).
251                // Additionally, we permit coercion of function
252                // items to drop the unsafe qualifier.
253                self.coerce_from_fn_item(a, b)
254            }
255            ty::FnPtr(a_sig_tys, a_hdr) => {
256                // We permit coercion of fn pointers to drop the
257                // unsafe qualifier.
258                self.coerce_from_fn_pointer(a_sig_tys.with(a_hdr), b)
259            }
260            ty::Closure(closure_def_id_a, args_a) => {
261                // Non-capturing closures are coercible to
262                // function pointers or unsafe function pointers.
263                // It cannot convert closures that require unsafe.
264                self.coerce_closure_to_fn(a, closure_def_id_a, args_a, b)
265            }
266            _ => {
267                // Otherwise, just use unification rules.
268                self.unify(a, b)
269            }
270        }
271    }
272
273    /// Coercing *from* an inference variable. In this case, we have no information
274    /// about the source type, so we can't really do a true coercion and we always
275    /// fall back to subtyping (`unify_and`).
276    fn coerce_from_inference_variable(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> CoerceResult<'tcx> {
277        debug!("coerce_from_inference_variable(a={:?}, b={:?})", a, b);
278        debug_assert!(a.is_ty_var() && self.shallow_resolve(a) == a);
279        debug_assert!(self.shallow_resolve(b) == b);
280
281        if b.is_ty_var() {
282            // Two unresolved type variables: create a `Coerce` predicate.
283            let target_ty = if self.use_lub { self.next_ty_var(self.cause.span) } else { b };
284
285            let mut obligations = PredicateObligations::with_capacity(2);
286            for &source_ty in &[a, b] {
287                if source_ty != target_ty {
288                    obligations.push(Obligation::new(
289                        self.tcx(),
290                        self.cause.clone(),
291                        self.param_env,
292                        ty::Binder::dummy(ty::PredicateKind::Coerce(ty::CoercePredicate {
293                            a: source_ty,
294                            b: target_ty,
295                        })),
296                    ));
297                }
298            }
299
300            debug!(
301                "coerce_from_inference_variable: two inference variables, target_ty={:?}, obligations={:?}",
302                target_ty, obligations
303            );
304            success(vec![], target_ty, obligations)
305        } else {
306            // One unresolved type variable: just apply subtyping, we may be able
307            // to do something useful.
308            self.unify(a, b)
309        }
310    }
311
312    /// Reborrows `&mut A` to `&mut B` and `&(mut) A` to `&B`.
313    /// To match `A` with `B`, autoderef will be performed,
314    /// calling `deref`/`deref_mut` where necessary.
315    fn coerce_borrowed_pointer(
316        &self,
317        a: Ty<'tcx>,
318        b: Ty<'tcx>,
319        r_b: ty::Region<'tcx>,
320        mutbl_b: hir::Mutability,
321    ) -> CoerceResult<'tcx> {
322        debug!("coerce_borrowed_pointer(a={:?}, b={:?})", a, b);
323        debug_assert!(self.shallow_resolve(a) == a);
324        debug_assert!(self.shallow_resolve(b) == b);
325
326        // If we have a parameter of type `&M T_a` and the value
327        // provided is `expr`, we will be adding an implicit borrow,
328        // meaning that we convert `f(expr)` to `f(&M *expr)`. Therefore,
329        // to type check, we will construct the type that `&M*expr` would
330        // yield.
331
332        let (r_a, mt_a) = match *a.kind() {
333            ty::Ref(r_a, ty, mutbl) => {
334                let mt_a = ty::TypeAndMut { ty, mutbl };
335                coerce_mutbls(mt_a.mutbl, mutbl_b)?;
336                (r_a, mt_a)
337            }
338            _ => return self.unify(a, b),
339        };
340
341        let span = self.cause.span;
342
343        let mut first_error = None;
344        let mut r_borrow_var = None;
345        let mut autoderef = self.autoderef(span, a);
346        let mut found = None;
347
348        for (referent_ty, autoderefs) in autoderef.by_ref() {
349            if autoderefs == 0 {
350                // Don't let this pass, otherwise it would cause
351                // &T to autoref to &&T.
352                continue;
353            }
354
355            // At this point, we have deref'd `a` to `referent_ty`. So
356            // imagine we are coercing from `&'a mut Vec<T>` to `&'b mut [T]`.
357            // In the autoderef loop for `&'a mut Vec<T>`, we would get
358            // three callbacks:
359            //
360            // - `&'a mut Vec<T>` -- 0 derefs, just ignore it
361            // - `Vec<T>` -- 1 deref
362            // - `[T]` -- 2 deref
363            //
364            // At each point after the first callback, we want to
365            // check to see whether this would match out target type
366            // (`&'b mut [T]`) if we autoref'd it. We can't just
367            // compare the referent types, though, because we still
368            // have to consider the mutability. E.g., in the case
369            // we've been considering, we have an `&mut` reference, so
370            // the `T` in `[T]` needs to be unified with equality.
371            //
372            // Therefore, we construct reference types reflecting what
373            // the types will be after we do the final auto-ref and
374            // compare those. Note that this means we use the target
375            // mutability [1], since it may be that we are coercing
376            // from `&mut T` to `&U`.
377            //
378            // One fine point concerns the region that we use. We
379            // choose the region such that the region of the final
380            // type that results from `unify` will be the region we
381            // want for the autoref:
382            //
383            // - if in sub mode, that means we want to use `'b` (the
384            //   region from the target reference) for both
385            //   pointers [2]. This is because sub mode (somewhat
386            //   arbitrarily) returns the subtype region. In the case
387            //   where we are coercing to a target type, we know we
388            //   want to use that target type region (`'b`) because --
389            //   for the program to type-check -- it must be the
390            //   smaller of the two.
391            //   - One fine point. It may be surprising that we can
392            //     use `'b` without relating `'a` and `'b`. The reason
393            //     that this is ok is that what we produce is
394            //     effectively a `&'b *x` expression (if you could
395            //     annotate the region of a borrow), and regionck has
396            //     code that adds edges from the region of a borrow
397            //     (`'b`, here) into the regions in the borrowed
398            //     expression (`*x`, here). (Search for "link".)
399            // - if in lub mode, things can get fairly complicated. The
400            //   easiest thing is just to make a fresh
401            //   region variable [4], which effectively means we defer
402            //   the decision to region inference (and regionck, which will add
403            //   some more edges to this variable). However, this can wind up
404            //   creating a crippling number of variables in some cases --
405            //   e.g., #32278 -- so we optimize one particular case [3].
406            //   Let me try to explain with some examples:
407            //   - The "running example" above represents the simple case,
408            //     where we have one `&` reference at the outer level and
409            //     ownership all the rest of the way down. In this case,
410            //     we want `LUB('a, 'b)` as the resulting region.
411            //   - However, if there are nested borrows, that region is
412            //     too strong. Consider a coercion from `&'a &'x Rc<T>` to
413            //     `&'b T`. In this case, `'a` is actually irrelevant.
414            //     The pointer we want is `LUB('x, 'b`). If we choose `LUB('a,'b)`
415            //     we get spurious errors (`ui/regions-lub-ref-ref-rc.rs`).
416            //     (The errors actually show up in borrowck, typically, because
417            //     this extra edge causes the region `'a` to be inferred to something
418            //     too big, which then results in borrowck errors.)
419            //   - We could track the innermost shared reference, but there is already
420            //     code in regionck that has the job of creating links between
421            //     the region of a borrow and the regions in the thing being
422            //     borrowed (here, `'a` and `'x`), and it knows how to handle
423            //     all the various cases. So instead we just make a region variable
424            //     and let regionck figure it out.
425            let r = if !self.use_lub {
426                r_b // [2] above
427            } else if autoderefs == 1 {
428                r_a // [3] above
429            } else {
430                if r_borrow_var.is_none() {
431                    // create var lazily, at most once
432                    let coercion = RegionVariableOrigin::Coercion(span);
433                    let r = self.next_region_var(coercion);
434                    r_borrow_var = Some(r); // [4] above
435                }
436                r_borrow_var.unwrap()
437            };
438            let derefd_ty_a = Ty::new_ref(
439                self.tcx,
440                r,
441                referent_ty,
442                mutbl_b, // [1] above
443            );
444            match self.unify_raw(derefd_ty_a, b) {
445                Ok(ok) => {
446                    found = Some(ok);
447                    break;
448                }
449                Err(err) => {
450                    if first_error.is_none() {
451                        first_error = Some(err);
452                    }
453                }
454            }
455        }
456
457        // Extract type or return an error. We return the first error
458        // we got, which should be from relating the "base" type
459        // (e.g., in example above, the failure from relating `Vec<T>`
460        // to the target type), since that should be the least
461        // confusing.
462        let Some(InferOk { value: ty, mut obligations }) = found else {
463            if let Some(first_error) = first_error {
464                debug!("coerce_borrowed_pointer: failed with err = {:?}", first_error);
465                return Err(first_error);
466            } else {
467                // This may happen in the new trait solver since autoderef requires
468                // the pointee to be structurally normalizable, or else it'll just bail.
469                // So when we have a type like `&<not well formed>`, then we get no
470                // autoderef steps (even though there should be at least one). That means
471                // we get no type mismatches, since the loop above just exits early.
472                return Err(TypeError::Mismatch);
473            }
474        };
475
476        if ty == a && mt_a.mutbl.is_not() && autoderef.step_count() == 1 {
477            // As a special case, if we would produce `&'a *x`, that's
478            // a total no-op. We end up with the type `&'a T` just as
479            // we started with. In that case, just skip it
480            // altogether. This is just an optimization.
481            //
482            // Note that for `&mut`, we DO want to reborrow --
483            // otherwise, this would be a move, which might be an
484            // error. For example `foo(self.x)` where `self` and
485            // `self.x` both have `&mut `type would be a move of
486            // `self.x`, but we auto-coerce it to `foo(&mut *self.x)`,
487            // which is a borrow.
488            assert!(mutbl_b.is_not()); // can only coerce &T -> &U
489            return success(vec![], ty, obligations);
490        }
491
492        let InferOk { value: mut adjustments, obligations: o } =
493            self.adjust_steps_as_infer_ok(&autoderef);
494        obligations.extend(o);
495        obligations.extend(autoderef.into_obligations());
496
497        // Now apply the autoref. We have to extract the region out of
498        // the final ref type we got.
499        let ty::Ref(..) = ty.kind() else {
500            span_bug!(span, "expected a ref type, got {:?}", ty);
501        };
502        let mutbl = AutoBorrowMutability::new(mutbl_b, self.allow_two_phase);
503        adjustments.push(Adjustment { kind: Adjust::Borrow(AutoBorrow::Ref(mutbl)), target: ty });
504
505        debug!("coerce_borrowed_pointer: succeeded ty={:?} adjustments={:?}", ty, adjustments);
506
507        success(adjustments, ty, obligations)
508    }
509
510    /// Performs [unsized coercion] by emulating a fulfillment loop on a
511    /// `CoerceUnsized` goal until all `CoerceUnsized` and `Unsize` goals
512    /// are successfully selected.
513    ///
514    /// [unsized coercion](https://doc.rust-lang.org/reference/type-coercions.html#unsized-coercions)
515    #[instrument(skip(self), level = "debug")]
516    fn coerce_unsized(&self, source: Ty<'tcx>, target: Ty<'tcx>) -> CoerceResult<'tcx> {
517        debug!(?source, ?target);
518        debug_assert!(self.shallow_resolve(source) == source);
519        debug_assert!(self.shallow_resolve(target) == target);
520
521        // We don't apply any coercions incase either the source or target
522        // aren't sufficiently well known but tend to instead just equate
523        // them both.
524        if source.is_ty_var() {
525            debug!("coerce_unsized: source is a TyVar, bailing out");
526            return Err(TypeError::Mismatch);
527        }
528        if target.is_ty_var() {
529            debug!("coerce_unsized: target is a TyVar, bailing out");
530            return Err(TypeError::Mismatch);
531        }
532
533        // This is an optimization because coercion is one of the most common
534        // operations that we do in typeck, since it happens at every assignment
535        // and call arg (among other positions).
536        //
537        // These targets are known to never be RHS in `LHS: CoerceUnsized<RHS>`.
538        // That's because these are built-in types for which a core-provided impl
539        // doesn't exist, and for which a user-written impl is invalid.
540        //
541        // This is technically incomplete when users write impossible bounds like
542        // `where T: CoerceUnsized<usize>`, for example, but that trait is unstable
543        // and coercion is allowed to be incomplete. The only case where this matters
544        // is impossible bounds.
545        //
546        // Note that some of these types implement `LHS: Unsize<RHS>`, but they
547        // do not implement *`CoerceUnsized`* which is the root obligation of the
548        // check below.
549        match target.kind() {
550            ty::Bool
551            | ty::Char
552            | ty::Int(_)
553            | ty::Uint(_)
554            | ty::Float(_)
555            | ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
556            | ty::Str
557            | ty::Array(_, _)
558            | ty::Slice(_)
559            | ty::FnDef(_, _)
560            | ty::FnPtr(_, _)
561            | ty::Dynamic(_, _, _)
562            | ty::Closure(_, _)
563            | ty::CoroutineClosure(_, _)
564            | ty::Coroutine(_, _)
565            | ty::CoroutineWitness(_, _)
566            | ty::Never
567            | ty::Tuple(_) => return Err(TypeError::Mismatch),
568            _ => {}
569        }
570        // Additionally, we ignore `&str -> &str` coercions, which happen very
571        // commonly since strings are one of the most used argument types in Rust,
572        // we do coercions when type checking call expressions.
573        if let ty::Ref(_, source_pointee, ty::Mutability::Not) = *source.kind()
574            && source_pointee.is_str()
575            && let ty::Ref(_, target_pointee, ty::Mutability::Not) = *target.kind()
576            && target_pointee.is_str()
577        {
578            return Err(TypeError::Mismatch);
579        }
580
581        let traits =
582            (self.tcx.lang_items().unsize_trait(), self.tcx.lang_items().coerce_unsized_trait());
583        let (Some(unsize_did), Some(coerce_unsized_did)) = traits else {
584            debug!("missing Unsize or CoerceUnsized traits");
585            return Err(TypeError::Mismatch);
586        };
587
588        // Note, we want to avoid unnecessary unsizing. We don't want to coerce to
589        // a DST unless we have to. This currently comes out in the wash since
590        // we can't unify [T] with U. But to properly support DST, we need to allow
591        // that, at which point we will need extra checks on the target here.
592
593        // Handle reborrows before selecting `Source: CoerceUnsized<Target>`.
594        let reborrow = match (source.kind(), target.kind()) {
595            (&ty::Ref(_, ty_a, mutbl_a), &ty::Ref(_, _, mutbl_b)) => {
596                coerce_mutbls(mutbl_a, mutbl_b)?;
597
598                let coercion = RegionVariableOrigin::Coercion(self.cause.span);
599                let r_borrow = self.next_region_var(coercion);
600
601                // We don't allow two-phase borrows here, at least for initial
602                // implementation. If it happens that this coercion is a function argument,
603                // the reborrow in coerce_borrowed_ptr will pick it up.
604                let mutbl = AutoBorrowMutability::new(mutbl_b, AllowTwoPhase::No);
605
606                Some((
607                    Adjustment { kind: Adjust::Deref(None), target: ty_a },
608                    Adjustment {
609                        kind: Adjust::Borrow(AutoBorrow::Ref(mutbl)),
610                        target: Ty::new_ref(self.tcx, r_borrow, ty_a, mutbl_b),
611                    },
612                ))
613            }
614            (&ty::Ref(_, ty_a, mt_a), &ty::RawPtr(_, mt_b)) => {
615                coerce_mutbls(mt_a, mt_b)?;
616
617                Some((
618                    Adjustment { kind: Adjust::Deref(None), target: ty_a },
619                    Adjustment {
620                        kind: Adjust::Borrow(AutoBorrow::RawPtr(mt_b)),
621                        target: Ty::new_ptr(self.tcx, ty_a, mt_b),
622                    },
623                ))
624            }
625            _ => None,
626        };
627        let coerce_source = reborrow.as_ref().map_or(source, |(_, r)| r.target);
628
629        // Setup either a subtyping or a LUB relationship between
630        // the `CoerceUnsized` target type and the expected type.
631        // We only have the latter, so we use an inference variable
632        // for the former and let type inference do the rest.
633        let coerce_target = self.next_ty_var(self.cause.span);
634
635        let mut coercion = self.unify_and(
636            coerce_target,
637            target,
638            reborrow.into_iter().flat_map(|(deref, autoref)| [deref, autoref]),
639            Adjust::Pointer(PointerCoercion::Unsize),
640        )?;
641
642        let mut selcx = traits::SelectionContext::new(self);
643
644        // Create an obligation for `Source: CoerceUnsized<Target>`.
645        let cause = self.cause(self.cause.span, ObligationCauseCode::Coercion { source, target });
646
647        // Use a FIFO queue for this custom fulfillment procedure.
648        //
649        // A Vec (or SmallVec) is not a natural choice for a queue. However,
650        // this code path is hot, and this queue usually has a max length of 1
651        // and almost never more than 3. By using a SmallVec we avoid an
652        // allocation, at the (very small) cost of (occasionally) having to
653        // shift subsequent elements down when removing the front element.
654        let mut queue: SmallVec<[PredicateObligation<'tcx>; 4]> = smallvec![Obligation::new(
655            self.tcx,
656            cause,
657            self.fcx.param_env,
658            ty::TraitRef::new(self.tcx, coerce_unsized_did, [coerce_source, coerce_target])
659        )];
660
661        // Keep resolving `CoerceUnsized` and `Unsize` predicates to avoid
662        // emitting a coercion in cases like `Foo<$1>` -> `Foo<$2>`, where
663        // inference might unify those two inner type variables later.
664        let traits = [coerce_unsized_did, unsize_did];
665        while !queue.is_empty() {
666            let obligation = queue.remove(0);
667            let trait_pred = match obligation.predicate.kind().no_bound_vars() {
668                Some(ty::PredicateKind::Clause(ty::ClauseKind::Trait(trait_pred)))
669                    if traits.contains(&trait_pred.def_id()) =>
670                {
671                    self.resolve_vars_if_possible(trait_pred)
672                }
673                // Eagerly process alias-relate obligations in new trait solver,
674                // since these can be emitted in the process of solving trait goals,
675                // but we need to constrain vars before processing goals mentioning
676                // them.
677                Some(ty::PredicateKind::AliasRelate(..)) => {
678                    let ocx = ObligationCtxt::new(self);
679                    ocx.register_obligation(obligation);
680                    if !ocx.select_where_possible().is_empty() {
681                        return Err(TypeError::Mismatch);
682                    }
683                    coercion.obligations.extend(ocx.into_pending_obligations());
684                    continue;
685                }
686                _ => {
687                    coercion.obligations.push(obligation);
688                    continue;
689                }
690            };
691            debug!("coerce_unsized resolve step: {:?}", trait_pred);
692            match selcx.select(&obligation.with(selcx.tcx(), trait_pred)) {
693                // Uncertain or unimplemented.
694                Ok(None) => {
695                    if trait_pred.def_id() == unsize_did {
696                        let self_ty = trait_pred.self_ty();
697                        let unsize_ty = trait_pred.trait_ref.args[1].expect_ty();
698                        debug!("coerce_unsized: ambiguous unsize case for {:?}", trait_pred);
699                        match (self_ty.kind(), unsize_ty.kind()) {
700                            (&ty::Infer(ty::TyVar(v)), ty::Dynamic(..))
701                                if self.type_var_is_sized(v) =>
702                            {
703                                debug!("coerce_unsized: have sized infer {:?}", v);
704                                coercion.obligations.push(obligation);
705                                // `$0: Unsize<dyn Trait>` where we know that `$0: Sized`, try going
706                                // for unsizing.
707                            }
708                            _ => {
709                                // Some other case for `$0: Unsize<Something>`. Note that we
710                                // hit this case even if `Something` is a sized type, so just
711                                // don't do the coercion.
712                                debug!("coerce_unsized: ambiguous unsize");
713                                return Err(TypeError::Mismatch);
714                            }
715                        }
716                    } else {
717                        debug!("coerce_unsized: early return - ambiguous");
718                        return Err(TypeError::Mismatch);
719                    }
720                }
721                Err(SelectionError::Unimplemented) => {
722                    debug!("coerce_unsized: early return - can't prove obligation");
723                    return Err(TypeError::Mismatch);
724                }
725
726                Err(SelectionError::TraitDynIncompatible(_)) => {
727                    // Dyn compatibility errors in coercion will *always* be due to the
728                    // fact that the RHS of the coercion is a non-dyn compatible `dyn Trait`
729                    // written in source somewhere (otherwise we will never have lowered
730                    // the dyn trait from HIR to middle).
731                    //
732                    // There's no reason to emit yet another dyn compatibility error,
733                    // especially since the span will differ slightly and thus not be
734                    // deduplicated at all!
735                    self.fcx.set_tainted_by_errors(
736                        self.fcx
737                            .dcx()
738                            .span_delayed_bug(self.cause.span, "dyn compatibility during coercion"),
739                    );
740                }
741                Err(err) => {
742                    let guar = self.err_ctxt().report_selection_error(
743                        obligation.clone(),
744                        &obligation,
745                        &err,
746                    );
747                    self.fcx.set_tainted_by_errors(guar);
748                    // Treat this like an obligation and follow through
749                    // with the unsizing - the lack of a coercion should
750                    // be silent, as it causes a type mismatch later.
751                }
752
753                Ok(Some(ImplSource::UserDefined(impl_source))) => {
754                    queue.extend(impl_source.nested);
755                    // Certain incoherent `CoerceUnsized` implementations may cause ICEs,
756                    // so check the impl's validity. Taint the body so that we don't try
757                    // to evaluate these invalid coercions in CTFE. We only need to do this
758                    // for local impls, since upstream impls should be valid.
759                    if impl_source.impl_def_id.is_local()
760                        && let Err(guar) =
761                            self.tcx.ensure_ok().coerce_unsized_info(impl_source.impl_def_id)
762                    {
763                        self.fcx.set_tainted_by_errors(guar);
764                    }
765                }
766                Ok(Some(impl_source)) => queue.extend(impl_source.nested_obligations()),
767            }
768        }
769
770        Ok(coercion)
771    }
772
773    /// Applies reborrowing for `Pin`
774    ///
775    /// We currently only support reborrowing `Pin<&mut T>` as `Pin<&mut T>`. This is accomplished
776    /// by inserting a call to `Pin::as_mut` during MIR building.
777    ///
778    /// In the future we might want to support other reborrowing coercions, such as:
779    /// - `Pin<&mut T>` as `Pin<&T>`
780    /// - `Pin<&T>` as `Pin<&T>`
781    /// - `Pin<Box<T>>` as `Pin<&T>`
782    /// - `Pin<Box<T>>` as `Pin<&mut T>`
783    #[instrument(skip(self), level = "trace")]
784    fn coerce_pin_ref(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> CoerceResult<'tcx> {
785        debug_assert!(self.shallow_resolve(a) == a);
786        debug_assert!(self.shallow_resolve(b) == b);
787
788        // We need to make sure the two types are compatible for coercion.
789        // Then we will build a ReborrowPin adjustment and return that as an InferOk.
790
791        // Right now we can only reborrow if this is a `Pin<&mut T>`.
792        let extract_pin_mut = |ty: Ty<'tcx>| {
793            // Get the T out of Pin<T>
794            let (pin, ty) = match ty.kind() {
795                ty::Adt(pin, args) if self.tcx.is_lang_item(pin.did(), hir::LangItem::Pin) => {
796                    (*pin, args[0].expect_ty())
797                }
798                _ => {
799                    debug!("can't reborrow {:?} as pinned", ty);
800                    return Err(TypeError::Mismatch);
801                }
802            };
803            // Make sure the T is something we understand (just `&mut U` for now)
804            match ty.kind() {
805                ty::Ref(region, ty, mutbl) => Ok((pin, *region, *ty, *mutbl)),
806                _ => {
807                    debug!("can't reborrow pin of inner type {:?}", ty);
808                    Err(TypeError::Mismatch)
809                }
810            }
811        };
812
813        let (pin, a_region, a_ty, mut_a) = extract_pin_mut(a)?;
814        let (_, _, _b_ty, mut_b) = extract_pin_mut(b)?;
815
816        coerce_mutbls(mut_a, mut_b)?;
817
818        // update a with b's mutability since we'll be coercing mutability
819        let a = Ty::new_adt(
820            self.tcx,
821            pin,
822            self.tcx.mk_args(&[Ty::new_ref(self.tcx, a_region, a_ty, mut_b).into()]),
823        );
824
825        // To complete the reborrow, we need to make sure we can unify the inner types, and if so we
826        // add the adjustments.
827        self.unify_and(a, b, [], Adjust::ReborrowPin(mut_b))
828    }
829
830    fn coerce_from_safe_fn(
831        &self,
832        fn_ty_a: ty::PolyFnSig<'tcx>,
833        b: Ty<'tcx>,
834        adjustment: Option<Adjust>,
835    ) -> CoerceResult<'tcx> {
836        debug_assert!(self.shallow_resolve(b) == b);
837
838        self.commit_if_ok(|snapshot| {
839            let outer_universe = self.infcx.universe();
840
841            let result = if let ty::FnPtr(_, hdr_b) = b.kind()
842                && fn_ty_a.safety().is_safe()
843                && hdr_b.safety.is_unsafe()
844            {
845                let unsafe_a = self.tcx.safe_to_unsafe_fn_ty(fn_ty_a);
846                self.unify_and(
847                    unsafe_a,
848                    b,
849                    adjustment
850                        .map(|kind| Adjustment { kind, target: Ty::new_fn_ptr(self.tcx, fn_ty_a) }),
851                    Adjust::Pointer(PointerCoercion::UnsafeFnPointer),
852                )
853            } else {
854                let a = Ty::new_fn_ptr(self.tcx, fn_ty_a);
855                match adjustment {
856                    Some(adjust) => self.unify_and(a, b, [], adjust),
857                    None => self.unify(a, b),
858                }
859            };
860
861            // FIXME(#73154): This is a hack. Currently LUB can generate
862            // unsolvable constraints. Additionally, it returns `a`
863            // unconditionally, even when the "LUB" is `b`. In the future, we
864            // want the coerced type to be the actual supertype of these two,
865            // but for now, we want to just error to ensure we don't lock
866            // ourselves into a specific behavior with NLL.
867            self.leak_check(outer_universe, Some(snapshot))?;
868
869            result
870        })
871    }
872
873    fn coerce_from_fn_pointer(
874        &self,
875        fn_ty_a: ty::PolyFnSig<'tcx>,
876        b: Ty<'tcx>,
877    ) -> CoerceResult<'tcx> {
878        debug!(?fn_ty_a, ?b, "coerce_from_fn_pointer");
879        debug_assert!(self.shallow_resolve(b) == b);
880
881        self.coerce_from_safe_fn(fn_ty_a, b, None)
882    }
883
884    fn coerce_from_fn_item(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> CoerceResult<'tcx> {
885        debug!("coerce_from_fn_item(a={:?}, b={:?})", a, b);
886        debug_assert!(self.shallow_resolve(a) == a);
887        debug_assert!(self.shallow_resolve(b) == b);
888
889        let InferOk { value: b, mut obligations } =
890            self.at(&self.cause, self.param_env).normalize(b);
891
892        match b.kind() {
893            ty::FnPtr(_, b_hdr) => {
894                let mut a_sig = a.fn_sig(self.tcx);
895                if let ty::FnDef(def_id, _) = *a.kind() {
896                    // Intrinsics are not coercible to function pointers
897                    if self.tcx.intrinsic(def_id).is_some() {
898                        return Err(TypeError::IntrinsicCast);
899                    }
900
901                    let fn_attrs = self.tcx.codegen_fn_attrs(def_id);
902                    if matches!(fn_attrs.inline, InlineAttr::Force { .. }) {
903                        return Err(TypeError::ForceInlineCast);
904                    }
905
906                    if b_hdr.safety.is_safe()
907                        && self.tcx.codegen_fn_attrs(def_id).safe_target_features
908                    {
909                        // Allow the coercion if the current function has all the features that would be
910                        // needed to call the coercee safely.
911                        if let Some(safe_sig) = self.tcx.adjust_target_feature_sig(
912                            def_id,
913                            a_sig,
914                            self.fcx.body_id.into(),
915                        ) {
916                            a_sig = safe_sig;
917                        } else {
918                            return Err(TypeError::TargetFeatureCast(def_id));
919                        }
920                    }
921                }
922
923                let InferOk { value: a_sig, obligations: o1 } =
924                    self.at(&self.cause, self.param_env).normalize(a_sig);
925                obligations.extend(o1);
926
927                let InferOk { value, obligations: o2 } = self.coerce_from_safe_fn(
928                    a_sig,
929                    b,
930                    Some(Adjust::Pointer(PointerCoercion::ReifyFnPointer)),
931                )?;
932
933                obligations.extend(o2);
934                Ok(InferOk { value, obligations })
935            }
936            _ => self.unify(a, b),
937        }
938    }
939
940    /// Attempts to coerce from the type of a non-capturing closure
941    /// into a function pointer.
942    fn coerce_closure_to_fn(
943        &self,
944        a: Ty<'tcx>,
945        closure_def_id_a: DefId,
946        args_a: GenericArgsRef<'tcx>,
947        b: Ty<'tcx>,
948    ) -> CoerceResult<'tcx> {
949        debug_assert!(self.shallow_resolve(a) == a);
950        debug_assert!(self.shallow_resolve(b) == b);
951
952        match b.kind() {
953            // At this point we haven't done capture analysis, which means
954            // that the ClosureArgs just contains an inference variable instead
955            // of tuple of captured types.
956            //
957            // All we care here is if any variable is being captured and not the exact paths,
958            // so we check `upvars_mentioned` for root variables being captured.
959            ty::FnPtr(_, hdr)
960                if self
961                    .tcx
962                    .upvars_mentioned(closure_def_id_a.expect_local())
963                    .is_none_or(|u| u.is_empty()) =>
964            {
965                // We coerce the closure, which has fn type
966                //     `extern "rust-call" fn((arg0,arg1,...)) -> _`
967                // to
968                //     `fn(arg0,arg1,...) -> _`
969                // or
970                //     `unsafe fn(arg0,arg1,...) -> _`
971                let closure_sig = args_a.as_closure().sig();
972                let safety = hdr.safety;
973                let pointer_ty =
974                    Ty::new_fn_ptr(self.tcx, self.tcx.signature_unclosure(closure_sig, safety));
975                debug!("coerce_closure_to_fn(a={:?}, b={:?}, pty={:?})", a, b, pointer_ty);
976                self.unify_and(
977                    pointer_ty,
978                    b,
979                    [],
980                    Adjust::Pointer(PointerCoercion::ClosureFnPointer(safety)),
981                )
982            }
983            _ => self.unify(a, b),
984        }
985    }
986
987    fn coerce_raw_ptr(
988        &self,
989        a: Ty<'tcx>,
990        b: Ty<'tcx>,
991        mutbl_b: hir::Mutability,
992    ) -> CoerceResult<'tcx> {
993        debug!("coerce_raw_ptr(a={:?}, b={:?})", a, b);
994        debug_assert!(self.shallow_resolve(a) == a);
995        debug_assert!(self.shallow_resolve(b) == b);
996
997        let (is_ref, mt_a) = match *a.kind() {
998            ty::Ref(_, ty, mutbl) => (true, ty::TypeAndMut { ty, mutbl }),
999            ty::RawPtr(ty, mutbl) => (false, ty::TypeAndMut { ty, mutbl }),
1000            _ => return self.unify(a, b),
1001        };
1002        coerce_mutbls(mt_a.mutbl, mutbl_b)?;
1003
1004        // Check that the types which they point at are compatible.
1005        let a_raw = Ty::new_ptr(self.tcx, mt_a.ty, mutbl_b);
1006        // Although references and raw ptrs have the same
1007        // representation, we still register an Adjust::DerefRef so that
1008        // regionck knows that the region for `a` must be valid here.
1009        if is_ref {
1010            self.unify_and(
1011                a_raw,
1012                b,
1013                [Adjustment { kind: Adjust::Deref(None), target: mt_a.ty }],
1014                Adjust::Borrow(AutoBorrow::RawPtr(mutbl_b)),
1015            )
1016        } else if mt_a.mutbl != mutbl_b {
1017            self.unify_and(a_raw, b, [], Adjust::Pointer(PointerCoercion::MutToConstPointer))
1018        } else {
1019            self.unify(a_raw, b)
1020        }
1021    }
1022}
1023
1024impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1025    /// Attempt to coerce an expression to a type, and return the
1026    /// adjusted type of the expression, if successful.
1027    /// Adjustments are only recorded if the coercion succeeded.
1028    /// The expressions *must not* have any preexisting adjustments.
1029    pub(crate) fn coerce(
1030        &self,
1031        expr: &'tcx hir::Expr<'tcx>,
1032        expr_ty: Ty<'tcx>,
1033        mut target: Ty<'tcx>,
1034        allow_two_phase: AllowTwoPhase,
1035        cause: Option<ObligationCause<'tcx>>,
1036    ) -> RelateResult<'tcx, Ty<'tcx>> {
1037        let source = self.try_structurally_resolve_type(expr.span, expr_ty);
1038        if self.next_trait_solver() {
1039            target = self.try_structurally_resolve_type(
1040                cause.as_ref().map_or(expr.span, |cause| cause.span),
1041                target,
1042            );
1043        }
1044        debug!("coercion::try({:?}: {:?} -> {:?})", expr, source, target);
1045
1046        let cause =
1047            cause.unwrap_or_else(|| self.cause(expr.span, ObligationCauseCode::ExprAssignable));
1048        let coerce = Coerce::new(
1049            self,
1050            cause,
1051            allow_two_phase,
1052            self.expr_guaranteed_to_constitute_read_for_never(expr),
1053        );
1054        let ok = self.commit_if_ok(|_| coerce.coerce(source, target))?;
1055
1056        let (adjustments, _) = self.register_infer_ok_obligations(ok);
1057        self.apply_adjustments(expr, adjustments);
1058        Ok(if let Err(guar) = expr_ty.error_reported() {
1059            Ty::new_error(self.tcx, guar)
1060        } else {
1061            target
1062        })
1063    }
1064
1065    /// Probe whether `expr_ty` can be coerced to `target_ty`. This has no side-effects,
1066    /// and may return false positives if types are not yet fully constrained by inference.
1067    ///
1068    /// Returns false if the coercion is not possible, or if the coercion creates any
1069    /// sub-obligations that result in errors.
1070    ///
1071    /// This should only be used for diagnostics.
1072    pub(crate) fn may_coerce(&self, expr_ty: Ty<'tcx>, target_ty: Ty<'tcx>) -> bool {
1073        let cause = self.cause(DUMMY_SP, ObligationCauseCode::ExprAssignable);
1074        // We don't ever need two-phase here since we throw out the result of the coercion.
1075        // We also just always set `coerce_never` to true, since this is a heuristic.
1076        let coerce = Coerce::new(self, cause.clone(), AllowTwoPhase::No, true);
1077        self.probe(|_| {
1078            // Make sure to structurally resolve the types, since we use
1079            // the `TyKind`s heavily in coercion.
1080            let ocx = ObligationCtxt::new(self);
1081            let structurally_resolve = |ty| {
1082                let ty = self.shallow_resolve(ty);
1083                if self.next_trait_solver()
1084                    && let ty::Alias(..) = ty.kind()
1085                {
1086                    ocx.structurally_normalize_ty(&cause, self.param_env, ty)
1087                } else {
1088                    Ok(ty)
1089                }
1090            };
1091            let Ok(expr_ty) = structurally_resolve(expr_ty) else {
1092                return false;
1093            };
1094            let Ok(target_ty) = structurally_resolve(target_ty) else {
1095                return false;
1096            };
1097
1098            let Ok(ok) = coerce.coerce(expr_ty, target_ty) else {
1099                return false;
1100            };
1101            ocx.register_obligations(ok.obligations);
1102            ocx.select_where_possible().is_empty()
1103        })
1104    }
1105
1106    /// Given a type and a target type, this function will calculate and return
1107    /// how many dereference steps needed to coerce `expr_ty` to `target`. If
1108    /// it's not possible, return `None`.
1109    pub(crate) fn deref_steps_for_suggestion(
1110        &self,
1111        expr_ty: Ty<'tcx>,
1112        target: Ty<'tcx>,
1113    ) -> Option<usize> {
1114        let cause = self.cause(DUMMY_SP, ObligationCauseCode::ExprAssignable);
1115        // We don't ever need two-phase here since we throw out the result of the coercion.
1116        let coerce = Coerce::new(self, cause, AllowTwoPhase::No, true);
1117        coerce.autoderef(DUMMY_SP, expr_ty).find_map(|(ty, steps)| {
1118            self.probe(|_| coerce.unify_raw(ty, target)).ok().map(|_| steps)
1119        })
1120    }
1121
1122    /// Given a type, this function will calculate and return the type given
1123    /// for `<Ty as Deref>::Target` only if `Ty` also implements `DerefMut`.
1124    ///
1125    /// This function is for diagnostics only, since it does not register
1126    /// trait or region sub-obligations. (presumably we could, but it's not
1127    /// particularly important for diagnostics...)
1128    pub(crate) fn deref_once_mutably_for_diagnostic(&self, expr_ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
1129        self.autoderef(DUMMY_SP, expr_ty).silence_errors().nth(1).and_then(|(deref_ty, _)| {
1130            self.infcx
1131                .type_implements_trait(
1132                    self.tcx.lang_items().deref_mut_trait()?,
1133                    [expr_ty],
1134                    self.param_env,
1135                )
1136                .may_apply()
1137                .then_some(deref_ty)
1138        })
1139    }
1140
1141    /// Given some expressions, their known unified type and another expression,
1142    /// tries to unify the types, potentially inserting coercions on any of the
1143    /// provided expressions and returns their LUB (aka "common supertype").
1144    ///
1145    /// This is really an internal helper. From outside the coercion
1146    /// module, you should instantiate a `CoerceMany` instance.
1147    fn try_find_coercion_lub<E>(
1148        &self,
1149        cause: &ObligationCause<'tcx>,
1150        exprs: &[E],
1151        prev_ty: Ty<'tcx>,
1152        new: &hir::Expr<'_>,
1153        new_ty: Ty<'tcx>,
1154    ) -> RelateResult<'tcx, Ty<'tcx>>
1155    where
1156        E: AsCoercionSite,
1157    {
1158        let prev_ty = self.try_structurally_resolve_type(cause.span, prev_ty);
1159        let new_ty = self.try_structurally_resolve_type(new.span, new_ty);
1160        debug!(
1161            "coercion::try_find_coercion_lub({:?}, {:?}, exprs={:?} exprs)",
1162            prev_ty,
1163            new_ty,
1164            exprs.len()
1165        );
1166
1167        // The following check fixes #88097, where the compiler erroneously
1168        // attempted to coerce a closure type to itself via a function pointer.
1169        if prev_ty == new_ty {
1170            return Ok(prev_ty);
1171        }
1172
1173        let is_force_inline = |ty: Ty<'tcx>| {
1174            if let ty::FnDef(did, _) = ty.kind() {
1175                matches!(self.tcx.codegen_fn_attrs(did).inline, InlineAttr::Force { .. })
1176            } else {
1177                false
1178            }
1179        };
1180        if is_force_inline(prev_ty) || is_force_inline(new_ty) {
1181            return Err(TypeError::ForceInlineCast);
1182        }
1183
1184        // Special-case that coercion alone cannot handle:
1185        // Function items or non-capturing closures of differing IDs or GenericArgs.
1186        let (a_sig, b_sig) = {
1187            let is_capturing_closure = |ty: Ty<'tcx>| {
1188                if let &ty::Closure(closure_def_id, _args) = ty.kind() {
1189                    self.tcx.upvars_mentioned(closure_def_id.expect_local()).is_some()
1190                } else {
1191                    false
1192                }
1193            };
1194            if is_capturing_closure(prev_ty) || is_capturing_closure(new_ty) {
1195                (None, None)
1196            } else {
1197                match (prev_ty.kind(), new_ty.kind()) {
1198                    (ty::FnDef(..), ty::FnDef(..)) => {
1199                        // Don't reify if the function types have a LUB, i.e., they
1200                        // are the same function and their parameters have a LUB.
1201                        match self.commit_if_ok(|_| {
1202                            // We need to eagerly handle nested obligations due to lazy norm.
1203                            if self.next_trait_solver() {
1204                                let ocx = ObligationCtxt::new(self);
1205                                let value = ocx.lub(cause, self.param_env, prev_ty, new_ty)?;
1206                                if ocx.select_where_possible().is_empty() {
1207                                    Ok(InferOk {
1208                                        value,
1209                                        obligations: ocx.into_pending_obligations(),
1210                                    })
1211                                } else {
1212                                    Err(TypeError::Mismatch)
1213                                }
1214                            } else {
1215                                self.at(cause, self.param_env).lub(prev_ty, new_ty)
1216                            }
1217                        }) {
1218                            // We have a LUB of prev_ty and new_ty, just return it.
1219                            Ok(ok) => return Ok(self.register_infer_ok_obligations(ok)),
1220                            Err(_) => {
1221                                (Some(prev_ty.fn_sig(self.tcx)), Some(new_ty.fn_sig(self.tcx)))
1222                            }
1223                        }
1224                    }
1225                    (ty::Closure(_, args), ty::FnDef(..)) => {
1226                        let b_sig = new_ty.fn_sig(self.tcx);
1227                        let a_sig =
1228                            self.tcx.signature_unclosure(args.as_closure().sig(), b_sig.safety());
1229                        (Some(a_sig), Some(b_sig))
1230                    }
1231                    (ty::FnDef(..), ty::Closure(_, args)) => {
1232                        let a_sig = prev_ty.fn_sig(self.tcx);
1233                        let b_sig =
1234                            self.tcx.signature_unclosure(args.as_closure().sig(), a_sig.safety());
1235                        (Some(a_sig), Some(b_sig))
1236                    }
1237                    (ty::Closure(_, args_a), ty::Closure(_, args_b)) => (
1238                        Some(
1239                            self.tcx
1240                                .signature_unclosure(args_a.as_closure().sig(), hir::Safety::Safe),
1241                        ),
1242                        Some(
1243                            self.tcx
1244                                .signature_unclosure(args_b.as_closure().sig(), hir::Safety::Safe),
1245                        ),
1246                    ),
1247                    _ => (None, None),
1248                }
1249            }
1250        };
1251        if let (Some(a_sig), Some(b_sig)) = (a_sig, b_sig) {
1252            // The signature must match.
1253            let (a_sig, b_sig) = self.normalize(new.span, (a_sig, b_sig));
1254            let sig = self
1255                .at(cause, self.param_env)
1256                .lub(a_sig, b_sig)
1257                .map(|ok| self.register_infer_ok_obligations(ok))?;
1258
1259            // Reify both sides and return the reified fn pointer type.
1260            let fn_ptr = Ty::new_fn_ptr(self.tcx, sig);
1261            let prev_adjustment = match prev_ty.kind() {
1262                ty::Closure(..) => {
1263                    Adjust::Pointer(PointerCoercion::ClosureFnPointer(a_sig.safety()))
1264                }
1265                ty::FnDef(..) => Adjust::Pointer(PointerCoercion::ReifyFnPointer),
1266                _ => span_bug!(cause.span, "should not try to coerce a {prev_ty} to a fn pointer"),
1267            };
1268            let next_adjustment = match new_ty.kind() {
1269                ty::Closure(..) => {
1270                    Adjust::Pointer(PointerCoercion::ClosureFnPointer(b_sig.safety()))
1271                }
1272                ty::FnDef(..) => Adjust::Pointer(PointerCoercion::ReifyFnPointer),
1273                _ => span_bug!(new.span, "should not try to coerce a {new_ty} to a fn pointer"),
1274            };
1275            for expr in exprs.iter().map(|e| e.as_coercion_site()) {
1276                self.apply_adjustments(
1277                    expr,
1278                    vec![Adjustment { kind: prev_adjustment.clone(), target: fn_ptr }],
1279                );
1280            }
1281            self.apply_adjustments(new, vec![Adjustment { kind: next_adjustment, target: fn_ptr }]);
1282            return Ok(fn_ptr);
1283        }
1284
1285        // Configure a Coerce instance to compute the LUB.
1286        // We don't allow two-phase borrows on any autorefs this creates since we
1287        // probably aren't processing function arguments here and even if we were,
1288        // they're going to get autorefed again anyway and we can apply 2-phase borrows
1289        // at that time.
1290        //
1291        // NOTE: we set `coerce_never` to `true` here because coercion LUBs only
1292        // operate on values and not places, so a never coercion is valid.
1293        let mut coerce = Coerce::new(self, cause.clone(), AllowTwoPhase::No, true);
1294        coerce.use_lub = true;
1295
1296        // First try to coerce the new expression to the type of the previous ones,
1297        // but only if the new expression has no coercion already applied to it.
1298        let mut first_error = None;
1299        if !self.typeck_results.borrow().adjustments().contains_key(new.hir_id) {
1300            let result = self.commit_if_ok(|_| coerce.coerce(new_ty, prev_ty));
1301            match result {
1302                Ok(ok) => {
1303                    let (adjustments, target) = self.register_infer_ok_obligations(ok);
1304                    self.apply_adjustments(new, adjustments);
1305                    debug!(
1306                        "coercion::try_find_coercion_lub: was able to coerce from new type {:?} to previous type {:?} ({:?})",
1307                        new_ty, prev_ty, target
1308                    );
1309                    return Ok(target);
1310                }
1311                Err(e) => first_error = Some(e),
1312            }
1313        }
1314
1315        match self.commit_if_ok(|_| coerce.coerce(prev_ty, new_ty)) {
1316            Err(_) => {
1317                // Avoid giving strange errors on failed attempts.
1318                if let Some(e) = first_error {
1319                    Err(e)
1320                } else {
1321                    Err(self
1322                        .commit_if_ok(|_| self.at(cause, self.param_env).lub(prev_ty, new_ty))
1323                        .unwrap_err())
1324                }
1325            }
1326            Ok(ok) => {
1327                let (adjustments, target) = self.register_infer_ok_obligations(ok);
1328                for expr in exprs {
1329                    let expr = expr.as_coercion_site();
1330                    self.apply_adjustments(expr, adjustments.clone());
1331                }
1332                debug!(
1333                    "coercion::try_find_coercion_lub: was able to coerce previous type {:?} to new type {:?} ({:?})",
1334                    prev_ty, new_ty, target
1335                );
1336                Ok(target)
1337            }
1338        }
1339    }
1340}
1341
1342/// Check whether `ty` can be coerced to `output_ty`.
1343/// Used from clippy.
1344pub fn can_coerce<'tcx>(
1345    tcx: TyCtxt<'tcx>,
1346    param_env: ty::ParamEnv<'tcx>,
1347    body_id: LocalDefId,
1348    ty: Ty<'tcx>,
1349    output_ty: Ty<'tcx>,
1350) -> bool {
1351    let root_ctxt = crate::typeck_root_ctxt::TypeckRootCtxt::new(tcx, body_id);
1352    let fn_ctxt = FnCtxt::new(&root_ctxt, param_env, body_id);
1353    fn_ctxt.may_coerce(ty, output_ty)
1354}
1355
1356/// CoerceMany encapsulates the pattern you should use when you have
1357/// many expressions that are all getting coerced to a common
1358/// type. This arises, for example, when you have a match (the result
1359/// of each arm is coerced to a common type). It also arises in less
1360/// obvious places, such as when you have many `break foo` expressions
1361/// that target the same loop, or the various `return` expressions in
1362/// a function.
1363///
1364/// The basic protocol is as follows:
1365///
1366/// - Instantiate the `CoerceMany` with an initial `expected_ty`.
1367///   This will also serve as the "starting LUB". The expectation is
1368///   that this type is something which all of the expressions *must*
1369///   be coercible to. Use a fresh type variable if needed.
1370/// - For each expression whose result is to be coerced, invoke `coerce()` with.
1371///   - In some cases we wish to coerce "non-expressions" whose types are implicitly
1372///     unit. This happens for example if you have a `break` with no expression,
1373///     or an `if` with no `else`. In that case, invoke `coerce_forced_unit()`.
1374///   - `coerce()` and `coerce_forced_unit()` may report errors. They hide this
1375///     from you so that you don't have to worry your pretty head about it.
1376///     But if an error is reported, the final type will be `err`.
1377///   - Invoking `coerce()` may cause us to go and adjust the "adjustments" on
1378///     previously coerced expressions.
1379/// - When all done, invoke `complete()`. This will return the LUB of
1380///   all your expressions.
1381///   - WARNING: I don't believe this final type is guaranteed to be
1382///     related to your initial `expected_ty` in any particular way,
1383///     although it will typically be a subtype, so you should check it.
1384///   - Invoking `complete()` may cause us to go and adjust the "adjustments" on
1385///     previously coerced expressions.
1386///
1387/// Example:
1388///
1389/// ```ignore (illustrative)
1390/// let mut coerce = CoerceMany::new(expected_ty);
1391/// for expr in exprs {
1392///     let expr_ty = fcx.check_expr_with_expectation(expr, expected);
1393///     coerce.coerce(fcx, &cause, expr, expr_ty);
1394/// }
1395/// let final_ty = coerce.complete(fcx);
1396/// ```
1397pub(crate) struct CoerceMany<'tcx, 'exprs, E: AsCoercionSite> {
1398    expected_ty: Ty<'tcx>,
1399    final_ty: Option<Ty<'tcx>>,
1400    expressions: Expressions<'tcx, 'exprs, E>,
1401    pushed: usize,
1402}
1403
1404/// The type of a `CoerceMany` that is storing up the expressions into
1405/// a buffer. We use this in `check/mod.rs` for things like `break`.
1406pub(crate) type DynamicCoerceMany<'tcx> = CoerceMany<'tcx, 'tcx, &'tcx hir::Expr<'tcx>>;
1407
1408enum Expressions<'tcx, 'exprs, E: AsCoercionSite> {
1409    Dynamic(Vec<&'tcx hir::Expr<'tcx>>),
1410    UpFront(&'exprs [E]),
1411}
1412
1413impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
1414    /// The usual case; collect the set of expressions dynamically.
1415    /// If the full set of coercion sites is known before hand,
1416    /// consider `with_coercion_sites()` instead to avoid allocation.
1417    pub(crate) fn new(expected_ty: Ty<'tcx>) -> Self {
1418        Self::make(expected_ty, Expressions::Dynamic(vec![]))
1419    }
1420
1421    /// As an optimization, you can create a `CoerceMany` with a
1422    /// preexisting slice of expressions. In this case, you are
1423    /// expected to pass each element in the slice to `coerce(...)` in
1424    /// order. This is used with arrays in particular to avoid
1425    /// needlessly cloning the slice.
1426    pub(crate) fn with_coercion_sites(expected_ty: Ty<'tcx>, coercion_sites: &'exprs [E]) -> Self {
1427        Self::make(expected_ty, Expressions::UpFront(coercion_sites))
1428    }
1429
1430    fn make(expected_ty: Ty<'tcx>, expressions: Expressions<'tcx, 'exprs, E>) -> Self {
1431        CoerceMany { expected_ty, final_ty: None, expressions, pushed: 0 }
1432    }
1433
1434    /// Returns the "expected type" with which this coercion was
1435    /// constructed. This represents the "downward propagated" type
1436    /// that was given to us at the start of typing whatever construct
1437    /// we are typing (e.g., the match expression).
1438    ///
1439    /// Typically, this is used as the expected type when
1440    /// type-checking each of the alternative expressions whose types
1441    /// we are trying to merge.
1442    pub(crate) fn expected_ty(&self) -> Ty<'tcx> {
1443        self.expected_ty
1444    }
1445
1446    /// Returns the current "merged type", representing our best-guess
1447    /// at the LUB of the expressions we've seen so far (if any). This
1448    /// isn't *final* until you call `self.complete()`, which will return
1449    /// the merged type.
1450    pub(crate) fn merged_ty(&self) -> Ty<'tcx> {
1451        self.final_ty.unwrap_or(self.expected_ty)
1452    }
1453
1454    /// Indicates that the value generated by `expression`, which is
1455    /// of type `expression_ty`, is one of the possibilities that we
1456    /// could coerce from. This will record `expression`, and later
1457    /// calls to `coerce` may come back and add adjustments and things
1458    /// if necessary.
1459    pub(crate) fn coerce<'a>(
1460        &mut self,
1461        fcx: &FnCtxt<'a, 'tcx>,
1462        cause: &ObligationCause<'tcx>,
1463        expression: &'tcx hir::Expr<'tcx>,
1464        expression_ty: Ty<'tcx>,
1465    ) {
1466        self.coerce_inner(fcx, cause, Some(expression), expression_ty, |_| {}, false)
1467    }
1468
1469    /// Indicates that one of the inputs is a "forced unit". This
1470    /// occurs in a case like `if foo { ... };`, where the missing else
1471    /// generates a "forced unit". Another example is a `loop { break;
1472    /// }`, where the `break` has no argument expression. We treat
1473    /// these cases slightly differently for error-reporting
1474    /// purposes. Note that these tend to correspond to cases where
1475    /// the `()` expression is implicit in the source, and hence we do
1476    /// not take an expression argument.
1477    ///
1478    /// The `augment_error` gives you a chance to extend the error
1479    /// message, in case any results (e.g., we use this to suggest
1480    /// removing a `;`).
1481    pub(crate) fn coerce_forced_unit<'a>(
1482        &mut self,
1483        fcx: &FnCtxt<'a, 'tcx>,
1484        cause: &ObligationCause<'tcx>,
1485        augment_error: impl FnOnce(&mut Diag<'_>),
1486        label_unit_as_expected: bool,
1487    ) {
1488        self.coerce_inner(
1489            fcx,
1490            cause,
1491            None,
1492            fcx.tcx.types.unit,
1493            augment_error,
1494            label_unit_as_expected,
1495        )
1496    }
1497
1498    /// The inner coercion "engine". If `expression` is `None`, this
1499    /// is a forced-unit case, and hence `expression_ty` must be
1500    /// `Nil`.
1501    #[instrument(skip(self, fcx, augment_error, label_expression_as_expected), level = "debug")]
1502    pub(crate) fn coerce_inner<'a>(
1503        &mut self,
1504        fcx: &FnCtxt<'a, 'tcx>,
1505        cause: &ObligationCause<'tcx>,
1506        expression: Option<&'tcx hir::Expr<'tcx>>,
1507        mut expression_ty: Ty<'tcx>,
1508        augment_error: impl FnOnce(&mut Diag<'_>),
1509        label_expression_as_expected: bool,
1510    ) {
1511        // Incorporate whatever type inference information we have
1512        // until now; in principle we might also want to process
1513        // pending obligations, but doing so should only improve
1514        // compatibility (hopefully that is true) by helping us
1515        // uncover never types better.
1516        if expression_ty.is_ty_var() {
1517            expression_ty = fcx.infcx.shallow_resolve(expression_ty);
1518        }
1519
1520        // If we see any error types, just propagate that error
1521        // upwards.
1522        if let Err(guar) = (expression_ty, self.merged_ty()).error_reported() {
1523            self.final_ty = Some(Ty::new_error(fcx.tcx, guar));
1524            return;
1525        }
1526
1527        let (expected, found) = if label_expression_as_expected {
1528            // In the case where this is a "forced unit", like
1529            // `break`, we want to call the `()` "expected"
1530            // since it is implied by the syntax.
1531            // (Note: not all force-units work this way.)"
1532            (expression_ty, self.merged_ty())
1533        } else {
1534            // Otherwise, the "expected" type for error
1535            // reporting is the current unification type,
1536            // which is basically the LUB of the expressions
1537            // we've seen so far (combined with the expected
1538            // type)
1539            (self.merged_ty(), expression_ty)
1540        };
1541
1542        // Handle the actual type unification etc.
1543        let result = if let Some(expression) = expression {
1544            if self.pushed == 0 {
1545                // Special-case the first expression we are coercing.
1546                // To be honest, I'm not entirely sure why we do this.
1547                // We don't allow two-phase borrows, see comment in try_find_coercion_lub for why
1548                fcx.coerce(
1549                    expression,
1550                    expression_ty,
1551                    self.expected_ty,
1552                    AllowTwoPhase::No,
1553                    Some(cause.clone()),
1554                )
1555            } else {
1556                match self.expressions {
1557                    Expressions::Dynamic(ref exprs) => fcx.try_find_coercion_lub(
1558                        cause,
1559                        exprs,
1560                        self.merged_ty(),
1561                        expression,
1562                        expression_ty,
1563                    ),
1564                    Expressions::UpFront(coercion_sites) => fcx.try_find_coercion_lub(
1565                        cause,
1566                        &coercion_sites[0..self.pushed],
1567                        self.merged_ty(),
1568                        expression,
1569                        expression_ty,
1570                    ),
1571                }
1572            }
1573        } else {
1574            // this is a hack for cases where we default to `()` because
1575            // the expression etc has been omitted from the source. An
1576            // example is an `if let` without an else:
1577            //
1578            //     if let Some(x) = ... { }
1579            //
1580            // we wind up with a second match arm that is like `_ =>
1581            // ()`. That is the case we are considering here. We take
1582            // a different path to get the right "expected, found"
1583            // message and so forth (and because we know that
1584            // `expression_ty` will be unit).
1585            //
1586            // Another example is `break` with no argument expression.
1587            assert!(expression_ty.is_unit(), "if let hack without unit type");
1588            fcx.at(cause, fcx.param_env)
1589                .eq(
1590                    // needed for tests/ui/type-alias-impl-trait/issue-65679-inst-opaque-ty-from-val-twice.rs
1591                    DefineOpaqueTypes::Yes,
1592                    expected,
1593                    found,
1594                )
1595                .map(|infer_ok| {
1596                    fcx.register_infer_ok_obligations(infer_ok);
1597                    expression_ty
1598                })
1599        };
1600
1601        debug!(?result);
1602        match result {
1603            Ok(v) => {
1604                self.final_ty = Some(v);
1605                if let Some(e) = expression {
1606                    match self.expressions {
1607                        Expressions::Dynamic(ref mut buffer) => buffer.push(e),
1608                        Expressions::UpFront(coercion_sites) => {
1609                            // if the user gave us an array to validate, check that we got
1610                            // the next expression in the list, as expected
1611                            assert_eq!(
1612                                coercion_sites[self.pushed].as_coercion_site().hir_id,
1613                                e.hir_id
1614                            );
1615                        }
1616                    }
1617                    self.pushed += 1;
1618                }
1619            }
1620            Err(coercion_error) => {
1621                // Mark that we've failed to coerce the types here to suppress
1622                // any superfluous errors we might encounter while trying to
1623                // emit or provide suggestions on how to fix the initial error.
1624                fcx.set_tainted_by_errors(
1625                    fcx.dcx().span_delayed_bug(cause.span, "coercion error but no error emitted"),
1626                );
1627                let (expected, found) = fcx.resolve_vars_if_possible((expected, found));
1628
1629                let mut err;
1630                let mut unsized_return = false;
1631                match *cause.code() {
1632                    ObligationCauseCode::ReturnNoExpression => {
1633                        err = struct_span_code_err!(
1634                            fcx.dcx(),
1635                            cause.span,
1636                            E0069,
1637                            "`return;` in a function whose return type is not `()`"
1638                        );
1639                        if let Some(value) = fcx.err_ctxt().ty_kind_suggestion(fcx.param_env, found)
1640                        {
1641                            err.span_suggestion_verbose(
1642                                cause.span.shrink_to_hi(),
1643                                "give the `return` a value of the expected type",
1644                                format!(" {value}"),
1645                                Applicability::HasPlaceholders,
1646                            );
1647                        }
1648                        err.span_label(cause.span, "return type is not `()`");
1649                    }
1650                    ObligationCauseCode::BlockTailExpression(blk_id, ..) => {
1651                        err = self.report_return_mismatched_types(
1652                            cause,
1653                            expected,
1654                            found,
1655                            coercion_error,
1656                            fcx,
1657                            blk_id,
1658                            expression,
1659                        );
1660                        unsized_return = self.is_return_ty_definitely_unsized(fcx);
1661                    }
1662                    ObligationCauseCode::ReturnValue(return_expr_id) => {
1663                        err = self.report_return_mismatched_types(
1664                            cause,
1665                            expected,
1666                            found,
1667                            coercion_error,
1668                            fcx,
1669                            return_expr_id,
1670                            expression,
1671                        );
1672                        unsized_return = self.is_return_ty_definitely_unsized(fcx);
1673                    }
1674                    ObligationCauseCode::MatchExpressionArm(box MatchExpressionArmCause {
1675                        arm_span,
1676                        arm_ty,
1677                        prior_arm_ty,
1678                        ref prior_non_diverging_arms,
1679                        tail_defines_return_position_impl_trait: Some(rpit_def_id),
1680                        ..
1681                    }) => {
1682                        err = fcx.err_ctxt().report_mismatched_types(
1683                            cause,
1684                            fcx.param_env,
1685                            expected,
1686                            found,
1687                            coercion_error,
1688                        );
1689                        // Check that we're actually in the second or later arm
1690                        if prior_non_diverging_arms.len() > 0 {
1691                            self.suggest_boxing_tail_for_return_position_impl_trait(
1692                                fcx,
1693                                &mut err,
1694                                rpit_def_id,
1695                                arm_ty,
1696                                prior_arm_ty,
1697                                prior_non_diverging_arms
1698                                    .iter()
1699                                    .chain(std::iter::once(&arm_span))
1700                                    .copied(),
1701                            );
1702                        }
1703                    }
1704                    ObligationCauseCode::IfExpression {
1705                        expr_id,
1706                        tail_defines_return_position_impl_trait: Some(rpit_def_id),
1707                    } => {
1708                        let hir::Node::Expr(hir::Expr {
1709                            kind: hir::ExprKind::If(_, then_expr, Some(else_expr)),
1710                            ..
1711                        }) = fcx.tcx.hir_node(expr_id)
1712                        else {
1713                            unreachable!();
1714                        };
1715                        err = fcx.err_ctxt().report_mismatched_types(
1716                            cause,
1717                            fcx.param_env,
1718                            expected,
1719                            found,
1720                            coercion_error,
1721                        );
1722                        let then_span = fcx.find_block_span_from_hir_id(then_expr.hir_id);
1723                        let else_span = fcx.find_block_span_from_hir_id(else_expr.hir_id);
1724                        // Don't suggest wrapping whole block in `Box::new`.
1725                        if then_span != then_expr.span && else_span != else_expr.span {
1726                            let then_ty = fcx.typeck_results.borrow().expr_ty(then_expr);
1727                            let else_ty = fcx.typeck_results.borrow().expr_ty(else_expr);
1728                            self.suggest_boxing_tail_for_return_position_impl_trait(
1729                                fcx,
1730                                &mut err,
1731                                rpit_def_id,
1732                                then_ty,
1733                                else_ty,
1734                                [then_span, else_span].into_iter(),
1735                            );
1736                        }
1737                    }
1738                    _ => {
1739                        err = fcx.err_ctxt().report_mismatched_types(
1740                            cause,
1741                            fcx.param_env,
1742                            expected,
1743                            found,
1744                            coercion_error,
1745                        );
1746                    }
1747                }
1748
1749                augment_error(&mut err);
1750
1751                if let Some(expr) = expression {
1752                    if let hir::ExprKind::Loop(
1753                        _,
1754                        _,
1755                        loop_src @ (hir::LoopSource::While | hir::LoopSource::ForLoop),
1756                        _,
1757                    ) = expr.kind
1758                    {
1759                        let loop_type = if loop_src == hir::LoopSource::While {
1760                            "`while` loops"
1761                        } else {
1762                            "`for` loops"
1763                        };
1764
1765                        err.note(format!("{loop_type} evaluate to unit type `()`"));
1766                    }
1767
1768                    fcx.emit_coerce_suggestions(
1769                        &mut err,
1770                        expr,
1771                        found,
1772                        expected,
1773                        None,
1774                        Some(coercion_error),
1775                    );
1776                }
1777
1778                let reported = err.emit_unless(unsized_return);
1779
1780                self.final_ty = Some(Ty::new_error(fcx.tcx, reported));
1781            }
1782        }
1783    }
1784
1785    fn suggest_boxing_tail_for_return_position_impl_trait(
1786        &self,
1787        fcx: &FnCtxt<'_, 'tcx>,
1788        err: &mut Diag<'_>,
1789        rpit_def_id: LocalDefId,
1790        a_ty: Ty<'tcx>,
1791        b_ty: Ty<'tcx>,
1792        arm_spans: impl Iterator<Item = Span>,
1793    ) {
1794        let compatible = |ty: Ty<'tcx>| {
1795            fcx.probe(|_| {
1796                let ocx = ObligationCtxt::new(fcx);
1797                ocx.register_obligations(
1798                    fcx.tcx.item_self_bounds(rpit_def_id).iter_identity().filter_map(|clause| {
1799                        let predicate = clause
1800                            .kind()
1801                            .map_bound(|clause| match clause {
1802                                ty::ClauseKind::Trait(trait_pred) => Some(ty::ClauseKind::Trait(
1803                                    trait_pred.with_self_ty(fcx.tcx, ty),
1804                                )),
1805                                ty::ClauseKind::Projection(proj_pred) => Some(
1806                                    ty::ClauseKind::Projection(proj_pred.with_self_ty(fcx.tcx, ty)),
1807                                ),
1808                                _ => None,
1809                            })
1810                            .transpose()?;
1811                        Some(Obligation::new(
1812                            fcx.tcx,
1813                            ObligationCause::dummy(),
1814                            fcx.param_env,
1815                            predicate,
1816                        ))
1817                    }),
1818                );
1819                ocx.select_where_possible().is_empty()
1820            })
1821        };
1822
1823        if !compatible(a_ty) || !compatible(b_ty) {
1824            return;
1825        }
1826
1827        let rpid_def_span = fcx.tcx.def_span(rpit_def_id);
1828        err.subdiagnostic(SuggestBoxingForReturnImplTrait::ChangeReturnType {
1829            start_sp: rpid_def_span.with_hi(rpid_def_span.lo() + BytePos(4)),
1830            end_sp: rpid_def_span.shrink_to_hi(),
1831        });
1832
1833        let (starts, ends) =
1834            arm_spans.map(|span| (span.shrink_to_lo(), span.shrink_to_hi())).unzip();
1835        err.subdiagnostic(SuggestBoxingForReturnImplTrait::BoxReturnExpr { starts, ends });
1836    }
1837
1838    fn report_return_mismatched_types<'infcx>(
1839        &self,
1840        cause: &ObligationCause<'tcx>,
1841        expected: Ty<'tcx>,
1842        found: Ty<'tcx>,
1843        ty_err: TypeError<'tcx>,
1844        fcx: &'infcx FnCtxt<'_, 'tcx>,
1845        block_or_return_id: hir::HirId,
1846        expression: Option<&'tcx hir::Expr<'tcx>>,
1847    ) -> Diag<'infcx> {
1848        let mut err =
1849            fcx.err_ctxt().report_mismatched_types(cause, fcx.param_env, expected, found, ty_err);
1850
1851        let due_to_block = matches!(fcx.tcx.hir_node(block_or_return_id), hir::Node::Block(..));
1852        let parent = fcx.tcx.parent_hir_node(block_or_return_id);
1853        if let Some(expr) = expression
1854            && let hir::Node::Expr(&hir::Expr {
1855                kind: hir::ExprKind::Closure(&hir::Closure { body, .. }),
1856                ..
1857            }) = parent
1858        {
1859            let needs_block =
1860                !matches!(fcx.tcx.hir_body(body).value.kind, hir::ExprKind::Block(..));
1861            fcx.suggest_missing_semicolon(&mut err, expr, expected, needs_block, true);
1862        }
1863        // Verify that this is a tail expression of a function, otherwise the
1864        // label pointing out the cause for the type coercion will be wrong
1865        // as prior return coercions would not be relevant (#57664).
1866        if let Some(expr) = expression
1867            && due_to_block
1868        {
1869            fcx.suggest_missing_semicolon(&mut err, expr, expected, false, false);
1870            let pointing_at_return_type = fcx.suggest_mismatched_types_on_tail(
1871                &mut err,
1872                expr,
1873                expected,
1874                found,
1875                block_or_return_id,
1876            );
1877            if let Some(cond_expr) = fcx.tcx.hir_get_if_cause(expr.hir_id)
1878                && expected.is_unit()
1879                && !pointing_at_return_type
1880                // If the block is from an external macro or try (`?`) desugaring, then
1881                // do not suggest adding a semicolon, because there's nowhere to put it.
1882                // See issues #81943 and #87051.
1883                && matches!(
1884                    cond_expr.span.desugaring_kind(),
1885                    None | Some(DesugaringKind::WhileLoop)
1886                )
1887                && !cond_expr.span.in_external_macro(fcx.tcx.sess.source_map())
1888                && !matches!(
1889                    cond_expr.kind,
1890                    hir::ExprKind::Match(.., hir::MatchSource::TryDesugar(_))
1891                )
1892            {
1893                err.span_label(cond_expr.span, "expected this to be `()`");
1894                if expr.can_have_side_effects() {
1895                    fcx.suggest_semicolon_at_end(cond_expr.span, &mut err);
1896                }
1897            }
1898        };
1899
1900        // If this is due to an explicit `return`, suggest adding a return type.
1901        if let Some((fn_id, fn_decl)) = fcx.get_fn_decl(block_or_return_id)
1902            && !due_to_block
1903        {
1904            fcx.suggest_missing_return_type(&mut err, fn_decl, expected, found, fn_id);
1905        }
1906
1907        // If this is due to a block, then maybe we forgot a `return`/`break`.
1908        if due_to_block
1909            && let Some(expr) = expression
1910            && let Some(parent_fn_decl) =
1911                fcx.tcx.hir_fn_decl_by_hir_id(fcx.tcx.local_def_id_to_hir_id(fcx.body_id))
1912        {
1913            fcx.suggest_missing_break_or_return_expr(
1914                &mut err,
1915                expr,
1916                parent_fn_decl,
1917                expected,
1918                found,
1919                block_or_return_id,
1920                fcx.body_id,
1921            );
1922        }
1923
1924        let ret_coercion_span = fcx.ret_coercion_span.get();
1925
1926        if let Some(sp) = ret_coercion_span
1927            // If the closure has an explicit return type annotation, or if
1928            // the closure's return type has been inferred from outside
1929            // requirements (such as an Fn* trait bound), then a type error
1930            // may occur at the first return expression we see in the closure
1931            // (if it conflicts with the declared return type). Skip adding a
1932            // note in this case, since it would be incorrect.
1933            && let Some(fn_sig) = fcx.body_fn_sig()
1934            && fn_sig.output().is_ty_var()
1935        {
1936            err.span_note(sp, format!("return type inferred to be `{expected}` here"));
1937        }
1938
1939        err
1940    }
1941
1942    /// Checks whether the return type is unsized via an obligation, which makes
1943    /// sure we consider `dyn Trait: Sized` where clauses, which are trivially
1944    /// false but technically valid for typeck.
1945    fn is_return_ty_definitely_unsized(&self, fcx: &FnCtxt<'_, 'tcx>) -> bool {
1946        if let Some(sig) = fcx.body_fn_sig() {
1947            !fcx.predicate_may_hold(&Obligation::new(
1948                fcx.tcx,
1949                ObligationCause::dummy(),
1950                fcx.param_env,
1951                ty::TraitRef::new(
1952                    fcx.tcx,
1953                    fcx.tcx.require_lang_item(hir::LangItem::Sized, DUMMY_SP),
1954                    [sig.output()],
1955                ),
1956            ))
1957        } else {
1958            false
1959        }
1960    }
1961
1962    pub(crate) fn complete<'a>(self, fcx: &FnCtxt<'a, 'tcx>) -> Ty<'tcx> {
1963        if let Some(final_ty) = self.final_ty {
1964            final_ty
1965        } else {
1966            // If we only had inputs that were of type `!` (or no
1967            // inputs at all), then the final type is `!`.
1968            assert_eq!(self.pushed, 0);
1969            fcx.tcx.types.never
1970        }
1971    }
1972}
1973
1974/// Something that can be converted into an expression to which we can
1975/// apply a coercion.
1976pub(crate) trait AsCoercionSite {
1977    fn as_coercion_site(&self) -> &hir::Expr<'_>;
1978}
1979
1980impl AsCoercionSite for hir::Expr<'_> {
1981    fn as_coercion_site(&self) -> &hir::Expr<'_> {
1982        self
1983    }
1984}
1985
1986impl<'a, T> AsCoercionSite for &'a T
1987where
1988    T: AsCoercionSite,
1989{
1990    fn as_coercion_site(&self) -> &hir::Expr<'_> {
1991        (**self).as_coercion_site()
1992    }
1993}
1994
1995impl AsCoercionSite for ! {
1996    fn as_coercion_site(&self) -> &hir::Expr<'_> {
1997        *self
1998    }
1999}
2000
2001impl AsCoercionSite for hir::Arm<'_> {
2002    fn as_coercion_site(&self) -> &hir::Expr<'_> {
2003        self.body
2004    }
2005}