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