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.require_lang_item(hir::LangItem::PointerLike, self.cause.span),
764                        [a],
765                    ),
766                ),
767            ])
768            .collect();
769
770        Ok(InferOk {
771            value: (
772                vec![Adjustment { kind: Adjust::Pointer(PointerCoercion::DynStar), target: b }],
773                b,
774            ),
775            obligations,
776        })
777    }
778
779    /// Applies reborrowing for `Pin`
780    ///
781    /// We currently only support reborrowing `Pin<&mut T>` as `Pin<&mut T>`. This is accomplished
782    /// by inserting a call to `Pin::as_mut` during MIR building.
783    ///
784    /// In the future we might want to support other reborrowing coercions, such as:
785    /// - `Pin<&mut T>` as `Pin<&T>`
786    /// - `Pin<&T>` as `Pin<&T>`
787    /// - `Pin<Box<T>>` as `Pin<&T>`
788    /// - `Pin<Box<T>>` as `Pin<&mut T>`
789    #[instrument(skip(self), level = "trace")]
790    fn coerce_pin_ref(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> CoerceResult<'tcx> {
791        // We need to make sure the two types are compatible for coercion.
792        // Then we will build a ReborrowPin adjustment and return that as an InferOk.
793
794        // Right now we can only reborrow if this is a `Pin<&mut T>`.
795        let extract_pin_mut = |ty: Ty<'tcx>| {
796            // Get the T out of Pin<T>
797            let (pin, ty) = match ty.kind() {
798                ty::Adt(pin, args) if self.tcx.is_lang_item(pin.did(), hir::LangItem::Pin) => {
799                    (*pin, args[0].expect_ty())
800                }
801                _ => {
802                    debug!("can't reborrow {:?} as pinned", ty);
803                    return Err(TypeError::Mismatch);
804                }
805            };
806            // Make sure the T is something we understand (just `&mut U` for now)
807            match ty.kind() {
808                ty::Ref(region, ty, mutbl) => Ok((pin, *region, *ty, *mutbl)),
809                _ => {
810                    debug!("can't reborrow pin of inner type {:?}", ty);
811                    Err(TypeError::Mismatch)
812                }
813            }
814        };
815
816        let (pin, a_region, a_ty, mut_a) = extract_pin_mut(a)?;
817        let (_, _, _b_ty, mut_b) = extract_pin_mut(b)?;
818
819        coerce_mutbls(mut_a, mut_b)?;
820
821        // update a with b's mutability since we'll be coercing mutability
822        let a = Ty::new_adt(
823            self.tcx,
824            pin,
825            self.tcx.mk_args(&[Ty::new_ref(self.tcx, a_region, a_ty, mut_b).into()]),
826        );
827
828        // To complete the reborrow, we need to make sure we can unify the inner types, and if so we
829        // add the adjustments.
830        self.unify_and(a, b, [], Adjust::ReborrowPin(mut_b))
831    }
832
833    fn coerce_from_safe_fn(
834        &self,
835        fn_ty_a: ty::PolyFnSig<'tcx>,
836        b: Ty<'tcx>,
837        adjustment: Option<Adjust>,
838    ) -> CoerceResult<'tcx> {
839        self.commit_if_ok(|snapshot| {
840            let outer_universe = self.infcx.universe();
841
842            let result = if let ty::FnPtr(_, hdr_b) = b.kind()
843                && fn_ty_a.safety().is_safe()
844                && hdr_b.safety.is_unsafe()
845            {
846                let unsafe_a = self.tcx.safe_to_unsafe_fn_ty(fn_ty_a);
847                self.unify_and(
848                    unsafe_a,
849                    b,
850                    adjustment
851                        .map(|kind| Adjustment { kind, target: Ty::new_fn_ptr(self.tcx, fn_ty_a) }),
852                    Adjust::Pointer(PointerCoercion::UnsafeFnPointer),
853                )
854            } else {
855                let a = Ty::new_fn_ptr(self.tcx, fn_ty_a);
856                match adjustment {
857                    Some(adjust) => self.unify_and(a, b, [], adjust),
858                    None => self.unify(a, b),
859                }
860            };
861
862            // FIXME(#73154): This is a hack. Currently LUB can generate
863            // unsolvable constraints. Additionally, it returns `a`
864            // unconditionally, even when the "LUB" is `b`. In the future, we
865            // want the coerced type to be the actual supertype of these two,
866            // but for now, we want to just error to ensure we don't lock
867            // ourselves into a specific behavior with NLL.
868            self.leak_check(outer_universe, Some(snapshot))?;
869
870            result
871        })
872    }
873
874    fn coerce_from_fn_pointer(
875        &self,
876        fn_ty_a: ty::PolyFnSig<'tcx>,
877        b: Ty<'tcx>,
878    ) -> CoerceResult<'tcx> {
879        //! Attempts to coerce from the type of a Rust function item
880        //! into a closure or a `proc`.
881        //!
882
883        let b = self.shallow_resolve(b);
884        debug!(?fn_ty_a, ?b, "coerce_from_fn_pointer");
885
886        self.coerce_from_safe_fn(fn_ty_a, b, None)
887    }
888
889    fn coerce_from_fn_item(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> CoerceResult<'tcx> {
890        //! Attempts to coerce from the type of a Rust function item
891        //! into a closure or a `proc`.
892
893        let b = self.shallow_resolve(b);
894        let InferOk { value: b, mut obligations } =
895            self.at(&self.cause, self.param_env).normalize(b);
896        debug!("coerce_from_fn_item(a={:?}, b={:?})", a, b);
897
898        match b.kind() {
899            ty::FnPtr(_, b_hdr) => {
900                let mut a_sig = a.fn_sig(self.tcx);
901                if let ty::FnDef(def_id, _) = *a.kind() {
902                    // Intrinsics are not coercible to function pointers
903                    if self.tcx.intrinsic(def_id).is_some() {
904                        return Err(TypeError::IntrinsicCast);
905                    }
906
907                    let fn_attrs = self.tcx.codegen_fn_attrs(def_id);
908                    if matches!(fn_attrs.inline, InlineAttr::Force { .. }) {
909                        return Err(TypeError::ForceInlineCast);
910                    }
911
912                    if b_hdr.safety.is_safe()
913                        && self.tcx.codegen_fn_attrs(def_id).safe_target_features
914                    {
915                        // Allow the coercion if the current function has all the features that would be
916                        // needed to call the coercee safely.
917                        if let Some(safe_sig) = self.tcx.adjust_target_feature_sig(
918                            def_id,
919                            a_sig,
920                            self.fcx.body_id.into(),
921                        ) {
922                            a_sig = safe_sig;
923                        } else {
924                            return Err(TypeError::TargetFeatureCast(def_id));
925                        }
926                    }
927                }
928
929                let InferOk { value: a_sig, obligations: o1 } =
930                    self.at(&self.cause, self.param_env).normalize(a_sig);
931                obligations.extend(o1);
932
933                let InferOk { value, obligations: o2 } = self.coerce_from_safe_fn(
934                    a_sig,
935                    b,
936                    Some(Adjust::Pointer(PointerCoercion::ReifyFnPointer)),
937                )?;
938
939                obligations.extend(o2);
940                Ok(InferOk { value, obligations })
941            }
942            _ => self.unify(a, b),
943        }
944    }
945
946    fn coerce_closure_to_fn(
947        &self,
948        a: Ty<'tcx>,
949        closure_def_id_a: DefId,
950        args_a: GenericArgsRef<'tcx>,
951        b: Ty<'tcx>,
952    ) -> CoerceResult<'tcx> {
953        //! Attempts to coerce from the type of a non-capturing closure
954        //! into a function pointer.
955        //!
956
957        let b = self.shallow_resolve(b);
958
959        match b.kind() {
960            // At this point we haven't done capture analysis, which means
961            // that the ClosureArgs just contains an inference variable instead
962            // of tuple of captured types.
963            //
964            // All we care here is if any variable is being captured and not the exact paths,
965            // so we check `upvars_mentioned` for root variables being captured.
966            ty::FnPtr(_, hdr)
967                if self
968                    .tcx
969                    .upvars_mentioned(closure_def_id_a.expect_local())
970                    .is_none_or(|u| u.is_empty()) =>
971            {
972                // We coerce the closure, which has fn type
973                //     `extern "rust-call" fn((arg0,arg1,...)) -> _`
974                // to
975                //     `fn(arg0,arg1,...) -> _`
976                // or
977                //     `unsafe fn(arg0,arg1,...) -> _`
978                let closure_sig = args_a.as_closure().sig();
979                let safety = hdr.safety;
980                let pointer_ty =
981                    Ty::new_fn_ptr(self.tcx, self.tcx.signature_unclosure(closure_sig, safety));
982                debug!("coerce_closure_to_fn(a={:?}, b={:?}, pty={:?})", a, b, pointer_ty);
983                self.unify_and(
984                    pointer_ty,
985                    b,
986                    [],
987                    Adjust::Pointer(PointerCoercion::ClosureFnPointer(safety)),
988                )
989            }
990            _ => self.unify(a, b),
991        }
992    }
993
994    fn coerce_raw_ptr(
995        &self,
996        a: Ty<'tcx>,
997        b: Ty<'tcx>,
998        mutbl_b: hir::Mutability,
999    ) -> CoerceResult<'tcx> {
1000        debug!("coerce_raw_ptr(a={:?}, b={:?})", a, b);
1001
1002        let (is_ref, mt_a) = match *a.kind() {
1003            ty::Ref(_, ty, mutbl) => (true, ty::TypeAndMut { ty, mutbl }),
1004            ty::RawPtr(ty, mutbl) => (false, ty::TypeAndMut { ty, mutbl }),
1005            _ => return self.unify(a, b),
1006        };
1007        coerce_mutbls(mt_a.mutbl, mutbl_b)?;
1008
1009        // Check that the types which they point at are compatible.
1010        let a_raw = Ty::new_ptr(self.tcx, mt_a.ty, mutbl_b);
1011        // Although references and raw ptrs have the same
1012        // representation, we still register an Adjust::DerefRef so that
1013        // regionck knows that the region for `a` must be valid here.
1014        if is_ref {
1015            self.unify_and(
1016                a_raw,
1017                b,
1018                [Adjustment { kind: Adjust::Deref(None), target: mt_a.ty }],
1019                Adjust::Borrow(AutoBorrow::RawPtr(mutbl_b)),
1020            )
1021        } else if mt_a.mutbl != mutbl_b {
1022            self.unify_and(a_raw, b, [], Adjust::Pointer(PointerCoercion::MutToConstPointer))
1023        } else {
1024            self.unify(a_raw, b)
1025        }
1026    }
1027}
1028
1029impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1030    /// Attempt to coerce an expression to a type, and return the
1031    /// adjusted type of the expression, if successful.
1032    /// Adjustments are only recorded if the coercion succeeded.
1033    /// The expressions *must not* have any preexisting adjustments.
1034    pub(crate) fn coerce(
1035        &self,
1036        expr: &'tcx hir::Expr<'tcx>,
1037        expr_ty: Ty<'tcx>,
1038        mut target: Ty<'tcx>,
1039        allow_two_phase: AllowTwoPhase,
1040        cause: Option<ObligationCause<'tcx>>,
1041    ) -> RelateResult<'tcx, Ty<'tcx>> {
1042        let source = self.try_structurally_resolve_type(expr.span, expr_ty);
1043        if self.next_trait_solver() {
1044            target = self.try_structurally_resolve_type(
1045                cause.as_ref().map_or(expr.span, |cause| cause.span),
1046                target,
1047            );
1048        }
1049        debug!("coercion::try({:?}: {:?} -> {:?})", expr, source, target);
1050
1051        let cause =
1052            cause.unwrap_or_else(|| self.cause(expr.span, ObligationCauseCode::ExprAssignable));
1053        let coerce = Coerce::new(
1054            self,
1055            cause,
1056            allow_two_phase,
1057            self.expr_guaranteed_to_constitute_read_for_never(expr),
1058        );
1059        let ok = self.commit_if_ok(|_| coerce.coerce(source, target))?;
1060
1061        let (adjustments, _) = self.register_infer_ok_obligations(ok);
1062        self.apply_adjustments(expr, adjustments);
1063        Ok(if let Err(guar) = expr_ty.error_reported() {
1064            Ty::new_error(self.tcx, guar)
1065        } else {
1066            target
1067        })
1068    }
1069
1070    /// Probe whether `expr_ty` can be coerced to `target_ty`. This has no side-effects,
1071    /// and may return false positives if types are not yet fully constrained by inference.
1072    ///
1073    /// Returns false if the coercion is not possible, or if the coercion creates any
1074    /// sub-obligations that result in errors.
1075    ///
1076    /// This should only be used for diagnostics.
1077    pub(crate) fn may_coerce(&self, expr_ty: Ty<'tcx>, target_ty: Ty<'tcx>) -> bool {
1078        let cause = self.cause(DUMMY_SP, ObligationCauseCode::ExprAssignable);
1079        // We don't ever need two-phase here since we throw out the result of the coercion.
1080        // We also just always set `coerce_never` to true, since this is a heuristic.
1081        let coerce = Coerce::new(self, cause.clone(), AllowTwoPhase::No, true);
1082        self.probe(|_| {
1083            // Make sure to structurally resolve the types, since we use
1084            // the `TyKind`s heavily in coercion.
1085            let ocx = ObligationCtxt::new(self);
1086            let structurally_resolve = |ty| {
1087                let ty = self.shallow_resolve(ty);
1088                if self.next_trait_solver()
1089                    && let ty::Alias(..) = ty.kind()
1090                {
1091                    ocx.structurally_normalize_ty(&cause, self.param_env, ty)
1092                } else {
1093                    Ok(ty)
1094                }
1095            };
1096            let Ok(expr_ty) = structurally_resolve(expr_ty) else {
1097                return false;
1098            };
1099            let Ok(target_ty) = structurally_resolve(target_ty) else {
1100                return false;
1101            };
1102
1103            let Ok(ok) = coerce.coerce(expr_ty, target_ty) else {
1104                return false;
1105            };
1106            ocx.register_obligations(ok.obligations);
1107            ocx.select_where_possible().is_empty()
1108        })
1109    }
1110
1111    /// Given a type and a target type, this function will calculate and return
1112    /// how many dereference steps needed to coerce `expr_ty` to `target`. If
1113    /// it's not possible, return `None`.
1114    pub(crate) fn deref_steps_for_suggestion(
1115        &self,
1116        expr_ty: Ty<'tcx>,
1117        target: Ty<'tcx>,
1118    ) -> Option<usize> {
1119        let cause = self.cause(DUMMY_SP, ObligationCauseCode::ExprAssignable);
1120        // We don't ever need two-phase here since we throw out the result of the coercion.
1121        let coerce = Coerce::new(self, cause, AllowTwoPhase::No, true);
1122        coerce.autoderef(DUMMY_SP, expr_ty).find_map(|(ty, steps)| {
1123            self.probe(|_| coerce.unify_raw(ty, target)).ok().map(|_| steps)
1124        })
1125    }
1126
1127    /// Given a type, this function will calculate and return the type given
1128    /// for `<Ty as Deref>::Target` only if `Ty` also implements `DerefMut`.
1129    ///
1130    /// This function is for diagnostics only, since it does not register
1131    /// trait or region sub-obligations. (presumably we could, but it's not
1132    /// particularly important for diagnostics...)
1133    pub(crate) fn deref_once_mutably_for_diagnostic(&self, expr_ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
1134        self.autoderef(DUMMY_SP, expr_ty).silence_errors().nth(1).and_then(|(deref_ty, _)| {
1135            self.infcx
1136                .type_implements_trait(
1137                    self.tcx.lang_items().deref_mut_trait()?,
1138                    [expr_ty],
1139                    self.param_env,
1140                )
1141                .may_apply()
1142                .then_some(deref_ty)
1143        })
1144    }
1145
1146    /// Given some expressions, their known unified type and another expression,
1147    /// tries to unify the types, potentially inserting coercions on any of the
1148    /// provided expressions and returns their LUB (aka "common supertype").
1149    ///
1150    /// This is really an internal helper. From outside the coercion
1151    /// module, you should instantiate a `CoerceMany` instance.
1152    fn try_find_coercion_lub<E>(
1153        &self,
1154        cause: &ObligationCause<'tcx>,
1155        exprs: &[E],
1156        prev_ty: Ty<'tcx>,
1157        new: &hir::Expr<'_>,
1158        new_ty: Ty<'tcx>,
1159    ) -> RelateResult<'tcx, Ty<'tcx>>
1160    where
1161        E: AsCoercionSite,
1162    {
1163        let prev_ty = self.try_structurally_resolve_type(cause.span, prev_ty);
1164        let new_ty = self.try_structurally_resolve_type(new.span, new_ty);
1165        debug!(
1166            "coercion::try_find_coercion_lub({:?}, {:?}, exprs={:?} exprs)",
1167            prev_ty,
1168            new_ty,
1169            exprs.len()
1170        );
1171
1172        // The following check fixes #88097, where the compiler erroneously
1173        // attempted to coerce a closure type to itself via a function pointer.
1174        if prev_ty == new_ty {
1175            return Ok(prev_ty);
1176        }
1177
1178        let is_force_inline = |ty: Ty<'tcx>| {
1179            if let ty::FnDef(did, _) = ty.kind() {
1180                matches!(self.tcx.codegen_fn_attrs(did).inline, InlineAttr::Force { .. })
1181            } else {
1182                false
1183            }
1184        };
1185        if is_force_inline(prev_ty) || is_force_inline(new_ty) {
1186            return Err(TypeError::ForceInlineCast);
1187        }
1188
1189        // Special-case that coercion alone cannot handle:
1190        // Function items or non-capturing closures of differing IDs or GenericArgs.
1191        let (a_sig, b_sig) = {
1192            let is_capturing_closure = |ty: Ty<'tcx>| {
1193                if let &ty::Closure(closure_def_id, _args) = ty.kind() {
1194                    self.tcx.upvars_mentioned(closure_def_id.expect_local()).is_some()
1195                } else {
1196                    false
1197                }
1198            };
1199            if is_capturing_closure(prev_ty) || is_capturing_closure(new_ty) {
1200                (None, None)
1201            } else {
1202                match (prev_ty.kind(), new_ty.kind()) {
1203                    (ty::FnDef(..), ty::FnDef(..)) => {
1204                        // Don't reify if the function types have a LUB, i.e., they
1205                        // are the same function and their parameters have a LUB.
1206                        match self.commit_if_ok(|_| {
1207                            // We need to eagerly handle nested obligations due to lazy norm.
1208                            if self.next_trait_solver() {
1209                                let ocx = ObligationCtxt::new(self);
1210                                let value = ocx.lub(cause, self.param_env, prev_ty, new_ty)?;
1211                                if ocx.select_where_possible().is_empty() {
1212                                    Ok(InferOk {
1213                                        value,
1214                                        obligations: ocx.into_pending_obligations(),
1215                                    })
1216                                } else {
1217                                    Err(TypeError::Mismatch)
1218                                }
1219                            } else {
1220                                self.at(cause, self.param_env).lub(prev_ty, new_ty)
1221                            }
1222                        }) {
1223                            // We have a LUB of prev_ty and new_ty, just return it.
1224                            Ok(ok) => return Ok(self.register_infer_ok_obligations(ok)),
1225                            Err(_) => {
1226                                (Some(prev_ty.fn_sig(self.tcx)), Some(new_ty.fn_sig(self.tcx)))
1227                            }
1228                        }
1229                    }
1230                    (ty::Closure(_, args), ty::FnDef(..)) => {
1231                        let b_sig = new_ty.fn_sig(self.tcx);
1232                        let a_sig =
1233                            self.tcx.signature_unclosure(args.as_closure().sig(), b_sig.safety());
1234                        (Some(a_sig), Some(b_sig))
1235                    }
1236                    (ty::FnDef(..), ty::Closure(_, args)) => {
1237                        let a_sig = prev_ty.fn_sig(self.tcx);
1238                        let b_sig =
1239                            self.tcx.signature_unclosure(args.as_closure().sig(), a_sig.safety());
1240                        (Some(a_sig), Some(b_sig))
1241                    }
1242                    (ty::Closure(_, args_a), ty::Closure(_, args_b)) => (
1243                        Some(
1244                            self.tcx
1245                                .signature_unclosure(args_a.as_closure().sig(), hir::Safety::Safe),
1246                        ),
1247                        Some(
1248                            self.tcx
1249                                .signature_unclosure(args_b.as_closure().sig(), hir::Safety::Safe),
1250                        ),
1251                    ),
1252                    _ => (None, None),
1253                }
1254            }
1255        };
1256        if let (Some(a_sig), Some(b_sig)) = (a_sig, b_sig) {
1257            // The signature must match.
1258            let (a_sig, b_sig) = self.normalize(new.span, (a_sig, b_sig));
1259            let sig = self
1260                .at(cause, self.param_env)
1261                .lub(a_sig, b_sig)
1262                .map(|ok| self.register_infer_ok_obligations(ok))?;
1263
1264            // Reify both sides and return the reified fn pointer type.
1265            let fn_ptr = Ty::new_fn_ptr(self.tcx, sig);
1266            let prev_adjustment = match prev_ty.kind() {
1267                ty::Closure(..) => {
1268                    Adjust::Pointer(PointerCoercion::ClosureFnPointer(a_sig.safety()))
1269                }
1270                ty::FnDef(..) => Adjust::Pointer(PointerCoercion::ReifyFnPointer),
1271                _ => span_bug!(cause.span, "should not try to coerce a {prev_ty} to a fn pointer"),
1272            };
1273            let next_adjustment = match new_ty.kind() {
1274                ty::Closure(..) => {
1275                    Adjust::Pointer(PointerCoercion::ClosureFnPointer(b_sig.safety()))
1276                }
1277                ty::FnDef(..) => Adjust::Pointer(PointerCoercion::ReifyFnPointer),
1278                _ => span_bug!(new.span, "should not try to coerce a {new_ty} to a fn pointer"),
1279            };
1280            for expr in exprs.iter().map(|e| e.as_coercion_site()) {
1281                self.apply_adjustments(
1282                    expr,
1283                    vec![Adjustment { kind: prev_adjustment.clone(), target: fn_ptr }],
1284                );
1285            }
1286            self.apply_adjustments(new, vec![Adjustment { kind: next_adjustment, target: fn_ptr }]);
1287            return Ok(fn_ptr);
1288        }
1289
1290        // Configure a Coerce instance to compute the LUB.
1291        // We don't allow two-phase borrows on any autorefs this creates since we
1292        // probably aren't processing function arguments here and even if we were,
1293        // they're going to get autorefed again anyway and we can apply 2-phase borrows
1294        // at that time.
1295        //
1296        // NOTE: we set `coerce_never` to `true` here because coercion LUBs only
1297        // operate on values and not places, so a never coercion is valid.
1298        let mut coerce = Coerce::new(self, cause.clone(), AllowTwoPhase::No, true);
1299        coerce.use_lub = true;
1300
1301        // First try to coerce the new expression to the type of the previous ones,
1302        // but only if the new expression has no coercion already applied to it.
1303        let mut first_error = None;
1304        if !self.typeck_results.borrow().adjustments().contains_key(new.hir_id) {
1305            let result = self.commit_if_ok(|_| coerce.coerce(new_ty, prev_ty));
1306            match result {
1307                Ok(ok) => {
1308                    let (adjustments, target) = self.register_infer_ok_obligations(ok);
1309                    self.apply_adjustments(new, adjustments);
1310                    debug!(
1311                        "coercion::try_find_coercion_lub: was able to coerce from new type {:?} to previous type {:?} ({:?})",
1312                        new_ty, prev_ty, target
1313                    );
1314                    return Ok(target);
1315                }
1316                Err(e) => first_error = Some(e),
1317            }
1318        }
1319
1320        match self.commit_if_ok(|_| coerce.coerce(prev_ty, new_ty)) {
1321            Err(_) => {
1322                // Avoid giving strange errors on failed attempts.
1323                if let Some(e) = first_error {
1324                    Err(e)
1325                } else {
1326                    Err(self
1327                        .commit_if_ok(|_| self.at(cause, self.param_env).lub(prev_ty, new_ty))
1328                        .unwrap_err())
1329                }
1330            }
1331            Ok(ok) => {
1332                let (adjustments, target) = self.register_infer_ok_obligations(ok);
1333                for expr in exprs {
1334                    let expr = expr.as_coercion_site();
1335                    self.apply_adjustments(expr, adjustments.clone());
1336                }
1337                debug!(
1338                    "coercion::try_find_coercion_lub: was able to coerce previous type {:?} to new type {:?} ({:?})",
1339                    prev_ty, new_ty, target
1340                );
1341                Ok(target)
1342            }
1343        }
1344    }
1345}
1346
1347/// Check whether `ty` can be coerced to `output_ty`.
1348/// Used from clippy.
1349pub fn can_coerce<'tcx>(
1350    tcx: TyCtxt<'tcx>,
1351    param_env: ty::ParamEnv<'tcx>,
1352    body_id: LocalDefId,
1353    ty: Ty<'tcx>,
1354    output_ty: Ty<'tcx>,
1355) -> bool {
1356    let root_ctxt = crate::typeck_root_ctxt::TypeckRootCtxt::new(tcx, body_id);
1357    let fn_ctxt = FnCtxt::new(&root_ctxt, param_env, body_id);
1358    fn_ctxt.may_coerce(ty, output_ty)
1359}
1360
1361/// CoerceMany encapsulates the pattern you should use when you have
1362/// many expressions that are all getting coerced to a common
1363/// type. This arises, for example, when you have a match (the result
1364/// of each arm is coerced to a common type). It also arises in less
1365/// obvious places, such as when you have many `break foo` expressions
1366/// that target the same loop, or the various `return` expressions in
1367/// a function.
1368///
1369/// The basic protocol is as follows:
1370///
1371/// - Instantiate the `CoerceMany` with an initial `expected_ty`.
1372///   This will also serve as the "starting LUB". The expectation is
1373///   that this type is something which all of the expressions *must*
1374///   be coercible to. Use a fresh type variable if needed.
1375/// - For each expression whose result is to be coerced, invoke `coerce()` with.
1376///   - In some cases we wish to coerce "non-expressions" whose types are implicitly
1377///     unit. This happens for example if you have a `break` with no expression,
1378///     or an `if` with no `else`. In that case, invoke `coerce_forced_unit()`.
1379///   - `coerce()` and `coerce_forced_unit()` may report errors. They hide this
1380///     from you so that you don't have to worry your pretty head about it.
1381///     But if an error is reported, the final type will be `err`.
1382///   - Invoking `coerce()` may cause us to go and adjust the "adjustments" on
1383///     previously coerced expressions.
1384/// - When all done, invoke `complete()`. This will return the LUB of
1385///   all your expressions.
1386///   - WARNING: I don't believe this final type is guaranteed to be
1387///     related to your initial `expected_ty` in any particular way,
1388///     although it will typically be a subtype, so you should check it.
1389///   - Invoking `complete()` may cause us to go and adjust the "adjustments" on
1390///     previously coerced expressions.
1391///
1392/// Example:
1393///
1394/// ```ignore (illustrative)
1395/// let mut coerce = CoerceMany::new(expected_ty);
1396/// for expr in exprs {
1397///     let expr_ty = fcx.check_expr_with_expectation(expr, expected);
1398///     coerce.coerce(fcx, &cause, expr, expr_ty);
1399/// }
1400/// let final_ty = coerce.complete(fcx);
1401/// ```
1402pub(crate) struct CoerceMany<'tcx, 'exprs, E: AsCoercionSite> {
1403    expected_ty: Ty<'tcx>,
1404    final_ty: Option<Ty<'tcx>>,
1405    expressions: Expressions<'tcx, 'exprs, E>,
1406    pushed: usize,
1407}
1408
1409/// The type of a `CoerceMany` that is storing up the expressions into
1410/// a buffer. We use this in `check/mod.rs` for things like `break`.
1411pub(crate) type DynamicCoerceMany<'tcx> = CoerceMany<'tcx, 'tcx, &'tcx hir::Expr<'tcx>>;
1412
1413enum Expressions<'tcx, 'exprs, E: AsCoercionSite> {
1414    Dynamic(Vec<&'tcx hir::Expr<'tcx>>),
1415    UpFront(&'exprs [E]),
1416}
1417
1418impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
1419    /// The usual case; collect the set of expressions dynamically.
1420    /// If the full set of coercion sites is known before hand,
1421    /// consider `with_coercion_sites()` instead to avoid allocation.
1422    pub(crate) fn new(expected_ty: Ty<'tcx>) -> Self {
1423        Self::make(expected_ty, Expressions::Dynamic(vec![]))
1424    }
1425
1426    /// As an optimization, you can create a `CoerceMany` with a
1427    /// preexisting slice of expressions. In this case, you are
1428    /// expected to pass each element in the slice to `coerce(...)` in
1429    /// order. This is used with arrays in particular to avoid
1430    /// needlessly cloning the slice.
1431    pub(crate) fn with_coercion_sites(expected_ty: Ty<'tcx>, coercion_sites: &'exprs [E]) -> Self {
1432        Self::make(expected_ty, Expressions::UpFront(coercion_sites))
1433    }
1434
1435    fn make(expected_ty: Ty<'tcx>, expressions: Expressions<'tcx, 'exprs, E>) -> Self {
1436        CoerceMany { expected_ty, final_ty: None, expressions, pushed: 0 }
1437    }
1438
1439    /// Returns the "expected type" with which this coercion was
1440    /// constructed. This represents the "downward propagated" type
1441    /// that was given to us at the start of typing whatever construct
1442    /// we are typing (e.g., the match expression).
1443    ///
1444    /// Typically, this is used as the expected type when
1445    /// type-checking each of the alternative expressions whose types
1446    /// we are trying to merge.
1447    pub(crate) fn expected_ty(&self) -> Ty<'tcx> {
1448        self.expected_ty
1449    }
1450
1451    /// Returns the current "merged type", representing our best-guess
1452    /// at the LUB of the expressions we've seen so far (if any). This
1453    /// isn't *final* until you call `self.complete()`, which will return
1454    /// the merged type.
1455    pub(crate) fn merged_ty(&self) -> Ty<'tcx> {
1456        self.final_ty.unwrap_or(self.expected_ty)
1457    }
1458
1459    /// Indicates that the value generated by `expression`, which is
1460    /// of type `expression_ty`, is one of the possibilities that we
1461    /// could coerce from. This will record `expression`, and later
1462    /// calls to `coerce` may come back and add adjustments and things
1463    /// if necessary.
1464    pub(crate) fn coerce<'a>(
1465        &mut self,
1466        fcx: &FnCtxt<'a, 'tcx>,
1467        cause: &ObligationCause<'tcx>,
1468        expression: &'tcx hir::Expr<'tcx>,
1469        expression_ty: Ty<'tcx>,
1470    ) {
1471        self.coerce_inner(fcx, cause, Some(expression), expression_ty, |_| {}, false)
1472    }
1473
1474    /// Indicates that one of the inputs is a "forced unit". This
1475    /// occurs in a case like `if foo { ... };`, where the missing else
1476    /// generates a "forced unit". Another example is a `loop { break;
1477    /// }`, where the `break` has no argument expression. We treat
1478    /// these cases slightly differently for error-reporting
1479    /// purposes. Note that these tend to correspond to cases where
1480    /// the `()` expression is implicit in the source, and hence we do
1481    /// not take an expression argument.
1482    ///
1483    /// The `augment_error` gives you a chance to extend the error
1484    /// message, in case any results (e.g., we use this to suggest
1485    /// removing a `;`).
1486    pub(crate) fn coerce_forced_unit<'a>(
1487        &mut self,
1488        fcx: &FnCtxt<'a, 'tcx>,
1489        cause: &ObligationCause<'tcx>,
1490        augment_error: impl FnOnce(&mut Diag<'_>),
1491        label_unit_as_expected: bool,
1492    ) {
1493        self.coerce_inner(
1494            fcx,
1495            cause,
1496            None,
1497            fcx.tcx.types.unit,
1498            augment_error,
1499            label_unit_as_expected,
1500        )
1501    }
1502
1503    /// The inner coercion "engine". If `expression` is `None`, this
1504    /// is a forced-unit case, and hence `expression_ty` must be
1505    /// `Nil`.
1506    #[instrument(skip(self, fcx, augment_error, label_expression_as_expected), level = "debug")]
1507    pub(crate) fn coerce_inner<'a>(
1508        &mut self,
1509        fcx: &FnCtxt<'a, 'tcx>,
1510        cause: &ObligationCause<'tcx>,
1511        expression: Option<&'tcx hir::Expr<'tcx>>,
1512        mut expression_ty: Ty<'tcx>,
1513        augment_error: impl FnOnce(&mut Diag<'_>),
1514        label_expression_as_expected: bool,
1515    ) {
1516        // Incorporate whatever type inference information we have
1517        // until now; in principle we might also want to process
1518        // pending obligations, but doing so should only improve
1519        // compatibility (hopefully that is true) by helping us
1520        // uncover never types better.
1521        if expression_ty.is_ty_var() {
1522            expression_ty = fcx.infcx.shallow_resolve(expression_ty);
1523        }
1524
1525        // If we see any error types, just propagate that error
1526        // upwards.
1527        if let Err(guar) = (expression_ty, self.merged_ty()).error_reported() {
1528            self.final_ty = Some(Ty::new_error(fcx.tcx, guar));
1529            return;
1530        }
1531
1532        let (expected, found) = if label_expression_as_expected {
1533            // In the case where this is a "forced unit", like
1534            // `break`, we want to call the `()` "expected"
1535            // since it is implied by the syntax.
1536            // (Note: not all force-units work this way.)"
1537            (expression_ty, self.merged_ty())
1538        } else {
1539            // Otherwise, the "expected" type for error
1540            // reporting is the current unification type,
1541            // which is basically the LUB of the expressions
1542            // we've seen so far (combined with the expected
1543            // type)
1544            (self.merged_ty(), expression_ty)
1545        };
1546
1547        // Handle the actual type unification etc.
1548        let result = if let Some(expression) = expression {
1549            if self.pushed == 0 {
1550                // Special-case the first expression we are coercing.
1551                // To be honest, I'm not entirely sure why we do this.
1552                // We don't allow two-phase borrows, see comment in try_find_coercion_lub for why
1553                fcx.coerce(
1554                    expression,
1555                    expression_ty,
1556                    self.expected_ty,
1557                    AllowTwoPhase::No,
1558                    Some(cause.clone()),
1559                )
1560            } else {
1561                match self.expressions {
1562                    Expressions::Dynamic(ref exprs) => fcx.try_find_coercion_lub(
1563                        cause,
1564                        exprs,
1565                        self.merged_ty(),
1566                        expression,
1567                        expression_ty,
1568                    ),
1569                    Expressions::UpFront(coercion_sites) => fcx.try_find_coercion_lub(
1570                        cause,
1571                        &coercion_sites[0..self.pushed],
1572                        self.merged_ty(),
1573                        expression,
1574                        expression_ty,
1575                    ),
1576                }
1577            }
1578        } else {
1579            // this is a hack for cases where we default to `()` because
1580            // the expression etc has been omitted from the source. An
1581            // example is an `if let` without an else:
1582            //
1583            //     if let Some(x) = ... { }
1584            //
1585            // we wind up with a second match arm that is like `_ =>
1586            // ()`. That is the case we are considering here. We take
1587            // a different path to get the right "expected, found"
1588            // message and so forth (and because we know that
1589            // `expression_ty` will be unit).
1590            //
1591            // Another example is `break` with no argument expression.
1592            assert!(expression_ty.is_unit(), "if let hack without unit type");
1593            fcx.at(cause, fcx.param_env)
1594                .eq(
1595                    // needed for tests/ui/type-alias-impl-trait/issue-65679-inst-opaque-ty-from-val-twice.rs
1596                    DefineOpaqueTypes::Yes,
1597                    expected,
1598                    found,
1599                )
1600                .map(|infer_ok| {
1601                    fcx.register_infer_ok_obligations(infer_ok);
1602                    expression_ty
1603                })
1604        };
1605
1606        debug!(?result);
1607        match result {
1608            Ok(v) => {
1609                self.final_ty = Some(v);
1610                if let Some(e) = expression {
1611                    match self.expressions {
1612                        Expressions::Dynamic(ref mut buffer) => buffer.push(e),
1613                        Expressions::UpFront(coercion_sites) => {
1614                            // if the user gave us an array to validate, check that we got
1615                            // the next expression in the list, as expected
1616                            assert_eq!(
1617                                coercion_sites[self.pushed].as_coercion_site().hir_id,
1618                                e.hir_id
1619                            );
1620                        }
1621                    }
1622                    self.pushed += 1;
1623                }
1624            }
1625            Err(coercion_error) => {
1626                // Mark that we've failed to coerce the types here to suppress
1627                // any superfluous errors we might encounter while trying to
1628                // emit or provide suggestions on how to fix the initial error.
1629                fcx.set_tainted_by_errors(
1630                    fcx.dcx().span_delayed_bug(cause.span, "coercion error but no error emitted"),
1631                );
1632                let (expected, found) = fcx.resolve_vars_if_possible((expected, found));
1633
1634                let mut err;
1635                let mut unsized_return = false;
1636                match *cause.code() {
1637                    ObligationCauseCode::ReturnNoExpression => {
1638                        err = struct_span_code_err!(
1639                            fcx.dcx(),
1640                            cause.span,
1641                            E0069,
1642                            "`return;` in a function whose return type is not `()`"
1643                        );
1644                        if let Some(value) = fcx.err_ctxt().ty_kind_suggestion(fcx.param_env, found)
1645                        {
1646                            err.span_suggestion_verbose(
1647                                cause.span.shrink_to_hi(),
1648                                "give the `return` a value of the expected type",
1649                                format!(" {value}"),
1650                                Applicability::HasPlaceholders,
1651                            );
1652                        }
1653                        err.span_label(cause.span, "return type is not `()`");
1654                    }
1655                    ObligationCauseCode::BlockTailExpression(blk_id, ..) => {
1656                        err = self.report_return_mismatched_types(
1657                            cause,
1658                            expected,
1659                            found,
1660                            coercion_error,
1661                            fcx,
1662                            blk_id,
1663                            expression,
1664                        );
1665                        if !fcx.tcx.features().unsized_locals() {
1666                            unsized_return = self.is_return_ty_definitely_unsized(fcx);
1667                        }
1668                    }
1669                    ObligationCauseCode::ReturnValue(return_expr_id) => {
1670                        err = self.report_return_mismatched_types(
1671                            cause,
1672                            expected,
1673                            found,
1674                            coercion_error,
1675                            fcx,
1676                            return_expr_id,
1677                            expression,
1678                        );
1679                        if !fcx.tcx.features().unsized_locals() {
1680                            unsized_return = self.is_return_ty_definitely_unsized(fcx);
1681                        }
1682                    }
1683                    ObligationCauseCode::MatchExpressionArm(box MatchExpressionArmCause {
1684                        arm_span,
1685                        arm_ty,
1686                        prior_arm_ty,
1687                        ref prior_non_diverging_arms,
1688                        tail_defines_return_position_impl_trait: Some(rpit_def_id),
1689                        ..
1690                    }) => {
1691                        err = fcx.err_ctxt().report_mismatched_types(
1692                            cause,
1693                            fcx.param_env,
1694                            expected,
1695                            found,
1696                            coercion_error,
1697                        );
1698                        // Check that we're actually in the second or later arm
1699                        if prior_non_diverging_arms.len() > 0 {
1700                            self.suggest_boxing_tail_for_return_position_impl_trait(
1701                                fcx,
1702                                &mut err,
1703                                rpit_def_id,
1704                                arm_ty,
1705                                prior_arm_ty,
1706                                prior_non_diverging_arms
1707                                    .iter()
1708                                    .chain(std::iter::once(&arm_span))
1709                                    .copied(),
1710                            );
1711                        }
1712                    }
1713                    ObligationCauseCode::IfExpression(box IfExpressionCause {
1714                        then_id,
1715                        else_id,
1716                        then_ty,
1717                        else_ty,
1718                        tail_defines_return_position_impl_trait: Some(rpit_def_id),
1719                        ..
1720                    }) => {
1721                        err = fcx.err_ctxt().report_mismatched_types(
1722                            cause,
1723                            fcx.param_env,
1724                            expected,
1725                            found,
1726                            coercion_error,
1727                        );
1728                        let then_span = fcx.find_block_span_from_hir_id(then_id);
1729                        let else_span = fcx.find_block_span_from_hir_id(else_id);
1730                        // don't suggest wrapping either blocks in `if .. {} else {}`
1731                        let is_empty_arm = |id| {
1732                            let hir::Node::Block(blk) = fcx.tcx.hir_node(id) else {
1733                                return false;
1734                            };
1735                            if blk.expr.is_some() || !blk.stmts.is_empty() {
1736                                return false;
1737                            }
1738                            let Some((_, hir::Node::Expr(expr))) =
1739                                fcx.tcx.hir_parent_iter(id).nth(1)
1740                            else {
1741                                return false;
1742                            };
1743                            matches!(expr.kind, hir::ExprKind::If(..))
1744                        };
1745                        if !is_empty_arm(then_id) && !is_empty_arm(else_id) {
1746                            self.suggest_boxing_tail_for_return_position_impl_trait(
1747                                fcx,
1748                                &mut err,
1749                                rpit_def_id,
1750                                then_ty,
1751                                else_ty,
1752                                [then_span, else_span].into_iter(),
1753                            );
1754                        }
1755                    }
1756                    _ => {
1757                        err = fcx.err_ctxt().report_mismatched_types(
1758                            cause,
1759                            fcx.param_env,
1760                            expected,
1761                            found,
1762                            coercion_error,
1763                        );
1764                    }
1765                }
1766
1767                augment_error(&mut err);
1768
1769                if let Some(expr) = expression {
1770                    if let hir::ExprKind::Loop(
1771                        _,
1772                        _,
1773                        loop_src @ (hir::LoopSource::While | hir::LoopSource::ForLoop),
1774                        _,
1775                    ) = expr.kind
1776                    {
1777                        let loop_type = if loop_src == hir::LoopSource::While {
1778                            "`while` loops"
1779                        } else {
1780                            "`for` loops"
1781                        };
1782
1783                        err.note(format!("{loop_type} evaluate to unit type `()`"));
1784                    }
1785
1786                    fcx.emit_coerce_suggestions(
1787                        &mut err,
1788                        expr,
1789                        found,
1790                        expected,
1791                        None,
1792                        Some(coercion_error),
1793                    );
1794                }
1795
1796                let reported = err.emit_unless(unsized_return);
1797
1798                self.final_ty = Some(Ty::new_error(fcx.tcx, reported));
1799            }
1800        }
1801    }
1802
1803    fn suggest_boxing_tail_for_return_position_impl_trait(
1804        &self,
1805        fcx: &FnCtxt<'_, 'tcx>,
1806        err: &mut Diag<'_>,
1807        rpit_def_id: LocalDefId,
1808        a_ty: Ty<'tcx>,
1809        b_ty: Ty<'tcx>,
1810        arm_spans: impl Iterator<Item = Span>,
1811    ) {
1812        let compatible = |ty: Ty<'tcx>| {
1813            fcx.probe(|_| {
1814                let ocx = ObligationCtxt::new(fcx);
1815                ocx.register_obligations(
1816                    fcx.tcx.item_self_bounds(rpit_def_id).iter_identity().filter_map(|clause| {
1817                        let predicate = clause
1818                            .kind()
1819                            .map_bound(|clause| match clause {
1820                                ty::ClauseKind::Trait(trait_pred) => Some(ty::ClauseKind::Trait(
1821                                    trait_pred.with_self_ty(fcx.tcx, ty),
1822                                )),
1823                                ty::ClauseKind::Projection(proj_pred) => Some(
1824                                    ty::ClauseKind::Projection(proj_pred.with_self_ty(fcx.tcx, ty)),
1825                                ),
1826                                _ => None,
1827                            })
1828                            .transpose()?;
1829                        Some(Obligation::new(
1830                            fcx.tcx,
1831                            ObligationCause::dummy(),
1832                            fcx.param_env,
1833                            predicate,
1834                        ))
1835                    }),
1836                );
1837                ocx.select_where_possible().is_empty()
1838            })
1839        };
1840
1841        if !compatible(a_ty) || !compatible(b_ty) {
1842            return;
1843        }
1844
1845        let rpid_def_span = fcx.tcx.def_span(rpit_def_id);
1846        err.subdiagnostic(SuggestBoxingForReturnImplTrait::ChangeReturnType {
1847            start_sp: rpid_def_span.with_hi(rpid_def_span.lo() + BytePos(4)),
1848            end_sp: rpid_def_span.shrink_to_hi(),
1849        });
1850
1851        let (starts, ends) =
1852            arm_spans.map(|span| (span.shrink_to_lo(), span.shrink_to_hi())).unzip();
1853        err.subdiagnostic(SuggestBoxingForReturnImplTrait::BoxReturnExpr { starts, ends });
1854    }
1855
1856    fn report_return_mismatched_types<'infcx>(
1857        &self,
1858        cause: &ObligationCause<'tcx>,
1859        expected: Ty<'tcx>,
1860        found: Ty<'tcx>,
1861        ty_err: TypeError<'tcx>,
1862        fcx: &'infcx FnCtxt<'_, 'tcx>,
1863        block_or_return_id: hir::HirId,
1864        expression: Option<&'tcx hir::Expr<'tcx>>,
1865    ) -> Diag<'infcx> {
1866        let mut err =
1867            fcx.err_ctxt().report_mismatched_types(cause, fcx.param_env, expected, found, ty_err);
1868
1869        let due_to_block = matches!(fcx.tcx.hir_node(block_or_return_id), hir::Node::Block(..));
1870        let parent = fcx.tcx.parent_hir_node(block_or_return_id);
1871        if let Some(expr) = expression
1872            && let hir::Node::Expr(&hir::Expr {
1873                kind: hir::ExprKind::Closure(&hir::Closure { body, .. }),
1874                ..
1875            }) = parent
1876        {
1877            let needs_block =
1878                !matches!(fcx.tcx.hir_body(body).value.kind, hir::ExprKind::Block(..));
1879            fcx.suggest_missing_semicolon(&mut err, expr, expected, needs_block, true);
1880        }
1881        // Verify that this is a tail expression of a function, otherwise the
1882        // label pointing out the cause for the type coercion will be wrong
1883        // as prior return coercions would not be relevant (#57664).
1884        if let Some(expr) = expression
1885            && due_to_block
1886        {
1887            fcx.suggest_missing_semicolon(&mut err, expr, expected, false, false);
1888            let pointing_at_return_type = fcx.suggest_mismatched_types_on_tail(
1889                &mut err,
1890                expr,
1891                expected,
1892                found,
1893                block_or_return_id,
1894            );
1895            if let Some(cond_expr) = fcx.tcx.hir_get_if_cause(expr.hir_id)
1896                && expected.is_unit()
1897                && !pointing_at_return_type
1898                // If the block is from an external macro or try (`?`) desugaring, then
1899                // do not suggest adding a semicolon, because there's nowhere to put it.
1900                // See issues #81943 and #87051.
1901                && matches!(
1902                    cond_expr.span.desugaring_kind(),
1903                    None | Some(DesugaringKind::WhileLoop)
1904                )
1905                && !cond_expr.span.in_external_macro(fcx.tcx.sess.source_map())
1906                && !matches!(
1907                    cond_expr.kind,
1908                    hir::ExprKind::Match(.., hir::MatchSource::TryDesugar(_))
1909                )
1910            {
1911                err.span_label(cond_expr.span, "expected this to be `()`");
1912                if expr.can_have_side_effects() {
1913                    fcx.suggest_semicolon_at_end(cond_expr.span, &mut err);
1914                }
1915            }
1916        };
1917
1918        // If this is due to an explicit `return`, suggest adding a return type.
1919        if let Some((fn_id, fn_decl)) = fcx.get_fn_decl(block_or_return_id)
1920            && !due_to_block
1921        {
1922            fcx.suggest_missing_return_type(&mut err, fn_decl, expected, found, fn_id);
1923        }
1924
1925        // If this is due to a block, then maybe we forgot a `return`/`break`.
1926        if due_to_block
1927            && let Some(expr) = expression
1928            && let Some(parent_fn_decl) =
1929                fcx.tcx.hir_fn_decl_by_hir_id(fcx.tcx.local_def_id_to_hir_id(fcx.body_id))
1930        {
1931            fcx.suggest_missing_break_or_return_expr(
1932                &mut err,
1933                expr,
1934                parent_fn_decl,
1935                expected,
1936                found,
1937                block_or_return_id,
1938                fcx.body_id,
1939            );
1940        }
1941
1942        let ret_coercion_span = fcx.ret_coercion_span.get();
1943
1944        if let Some(sp) = ret_coercion_span
1945            // If the closure has an explicit return type annotation, or if
1946            // the closure's return type has been inferred from outside
1947            // requirements (such as an Fn* trait bound), then a type error
1948            // may occur at the first return expression we see in the closure
1949            // (if it conflicts with the declared return type). Skip adding a
1950            // note in this case, since it would be incorrect.
1951            && let Some(fn_sig) = fcx.body_fn_sig()
1952            && fn_sig.output().is_ty_var()
1953        {
1954            err.span_note(sp, format!("return type inferred to be `{expected}` here"));
1955        }
1956
1957        err
1958    }
1959
1960    /// Checks whether the return type is unsized via an obligation, which makes
1961    /// sure we consider `dyn Trait: Sized` where clauses, which are trivially
1962    /// false but technically valid for typeck.
1963    fn is_return_ty_definitely_unsized(&self, fcx: &FnCtxt<'_, 'tcx>) -> bool {
1964        if let Some(sig) = fcx.body_fn_sig() {
1965            !fcx.predicate_may_hold(&Obligation::new(
1966                fcx.tcx,
1967                ObligationCause::dummy(),
1968                fcx.param_env,
1969                ty::TraitRef::new(
1970                    fcx.tcx,
1971                    fcx.tcx.require_lang_item(hir::LangItem::Sized, DUMMY_SP),
1972                    [sig.output()],
1973                ),
1974            ))
1975        } else {
1976            false
1977        }
1978    }
1979
1980    pub(crate) fn complete<'a>(self, fcx: &FnCtxt<'a, 'tcx>) -> Ty<'tcx> {
1981        if let Some(final_ty) = self.final_ty {
1982            final_ty
1983        } else {
1984            // If we only had inputs that were of type `!` (or no
1985            // inputs at all), then the final type is `!`.
1986            assert_eq!(self.pushed, 0);
1987            fcx.tcx.types.never
1988        }
1989    }
1990}
1991
1992/// Something that can be converted into an expression to which we can
1993/// apply a coercion.
1994pub(crate) trait AsCoercionSite {
1995    fn as_coercion_site(&self) -> &hir::Expr<'_>;
1996}
1997
1998impl AsCoercionSite for hir::Expr<'_> {
1999    fn as_coercion_site(&self) -> &hir::Expr<'_> {
2000        self
2001    }
2002}
2003
2004impl<'a, T> AsCoercionSite for &'a T
2005where
2006    T: AsCoercionSite,
2007{
2008    fn as_coercion_site(&self) -> &hir::Expr<'_> {
2009        (**self).as_coercion_site()
2010    }
2011}
2012
2013impl AsCoercionSite for ! {
2014    fn as_coercion_site(&self) -> &hir::Expr<'_> {
2015        *self
2016    }
2017}
2018
2019impl AsCoercionSite for hir::Arm<'_> {
2020    fn as_coercion_site(&self) -> &hir::Expr<'_> {
2021        self.body
2022    }
2023}