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