1//! Code for type-checking closure expressions.
23use std::iter;
4use std::ops::ControlFlow;
56use rustc_abi::ExternAbi;
7use rustc_errors::ErrorGuaranteed;
8use rustc_hiras hir;
9use rustc_hir::lang_items::LangItem;
10use rustc_hir_analysis::hir_ty_lowering::HirTyLowerer;
11use rustc_infer::infer::{BoundRegionConversionTime, DefineOpaqueTypes, InferOk, InferResult};
12use rustc_infer::traits::{ObligationCauseCode, PredicateObligations};
13use rustc_macros::{TypeFoldable, TypeVisitable};
14use rustc_middle::span_bug;
15use rustc_middle::ty::{
16self, ClosureKind, GenericArgs, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable,
17TypeVisitableExt, TypeVisitor,
18};
19use rustc_span::def_id::LocalDefId;
20use rustc_span::{DUMMY_SP, Span};
21use rustc_trait_selection::error_reporting::traits::ArgKind;
22use rustc_trait_selection::traits;
23use tracing::{debug, instrument, trace};
2425use super::{CoroutineTypes, Expectation, FnCtxt, check_fn};
2627/// What signature do we *expect* the closure to have from context?
28#[derive(#[automatically_derived]
impl<'tcx> ::core::fmt::Debug for ExpectedSig<'tcx> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field2_finish(f, "ExpectedSig",
"cause_span", &self.cause_span, "sig", &&self.sig)
}
}Debug, #[automatically_derived]
impl<'tcx> ::core::clone::Clone for ExpectedSig<'tcx> {
#[inline]
fn clone(&self) -> ExpectedSig<'tcx> {
ExpectedSig {
cause_span: ::core::clone::Clone::clone(&self.cause_span),
sig: ::core::clone::Clone::clone(&self.sig),
}
}
}Clone, const _: () =
{
impl<'tcx>
::rustc_middle::ty::TypeFoldable<::rustc_middle::ty::TyCtxt<'tcx>>
for ExpectedSig<'tcx> {
fn try_fold_with<__F: ::rustc_middle::ty::FallibleTypeFolder<::rustc_middle::ty::TyCtxt<'tcx>>>(self,
__folder: &mut __F) -> Result<Self, __F::Error> {
Ok(match self {
ExpectedSig { cause_span: __binding_0, sig: __binding_1 } =>
{
ExpectedSig {
cause_span: ::rustc_middle::ty::TypeFoldable::try_fold_with(__binding_0,
__folder)?,
sig: ::rustc_middle::ty::TypeFoldable::try_fold_with(__binding_1,
__folder)?,
}
}
})
}
fn fold_with<__F: ::rustc_middle::ty::TypeFolder<::rustc_middle::ty::TyCtxt<'tcx>>>(self,
__folder: &mut __F) -> Self {
match self {
ExpectedSig { cause_span: __binding_0, sig: __binding_1 } =>
{
ExpectedSig {
cause_span: ::rustc_middle::ty::TypeFoldable::fold_with(__binding_0,
__folder),
sig: ::rustc_middle::ty::TypeFoldable::fold_with(__binding_1,
__folder),
}
}
}
}
}
};TypeFoldable, const _: () =
{
impl<'tcx>
::rustc_middle::ty::TypeVisitable<::rustc_middle::ty::TyCtxt<'tcx>>
for ExpectedSig<'tcx> {
fn visit_with<__V: ::rustc_middle::ty::TypeVisitor<::rustc_middle::ty::TyCtxt<'tcx>>>(&self,
__visitor: &mut __V) -> __V::Result {
match *self {
ExpectedSig {
cause_span: ref __binding_0, sig: ref __binding_1 } => {
{
match ::rustc_middle::ty::VisitorResult::branch(::rustc_middle::ty::TypeVisitable::visit_with(__binding_0,
__visitor)) {
::core::ops::ControlFlow::Continue(()) => {}
::core::ops::ControlFlow::Break(r) => {
return ::rustc_middle::ty::VisitorResult::from_residual(r);
}
}
}
{
match ::rustc_middle::ty::VisitorResult::branch(::rustc_middle::ty::TypeVisitable::visit_with(__binding_1,
__visitor)) {
::core::ops::ControlFlow::Continue(()) => {}
::core::ops::ControlFlow::Break(r) => {
return ::rustc_middle::ty::VisitorResult::from_residual(r);
}
}
}
}
}
<__V::Result as ::rustc_middle::ty::VisitorResult>::output()
}
}
};TypeVisitable)]
29struct ExpectedSig<'tcx> {
30/// Span that gave us this expectation, if we know that.
31cause_span: Option<Span>,
32 sig: ty::PolyFnSig<'tcx>,
33}
3435#[derive(#[automatically_derived]
impl<'tcx> ::core::fmt::Debug for ClosureSignatures<'tcx> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field2_finish(f,
"ClosureSignatures", "bound_sig", &self.bound_sig,
"liberated_sig", &&self.liberated_sig)
}
}Debug)]
36struct ClosureSignatures<'tcx> {
37/// The signature users of the closure see.
38bound_sig: ty::PolyFnSig<'tcx>,
39/// The signature within the function body.
40 /// This mostly differs in the sense that lifetimes are now early bound and any
41 /// opaque types from the signature expectation are overridden in case there are
42 /// explicit hidden types written by the user in the closure signature.
43liberated_sig: ty::FnSig<'tcx>,
44}
4546impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
47#[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("check_expr_closure",
"rustc_hir_typeck::closure", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/closure.rs"),
::tracing_core::__macro_support::Option::Some(47u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::closure"),
::tracing_core::field::FieldSet::new(&["expr_span",
"expected"],
::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};
let mut iter = meta.fields().iter();
meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&expr_span)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&expected)
as &dyn 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: Ty<'tcx> = loop {};
return __tracing_attr_fake_return;
}
{
let tcx = self.tcx;
let body = tcx.hir_body(closure.body);
let expr_def_id = closure.def_id;
let (expected_sig, expected_kind) =
match expected.to_option(self) {
Some(ty) =>
self.deduce_closure_signature(self.try_structurally_resolve_type(expr_span,
ty), closure.kind),
None => (None, None),
};
let ClosureSignatures { bound_sig, mut liberated_sig } =
self.sig_of_closure(expr_def_id, closure.fn_decl,
closure.kind, expected_sig);
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/closure.rs:72",
"rustc_hir_typeck::closure", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/closure.rs"),
::tracing_core::__macro_support::Option::Some(72u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::closure"),
::tracing_core::field::FieldSet::new(&["bound_sig",
"liberated_sig"],
::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};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&bound_sig)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&liberated_sig)
as &dyn Value))])
});
} else { ; }
};
let parent_args =
GenericArgs::identity_for_item(tcx,
tcx.typeck_root_def_id(expr_def_id.to_def_id()));
let tupled_upvars_ty = self.next_ty_var(expr_span);
let (closure_ty, coroutine_types) =
match closure.kind {
hir::ClosureKind::Closure => {
let sig =
bound_sig.map_bound(|sig|
{
tcx.mk_fn_sig([Ty::new_tup(tcx, sig.inputs())],
sig.output(), sig.c_variadic, sig.safety, sig.abi)
});
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/closure.rs:97",
"rustc_hir_typeck::closure", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/closure.rs"),
::tracing_core::__macro_support::Option::Some(97u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::closure"),
::tracing_core::field::FieldSet::new(&["sig",
"expected_kind"],
::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};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&sig) as
&dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&expected_kind)
as &dyn Value))])
});
} else { ; }
};
let closure_kind_ty =
match expected_kind {
Some(kind) => Ty::from_closure_kind(tcx, kind),
None => self.next_ty_var(expr_span),
};
let closure_args =
ty::ClosureArgs::new(tcx,
ty::ClosureArgsParts {
parent_args,
closure_kind_ty,
closure_sig_as_fn_ptr_ty: Ty::new_fn_ptr(tcx, sig),
tupled_upvars_ty,
});
(Ty::new_closure(tcx, expr_def_id.to_def_id(),
closure_args.args), None)
}
hir::ClosureKind::Coroutine(kind) => {
let yield_ty =
match kind {
hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Gen,
_) | hir::CoroutineKind::Coroutine(_) => {
let yield_ty = self.next_ty_var(expr_span);
self.require_type_is_sized(yield_ty, expr_span,
ObligationCauseCode::SizedYieldType);
yield_ty
}
hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::AsyncGen,
_) => {
let yield_ty = self.next_ty_var(expr_span);
self.require_type_is_sized(yield_ty, expr_span,
ObligationCauseCode::SizedYieldType);
Ty::new_adt(tcx,
tcx.adt_def(tcx.require_lang_item(hir::LangItem::Poll,
expr_span)),
tcx.mk_args(&[Ty::new_adt(tcx,
tcx.adt_def(tcx.require_lang_item(hir::LangItem::Option,
expr_span)), tcx.mk_args(&[yield_ty.into()])).into()]))
}
hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Async,
_) => {
tcx.types.unit
}
};
let resume_ty =
liberated_sig.inputs().get(0).copied().unwrap_or(tcx.types.unit);
let kind_ty =
match kind {
hir::CoroutineKind::Desugared(_,
hir::CoroutineSource::Closure) => {
self.next_ty_var(expr_span)
}
_ => tcx.types.unit,
};
let coroutine_args =
ty::CoroutineArgs::new(tcx,
ty::CoroutineArgsParts {
parent_args,
kind_ty,
resume_ty,
yield_ty,
return_ty: liberated_sig.output(),
tupled_upvars_ty,
});
(Ty::new_coroutine(tcx, expr_def_id.to_def_id(),
coroutine_args.args),
Some(CoroutineTypes { resume_ty, yield_ty }))
}
hir::ClosureKind::CoroutineClosure(kind) => {
let (bound_return_ty, bound_yield_ty) =
match kind {
hir::CoroutineDesugaring::Gen => {
(tcx.types.unit, self.infcx.next_ty_var(expr_span))
}
hir::CoroutineDesugaring::Async => {
(bound_sig.skip_binder().output(), tcx.types.unit)
}
hir::CoroutineDesugaring::AsyncGen => {
{
::core::panicking::panic_fmt(format_args!("not yet implemented: {0}",
format_args!("`async gen` closures not supported yet")));
}
}
};
let resume_ty = self.next_ty_var(expr_span);
let closure_kind_ty =
match expected_kind {
Some(kind) => Ty::from_closure_kind(tcx, kind),
None => self.next_ty_var(expr_span),
};
let coroutine_captures_by_ref_ty =
self.next_ty_var(expr_span);
let closure_args =
ty::CoroutineClosureArgs::new(tcx,
ty::CoroutineClosureArgsParts {
parent_args,
closure_kind_ty,
signature_parts_ty: Ty::new_fn_ptr(tcx,
bound_sig.map_bound(|sig|
{
tcx.mk_fn_sig([resume_ty,
Ty::new_tup_from_iter(tcx, sig.inputs().iter().copied())],
Ty::new_tup(tcx, &[bound_yield_ty, bound_return_ty]),
sig.c_variadic, sig.safety, sig.abi)
})),
tupled_upvars_ty,
coroutine_captures_by_ref_ty,
});
let coroutine_kind_ty =
match expected_kind {
Some(kind) => Ty::from_coroutine_closure_kind(tcx, kind),
None => self.next_ty_var(expr_span),
};
let coroutine_upvars_ty = self.next_ty_var(expr_span);
let coroutine_output_ty =
tcx.liberate_late_bound_regions(expr_def_id.to_def_id(),
closure_args.coroutine_closure_sig().map_bound(|sig|
{
sig.to_coroutine(tcx, parent_args, coroutine_kind_ty,
tcx.coroutine_for_closure(expr_def_id), coroutine_upvars_ty)
}));
liberated_sig =
tcx.mk_fn_sig(liberated_sig.inputs().iter().copied(),
coroutine_output_ty, liberated_sig.c_variadic,
liberated_sig.safety, liberated_sig.abi);
(Ty::new_coroutine_closure(tcx, expr_def_id.to_def_id(),
closure_args.args), None)
}
};
check_fn(&mut FnCtxt::new(self, self.param_env, closure.def_id),
liberated_sig, coroutine_types, closure.fn_decl, expr_def_id,
body, false);
closure_ty
}
}
}#[instrument(skip(self, closure), level = "debug")]48pub(crate) fn check_expr_closure(
49&self,
50 closure: &hir::Closure<'tcx>,
51 expr_span: Span,
52 expected: Expectation<'tcx>,
53 ) -> Ty<'tcx> {
54let tcx = self.tcx;
55let body = tcx.hir_body(closure.body);
56let expr_def_id = closure.def_id;
5758// It's always helpful for inference if we know the kind of
59 // closure sooner rather than later, so first examine the expected
60 // type, and see if can glean a closure kind from there.
61let (expected_sig, expected_kind) = match expected.to_option(self) {
62Some(ty) => self.deduce_closure_signature(
63self.try_structurally_resolve_type(expr_span, ty),
64 closure.kind,
65 ),
66None => (None, None),
67 };
6869let ClosureSignatures { bound_sig, mut liberated_sig } =
70self.sig_of_closure(expr_def_id, closure.fn_decl, closure.kind, expected_sig);
7172debug!(?bound_sig, ?liberated_sig);
7374let parent_args =
75 GenericArgs::identity_for_item(tcx, tcx.typeck_root_def_id(expr_def_id.to_def_id()));
7677let tupled_upvars_ty = self.next_ty_var(expr_span);
7879// FIXME: We could probably actually just unify this further --
80 // instead of having a `FnSig` and a `Option<CoroutineTypes>`,
81 // we can have a `ClosureSignature { Coroutine { .. }, Closure { .. } }`,
82 // similar to how `ty::GenSig` is a distinct data structure.
83let (closure_ty, coroutine_types) = match closure.kind {
84 hir::ClosureKind::Closure => {
85// Tuple up the arguments and insert the resulting function type into
86 // the `closures` table.
87let sig = bound_sig.map_bound(|sig| {
88 tcx.mk_fn_sig(
89 [Ty::new_tup(tcx, sig.inputs())],
90 sig.output(),
91 sig.c_variadic,
92 sig.safety,
93 sig.abi,
94 )
95 });
9697debug!(?sig, ?expected_kind);
9899let closure_kind_ty = match expected_kind {
100Some(kind) => Ty::from_closure_kind(tcx, kind),
101102// Create a type variable (for now) to represent the closure kind.
103 // It will be unified during the upvar inference phase (`upvar.rs`)
104None => self.next_ty_var(expr_span),
105 };
106107let closure_args = ty::ClosureArgs::new(
108 tcx,
109 ty::ClosureArgsParts {
110 parent_args,
111 closure_kind_ty,
112 closure_sig_as_fn_ptr_ty: Ty::new_fn_ptr(tcx, sig),
113 tupled_upvars_ty,
114 },
115 );
116117 (Ty::new_closure(tcx, expr_def_id.to_def_id(), closure_args.args), None)
118 }
119 hir::ClosureKind::Coroutine(kind) => {
120let yield_ty = match kind {
121 hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Gen, _)
122 | hir::CoroutineKind::Coroutine(_) => {
123let yield_ty = self.next_ty_var(expr_span);
124self.require_type_is_sized(
125 yield_ty,
126 expr_span,
127 ObligationCauseCode::SizedYieldType,
128 );
129 yield_ty
130 }
131// HACK(-Ztrait-solver=next): In the *old* trait solver, we must eagerly
132 // guide inference on the yield type so that we can handle `AsyncIterator`
133 // in this block in projection correctly. In the new trait solver, it is
134 // not a problem.
135hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::AsyncGen, _) => {
136let yield_ty = self.next_ty_var(expr_span);
137self.require_type_is_sized(
138 yield_ty,
139 expr_span,
140 ObligationCauseCode::SizedYieldType,
141 );
142143 Ty::new_adt(
144 tcx,
145 tcx.adt_def(tcx.require_lang_item(hir::LangItem::Poll, expr_span)),
146 tcx.mk_args(&[Ty::new_adt(
147 tcx,
148 tcx.adt_def(
149 tcx.require_lang_item(hir::LangItem::Option, expr_span),
150 ),
151 tcx.mk_args(&[yield_ty.into()]),
152 )
153 .into()]),
154 )
155 }
156 hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Async, _) => {
157 tcx.types.unit
158 }
159 };
160161// Resume type defaults to `()` if the coroutine has no argument.
162let resume_ty = liberated_sig.inputs().get(0).copied().unwrap_or(tcx.types.unit);
163164// Coroutines that come from coroutine closures have not yet determined
165 // their kind ty, so make a fresh infer var which will be constrained
166 // later during upvar analysis. Regular coroutines always have the kind
167 // ty of `().`
168let kind_ty = match kind {
169 hir::CoroutineKind::Desugared(_, hir::CoroutineSource::Closure) => {
170self.next_ty_var(expr_span)
171 }
172_ => tcx.types.unit,
173 };
174175let coroutine_args = ty::CoroutineArgs::new(
176 tcx,
177 ty::CoroutineArgsParts {
178 parent_args,
179 kind_ty,
180 resume_ty,
181 yield_ty,
182 return_ty: liberated_sig.output(),
183 tupled_upvars_ty,
184 },
185 );
186187 (
188 Ty::new_coroutine(tcx, expr_def_id.to_def_id(), coroutine_args.args),
189Some(CoroutineTypes { resume_ty, yield_ty }),
190 )
191 }
192 hir::ClosureKind::CoroutineClosure(kind) => {
193let (bound_return_ty, bound_yield_ty) = match kind {
194 hir::CoroutineDesugaring::Gen => {
195// `iter!` closures always return unit and yield the `Iterator::Item` type
196 // that we have to infer.
197(tcx.types.unit, self.infcx.next_ty_var(expr_span))
198 }
199 hir::CoroutineDesugaring::Async => {
200// async closures always return the type ascribed after the `->` (if present),
201 // and yield `()`.
202(bound_sig.skip_binder().output(), tcx.types.unit)
203 }
204 hir::CoroutineDesugaring::AsyncGen => {
205todo!("`async gen` closures not supported yet")
206 }
207 };
208// Compute all of the variables that will be used to populate the coroutine.
209let resume_ty = self.next_ty_var(expr_span);
210211let closure_kind_ty = match expected_kind {
212Some(kind) => Ty::from_closure_kind(tcx, kind),
213214// Create a type variable (for now) to represent the closure kind.
215 // It will be unified during the upvar inference phase (`upvar.rs`)
216None => self.next_ty_var(expr_span),
217 };
218219let coroutine_captures_by_ref_ty = self.next_ty_var(expr_span);
220let closure_args = ty::CoroutineClosureArgs::new(
221 tcx,
222 ty::CoroutineClosureArgsParts {
223 parent_args,
224 closure_kind_ty,
225 signature_parts_ty: Ty::new_fn_ptr(
226 tcx,
227 bound_sig.map_bound(|sig| {
228 tcx.mk_fn_sig(
229 [
230 resume_ty,
231 Ty::new_tup_from_iter(tcx, sig.inputs().iter().copied()),
232 ],
233 Ty::new_tup(tcx, &[bound_yield_ty, bound_return_ty]),
234 sig.c_variadic,
235 sig.safety,
236 sig.abi,
237 )
238 }),
239 ),
240 tupled_upvars_ty,
241 coroutine_captures_by_ref_ty,
242 },
243 );
244245let coroutine_kind_ty = match expected_kind {
246Some(kind) => Ty::from_coroutine_closure_kind(tcx, kind),
247248// Create a type variable (for now) to represent the closure kind.
249 // It will be unified during the upvar inference phase (`upvar.rs`)
250None => self.next_ty_var(expr_span),
251 };
252253let coroutine_upvars_ty = self.next_ty_var(expr_span);
254255// We need to turn the liberated signature that we got from HIR, which
256 // looks something like `|Args...| -> T`, into a signature that is suitable
257 // for type checking the inner body of the closure, which always returns a
258 // coroutine. To do so, we use the `CoroutineClosureSignature` to compute
259 // the coroutine type, filling in the tupled_upvars_ty and kind_ty with infer
260 // vars which will get constrained during upvar analysis.
261let coroutine_output_ty = tcx.liberate_late_bound_regions(
262 expr_def_id.to_def_id(),
263 closure_args.coroutine_closure_sig().map_bound(|sig| {
264 sig.to_coroutine(
265 tcx,
266 parent_args,
267 coroutine_kind_ty,
268 tcx.coroutine_for_closure(expr_def_id),
269 coroutine_upvars_ty,
270 )
271 }),
272 );
273 liberated_sig = tcx.mk_fn_sig(
274 liberated_sig.inputs().iter().copied(),
275 coroutine_output_ty,
276 liberated_sig.c_variadic,
277 liberated_sig.safety,
278 liberated_sig.abi,
279 );
280281 (Ty::new_coroutine_closure(tcx, expr_def_id.to_def_id(), closure_args.args), None)
282 }
283 };
284285 check_fn(
286&mut FnCtxt::new(self, self.param_env, closure.def_id),
287 liberated_sig,
288 coroutine_types,
289 closure.fn_decl,
290 expr_def_id,
291 body,
292// Closure "rust-call" ABI doesn't support unsized params
293false,
294 );
295296 closure_ty
297 }
298299/// Given the expected type, figures out what it can about this closure we
300 /// are about to type check:
301x;#[instrument(skip(self), level = "debug", ret)]302fn deduce_closure_signature(
303&self,
304 expected_ty: Ty<'tcx>,
305 closure_kind: hir::ClosureKind,
306 ) -> (Option<ExpectedSig<'tcx>>, Option<ty::ClosureKind>) {
307match *expected_ty.kind() {
308 ty::Alias(ty::Opaque, ty::AliasTy { def_id, args, .. }) => self
309.deduce_closure_signature_from_predicates(
310 expected_ty,
311 closure_kind,
312self.tcx
313 .explicit_item_self_bounds(def_id)
314 .iter_instantiated_copied(self.tcx, args)
315 .map(|(c, s)| (c.as_predicate(), s)),
316 ),
317 ty::Dynamic(object_type, ..) => {
318let sig = object_type.projection_bounds().find_map(|pb| {
319let pb = pb.with_self_ty(self.tcx, self.tcx.types.trait_object_dummy_self);
320self.deduce_sig_from_projection(None, closure_kind, pb)
321 });
322let kind = object_type
323 .principal_def_id()
324 .and_then(|did| self.tcx.fn_trait_kind_from_def_id(did));
325 (sig, kind)
326 }
327 ty::Infer(ty::TyVar(vid)) => self.deduce_closure_signature_from_predicates(
328 Ty::new_var(self.tcx, self.root_var(vid)),
329 closure_kind,
330self.obligations_for_self_ty(vid)
331 .into_iter()
332 .map(|obl| (obl.predicate, obl.cause.span)),
333 ),
334 ty::FnPtr(sig_tys, hdr) => match closure_kind {
335 hir::ClosureKind::Closure => {
336let expected_sig = ExpectedSig { cause_span: None, sig: sig_tys.with(hdr) };
337 (Some(expected_sig), Some(ty::ClosureKind::Fn))
338 }
339 hir::ClosureKind::Coroutine(_) | hir::ClosureKind::CoroutineClosure(_) => {
340 (None, None)
341 }
342 },
343_ => (None, None),
344 }
345 }
346347fn deduce_closure_signature_from_predicates(
348&self,
349 expected_ty: Ty<'tcx>,
350 closure_kind: hir::ClosureKind,
351 predicates: impl DoubleEndedIterator<Item = (ty::Predicate<'tcx>, Span)>,
352 ) -> (Option<ExpectedSig<'tcx>>, Option<ty::ClosureKind>) {
353let mut expected_sig = None;
354let mut expected_kind = None;
355356for (pred, span) in traits::elaborate(
357self.tcx,
358// Reverse the obligations here, since `elaborate_*` uses a stack,
359 // and we want to keep inference generally in the same order of
360 // the registered obligations.
361predicates.rev(),
362 )
363// We only care about self bounds
364.filter_only_self()
365 {
366{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/closure.rs:366",
"rustc_hir_typeck::closure", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/closure.rs"),
::tracing_core::__macro_support::Option::Some(366u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::closure"),
::tracing_core::field::FieldSet::new(&["pred"],
::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};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&pred) as
&dyn Value))])
});
} else { ; }
};debug!(?pred);
367let bound_predicate = pred.kind();
368369// Given a Projection predicate, we can potentially infer
370 // the complete signature.
371if expected_sig.is_none()
372 && let ty::PredicateKind::Clause(ty::ClauseKind::Projection(proj_predicate)) =
373 bound_predicate.skip_binder()
374 {
375let inferred_sig = self.normalize(
376 span,
377self.deduce_sig_from_projection(
378Some(span),
379 closure_kind,
380 bound_predicate.rebind(proj_predicate),
381 ),
382 );
383384// Make sure that we didn't infer a signature that mentions itself.
385 // This can happen when we elaborate certain supertrait bounds that
386 // mention projections containing the `Self` type. See #105401.
387struct MentionsTy<'tcx> {
388 expected_ty: Ty<'tcx>,
389 }
390impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for MentionsTy<'tcx> {
391type Result = ControlFlow<()>;
392393fn visit_ty(&mut self, t: Ty<'tcx>) -> Self::Result {
394if t == self.expected_ty {
395 ControlFlow::Break(())
396 } else {
397 t.super_visit_with(self)
398 }
399 }
400 }
401402// Don't infer a closure signature from a goal that names the closure type as this will
403 // (almost always) lead to occurs check errors later in type checking.
404if self.next_trait_solver()
405 && let Some(inferred_sig) = inferred_sig
406 {
407// In the new solver it is difficult to explicitly normalize the inferred signature as we
408 // would have to manually handle universes and rewriting bound vars and placeholders back
409 // and forth.
410 //
411 // Instead we take advantage of the fact that we relating an inference variable with an alias
412 // will only instantiate the variable if the alias is rigid(*not quite). Concretely we:
413 // - Create some new variable `?sig`
414 // - Equate `?sig` with the unnormalized signature, e.g. `fn(<Foo<?x> as Trait>::Assoc)`
415 // - Depending on whether `<Foo<?x> as Trait>::Assoc` is rigid, ambiguous or normalizeable,
416 // we will either wind up with `?sig=<Foo<?x> as Trait>::Assoc/?y/ConcreteTy` respectively.
417 //
418 // *: In cases where there are ambiguous aliases in the signature that make use of bound vars
419 // they will wind up present in `?sig` even though they are non-rigid.
420 //
421 // This is a bit weird and means we may wind up discarding the goal due to it naming `expected_ty`
422 // even though the normalized form may not name `expected_ty`. However, this matches the existing
423 // behaviour of the old solver and would be technically a breaking change to fix.
424let generalized_fnptr_sig = self.next_ty_var(span);
425let inferred_fnptr_sig = Ty::new_fn_ptr(self.tcx, inferred_sig.sig);
426self.demand_eqtype(span, inferred_fnptr_sig, generalized_fnptr_sig);
427428let resolved_sig = self.resolve_vars_if_possible(generalized_fnptr_sig);
429430if resolved_sig.visit_with(&mut MentionsTy { expected_ty }).is_continue() {
431 expected_sig = Some(ExpectedSig {
432 cause_span: inferred_sig.cause_span,
433 sig: resolved_sig.fn_sig(self.tcx),
434 });
435 }
436 } else {
437if inferred_sig.visit_with(&mut MentionsTy { expected_ty }).is_continue() {
438 expected_sig = inferred_sig;
439 }
440 }
441 }
442443// Even if we can't infer the full signature, we may be able to
444 // infer the kind. This can occur when we elaborate a predicate
445 // like `F : Fn<A>`. Note that due to subtyping we could encounter
446 // many viable options, so pick the most restrictive.
447let trait_def_id = match bound_predicate.skip_binder() {
448 ty::PredicateKind::Clause(ty::ClauseKind::Projection(data)) => {
449Some(data.projection_term.trait_def_id(self.tcx))
450 }
451 ty::PredicateKind::Clause(ty::ClauseKind::Trait(data)) => Some(data.def_id()),
452_ => None,
453 };
454455if let Some(trait_def_id) = trait_def_id {
456let found_kind = match closure_kind {
457 hir::ClosureKind::Closure
458// FIXME(iter_macro): Someday we'll probably want iterator closures instead of
459 // just using Fn* for iterators.
460| hir::ClosureKind::CoroutineClosure(hir::CoroutineDesugaring::Gen) => {
461self.tcx.fn_trait_kind_from_def_id(trait_def_id)
462 }
463 hir::ClosureKind::CoroutineClosure(hir::CoroutineDesugaring::Async) => self
464.tcx
465 .async_fn_trait_kind_from_def_id(trait_def_id)
466 .or_else(|| self.tcx.fn_trait_kind_from_def_id(trait_def_id)),
467_ => None,
468 };
469470if let Some(found_kind) = found_kind {
471// always use the closure kind that is more permissive.
472match (expected_kind, found_kind) {
473 (None, _) => expected_kind = Some(found_kind),
474 (Some(ClosureKind::FnMut), ClosureKind::Fn) => {
475 expected_kind = Some(ClosureKind::Fn)
476 }
477 (Some(ClosureKind::FnOnce), ClosureKind::Fn | ClosureKind::FnMut) => {
478 expected_kind = Some(found_kind)
479 }
480_ => {}
481 }
482 }
483 }
484 }
485486 (expected_sig, expected_kind)
487 }
488489/// Given a projection like "<F as Fn(X)>::Result == Y", we can deduce
490 /// everything we need to know about a closure or coroutine.
491 ///
492 /// The `cause_span` should be the span that caused us to
493 /// have this expected signature, or `None` if we can't readily
494 /// know that.
495x;#[instrument(level = "debug", skip(self, cause_span), ret)]496fn deduce_sig_from_projection(
497&self,
498 cause_span: Option<Span>,
499 closure_kind: hir::ClosureKind,
500 projection: ty::PolyProjectionPredicate<'tcx>,
501 ) -> Option<ExpectedSig<'tcx>> {
502let def_id = projection.item_def_id();
503504// For now, we only do signature deduction based off of the `Fn` and `AsyncFn` traits,
505 // for closures and async closures, respectively.
506match closure_kind {
507 hir::ClosureKind::Closure if self.tcx.is_lang_item(def_id, LangItem::FnOnceOutput) => {
508self.extract_sig_from_projection(cause_span, projection)
509 }
510 hir::ClosureKind::CoroutineClosure(hir::CoroutineDesugaring::Async)
511if self.tcx.is_lang_item(def_id, LangItem::AsyncFnOnceOutput) =>
512 {
513self.extract_sig_from_projection(cause_span, projection)
514 }
515// It's possible we've passed the closure to a (somewhat out-of-fashion)
516 // `F: FnOnce() -> Fut, Fut: Future<Output = T>` style bound. Let's still
517 // guide inference here, since it's beneficial for the user.
518hir::ClosureKind::CoroutineClosure(hir::CoroutineDesugaring::Async)
519if self.tcx.is_lang_item(def_id, LangItem::FnOnceOutput) =>
520 {
521self.extract_sig_from_projection_and_future_bound(cause_span, projection)
522 }
523_ => None,
524 }
525 }
526527/// Given an `FnOnce::Output` or `AsyncFn::Output` projection, extract the args
528 /// and return type to infer a [`ty::PolyFnSig`] for the closure.
529fn extract_sig_from_projection(
530&self,
531 cause_span: Option<Span>,
532 projection: ty::PolyProjectionPredicate<'tcx>,
533 ) -> Option<ExpectedSig<'tcx>> {
534let projection = self.resolve_vars_if_possible(projection);
535536let arg_param_ty = projection.skip_binder().projection_term.args.type_at(1);
537{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/closure.rs:537",
"rustc_hir_typeck::closure", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/closure.rs"),
::tracing_core::__macro_support::Option::Some(537u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::closure"),
::tracing_core::field::FieldSet::new(&["arg_param_ty"],
::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};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&arg_param_ty)
as &dyn Value))])
});
} else { ; }
};debug!(?arg_param_ty);
538539let ty::Tuple(input_tys) = *arg_param_ty.kind() else {
540return None;
541 };
542543// Since this is a return parameter type it is safe to unwrap.
544let ret_param_ty = projection.skip_binder().term.expect_type();
545{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/closure.rs:545",
"rustc_hir_typeck::closure", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/closure.rs"),
::tracing_core::__macro_support::Option::Some(545u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::closure"),
::tracing_core::field::FieldSet::new(&["ret_param_ty"],
::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};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&ret_param_ty)
as &dyn Value))])
});
} else { ; }
};debug!(?ret_param_ty);
546547let sig = projection.rebind(self.tcx.mk_fn_sig(
548input_tys,
549ret_param_ty,
550false,
551 hir::Safety::Safe,
552 ExternAbi::Rust,
553 ));
554555Some(ExpectedSig { cause_span, sig })
556 }
557558/// When an async closure is passed to a function that has a "two-part" `Fn`
559 /// and `Future` trait bound, like:
560 ///
561 /// ```rust
562 /// use std::future::Future;
563 ///
564 /// fn not_exactly_an_async_closure<F, Fut>(_f: F)
565 /// where
566 /// F: FnOnce(String, u32) -> Fut,
567 /// Fut: Future<Output = i32>,
568 /// {}
569 /// ```
570 ///
571 /// The we want to be able to extract the signature to guide inference in the async
572 /// closure. We will have two projection predicates registered in this case. First,
573 /// we identify the `FnOnce<Args, Output = ?Fut>` bound, and if the output type is
574 /// an inference variable `?Fut`, we check if that is bounded by a `Future<Output = Ty>`
575 /// projection.
576 ///
577 /// This function is actually best-effort with the return type; if we don't find a
578 /// `Future` projection, we still will return arguments that we extracted from the `FnOnce`
579 /// projection, and the output will be an unconstrained type variable instead.
580fn extract_sig_from_projection_and_future_bound(
581&self,
582 cause_span: Option<Span>,
583 projection: ty::PolyProjectionPredicate<'tcx>,
584 ) -> Option<ExpectedSig<'tcx>> {
585let projection = self.resolve_vars_if_possible(projection);
586587let arg_param_ty = projection.skip_binder().projection_term.args.type_at(1);
588{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/closure.rs:588",
"rustc_hir_typeck::closure", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/closure.rs"),
::tracing_core::__macro_support::Option::Some(588u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::closure"),
::tracing_core::field::FieldSet::new(&["arg_param_ty"],
::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};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&arg_param_ty)
as &dyn Value))])
});
} else { ; }
};debug!(?arg_param_ty);
589590let ty::Tuple(input_tys) = *arg_param_ty.kind() else {
591return None;
592 };
593594// If the return type is a type variable, look for bounds on it.
595 // We could theoretically support other kinds of return types here,
596 // but none of them would be useful, since async closures return
597 // concrete anonymous future types, and their futures are not coerced
598 // into any other type within the body of the async closure.
599let ty::Infer(ty::TyVar(return_vid)) = *projection.skip_binder().term.expect_type().kind()
600else {
601return None;
602 };
603604// FIXME: We may want to elaborate here, though I assume this will be exceedingly rare.
605let mut return_ty = None;
606for bound in self.obligations_for_self_ty(return_vid) {
607if let Some(ret_projection) = bound.predicate.as_projection_clause()
608 && let Some(ret_projection) = ret_projection.no_bound_vars()
609 && self.tcx.is_lang_item(ret_projection.def_id(), LangItem::FutureOutput)
610 {
611 return_ty = Some(ret_projection.term.expect_type());
612break;
613 }
614 }
615616// SUBTLE: If we didn't find a `Future<Output = ...>` bound for the return
617 // vid, we still want to attempt to provide inference guidance for the async
618 // closure's arguments. Instantiate a new vid to plug into the output type.
619 //
620 // You may be wondering, what if it's higher-ranked? Well, given that we
621 // found a type variable for the `FnOnce::Output` projection above, we know
622 // that the output can't mention any of the vars.
623 //
624 // Also note that we use a fresh var here for the signature since the signature
625 // records the output of the *future*, and `return_vid` above is the type
626 // variable of the future, not its output.
627 //
628 // FIXME: We probably should store this signature inference output in a way
629 // that does not misuse a `FnSig` type, but that can be done separately.
630let return_ty =
631return_ty.unwrap_or_else(|| self.next_ty_var(cause_span.unwrap_or(DUMMY_SP)));
632633let sig = projection.rebind(self.tcx.mk_fn_sig(
634input_tys,
635return_ty,
636false,
637 hir::Safety::Safe,
638 ExternAbi::Rust,
639 ));
640641Some(ExpectedSig { cause_span, sig })
642 }
643644fn sig_of_closure(
645&self,
646 expr_def_id: LocalDefId,
647 decl: &hir::FnDecl<'tcx>,
648 closure_kind: hir::ClosureKind,
649 expected_sig: Option<ExpectedSig<'tcx>>,
650 ) -> ClosureSignatures<'tcx> {
651if let Some(e) = expected_sig {
652self.sig_of_closure_with_expectation(expr_def_id, decl, closure_kind, e)
653 } else {
654self.sig_of_closure_no_expectation(expr_def_id, decl, closure_kind)
655 }
656 }
657658/// If there is no expected signature, then we will convert the
659 /// types that the user gave into a signature.
660#[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("sig_of_closure_no_expectation",
"rustc_hir_typeck::closure", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/closure.rs"),
::tracing_core::__macro_support::Option::Some(660u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::closure"),
::tracing_core::field::FieldSet::new(&["closure_kind"],
::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};
let mut iter = meta.fields().iter();
meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&closure_kind)
as &dyn 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: ClosureSignatures<'tcx> = loop {};
return __tracing_attr_fake_return;
}
{
let bound_sig =
self.supplied_sig_of_closure(expr_def_id, decl, closure_kind);
self.closure_sigs(expr_def_id, bound_sig)
}
}
}#[instrument(skip(self, expr_def_id, decl), level = "debug")]661fn sig_of_closure_no_expectation(
662&self,
663 expr_def_id: LocalDefId,
664 decl: &hir::FnDecl<'tcx>,
665 closure_kind: hir::ClosureKind,
666 ) -> ClosureSignatures<'tcx> {
667let bound_sig = self.supplied_sig_of_closure(expr_def_id, decl, closure_kind);
668669self.closure_sigs(expr_def_id, bound_sig)
670 }
671672/// Invoked to compute the signature of a closure expression. This
673 /// combines any user-provided type annotations (e.g., `|x: u32|
674 /// -> u32 { .. }`) with the expected signature.
675 ///
676 /// The approach is as follows:
677 ///
678 /// - Let `S` be the (higher-ranked) signature that we derive from the user's annotations.
679 /// - Let `E` be the (higher-ranked) signature that we derive from the expectations, if any.
680 /// - If we have no expectation `E`, then the signature of the closure is `S`.
681 /// - Otherwise, the signature of the closure is E. Moreover:
682 /// - Skolemize the late-bound regions in `E`, yielding `E'`.
683 /// - Instantiate all the late-bound regions bound in the closure within `S`
684 /// with fresh (existential) variables, yielding `S'`
685 /// - Require that `E' = S'`
686 /// - We could use some kind of subtyping relationship here,
687 /// I imagine, but equality is easier and works fine for
688 /// our purposes.
689 ///
690 /// The key intuition here is that the user's types must be valid
691 /// from "the inside" of the closure, but the expectation
692 /// ultimately drives the overall signature.
693 ///
694 /// # Examples
695 ///
696 /// ```ignore (illustrative)
697 /// fn with_closure<F>(_: F)
698 /// where F: Fn(&u32) -> &u32 { .. }
699 ///
700 /// with_closure(|x: &u32| { ... })
701 /// ```
702 ///
703 /// Here:
704 /// - E would be `fn(&u32) -> &u32`.
705 /// - S would be `fn(&u32) -> ?T`
706 /// - E' is `&'!0 u32 -> &'!0 u32`
707 /// - S' is `&'?0 u32 -> ?T`
708 ///
709 /// S' can be unified with E' with `['?0 = '!0, ?T = &'!10 u32]`.
710 ///
711 /// # Arguments
712 ///
713 /// - `expr_def_id`: the `LocalDefId` of the closure expression
714 /// - `decl`: the HIR declaration of the closure
715 /// - `body`: the body of the closure
716 /// - `expected_sig`: the expected signature (if any). Note that
717 /// this is missing a binder: that is, there may be late-bound
718 /// regions with depth 1, which are bound then by the closure.
719#[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("sig_of_closure_with_expectation",
"rustc_hir_typeck::closure", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/closure.rs"),
::tracing_core::__macro_support::Option::Some(719u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::closure"),
::tracing_core::field::FieldSet::new(&["closure_kind",
"expected_sig"],
::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};
let mut iter = meta.fields().iter();
meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&closure_kind)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&expected_sig)
as &dyn 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: ClosureSignatures<'tcx> = loop {};
return __tracing_attr_fake_return;
}
{
if expected_sig.sig.c_variadic() != decl.c_variadic {
return self.sig_of_closure_no_expectation(expr_def_id, decl,
closure_kind);
} else if expected_sig.sig.skip_binder().inputs_and_output.len()
!= decl.inputs.len() + 1 {
return self.sig_of_closure_with_mismatched_number_of_arguments(expr_def_id,
decl, expected_sig);
}
if !!expected_sig.sig.skip_binder().has_vars_bound_above(ty::INNERMOST)
{
::core::panicking::panic("assertion failed: !expected_sig.sig.skip_binder().has_vars_bound_above(ty::INNERMOST)")
};
let bound_sig =
expected_sig.sig.map_bound(|sig|
{
self.tcx.mk_fn_sig(sig.inputs().iter().cloned(),
sig.output(), sig.c_variadic, hir::Safety::Safe,
ExternAbi::RustCall)
});
let bound_sig = self.tcx.anonymize_bound_vars(bound_sig);
let closure_sigs = self.closure_sigs(expr_def_id, bound_sig);
match self.merge_supplied_sig_with_expectation(expr_def_id, decl,
closure_kind, closure_sigs) {
Ok(infer_ok) => self.register_infer_ok_obligations(infer_ok),
Err(_) =>
self.sig_of_closure_no_expectation(expr_def_id, decl,
closure_kind),
}
}
}
}#[instrument(skip(self, expr_def_id, decl), level = "debug")]720fn sig_of_closure_with_expectation(
721&self,
722 expr_def_id: LocalDefId,
723 decl: &hir::FnDecl<'tcx>,
724 closure_kind: hir::ClosureKind,
725 expected_sig: ExpectedSig<'tcx>,
726 ) -> ClosureSignatures<'tcx> {
727// Watch out for some surprises and just ignore the
728 // expectation if things don't see to match up with what we
729 // expect.
730if expected_sig.sig.c_variadic() != decl.c_variadic {
731return self.sig_of_closure_no_expectation(expr_def_id, decl, closure_kind);
732 } else if expected_sig.sig.skip_binder().inputs_and_output.len() != decl.inputs.len() + 1 {
733return self.sig_of_closure_with_mismatched_number_of_arguments(
734 expr_def_id,
735 decl,
736 expected_sig,
737 );
738 }
739740// Create a `PolyFnSig`. Note the oddity that late bound
741 // regions appearing free in `expected_sig` are now bound up
742 // in this binder we are creating.
743assert!(!expected_sig.sig.skip_binder().has_vars_bound_above(ty::INNERMOST));
744let bound_sig = expected_sig.sig.map_bound(|sig| {
745self.tcx.mk_fn_sig(
746 sig.inputs().iter().cloned(),
747 sig.output(),
748 sig.c_variadic,
749 hir::Safety::Safe,
750 ExternAbi::RustCall,
751 )
752 });
753754// `deduce_expectations_from_expected_type` introduces
755 // late-bound lifetimes defined elsewhere, which we now
756 // anonymize away, so as not to confuse the user.
757let bound_sig = self.tcx.anonymize_bound_vars(bound_sig);
758759let closure_sigs = self.closure_sigs(expr_def_id, bound_sig);
760761// Up till this point, we have ignored the annotations that the user
762 // gave. This function will check that they unify successfully.
763 // Along the way, it also writes out entries for types that the user
764 // wrote into our typeck results, which are then later used by the privacy
765 // check.
766match self.merge_supplied_sig_with_expectation(
767 expr_def_id,
768 decl,
769 closure_kind,
770 closure_sigs,
771 ) {
772Ok(infer_ok) => self.register_infer_ok_obligations(infer_ok),
773Err(_) => self.sig_of_closure_no_expectation(expr_def_id, decl, closure_kind),
774 }
775 }
776777fn sig_of_closure_with_mismatched_number_of_arguments(
778&self,
779 expr_def_id: LocalDefId,
780 decl: &hir::FnDecl<'tcx>,
781 expected_sig: ExpectedSig<'tcx>,
782 ) -> ClosureSignatures<'tcx> {
783let expr_map_node = self.tcx.hir_node_by_def_id(expr_def_id);
784let expected_args: Vec<_> = expected_sig785 .sig
786 .skip_binder()
787 .inputs()
788 .iter()
789 .map(|ty| ArgKind::from_expected_ty(*ty, None))
790 .collect();
791let (closure_span, closure_arg_span, found_args) =
792match self.err_ctxt().get_fn_like_arguments(expr_map_node) {
793Some((sp, arg_sp, args)) => (Some(sp), arg_sp, args),
794None => (None, None, Vec::new()),
795 };
796let expected_span =
797expected_sig.cause_span.unwrap_or_else(|| self.tcx.def_span(expr_def_id));
798let guar = self799 .err_ctxt()
800 .report_arg_count_mismatch(
801expected_span,
802closure_span,
803expected_args,
804found_args,
805true,
806closure_arg_span,
807 )
808 .emit();
809810let error_sig = self.error_sig_of_closure(decl, guar);
811812self.closure_sigs(expr_def_id, error_sig)
813 }
814815/// Enforce the user's types against the expectation. See
816 /// `sig_of_closure_with_expectation` for details on the overall
817 /// strategy.
818#[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("merge_supplied_sig_with_expectation",
"rustc_hir_typeck::closure", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/closure.rs"),
::tracing_core::__macro_support::Option::Some(818u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::closure"),
::tracing_core::field::FieldSet::new(&["closure_kind"],
::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};
let mut iter = meta.fields().iter();
meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&closure_kind)
as &dyn 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:
InferResult<'tcx, ClosureSignatures<'tcx>> = loop {};
return __tracing_attr_fake_return;
}
{
let supplied_sig =
self.supplied_sig_of_closure(expr_def_id, decl, closure_kind);
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/closure.rs:832",
"rustc_hir_typeck::closure", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/closure.rs"),
::tracing_core::__macro_support::Option::Some(832u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::closure"),
::tracing_core::field::FieldSet::new(&["supplied_sig"],
::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};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&supplied_sig)
as &dyn Value))])
});
} else { ; }
};
self.commit_if_ok(|_|
{
let mut all_obligations = PredicateObligations::new();
let supplied_sig =
self.instantiate_binder_with_fresh_vars(self.tcx.def_span(expr_def_id),
BoundRegionConversionTime::FnCall, supplied_sig);
for ((hir_ty, &supplied_ty), expected_ty) in
iter::zip(iter::zip(decl.inputs, supplied_sig.inputs()),
expected_sigs.liberated_sig.inputs()) {
let cause = self.misc(hir_ty.span);
let InferOk { value: (), obligations } =
self.at(&cause,
self.param_env).eq(DefineOpaqueTypes::Yes, *expected_ty,
supplied_ty)?;
all_obligations.extend(obligations);
}
let supplied_output_ty = supplied_sig.output();
let cause = &self.misc(decl.output.span());
let InferOk { value: (), obligations } =
self.at(cause,
self.param_env).eq(DefineOpaqueTypes::Yes,
expected_sigs.liberated_sig.output(), supplied_output_ty)?;
all_obligations.extend(obligations);
let inputs =
supplied_sig.inputs().into_iter().map(|&ty|
self.resolve_vars_if_possible(ty));
expected_sigs.liberated_sig =
self.tcx.mk_fn_sig(inputs, supplied_output_ty,
expected_sigs.liberated_sig.c_variadic, hir::Safety::Safe,
ExternAbi::RustCall);
Ok(InferOk {
value: expected_sigs,
obligations: all_obligations,
})
})
}
}
}#[instrument(level = "debug", skip(self, expr_def_id, decl, expected_sigs))]819fn merge_supplied_sig_with_expectation(
820&self,
821 expr_def_id: LocalDefId,
822 decl: &hir::FnDecl<'tcx>,
823 closure_kind: hir::ClosureKind,
824mut expected_sigs: ClosureSignatures<'tcx>,
825 ) -> InferResult<'tcx, ClosureSignatures<'tcx>> {
826// Get the signature S that the user gave.
827 //
828 // (See comment on `sig_of_closure_with_expectation` for the
829 // meaning of these letters.)
830let supplied_sig = self.supplied_sig_of_closure(expr_def_id, decl, closure_kind);
831832debug!(?supplied_sig);
833834// FIXME(#45727): As discussed in [this comment][c1], naively
835 // forcing equality here actually results in suboptimal error
836 // messages in some cases. For now, if there would have been
837 // an obvious error, we fallback to declaring the type of the
838 // closure to be the one the user gave, which allows other
839 // error message code to trigger.
840 //
841 // However, I think [there is potential to do even better
842 // here][c2], since in *this* code we have the precise span of
843 // the type parameter in question in hand when we report the
844 // error.
845 //
846 // [c1]: https://github.com/rust-lang/rust/pull/45072#issuecomment-341089706
847 // [c2]: https://github.com/rust-lang/rust/pull/45072#issuecomment-341096796
848self.commit_if_ok(|_| {
849let mut all_obligations = PredicateObligations::new();
850let supplied_sig = self.instantiate_binder_with_fresh_vars(
851self.tcx.def_span(expr_def_id),
852 BoundRegionConversionTime::FnCall,
853 supplied_sig,
854 );
855856// The liberated version of this signature should be a subtype
857 // of the liberated form of the expectation.
858for ((hir_ty, &supplied_ty), expected_ty) in iter::zip(
859 iter::zip(decl.inputs, supplied_sig.inputs()),
860 expected_sigs.liberated_sig.inputs(), // `liberated_sig` is E'.
861) {
862// Check that E' = S'.
863let cause = self.misc(hir_ty.span);
864let InferOk { value: (), obligations } = self.at(&cause, self.param_env).eq(
865 DefineOpaqueTypes::Yes,
866*expected_ty,
867 supplied_ty,
868 )?;
869 all_obligations.extend(obligations);
870 }
871872let supplied_output_ty = supplied_sig.output();
873let cause = &self.misc(decl.output.span());
874let InferOk { value: (), obligations } = self.at(cause, self.param_env).eq(
875 DefineOpaqueTypes::Yes,
876 expected_sigs.liberated_sig.output(),
877 supplied_output_ty,
878 )?;
879 all_obligations.extend(obligations);
880881let inputs =
882 supplied_sig.inputs().into_iter().map(|&ty| self.resolve_vars_if_possible(ty));
883884 expected_sigs.liberated_sig = self.tcx.mk_fn_sig(
885 inputs,
886 supplied_output_ty,
887 expected_sigs.liberated_sig.c_variadic,
888 hir::Safety::Safe,
889 ExternAbi::RustCall,
890 );
891892Ok(InferOk { value: expected_sigs, obligations: all_obligations })
893 })
894 }
895896/// If there is no expected signature, then we will convert the
897 /// types that the user gave into a signature.
898 ///
899 /// Also, record this closure signature for later.
900x;#[instrument(skip(self, decl), level = "debug", ret)]901fn supplied_sig_of_closure(
902&self,
903 expr_def_id: LocalDefId,
904 decl: &hir::FnDecl<'tcx>,
905 closure_kind: hir::ClosureKind,
906 ) -> ty::PolyFnSig<'tcx> {
907let lowerer = self.lowerer();
908909trace!("decl = {:#?}", decl);
910debug!(?closure_kind);
911912let hir_id = self.tcx.local_def_id_to_hir_id(expr_def_id);
913let bound_vars = self.tcx.late_bound_vars(hir_id);
914915// First, convert the types that the user supplied (if any).
916let supplied_arguments = decl.inputs.iter().map(|a| lowerer.lower_ty(a));
917let supplied_return = match decl.output {
918 hir::FnRetTy::Return(ref output) => lowerer.lower_ty(output),
919 hir::FnRetTy::DefaultReturn(_) => match closure_kind {
920// In the case of the async block that we create for a function body,
921 // we expect the return type of the block to match that of the enclosing
922 // function.
923hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
924 hir::CoroutineDesugaring::Async,
925 hir::CoroutineSource::Fn,
926 )) => {
927debug!("closure is async fn body");
928self.deduce_future_output_from_obligations(expr_def_id).unwrap_or_else(|| {
929// AFAIK, deducing the future output
930 // always succeeds *except* in error cases
931 // like #65159. I'd like to return Error
932 // here, but I can't because I can't
933 // easily (and locally) prove that we
934 // *have* reported an
935 // error. --nikomatsakis
936lowerer.ty_infer(None, decl.output.span())
937 })
938 }
939// All `gen {}` and `async gen {}` must return unit.
940hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
941 hir::CoroutineDesugaring::Gen | hir::CoroutineDesugaring::AsyncGen,
942_,
943 )) => self.tcx.types.unit,
944945// For async blocks, we just fall back to `_` here.
946 // For closures/coroutines, we know nothing about the return
947 // type unless it was supplied.
948hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
949 hir::CoroutineDesugaring::Async,
950_,
951 ))
952 | hir::ClosureKind::Coroutine(hir::CoroutineKind::Coroutine(_))
953 | hir::ClosureKind::Closure
954 | hir::ClosureKind::CoroutineClosure(_) => {
955 lowerer.ty_infer(None, decl.output.span())
956 }
957 },
958 };
959960let result = ty::Binder::bind_with_vars(
961self.tcx.mk_fn_sig(
962 supplied_arguments,
963 supplied_return,
964 decl.c_variadic,
965 hir::Safety::Safe,
966 ExternAbi::RustCall,
967 ),
968 bound_vars,
969 );
970971let c_result = self.infcx.canonicalize_response(result);
972self.typeck_results.borrow_mut().user_provided_sigs.insert(expr_def_id, c_result);
973974// Normalize only after registering in `user_provided_sigs`.
975self.normalize(self.tcx.def_span(expr_def_id), result)
976 }
977978/// Invoked when we are translating the coroutine that results
979 /// from desugaring an `async fn`. Returns the "sugared" return
980 /// type of the `async fn` -- that is, the return type that the
981 /// user specified. The "desugared" return type is an `impl
982 /// Future<Output = T>`, so we do this by searching through the
983 /// obligations to extract the `T`.
984x;#[instrument(skip(self), level = "debug", ret)]985fn deduce_future_output_from_obligations(&self, body_def_id: LocalDefId) -> Option<Ty<'tcx>> {
986let ret_coercion = self.ret_coercion.as_ref().unwrap_or_else(|| {
987span_bug!(self.tcx.def_span(body_def_id), "async fn coroutine outside of a fn")
988 });
989990let closure_span = self.tcx.def_span(body_def_id);
991let ret_ty = ret_coercion.borrow().expected_ty();
992let ret_ty = self.try_structurally_resolve_type(closure_span, ret_ty);
993994let get_future_output = |predicate: ty::Predicate<'tcx>, span| {
995// Search for a pending obligation like
996 //
997 // `<R as Future>::Output = T`
998 //
999 // where R is the return type we are expecting. This type `T`
1000 // will be our output.
1001let bound_predicate = predicate.kind();
1002if let ty::PredicateKind::Clause(ty::ClauseKind::Projection(proj_predicate)) =
1003 bound_predicate.skip_binder()
1004 {
1005self.deduce_future_output_from_projection(
1006 span,
1007 bound_predicate.rebind(proj_predicate),
1008 )
1009 } else {
1010None
1011}
1012 };
10131014let output_ty = match *ret_ty.kind() {
1015 ty::Infer(ty::TyVar(ret_vid)) => {
1016self.obligations_for_self_ty(ret_vid).into_iter().find_map(|obligation| {
1017 get_future_output(obligation.predicate, obligation.cause.span)
1018 })?
1019}
1020 ty::Alias(ty::Projection, _) => {
1021return Some(Ty::new_error_with_message(
1022self.tcx,
1023 closure_span,
1024"this projection should have been projected to an opaque type",
1025 ));
1026 }
1027 ty::Alias(ty::Opaque, ty::AliasTy { def_id, args, .. }) => self
1028.tcx
1029 .explicit_item_self_bounds(def_id)
1030 .iter_instantiated_copied(self.tcx, args)
1031 .find_map(|(p, s)| get_future_output(p.as_predicate(), s))?,
1032 ty::Error(_) => return Some(ret_ty),
1033_ => {
1034span_bug!(closure_span, "invalid async fn coroutine return type: {ret_ty:?}")
1035 }
1036 };
10371038let output_ty = self.normalize(closure_span, output_ty);
10391040// async fn that have opaque types in their return type need to redo the conversion to inference variables
1041 // as they fetch the still opaque version from the signature.
1042let InferOk { value: output_ty, obligations } = self
1043.replace_opaque_types_with_inference_vars(
1044 output_ty,
1045 body_def_id,
1046 closure_span,
1047self.param_env,
1048 );
1049self.register_predicates(obligations);
10501051Some(output_ty)
1052 }
10531054/// Given a projection like
1055 ///
1056 /// `<X as Future>::Output = T`
1057 ///
1058 /// where `X` is some type that has no late-bound regions, returns
1059 /// `Some(T)`. If the projection is for some other trait, returns
1060 /// `None`.
1061fn deduce_future_output_from_projection(
1062&self,
1063 cause_span: Span,
1064 predicate: ty::PolyProjectionPredicate<'tcx>,
1065 ) -> Option<Ty<'tcx>> {
1066{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/closure.rs:1066",
"rustc_hir_typeck::closure", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/closure.rs"),
::tracing_core::__macro_support::Option::Some(1066u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::closure"),
::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};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("deduce_future_output_from_projection(predicate={0:?})",
predicate) as &dyn Value))])
});
} else { ; }
};debug!("deduce_future_output_from_projection(predicate={:?})", predicate);
10671068// We do not expect any bound regions in our predicate, so
1069 // skip past the bound vars.
1070let Some(predicate) = predicate.no_bound_vars() else {
1071{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/closure.rs:1071",
"rustc_hir_typeck::closure", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/closure.rs"),
::tracing_core::__macro_support::Option::Some(1071u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::closure"),
::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};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("deduce_future_output_from_projection: has late-bound regions")
as &dyn Value))])
});
} else { ; }
};debug!("deduce_future_output_from_projection: has late-bound regions");
1072return None;
1073 };
10741075// Check that this is a projection from the `Future` trait.
1076let trait_def_id = predicate.projection_term.trait_def_id(self.tcx);
1077if !self.tcx.is_lang_item(trait_def_id, LangItem::Future) {
1078{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/closure.rs:1078",
"rustc_hir_typeck::closure", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/closure.rs"),
::tracing_core::__macro_support::Option::Some(1078u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::closure"),
::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};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("deduce_future_output_from_projection: not a future")
as &dyn Value))])
});
} else { ; }
};debug!("deduce_future_output_from_projection: not a future");
1079return None;
1080 }
10811082// The `Future` trait has only one associated item, `Output`,
1083 // so check that this is what we see.
1084let output_assoc_item = self.tcx.associated_item_def_ids(trait_def_id)[0];
1085if output_assoc_item != predicate.projection_term.def_id {
1086::rustc_middle::util::bug::span_bug_fmt(cause_span,
format_args!("projecting associated item `{0:?}` from future, which is not Output `{1:?}`",
predicate.projection_term.def_id, output_assoc_item));span_bug!(
1087cause_span,
1088"projecting associated item `{:?}` from future, which is not Output `{:?}`",
1089 predicate.projection_term.def_id,
1090 output_assoc_item,
1091 );
1092 }
10931094// Extract the type from the projection. Note that there can
1095 // be no bound variables in this type because the "self type"
1096 // does not have any regions in it.
1097let output_ty = self.resolve_vars_if_possible(predicate.term);
1098{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/closure.rs:1098",
"rustc_hir_typeck::closure", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/closure.rs"),
::tracing_core::__macro_support::Option::Some(1098u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::closure"),
::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};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("deduce_future_output_from_projection: output_ty={0:?}",
output_ty) as &dyn Value))])
});
} else { ; }
};debug!("deduce_future_output_from_projection: output_ty={:?}", output_ty);
1099// This is a projection on a Fn trait so will always be a type.
1100Some(output_ty.expect_type())
1101 }
11021103/// Converts the types that the user supplied, in case that doing
1104 /// so should yield an error, but returns back a signature where
1105 /// all parameters are of type `ty::Error`.
1106fn error_sig_of_closure(
1107&self,
1108 decl: &hir::FnDecl<'tcx>,
1109 guar: ErrorGuaranteed,
1110 ) -> ty::PolyFnSig<'tcx> {
1111let lowerer = self.lowerer();
1112let err_ty = Ty::new_error(self.tcx, guar);
11131114let supplied_arguments = decl.inputs.iter().map(|a| {
1115// Convert the types that the user supplied (if any), but ignore them.
1116lowerer.lower_ty(a);
1117err_ty1118 });
11191120if let hir::FnRetTy::Return(ref output) = decl.output {
1121lowerer.lower_ty(output);
1122 }
11231124let result = ty::Binder::dummy(self.tcx.mk_fn_sig(
1125supplied_arguments,
1126err_ty,
1127decl.c_variadic,
1128 hir::Safety::Safe,
1129 ExternAbi::RustCall,
1130 ));
11311132{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/closure.rs:1132",
"rustc_hir_typeck::closure", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/closure.rs"),
::tracing_core::__macro_support::Option::Some(1132u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::closure"),
::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};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("supplied_sig_of_closure: result={0:?}",
result) as &dyn Value))])
});
} else { ; }
};debug!("supplied_sig_of_closure: result={:?}", result);
11331134result1135 }
11361137x;#[instrument(level = "debug", skip(self), ret)]1138fn closure_sigs(
1139&self,
1140 expr_def_id: LocalDefId,
1141 bound_sig: ty::PolyFnSig<'tcx>,
1142 ) -> ClosureSignatures<'tcx> {
1143let liberated_sig =
1144self.tcx().liberate_late_bound_regions(expr_def_id.to_def_id(), bound_sig);
1145let liberated_sig = self.normalize(self.tcx.def_span(expr_def_id), liberated_sig);
1146 ClosureSignatures { bound_sig, liberated_sig }
1147 }
1148}