rustc_hir_typeck/closure.rs
1//! Code for type-checking closure expressions.
2
3use std::iter;
4use std::ops::ControlFlow;
5
6use rustc_abi::ExternAbi;
7use rustc_errors::ErrorGuaranteed;
8use rustc_hir as 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::{
16 self, ClosureKind, GenericArgs, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable,
17 TypeVisitableExt, 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};
24
25use super::{CoroutineTypes, Expectation, FnCtxt, check_fn};
26
27/// What signature do we *expect* the closure to have from context?
28#[derive(Debug, Clone, TypeFoldable, TypeVisitable)]
29struct ExpectedSig<'tcx> {
30 /// Span that gave us this expectation, if we know that.
31 cause_span: Option<Span>,
32 sig: ty::PolyFnSig<'tcx>,
33}
34
35#[derive(Debug)]
36struct ClosureSignatures<'tcx> {
37 /// The signature users of the closure see.
38 bound_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.
43 liberated_sig: ty::FnSig<'tcx>,
44}
45
46impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
47 #[instrument(skip(self, closure), level = "debug")]
48 pub(crate) fn check_expr_closure(
49 &self,
50 closure: &hir::Closure<'tcx>,
51 expr_span: Span,
52 expected: Expectation<'tcx>,
53 ) -> Ty<'tcx> {
54 let tcx = self.tcx;
55 let body = tcx.hir_body(closure.body);
56 let expr_def_id = closure.def_id;
57
58 // 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.
61 let (expected_sig, expected_kind) = match expected.to_option(self) {
62 Some(ty) => self.deduce_closure_signature(
63 self.try_structurally_resolve_type(expr_span, ty),
64 closure.kind,
65 ),
66 None => (None, None),
67 };
68
69 let ClosureSignatures { bound_sig, mut liberated_sig } =
70 self.sig_of_closure(expr_def_id, closure.fn_decl, closure.kind, expected_sig);
71
72 debug!(?bound_sig, ?liberated_sig);
73
74 let parent_args =
75 GenericArgs::identity_for_item(tcx, tcx.typeck_root_def_id(expr_def_id.to_def_id()));
76
77 let tupled_upvars_ty = self.next_ty_var(expr_span);
78
79 // 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.
83 let (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.
87 let 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 });
96
97 debug!(?sig, ?expected_kind);
98
99 let closure_kind_ty = match expected_kind {
100 Some(kind) => Ty::from_closure_kind(tcx, kind),
101
102 // Create a type variable (for now) to represent the closure kind.
103 // It will be unified during the upvar inference phase (`upvar.rs`)
104 None => self.next_ty_var(expr_span),
105 };
106
107 let 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 );
116
117 (Ty::new_closure(tcx, expr_def_id.to_def_id(), closure_args.args), None)
118 }
119 hir::ClosureKind::Coroutine(kind) => {
120 let yield_ty = match kind {
121 hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Gen, _)
122 | hir::CoroutineKind::Coroutine(_) => {
123 let yield_ty = self.next_ty_var(expr_span);
124 self.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.
135 hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::AsyncGen, _) => {
136 let yield_ty = self.next_ty_var(expr_span);
137 self.require_type_is_sized(
138 yield_ty,
139 expr_span,
140 ObligationCauseCode::SizedYieldType,
141 );
142
143 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 };
160
161 // Resume type defaults to `()` if the coroutine has no argument.
162 let resume_ty = liberated_sig.inputs().get(0).copied().unwrap_or(tcx.types.unit);
163
164 let interior = Ty::new_coroutine_witness(tcx, expr_def_id.to_def_id(), parent_args);
165
166 // Coroutines that come from coroutine closures have not yet determined
167 // their kind ty, so make a fresh infer var which will be constrained
168 // later during upvar analysis. Regular coroutines always have the kind
169 // ty of `().`
170 let kind_ty = match kind {
171 hir::CoroutineKind::Desugared(_, hir::CoroutineSource::Closure) => {
172 self.next_ty_var(expr_span)
173 }
174 _ => tcx.types.unit,
175 };
176
177 let coroutine_args = ty::CoroutineArgs::new(
178 tcx,
179 ty::CoroutineArgsParts {
180 parent_args,
181 kind_ty,
182 resume_ty,
183 yield_ty,
184 return_ty: liberated_sig.output(),
185 witness: interior,
186 tupled_upvars_ty,
187 },
188 );
189
190 (
191 Ty::new_coroutine(tcx, expr_def_id.to_def_id(), coroutine_args.args),
192 Some(CoroutineTypes { resume_ty, yield_ty }),
193 )
194 }
195 hir::ClosureKind::CoroutineClosure(kind) => {
196 let (bound_return_ty, bound_yield_ty) = match kind {
197 hir::CoroutineDesugaring::Gen => {
198 // `iter!` closures always return unit and yield the `Iterator::Item` type
199 // that we have to infer.
200 (tcx.types.unit, self.infcx.next_ty_var(expr_span))
201 }
202 hir::CoroutineDesugaring::Async => {
203 // async closures always return the type ascribed after the `->` (if present),
204 // and yield `()`.
205 (bound_sig.skip_binder().output(), tcx.types.unit)
206 }
207 hir::CoroutineDesugaring::AsyncGen => {
208 todo!("`async gen` closures not supported yet")
209 }
210 };
211 // Compute all of the variables that will be used to populate the coroutine.
212 let resume_ty = self.next_ty_var(expr_span);
213 let interior = self.next_ty_var(expr_span);
214
215 let closure_kind_ty = match expected_kind {
216 Some(kind) => Ty::from_closure_kind(tcx, kind),
217
218 // Create a type variable (for now) to represent the closure kind.
219 // It will be unified during the upvar inference phase (`upvar.rs`)
220 None => self.next_ty_var(expr_span),
221 };
222
223 let coroutine_captures_by_ref_ty = self.next_ty_var(expr_span);
224 let closure_args = ty::CoroutineClosureArgs::new(
225 tcx,
226 ty::CoroutineClosureArgsParts {
227 parent_args,
228 closure_kind_ty,
229 signature_parts_ty: Ty::new_fn_ptr(
230 tcx,
231 bound_sig.map_bound(|sig| {
232 tcx.mk_fn_sig(
233 [
234 resume_ty,
235 Ty::new_tup_from_iter(tcx, sig.inputs().iter().copied()),
236 ],
237 Ty::new_tup(tcx, &[bound_yield_ty, bound_return_ty]),
238 sig.c_variadic,
239 sig.safety,
240 sig.abi,
241 )
242 }),
243 ),
244 tupled_upvars_ty,
245 coroutine_captures_by_ref_ty,
246 coroutine_witness_ty: interior,
247 },
248 );
249
250 let coroutine_kind_ty = match expected_kind {
251 Some(kind) => Ty::from_coroutine_closure_kind(tcx, kind),
252
253 // Create a type variable (for now) to represent the closure kind.
254 // It will be unified during the upvar inference phase (`upvar.rs`)
255 None => self.next_ty_var(expr_span),
256 };
257
258 let coroutine_upvars_ty = self.next_ty_var(expr_span);
259
260 // We need to turn the liberated signature that we got from HIR, which
261 // looks something like `|Args...| -> T`, into a signature that is suitable
262 // for type checking the inner body of the closure, which always returns a
263 // coroutine. To do so, we use the `CoroutineClosureSignature` to compute
264 // the coroutine type, filling in the tupled_upvars_ty and kind_ty with infer
265 // vars which will get constrained during upvar analysis.
266 let coroutine_output_ty = tcx.liberate_late_bound_regions(
267 expr_def_id.to_def_id(),
268 closure_args.coroutine_closure_sig().map_bound(|sig| {
269 sig.to_coroutine(
270 tcx,
271 parent_args,
272 coroutine_kind_ty,
273 tcx.coroutine_for_closure(expr_def_id),
274 coroutine_upvars_ty,
275 )
276 }),
277 );
278 liberated_sig = tcx.mk_fn_sig(
279 liberated_sig.inputs().iter().copied(),
280 coroutine_output_ty,
281 liberated_sig.c_variadic,
282 liberated_sig.safety,
283 liberated_sig.abi,
284 );
285
286 (Ty::new_coroutine_closure(tcx, expr_def_id.to_def_id(), closure_args.args), None)
287 }
288 };
289
290 check_fn(
291 &mut FnCtxt::new(self, self.param_env, closure.def_id),
292 liberated_sig,
293 coroutine_types,
294 closure.fn_decl,
295 expr_def_id,
296 body,
297 // Closure "rust-call" ABI doesn't support unsized params
298 false,
299 );
300
301 closure_ty
302 }
303
304 /// Given the expected type, figures out what it can about this closure we
305 /// are about to type check:
306 #[instrument(skip(self), level = "debug", ret)]
307 fn deduce_closure_signature(
308 &self,
309 expected_ty: Ty<'tcx>,
310 closure_kind: hir::ClosureKind,
311 ) -> (Option<ExpectedSig<'tcx>>, Option<ty::ClosureKind>) {
312 match *expected_ty.kind() {
313 ty::Alias(ty::Opaque, ty::AliasTy { def_id, args, .. }) => self
314 .deduce_closure_signature_from_predicates(
315 expected_ty,
316 closure_kind,
317 self.tcx
318 .explicit_item_self_bounds(def_id)
319 .iter_instantiated_copied(self.tcx, args)
320 .map(|(c, s)| (c.as_predicate(), s)),
321 ),
322 ty::Dynamic(object_type, ..) => {
323 let sig = object_type.projection_bounds().find_map(|pb| {
324 let pb = pb.with_self_ty(self.tcx, self.tcx.types.trait_object_dummy_self);
325 self.deduce_sig_from_projection(None, closure_kind, pb)
326 });
327 let kind = object_type
328 .principal_def_id()
329 .and_then(|did| self.tcx.fn_trait_kind_from_def_id(did));
330 (sig, kind)
331 }
332 ty::Infer(ty::TyVar(vid)) => self.deduce_closure_signature_from_predicates(
333 Ty::new_var(self.tcx, self.root_var(vid)),
334 closure_kind,
335 self.obligations_for_self_ty(vid)
336 .into_iter()
337 .map(|obl| (obl.predicate, obl.cause.span)),
338 ),
339 ty::FnPtr(sig_tys, hdr) => match closure_kind {
340 hir::ClosureKind::Closure => {
341 let expected_sig = ExpectedSig { cause_span: None, sig: sig_tys.with(hdr) };
342 (Some(expected_sig), Some(ty::ClosureKind::Fn))
343 }
344 hir::ClosureKind::Coroutine(_) | hir::ClosureKind::CoroutineClosure(_) => {
345 (None, None)
346 }
347 },
348 _ => (None, None),
349 }
350 }
351
352 fn deduce_closure_signature_from_predicates(
353 &self,
354 expected_ty: Ty<'tcx>,
355 closure_kind: hir::ClosureKind,
356 predicates: impl DoubleEndedIterator<Item = (ty::Predicate<'tcx>, Span)>,
357 ) -> (Option<ExpectedSig<'tcx>>, Option<ty::ClosureKind>) {
358 let mut expected_sig = None;
359 let mut expected_kind = None;
360
361 for (pred, span) in traits::elaborate(
362 self.tcx,
363 // Reverse the obligations here, since `elaborate_*` uses a stack,
364 // and we want to keep inference generally in the same order of
365 // the registered obligations.
366 predicates.rev(),
367 )
368 // We only care about self bounds
369 .filter_only_self()
370 {
371 debug!(?pred);
372 let bound_predicate = pred.kind();
373
374 // Given a Projection predicate, we can potentially infer
375 // the complete signature.
376 if expected_sig.is_none()
377 && let ty::PredicateKind::Clause(ty::ClauseKind::Projection(proj_predicate)) =
378 bound_predicate.skip_binder()
379 {
380 let inferred_sig = self.normalize(
381 span,
382 self.deduce_sig_from_projection(
383 Some(span),
384 closure_kind,
385 bound_predicate.rebind(proj_predicate),
386 ),
387 );
388
389 // Make sure that we didn't infer a signature that mentions itself.
390 // This can happen when we elaborate certain supertrait bounds that
391 // mention projections containing the `Self` type. See #105401.
392 struct MentionsTy<'tcx> {
393 expected_ty: Ty<'tcx>,
394 }
395 impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for MentionsTy<'tcx> {
396 type Result = ControlFlow<()>;
397
398 fn visit_ty(&mut self, t: Ty<'tcx>) -> Self::Result {
399 if t == self.expected_ty {
400 ControlFlow::Break(())
401 } else {
402 t.super_visit_with(self)
403 }
404 }
405 }
406
407 // Don't infer a closure signature from a goal that names the closure type as this will
408 // (almost always) lead to occurs check errors later in type checking.
409 if self.next_trait_solver()
410 && let Some(inferred_sig) = inferred_sig
411 {
412 // In the new solver it is difficult to explicitly normalize the inferred signature as we
413 // would have to manually handle universes and rewriting bound vars and placeholders back
414 // and forth.
415 //
416 // Instead we take advantage of the fact that we relating an inference variable with an alias
417 // will only instantiate the variable if the alias is rigid(*not quite). Concretely we:
418 // - Create some new variable `?sig`
419 // - Equate `?sig` with the unnormalized signature, e.g. `fn(<Foo<?x> as Trait>::Assoc)`
420 // - Depending on whether `<Foo<?x> as Trait>::Assoc` is rigid, ambiguous or normalizeable,
421 // we will either wind up with `?sig=<Foo<?x> as Trait>::Assoc/?y/ConcreteTy` respectively.
422 //
423 // *: In cases where there are ambiguous aliases in the signature that make use of bound vars
424 // they will wind up present in `?sig` even though they are non-rigid.
425 //
426 // This is a bit weird and means we may wind up discarding the goal due to it naming `expected_ty`
427 // even though the normalized form may not name `expected_ty`. However, this matches the existing
428 // behaviour of the old solver and would be technically a breaking change to fix.
429 let generalized_fnptr_sig = self.next_ty_var(span);
430 let inferred_fnptr_sig = Ty::new_fn_ptr(self.tcx, inferred_sig.sig);
431 self.demand_eqtype(span, inferred_fnptr_sig, generalized_fnptr_sig);
432
433 let resolved_sig = self.resolve_vars_if_possible(generalized_fnptr_sig);
434
435 if resolved_sig.visit_with(&mut MentionsTy { expected_ty }).is_continue() {
436 expected_sig = Some(ExpectedSig {
437 cause_span: inferred_sig.cause_span,
438 sig: resolved_sig.fn_sig(self.tcx),
439 });
440 }
441 } else {
442 if inferred_sig.visit_with(&mut MentionsTy { expected_ty }).is_continue() {
443 expected_sig = inferred_sig;
444 }
445 }
446 }
447
448 // Even if we can't infer the full signature, we may be able to
449 // infer the kind. This can occur when we elaborate a predicate
450 // like `F : Fn<A>`. Note that due to subtyping we could encounter
451 // many viable options, so pick the most restrictive.
452 let trait_def_id = match bound_predicate.skip_binder() {
453 ty::PredicateKind::Clause(ty::ClauseKind::Projection(data)) => {
454 Some(data.projection_term.trait_def_id(self.tcx))
455 }
456 ty::PredicateKind::Clause(ty::ClauseKind::Trait(data)) => Some(data.def_id()),
457 _ => None,
458 };
459
460 if let Some(trait_def_id) = trait_def_id {
461 let found_kind = match closure_kind {
462 hir::ClosureKind::Closure
463 // FIXME(iter_macro): Someday we'll probably want iterator closures instead of
464 // just using Fn* for iterators.
465 | hir::ClosureKind::CoroutineClosure(hir::CoroutineDesugaring::Gen) => {
466 self.tcx.fn_trait_kind_from_def_id(trait_def_id)
467 }
468 hir::ClosureKind::CoroutineClosure(hir::CoroutineDesugaring::Async) => self
469 .tcx
470 .async_fn_trait_kind_from_def_id(trait_def_id)
471 .or_else(|| self.tcx.fn_trait_kind_from_def_id(trait_def_id)),
472 _ => None,
473 };
474
475 if let Some(found_kind) = found_kind {
476 // always use the closure kind that is more permissive.
477 match (expected_kind, found_kind) {
478 (None, _) => expected_kind = Some(found_kind),
479 (Some(ClosureKind::FnMut), ClosureKind::Fn) => {
480 expected_kind = Some(ClosureKind::Fn)
481 }
482 (Some(ClosureKind::FnOnce), ClosureKind::Fn | ClosureKind::FnMut) => {
483 expected_kind = Some(found_kind)
484 }
485 _ => {}
486 }
487 }
488 }
489 }
490
491 (expected_sig, expected_kind)
492 }
493
494 /// Given a projection like "<F as Fn(X)>::Result == Y", we can deduce
495 /// everything we need to know about a closure or coroutine.
496 ///
497 /// The `cause_span` should be the span that caused us to
498 /// have this expected signature, or `None` if we can't readily
499 /// know that.
500 #[instrument(level = "debug", skip(self, cause_span), ret)]
501 fn deduce_sig_from_projection(
502 &self,
503 cause_span: Option<Span>,
504 closure_kind: hir::ClosureKind,
505 projection: ty::PolyProjectionPredicate<'tcx>,
506 ) -> Option<ExpectedSig<'tcx>> {
507 let def_id = projection.item_def_id();
508
509 // For now, we only do signature deduction based off of the `Fn` and `AsyncFn` traits,
510 // for closures and async closures, respectively.
511 match closure_kind {
512 hir::ClosureKind::Closure if self.tcx.is_lang_item(def_id, LangItem::FnOnceOutput) => {
513 self.extract_sig_from_projection(cause_span, projection)
514 }
515 hir::ClosureKind::CoroutineClosure(hir::CoroutineDesugaring::Async)
516 if self.tcx.is_lang_item(def_id, LangItem::AsyncFnOnceOutput) =>
517 {
518 self.extract_sig_from_projection(cause_span, projection)
519 }
520 // It's possible we've passed the closure to a (somewhat out-of-fashion)
521 // `F: FnOnce() -> Fut, Fut: Future<Output = T>` style bound. Let's still
522 // guide inference here, since it's beneficial for the user.
523 hir::ClosureKind::CoroutineClosure(hir::CoroutineDesugaring::Async)
524 if self.tcx.is_lang_item(def_id, LangItem::FnOnceOutput) =>
525 {
526 self.extract_sig_from_projection_and_future_bound(cause_span, projection)
527 }
528 _ => None,
529 }
530 }
531
532 /// Given an `FnOnce::Output` or `AsyncFn::Output` projection, extract the args
533 /// and return type to infer a [`ty::PolyFnSig`] for the closure.
534 fn extract_sig_from_projection(
535 &self,
536 cause_span: Option<Span>,
537 projection: ty::PolyProjectionPredicate<'tcx>,
538 ) -> Option<ExpectedSig<'tcx>> {
539 let projection = self.resolve_vars_if_possible(projection);
540
541 let arg_param_ty = projection.skip_binder().projection_term.args.type_at(1);
542 debug!(?arg_param_ty);
543
544 let ty::Tuple(input_tys) = *arg_param_ty.kind() else {
545 return None;
546 };
547
548 // Since this is a return parameter type it is safe to unwrap.
549 let ret_param_ty = projection.skip_binder().term.expect_type();
550 debug!(?ret_param_ty);
551
552 let sig = projection.rebind(self.tcx.mk_fn_sig(
553 input_tys,
554 ret_param_ty,
555 false,
556 hir::Safety::Safe,
557 ExternAbi::Rust,
558 ));
559
560 Some(ExpectedSig { cause_span, sig })
561 }
562
563 /// When an async closure is passed to a function that has a "two-part" `Fn`
564 /// and `Future` trait bound, like:
565 ///
566 /// ```rust
567 /// use std::future::Future;
568 ///
569 /// fn not_exactly_an_async_closure<F, Fut>(_f: F)
570 /// where
571 /// F: FnOnce(String, u32) -> Fut,
572 /// Fut: Future<Output = i32>,
573 /// {}
574 /// ```
575 ///
576 /// The we want to be able to extract the signature to guide inference in the async
577 /// closure. We will have two projection predicates registered in this case. First,
578 /// we identify the `FnOnce<Args, Output = ?Fut>` bound, and if the output type is
579 /// an inference variable `?Fut`, we check if that is bounded by a `Future<Output = Ty>`
580 /// projection.
581 ///
582 /// This function is actually best-effort with the return type; if we don't find a
583 /// `Future` projection, we still will return arguments that we extracted from the `FnOnce`
584 /// projection, and the output will be an unconstrained type variable instead.
585 fn extract_sig_from_projection_and_future_bound(
586 &self,
587 cause_span: Option<Span>,
588 projection: ty::PolyProjectionPredicate<'tcx>,
589 ) -> Option<ExpectedSig<'tcx>> {
590 let projection = self.resolve_vars_if_possible(projection);
591
592 let arg_param_ty = projection.skip_binder().projection_term.args.type_at(1);
593 debug!(?arg_param_ty);
594
595 let ty::Tuple(input_tys) = *arg_param_ty.kind() else {
596 return None;
597 };
598
599 // If the return type is a type variable, look for bounds on it.
600 // We could theoretically support other kinds of return types here,
601 // but none of them would be useful, since async closures return
602 // concrete anonymous future types, and their futures are not coerced
603 // into any other type within the body of the async closure.
604 let ty::Infer(ty::TyVar(return_vid)) = *projection.skip_binder().term.expect_type().kind()
605 else {
606 return None;
607 };
608
609 // FIXME: We may want to elaborate here, though I assume this will be exceedingly rare.
610 let mut return_ty = None;
611 for bound in self.obligations_for_self_ty(return_vid) {
612 if let Some(ret_projection) = bound.predicate.as_projection_clause()
613 && let Some(ret_projection) = ret_projection.no_bound_vars()
614 && self.tcx.is_lang_item(ret_projection.def_id(), LangItem::FutureOutput)
615 {
616 return_ty = Some(ret_projection.term.expect_type());
617 break;
618 }
619 }
620
621 // SUBTLE: If we didn't find a `Future<Output = ...>` bound for the return
622 // vid, we still want to attempt to provide inference guidance for the async
623 // closure's arguments. Instantiate a new vid to plug into the output type.
624 //
625 // You may be wondering, what if it's higher-ranked? Well, given that we
626 // found a type variable for the `FnOnce::Output` projection above, we know
627 // that the output can't mention any of the vars.
628 //
629 // Also note that we use a fresh var here for the signature since the signature
630 // records the output of the *future*, and `return_vid` above is the type
631 // variable of the future, not its output.
632 //
633 // FIXME: We probably should store this signature inference output in a way
634 // that does not misuse a `FnSig` type, but that can be done separately.
635 let return_ty =
636 return_ty.unwrap_or_else(|| self.next_ty_var(cause_span.unwrap_or(DUMMY_SP)));
637
638 let sig = projection.rebind(self.tcx.mk_fn_sig(
639 input_tys,
640 return_ty,
641 false,
642 hir::Safety::Safe,
643 ExternAbi::Rust,
644 ));
645
646 Some(ExpectedSig { cause_span, sig })
647 }
648
649 fn sig_of_closure(
650 &self,
651 expr_def_id: LocalDefId,
652 decl: &hir::FnDecl<'tcx>,
653 closure_kind: hir::ClosureKind,
654 expected_sig: Option<ExpectedSig<'tcx>>,
655 ) -> ClosureSignatures<'tcx> {
656 if let Some(e) = expected_sig {
657 self.sig_of_closure_with_expectation(expr_def_id, decl, closure_kind, e)
658 } else {
659 self.sig_of_closure_no_expectation(expr_def_id, decl, closure_kind)
660 }
661 }
662
663 /// If there is no expected signature, then we will convert the
664 /// types that the user gave into a signature.
665 #[instrument(skip(self, expr_def_id, decl), level = "debug")]
666 fn sig_of_closure_no_expectation(
667 &self,
668 expr_def_id: LocalDefId,
669 decl: &hir::FnDecl<'tcx>,
670 closure_kind: hir::ClosureKind,
671 ) -> ClosureSignatures<'tcx> {
672 let bound_sig = self.supplied_sig_of_closure(expr_def_id, decl, closure_kind);
673
674 self.closure_sigs(expr_def_id, bound_sig)
675 }
676
677 /// Invoked to compute the signature of a closure expression. This
678 /// combines any user-provided type annotations (e.g., `|x: u32|
679 /// -> u32 { .. }`) with the expected signature.
680 ///
681 /// The approach is as follows:
682 ///
683 /// - Let `S` be the (higher-ranked) signature that we derive from the user's annotations.
684 /// - Let `E` be the (higher-ranked) signature that we derive from the expectations, if any.
685 /// - If we have no expectation `E`, then the signature of the closure is `S`.
686 /// - Otherwise, the signature of the closure is E. Moreover:
687 /// - Skolemize the late-bound regions in `E`, yielding `E'`.
688 /// - Instantiate all the late-bound regions bound in the closure within `S`
689 /// with fresh (existential) variables, yielding `S'`
690 /// - Require that `E' = S'`
691 /// - We could use some kind of subtyping relationship here,
692 /// I imagine, but equality is easier and works fine for
693 /// our purposes.
694 ///
695 /// The key intuition here is that the user's types must be valid
696 /// from "the inside" of the closure, but the expectation
697 /// ultimately drives the overall signature.
698 ///
699 /// # Examples
700 ///
701 /// ```ignore (illustrative)
702 /// fn with_closure<F>(_: F)
703 /// where F: Fn(&u32) -> &u32 { .. }
704 ///
705 /// with_closure(|x: &u32| { ... })
706 /// ```
707 ///
708 /// Here:
709 /// - E would be `fn(&u32) -> &u32`.
710 /// - S would be `fn(&u32) -> ?T`
711 /// - E' is `&'!0 u32 -> &'!0 u32`
712 /// - S' is `&'?0 u32 -> ?T`
713 ///
714 /// S' can be unified with E' with `['?0 = '!0, ?T = &'!10 u32]`.
715 ///
716 /// # Arguments
717 ///
718 /// - `expr_def_id`: the `LocalDefId` of the closure expression
719 /// - `decl`: the HIR declaration of the closure
720 /// - `body`: the body of the closure
721 /// - `expected_sig`: the expected signature (if any). Note that
722 /// this is missing a binder: that is, there may be late-bound
723 /// regions with depth 1, which are bound then by the closure.
724 #[instrument(skip(self, expr_def_id, decl), level = "debug")]
725 fn sig_of_closure_with_expectation(
726 &self,
727 expr_def_id: LocalDefId,
728 decl: &hir::FnDecl<'tcx>,
729 closure_kind: hir::ClosureKind,
730 expected_sig: ExpectedSig<'tcx>,
731 ) -> ClosureSignatures<'tcx> {
732 // Watch out for some surprises and just ignore the
733 // expectation if things don't see to match up with what we
734 // expect.
735 if expected_sig.sig.c_variadic() != decl.c_variadic {
736 return self.sig_of_closure_no_expectation(expr_def_id, decl, closure_kind);
737 } else if expected_sig.sig.skip_binder().inputs_and_output.len() != decl.inputs.len() + 1 {
738 return self.sig_of_closure_with_mismatched_number_of_arguments(
739 expr_def_id,
740 decl,
741 expected_sig,
742 );
743 }
744
745 // Create a `PolyFnSig`. Note the oddity that late bound
746 // regions appearing free in `expected_sig` are now bound up
747 // in this binder we are creating.
748 assert!(!expected_sig.sig.skip_binder().has_vars_bound_above(ty::INNERMOST));
749 let bound_sig = expected_sig.sig.map_bound(|sig| {
750 self.tcx.mk_fn_sig(
751 sig.inputs().iter().cloned(),
752 sig.output(),
753 sig.c_variadic,
754 hir::Safety::Safe,
755 ExternAbi::RustCall,
756 )
757 });
758
759 // `deduce_expectations_from_expected_type` introduces
760 // late-bound lifetimes defined elsewhere, which we now
761 // anonymize away, so as not to confuse the user.
762 let bound_sig = self.tcx.anonymize_bound_vars(bound_sig);
763
764 let closure_sigs = self.closure_sigs(expr_def_id, bound_sig);
765
766 // Up till this point, we have ignored the annotations that the user
767 // gave. This function will check that they unify successfully.
768 // Along the way, it also writes out entries for types that the user
769 // wrote into our typeck results, which are then later used by the privacy
770 // check.
771 match self.merge_supplied_sig_with_expectation(
772 expr_def_id,
773 decl,
774 closure_kind,
775 closure_sigs,
776 ) {
777 Ok(infer_ok) => self.register_infer_ok_obligations(infer_ok),
778 Err(_) => self.sig_of_closure_no_expectation(expr_def_id, decl, closure_kind),
779 }
780 }
781
782 fn sig_of_closure_with_mismatched_number_of_arguments(
783 &self,
784 expr_def_id: LocalDefId,
785 decl: &hir::FnDecl<'tcx>,
786 expected_sig: ExpectedSig<'tcx>,
787 ) -> ClosureSignatures<'tcx> {
788 let expr_map_node = self.tcx.hir_node_by_def_id(expr_def_id);
789 let expected_args: Vec<_> = expected_sig
790 .sig
791 .skip_binder()
792 .inputs()
793 .iter()
794 .map(|ty| ArgKind::from_expected_ty(*ty, None))
795 .collect();
796 let (closure_span, closure_arg_span, found_args) =
797 match self.err_ctxt().get_fn_like_arguments(expr_map_node) {
798 Some((sp, arg_sp, args)) => (Some(sp), arg_sp, args),
799 None => (None, None, Vec::new()),
800 };
801 let expected_span =
802 expected_sig.cause_span.unwrap_or_else(|| self.tcx.def_span(expr_def_id));
803 let guar = self
804 .err_ctxt()
805 .report_arg_count_mismatch(
806 expected_span,
807 closure_span,
808 expected_args,
809 found_args,
810 true,
811 closure_arg_span,
812 )
813 .emit();
814
815 let error_sig = self.error_sig_of_closure(decl, guar);
816
817 self.closure_sigs(expr_def_id, error_sig)
818 }
819
820 /// Enforce the user's types against the expectation. See
821 /// `sig_of_closure_with_expectation` for details on the overall
822 /// strategy.
823 #[instrument(level = "debug", skip(self, expr_def_id, decl, expected_sigs))]
824 fn merge_supplied_sig_with_expectation(
825 &self,
826 expr_def_id: LocalDefId,
827 decl: &hir::FnDecl<'tcx>,
828 closure_kind: hir::ClosureKind,
829 mut expected_sigs: ClosureSignatures<'tcx>,
830 ) -> InferResult<'tcx, ClosureSignatures<'tcx>> {
831 // Get the signature S that the user gave.
832 //
833 // (See comment on `sig_of_closure_with_expectation` for the
834 // meaning of these letters.)
835 let supplied_sig = self.supplied_sig_of_closure(expr_def_id, decl, closure_kind);
836
837 debug!(?supplied_sig);
838
839 // FIXME(#45727): As discussed in [this comment][c1], naively
840 // forcing equality here actually results in suboptimal error
841 // messages in some cases. For now, if there would have been
842 // an obvious error, we fallback to declaring the type of the
843 // closure to be the one the user gave, which allows other
844 // error message code to trigger.
845 //
846 // However, I think [there is potential to do even better
847 // here][c2], since in *this* code we have the precise span of
848 // the type parameter in question in hand when we report the
849 // error.
850 //
851 // [c1]: https://github.com/rust-lang/rust/pull/45072#issuecomment-341089706
852 // [c2]: https://github.com/rust-lang/rust/pull/45072#issuecomment-341096796
853 self.commit_if_ok(|_| {
854 let mut all_obligations = PredicateObligations::new();
855 let supplied_sig = self.instantiate_binder_with_fresh_vars(
856 self.tcx.def_span(expr_def_id),
857 BoundRegionConversionTime::FnCall,
858 supplied_sig,
859 );
860
861 // The liberated version of this signature should be a subtype
862 // of the liberated form of the expectation.
863 for ((hir_ty, &supplied_ty), expected_ty) in iter::zip(
864 iter::zip(decl.inputs, supplied_sig.inputs()),
865 expected_sigs.liberated_sig.inputs(), // `liberated_sig` is E'.
866 ) {
867 // Check that E' = S'.
868 let cause = self.misc(hir_ty.span);
869 let InferOk { value: (), obligations } = self.at(&cause, self.param_env).eq(
870 DefineOpaqueTypes::Yes,
871 *expected_ty,
872 supplied_ty,
873 )?;
874 all_obligations.extend(obligations);
875 }
876
877 let supplied_output_ty = supplied_sig.output();
878 let cause = &self.misc(decl.output.span());
879 let InferOk { value: (), obligations } = self.at(cause, self.param_env).eq(
880 DefineOpaqueTypes::Yes,
881 expected_sigs.liberated_sig.output(),
882 supplied_output_ty,
883 )?;
884 all_obligations.extend(obligations);
885
886 let inputs =
887 supplied_sig.inputs().into_iter().map(|&ty| self.resolve_vars_if_possible(ty));
888
889 expected_sigs.liberated_sig = self.tcx.mk_fn_sig(
890 inputs,
891 supplied_output_ty,
892 expected_sigs.liberated_sig.c_variadic,
893 hir::Safety::Safe,
894 ExternAbi::RustCall,
895 );
896
897 Ok(InferOk { value: expected_sigs, obligations: all_obligations })
898 })
899 }
900
901 /// If there is no expected signature, then we will convert the
902 /// types that the user gave into a signature.
903 ///
904 /// Also, record this closure signature for later.
905 #[instrument(skip(self, decl), level = "debug", ret)]
906 fn supplied_sig_of_closure(
907 &self,
908 expr_def_id: LocalDefId,
909 decl: &hir::FnDecl<'tcx>,
910 closure_kind: hir::ClosureKind,
911 ) -> ty::PolyFnSig<'tcx> {
912 let lowerer = self.lowerer();
913
914 trace!("decl = {:#?}", decl);
915 debug!(?closure_kind);
916
917 let hir_id = self.tcx.local_def_id_to_hir_id(expr_def_id);
918 let bound_vars = self.tcx.late_bound_vars(hir_id);
919
920 // First, convert the types that the user supplied (if any).
921 let supplied_arguments = decl.inputs.iter().map(|a| lowerer.lower_ty(a));
922 let supplied_return = match decl.output {
923 hir::FnRetTy::Return(ref output) => lowerer.lower_ty(output),
924 hir::FnRetTy::DefaultReturn(_) => match closure_kind {
925 // In the case of the async block that we create for a function body,
926 // we expect the return type of the block to match that of the enclosing
927 // function.
928 hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
929 hir::CoroutineDesugaring::Async,
930 hir::CoroutineSource::Fn,
931 )) => {
932 debug!("closure is async fn body");
933 self.deduce_future_output_from_obligations(expr_def_id).unwrap_or_else(|| {
934 // AFAIK, deducing the future output
935 // always succeeds *except* in error cases
936 // like #65159. I'd like to return Error
937 // here, but I can't because I can't
938 // easily (and locally) prove that we
939 // *have* reported an
940 // error. --nikomatsakis
941 lowerer.ty_infer(None, decl.output.span())
942 })
943 }
944 // All `gen {}` and `async gen {}` must return unit.
945 hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
946 hir::CoroutineDesugaring::Gen | hir::CoroutineDesugaring::AsyncGen,
947 _,
948 )) => self.tcx.types.unit,
949
950 // For async blocks, we just fall back to `_` here.
951 // For closures/coroutines, we know nothing about the return
952 // type unless it was supplied.
953 hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
954 hir::CoroutineDesugaring::Async,
955 _,
956 ))
957 | hir::ClosureKind::Coroutine(hir::CoroutineKind::Coroutine(_))
958 | hir::ClosureKind::Closure
959 | hir::ClosureKind::CoroutineClosure(_) => {
960 lowerer.ty_infer(None, decl.output.span())
961 }
962 },
963 };
964
965 let result = ty::Binder::bind_with_vars(
966 self.tcx.mk_fn_sig(
967 supplied_arguments,
968 supplied_return,
969 decl.c_variadic,
970 hir::Safety::Safe,
971 ExternAbi::RustCall,
972 ),
973 bound_vars,
974 );
975
976 let c_result = self.infcx.canonicalize_response(result);
977 self.typeck_results.borrow_mut().user_provided_sigs.insert(expr_def_id, c_result);
978
979 // Normalize only after registering in `user_provided_sigs`.
980 self.normalize(self.tcx.def_span(expr_def_id), result)
981 }
982
983 /// Invoked when we are translating the coroutine that results
984 /// from desugaring an `async fn`. Returns the "sugared" return
985 /// type of the `async fn` -- that is, the return type that the
986 /// user specified. The "desugared" return type is an `impl
987 /// Future<Output = T>`, so we do this by searching through the
988 /// obligations to extract the `T`.
989 #[instrument(skip(self), level = "debug", ret)]
990 fn deduce_future_output_from_obligations(&self, body_def_id: LocalDefId) -> Option<Ty<'tcx>> {
991 let ret_coercion = self.ret_coercion.as_ref().unwrap_or_else(|| {
992 span_bug!(self.tcx.def_span(body_def_id), "async fn coroutine outside of a fn")
993 });
994
995 let closure_span = self.tcx.def_span(body_def_id);
996 let ret_ty = ret_coercion.borrow().expected_ty();
997 let ret_ty = self.try_structurally_resolve_type(closure_span, ret_ty);
998
999 let get_future_output = |predicate: ty::Predicate<'tcx>, span| {
1000 // Search for a pending obligation like
1001 //
1002 // `<R as Future>::Output = T`
1003 //
1004 // where R is the return type we are expecting. This type `T`
1005 // will be our output.
1006 let bound_predicate = predicate.kind();
1007 if let ty::PredicateKind::Clause(ty::ClauseKind::Projection(proj_predicate)) =
1008 bound_predicate.skip_binder()
1009 {
1010 self.deduce_future_output_from_projection(
1011 span,
1012 bound_predicate.rebind(proj_predicate),
1013 )
1014 } else {
1015 None
1016 }
1017 };
1018
1019 let output_ty = match *ret_ty.kind() {
1020 ty::Infer(ty::TyVar(ret_vid)) => {
1021 self.obligations_for_self_ty(ret_vid).into_iter().find_map(|obligation| {
1022 get_future_output(obligation.predicate, obligation.cause.span)
1023 })?
1024 }
1025 ty::Alias(ty::Projection, _) => {
1026 return Some(Ty::new_error_with_message(
1027 self.tcx,
1028 closure_span,
1029 "this projection should have been projected to an opaque type",
1030 ));
1031 }
1032 ty::Alias(ty::Opaque, ty::AliasTy { def_id, args, .. }) => self
1033 .tcx
1034 .explicit_item_self_bounds(def_id)
1035 .iter_instantiated_copied(self.tcx, args)
1036 .find_map(|(p, s)| get_future_output(p.as_predicate(), s))?,
1037 ty::Error(_) => return Some(ret_ty),
1038 _ => {
1039 span_bug!(closure_span, "invalid async fn coroutine return type: {ret_ty:?}")
1040 }
1041 };
1042
1043 let output_ty = self.normalize(closure_span, output_ty);
1044
1045 // async fn that have opaque types in their return type need to redo the conversion to inference variables
1046 // as they fetch the still opaque version from the signature.
1047 let InferOk { value: output_ty, obligations } = self
1048 .replace_opaque_types_with_inference_vars(
1049 output_ty,
1050 body_def_id,
1051 closure_span,
1052 self.param_env,
1053 );
1054 self.register_predicates(obligations);
1055
1056 Some(output_ty)
1057 }
1058
1059 /// Given a projection like
1060 ///
1061 /// `<X as Future>::Output = T`
1062 ///
1063 /// where `X` is some type that has no late-bound regions, returns
1064 /// `Some(T)`. If the projection is for some other trait, returns
1065 /// `None`.
1066 fn deduce_future_output_from_projection(
1067 &self,
1068 cause_span: Span,
1069 predicate: ty::PolyProjectionPredicate<'tcx>,
1070 ) -> Option<Ty<'tcx>> {
1071 debug!("deduce_future_output_from_projection(predicate={:?})", predicate);
1072
1073 // We do not expect any bound regions in our predicate, so
1074 // skip past the bound vars.
1075 let Some(predicate) = predicate.no_bound_vars() else {
1076 debug!("deduce_future_output_from_projection: has late-bound regions");
1077 return None;
1078 };
1079
1080 // Check that this is a projection from the `Future` trait.
1081 let trait_def_id = predicate.projection_term.trait_def_id(self.tcx);
1082 if !self.tcx.is_lang_item(trait_def_id, LangItem::Future) {
1083 debug!("deduce_future_output_from_projection: not a future");
1084 return None;
1085 }
1086
1087 // The `Future` trait has only one associated item, `Output`,
1088 // so check that this is what we see.
1089 let output_assoc_item = self.tcx.associated_item_def_ids(trait_def_id)[0];
1090 if output_assoc_item != predicate.projection_term.def_id {
1091 span_bug!(
1092 cause_span,
1093 "projecting associated item `{:?}` from future, which is not Output `{:?}`",
1094 predicate.projection_term.def_id,
1095 output_assoc_item,
1096 );
1097 }
1098
1099 // Extract the type from the projection. Note that there can
1100 // be no bound variables in this type because the "self type"
1101 // does not have any regions in it.
1102 let output_ty = self.resolve_vars_if_possible(predicate.term);
1103 debug!("deduce_future_output_from_projection: output_ty={:?}", output_ty);
1104 // This is a projection on a Fn trait so will always be a type.
1105 Some(output_ty.expect_type())
1106 }
1107
1108 /// Converts the types that the user supplied, in case that doing
1109 /// so should yield an error, but returns back a signature where
1110 /// all parameters are of type `ty::Error`.
1111 fn error_sig_of_closure(
1112 &self,
1113 decl: &hir::FnDecl<'tcx>,
1114 guar: ErrorGuaranteed,
1115 ) -> ty::PolyFnSig<'tcx> {
1116 let lowerer = self.lowerer();
1117 let err_ty = Ty::new_error(self.tcx, guar);
1118
1119 let supplied_arguments = decl.inputs.iter().map(|a| {
1120 // Convert the types that the user supplied (if any), but ignore them.
1121 lowerer.lower_ty(a);
1122 err_ty
1123 });
1124
1125 if let hir::FnRetTy::Return(ref output) = decl.output {
1126 lowerer.lower_ty(output);
1127 }
1128
1129 let result = ty::Binder::dummy(self.tcx.mk_fn_sig(
1130 supplied_arguments,
1131 err_ty,
1132 decl.c_variadic,
1133 hir::Safety::Safe,
1134 ExternAbi::RustCall,
1135 ));
1136
1137 debug!("supplied_sig_of_closure: result={:?}", result);
1138
1139 result
1140 }
1141
1142 #[instrument(level = "debug", skip(self), ret)]
1143 fn closure_sigs(
1144 &self,
1145 expr_def_id: LocalDefId,
1146 bound_sig: ty::PolyFnSig<'tcx>,
1147 ) -> ClosureSignatures<'tcx> {
1148 let liberated_sig =
1149 self.tcx().liberate_late_bound_regions(expr_def_id.to_def_id(), bound_sig);
1150 let liberated_sig = self.normalize(self.tcx.def_span(expr_def_id), liberated_sig);
1151 ClosureSignatures { bound_sig, liberated_sig }
1152 }
1153}