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, FnSigKind, GenericArgs, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable,
17TypeVisitableExt, TypeVisitor, Unnormalized,
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.resolve_vars_with_obligations(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:71",
"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(71u32),
::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_local(expr_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.fn_sig_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:90",
"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(90u32),
::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.fn_sig_kind)
})),
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.fn_sig_kind);
(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) => {
63self.deduce_closure_signature(self.resolve_vars_with_obligations(ty), closure.kind)
64 }
65None => (None, None),
66 };
6768let ClosureSignatures { bound_sig, mut liberated_sig } =
69self.sig_of_closure(expr_def_id, closure.fn_decl, closure.kind, expected_sig);
7071debug!(?bound_sig, ?liberated_sig);
7273let parent_args =
74 GenericArgs::identity_for_item(tcx, tcx.typeck_root_def_id_local(expr_def_id));
7576let tupled_upvars_ty = self.next_ty_var(expr_span);
7778// FIXME: We could probably actually just unify this further --
79 // instead of having a `FnSig` and a `Option<CoroutineTypes>`,
80 // we can have a `ClosureSignature { Coroutine { .. }, Closure { .. } }`,
81 // similar to how `ty::GenSig` is a distinct data structure.
82let (closure_ty, coroutine_types) = match closure.kind {
83 hir::ClosureKind::Closure => {
84// Tuple up the arguments and insert the resulting function type into
85 // the `closures` table.
86let sig = bound_sig.map_bound(|sig| {
87 tcx.mk_fn_sig([Ty::new_tup(tcx, sig.inputs())], sig.output(), sig.fn_sig_kind)
88 });
8990debug!(?sig, ?expected_kind);
9192let closure_kind_ty = match expected_kind {
93Some(kind) => Ty::from_closure_kind(tcx, kind),
9495// Create a type variable (for now) to represent the closure kind.
96 // It will be unified during the upvar inference phase (`upvar.rs`)
97None => self.next_ty_var(expr_span),
98 };
99100let closure_args = ty::ClosureArgs::new(
101 tcx,
102 ty::ClosureArgsParts {
103 parent_args,
104 closure_kind_ty,
105 closure_sig_as_fn_ptr_ty: Ty::new_fn_ptr(tcx, sig),
106 tupled_upvars_ty,
107 },
108 );
109110 (Ty::new_closure(tcx, expr_def_id.to_def_id(), closure_args.args), None)
111 }
112 hir::ClosureKind::Coroutine(kind) => {
113let yield_ty = match kind {
114 hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Gen, _)
115 | hir::CoroutineKind::Coroutine(_) => {
116let yield_ty = self.next_ty_var(expr_span);
117self.require_type_is_sized(
118 yield_ty,
119 expr_span,
120 ObligationCauseCode::SizedYieldType,
121 );
122 yield_ty
123 }
124// HACK(-Ztrait-solver=next): In the *old* trait solver, we must eagerly
125 // guide inference on the yield type so that we can handle `AsyncIterator`
126 // in this block in projection correctly. In the new trait solver, it is
127 // not a problem.
128hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::AsyncGen, _) => {
129let yield_ty = self.next_ty_var(expr_span);
130self.require_type_is_sized(
131 yield_ty,
132 expr_span,
133 ObligationCauseCode::SizedYieldType,
134 );
135136 Ty::new_adt(
137 tcx,
138 tcx.adt_def(tcx.require_lang_item(hir::LangItem::Poll, expr_span)),
139 tcx.mk_args(&[Ty::new_adt(
140 tcx,
141 tcx.adt_def(
142 tcx.require_lang_item(hir::LangItem::Option, expr_span),
143 ),
144 tcx.mk_args(&[yield_ty.into()]),
145 )
146 .into()]),
147 )
148 }
149 hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Async, _) => {
150 tcx.types.unit
151 }
152 };
153154// Resume type defaults to `()` if the coroutine has no argument.
155let resume_ty = liberated_sig.inputs().get(0).copied().unwrap_or(tcx.types.unit);
156157// Coroutines that come from coroutine closures have not yet determined
158 // their kind ty, so make a fresh infer var which will be constrained
159 // later during upvar analysis. Regular coroutines always have the kind
160 // ty of `().`
161let kind_ty = match kind {
162 hir::CoroutineKind::Desugared(_, hir::CoroutineSource::Closure) => {
163self.next_ty_var(expr_span)
164 }
165_ => tcx.types.unit,
166 };
167168let coroutine_args = ty::CoroutineArgs::new(
169 tcx,
170 ty::CoroutineArgsParts {
171 parent_args,
172 kind_ty,
173 resume_ty,
174 yield_ty,
175 return_ty: liberated_sig.output(),
176 tupled_upvars_ty,
177 },
178 );
179180 (
181 Ty::new_coroutine(tcx, expr_def_id.to_def_id(), coroutine_args.args),
182Some(CoroutineTypes { resume_ty, yield_ty }),
183 )
184 }
185 hir::ClosureKind::CoroutineClosure(kind) => {
186let (bound_return_ty, bound_yield_ty) = match kind {
187 hir::CoroutineDesugaring::Gen => {
188// `iter!` closures always return unit and yield the `Iterator::Item` type
189 // that we have to infer.
190(tcx.types.unit, self.infcx.next_ty_var(expr_span))
191 }
192 hir::CoroutineDesugaring::Async => {
193// async closures always return the type ascribed after the `->` (if present),
194 // and yield `()`.
195(bound_sig.skip_binder().output(), tcx.types.unit)
196 }
197 hir::CoroutineDesugaring::AsyncGen => {
198todo!("`async gen` closures not supported yet")
199 }
200 };
201// Compute all of the variables that will be used to populate the coroutine.
202let resume_ty = self.next_ty_var(expr_span);
203204let closure_kind_ty = match expected_kind {
205Some(kind) => Ty::from_closure_kind(tcx, kind),
206207// Create a type variable (for now) to represent the closure kind.
208 // It will be unified during the upvar inference phase (`upvar.rs`)
209None => self.next_ty_var(expr_span),
210 };
211212let coroutine_captures_by_ref_ty = self.next_ty_var(expr_span);
213let closure_args = ty::CoroutineClosureArgs::new(
214 tcx,
215 ty::CoroutineClosureArgsParts {
216 parent_args,
217 closure_kind_ty,
218 signature_parts_ty: Ty::new_fn_ptr(
219 tcx,
220 bound_sig.map_bound(|sig| {
221 tcx.mk_fn_sig(
222 [
223 resume_ty,
224 Ty::new_tup_from_iter(tcx, sig.inputs().iter().copied()),
225 ],
226 Ty::new_tup(tcx, &[bound_yield_ty, bound_return_ty]),
227 sig.fn_sig_kind,
228 )
229 }),
230 ),
231 tupled_upvars_ty,
232 coroutine_captures_by_ref_ty,
233 },
234 );
235236let coroutine_kind_ty = match expected_kind {
237Some(kind) => Ty::from_coroutine_closure_kind(tcx, kind),
238239// Create a type variable (for now) to represent the closure kind.
240 // It will be unified during the upvar inference phase (`upvar.rs`)
241None => self.next_ty_var(expr_span),
242 };
243244let coroutine_upvars_ty = self.next_ty_var(expr_span);
245246// We need to turn the liberated signature that we got from HIR, which
247 // looks something like `|Args...| -> T`, into a signature that is suitable
248 // for type checking the inner body of the closure, which always returns a
249 // coroutine. To do so, we use the `CoroutineClosureSignature` to compute
250 // the coroutine type, filling in the tupled_upvars_ty and kind_ty with infer
251 // vars which will get constrained during upvar analysis.
252let coroutine_output_ty = tcx.liberate_late_bound_regions(
253 expr_def_id.to_def_id(),
254 closure_args.coroutine_closure_sig().map_bound(|sig| {
255 sig.to_coroutine(
256 tcx,
257 parent_args,
258 coroutine_kind_ty,
259 tcx.coroutine_for_closure(expr_def_id),
260 coroutine_upvars_ty,
261 )
262 }),
263 );
264 liberated_sig = tcx.mk_fn_sig(
265 liberated_sig.inputs().iter().copied(),
266 coroutine_output_ty,
267 liberated_sig.fn_sig_kind,
268 );
269270 (Ty::new_coroutine_closure(tcx, expr_def_id.to_def_id(), closure_args.args), None)
271 }
272 };
273274 check_fn(
275&mut FnCtxt::new(self, self.param_env, closure.def_id),
276 liberated_sig,
277 coroutine_types,
278 closure.fn_decl,
279 expr_def_id,
280 body,
281// Closure "rust-call" ABI doesn't support unsized params
282false,
283 );
284285 closure_ty
286 }
287288/// Given the expected type, figures out what it can about this closure we
289 /// are about to type check:
290x;#[instrument(skip(self), level = "debug", ret)]291fn deduce_closure_signature(
292&self,
293 expected_ty: Ty<'tcx>,
294 closure_kind: hir::ClosureKind,
295 ) -> (Option<ExpectedSig<'tcx>>, Option<ty::ClosureKind>) {
296match *expected_ty.kind() {
297 ty::Alias(_, ty::AliasTy { kind: ty::Opaque { def_id }, args, .. }) => self
298.deduce_closure_signature_from_predicates(
299 expected_ty,
300 closure_kind,
301self.tcx
302 .explicit_item_self_bounds(def_id)
303 .iter_instantiated_copied(self.tcx, args)
304 .map(Unnormalized::skip_norm_wip)
305 .map(|(c, s)| (c.as_predicate(), s)),
306 ),
307 ty::Dynamic(object_type, ..) => {
308let sig = object_type.projection_bounds().find_map(|pb| {
309let pb = pb.with_self_ty(self.tcx, self.tcx.types.trait_object_dummy_self);
310self.deduce_sig_from_projection(None, closure_kind, pb)
311 });
312let kind = object_type
313 .principal_def_id()
314 .and_then(|did| self.tcx.fn_trait_kind_from_def_id(did));
315 (sig, kind)
316 }
317 ty::Infer(ty::TyVar(vid)) => self.deduce_closure_signature_from_predicates(
318 Ty::new_var(self.tcx, self.root_var(vid)),
319 closure_kind,
320self.obligations_for_self_ty(vid)
321 .into_iter()
322 .map(|obl| (obl.predicate, obl.cause.span)),
323 ),
324 ty::FnPtr(sig_tys, hdr) => match closure_kind {
325 hir::ClosureKind::Closure => {
326let expected_sig = ExpectedSig { cause_span: None, sig: sig_tys.with(hdr) };
327 (Some(expected_sig), Some(ty::ClosureKind::Fn))
328 }
329 hir::ClosureKind::Coroutine(_) | hir::ClosureKind::CoroutineClosure(_) => {
330 (None, None)
331 }
332 },
333_ => (None, None),
334 }
335 }
336337fn deduce_closure_signature_from_predicates(
338&self,
339 expected_ty: Ty<'tcx>,
340 closure_kind: hir::ClosureKind,
341 predicates: impl DoubleEndedIterator<Item = (ty::Predicate<'tcx>, Span)>,
342 ) -> (Option<ExpectedSig<'tcx>>, Option<ty::ClosureKind>) {
343let mut expected_sig = None;
344let mut expected_kind = None;
345346for (pred, span) in traits::elaborate(
347self.tcx,
348// Reverse the obligations here, since `elaborate_*` uses a stack,
349 // and we want to keep inference generally in the same order of
350 // the registered obligations.
351predicates.rev(),
352 )
353// We only care about self bounds
354.filter_only_self()
355 {
356{
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:356",
"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(356u32),
::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);
357let bound_predicate = pred.kind();
358359// Given a Projection predicate, we can potentially infer
360 // the complete signature.
361if expected_sig.is_none()
362 && let ty::PredicateKind::Clause(ty::ClauseKind::Projection(proj_predicate)) =
363 bound_predicate.skip_binder()
364 {
365let inferred_sig = self.normalize(
366 span,
367 Unnormalized::new_wip(self.deduce_sig_from_projection(
368Some(span),
369 closure_kind,
370 bound_predicate.rebind(proj_predicate),
371 )),
372 );
373374// Make sure that we didn't infer a signature that mentions itself.
375 // This can happen when we elaborate certain supertrait bounds that
376 // mention projections containing the `Self` type. See #105401.
377struct MentionsTy<'tcx> {
378 expected_ty: Ty<'tcx>,
379 }
380impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for MentionsTy<'tcx> {
381type Result = ControlFlow<()>;
382383fn visit_ty(&mut self, t: Ty<'tcx>) -> Self::Result {
384if t == self.expected_ty {
385 ControlFlow::Break(())
386 } else {
387 t.super_visit_with(self)
388 }
389 }
390 }
391392// Don't infer a closure signature from a goal that names the closure type as this will
393 // (almost always) lead to occurs check errors later in type checking.
394if self.next_trait_solver()
395 && let Some(inferred_sig) = inferred_sig
396 {
397// In the new solver it is difficult to explicitly normalize the inferred signature as we
398 // would have to manually handle universes and rewriting bound vars and placeholders back
399 // and forth.
400 //
401 // Instead we take advantage of the fact that we relating an inference variable with an alias
402 // will only instantiate the variable if the alias is rigid(*not quite). Concretely we:
403 // - Create some new variable `?sig`
404 // - Equate `?sig` with the unnormalized signature, e.g. `fn(<Foo<?x> as Trait>::Assoc)`
405 // - Depending on whether `<Foo<?x> as Trait>::Assoc` is rigid, ambiguous or normalizeable,
406 // we will either wind up with `?sig=<Foo<?x> as Trait>::Assoc/?y/ConcreteTy` respectively.
407 //
408 // *: In cases where there are ambiguous aliases in the signature that make use of bound vars
409 // they will wind up present in `?sig` even though they are non-rigid.
410 //
411 // This is a bit weird and means we may wind up discarding the goal due to it naming `expected_ty`
412 // even though the normalized form may not name `expected_ty`. However, this matches the existing
413 // behaviour of the old solver and would be technically a breaking change to fix.
414let generalized_fnptr_sig = self.next_ty_var(span);
415let inferred_fnptr_sig = Ty::new_fn_ptr(self.tcx, inferred_sig.sig);
416self.demand_eqtype(span, inferred_fnptr_sig, generalized_fnptr_sig);
417418let resolved_sig = self.resolve_vars_if_possible(generalized_fnptr_sig);
419420if resolved_sig.visit_with(&mut MentionsTy { expected_ty }).is_continue() {
421 expected_sig = Some(ExpectedSig {
422 cause_span: inferred_sig.cause_span,
423 sig: resolved_sig.fn_sig(self.tcx),
424 });
425 }
426 } else {
427if inferred_sig.visit_with(&mut MentionsTy { expected_ty }).is_continue() {
428 expected_sig = inferred_sig;
429 }
430 }
431 }
432433// Even if we can't infer the full signature, we may be able to
434 // infer the kind. This can occur when we elaborate a predicate
435 // like `F : Fn<A>`. Note that due to subtyping we could encounter
436 // many viable options, so pick the most restrictive.
437let trait_def_id = match bound_predicate.skip_binder() {
438 ty::PredicateKind::Clause(ty::ClauseKind::Projection(data)) => {
439Some(data.projection_term.trait_def_id(self.tcx))
440 }
441 ty::PredicateKind::Clause(ty::ClauseKind::Trait(data)) => Some(data.def_id()),
442_ => None,
443 };
444445if let Some(trait_def_id) = trait_def_id {
446let found_kind = match closure_kind {
447 hir::ClosureKind::Closure
448// FIXME(iter_macro): Someday we'll probably want iterator closures instead of
449 // just using Fn* for iterators.
450| hir::ClosureKind::CoroutineClosure(hir::CoroutineDesugaring::Gen) => {
451self.tcx.fn_trait_kind_from_def_id(trait_def_id)
452 }
453 hir::ClosureKind::CoroutineClosure(hir::CoroutineDesugaring::Async) => self
454.tcx
455 .async_fn_trait_kind_from_def_id(trait_def_id)
456 .or_else(|| self.tcx.fn_trait_kind_from_def_id(trait_def_id)),
457_ => None,
458 };
459460if let Some(found_kind) = found_kind {
461// always use the closure kind that is more permissive.
462match (expected_kind, found_kind) {
463 (None, _) => expected_kind = Some(found_kind),
464 (Some(ClosureKind::FnMut), ClosureKind::Fn) => {
465 expected_kind = Some(ClosureKind::Fn)
466 }
467 (Some(ClosureKind::FnOnce), ClosureKind::Fn | ClosureKind::FnMut) => {
468 expected_kind = Some(found_kind)
469 }
470_ => {}
471 }
472 }
473 }
474 }
475476 (expected_sig, expected_kind)
477 }
478479/// Given a projection like "<F as Fn(X)>::Result == Y", we can deduce
480 /// everything we need to know about a closure or coroutine.
481 ///
482 /// The `cause_span` should be the span that caused us to
483 /// have this expected signature, or `None` if we can't readily
484 /// know that.
485x;#[instrument(level = "debug", skip(self, cause_span), ret)]486fn deduce_sig_from_projection(
487&self,
488 cause_span: Option<Span>,
489 closure_kind: hir::ClosureKind,
490 projection: ty::PolyProjectionPredicate<'tcx>,
491 ) -> Option<ExpectedSig<'tcx>> {
492let def_id = projection.item_def_id();
493494// For now, we only do signature deduction based off of the `Fn` and `AsyncFn` traits,
495 // for closures and async closures, respectively.
496match closure_kind {
497 hir::ClosureKind::Closure if self.tcx.is_lang_item(def_id, LangItem::FnOnceOutput) => {
498self.extract_sig_from_projection(cause_span, projection)
499 }
500 hir::ClosureKind::CoroutineClosure(hir::CoroutineDesugaring::Async)
501if self.tcx.is_lang_item(def_id, LangItem::AsyncFnOnceOutput) =>
502 {
503self.extract_sig_from_projection(cause_span, projection)
504 }
505// It's possible we've passed the closure to a (somewhat out-of-fashion)
506 // `F: FnOnce() -> Fut, Fut: Future<Output = T>` style bound. Let's still
507 // guide inference here, since it's beneficial for the user.
508hir::ClosureKind::CoroutineClosure(hir::CoroutineDesugaring::Async)
509if self.tcx.is_lang_item(def_id, LangItem::FnOnceOutput) =>
510 {
511self.extract_sig_from_projection_and_future_bound(cause_span, projection)
512 }
513_ => None,
514 }
515 }
516517/// Given an `FnOnce::Output` or `AsyncFn::Output` projection, extract the args
518 /// and return type to infer a [`ty::PolyFnSig`] for the closure.
519fn extract_sig_from_projection(
520&self,
521 cause_span: Option<Span>,
522 projection: ty::PolyProjectionPredicate<'tcx>,
523 ) -> Option<ExpectedSig<'tcx>> {
524let projection = self.resolve_vars_if_possible(projection);
525526let arg_param_ty = projection.skip_binder().projection_term.args.type_at(1);
527{
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:527",
"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(527u32),
::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);
528529let ty::Tuple(input_tys) = *arg_param_ty.kind() else {
530return None;
531 };
532533// Since this is a return parameter type it is safe to unwrap.
534let ret_param_ty = projection.skip_binder().term.expect_type();
535{
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:535",
"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(535u32),
::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);
536537let sig = projection.rebind(self.tcx.mk_fn_sig_safe_rust_abi(input_tys, ret_param_ty));
538539Some(ExpectedSig { cause_span, sig })
540 }
541542/// When an async closure is passed to a function that has a "two-part" `Fn`
543 /// and `Future` trait bound, like:
544 ///
545 /// ```rust
546 /// use std::future::Future;
547 ///
548 /// fn not_exactly_an_async_closure<F, Fut>(_f: F)
549 /// where
550 /// F: FnOnce(String, u32) -> Fut,
551 /// Fut: Future<Output = i32>,
552 /// {}
553 /// ```
554 ///
555 /// The we want to be able to extract the signature to guide inference in the async
556 /// closure. We will have two projection predicates registered in this case. First,
557 /// we identify the `FnOnce<Args, Output = ?Fut>` bound, and if the output type is
558 /// an inference variable `?Fut`, we check if that is bounded by a `Future<Output = Ty>`
559 /// projection.
560 ///
561 /// This function is actually best-effort with the return type; if we don't find a
562 /// `Future` projection, we still will return arguments that we extracted from the `FnOnce`
563 /// projection, and the output will be an unconstrained type variable instead.
564fn extract_sig_from_projection_and_future_bound(
565&self,
566 cause_span: Option<Span>,
567 projection: ty::PolyProjectionPredicate<'tcx>,
568 ) -> Option<ExpectedSig<'tcx>> {
569let projection = self.resolve_vars_if_possible(projection);
570571let arg_param_ty = projection.skip_binder().projection_term.args.type_at(1);
572{
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:572",
"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(572u32),
::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);
573574let ty::Tuple(input_tys) = *arg_param_ty.kind() else {
575return None;
576 };
577578// If the return type is a type variable, look for bounds on it.
579 // We could theoretically support other kinds of return types here,
580 // but none of them would be useful, since async closures return
581 // concrete anonymous future types, and their futures are not coerced
582 // into any other type within the body of the async closure.
583let ty::Infer(ty::TyVar(return_vid)) = *projection.skip_binder().term.expect_type().kind()
584else {
585return None;
586 };
587588// FIXME: We may want to elaborate here, though I assume this will be exceedingly rare.
589let mut return_ty = None;
590for bound in self.obligations_for_self_ty(return_vid) {
591if let Some(ret_projection) = bound.predicate.as_projection_clause()
592 && let Some(ret_projection) = ret_projection.no_bound_vars()
593 && self.tcx.is_lang_item(ret_projection.def_id(), LangItem::FutureOutput)
594 {
595 return_ty = Some(ret_projection.term.expect_type());
596break;
597 }
598 }
599600// SUBTLE: If we didn't find a `Future<Output = ...>` bound for the return
601 // vid, we still want to attempt to provide inference guidance for the async
602 // closure's arguments. Instantiate a new vid to plug into the output type.
603 //
604 // You may be wondering, what if it's higher-ranked? Well, given that we
605 // found a type variable for the `FnOnce::Output` projection above, we know
606 // that the output can't mention any of the vars.
607 //
608 // Also note that we use a fresh var here for the signature since the signature
609 // records the output of the *future*, and `return_vid` above is the type
610 // variable of the future, not its output.
611 //
612 // FIXME: We probably should store this signature inference output in a way
613 // that does not misuse a `FnSig` type, but that can be done separately.
614let return_ty =
615return_ty.unwrap_or_else(|| self.next_ty_var(cause_span.unwrap_or(DUMMY_SP)));
616617let sig = projection.rebind(self.tcx.mk_fn_sig_safe_rust_abi(input_tys, return_ty));
618619Some(ExpectedSig { cause_span, sig })
620 }
621622fn sig_of_closure(
623&self,
624 expr_def_id: LocalDefId,
625 decl: &hir::FnDecl<'tcx>,
626 closure_kind: hir::ClosureKind,
627 expected_sig: Option<ExpectedSig<'tcx>>,
628 ) -> ClosureSignatures<'tcx> {
629if let Some(e) = expected_sig {
630self.sig_of_closure_with_expectation(expr_def_id, decl, closure_kind, e)
631 } else {
632self.sig_of_closure_no_expectation(expr_def_id, decl, closure_kind)
633 }
634 }
635636/// If there is no expected signature, then we will convert the
637 /// types that the user gave into a signature.
638#[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(638u32),
::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")]639fn sig_of_closure_no_expectation(
640&self,
641 expr_def_id: LocalDefId,
642 decl: &hir::FnDecl<'tcx>,
643 closure_kind: hir::ClosureKind,
644 ) -> ClosureSignatures<'tcx> {
645let bound_sig = self.supplied_sig_of_closure(expr_def_id, decl, closure_kind);
646647self.closure_sigs(expr_def_id, bound_sig)
648 }
649650/// Invoked to compute the signature of a closure expression. This
651 /// combines any user-provided type annotations (e.g., `|x: u32|
652 /// -> u32 { .. }`) with the expected signature.
653 ///
654 /// The approach is as follows:
655 ///
656 /// - Let `S` be the (higher-ranked) signature that we derive from the user's annotations.
657 /// - Let `E` be the (higher-ranked) signature that we derive from the expectations, if any.
658 /// - If we have no expectation `E`, then the signature of the closure is `S`.
659 /// - Otherwise, the signature of the closure is E. Moreover:
660 /// - Skolemize the late-bound regions in `E`, yielding `E'`.
661 /// - Instantiate all the late-bound regions bound in the closure within `S`
662 /// with fresh (existential) variables, yielding `S'`
663 /// - Require that `E' = S'`
664 /// - We could use some kind of subtyping relationship here,
665 /// I imagine, but equality is easier and works fine for
666 /// our purposes.
667 ///
668 /// The key intuition here is that the user's types must be valid
669 /// from "the inside" of the closure, but the expectation
670 /// ultimately drives the overall signature.
671 ///
672 /// # Examples
673 ///
674 /// ```ignore (illustrative)
675 /// fn with_closure<F>(_: F)
676 /// where F: Fn(&u32) -> &u32 { .. }
677 ///
678 /// with_closure(|x: &u32| { ... })
679 /// ```
680 ///
681 /// Here:
682 /// - E would be `fn(&u32) -> &u32`.
683 /// - S would be `fn(&u32) -> ?T`
684 /// - E' is `&'!0 u32 -> &'!0 u32`
685 /// - S' is `&'?0 u32 -> ?T`
686 ///
687 /// S' can be unified with E' with `['?0 = '!0, ?T = &'!10 u32]`.
688 ///
689 /// # Arguments
690 ///
691 /// - `expr_def_id`: the `LocalDefId` of the closure expression
692 /// - `decl`: the HIR declaration of the closure
693 /// - `body`: the body of the closure
694 /// - `expected_sig`: the expected signature (if any). Note that
695 /// this is missing a binder: that is, there may be late-bound
696 /// regions with depth 1, which are bound then by the closure.
697#[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(697u32),
::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|
{
let fn_sig_kind =
FnSigKind::default().set_abi(ExternAbi::RustCall).set_safety(hir::Safety::Safe).set_c_variadic(sig.c_variadic());
self.tcx.mk_fn_sig(sig.inputs().iter().cloned(),
sig.output(), fn_sig_kind)
});
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")]698fn sig_of_closure_with_expectation(
699&self,
700 expr_def_id: LocalDefId,
701 decl: &hir::FnDecl<'tcx>,
702 closure_kind: hir::ClosureKind,
703 expected_sig: ExpectedSig<'tcx>,
704 ) -> ClosureSignatures<'tcx> {
705// Watch out for some surprises and just ignore the
706 // expectation if things don't see to match up with what we
707 // expect.
708if expected_sig.sig.c_variadic() != decl.c_variadic() {
709return self.sig_of_closure_no_expectation(expr_def_id, decl, closure_kind);
710 } else if expected_sig.sig.skip_binder().inputs_and_output.len() != decl.inputs.len() + 1 {
711return self.sig_of_closure_with_mismatched_number_of_arguments(
712 expr_def_id,
713 decl,
714 expected_sig,
715 );
716 }
717718// Create a `PolyFnSig`. Note the oddity that late bound
719 // regions appearing free in `expected_sig` are now bound up
720 // in this binder we are creating.
721assert!(!expected_sig.sig.skip_binder().has_vars_bound_above(ty::INNERMOST));
722let bound_sig = expected_sig.sig.map_bound(|sig| {
723// Ignore splatting, it is unsupported on closures.
724let fn_sig_kind = FnSigKind::default()
725 .set_abi(ExternAbi::RustCall)
726 .set_safety(hir::Safety::Safe)
727 .set_c_variadic(sig.c_variadic());
728self.tcx.mk_fn_sig(sig.inputs().iter().cloned(), sig.output(), fn_sig_kind)
729 });
730731// `deduce_expectations_from_expected_type` introduces
732 // late-bound lifetimes defined elsewhere, which we now
733 // anonymize away, so as not to confuse the user.
734let bound_sig = self.tcx.anonymize_bound_vars(bound_sig);
735736let closure_sigs = self.closure_sigs(expr_def_id, bound_sig);
737738// Up till this point, we have ignored the annotations that the user
739 // gave. This function will check that they unify successfully.
740 // Along the way, it also writes out entries for types that the user
741 // wrote into our typeck results, which are then later used by the privacy
742 // check.
743match self.merge_supplied_sig_with_expectation(
744 expr_def_id,
745 decl,
746 closure_kind,
747 closure_sigs,
748 ) {
749Ok(infer_ok) => self.register_infer_ok_obligations(infer_ok),
750Err(_) => self.sig_of_closure_no_expectation(expr_def_id, decl, closure_kind),
751 }
752 }
753754fn sig_of_closure_with_mismatched_number_of_arguments(
755&self,
756 expr_def_id: LocalDefId,
757 decl: &hir::FnDecl<'tcx>,
758 expected_sig: ExpectedSig<'tcx>,
759 ) -> ClosureSignatures<'tcx> {
760let expr_map_node = self.tcx.hir_node_by_def_id(expr_def_id);
761let expected_args: Vec<_> = expected_sig762 .sig
763 .skip_binder()
764 .inputs()
765 .iter()
766 .map(|ty| ArgKind::from_expected_ty(*ty, None))
767 .collect();
768let (closure_span, closure_arg_span, found_args) =
769match self.err_ctxt().get_fn_like_arguments(expr_map_node) {
770Some((sp, arg_sp, args)) => (Some(sp), arg_sp, args),
771None => (None, None, Vec::new()),
772 };
773let expected_span =
774expected_sig.cause_span.unwrap_or_else(|| self.tcx.def_span(expr_def_id));
775let guar = self776 .err_ctxt()
777 .report_arg_count_mismatch(
778expected_span,
779closure_span,
780expected_args,
781found_args,
782true,
783closure_arg_span,
784 )
785 .emit();
786787let error_sig = self.error_sig_of_closure(decl, guar);
788789self.closure_sigs(expr_def_id, error_sig)
790 }
791792/// Enforce the user's types against the expectation. See
793 /// `sig_of_closure_with_expectation` for details on the overall
794 /// strategy.
795#[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(795u32),
::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:809",
"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(809u32),
::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));
let fn_sig_kind =
FnSigKind::default().set_abi(ExternAbi::RustCall).set_safety(hir::Safety::Safe).set_c_variadic(expected_sigs.liberated_sig.c_variadic());
expected_sigs.liberated_sig =
self.tcx.mk_fn_sig(inputs, supplied_output_ty, fn_sig_kind);
Ok(InferOk {
value: expected_sigs,
obligations: all_obligations,
})
})
}
}
}#[instrument(level = "debug", skip(self, expr_def_id, decl, expected_sigs))]796fn merge_supplied_sig_with_expectation(
797&self,
798 expr_def_id: LocalDefId,
799 decl: &hir::FnDecl<'tcx>,
800 closure_kind: hir::ClosureKind,
801mut expected_sigs: ClosureSignatures<'tcx>,
802 ) -> InferResult<'tcx, ClosureSignatures<'tcx>> {
803// Get the signature S that the user gave.
804 //
805 // (See comment on `sig_of_closure_with_expectation` for the
806 // meaning of these letters.)
807let supplied_sig = self.supplied_sig_of_closure(expr_def_id, decl, closure_kind);
808809debug!(?supplied_sig);
810811// FIXME(#45727): As discussed in [this comment][c1], naively
812 // forcing equality here actually results in suboptimal error
813 // messages in some cases. For now, if there would have been
814 // an obvious error, we fallback to declaring the type of the
815 // closure to be the one the user gave, which allows other
816 // error message code to trigger.
817 //
818 // However, I think [there is potential to do even better
819 // here][c2], since in *this* code we have the precise span of
820 // the type parameter in question in hand when we report the
821 // error.
822 //
823 // [c1]: https://github.com/rust-lang/rust/pull/45072#issuecomment-341089706
824 // [c2]: https://github.com/rust-lang/rust/pull/45072#issuecomment-341096796
825self.commit_if_ok(|_| {
826let mut all_obligations = PredicateObligations::new();
827let supplied_sig = self.instantiate_binder_with_fresh_vars(
828self.tcx.def_span(expr_def_id),
829 BoundRegionConversionTime::FnCall,
830 supplied_sig,
831 );
832833// The liberated version of this signature should be a subtype
834 // of the liberated form of the expectation.
835for ((hir_ty, &supplied_ty), expected_ty) in iter::zip(
836 iter::zip(decl.inputs, supplied_sig.inputs()),
837 expected_sigs.liberated_sig.inputs(), // `liberated_sig` is E'.
838) {
839// Check that E' = S'.
840let cause = self.misc(hir_ty.span);
841let InferOk { value: (), obligations } = self.at(&cause, self.param_env).eq(
842 DefineOpaqueTypes::Yes,
843*expected_ty,
844 supplied_ty,
845 )?;
846 all_obligations.extend(obligations);
847 }
848849let supplied_output_ty = supplied_sig.output();
850let cause = &self.misc(decl.output.span());
851let InferOk { value: (), obligations } = self.at(cause, self.param_env).eq(
852 DefineOpaqueTypes::Yes,
853 expected_sigs.liberated_sig.output(),
854 supplied_output_ty,
855 )?;
856 all_obligations.extend(obligations);
857858let inputs =
859 supplied_sig.inputs().into_iter().map(|&ty| self.resolve_vars_if_possible(ty));
860861let fn_sig_kind = FnSigKind::default()
862 .set_abi(ExternAbi::RustCall)
863 .set_safety(hir::Safety::Safe)
864 .set_c_variadic(expected_sigs.liberated_sig.c_variadic());
865 expected_sigs.liberated_sig =
866self.tcx.mk_fn_sig(inputs, supplied_output_ty, fn_sig_kind);
867868Ok(InferOk { value: expected_sigs, obligations: all_obligations })
869 })
870 }
871872/// If there is no expected signature, then we will convert the
873 /// types that the user gave into a signature.
874 ///
875 /// Also, record this closure signature for later.
876x;#[instrument(skip(self, decl), level = "debug", ret)]877fn supplied_sig_of_closure(
878&self,
879 expr_def_id: LocalDefId,
880 decl: &hir::FnDecl<'tcx>,
881 closure_kind: hir::ClosureKind,
882 ) -> ty::PolyFnSig<'tcx> {
883let lowerer = self.lowerer();
884885trace!("decl = {:#?}", decl);
886debug!(?closure_kind);
887888let hir_id = self.tcx.local_def_id_to_hir_id(expr_def_id);
889let bound_vars = self.tcx.late_bound_vars(hir_id);
890891// First, convert the types that the user supplied (if any).
892let supplied_arguments = decl.inputs.iter().map(|a| lowerer.lower_ty(a));
893let supplied_return = match decl.output {
894 hir::FnRetTy::Return(ref output) => lowerer.lower_ty(output),
895 hir::FnRetTy::DefaultReturn(_) => match closure_kind {
896// In the case of the async block that we create for a function body,
897 // we expect the return type of the block to match that of the enclosing
898 // function.
899hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
900 hir::CoroutineDesugaring::Async,
901 hir::CoroutineSource::Fn,
902 )) => {
903debug!("closure is async fn body");
904self.deduce_future_output_from_obligations(expr_def_id).unwrap_or_else(|| {
905// AFAIK, deducing the future output
906 // always succeeds *except* in error cases
907 // like #65159. I'd like to return Error
908 // here, but I can't because I can't
909 // easily (and locally) prove that we
910 // *have* reported an
911 // error. --nikomatsakis
912lowerer.ty_infer(None, decl.output.span())
913 })
914 }
915// All `gen {}` and `async gen {}` must return unit.
916hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
917 hir::CoroutineDesugaring::Gen | hir::CoroutineDesugaring::AsyncGen,
918_,
919 )) => self.tcx.types.unit,
920921// For async blocks, we just fall back to `_` here.
922 // For closures/coroutines, we know nothing about the return
923 // type unless it was supplied.
924hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
925 hir::CoroutineDesugaring::Async,
926_,
927 ))
928 | hir::ClosureKind::Coroutine(hir::CoroutineKind::Coroutine(_))
929 | hir::ClosureKind::Closure
930 | hir::ClosureKind::CoroutineClosure(_) => {
931 lowerer.ty_infer(None, decl.output.span())
932 }
933 },
934 };
935936let fn_sig_kind = FnSigKind::default()
937 .set_abi(ExternAbi::RustCall)
938 .set_safety(hir::Safety::Safe)
939 .set_c_variadic(decl.c_variadic());
940let result = ty::Binder::bind_with_vars(
941self.tcx.mk_fn_sig(supplied_arguments, supplied_return, fn_sig_kind),
942 bound_vars,
943 );
944945let c_result = self.infcx.canonicalize_response(result);
946self.typeck_results.borrow_mut().user_provided_sigs.insert(expr_def_id, c_result);
947948// Normalize only after registering in `user_provided_sigs`.
949self.normalize(self.tcx.def_span(expr_def_id), Unnormalized::new_wip(result))
950 }
951952/// Invoked when we are translating the coroutine that results
953 /// from desugaring an `async fn`. Returns the "sugared" return
954 /// type of the `async fn` -- that is, the return type that the
955 /// user specified. The "desugared" return type is an `impl
956 /// Future<Output = T>`, so we do this by searching through the
957 /// obligations to extract the `T`.
958x;#[instrument(skip(self), level = "debug", ret)]959fn deduce_future_output_from_obligations(&self, body_def_id: LocalDefId) -> Option<Ty<'tcx>> {
960let ret_coercion = self.ret_coercion.as_ref().unwrap_or_else(|| {
961span_bug!(self.tcx.def_span(body_def_id), "async fn coroutine outside of a fn")
962 });
963964let closure_span = self.tcx.def_span(body_def_id);
965let ret_ty = ret_coercion.borrow().expected_ty();
966let ret_ty = self.resolve_vars_with_obligations(ret_ty);
967968let get_future_output = |predicate: ty::Predicate<'tcx>, span| {
969// Search for a pending obligation like
970 //
971 // `<R as Future>::Output = T`
972 //
973 // where R is the return type we are expecting. This type `T`
974 // will be our output.
975let bound_predicate = predicate.kind();
976if let ty::PredicateKind::Clause(ty::ClauseKind::Projection(proj_predicate)) =
977 bound_predicate.skip_binder()
978 {
979self.deduce_future_output_from_projection(
980 span,
981 bound_predicate.rebind(proj_predicate),
982 )
983 } else {
984None
985}
986 };
987988let output_ty = match *ret_ty.kind() {
989 ty::Infer(ty::TyVar(ret_vid)) => {
990self.obligations_for_self_ty(ret_vid).into_iter().find_map(|obligation| {
991 get_future_output(obligation.predicate, obligation.cause.span)
992 })?
993}
994 ty::Alias(_, ty::AliasTy { kind: ty::Projection { .. }, .. }) => {
995return Some(Ty::new_error_with_message(
996self.tcx,
997 closure_span,
998"this projection should have been projected to an opaque type",
999 ));
1000 }
1001 ty::Alias(_, ty::AliasTy { kind: ty::Opaque { def_id }, args, .. }) => self
1002.tcx
1003 .explicit_item_self_bounds(def_id)
1004 .iter_instantiated_copied(self.tcx, args)
1005 .map(Unnormalized::skip_norm_wip)
1006 .find_map(|(p, s)| get_future_output(p.as_predicate(), s))?,
1007 ty::Error(_) => return Some(ret_ty),
1008_ => {
1009span_bug!(closure_span, "invalid async fn coroutine return type: {ret_ty:?}")
1010 }
1011 };
10121013let output_ty = self.normalize(closure_span, Unnormalized::new_wip(output_ty));
10141015// async fn that have opaque types in their return type need to redo the conversion to inference variables
1016 // as they fetch the still opaque version from the signature.
1017let InferOk { value: output_ty, obligations } = self
1018.replace_opaque_types_with_inference_vars(
1019 output_ty,
1020 body_def_id,
1021 closure_span,
1022self.param_env,
1023 );
1024self.register_predicates(obligations);
10251026Some(output_ty)
1027 }
10281029/// Given a projection like
1030 ///
1031 /// `<X as Future>::Output = T`
1032 ///
1033 /// where `X` is some type that has no late-bound regions, returns
1034 /// `Some(T)`. If the projection is for some other trait, returns
1035 /// `None`.
1036fn deduce_future_output_from_projection(
1037&self,
1038 cause_span: Span,
1039 predicate: ty::PolyProjectionPredicate<'tcx>,
1040 ) -> Option<Ty<'tcx>> {
1041{
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:1041",
"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(1041u32),
::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);
10421043// We do not expect any bound regions in our predicate, so
1044 // skip past the bound vars.
1045let Some(predicate) = predicate.no_bound_vars() else {
1046{
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:1046",
"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(1046u32),
::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");
1047return None;
1048 };
10491050// Check that this is a projection from the `Future` trait.
1051let trait_def_id = predicate.projection_term.trait_def_id(self.tcx);
1052if !self.tcx.is_lang_item(trait_def_id, LangItem::Future) {
1053{
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:1053",
"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(1053u32),
::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");
1054return None;
1055 }
10561057// The `Future` trait has only one associated item, `Output`,
1058 // so check that this is what we see.
1059let output_assoc_item = self.tcx.associated_item_def_ids(trait_def_id)[0];
1060if output_assoc_item != predicate.def_id() {
1061::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.kind, output_assoc_item));span_bug!(
1062 cause_span,
1063"projecting associated item `{:?}` from future, which is not Output `{:?}`",
1064 predicate.projection_term.kind,
1065 output_assoc_item,
1066 );
1067 }
10681069// Extract the type from the projection. Note that there can
1070 // be no bound variables in this type because the "self type"
1071 // does not have any regions in it.
1072let output_ty = self.resolve_vars_if_possible(predicate.term);
1073{
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:1073",
"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(1073u32),
::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);
1074// This is a projection on a Fn trait so will always be a type.
1075Some(output_ty.expect_type())
1076 }
10771078/// Converts the types that the user supplied, in case that doing
1079 /// so should yield an error, but returns back a signature where
1080 /// all parameters are of type `ty::Error`.
1081fn error_sig_of_closure(
1082&self,
1083 decl: &hir::FnDecl<'tcx>,
1084 guar: ErrorGuaranteed,
1085 ) -> ty::PolyFnSig<'tcx> {
1086let lowerer = self.lowerer();
1087let err_ty = Ty::new_error(self.tcx, guar);
10881089let supplied_arguments = decl.inputs.iter().map(|a| {
1090// Convert the types that the user supplied (if any), but ignore them.
1091lowerer.lower_ty(a);
1092err_ty1093 });
10941095if let hir::FnRetTy::Return(ref output) = decl.output {
1096lowerer.lower_ty(output);
1097 }
10981099let fn_sig_kind = FnSigKind::default()
1100 .set_abi(ExternAbi::RustCall)
1101 .set_safety(hir::Safety::Safe)
1102 .set_c_variadic(decl.c_variadic());
1103let result = ty::Binder::dummy(self.tcx.mk_fn_sig(supplied_arguments, err_ty, fn_sig_kind));
11041105{
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:1105",
"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(1105u32),
::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);
11061107result1108 }
11091110x;#[instrument(level = "debug", skip(self), ret)]1111fn closure_sigs(
1112&self,
1113 expr_def_id: LocalDefId,
1114 bound_sig: ty::PolyFnSig<'tcx>,
1115 ) -> ClosureSignatures<'tcx> {
1116let liberated_sig =
1117self.tcx().liberate_late_bound_regions(expr_def_id.to_def_id(), bound_sig);
1118let liberated_sig =
1119self.normalize(self.tcx.def_span(expr_def_id), Unnormalized::new_wip(liberated_sig));
1120 ClosureSignatures { bound_sig, liberated_sig }
1121 }
1122}