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