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