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