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//! ```
3738use std::ops::{ControlFlow, Deref};
3940use rustc_attr_ir::InlineAttr;
41use rustc_attr_ir::lang_items::LangItem;
42use rustc_errors::codes::*;
43use rustc_errors::{Applicability, Diag, struct_span_code_err};
44use rustc_hiras hir;
45use rustc_hir::def_id::{DefId, LocalDefId};
46use rustc_hir_analysis::hir_ty_lowering::HirTyLowerer;
47use rustc_infer::infer::relate::RelateResult;
48use rustc_infer::infer::{DefineOpaqueTypes, InferOk, InferResult, RegionVariableOrigin};
49use rustc_infer::traits::{
50MatchExpressionArmCause, Obligation, PredicateObligation, PredicateObligations, SelectionError,
51};
52use rustc_middle::ty::adjustment::{
53Adjust, Adjustment, AllowTwoPhase, AutoBorrow, AutoBorrowMutability, DerefAdjustKind,
54PointerCoercion,
55};
56use rustc_middle::ty::error::TypeError;
57use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt, Unnormalized};
58use rustc_span::{BytePos, DUMMY_SP, Span, span_bug};
59use rustc_trait_selection::infer::InferCtxtExtas _;
60use rustc_trait_selection::solve::inspect::{self, InferCtxtProofTreeExt, ProofTreeVisitor};
61use rustc_trait_selection::solve::{Certainty, Goal, NoSolution};
62use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt;
63use rustc_trait_selection::traits::{
64self, ImplSource, NormalizeExt, ObligationCause, ObligationCauseCode, ObligationCtxt,
65};
66use smallvec::{SmallVec, smallvec};
67use tracing::{debug, instrument};
6869use crate::FnCtxt;
70use crate::diagnostics::SuggestBoxingForReturnImplTrait;
7172struct Coerce<'a, 'tcx> {
73 fcx: &'a FnCtxt<'a, 'tcx>,
74 cause: ObligationCause<'tcx>,
75 use_lub: bool,
76/// Determines whether or not allow_two_phase_borrow is set on any
77 /// autoref adjustments we create while coercing. We don't want to
78 /// allow deref coercions to create two-phase borrows, at least initially,
79 /// but we do need two-phase borrows for function argument reborrows.
80 /// See #47489 and #48598
81 /// See docs on the "AllowTwoPhase" type for a more detailed discussion
82allow_two_phase: AllowTwoPhase,
83/// Whether we allow `NeverToAny` coercions. This is unsound if we're
84 /// coercing a place expression without it counting as a read in the MIR.
85 /// This is a side-effect of HIR not really having a great distinction
86 /// between places and values.
87coerce_never: bool,
88}
8990impl<'a, 'tcx> Dereffor Coerce<'a, 'tcx> {
91type Target = FnCtxt<'a, 'tcx>;
92fn deref(&self) -> &Self::Target {
93self.fcx
94 }
95}
9697type CoerceResult<'tcx> = InferResult<'tcx, (Vec<Adjustment<'tcx>>, Ty<'tcx>)>;
9899/// Coercing a mutable reference to an immutable works, while
100/// coercing `&T` to `&mut T` should be forbidden.
101fn coerce_mutbls<'tcx>(
102 from_mutbl: hir::Mutability,
103 to_mutbl: hir::Mutability,
104) -> RelateResult<'tcx, ()> {
105if from_mutbl >= to_mutbl { Ok(()) } else { Err(TypeError::Mutability) }
106}
107108/// This always returns `Ok(...)`.
109fn success<'tcx>(
110 adj: Vec<Adjustment<'tcx>>,
111 target: Ty<'tcx>,
112 obligations: PredicateObligations<'tcx>,
113) -> CoerceResult<'tcx> {
114Ok(InferOk { value: (adj, target), obligations })
115}
116117/// Data extracted from a reference (pinned or not) for coercion to a reference (pinned or not).
118struct CoerceMaybePinnedRef<'tcx> {
119/// coercion source, must be a pinned (i.e. `Pin<&T>` or `Pin<&mut T>`) or normal reference (`&T` or `&mut T`)
120a: Ty<'tcx>,
121/// coercion target, must be a pinned (i.e. `Pin<&T>` or `Pin<&mut T>`) or normal reference (`&T` or `&mut T`)
122b: Ty<'tcx>,
123/// referent type of the source
124a_ty: Ty<'tcx>,
125/// pinnedness of the source
126a_pin: ty::Pinnedness,
127/// mutability of the source
128a_mut: ty::Mutability,
129/// region of the source
130a_r: ty::Region<'tcx>,
131/// pinnedness of the target
132b_pin: ty::Pinnedness,
133/// mutability of the target
134b_mut: ty::Mutability,
135}
136137/// Whether to force a leak check to occur in `Coerce::unify_raw`.
138/// Note that leak checks may still occur evn with `ForceLeakCheck::No`.
139///
140/// FIXME: We may want to change type relations to always leak-check
141/// after exiting a binder, at which point we will always do so and
142/// no longer need to handle this explicitly
143enum ForceLeakCheck {
144 Yes,
145 No,
146}
147148impl<'f, 'tcx> Coerce<'f, 'tcx> {
149fn new(
150 fcx: &'f FnCtxt<'f, 'tcx>,
151 cause: ObligationCause<'tcx>,
152 allow_two_phase: AllowTwoPhase,
153 coerce_never: bool,
154 ) -> Self {
155Coerce { fcx, cause, allow_two_phase, use_lub: false, coerce_never }
156 }
157158fn unify_raw(
159&self,
160 a: Ty<'tcx>,
161 b: Ty<'tcx>,
162 leak_check: ForceLeakCheck,
163 ) -> InferResult<'tcx, Ty<'tcx>> {
164{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:164",
"rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
::tracing_core::__macro_support::Option::Some(164u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("unify(a: {0:?}, b: {1:?}, use_lub: {2})",
a, b, self.use_lub) as &dyn ::tracing::field::Value))])
});
} else { ; }
};debug!("unify(a: {:?}, b: {:?}, use_lub: {})", a, b, self.use_lub);
165self.commit_if_ok(|snapshot| {
166let outer_universe = self.infcx.universe();
167168let at = self.at(&self.cause, self.fcx.param_env);
169170let res = if self.use_lub {
171at.lub(b, a)
172 } else {
173self.sup_raw(&self.cause, self.param_env, DefineOpaqueTypes::Yes, b, a)
174 .map(|InferOk { value: (), obligations }| InferOk { value: b, obligations })
175 };
176177// In the new solver, lazy norm may allow us to shallowly equate
178 // more types, but we emit possibly impossible-to-satisfy obligations.
179 // Filter these cases out to make sure our coercion is more accurate.
180let res = match res {
181Ok(InferOk { value, obligations }) if self.next_trait_solver() => {
182let ocx = ObligationCtxt::new(self);
183ocx.register_obligations(obligations);
184if ocx.try_evaluate_obligations().no_errors() {
185Ok(InferOk { value, obligations: ocx.into_pending_obligations() })
186 } else {
187Err(TypeError::Mismatch)
188 }
189 }
190 res => res,
191 };
192193// We leak check here mostly because lub operations are
194 // kind of scuffed around binders. Instead of computing an actual
195 // lub'd binder we instead:
196 // - Equate the binders
197 // - Return the lhs of the lub operation
198 //
199 // This may lead to incomplete type inference for the resulting type
200 // of a `match` or `if .. else`, etc. This is a backwards compat
201 // hazard for if/when we start handling `lub` more correctly.
202 //
203 // In order to actually ensure that equating the binders *does*
204 // result in equal binders, and that the lhs is actually a supertype
205 // of the rhs, we must perform a leak check here.
206if #[allow(non_exhaustive_omitted_patterns)] match leak_check {
ForceLeakCheck::Yes => true,
_ => false,
}matches!(leak_check, ForceLeakCheck::Yes) {
207self.leak_check(outer_universe, Some(snapshot))?;
208 }
209210res211 })
212 }
213214/// Unify two types (using sub or lub).
215fn unify(&self, a: Ty<'tcx>, b: Ty<'tcx>, leak_check: ForceLeakCheck) -> CoerceResult<'tcx> {
216self.unify_raw(a, b, leak_check)
217 .and_then(|InferOk { value: ty, obligations }| success(::alloc::vec::Vec::new()vec![], ty, obligations))
218 }
219220/// Unify two types (using sub or lub) and produce a specific coercion.
221fn unify_and(
222&self,
223 a: Ty<'tcx>,
224 b: Ty<'tcx>,
225 adjustments: impl IntoIterator<Item = Adjustment<'tcx>>,
226 final_adjustment: Adjust,
227 leak_check: ForceLeakCheck,
228 ) -> CoerceResult<'tcx> {
229self.unify_raw(a, b, leak_check).and_then(|InferOk { value: ty, obligations }| {
230success(
231adjustments232 .into_iter()
233 .chain(std::iter::once(Adjustment { target: ty, kind: final_adjustment }))
234 .collect(),
235ty,
236obligations,
237 )
238 })
239 }
240241{}
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::INFO <= ::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::INFO <=
::tracing::level_filters::LevelFilter::current() || { false }
{
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("coerce",
"rustc_hir_typeck::coercion", ::tracing::Level::INFO,
::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
::tracing_core::__macro_support::Option::Some(241u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
::tracing_core::field::FieldSet::new(&[{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("a")
}> =
::tracing::__macro_support::FieldName::new("a");
NAME.as_str()
},
{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("b")
}> =
::tracing::__macro_support::FieldName::new("b");
NAME.as_str()
}], ::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::INFO <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::INFO <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
meta.fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&a)
as &dyn ::tracing::field::Value)),
(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&b)
as &dyn ::tracing::field::Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[allow(clippy :: redundant_closure_call)]
let x =
(move ||
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy
:: needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: CoerceResult<'tcx> =
loop {};
return __tracing_attr_fake_return;
}
{
let a = self.shallow_resolve(a);
let b = self.shallow_resolve(b);
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:246",
"rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
::tracing_core::__macro_support::Option::Some(246u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("Coerce.tys({0:?} => {1:?})",
a, b) as &dyn ::tracing::field::Value))])
});
} else { ; }
};
if a.is_never() {
if self.coerce_never {
return success(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[Adjustment { kind: Adjust::NeverToAny, target: b }])), b,
PredicateObligations::new());
} else { return self.unify(a, b, ForceLeakCheck::No); }
}
if a.is_ty_var() {
return self.coerce_from_inference_variable(a, b);
}
let unsize = self.coerce_unsized(a, b);
match unsize {
Ok(_) => {
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:273",
"rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
::tracing_core::__macro_support::Option::Some(273u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("coerce: unsize successful")
as &dyn ::tracing::field::Value))])
});
} else { ; }
};
return unsize;
}
Err(error) => {
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:277",
"rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
::tracing_core::__macro_support::Option::Some(277u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
::tracing_core::field::FieldSet::new(&["message",
{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("error")
}> =
::tracing::__macro_support::FieldName::new("error");
NAME.as_str()
}], ::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("coerce: unsize failed")
as &dyn ::tracing::field::Value)),
(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&error)
as &dyn ::tracing::field::Value))])
});
} else { ; }
};
}
}
match *b.kind() {
ty::RawPtr(_, b_mutbl) => {
return self.coerce_to_raw_ptr(a, b, b_mutbl);
}
ty::Ref(r_b, _, mutbl_b) => {
if let Some(pin_ref_to_ref) =
self.maybe_pin_ref_to_ref(a, b) {
return self.coerce_pin_ref_to_ref(pin_ref_to_ref);
}
return self.coerce_to_ref(a, b, r_b, mutbl_b);
}
_ if let Some(to_pin_ref) = self.maybe_to_pin_ref(a, b) => {
return self.coerce_to_pin_ref(to_pin_ref);
}
ty::Adt(_, _) if
self.tcx.features().reborrow() &&
self.fcx.infcx.type_implements_trait(self.tcx.lang_items().reborrow().expect("Unexpectedly using core/std without reborrow"),
[b], self.fcx.param_env).must_apply_modulo_regions() => {
let reborrow_coerce =
self.commit_if_ok(|_| self.coerce_reborrow(a, b));
if reborrow_coerce.is_ok() { return reborrow_coerce; }
}
ty::Int(_) | ty::Uint(_) | ty::Float(_) |
ty::Infer(ty::IntVar(_) | ty::FloatVar(_)) => {}
_ => {}
}
match *a.kind() {
ty::FnDef(..) => { self.coerce_from_fn_item(a, b) }
ty::FnPtr(a_sig_tys, a_hdr) => {
self.coerce_from_fn_pointer(a, a_sig_tys.with(a_hdr), b)
}
ty::Closure(..) => { self.coerce_closure_to_fn(a, b) }
ty::Adt(_, _) if self.tcx.features().reborrow() => {
let reborrow_coerce =
self.commit_if_ok(|_| self.coerce_shared_reborrow(a, b));
if reborrow_coerce.is_ok() {
reborrow_coerce
} else { self.unify(a, b, ForceLeakCheck::No) }
}
ty::Int(_) | ty::Uint(_) | ty::Float(_) |
ty::Infer(ty::IntVar(_) | ty::FloatVar(_)) => {
self.unify(a, b, ForceLeakCheck::No)
}
_ => { self.unify(a, b, ForceLeakCheck::No) }
}
}
})();
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:241",
"rustc_hir_typeck::coercion", ::tracing::Level::INFO,
::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
::tracing_core::__macro_support::Option::Some(241u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
::tracing_core::field::FieldSet::new(&[{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("return")
}> =
::tracing::__macro_support::FieldName::new("return");
NAME.as_str()
}], ::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::INFO <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::INFO <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&x)
as &dyn ::tracing::field::Value))])
});
} else { ; }
};
x;#[instrument(skip(self), ret)]242fn coerce(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> CoerceResult<'tcx> {
243// First, remove any resolved type variables (at the top level, at least):
244let a = self.shallow_resolve(a);
245let b = self.shallow_resolve(b);
246debug!("Coerce.tys({:?} => {:?})", a, b);
247248// Coercing from `!` to any type is allowed:
249if a.is_never() {
250if self.coerce_never {
251return success(
252vec![Adjustment { kind: Adjust::NeverToAny, target: b }],
253 b,
254 PredicateObligations::new(),
255 );
256 } else {
257// Otherwise the only coercion we can do is unification.
258return self.unify(a, b, ForceLeakCheck::No);
259 }
260 }
261262// Coercing *from* an unresolved inference variable means that
263 // we have no information about the source type. This will always
264 // ultimately fall back to some form of subtyping.
265if a.is_ty_var() {
266return self.coerce_from_inference_variable(a, b);
267 }
268269// Consider coercing the subtype to a DST
270let unsize = self.coerce_unsized(a, b);
271match unsize {
272Ok(_) => {
273debug!("coerce: unsize successful");
274return unsize;
275 }
276Err(error) => {
277debug!(?error, "coerce: unsize failed");
278 }
279 }
280281// Examine the target type and consider type-specific coercions, such
282 // as auto-borrowing, coercing pointer mutability, pin-ergonomics, or
283 // generic reborrow.
284match *b.kind() {
285 ty::RawPtr(_, b_mutbl) => {
286return self.coerce_to_raw_ptr(a, b, b_mutbl);
287 }
288 ty::Ref(r_b, _, mutbl_b) => {
289if let Some(pin_ref_to_ref) = self.maybe_pin_ref_to_ref(a, b) {
290return self.coerce_pin_ref_to_ref(pin_ref_to_ref);
291 }
292return self.coerce_to_ref(a, b, r_b, mutbl_b);
293 }
294_ if let Some(to_pin_ref) = self.maybe_to_pin_ref(a, b) => {
295return self.coerce_to_pin_ref(to_pin_ref);
296 }
297 ty::Adt(_, _)
298if self.tcx.features().reborrow()
299 && self
300.fcx
301 .infcx
302 .type_implements_trait(
303self.tcx
304 .lang_items()
305 .reborrow()
306 .expect("Unexpectedly using core/std without reborrow"),
307 [b],
308self.fcx.param_env,
309 )
310 .must_apply_modulo_regions() =>
311 {
312let reborrow_coerce = self.commit_if_ok(|_| self.coerce_reborrow(a, b));
313if reborrow_coerce.is_ok() {
314return reborrow_coerce;
315 }
316 }
317 ty::Int(_)
318 | ty::Uint(_)
319 | ty::Float(_)
320 | ty::Infer(ty::IntVar(_) | ty::FloatVar(_)) => {
321// For now, do nothing. If coercions are ever added for ints/floats then the arms
322 // handling these types in `check_expr_lit` will need re-evaluation.
323}
324_ => {}
325 }
326327match *a.kind() {
328 ty::FnDef(..) => {
329// Function items are coercible to any closure
330 // type; function pointers are not (that would
331 // require double indirection).
332 // Additionally, we permit coercion of function
333 // items to drop the unsafe qualifier.
334self.coerce_from_fn_item(a, b)
335 }
336 ty::FnPtr(a_sig_tys, a_hdr) => {
337// We permit coercion of fn pointers to drop the
338 // unsafe qualifier.
339self.coerce_from_fn_pointer(a, a_sig_tys.with(a_hdr), b)
340 }
341 ty::Closure(..) => {
342// Non-capturing closures are coercible to
343 // function pointers or unsafe function pointers.
344 // It cannot convert closures that require unsafe.
345self.coerce_closure_to_fn(a, b)
346 }
347 ty::Adt(_, _) if self.tcx.features().reborrow() => {
348let reborrow_coerce = self.commit_if_ok(|_| self.coerce_shared_reborrow(a, b));
349if reborrow_coerce.is_ok() {
350 reborrow_coerce
351 } else {
352self.unify(a, b, ForceLeakCheck::No)
353 }
354 }
355 ty::Int(_)
356 | ty::Uint(_)
357 | ty::Float(_)
358 | ty::Infer(ty::IntVar(_) | ty::FloatVar(_)) => {
359// For now, just use unification rules. If coercions are ever added for ints/floats
360 // then the arms handling these types in `check_expr_lit` will need re-evaluation.
361self.unify(a, b, ForceLeakCheck::No)
362 }
363_ => {
364// Otherwise, just use unification rules.
365self.unify(a, b, ForceLeakCheck::No)
366 }
367 }
368 }
369370/// Coercing *from* an inference variable. In this case, we have no information
371 /// about the source type, so we can't really do a true coercion and we always
372 /// fall back to subtyping (`unify_and`).
373fn coerce_from_inference_variable(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> CoerceResult<'tcx> {
374{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:374",
"rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
::tracing_core::__macro_support::Option::Some(374u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("coerce_from_inference_variable(a={0:?}, b={1:?})",
a, b) as &dyn ::tracing::field::Value))])
});
} else { ; }
};debug!("coerce_from_inference_variable(a={:?}, b={:?})", a, b);
375if true {
if !(a.is_ty_var() && self.shallow_resolve(a) == a) {
::core::panicking::panic("assertion failed: a.is_ty_var() && self.shallow_resolve(a) == a")
};
};debug_assert!(a.is_ty_var() && self.shallow_resolve(a) == a);
376if true {
if !(self.shallow_resolve(b) == b) {
::core::panicking::panic("assertion failed: self.shallow_resolve(b) == b")
};
};debug_assert!(self.shallow_resolve(b) == b);
377378if b.is_ty_var() {
379let mut obligations = PredicateObligations::with_capacity(2);
380let mut push_coerce_obligation = |a, b| {
381obligations.push(Obligation::new(
382self.tcx(),
383self.cause.clone(),
384self.param_env,
385 ty::Binder::dummy(ty::PredicateKind::Coerce(ty::CoercePredicate { a, b })),
386 ));
387 };
388389let target_ty = if self.use_lub {
390// When computing the lub, we create a new target
391 // and coerce both `a` and `b` to it.
392let target_ty = self.next_ty_var(self.cause.span);
393push_coerce_obligation(a, target_ty);
394push_coerce_obligation(b, target_ty);
395target_ty396 } else {
397// When subtyping, we don't need to create a new target
398 // as we only coerce `a` to `b`.
399push_coerce_obligation(a, b);
400b401 };
402403{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:403",
"rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
::tracing_core::__macro_support::Option::Some(403u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("coerce_from_inference_variable: two inference variables, target_ty={0:?}, obligations={1:?}",
target_ty, obligations) as &dyn ::tracing::field::Value))])
});
} else { ; }
};debug!(
404"coerce_from_inference_variable: two inference variables, target_ty={:?}, obligations={:?}",
405 target_ty, obligations
406 );
407success(::alloc::vec::Vec::new()vec![], target_ty, obligations)
408 } else {
409// One unresolved type variable: just apply subtyping, we may be able
410 // to do something useful.
411self.unify(a, b, ForceLeakCheck::No)
412 }
413 }
414415/// Handles coercing some arbitrary type `a` to some reference (`b`). This
416 /// handles a few cases:
417 /// - Introducing reborrows to give more flexible lifetimes
418 /// - Deref coercions to allow `&T` to coerce to `&T::Target`
419 /// - Coercing mutable references to immutable references
420 /// These coercions can be freely intermixed, for example we are able to
421 /// coerce `&mut T` to `&mut T::Target`.
422fn coerce_to_ref(
423&self,
424 a: Ty<'tcx>,
425 b: Ty<'tcx>,
426 r_b: ty::Region<'tcx>,
427 mutbl_b: hir::Mutability,
428 ) -> CoerceResult<'tcx> {
429{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:429",
"rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
::tracing_core::__macro_support::Option::Some(429u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("coerce_to_ref(a={0:?}, b={1:?})",
a, b) as &dyn ::tracing::field::Value))])
});
} else { ; }
};debug!("coerce_to_ref(a={:?}, b={:?})", a, b);
430if true {
if !(self.shallow_resolve(a) == a) {
::core::panicking::panic("assertion failed: self.shallow_resolve(a) == a")
};
};debug_assert!(self.shallow_resolve(a) == a);
431if true {
if !(self.shallow_resolve(b) == b) {
::core::panicking::panic("assertion failed: self.shallow_resolve(b) == b")
};
};debug_assert!(self.shallow_resolve(b) == b);
432433let (r_a, mt_a) = match *a.kind() {
434 ty::Ref(r_a, ty, mutbl) => {
435 coerce_mutbls(mutbl, mutbl_b)?;
436 (r_a, ty::TypeAndMut { ty, mutbl })
437 }
438_ => return self.unify(a, b, ForceLeakCheck::No),
439 };
440441// Look at each step in the `Deref` chain and check if
442 // any of the autoref'd `Target` types unify with the
443 // coercion target.
444 //
445 // For example when coercing from `&mut Vec<T>` to `&M [T]` we
446 // have three deref steps:
447 // 1. `&mut Vec<T>`, skip autoref
448 // 2. `Vec<T>`, autoref'd ty: `&M Vec<T>`
449 // - `&M Vec<T>` does not unify with `&M [T]`
450 // 3. `[T]`, autoref'd ty: `&M [T]`
451 // - `&M [T]` does unify with `&M [T]`
452let mut first_error = None;
453let mut r_borrow_var = None;
454let mut autoderef = self.autoderef(self.cause.span, a);
455let found = autoderef.by_ref().find_map(|(deref_ty, autoderefs)| {
456if autoderefs == 0 {
457// Don't autoref the first step as otherwise we'd allow
458 // coercing `&T` to `&&T`.
459return None;
460 }
461462// The logic here really shouldn't exist. We don't care about free
463 // lifetimes during HIR typeck. Unfortunately later parts of this
464 // function rely on structural identity of the autoref'd deref'd ty.
465 //
466 // This means that what region we use here actually impacts whether
467 // we emit a reborrow coercion or not which can affect diagnostics
468 // and capture analysis (which in turn affects borrowck).
469let r = if !self.use_lub {
470r_b471 } else if autoderefs == 1 {
472r_a473 } else {
474if r_borrow_var.is_none() {
475// create var lazily, at most once
476let coercion = RegionVariableOrigin::Coercion(self.cause.span);
477let r = self.next_region_var(coercion);
478r_borrow_var = Some(r);
479 }
480r_borrow_var.unwrap()
481 };
482483let autorefd_deref_ty = Ty::new_ref(self.tcx, r, deref_ty, mutbl_b);
484485// Note that we unify the autoref'd `Target` type with `b` rather than
486 // the `Target` type with the pointee of `b`. This is necessary
487 // to properly account for the differing variances of the pointees
488 // of `&` vs `&mut` references.
489match self.unify_raw(autorefd_deref_ty, b, ForceLeakCheck::No) {
490Ok(ok) => Some(ok),
491Err(err) => {
492if first_error.is_none() {
493first_error = Some(err);
494 }
495None496 }
497 }
498 });
499500// Extract type or return an error. We return the first error
501 // we got, which should be from relating the "base" type
502 // (e.g., in example above, the failure from relating `Vec<T>`
503 // to the target type), since that should be the least
504 // confusing.
505let Some(InferOk { value: coerced_a, mut obligations }) = foundelse {
506if let Some(first_error) = first_error {
507{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:507",
"rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
::tracing_core::__macro_support::Option::Some(507u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("coerce_to_ref: failed with err = {0:?}",
first_error) as &dyn ::tracing::field::Value))])
});
} else { ; }
};debug!("coerce_to_ref: failed with err = {:?}", first_error);
508return Err(first_error);
509 } else {
510// This may happen in the new trait solver since autoderef requires
511 // the pointee to be structurally normalizable, or else it'll just bail.
512 // So when we have a type like `&<not well formed>`, then we get no
513 // autoderef steps (even though there should be at least one). That means
514 // we get no type mismatches, since the loop above just exits early.
515return Err(TypeError::Mismatch);
516 }
517 };
518519if coerced_a == a && mt_a.mutbl.is_not() && autoderef.step_count() == 1 {
520// As a special case, if we would produce `&'a *x`, that's
521 // a total no-op. We end up with the type `&'a T` just as
522 // we started with. In that case, just skip it altogether.
523 //
524 // Unfortunately, this can actually effect capture analysis
525 // which in turn means this effects borrow checking. This can
526 // also effect diagnostics.
527 // FIXME(BoxyUwU): we should always emit reborrow coercions
528 //
529 // Note that for `&mut`, we DO want to reborrow --
530 // otherwise, this would be a move, which might be an
531 // error. For example `foo(self.x)` where `self` and
532 // `self.x` both have `&mut `type would be a move of
533 // `self.x`, but we auto-coerce it to `foo(&mut *self.x)`,
534 // which is a borrow.
535if !mutbl_b.is_not() {
::core::panicking::panic("assertion failed: mutbl_b.is_not()")
};assert!(mutbl_b.is_not()); // can only coerce &T -> &U
536return success(::alloc::vec::Vec::new()vec![], coerced_a, obligations);
537 }
538539let InferOk { value: mut adjustments, obligations: o } =
540self.adjust_steps_as_infer_ok(&autoderef);
541obligations.extend(o);
542obligations.extend(autoderef.into_obligations());
543544if !#[allow(non_exhaustive_omitted_patterns)] match coerced_a.kind() {
ty::Ref(..) => true,
_ => false,
} {
{
::core::panicking::panic_fmt(format_args!("expected a ref type, got {0:?}",
coerced_a));
}
};assert!(
545matches!(coerced_a.kind(), ty::Ref(..)),
546"expected a ref type, got {:?}",
547 coerced_a
548 );
549550// Now apply the autoref
551let mutbl = AutoBorrowMutability::new(mutbl_b, self.allow_two_phase);
552adjustments553 .push(Adjustment { kind: Adjust::Borrow(AutoBorrow::Ref(mutbl)), target: coerced_a });
554555{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:555",
"rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
::tracing_core::__macro_support::Option::Some(555u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("coerce_to_ref: succeeded coerced_a={0:?} adjustments={1:?}",
coerced_a, adjustments) as &dyn ::tracing::field::Value))])
});
} else { ; }
};debug!("coerce_to_ref: succeeded coerced_a={:?} adjustments={:?}", coerced_a, adjustments);
556557success(adjustments, coerced_a, obligations)
558 }
559560/// Performs [unsized coercion] by emulating a fulfillment loop on a
561 /// `CoerceUnsized` goal until all `CoerceUnsized` and `Unsize` goals
562 /// are successfully selected.
563 ///
564 /// [unsized coercion](https://doc.rust-lang.org/reference/type-coercions.html#unsized-coercions)
565{}
#[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("coerce_unsized",
"rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
::tracing_core::__macro_support::Option::Some(565u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
::tracing_core::field::FieldSet::new(&[{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("source")
}> =
::tracing::__macro_support::FieldName::new("source");
NAME.as_str()
},
{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("target")
}> =
::tracing::__macro_support::FieldName::new("target");
NAME.as_str()
}], ::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
meta.fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&source)
as &dyn ::tracing::field::Value)),
(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&target)
as &dyn ::tracing::field::Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: CoerceResult<'tcx> = loop {};
return __tracing_attr_fake_return;
}
{
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:567",
"rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
::tracing_core::__macro_support::Option::Some(567u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
::tracing_core::field::FieldSet::new(&[{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("source")
}> =
::tracing::__macro_support::FieldName::new("source");
NAME.as_str()
},
{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("target")
}> =
::tracing::__macro_support::FieldName::new("target");
NAME.as_str()
}], ::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&source)
as &dyn ::tracing::field::Value)),
(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&target)
as &dyn ::tracing::field::Value))])
});
} else { ; }
};
if true {
if !(self.shallow_resolve(source) == source) {
::core::panicking::panic("assertion failed: self.shallow_resolve(source) == source")
};
};
if true {
if !(self.shallow_resolve(target) == target) {
::core::panicking::panic("assertion failed: self.shallow_resolve(target) == target")
};
};
if source.is_ty_var() {
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:575",
"rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
::tracing_core::__macro_support::Option::Some(575u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("coerce_unsized: source is a TyVar, bailing out")
as &dyn ::tracing::field::Value))])
});
} else { ; }
};
return Err(TypeError::Mismatch);
}
if target.is_ty_var() {
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:579",
"rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
::tracing_core::__macro_support::Option::Some(579u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("coerce_unsized: target is a TyVar, bailing out")
as &dyn ::tracing::field::Value))])
});
} else { ; }
};
return Err(TypeError::Mismatch);
}
match target.kind() {
ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::Float(_)
| ty::Infer(ty::IntVar(_) | ty::FloatVar(_)) | ty::Str |
ty::Array(_, _) | ty::Slice(_) | ty::FnDef(_, _) |
ty::FnPtr(_, _) | ty::Dynamic(_, _) | ty::Closure(_, _) |
ty::CoroutineClosure(_, _) | ty::Coroutine(_, _) |
ty::CoroutineWitness(_, _) | ty::Never | ty::Tuple(_) =>
return Err(TypeError::Mismatch),
ty::Adt(adt, args) =>
match adt.adt_kind() {
ty::AdtKind::Enum | ty::AdtKind::Union =>
return Err(TypeError::Mismatch),
ty::AdtKind::Struct if args.is_empty() =>
return Err(TypeError::Mismatch),
_ => {}
},
_ => {}
}
if let ty::Ref(_, source_pointee, ty::Mutability::Not) =
*source.kind() && source_pointee.is_str() &&
let ty::Ref(_, target_pointee, ty::Mutability::Not) =
*target.kind() && target_pointee.is_str() {
return Err(TypeError::Mismatch);
}
let traits =
(self.tcx.lang_items().unsize_trait(),
self.tcx.lang_items().coerce_unsized_trait());
let (Some(unsize_did), Some(coerce_unsized_did)) =
traits else {
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:645",
"rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
::tracing_core::__macro_support::Option::Some(645u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("missing Unsize or CoerceUnsized traits")
as &dyn ::tracing::field::Value))])
});
} else { ; }
};
return Err(TypeError::Mismatch);
};
self.commit_if_ok(|_|
{
let reborrow =
match (source.kind(), target.kind()) {
(&ty::Ref(_, ty_a, mutbl_a), &ty::Ref(_, _, mutbl_b)) => {
coerce_mutbls(mutbl_a, mutbl_b)?;
let coercion =
RegionVariableOrigin::Coercion(self.cause.span);
let r_borrow = self.next_region_var(coercion);
let mutbl =
AutoBorrowMutability::new(mutbl_b, AllowTwoPhase::No);
Some((Adjustment {
kind: Adjust::Deref(DerefAdjustKind::Builtin),
target: ty_a,
},
Adjustment {
kind: Adjust::Borrow(AutoBorrow::Ref(mutbl)),
target: Ty::new_ref(self.tcx, r_borrow, ty_a, mutbl_b),
}))
}
(&ty::Ref(_, ty_a, mt_a), &ty::RawPtr(_, mt_b)) => {
coerce_mutbls(mt_a, mt_b)?;
Some((Adjustment {
kind: Adjust::Deref(DerefAdjustKind::Builtin),
target: ty_a,
},
Adjustment {
kind: Adjust::Borrow(AutoBorrow::RawPtr(mt_b)),
target: Ty::new_ptr(self.tcx, ty_a, mt_b),
}))
}
_ => None,
};
let coerce_source =
reborrow.as_ref().map_or(source, |(_, r)| r.target);
let coerce_target = self.next_ty_var(self.cause.span);
let mut coercion =
self.unify_and(coerce_target, target,
reborrow.map(|(deref, autoref)|
[deref, autoref]).into_flat_iter(),
Adjust::Pointer(PointerCoercion::Unsize),
ForceLeakCheck::No)?;
let cause =
self.cause(self.cause.span,
ObligationCauseCode::Coercion { source, target });
let pred =
ty::TraitRef::new(self.tcx, coerce_unsized_did,
[coerce_source, coerce_target]);
let obligation =
Obligation::new(self.tcx, cause, self.fcx.param_env, pred);
if self.next_trait_solver() {
coercion.obligations.push(obligation);
if self.infcx.visit_proof_tree(Goal::new(self.tcx,
self.param_env, pred),
&mut CoerceVisitor {
fcx: self.fcx,
span: self.cause.span,
errored: false,
}).is_break() {
return Err(TypeError::Mismatch);
}
} else {
self.coerce_unsized_old_solver(obligation, &mut coercion,
coerce_unsized_did, unsize_did)?;
}
Ok(coercion)
})
}
}
}#[instrument(skip(self), level = "debug")]566fn coerce_unsized(&self, source: Ty<'tcx>, target: Ty<'tcx>) -> CoerceResult<'tcx> {
567debug!(?source, ?target);
568debug_assert!(self.shallow_resolve(source) == source);
569debug_assert!(self.shallow_resolve(target) == target);
570571// We don't apply any coercions incase either the source or target
572 // aren't sufficiently well known but tend to instead just equate
573 // them both.
574if source.is_ty_var() {
575debug!("coerce_unsized: source is a TyVar, bailing out");
576return Err(TypeError::Mismatch);
577 }
578if target.is_ty_var() {
579debug!("coerce_unsized: target is a TyVar, bailing out");
580return Err(TypeError::Mismatch);
581 }
582583// This is an optimization because coercion is one of the most common
584 // operations that we do in typeck, since it happens at every assignment
585 // and call arg (among other positions).
586 //
587 // These targets are known to never be RHS in `LHS: CoerceUnsized<RHS>`.
588 // That's because one of the following is true.
589 // - For the built-in types, a core-provided impl doesn't exist and a
590 // user-written impl is invalid.
591 // - For enums and unions, they can never be the target of a
592 // `CoerceUnsized` impl.
593 // - For structs, they can only be the target of a `CoerceUnsized` impl
594 // if `LHS` and `RHS` are instances of the same struct type but with
595 // at least one differing field type. This is impossible when we
596 // exclude structs with any generic args.
597 //
598 // This is technically incomplete when users write impossible bounds like
599 // `where T: CoerceUnsized<usize>`, for example, but that trait is unstable
600 // and coercion is allowed to be incomplete. The only case where this matters
601 // is impossible bounds.
602 //
603 // Note that some of these types implement `LHS: Unsize<RHS>`, but they
604 // do not implement *`CoerceUnsized`* which is the root obligation of the
605 // check below.
606match target.kind() {
607 ty::Bool
608 | ty::Char
609 | ty::Int(_)
610 | ty::Uint(_)
611 | ty::Float(_)
612 | ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
613 | ty::Str
614 | ty::Array(_, _)
615 | ty::Slice(_)
616 | ty::FnDef(_, _)
617 | ty::FnPtr(_, _)
618 | ty::Dynamic(_, _)
619 | ty::Closure(_, _)
620 | ty::CoroutineClosure(_, _)
621 | ty::Coroutine(_, _)
622 | ty::CoroutineWitness(_, _)
623 | ty::Never
624 | ty::Tuple(_) => return Err(TypeError::Mismatch),
625 ty::Adt(adt, args) => match adt.adt_kind() {
626 ty::AdtKind::Enum | ty::AdtKind::Union => return Err(TypeError::Mismatch),
627 ty::AdtKind::Struct if args.is_empty() => return Err(TypeError::Mismatch),
628_ => {}
629 },
630_ => {}
631 }
632// `&str: CoerceUnsized<&str>` does not hold but is encountered frequently
633 // so we fast path bail out here
634if let ty::Ref(_, source_pointee, ty::Mutability::Not) = *source.kind()
635 && source_pointee.is_str()
636 && let ty::Ref(_, target_pointee, ty::Mutability::Not) = *target.kind()
637 && target_pointee.is_str()
638 {
639return Err(TypeError::Mismatch);
640 }
641642let traits =
643 (self.tcx.lang_items().unsize_trait(), self.tcx.lang_items().coerce_unsized_trait());
644let (Some(unsize_did), Some(coerce_unsized_did)) = traits else {
645debug!("missing Unsize or CoerceUnsized traits");
646return Err(TypeError::Mismatch);
647 };
648649// Note, we want to avoid unnecessary unsizing. We don't want to coerce to
650 // a DST unless we have to. This currently comes out in the wash since
651 // we can't unify [T] with U. But to properly support DST, we need to allow
652 // that, at which point we will need extra checks on the target here.
653 //
654 // NOTE: this is wrapped in a `commit_if_ok` because it creates
655 // a "spurious" type variable, and we don't want to have that
656 // type variable in memory if the coercion fails.
657self.commit_if_ok(|_| {
658// Handle reborrows before selecting `Source: CoerceUnsized<Target>`.
659let reborrow = match (source.kind(), target.kind()) {
660 (&ty::Ref(_, ty_a, mutbl_a), &ty::Ref(_, _, mutbl_b)) => {
661 coerce_mutbls(mutbl_a, mutbl_b)?;
662663let coercion = RegionVariableOrigin::Coercion(self.cause.span);
664let r_borrow = self.next_region_var(coercion);
665666// We don't allow two-phase borrows here, at least for initial
667 // implementation. If it happens that this coercion is a function argument,
668 // the reborrow in coerce_borrowed_ptr will pick it up.
669let mutbl = AutoBorrowMutability::new(mutbl_b, AllowTwoPhase::No);
670671Some((
672 Adjustment { kind: Adjust::Deref(DerefAdjustKind::Builtin), target: ty_a },
673 Adjustment {
674 kind: Adjust::Borrow(AutoBorrow::Ref(mutbl)),
675 target: Ty::new_ref(self.tcx, r_borrow, ty_a, mutbl_b),
676 },
677 ))
678 }
679 (&ty::Ref(_, ty_a, mt_a), &ty::RawPtr(_, mt_b)) => {
680 coerce_mutbls(mt_a, mt_b)?;
681682Some((
683 Adjustment { kind: Adjust::Deref(DerefAdjustKind::Builtin), target: ty_a },
684 Adjustment {
685 kind: Adjust::Borrow(AutoBorrow::RawPtr(mt_b)),
686 target: Ty::new_ptr(self.tcx, ty_a, mt_b),
687 },
688 ))
689 }
690_ => None,
691 };
692let coerce_source = reborrow.as_ref().map_or(source, |(_, r)| r.target);
693694// Setup either a subtyping or a LUB relationship between
695 // the `CoerceUnsized` target type and the expected type.
696 // We only have the latter, so we use an inference variable
697 // for the former and let type inference do the rest.
698let coerce_target = self.next_ty_var(self.cause.span);
699700let mut coercion = self.unify_and(
701 coerce_target,
702 target,
703 reborrow.map(|(deref, autoref)| [deref, autoref]).into_flat_iter(),
704 Adjust::Pointer(PointerCoercion::Unsize),
705 ForceLeakCheck::No,
706 )?;
707708// Create an obligation for `Source: CoerceUnsized<Target>`.
709let cause =
710self.cause(self.cause.span, ObligationCauseCode::Coercion { source, target });
711let pred =
712 ty::TraitRef::new(self.tcx, coerce_unsized_did, [coerce_source, coerce_target]);
713let obligation = Obligation::new(self.tcx, cause, self.fcx.param_env, pred);
714715if self.next_trait_solver() {
716 coercion.obligations.push(obligation);
717718if self
719.infcx
720 .visit_proof_tree(
721 Goal::new(self.tcx, self.param_env, pred),
722&mut CoerceVisitor { fcx: self.fcx, span: self.cause.span, errored: false },
723 )
724 .is_break()
725 {
726return Err(TypeError::Mismatch);
727 }
728 } else {
729self.coerce_unsized_old_solver(
730 obligation,
731&mut coercion,
732 coerce_unsized_did,
733 unsize_did,
734 )?;
735 }
736737Ok(coercion)
738 })
739 }
740741fn coerce_unsized_old_solver(
742&self,
743 obligation: Obligation<'tcx, ty::Predicate<'tcx>>,
744 coercion: &mut InferOk<'tcx, (Vec<Adjustment<'tcx>>, Ty<'tcx>)>,
745 coerce_unsized_did: DefId,
746 unsize_did: DefId,
747 ) -> Result<(), TypeError<'tcx>> {
748let mut selcx = traits::SelectionContext::new(self);
749// Use a FIFO queue for this custom fulfillment procedure.
750 //
751 // A Vec (or SmallVec) is not a natural choice for a queue. However,
752 // this code path is hot, and this queue usually has a max length of 1
753 // and almost never more than 3. By using a SmallVec we avoid an
754 // allocation, at the (very small) cost of (occasionally) having to
755 // shift subsequent elements down when removing the front element.
756let mut queue: SmallVec<[PredicateObligation<'tcx>; 4]> = {
let count = 0usize + 1usize;
let mut vec = ::smallvec::SmallVec::new();
if count <= vec.inline_size() {
vec.push(obligation);
vec
} else {
::smallvec::SmallVec::from_vec(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[obligation])))
}
}smallvec![obligation];
757758// Keep resolving `CoerceUnsized` and `Unsize` predicates to avoid
759 // emitting a coercion in cases like `Foo<$1>` -> `Foo<$2>`, where
760 // inference might unify those two inner type variables later.
761let traits = [coerce_unsized_did, unsize_did];
762while !queue.is_empty() {
763let obligation = queue.remove(0);
764let trait_pred = match obligation.predicate.kind().no_bound_vars() {
765Some(ty::PredicateKind::Clause(ty::ClauseKind::Trait(trait_pred)))
766if traits.contains(&trait_pred.def_id()) =>
767 {
768self.deeply_resolve_ignoring_regions(trait_pred)
769 }
770_ => {
771 coercion.obligations.push(obligation);
772continue;
773 }
774 };
775{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:775",
"rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
::tracing_core::__macro_support::Option::Some(775u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("coerce_unsized resolve step: {0:?}",
trait_pred) as &dyn ::tracing::field::Value))])
});
} else { ; }
};debug!("coerce_unsized resolve step: {:?}", trait_pred);
776match selcx.select(&obligation.with(selcx.tcx(), trait_pred)) {
777// Uncertain or unimplemented.
778Ok(None) => {
779if trait_pred.def_id() == unsize_did {
780let self_ty = trait_pred.self_ty();
781let unsize_ty = trait_pred.trait_ref.args[1].expect_ty();
782{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:782",
"rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
::tracing_core::__macro_support::Option::Some(782u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("coerce_unsized: ambiguous unsize case for {0:?}",
trait_pred) as &dyn ::tracing::field::Value))])
});
} else { ; }
};debug!("coerce_unsized: ambiguous unsize case for {:?}", trait_pred);
783match (self_ty.kind(), unsize_ty.kind()) {
784 (&ty::Infer(ty::TyVar(v)), ty::Dynamic(..))
785if self.type_var_is_sized(v) =>
786 {
787{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:787",
"rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
::tracing_core::__macro_support::Option::Some(787u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("coerce_unsized: have sized infer {0:?}",
v) as &dyn ::tracing::field::Value))])
});
} else { ; }
};debug!("coerce_unsized: have sized infer {:?}", v);
788 coercion.obligations.push(obligation);
789// `$0: Unsize<dyn Trait>` where we know that `$0: Sized`, try going
790 // for unsizing.
791}
792_ => {
793// Some other case for `$0: Unsize<Something>`. Note that we
794 // hit this case even if `Something` is a sized type, so just
795 // don't do the coercion.
796{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:796",
"rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
::tracing_core::__macro_support::Option::Some(796u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("coerce_unsized: ambiguous unsize")
as &dyn ::tracing::field::Value))])
});
} else { ; }
};debug!("coerce_unsized: ambiguous unsize");
797return Err(TypeError::Mismatch);
798 }
799 }
800 } else {
801{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:801",
"rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
::tracing_core::__macro_support::Option::Some(801u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("coerce_unsized: early return - ambiguous")
as &dyn ::tracing::field::Value))])
});
} else { ; }
};debug!("coerce_unsized: early return - ambiguous");
802return Err(TypeError::Mismatch);
803 }
804 }
805Err(SelectionError::Unimplemented) => {
806{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:806",
"rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
::tracing_core::__macro_support::Option::Some(806u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("coerce_unsized: early return - can\'t prove obligation")
as &dyn ::tracing::field::Value))])
});
} else { ; }
};debug!("coerce_unsized: early return - can't prove obligation");
807return Err(TypeError::Mismatch);
808 }
809810Err(SelectionError::TraitDynIncompatible(_)) => {
811// Dyn compatibility errors in coercion will *always* be due to the
812 // fact that the RHS of the coercion is a non-dyn compatible `dyn Trait`
813 // written in source somewhere (otherwise we will never have lowered
814 // the dyn trait from HIR to middle).
815 //
816 // There's no reason to emit yet another dyn compatibility error,
817 // especially since the span will differ slightly and thus not be
818 // deduplicated at all!
819self.fcx.set_tainted_by_errors(
820self.fcx
821 .dcx()
822 .span_delayed_bug(self.cause.span, "dyn compatibility during coercion"),
823 );
824 }
825Err(err) => {
826let guar = self.err_ctxt().report_selection_error(
827 obligation.clone(),
828&obligation,
829&err,
830 );
831self.fcx.set_tainted_by_errors(guar);
832// Treat this like an obligation and follow through
833 // with the unsizing - the lack of a coercion should
834 // be silent, as it causes a type mismatch later.
835}
836Ok(Some(ImplSource::UserDefined(impl_source))) => {
837 queue.extend(impl_source.nested);
838// Certain incoherent `CoerceUnsized` implementations may cause ICEs,
839 // so check the impl's validity. Taint the body so that we don't try
840 // to evaluate these invalid coercions in CTFE. We only need to do this
841 // for local impls, since upstream impls should be valid.
842if impl_source.impl_def_id.is_local()
843 && let Err(guar) =
844self.tcx.ensure_result().coerce_unsized_info(impl_source.impl_def_id)
845 {
846self.fcx.set_tainted_by_errors(guar);
847 }
848 }
849Ok(Some(impl_source)) => queue.extend(impl_source.nested_obligations()),
850 }
851 }
852853Ok(())
854 }
855856/// Create an obligation for `ty: Unpin`, where .
857fn unpin_obligation(
858&self,
859 source: Ty<'tcx>,
860 target: Ty<'tcx>,
861 ty: Ty<'tcx>,
862 ) -> PredicateObligation<'tcx> {
863let pred = ty::TraitRef::new(
864self.tcx,
865self.tcx.require_lang_item(LangItem::Unpin, self.cause.span),
866 [ty],
867 );
868let cause = self.cause(self.cause.span, ObligationCauseCode::Coercion { source, target });
869PredicateObligation::new(self.tcx, cause, self.param_env, pred)
870 }
871872/// Checks if the given types are compatible for coercion from a pinned reference to a normal reference.
873fn maybe_pin_ref_to_ref(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> Option<CoerceMaybePinnedRef<'tcx>> {
874if !self.tcx.features().pin_ergonomics() {
875return None;
876 }
877if let Some((a_ty, a_pin @ ty::Pinnedness::Pinned, a_mut, a_r)) = a.maybe_pinned_ref()
878 && let Some((_, b_pin @ ty::Pinnedness::Not, b_mut, _)) = b.maybe_pinned_ref()
879 {
880return Some(CoerceMaybePinnedRef { a, b, a_ty, a_pin, a_mut, a_r, b_pin, b_mut });
881 }
882{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:882",
"rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
::tracing_core::__macro_support::Option::Some(882u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("not fitting pinned ref to ref coercion (`{0:?}` -> `{1:?}`)",
a, b) as &dyn ::tracing::field::Value))])
});
} else { ; }
};debug!("not fitting pinned ref to ref coercion (`{:?}` -> `{:?}`)", a, b);
883None884 }
885886/// Coerces from a pinned reference to a normal reference.
887{}
#[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::TRACE <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("coerce_pin_ref_to_ref",
"rustc_hir_typeck::coercion", ::tracing::Level::TRACE,
::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
::tracing_core::__macro_support::Option::Some(887u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
::tracing_core::field::FieldSet::new(&[{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("a")
}> =
::tracing::__macro_support::FieldName::new("a");
NAME.as_str()
},
{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("b")
}> =
::tracing::__macro_support::FieldName::new("b");
NAME.as_str()
},
{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("a_ty")
}> =
::tracing::__macro_support::FieldName::new("a_ty");
NAME.as_str()
},
{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("a_pin")
}> =
::tracing::__macro_support::FieldName::new("a_pin");
NAME.as_str()
},
{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("a_mut")
}> =
::tracing::__macro_support::FieldName::new("a_mut");
NAME.as_str()
},
{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("a_r")
}> =
::tracing::__macro_support::FieldName::new("a_r");
NAME.as_str()
},
{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("b_pin")
}> =
::tracing::__macro_support::FieldName::new("b_pin");
NAME.as_str()
},
{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("b_mut")
}> =
::tracing::__macro_support::FieldName::new("b_mut");
NAME.as_str()
}], ::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::TRACE <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::TRACE <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
meta.fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&a)
as &dyn ::tracing::field::Value)),
(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&b)
as &dyn ::tracing::field::Value)),
(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&a_ty)
as &dyn ::tracing::field::Value)),
(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&a_pin)
as &dyn ::tracing::field::Value)),
(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&a_mut)
as &dyn ::tracing::field::Value)),
(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&a_r)
as &dyn ::tracing::field::Value)),
(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&b_pin)
as &dyn ::tracing::field::Value)),
(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&b_mut)
as &dyn ::tracing::field::Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: CoerceResult<'tcx> = loop {};
return __tracing_attr_fake_return;
}
{
if true {
if !(self.shallow_resolve(a) == a) {
::core::panicking::panic("assertion failed: self.shallow_resolve(a) == a")
};
};
if true {
if !(self.shallow_resolve(b) == b) {
::core::panicking::panic("assertion failed: self.shallow_resolve(b) == b")
};
};
if true {
if !self.tcx.features().pin_ergonomics() {
::core::panicking::panic("assertion failed: self.tcx.features().pin_ergonomics()")
};
};
if true {
{
match (&a_pin, &ty::Pinnedness::Pinned) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
}
};
};
if true {
{
match (&b_pin, &ty::Pinnedness::Not) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
}
};
};
coerce_mutbls(a_mut, b_mut)?;
let unpin_obligation = self.unpin_obligation(a, b, a_ty);
let a = Ty::new_ref(self.tcx, a_r, a_ty, b_mut);
let mut coerce =
self.unify_and(a, b,
[Adjustment {
kind: Adjust::Deref(DerefAdjustKind::Pin),
target: a_ty,
}],
Adjust::Borrow(AutoBorrow::Ref(AutoBorrowMutability::new(b_mut,
self.allow_two_phase))), ForceLeakCheck::No)?;
coerce.obligations.push(unpin_obligation);
Ok(coerce)
}
}
}#[instrument(skip(self), level = "trace")]888fn coerce_pin_ref_to_ref(
889&self,
890CoerceMaybePinnedRef { a, b, a_ty, a_pin, a_mut, a_r, b_pin, b_mut }: CoerceMaybePinnedRef<
891'tcx,
892 >,
893 ) -> CoerceResult<'tcx> {
894debug_assert!(self.shallow_resolve(a) == a);
895debug_assert!(self.shallow_resolve(b) == b);
896debug_assert!(self.tcx.features().pin_ergonomics());
897debug_assert_eq!(a_pin, ty::Pinnedness::Pinned);
898debug_assert_eq!(b_pin, ty::Pinnedness::Not);
899900 coerce_mutbls(a_mut, b_mut)?;
901902let unpin_obligation = self.unpin_obligation(a, b, a_ty);
903904let a = Ty::new_ref(self.tcx, a_r, a_ty, b_mut);
905let mut coerce = self.unify_and(
906 a,
907 b,
908 [Adjustment { kind: Adjust::Deref(DerefAdjustKind::Pin), target: a_ty }],
909 Adjust::Borrow(AutoBorrow::Ref(AutoBorrowMutability::new(b_mut, self.allow_two_phase))),
910 ForceLeakCheck::No,
911 )?;
912 coerce.obligations.push(unpin_obligation);
913Ok(coerce)
914 }
915916/// Checks if the given types are compatible for coercion to a pinned reference.
917fn maybe_to_pin_ref(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> Option<CoerceMaybePinnedRef<'tcx>> {
918if !self.tcx.features().pin_ergonomics() {
919return None;
920 }
921if let Some((a_ty, a_pin, a_mut, a_r)) = a.maybe_pinned_ref()
922 && let Some((_, b_pin @ ty::Pinnedness::Pinned, b_mut, _)) = b.maybe_pinned_ref()
923 {
924return Some(CoerceMaybePinnedRef { a, b, a_ty, a_pin, a_mut, a_r, b_pin, b_mut });
925 }
926{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:926",
"rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
::tracing_core::__macro_support::Option::Some(926u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("not fitting ref to pinned ref coercion (`{0:?}` -> `{1:?}`)",
a, b) as &dyn ::tracing::field::Value))])
});
} else { ; }
};debug!("not fitting ref to pinned ref coercion (`{:?}` -> `{:?}`)", a, b);
927None928 }
929930/// Applies reborrowing and auto-borrowing that results to `Pin<&T>` or `Pin<&mut T>`:
931 ///
932 /// Currently we only support the following coercions:
933 /// - Reborrowing `Pin<&mut T>` -> `Pin<&mut T>`
934 /// - Reborrowing `Pin<&T>` -> `Pin<&T>`
935 /// - Auto-borrowing `&mut T` -> `Pin<&mut T>` where `T: Unpin`
936 /// - Auto-borrowing `&mut T` -> `Pin<&T>` where `T: Unpin`
937 /// - Auto-borrowing `&T` -> `Pin<&T>` where `T: Unpin`
938 ///
939 /// In the future we might want to support other reborrowing coercions, such as:
940 /// - `Pin<Box<T>>` as `Pin<&T>`
941 /// - `Pin<Box<T>>` as `Pin<&mut T>`
942{}
#[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::TRACE <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("coerce_to_pin_ref",
"rustc_hir_typeck::coercion", ::tracing::Level::TRACE,
::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
::tracing_core::__macro_support::Option::Some(942u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
::tracing_core::field::FieldSet::new(&[{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("a")
}> =
::tracing::__macro_support::FieldName::new("a");
NAME.as_str()
},
{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("b")
}> =
::tracing::__macro_support::FieldName::new("b");
NAME.as_str()
},
{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("a_ty")
}> =
::tracing::__macro_support::FieldName::new("a_ty");
NAME.as_str()
},
{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("a_pin")
}> =
::tracing::__macro_support::FieldName::new("a_pin");
NAME.as_str()
},
{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("a_mut")
}> =
::tracing::__macro_support::FieldName::new("a_mut");
NAME.as_str()
},
{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("a_r")
}> =
::tracing::__macro_support::FieldName::new("a_r");
NAME.as_str()
},
{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("b_pin")
}> =
::tracing::__macro_support::FieldName::new("b_pin");
NAME.as_str()
},
{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("b_mut")
}> =
::tracing::__macro_support::FieldName::new("b_mut");
NAME.as_str()
}], ::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::TRACE <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::TRACE <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
meta.fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&a)
as &dyn ::tracing::field::Value)),
(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&b)
as &dyn ::tracing::field::Value)),
(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&a_ty)
as &dyn ::tracing::field::Value)),
(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&a_pin)
as &dyn ::tracing::field::Value)),
(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&a_mut)
as &dyn ::tracing::field::Value)),
(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&a_r)
as &dyn ::tracing::field::Value)),
(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&b_pin)
as &dyn ::tracing::field::Value)),
(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&b_mut)
as &dyn ::tracing::field::Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: CoerceResult<'tcx> = loop {};
return __tracing_attr_fake_return;
}
{
if true {
if !(self.shallow_resolve(a) == a) {
::core::panicking::panic("assertion failed: self.shallow_resolve(a) == a")
};
};
if true {
if !(self.shallow_resolve(b) == b) {
::core::panicking::panic("assertion failed: self.shallow_resolve(b) == b")
};
};
if true {
if !self.tcx.features().pin_ergonomics() {
::core::panicking::panic("assertion failed: self.tcx.features().pin_ergonomics()")
};
};
if true {
{
match (&b_pin, &ty::Pinnedness::Pinned) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
}
};
};
let (deref, unpin_obligation) =
match a_pin {
ty::Pinnedness::Pinned => (DerefAdjustKind::Pin, None),
ty::Pinnedness::Not => {
(DerefAdjustKind::Builtin,
Some(self.unpin_obligation(a, b, a_ty)))
}
};
coerce_mutbls(a_mut, b_mut)?;
let a = Ty::new_pinned_ref(self.tcx, a_r, a_ty, b_mut);
let mut coerce =
self.unify_and(a, b,
[Adjustment { kind: Adjust::Deref(deref), target: a_ty }],
Adjust::Borrow(AutoBorrow::Pin(b_mut)),
ForceLeakCheck::No)?;
coerce.obligations.extend(unpin_obligation);
Ok(coerce)
}
}
}#[instrument(skip(self), level = "trace")]943fn coerce_to_pin_ref(
944&self,
945CoerceMaybePinnedRef { a, b, a_ty, a_pin, a_mut, a_r, b_pin, b_mut }: CoerceMaybePinnedRef<
946'tcx,
947 >,
948 ) -> CoerceResult<'tcx> {
949debug_assert!(self.shallow_resolve(a) == a);
950debug_assert!(self.shallow_resolve(b) == b);
951debug_assert!(self.tcx.features().pin_ergonomics());
952debug_assert_eq!(b_pin, ty::Pinnedness::Pinned);
953954// We need to deref the reference first before we reborrow it to a pinned reference.
955let (deref, unpin_obligation) = match a_pin {
956// no `Unpin` required when reborrowing a pinned reference to a pinned reference
957ty::Pinnedness::Pinned => (DerefAdjustKind::Pin, None),
958// `Unpin` required when reborrowing a non-pinned reference to a pinned reference
959ty::Pinnedness::Not => {
960 (DerefAdjustKind::Builtin, Some(self.unpin_obligation(a, b, a_ty)))
961 }
962 };
963964 coerce_mutbls(a_mut, b_mut)?;
965966// update a with b's mutability since we'll be coercing mutability
967let a = Ty::new_pinned_ref(self.tcx, a_r, a_ty, b_mut);
968969// To complete the reborrow, we need to make sure we can unify the inner types, and if so we
970 // add the adjustments.
971let mut coerce = self.unify_and(
972 a,
973 b,
974 [Adjustment { kind: Adjust::Deref(deref), target: a_ty }],
975 Adjust::Borrow(AutoBorrow::Pin(b_mut)),
976 ForceLeakCheck::No,
977 )?;
978979 coerce.obligations.extend(unpin_obligation);
980Ok(coerce)
981 }
982983/// Applies generic exclusive reborrowing on type implementing `Reborrow`.
984{}
#[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::TRACE <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("coerce_reborrow",
"rustc_hir_typeck::coercion", ::tracing::Level::TRACE,
::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
::tracing_core::__macro_support::Option::Some(984u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
::tracing_core::field::FieldSet::new(&[{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("a")
}> =
::tracing::__macro_support::FieldName::new("a");
NAME.as_str()
},
{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("b")
}> =
::tracing::__macro_support::FieldName::new("b");
NAME.as_str()
}], ::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::TRACE <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::TRACE <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
meta.fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&a)
as &dyn ::tracing::field::Value)),
(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&b)
as &dyn ::tracing::field::Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: CoerceResult<'tcx> = loop {};
return __tracing_attr_fake_return;
}
{
if true {
if !(self.shallow_resolve(a) == a) {
::core::panicking::panic("assertion failed: self.shallow_resolve(a) == a")
};
};
if true {
if !(self.shallow_resolve(b) == b) {
::core::panicking::panic("assertion failed: self.shallow_resolve(b) == b")
};
};
let (ty::Adt(a_def, _), ty::Adt(b_def, _)) =
(a.kind(),
b.kind()) else { return Err(TypeError::Mismatch); };
if a_def.did() == b_def.did() {
let coerce =
self.unify_and(a, b, [],
Adjust::GenericReborrow(ty::Mutability::Mut),
ForceLeakCheck::No)?;
self.taint_if_reborrow_impls_are_invalid(ty::Mutability::Mut);
Ok(coerce)
} else { Err(TypeError::Mismatch) }
}
}
}#[instrument(skip(self), level = "trace")]985fn coerce_reborrow(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> CoerceResult<'tcx> {
986debug_assert!(self.shallow_resolve(a) == a);
987debug_assert!(self.shallow_resolve(b) == b);
988989// We need to make sure the two types are compatible for reborrow.
990let (ty::Adt(a_def, _), ty::Adt(b_def, _)) = (a.kind(), b.kind()) else {
991return Err(TypeError::Mismatch);
992 };
993if a_def.did() == b_def.did() {
994// Reborrow is applicable here
995let coerce = self.unify_and(
996 a,
997 b,
998 [],
999 Adjust::GenericReborrow(ty::Mutability::Mut),
1000 ForceLeakCheck::No,
1001 )?;
1002self.taint_if_reborrow_impls_are_invalid(ty::Mutability::Mut);
1003Ok(coerce)
1004 } else {
1005// FIXME: CoerceShared check goes here, error for now
1006Err(TypeError::Mismatch)
1007 }
1008 }
10091010/// Applies generic exclusive reborrowing on type implementing `Reborrow`.
1011{}
#[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::TRACE <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("coerce_shared_reborrow",
"rustc_hir_typeck::coercion", ::tracing::Level::TRACE,
::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
::tracing_core::__macro_support::Option::Some(1011u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
::tracing_core::field::FieldSet::new(&[{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("a")
}> =
::tracing::__macro_support::FieldName::new("a");
NAME.as_str()
},
{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("b")
}> =
::tracing::__macro_support::FieldName::new("b");
NAME.as_str()
}], ::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::TRACE <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::TRACE <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
meta.fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&a)
as &dyn ::tracing::field::Value)),
(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&b)
as &dyn ::tracing::field::Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: CoerceResult<'tcx> = loop {};
return __tracing_attr_fake_return;
}
{
if true {
if !(self.shallow_resolve(a) == a) {
::core::panicking::panic("assertion failed: self.shallow_resolve(a) == a")
};
};
if true {
if !(self.shallow_resolve(b) == b) {
::core::panicking::panic("assertion failed: self.shallow_resolve(b) == b")
};
};
let (ty::Adt(a_def, _), ty::Adt(b_def, _)) =
(a.kind(),
b.kind()) else { return Err(TypeError::Mismatch); };
if a_def.did() == b_def.did() { return Err(TypeError::Mismatch); }
let Some(coerce_shared_trait_did) =
self.tcx.lang_items().coerce_shared() else {
return Err(TypeError::Mismatch);
};
let coerce_shared_trait_ref =
ty::TraitRef::new(self.tcx, coerce_shared_trait_did, [a, b]);
let obligation =
traits::Obligation::new(self.tcx, ObligationCause::dummy(),
self.param_env, ty::Binder::dummy(coerce_shared_trait_ref));
let ocx = ObligationCtxt::new(&self.infcx);
ocx.register_obligation(obligation);
let errs = ocx.evaluate_obligations_error_on_ambiguity();
if errs.no_errors() {
self.taint_if_reborrow_impls_are_invalid(ty::Mutability::Not);
Ok(InferOk {
value: (::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[Adjustment {
kind: Adjust::GenericReborrow(ty::Mutability::Not),
target: b,
}])), b),
obligations: ocx.into_pending_obligations(),
})
} else { Err(TypeError::Mismatch) }
}
}
}#[instrument(skip(self), level = "trace")]1012fn coerce_shared_reborrow(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> CoerceResult<'tcx> {
1013debug_assert!(self.shallow_resolve(a) == a);
1014debug_assert!(self.shallow_resolve(b) == b);
10151016// We need to make sure the two types are compatible for reborrow.
1017let (ty::Adt(a_def, _), ty::Adt(b_def, _)) = (a.kind(), b.kind()) else {
1018return Err(TypeError::Mismatch);
1019 };
1020if a_def.did() == b_def.did() {
1021// CoerceShared cannot be T -> T.
1022return Err(TypeError::Mismatch);
1023 }
1024let Some(coerce_shared_trait_did) = self.tcx.lang_items().coerce_shared() else {
1025return Err(TypeError::Mismatch);
1026 };
1027let coerce_shared_trait_ref = ty::TraitRef::new(self.tcx, coerce_shared_trait_did, [a, b]);
1028let obligation = traits::Obligation::new(
1029self.tcx,
1030 ObligationCause::dummy(),
1031self.param_env,
1032 ty::Binder::dummy(coerce_shared_trait_ref),
1033 );
1034let ocx = ObligationCtxt::new(&self.infcx);
1035 ocx.register_obligation(obligation);
1036let errs = ocx.evaluate_obligations_error_on_ambiguity();
1037if errs.no_errors() {
1038self.taint_if_reborrow_impls_are_invalid(ty::Mutability::Not);
1039Ok(InferOk {
1040 value: (
1041vec![Adjustment {
1042 kind: Adjust::GenericReborrow(ty::Mutability::Not),
1043 target: b,
1044 }],
1045 b,
1046 ),
1047 obligations: ocx.into_pending_obligations(),
1048 })
1049 } else {
1050Err(TypeError::Mismatch)
1051 }
1052 }
10531054/// Borrowck and const eval lower a generic reborrow by walking the fields of the source and
1055 /// target types, trusting that coherence has validated the `Reborrow` and `CoerceShared` impls
1056 /// that permit it. If coherence rejected any of those impls, that walk can hit shapes it does
1057 /// not expect, so we taint the body to keep it away from them.
1058fn taint_if_reborrow_impls_are_invalid(&self, mutability: ty::Mutability) {
1059let lang_items = self.tcx.lang_items();
1060let trait_def_ids = match mutability {
1061 ty::Mutability::Mut => [lang_items.reborrow(), None],
1062// A shared reborrow also reborrows any fields of the source that implement `Reborrow`.
1063ty::Mutability::Not => [lang_items.coerce_shared(), lang_items.reborrow()],
1064 };
1065for trait_def_id in trait_def_ids.into_iter().flatten() {
1066if let Err(guar) = self.tcx.ensure_result().coherent_trait(trait_def_id) {
1067self.set_tainted_by_errors(guar);
1068 }
1069 }
1070 }
10711072fn coerce_from_fn_pointer(
1073&self,
1074 a: Ty<'tcx>,
1075 a_sig: ty::PolyFnSig<'tcx>,
1076 b: Ty<'tcx>,
1077 ) -> CoerceResult<'tcx> {
1078{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:1078",
"rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
::tracing_core::__macro_support::Option::Some(1078u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
::tracing_core::field::FieldSet::new(&["message",
{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("a_sig")
}> =
::tracing::__macro_support::FieldName::new("a_sig");
NAME.as_str()
},
{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("b")
}> =
::tracing::__macro_support::FieldName::new("b");
NAME.as_str()
}], ::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("coerce_from_fn_pointer")
as &dyn ::tracing::field::Value)),
(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&a_sig)
as &dyn ::tracing::field::Value)),
(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&b)
as &dyn ::tracing::field::Value))])
});
} else { ; }
};debug!(?a_sig, ?b, "coerce_from_fn_pointer");
1079if true {
if !(self.shallow_resolve(b) == b) {
::core::panicking::panic("assertion failed: self.shallow_resolve(b) == b")
};
};debug_assert!(self.shallow_resolve(b) == b);
10801081match b.kind() {
1082 ty::FnPtr(_, b_hdr) if a_sig.safety().is_safe() && b_hdr.safety().is_unsafe() => {
1083let a = self.tcx.safe_to_unsafe_fn_ty(a_sig);
1084let adjust = Adjust::Pointer(PointerCoercion::UnsafeFnPointer);
1085self.unify_and(a, b, [], adjust, ForceLeakCheck::Yes)
1086 }
1087_ => self.unify(a, b, ForceLeakCheck::Yes),
1088 }
1089 }
10901091fn coerce_from_fn_item(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> CoerceResult<'tcx> {
1092{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:1092",
"rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
::tracing_core::__macro_support::Option::Some(1092u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("coerce_from_fn_item(a={0:?}, b={1:?})",
a, b) as &dyn ::tracing::field::Value))])
});
} else { ; }
};debug!("coerce_from_fn_item(a={:?}, b={:?})", a, b);
1093if true {
if !(self.shallow_resolve(a) == a) {
::core::panicking::panic("assertion failed: self.shallow_resolve(a) == a")
};
};debug_assert!(self.shallow_resolve(a) == a);
1094if true {
if !(self.shallow_resolve(b) == b) {
::core::panicking::panic("assertion failed: self.shallow_resolve(b) == b")
};
};debug_assert!(self.shallow_resolve(b) == b);
10951096match b.kind() {
1097 ty::FnPtr(_, b_hdr) => {
1098let a_sig = self.sig_for_fn_def_coercion(a, Some(b_hdr.safety()))?;
10991100let InferOk { value: a_sig, mut obligations } =
1101self.at(&self.cause, self.param_env).normalize(Unnormalized::new_wip(a_sig));
1102let a = Ty::new_fn_ptr(self.tcx, a_sig);
11031104let adjust = Adjust::Pointer(PointerCoercion::ReifyFnPointer(b_hdr.safety()));
1105let InferOk { value, obligations: o2 } =
1106self.unify_and(a, b, [], adjust, ForceLeakCheck::Yes)?;
11071108obligations.extend(o2);
1109Ok(InferOk { value, obligations })
1110 }
1111_ => self.unify(a, b, ForceLeakCheck::No),
1112 }
1113 }
11141115/// Attempts to coerce from a closure to a function pointer. Fails
1116 /// if the closure has any upvars.
1117fn coerce_closure_to_fn(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> CoerceResult<'tcx> {
1118if true {
if !(self.shallow_resolve(a) == a) {
::core::panicking::panic("assertion failed: self.shallow_resolve(a) == a")
};
};debug_assert!(self.shallow_resolve(a) == a);
1119if true {
if !(self.shallow_resolve(b) == b) {
::core::panicking::panic("assertion failed: self.shallow_resolve(b) == b")
};
};debug_assert!(self.shallow_resolve(b) == b);
11201121match b.kind() {
1122 ty::FnPtr(_, hdr) => {
1123let safety = hdr.safety();
1124let terr = TypeError::Sorts(ty::error::ExpectedFound::new(a, b));
1125let closure_sig = self.sig_for_closure_coercion(a, Some(hdr.safety()), terr)?;
1126let pointer_ty = Ty::new_fn_ptr(self.tcx, closure_sig);
1127{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:1127",
"rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
::tracing_core::__macro_support::Option::Some(1127u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("coerce_closure_to_fn(a={0:?}, b={1:?}, pty={2:?})",
a, b, pointer_ty) as &dyn ::tracing::field::Value))])
});
} else { ; }
};debug!("coerce_closure_to_fn(a={:?}, b={:?}, pty={:?})", a, b, pointer_ty);
11281129let adjust = Adjust::Pointer(PointerCoercion::ClosureFnPointer(safety));
1130self.unify_and(pointer_ty, b, [], adjust, ForceLeakCheck::No)
1131 }
1132_ => self.unify(a, b, ForceLeakCheck::No),
1133 }
1134 }
11351136fn coerce_to_raw_ptr(
1137&self,
1138 a: Ty<'tcx>,
1139 b: Ty<'tcx>,
1140 mutbl_b: hir::Mutability,
1141 ) -> CoerceResult<'tcx> {
1142{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:1142",
"rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
::tracing_core::__macro_support::Option::Some(1142u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("coerce_to_raw_ptr(a={0:?}, b={1:?})",
a, b) as &dyn ::tracing::field::Value))])
});
} else { ; }
};debug!("coerce_to_raw_ptr(a={:?}, b={:?})", a, b);
1143if true {
if !(self.shallow_resolve(a) == a) {
::core::panicking::panic("assertion failed: self.shallow_resolve(a) == a")
};
};debug_assert!(self.shallow_resolve(a) == a);
1144if true {
if !(self.shallow_resolve(b) == b) {
::core::panicking::panic("assertion failed: self.shallow_resolve(b) == b")
};
};debug_assert!(self.shallow_resolve(b) == b);
11451146let (is_ref, mt_a) = match *a.kind() {
1147 ty::Ref(_, ty, mutbl) => (true, ty::TypeAndMut { ty, mutbl }),
1148 ty::RawPtr(ty, mutbl) => (false, ty::TypeAndMut { ty, mutbl }),
1149_ => return self.unify(a, b, ForceLeakCheck::No),
1150 };
1151 coerce_mutbls(mt_a.mutbl, mutbl_b)?;
11521153// Check that the types which they point at are compatible.
1154let a_raw = Ty::new_ptr(self.tcx, mt_a.ty, mutbl_b);
1155// Although references and raw ptrs have the same
1156 // representation, we still register an Adjust::DerefRef so that
1157 // regionck knows that the region for `a` must be valid here.
1158if is_ref {
1159self.unify_and(
1160a_raw,
1161b,
1162 [Adjustment { kind: Adjust::Deref(DerefAdjustKind::Builtin), target: mt_a.ty }],
1163 Adjust::Borrow(AutoBorrow::RawPtr(mutbl_b)),
1164 ForceLeakCheck::No,
1165 )
1166 } else if mt_a.mutbl != mutbl_b {
1167self.unify_and(
1168a_raw,
1169b,
1170 [],
1171 Adjust::Pointer(PointerCoercion::MutToConstPointer),
1172 ForceLeakCheck::No,
1173 )
1174 } else {
1175self.unify(a_raw, b, ForceLeakCheck::No)
1176 }
1177 }
1178}
11791180impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1181/// Attempt to coerce an expression to a type, and return the
1182 /// adjusted type of the expression, if successful.
1183 /// Adjustments are only recorded if the coercion succeeded.
1184 /// The expressions *must not* have any preexisting adjustments.
1185pub(crate) fn coerce(
1186&self,
1187 expr: &'tcx hir::Expr<'tcx>,
1188 expr_ty: Ty<'tcx>,
1189 target: Ty<'tcx>,
1190 allow_two_phase: AllowTwoPhase,
1191 cause: Option<ObligationCause<'tcx>>,
1192 ) -> RelateResult<'tcx, Ty<'tcx>> {
1193let source = self.deeply_resolve_ignoring_regions_with_obligations(expr_ty);
1194{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:1194",
"rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
::tracing_core::__macro_support::Option::Some(1194u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("coercion::try({0:?}: {1:?} -> {2:?})",
expr, source, target) as &dyn ::tracing::field::Value))])
});
} else { ; }
};debug!("coercion::try({:?}: {:?} -> {:?})", expr, source, target);
11951196let cause =
1197cause.unwrap_or_else(|| self.cause(expr.span, ObligationCauseCode::ExprAssignable));
1198let coerce = Coerce::new(
1199self,
1200cause,
1201allow_two_phase,
1202self.tcx.expr_guaranteed_to_constitute_read_for_never(expr),
1203 );
1204let ok = self.commit_if_ok(|_| coerce.coerce(source, target))?;
12051206let (adjustments, _) = self.register_infer_ok_obligations(ok);
1207self.apply_adjustments(expr, adjustments);
1208Ok(if let Err(guar) = expr_ty.error_reported() {
1209Ty::new_error(self.tcx, guar)
1210 } else {
1211target1212 })
1213 }
12141215/// Probe whether `expr_ty` can be coerced to `target_ty`. This has no side-effects,
1216 /// and may return false positives if types are not yet fully constrained by inference.
1217 ///
1218 /// Returns false if the coercion is not possible, or if the coercion creates any
1219 /// sub-obligations that result in errors.
1220 ///
1221 /// This should only be used for diagnostics.
1222pub(crate) fn may_coerce(&self, expr_ty: Ty<'tcx>, target_ty: Ty<'tcx>) -> bool {
1223let cause = self.cause(DUMMY_SP, ObligationCauseCode::ExprAssignable);
1224// We don't ever need two-phase here since we throw out the result of the coercion.
1225 // We also just always set `coerce_never` to true, since this is a heuristic.
1226let coerce = Coerce::new(self, cause.clone(), AllowTwoPhase::No, true);
1227self.probe(|_| {
1228// Make sure to structurally resolve the types, since we use
1229 // the `TyKind`s heavily in coercion.
1230let ocx = ObligationCtxt::new(self);
1231let Ok(ok) = coerce.coerce(expr_ty, target_ty) else {
1232return false;
1233 };
1234ocx.register_obligations(ok.obligations);
1235ocx.try_evaluate_obligations().no_errors()
1236 })
1237 }
12381239/// Like [`Self::may_coerce`], but for suggestions whose replacement must complete with a
1240 /// value of the target type. A coercion from `!` to another type does not provide such a
1241 /// value, so it should not by itself justify these suggestions.
1242 ///
1243 /// This should only be used for suggestions.
1244pub(crate) fn may_coerce_except_never(&self, expr_ty: Ty<'tcx>, target_ty: Ty<'tcx>) -> bool {
1245if expr_ty.is_never() && !target_ty.is_never() {
1246return false;
1247 }
1248self.may_coerce(expr_ty, target_ty)
1249 }
12501251/// Given a type and a target type, this function will calculate and return
1252 /// how many dereference steps needed to coerce `expr_ty` to `target`. If
1253 /// it's not possible, return `None`.
1254pub(crate) fn deref_steps_for_suggestion(
1255&self,
1256 expr_ty: Ty<'tcx>,
1257 target: Ty<'tcx>,
1258 ) -> Option<usize> {
1259let cause = self.cause(DUMMY_SP, ObligationCauseCode::ExprAssignable);
1260// We don't ever need two-phase here since we throw out the result of the coercion.
1261let coerce = Coerce::new(self, cause, AllowTwoPhase::No, true);
1262coerce.autoderef(DUMMY_SP, expr_ty).find_map(|(ty, steps)| {
1263self.probe(|_| coerce.unify_raw(ty, target, ForceLeakCheck::No)).ok().map(|_| steps)
1264 })
1265 }
12661267/// Given a type, this function will calculate and return the type given
1268 /// for `<Ty as Deref>::Target` only if `Ty` also implements `DerefMut`.
1269 ///
1270 /// This function is for diagnostics only, since it does not register
1271 /// trait or region sub-obligations. (presumably we could, but it's not
1272 /// particularly important for diagnostics...)
1273pub(crate) fn deref_once_mutably_for_diagnostic(&self, expr_ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
1274self.autoderef(DUMMY_SP, expr_ty).silence_errors().nth(1).and_then(|(deref_ty, _)| {
1275self.infcx
1276 .type_implements_trait(
1277self.tcx.lang_items().deref_mut_trait()?,
1278 [expr_ty],
1279self.param_env,
1280 )
1281 .may_apply()
1282 .then_some(deref_ty)
1283 })
1284 }
12851286{}
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() || { false }
{
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("sig_for_coerce_lub",
"rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
::tracing_core::__macro_support::Option::Some(1286u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
::tracing_core::field::FieldSet::new(&[{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("ty")
}> =
::tracing::__macro_support::FieldName::new("ty");
NAME.as_str()
},
{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("closure_upvars_terr")
}> =
::tracing::__macro_support::FieldName::new("closure_upvars_terr");
NAME.as_str()
}], ::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
meta.fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&ty)
as &dyn ::tracing::field::Value)),
(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&closure_upvars_terr)
as &dyn ::tracing::field::Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[allow(clippy :: redundant_closure_call)]
let x =
(move ||
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy
:: needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return:
Result<ty::PolyFnSig<'tcx>, TypeError<'tcx>> = loop {};
return __tracing_attr_fake_return;
}
{
match ty.kind() {
ty::FnDef(..) => self.sig_for_fn_def_coercion(ty, None),
ty::Closure(..) =>
self.sig_for_closure_coercion(ty, None,
closure_upvars_terr),
_ => {
::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
format_args!("`sig_for_fn_def_closure_coerce_lub` called with wrong ty: {0:?}",
ty)));
}
}
}
})();
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:1286",
"rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
::tracing_core::__macro_support::Option::Some(1286u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
::tracing_core::field::FieldSet::new(&[{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("return")
}> =
::tracing::__macro_support::FieldName::new("return");
NAME.as_str()
}], ::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&x)
as &dyn ::tracing::field::Value))])
});
} else { ; }
};
x;#[instrument(level = "debug", skip(self), ret)]1287fn sig_for_coerce_lub(
1288&self,
1289 ty: Ty<'tcx>,
1290 closure_upvars_terr: TypeError<'tcx>,
1291 ) -> Result<ty::PolyFnSig<'tcx>, TypeError<'tcx>> {
1292match ty.kind() {
1293 ty::FnDef(..) => self.sig_for_fn_def_coercion(ty, None),
1294 ty::Closure(..) => self.sig_for_closure_coercion(ty, None, closure_upvars_terr),
1295_ => unreachable!("`sig_for_fn_def_closure_coerce_lub` called with wrong ty: {:?}", ty),
1296 }
1297 }
12981299fn sig_for_fn_def_coercion(
1300&self,
1301 fndef: Ty<'tcx>,
1302 expected_safety: Option<hir::Safety>,
1303 ) -> Result<ty::PolyFnSig<'tcx>, TypeError<'tcx>> {
1304let tcx = self.tcx;
13051306let &ty::FnDef(def_id, _) = fndef.kind() else {
1307{
::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
format_args!("`sig_for_fn_def_coercion` called with non-fndef: {0:?}",
fndef)));
};unreachable!("`sig_for_fn_def_coercion` called with non-fndef: {:?}", fndef);
1308 };
13091310// Intrinsics are not coercible to function pointers
1311if tcx.intrinsic(def_id).is_some() {
1312return Err(TypeError::IntrinsicCast);
1313 }
13141315let fn_attrs = tcx.codegen_fn_attrs(def_id);
1316if #[allow(non_exhaustive_omitted_patterns)] match fn_attrs.inline {
InlineAttr::Force { .. } => true,
_ => false,
}matches!(fn_attrs.inline, InlineAttr::Force { .. }) {
1317return Err(TypeError::ForceInlineCast);
1318 }
13191320let sig = fndef.fn_sig(tcx);
1321let sig = if fn_attrs.safe_target_features {
1322// Allow the coercion if the current function has all the features that would be
1323 // needed to call the coercee safely.
1324match tcx.adjust_target_feature_sig(def_id, sig, self.body_def_id.into()) {
1325Some(adjusted_sig) => adjusted_sig,
1326Noneif #[allow(non_exhaustive_omitted_patterns)] match expected_safety {
Some(hir::Safety::Safe) => true,
_ => false,
}matches!(expected_safety, Some(hir::Safety::Safe)) => {
1327return Err(TypeError::TargetFeatureCast(def_id));
1328 }
1329None => sig,
1330 }
1331 } else {
1332sig1333 };
13341335if sig.safety().is_safe() && #[allow(non_exhaustive_omitted_patterns)] match expected_safety {
Some(hir::Safety::Unsafe) => true,
_ => false,
}matches!(expected_safety, Some(hir::Safety::Unsafe)) {
1336Ok(tcx.safe_to_unsafe_sig(sig))
1337 } else {
1338Ok(sig)
1339 }
1340 }
13411342fn sig_for_closure_coercion(
1343&self,
1344 closure: Ty<'tcx>,
1345 expected_safety: Option<hir::Safety>,
1346 closure_upvars_terr: TypeError<'tcx>,
1347 ) -> Result<ty::PolyFnSig<'tcx>, TypeError<'tcx>> {
1348let tcx = self.tcx;
13491350let ty::Closure(closure_def, closure_args) = closure.kind() else {
1351{
::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
format_args!("`sig_for_closure_coercion` called with non closure ty: {0:?}",
closure)));
};unreachable!("`sig_for_closure_coercion` called with non closure ty: {:?}", closure);
1352 };
13531354// At this point we haven't done capture analysis, which means
1355 // that the ClosureArgs just contains an inference variable instead
1356 // of tuple of captured types.
1357 //
1358 // All we care here is if any variable is being captured and not the exact paths,
1359 // so we check `upvars_mentioned` for root variables being captured.
1360if !tcx.upvars_mentioned(closure_def.expect_local()).is_none_or(|u| u.is_empty()) {
1361return Err(closure_upvars_terr);
1362 }
13631364// We coerce the closure, which has fn type
1365 // `extern "rust-call" fn((arg0,arg1,...)) -> _`
1366 // to
1367 // `fn(arg0,arg1,...) -> _`
1368 // or
1369 // `unsafe fn(arg0,arg1,...) -> _`
1370let closure_sig = closure_args.as_closure().sig();
1371Ok(tcx.signature_unclosure(closure_sig, expected_safety.unwrap_or(hir::Safety::Safe)))
1372 }
13731374/// Given some expressions, their known unified type and another expression,
1375 /// tries to unify the types, potentially inserting coercions on any of the
1376 /// provided expressions and returns their LUB (aka "common supertype").
1377 ///
1378 /// This is really an internal helper. From outside the coercion
1379 /// module, you should instantiate a `CoerceMany` instance.
1380fn try_find_coercion_lub(
1381&self,
1382 cause: &ObligationCause<'tcx>,
1383 exprs: &[&'tcx hir::Expr<'tcx>],
1384 prev_ty: Ty<'tcx>,
1385 new: &hir::Expr<'_>,
1386 new_ty: Ty<'tcx>,
1387 ) -> RelateResult<'tcx, Ty<'tcx>> {
1388let prev_ty = self.deeply_resolve_ignoring_regions_with_obligations(prev_ty);
1389let new_ty = self.deeply_resolve_ignoring_regions_with_obligations(new_ty);
1390{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:1390",
"rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
::tracing_core::__macro_support::Option::Some(1390u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("coercion::try_find_coercion_lub({0:?}, {1:?}, exprs={2:?} exprs)",
prev_ty, new_ty, exprs.len()) as
&dyn ::tracing::field::Value))])
});
} else { ; }
};debug!(
1391"coercion::try_find_coercion_lub({:?}, {:?}, exprs={:?} exprs)",
1392 prev_ty,
1393 new_ty,
1394 exprs.len()
1395 );
13961397// Fast Path: don't go through the coercion logic if we're coercing
1398 // a type to itself. This is unfortunately quite perf relevant so
1399 // we do it even though it may mask bugs in the coercion logic.
1400if prev_ty == new_ty {
1401return Ok(prev_ty);
1402 }
14031404let terr = TypeError::Sorts(ty::error::ExpectedFound::new(prev_ty, new_ty));
1405let opt_sigs = match (prev_ty.kind(), new_ty.kind()) {
1406// Don't coerce pairs of fndefs or pairs of closures to fn ptrs
1407 // if they can just be lubbed.
1408 //
1409 // See #88097 or `lub_closures_before_fnptr_coercion.rs` for where
1410 // we would erroneously coerce closures to fnptrs when attempting to
1411 // coerce a closure to itself.
1412(ty::FnDef(..), ty::FnDef(..)) | (ty::Closure(..), ty::Closure(..)) => {
1413let lubbed_ty = self.commit_if_ok(|snapshot| {
1414let outer_universe = self.infcx.universe();
14151416// We need to eagerly handle nested obligations due to lazy norm.
1417let result = if self.next_trait_solver() {
1418let ocx = ObligationCtxt::new(self);
1419let value = ocx.lub(cause, self.param_env, prev_ty, new_ty)?;
1420if ocx.try_evaluate_obligations().no_errors() {
1421Ok(InferOk { value, obligations: ocx.into_pending_obligations() })
1422 } else {
1423Err(TypeError::Mismatch)
1424 }
1425 } else {
1426self.at(cause, self.param_env).lub(prev_ty, new_ty)
1427 };
14281429self.leak_check(outer_universe, Some(snapshot))?;
1430result1431 });
14321433match lubbed_ty {
1434Ok(ok) => return Ok(self.register_infer_ok_obligations(ok)),
1435Err(_) => {
1436let a_sig = self.sig_for_coerce_lub(prev_ty, terr)?;
1437let b_sig = self.sig_for_coerce_lub(new_ty, terr)?;
1438Some((a_sig, b_sig))
1439 }
1440 }
1441 }
14421443 (ty::Closure(..), ty::FnDef(..)) | (ty::FnDef(..), ty::Closure(..)) => {
1444let a_sig = self.sig_for_coerce_lub(prev_ty, terr)?;
1445let b_sig = self.sig_for_coerce_lub(new_ty, terr)?;
1446Some((a_sig, b_sig))
1447 }
1448// ty::FnPtr x ty::FnPtr is fine to just be handled through a normal `unify`
1449 // call using `lub` which is what will happen on the normal path.
1450(ty::FnPtr(..), ty::FnPtr(..)) => None,
1451_ => None,
1452 };
14531454if let Some((mut a_sig, mut b_sig)) = opt_sigs {
1455// Allow coercing safe sigs to unsafe sigs
1456if a_sig.safety().is_safe() && b_sig.safety().is_unsafe() {
1457a_sig = self.tcx.safe_to_unsafe_sig(a_sig);
1458 } else if b_sig.safety().is_safe() && a_sig.safety().is_unsafe() {
1459b_sig = self.tcx.safe_to_unsafe_sig(b_sig);
1460 };
14611462// The signature must match.
1463let (a_sig, b_sig) = self.normalize(new.span, Unnormalized::new_wip((a_sig, b_sig)));
1464let sig = self
1465.at(cause, self.param_env)
1466 .lub(a_sig, b_sig)
1467 .map(|ok| self.register_infer_ok_obligations(ok))?;
14681469// Reify both sides and return the reified fn pointer type.
1470let fn_ptr = Ty::new_fn_ptr(self.tcx, sig);
1471let prev_adjustment = match prev_ty.kind() {
1472 ty::Closure(..) => Adjust::Pointer(PointerCoercion::ClosureFnPointer(sig.safety())),
1473 ty::FnDef(..) => Adjust::Pointer(PointerCoercion::ReifyFnPointer(sig.safety())),
1474_ => ::rustc_span::macros::bug_impl(Some(cause.span),
format_args!("should not try to coerce a {0} to a fn pointer", prev_ty),
Location::caller())span_bug!(cause.span, "should not try to coerce a {prev_ty} to a fn pointer"),
1475 };
1476let next_adjustment = match new_ty.kind() {
1477 ty::Closure(..) => Adjust::Pointer(PointerCoercion::ClosureFnPointer(sig.safety())),
1478 ty::FnDef(..) => Adjust::Pointer(PointerCoercion::ReifyFnPointer(sig.safety())),
1479_ => ::rustc_span::macros::bug_impl(Some(new.span),
format_args!("should not try to coerce a {0} to a fn pointer", new_ty),
Location::caller())span_bug!(new.span, "should not try to coerce a {new_ty} to a fn pointer"),
1480 };
1481for expr in exprs.iter() {
1482self.apply_adjustments(
1483 expr,
1484::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[Adjustment { kind: prev_adjustment.clone(), target: fn_ptr }]))vec![Adjustment { kind: prev_adjustment.clone(), target: fn_ptr }],
1485 );
1486 }
1487self.apply_adjustments(new, ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[Adjustment { kind: next_adjustment, target: fn_ptr }]))vec![Adjustment { kind: next_adjustment, target: fn_ptr }]);
1488return Ok(fn_ptr);
1489 }
14901491// Configure a Coerce instance to compute the LUB.
1492 // We don't allow two-phase borrows on any autorefs this creates since we
1493 // probably aren't processing function arguments here and even if we were,
1494 // they're going to get autorefed again anyway and we can apply 2-phase borrows
1495 // at that time.
1496 //
1497 // NOTE: we set `coerce_never` to `true` here because coercion LUBs only
1498 // operate on values and not places, so a never coercion is valid.
1499let mut coerce = Coerce::new(self, cause.clone(), AllowTwoPhase::No, true);
1500coerce.use_lub = true;
15011502// First try to coerce the new expression to the type of the previous ones,
1503 // but only if the new expression has no coercion already applied to it.
1504let mut first_error = None;
1505if !self.typeck_results.borrow().adjustments().contains_key(new.hir_id) {
1506let result = self.commit_if_ok(|_| coerce.coerce(new_ty, prev_ty));
1507match result {
1508Ok(ok) => {
1509let (adjustments, target) = self.register_infer_ok_obligations(ok);
1510self.apply_adjustments(new, adjustments);
1511{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:1511",
"rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
::tracing_core::__macro_support::Option::Some(1511u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("coercion::try_find_coercion_lub: was able to coerce from new type {0:?} to previous type {1:?} ({2:?})",
new_ty, prev_ty, target) as &dyn ::tracing::field::Value))])
});
} else { ; }
};debug!(
1512"coercion::try_find_coercion_lub: was able to coerce from new type {:?} to previous type {:?} ({:?})",
1513 new_ty, prev_ty, target
1514 );
1515return Ok(target);
1516 }
1517Err(e) => first_error = Some(e),
1518 }
1519 }
15201521let ok = self
1522.commit_if_ok(|_| coerce.coerce(prev_ty, new_ty))
1523// Avoid giving strange errors on failed attempts.
1524.map_err(|e| first_error.unwrap_or(e))?;
15251526let (adjustments, target) = self.register_infer_ok_obligations(ok);
1527for expr in exprs {
1528self.apply_adjustments(expr, adjustments.clone());
1529 }
1530{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:1530",
"rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
::tracing_core::__macro_support::Option::Some(1530u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("coercion::try_find_coercion_lub: was able to coerce previous type {0:?} to new type {1:?} ({2:?})",
prev_ty, new_ty, target) as &dyn ::tracing::field::Value))])
});
} else { ; }
};debug!(
1531"coercion::try_find_coercion_lub: was able to coerce previous type {:?} to new type {:?} ({:?})",
1532 prev_ty, new_ty, target
1533 );
1534Ok(target)
1535 }
1536}
15371538/// Check whether `ty` can be coerced to `output_ty`.
1539/// Used from clippy.
1540pub fn can_coerce<'tcx>(
1541 tcx: TyCtxt<'tcx>,
1542 param_env: ty::ParamEnv<'tcx>,
1543 body_def_id: LocalDefId,
1544 ty: Ty<'tcx>,
1545 output_ty: Ty<'tcx>,
1546) -> bool {
1547let root_ctxt = crate::typeck_root_ctxt::TypeckRootCtxt::new(tcx, body_def_id);
1548let fn_ctxt = FnCtxt::new(&root_ctxt, param_env, body_def_id);
1549fn_ctxt.may_coerce(ty, output_ty)
1550}
15511552/// CoerceMany encapsulates the pattern you should use when you have
1553/// many expressions that are all getting coerced to a common
1554/// type. This arises, for example, when you have a match (the result
1555/// of each arm is coerced to a common type). It also arises in less
1556/// obvious places, such as when you have many `break foo` expressions
1557/// that target the same loop, or the various `return` expressions in
1558/// a function.
1559///
1560/// The basic protocol is as follows:
1561///
1562/// - Instantiate the `CoerceMany` with an initial `expected_ty`.
1563/// This will also serve as the "starting LUB". The expectation is
1564/// that this type is something which all of the expressions *must*
1565/// be coercible to. Use a fresh type variable if needed.
1566/// - For each expression whose result is to be coerced, invoke `coerce()` with.
1567/// - In some cases we wish to coerce "non-expressions" whose types are implicitly
1568/// unit. This happens for example if you have a `break` with no expression,
1569/// or an `if` with no `else`. In that case, invoke `coerce_forced_unit()`.
1570/// - `coerce()` and `coerce_forced_unit()` may report errors. They hide this
1571/// from you so that you don't have to worry your pretty head about it.
1572/// But if an error is reported, the final type will be `err`.
1573/// - Invoking `coerce()` may cause us to go and adjust the "adjustments" on
1574/// previously coerced expressions.
1575/// - When all done, invoke `complete()`. This will return the LUB of
1576/// all your expressions.
1577/// - WARNING: I don't believe this final type is guaranteed to be
1578/// related to your initial `expected_ty` in any particular way,
1579/// although it will typically be a subtype, so you should check it.
1580/// Check the note below for more details.
1581/// - Invoking `complete()` may cause us to go and adjust the "adjustments" on
1582/// previously coerced expressions.
1583///
1584/// Example:
1585///
1586/// ```ignore (illustrative)
1587/// let mut coerce = CoerceMany::new(expected_ty);
1588/// for expr in exprs {
1589/// let expr_ty = fcx.check_expr_with_expectation(expr, expected);
1590/// coerce.coerce(fcx, &cause, expr, expr_ty);
1591/// }
1592/// let final_ty = coerce.complete(fcx);
1593/// ```
1594///
1595/// NOTE: Why does the `expected_ty` participate in the LUB?
1596/// When coercing, each branch should use the following expectations for type inference:
1597/// - The branch can be coerced to the expected type of the match/if/whatever.
1598/// - The branch can be coercion lub'd with the types of the previous branches.
1599/// Ideally we'd have some sort of `Expectation::ParticipatesInCoerceLub(ongoing_lub_ty, final_ty)`,
1600/// but adding and using this feels very challenging.
1601/// What we instead do is to use the expected type of the match/if/whatever as
1602/// the initial coercion lub. This allows us to use the lub of "expected type of match" with
1603/// "types from previous branches" as the coercion target, which can contains both expectations.
1604///
1605/// Two concerns with this approach:
1606/// - We may have incompatible `final_ty` if that lub is different from the expected
1607/// type of the match. However, in this case coercing the final type of the
1608/// `CoerceMany` to its expected type would have error'd anyways, so we don't care.
1609/// - We may constrain the `expected_ty` too early. For some branches with
1610/// type `a` and `b`, we end up with `(a lub expected_ty) lub b` instead of
1611/// `(a lub b) lub expected_ty`. They should be the same type. However,
1612/// `a lub expected_ty` may constrain inference variables in `expected_ty`.
1613/// In this case the difference does matter and we get actually incorrect results.
1614/// FIXME: Ideally we'd compute the final type without unnecessarily constraining
1615/// the expected type of the match when computing the types of its branches.
1616pub(crate) struct CoerceMany<'tcx> {
1617 expected_ty: Ty<'tcx>,
1618 final_ty: Option<Ty<'tcx>>,
1619 expressions: Vec<&'tcx hir::Expr<'tcx>>,
1620}
16211622impl<'tcx> CoerceMany<'tcx> {
1623/// Creates a `CoerceMany` with a default capacity of 1. If the full set of
1624 /// coercion sites is known before hand, consider `with_capacity()` instead
1625 /// to avoid allocation.
1626pub(crate) fn new(expected_ty: Ty<'tcx>) -> Self {
1627Self::with_capacity(expected_ty, 1)
1628 }
16291630/// Creates a `CoerceMany` with a given capacity.
1631pub(crate) fn with_capacity(expected_ty: Ty<'tcx>, capacity: usize) -> Self {
1632CoerceMany { expected_ty, final_ty: None, expressions: Vec::with_capacity(capacity) }
1633 }
16341635/// Returns the "expected type" with which this coercion was
1636 /// constructed. This represents the "downward propagated" type
1637 /// that was given to us at the start of typing whatever construct
1638 /// we are typing (e.g., the match expression).
1639 ///
1640 /// Typically, this is used as the expected type when
1641 /// type-checking each of the alternative expressions whose types
1642 /// we are trying to merge.
1643pub(crate) fn expected_ty(&self) -> Ty<'tcx> {
1644self.expected_ty
1645 }
16461647/// Returns the current "merged type", representing our best-guess
1648 /// at the LUB of the expressions we've seen so far (if any). This
1649 /// isn't *final* until you call `self.complete()`, which will return
1650 /// the merged type.
1651pub(crate) fn merged_ty(&self) -> Ty<'tcx> {
1652self.final_ty.unwrap_or(self.expected_ty)
1653 }
16541655/// Indicates that the value generated by `expression`, which is
1656 /// of type `expression_ty`, is one of the possibilities that we
1657 /// could coerce from. This will record `expression`, and later
1658 /// calls to `coerce` may come back and add adjustments and things
1659 /// if necessary.
1660pub(crate) fn coerce<'a>(
1661&mut self,
1662 fcx: &FnCtxt<'a, 'tcx>,
1663 cause: &ObligationCause<'tcx>,
1664 expression: &'tcx hir::Expr<'tcx>,
1665 expression_ty: Ty<'tcx>,
1666 ) {
1667self.coerce_inner(fcx, cause, Some(expression), expression_ty, |_| {}, false)
1668 }
16691670/// Indicates that one of the inputs is a "forced unit". This
1671 /// occurs in a case like `if foo { ... };`, where the missing else
1672 /// generates a "forced unit". Another example is a `loop { break;
1673 /// }`, where the `break` has no argument expression. We treat
1674 /// these cases slightly differently for error-reporting
1675 /// purposes. Note that these tend to correspond to cases where
1676 /// the `()` expression is implicit in the source, and hence we do
1677 /// not take an expression argument.
1678 ///
1679 /// The `augment_error` gives you a chance to extend the error
1680 /// message, in case any results (e.g., we use this to suggest
1681 /// removing a `;`).
1682pub(crate) fn coerce_forced_unit<'a>(
1683&mut self,
1684 fcx: &FnCtxt<'a, 'tcx>,
1685 cause: &ObligationCause<'tcx>,
1686 augment_error: impl FnOnce(&mut Diag<'_>),
1687 label_unit_as_expected: bool,
1688 ) {
1689self.coerce_inner(
1690fcx,
1691cause,
1692None,
1693fcx.tcx.types.unit,
1694augment_error,
1695label_unit_as_expected,
1696 )
1697 }
16981699/// The inner coercion "engine". If `expression` is `None`, this
1700 /// is a forced-unit case, and hence `expression_ty` must be
1701 /// `Nil`.
1702{}
#[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("coerce_inner",
"rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
::tracing_core::__macro_support::Option::Some(1702u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
::tracing_core::field::FieldSet::new(&[{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("cause")
}> =
::tracing::__macro_support::FieldName::new("cause");
NAME.as_str()
},
{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("expression")
}> =
::tracing::__macro_support::FieldName::new("expression");
NAME.as_str()
},
{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("expression_ty")
}> =
::tracing::__macro_support::FieldName::new("expression_ty");
NAME.as_str()
}], ::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
meta.fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&cause)
as &dyn ::tracing::field::Value)),
(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&expression)
as &dyn ::tracing::field::Value)),
(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&expression_ty)
as &dyn ::tracing::field::Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: () = loop {};
return __tracing_attr_fake_return;
}
{
if expression_ty.is_ty_var() {
expression_ty = fcx.infcx.shallow_resolve(expression_ty);
}
if let Err(guar) =
(expression_ty, self.merged_ty()).error_reported() {
self.final_ty = Some(Ty::new_error(fcx.tcx, guar));
return;
}
let (expected, found) =
if label_expression_as_expected {
(expression_ty, self.merged_ty())
} else { (self.merged_ty(), expression_ty) };
let result =
if let Some(expression) = expression {
if self.expressions.is_empty() {
fcx.coerce(expression, expression_ty, self.expected_ty,
AllowTwoPhase::No, Some(cause.clone()))
} else {
fcx.try_find_coercion_lub(cause, &self.expressions,
self.merged_ty(), expression, expression_ty)
}
} else {
if !expression_ty.is_unit() {
{
::core::panicking::panic_fmt(format_args!("if let hack without unit type"));
}
};
fcx.at(cause,
fcx.param_env).eq(DefineOpaqueTypes::Yes, expected,
found).map(|infer_ok|
{
fcx.register_infer_ok_obligations(infer_ok);
expression_ty
})
};
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs:1793",
"rustc_hir_typeck::coercion", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/6905905bb7727fd0f9223f5fdc9ad4e62236d5a2/compiler/rustc_hir_typeck/src/coercion.rs"),
::tracing_core::__macro_support::Option::Some(1793u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::coercion"),
::tracing_core::field::FieldSet::new(&[{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("result")
}> =
::tracing::__macro_support::FieldName::new("result");
NAME.as_str()
}], ::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&result)
as &dyn ::tracing::field::Value))])
});
} else { ; }
};
match result {
Ok(v) => {
self.final_ty = Some(v);
if let Some(e) = expression { self.expressions.push(e); }
}
Err(coercion_error) => {
fcx.set_tainted_by_errors(fcx.dcx().span_delayed_bug(cause.span,
"coercion error but no error emitted"));
let (expected, found) =
fcx.deeply_resolve_ignoring_regions((expected, found));
let mut err;
let mut unsized_return = false;
match *cause.code() {
ObligationCauseCode::ReturnNoExpression => {
err =
{
fcx.dcx().struct_span_err(cause.span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`return;` in a function whose return type is not `()`"))
})).with_code(E0069)
};
if let Some(value) =
fcx.err_ctxt().ty_kind_suggestion(fcx.param_env, found) {
err.span_suggestion_verbose(cause.span.shrink_to_hi(),
"give the `return` a value of the expected type",
::alloc::__export::must_use({
::alloc::fmt::format(format_args!(" {0}", value))
}), Applicability::HasPlaceholders);
}
err.span_label(cause.span, "return type is not `()`");
}
ObligationCauseCode::BlockTailExpression(blk_id, ..) => {
err =
self.report_return_mismatched_types(cause, expected, found,
coercion_error, fcx, blk_id, expression);
unsized_return = self.is_return_ty_definitely_unsized(fcx);
}
ObligationCauseCode::ReturnValue(return_expr_id) => {
err =
self.report_return_mismatched_types(cause, expected, found,
coercion_error, fcx, return_expr_id, expression);
unsized_return = self.is_return_ty_definitely_unsized(fcx);
}
ObligationCauseCode::MatchExpressionArm(MatchExpressionArmCause {
arm_span,
arm_ty,
prior_arm_ty,
ref prior_non_diverging_arms,
tail_defines_return_position_impl_trait: Some(rpit_def_id),
.. }) => {
err =
fcx.err_ctxt().report_mismatched_types(cause, fcx.param_env,
expected, found, coercion_error);
if prior_non_diverging_arms.len() > 0 {
self.suggest_boxing_tail_for_return_position_impl_trait(fcx,
&mut err, rpit_def_id, arm_ty, prior_arm_ty,
prior_non_diverging_arms.iter().chain(std::iter::once(&arm_span)).copied());
}
}
ObligationCauseCode::IfExpression {
expr_id,
tail_defines_return_position_impl_trait: Some(rpit_def_id) }
=> {
let hir::Node::Expr(hir::Expr {
kind: hir::ExprKind::If(_, then_expr, Some(else_expr)), ..
}) =
fcx.tcx.hir_node(expr_id) else {
::core::panicking::unreachable_default_msg();
};
err =
fcx.err_ctxt().report_mismatched_types(cause, fcx.param_env,
expected, found, coercion_error);
let then_span =
fcx.find_block_span_from_hir_id(then_expr.hir_id);
let else_span =
fcx.find_block_span_from_hir_id(else_expr.hir_id);
if then_span != then_expr.span &&
else_span != else_expr.span {
let then_ty =
fcx.typeck_results.borrow().expr_ty(then_expr);
let else_ty =
fcx.typeck_results.borrow().expr_ty(else_expr);
self.suggest_boxing_tail_for_return_position_impl_trait(fcx,
&mut err, rpit_def_id, then_ty, else_ty,
[then_span, else_span].into_iter());
}
}
_ => {
err =
fcx.err_ctxt().report_mismatched_types(cause, fcx.param_env,
expected, found, coercion_error);
}
}
augment_error(&mut err);
if let Some(expr) = expression {
if let hir::ExprKind::Loop(block, _, loop_src @
(hir::LoopSource::While | hir::LoopSource::ForLoop), _) =
expr.kind {
let loop_type =
if loop_src == hir::LoopSource::While {
"`while` loops"
} else { "`for` loops" };
err.note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} evaluate to unit type `()`",
loop_type))
}));
if loop_src == hir::LoopSource::While &&
let Some(pat) = irrefutable_if_let_expr(block) {
err.span_label(pat.span,
"this pattern always matches, consider using `loop` instead");
}
}
fcx.emit_coerce_suggestions(&mut err, expr, found, expected,
None, Some(coercion_error));
}
let guar = err.emit_err_unless_delay(unsized_return);
self.final_ty = Some(Ty::new_error(fcx.tcx, guar));
}
}
}
}
}#[instrument(skip(self, fcx, augment_error, label_expression_as_expected), level = "debug")]1703pub(crate) fn coerce_inner<'a>(
1704&mut self,
1705 fcx: &FnCtxt<'a, 'tcx>,
1706 cause: &ObligationCause<'tcx>,
1707 expression: Option<&'tcx hir::Expr<'tcx>>,
1708mut expression_ty: Ty<'tcx>,
1709 augment_error: impl FnOnce(&mut Diag<'_>),
1710 label_expression_as_expected: bool,
1711 ) {
1712// Incorporate whatever type inference information we have
1713 // until now; in principle we might also want to process
1714 // pending obligations, but doing so should only improve
1715 // compatibility (hopefully that is true) by helping us
1716 // uncover never types better.
1717if expression_ty.is_ty_var() {
1718 expression_ty = fcx.infcx.shallow_resolve(expression_ty);
1719 }
17201721// If we see any error types, just propagate that error
1722 // upwards.
1723if let Err(guar) = (expression_ty, self.merged_ty()).error_reported() {
1724self.final_ty = Some(Ty::new_error(fcx.tcx, guar));
1725return;
1726 }
17271728let (expected, found) = if label_expression_as_expected {
1729// In the case where this is a "forced unit", like
1730 // `break`, we want to call the `()` "expected"
1731 // since it is implied by the syntax.
1732 // (Note: not all force-units work this way.)"
1733(expression_ty, self.merged_ty())
1734 } else {
1735// Otherwise, the "expected" type for error
1736 // reporting is the current unification type,
1737 // which is basically the LUB of the expressions
1738 // we've seen so far (combined with the expected
1739 // type)
1740(self.merged_ty(), expression_ty)
1741 };
17421743// Handle the actual type unification etc.
1744let result = if let Some(expression) = expression {
1745if self.expressions.is_empty() {
1746// Special-case the first expression we are coercing.
1747 // To be honest, I'm not entirely sure why we do this.
1748 // We don't allow two-phase borrows, see comment in try_find_coercion_lub for why
1749fcx.coerce(
1750 expression,
1751 expression_ty,
1752self.expected_ty,
1753 AllowTwoPhase::No,
1754Some(cause.clone()),
1755 )
1756 } else {
1757 fcx.try_find_coercion_lub(
1758 cause,
1759&self.expressions,
1760self.merged_ty(),
1761 expression,
1762 expression_ty,
1763 )
1764 }
1765 } else {
1766// this is a hack for cases where we default to `()` because
1767 // the expression etc has been omitted from the source. An
1768 // example is an `if let` without an else:
1769 //
1770 // if let Some(x) = ... { }
1771 //
1772 // we wind up with a second match arm that is like `_ =>
1773 // ()`. That is the case we are considering here. We take
1774 // a different path to get the right "expected, found"
1775 // message and so forth (and because we know that
1776 // `expression_ty` will be unit).
1777 //
1778 // Another example is `break` with no argument expression.
1779assert!(expression_ty.is_unit(), "if let hack without unit type");
1780 fcx.at(cause, fcx.param_env)
1781 .eq(
1782// needed for tests/ui/type-alias-impl-trait/issue-65679-inst-opaque-ty-from-val-twice.rs
1783DefineOpaqueTypes::Yes,
1784 expected,
1785 found,
1786 )
1787 .map(|infer_ok| {
1788 fcx.register_infer_ok_obligations(infer_ok);
1789 expression_ty
1790 })
1791 };
17921793debug!(?result);
1794match result {
1795Ok(v) => {
1796self.final_ty = Some(v);
1797if let Some(e) = expression {
1798self.expressions.push(e);
1799 }
1800 }
1801Err(coercion_error) => {
1802// Mark that we've failed to coerce the types here to suppress
1803 // any superfluous errors we might encounter while trying to
1804 // emit or provide suggestions on how to fix the initial error.
1805fcx.set_tainted_by_errors(
1806 fcx.dcx().span_delayed_bug(cause.span, "coercion error but no error emitted"),
1807 );
1808let (expected, found) = fcx.deeply_resolve_ignoring_regions((expected, found));
18091810let mut err;
1811let mut unsized_return = false;
1812match *cause.code() {
1813 ObligationCauseCode::ReturnNoExpression => {
1814 err = struct_span_code_err!(
1815 fcx.dcx(),
1816 cause.span,
1817 E0069,
1818"`return;` in a function whose return type is not `()`"
1819);
1820if let Some(value) = fcx.err_ctxt().ty_kind_suggestion(fcx.param_env, found)
1821 {
1822 err.span_suggestion_verbose(
1823 cause.span.shrink_to_hi(),
1824"give the `return` a value of the expected type",
1825format!(" {value}"),
1826 Applicability::HasPlaceholders,
1827 );
1828 }
1829 err.span_label(cause.span, "return type is not `()`");
1830 }
1831 ObligationCauseCode::BlockTailExpression(blk_id, ..) => {
1832 err = self.report_return_mismatched_types(
1833 cause,
1834 expected,
1835 found,
1836 coercion_error,
1837 fcx,
1838 blk_id,
1839 expression,
1840 );
1841 unsized_return = self.is_return_ty_definitely_unsized(fcx);
1842 }
1843 ObligationCauseCode::ReturnValue(return_expr_id) => {
1844 err = self.report_return_mismatched_types(
1845 cause,
1846 expected,
1847 found,
1848 coercion_error,
1849 fcx,
1850 return_expr_id,
1851 expression,
1852 );
1853 unsized_return = self.is_return_ty_definitely_unsized(fcx);
1854 }
1855 ObligationCauseCode::MatchExpressionArm(MatchExpressionArmCause {
1856 arm_span,
1857 arm_ty,
1858 prior_arm_ty,
1859ref prior_non_diverging_arms,
1860 tail_defines_return_position_impl_trait: Some(rpit_def_id),
1861 ..
1862 }) => {
1863 err = fcx.err_ctxt().report_mismatched_types(
1864 cause,
1865 fcx.param_env,
1866 expected,
1867 found,
1868 coercion_error,
1869 );
1870// Check that we're actually in the second or later arm
1871if prior_non_diverging_arms.len() > 0 {
1872self.suggest_boxing_tail_for_return_position_impl_trait(
1873 fcx,
1874&mut err,
1875 rpit_def_id,
1876 arm_ty,
1877 prior_arm_ty,
1878 prior_non_diverging_arms
1879 .iter()
1880 .chain(std::iter::once(&arm_span))
1881 .copied(),
1882 );
1883 }
1884 }
1885 ObligationCauseCode::IfExpression {
1886 expr_id,
1887 tail_defines_return_position_impl_trait: Some(rpit_def_id),
1888 } => {
1889let hir::Node::Expr(hir::Expr {
1890 kind: hir::ExprKind::If(_, then_expr, Some(else_expr)),
1891 ..
1892 }) = fcx.tcx.hir_node(expr_id)
1893else {
1894unreachable!();
1895 };
1896 err = fcx.err_ctxt().report_mismatched_types(
1897 cause,
1898 fcx.param_env,
1899 expected,
1900 found,
1901 coercion_error,
1902 );
1903let then_span = fcx.find_block_span_from_hir_id(then_expr.hir_id);
1904let else_span = fcx.find_block_span_from_hir_id(else_expr.hir_id);
1905// Don't suggest wrapping whole block in `Box::new`.
1906if then_span != then_expr.span && else_span != else_expr.span {
1907let then_ty = fcx.typeck_results.borrow().expr_ty(then_expr);
1908let else_ty = fcx.typeck_results.borrow().expr_ty(else_expr);
1909self.suggest_boxing_tail_for_return_position_impl_trait(
1910 fcx,
1911&mut err,
1912 rpit_def_id,
1913 then_ty,
1914 else_ty,
1915 [then_span, else_span].into_iter(),
1916 );
1917 }
1918 }
1919_ => {
1920 err = fcx.err_ctxt().report_mismatched_types(
1921 cause,
1922 fcx.param_env,
1923 expected,
1924 found,
1925 coercion_error,
1926 );
1927 }
1928 }
19291930 augment_error(&mut err);
19311932if let Some(expr) = expression {
1933if let hir::ExprKind::Loop(
1934 block,
1935_,
1936 loop_src @ (hir::LoopSource::While | hir::LoopSource::ForLoop),
1937_,
1938 ) = expr.kind
1939 {
1940let loop_type = if loop_src == hir::LoopSource::While {
1941"`while` loops"
1942} else {
1943"`for` loops"
1944};
19451946 err.note(format!("{loop_type} evaluate to unit type `()`"));
1947if loop_src == hir::LoopSource::While
1948 && let Some(pat) = irrefutable_if_let_expr(block)
1949 {
1950 err.span_label(
1951 pat.span,
1952"this pattern always matches, consider using `loop` instead",
1953 );
1954 }
1955 }
19561957 fcx.emit_coerce_suggestions(
1958&mut err,
1959 expr,
1960 found,
1961 expected,
1962None,
1963Some(coercion_error),
1964 );
1965 }
19661967let guar = err.emit_err_unless_delay(unsized_return);
19681969self.final_ty = Some(Ty::new_error(fcx.tcx, guar));
1970 }
1971 }
1972 }
19731974fn suggest_boxing_tail_for_return_position_impl_trait(
1975&self,
1976 fcx: &FnCtxt<'_, 'tcx>,
1977 err: &mut Diag<'_>,
1978 rpit_def_id: LocalDefId,
1979 a_ty: Ty<'tcx>,
1980 b_ty: Ty<'tcx>,
1981 arm_spans: impl Iterator<Item = Span>,
1982 ) {
1983let compatible = |ty: Ty<'tcx>| {
1984fcx.probe(|_| {
1985let ocx = ObligationCtxt::new(fcx);
1986ocx.register_obligations(
1987fcx.tcx
1988 .item_self_bounds(rpit_def_id)
1989 .iter_identity()
1990 .map(Unnormalized::skip_norm_wip)
1991 .filter_map(|clause| {
1992let predicate = clause
1993 .kind()
1994 .map_bound(|clause| match clause {
1995 ty::ClauseKind::Trait(trait_pred) => {
1996Some(ty::ClauseKind::Trait(
1997 trait_pred.with_replaced_self_ty(fcx.tcx, ty),
1998 ))
1999 }
2000 ty::ClauseKind::Projection(proj_pred) => {
2001Some(ty::ClauseKind::Projection(
2002 proj_pred.with_replaced_self_ty(fcx.tcx, ty),
2003 ))
2004 }
2005_ => None,
2006 })
2007 .transpose()?;
2008Some(Obligation::new(
2009fcx.tcx,
2010ObligationCause::dummy(),
2011fcx.param_env,
2012predicate,
2013 ))
2014 }),
2015 );
2016ocx.try_evaluate_obligations().no_errors()
2017 })
2018 };
20192020if !compatible(a_ty) || !compatible(b_ty) {
2021return;
2022 }
20232024let rpid_def_span = fcx.tcx.def_span(rpit_def_id);
2025err.subdiagnostic(SuggestBoxingForReturnImplTrait::ChangeReturnType {
2026 start_sp: rpid_def_span.with_hi(rpid_def_span.lo() + BytePos(4)),
2027 end_sp: rpid_def_span.shrink_to_hi(),
2028 });
20292030let (starts, ends) =
2031arm_spans.map(|span| (span.shrink_to_lo(), span.shrink_to_hi())).unzip();
2032err.subdiagnostic(SuggestBoxingForReturnImplTrait::BoxReturnExpr { starts, ends });
2033 }
20342035fn report_return_mismatched_types<'infcx>(
2036&self,
2037 cause: &ObligationCause<'tcx>,
2038 expected: Ty<'tcx>,
2039 found: Ty<'tcx>,
2040 ty_err: TypeError<'tcx>,
2041 fcx: &'infcx FnCtxt<'_, 'tcx>,
2042 block_or_return_id: hir::HirId,
2043 expression: Option<&'tcx hir::Expr<'tcx>>,
2044 ) -> Diag<'infcx> {
2045let mut err =
2046fcx.err_ctxt().report_mismatched_types(cause, fcx.param_env, expected, found, ty_err);
20472048let due_to_block = #[allow(non_exhaustive_omitted_patterns)] match fcx.tcx.hir_node(block_or_return_id)
{
hir::Node::Block(..) => true,
_ => false,
}matches!(fcx.tcx.hir_node(block_or_return_id), hir::Node::Block(..));
2049let parent = fcx.tcx.parent_hir_node(block_or_return_id);
2050if let Some(expr) = expression2051 && let hir::Node::Expr(&hir::Expr {
2052 kind: hir::ExprKind::Closure(&hir::Closure { body, .. }),
2053 ..
2054 }) = parent2055 {
2056let needs_block =
2057 !#[allow(non_exhaustive_omitted_patterns)] match fcx.tcx.hir_body(body).value.kind
{
hir::ExprKind::Block(..) => true,
_ => false,
}matches!(fcx.tcx.hir_body(body).value.kind, hir::ExprKind::Block(..));
2058fcx.suggest_missing_semicolon(&mut err, expr, expected, needs_block, true);
2059 }
2060// Verify that this is a tail expression of a function, otherwise the
2061 // label pointing out the cause for the type coercion will be wrong
2062 // as prior return coercions would not be relevant (#57664).
2063if let Some(expr) = expression2064 && due_to_block2065 {
2066fcx.suggest_missing_semicolon(&mut err, expr, expected, false, false);
2067let pointing_at_return_type = fcx.suggest_mismatched_types_on_tail(
2068&mut err,
2069expr,
2070expected,
2071found,
2072block_or_return_id,
2073 );
2074if let Some(cond_expr) = fcx.tcx.hir_get_if_cause(expr.hir_id)
2075 && expected.is_unit()
2076 && !pointing_at_return_type2077// If the block is from an external macro or try (`?`) desugaring, then
2078 // do not suggest adding a semicolon, because there's nowhere to put it.
2079 // See issues #81943 and #87051.
2080 // Similarly, if the block is from a loop desugaring, then also do not
2081 // suggest adding a semicolon. See issue #150850.
2082&& cond_expr.span.desugaring_kind().is_none()
2083 && !cond_expr.span.in_external_macro(fcx.tcx.sess.source_map())
2084 && !#[allow(non_exhaustive_omitted_patterns)] match cond_expr.kind {
hir::ExprKind::Match(.., hir::MatchSource::TryDesugar(_)) => true,
_ => false,
}matches!(
2085 cond_expr.kind,
2086 hir::ExprKind::Match(.., hir::MatchSource::TryDesugar(_))
2087 )2088 {
2089if let ObligationCauseCode::BlockTailExpression(hir_id, hir::MatchSource::Normal) =
2090cause.code()
2091 && let hir::Node::Block(block) = fcx.tcx.hir_node(*hir_id)
2092 && let hir::Node::Expr(expr) = fcx.tcx.parent_hir_node(block.hir_id)
2093 && let hir::Node::Expr(if_expr) = fcx.tcx.parent_hir_node(expr.hir_id)
2094 && let hir::ExprKind::If(_cond, _then, None) = if_expr.kind
2095 {
2096err.span_label(
2097cond_expr.span,
2098"`if` expressions without `else` arms expect their inner expression to be `()`",
2099 );
2100 } else {
2101err.span_label(cond_expr.span, "expected this to be `()`");
2102 }
2103if expr.can_have_side_effects() {
2104// Don't suggest semicolon after if expressions as it does not fix the issue
2105if !#[allow(non_exhaustive_omitted_patterns)] match cond_expr.kind {
hir::ExprKind::If(..) => true,
_ => false,
}matches!(cond_expr.kind, hir::ExprKind::If(..)) {
2106fcx.suggest_semicolon_at_end(cond_expr.span, &mut err);
2107 }
2108 }
2109 }
2110 }
21112112// If this is due to an explicit `return`, suggest adding a return type.
2113if let Some((fn_id, fn_decl)) = fcx.get_fn_decl(block_or_return_id)
2114 && !due_to_block2115 {
2116fcx.suggest_missing_return_type(&mut err, fn_decl, expected, found, fn_id);
2117 }
21182119let is_return_position = fcx2120 .tcx
2121 .hir_get_fn_id_for_return_block(block_or_return_id)
2122 .is_some_and(|fn_id| fn_id == fcx.tcx.local_def_id_to_hir_id(fcx.body_def_id));
21232124// If this is due to a block, then maybe we forgot a `return`/`break`.
2125if due_to_block2126 && let Some(expr) = expression2127 && let Some(parent_fn_decl) =
2128fcx.tcx.hir_fn_decl_by_hir_id(fcx.tcx.local_def_id_to_hir_id(fcx.body_def_id))
2129 && !is_return_position2130 {
2131fcx.suggest_missing_break_or_return_expr(
2132&mut err,
2133expr,
2134parent_fn_decl,
2135expected,
2136found,
2137block_or_return_id,
2138fcx.body_def_id,
2139 );
2140 }
21412142if is_return_position2143 && let Some(sp) = fcx.ret_coercion_span.get()
2144// If the closure has an explicit return type annotation, or if
2145 // the closure's return type has been inferred from outside
2146 // requirements (such as an Fn* trait bound), then a type error
2147 // may occur at the first return expression we see in the closure
2148 // (if it conflicts with the declared return type). Skip adding a
2149 // note in this case, since it would be incorrect.
2150&& let Some(fn_sig) = fcx.fn_sig()
2151 && fn_sig.output().is_ty_var()
2152 {
2153err.span_note(sp, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("return type inferred to be `{0}` here",
expected))
})format!("return type inferred to be `{expected}` here"));
2154 }
21552156err2157 }
21582159/// Checks whether the return type is unsized via an obligation, which makes
2160 /// sure we consider `dyn Trait: Sized` where clauses, which are trivially
2161 /// false but technically valid for typeck.
2162fn is_return_ty_definitely_unsized(&self, fcx: &FnCtxt<'_, 'tcx>) -> bool {
2163if let Some(sig) = fcx.fn_sig() {
2164 !fcx.predicate_may_hold(&Obligation::new(
2165fcx.tcx,
2166ObligationCause::dummy(),
2167fcx.param_env,
2168 ty::TraitRef::new(
2169fcx.tcx,
2170fcx.tcx.require_lang_item(LangItem::Sized, DUMMY_SP),
2171 [sig.output()],
2172 ),
2173 ))
2174 } else {
2175false
2176}
2177 }
21782179pub(crate) fn complete<'a>(self, fcx: &FnCtxt<'a, 'tcx>) -> Ty<'tcx> {
2180if let Some(final_ty) = self.final_ty {
2181final_ty2182 } else {
2183// If we only had inputs that were of type `!` (or no
2184 // inputs at all), then the final type is `!`.
2185if !self.expressions.is_empty() {
::core::panicking::panic("assertion failed: self.expressions.is_empty()")
};assert!(self.expressions.is_empty());
2186fcx.tcx.types.never
2187 }
2188 }
2189}
21902191fn irrefutable_if_let_expr<'hir>(block: &hir::Block<'hir>) -> Option<&'hir hir::Pat<'hir>> {
2192let hir::ExprKind::If(cond, _, _) = block.expr?.kind else {
2193return None;
2194 };
2195let hir::ExprKind::Let(let_expr) = cond.kind else {
2196return None;
2197 };
2198simple_irrefutable_pattern(let_expr.pat).then_some(let_expr.pat)
2199}
22002201fn simple_irrefutable_pattern(pat: &hir::Pat<'_>) -> bool {
2202match pat.kind {
2203 hir::PatKind::Wild | hir::PatKind::Binding(_, _, _, None) => true,
2204 hir::PatKind::Tuple(pats, _) => pats.iter().all(simple_irrefutable_pattern),
2205_ => false,
2206 }
2207}
22082209/// Recursively visit goals to decide whether an unsizing is possible.
2210/// `Break`s when it isn't, and an error should be raised.
2211/// `Continue`s when an unsizing ok based on an implementation of the `Unsize` trait / lang item.
2212struct CoerceVisitor<'a, 'tcx> {
2213 fcx: &'a FnCtxt<'a, 'tcx>,
2214 span: Span,
2215/// Whether the coercion is impossible. If so we sometimes still try to
2216 /// coerce in these cases to emit better errors. This changes the behavior
2217 /// when hitting the recursion limit.
2218errored: bool,
2219}
22202221impl<'tcx> ProofTreeVisitor<'tcx> for CoerceVisitor<'_, 'tcx> {
2222type Result = ControlFlow<()>;
22232224fn span(&self) -> Span {
2225self.span
2226 }
22272228fn visit_goal(&mut self, goal: &inspect::InspectGoal<'_, 'tcx>) -> Self::Result {
2229let Some(pred) = goal.goal().predicate.as_trait_clause() else {
2230return ControlFlow::Continue(());
2231 };
22322233// Make sure this predicate is referring to either an `Unsize` or `CoerceUnsized` trait,
2234 // Otherwise there's nothing to do.
2235if !self.fcx.tcx.is_lang_item(pred.def_id(), LangItem::Unsize)
2236 && !self.fcx.tcx.is_lang_item(pred.def_id(), LangItem::CoerceUnsized)
2237 {
2238return ControlFlow::Continue(());
2239 }
22402241match goal.result() {
2242// If we prove the `Unsize` or `CoerceUnsized` goal, continue recursing.
2243Ok(Certainty::Yes) => ControlFlow::Continue(()),
2244Err(NoSolution) => {
2245self.errored = true;
2246// Even if we find no solution, continue recursing if we find a single candidate
2247 // for which we're shallowly certain it holds to get the right error source.
2248if let [only_candidate] = &goal.candidates()[..]
2249 && only_candidate.shallow_certainty() == Certainty::Yes2250 {
2251only_candidate.visit_nested_no_probe(self)
2252 } else {
2253 ControlFlow::Break(())
2254 }
2255 }
2256Ok(Certainty::Maybe(_)) => {
2257// FIXME: structurally normalize?
2258if self.fcx.tcx.is_lang_item(pred.def_id(), LangItem::Unsize)
2259 && let ty::Dynamic(..) = pred.skip_binder().trait_ref.args.type_at(1).kind()
2260 && let ty::Infer(ty::TyVar(vid)) = *pred.self_ty().skip_binder().kind()
2261 && self.fcx.type_var_is_sized(vid)
2262 {
2263// We get here when trying to unsize a type variable to a `dyn Trait`,
2264 // knowing that that variable is sized. Unsizing definitely has to happen in that case.
2265 // If the variable weren't sized, we may not need an unsizing coercion.
2266 // In general, we don't want to add coercions too eagerly since it makes error messages much worse.
2267ControlFlow::Continue(())
2268 } else if let Some(cand) = goal.unique_applicable_candidate()
2269 && cand.shallow_certainty() == Certainty::Yes2270 {
2271cand.visit_nested_no_probe(self)
2272 } else {
2273 ControlFlow::Break(())
2274 }
2275 }
2276 }
2277 }
22782279fn on_recursion_limit(&mut self) -> Self::Result {
2280if self.errored {
2281// This prevents accidentally committing unfulfilled unsized coercions while trying to
2282 // find the error source for diagnostics.
2283 // See https://github.com/rust-lang/trait-system-refactor-initiative/issues/266.
2284ControlFlow::Break(())
2285 } else {
2286 ControlFlow::Continue(())
2287 }
2288 }
2289}