1 //! Code for type-checking closure expressions.
3 use super::{check_fn, Expectation, FnCtxt, GeneratorTypes};
5 use crate::astconv::AstConv;
7 use rustc_hir::def_id::DefId;
8 use rustc_hir::lang_items::LangItem;
9 use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
10 use rustc_infer::infer::LateBoundRegionConversionTime;
11 use rustc_infer::infer::{InferOk, InferResult};
12 use rustc_middle::ty::fold::TypeFoldable;
13 use rustc_middle::ty::subst::InternalSubsts;
14 use rustc_middle::ty::{self, Ty};
15 use rustc_span::source_map::Span;
16 use rustc_target::spec::abi::Abi;
17 use rustc_trait_selection::traits::error_reporting::ArgKind;
18 use rustc_trait_selection::traits::error_reporting::InferCtxtExt as _;
22 /// What signature do we *expect* the closure to have from context?
24 struct ExpectedSig<'tcx> {
25 /// Span that gave us this expectation, if we know that.
26 cause_span: Option<Span>,
27 sig: ty::PolyFnSig<'tcx>,
30 struct ClosureSignatures<'tcx> {
31 bound_sig: ty::PolyFnSig<'tcx>,
32 liberated_sig: ty::FnSig<'tcx>,
35 impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
36 #[instrument(skip(self, expr, _capture, decl, body_id), level = "debug")]
37 pub fn check_expr_closure(
40 _capture: hir::CaptureBy,
41 decl: &'tcx hir::FnDecl<'tcx>,
43 gen: Option<hir::Movability>,
44 expected: Expectation<'tcx>,
46 trace!("decl = {:#?}", decl);
47 trace!("expr = {:#?}", expr);
49 // It's always helpful for inference if we know the kind of
50 // closure sooner rather than later, so first examine the expected
51 // type, and see if can glean a closure kind from there.
52 let (expected_sig, expected_kind) = match expected.to_option(self) {
53 Some(ty) => self.deduce_expectations_from_expected_type(ty),
56 let body = self.tcx.hir().body(body_id);
57 self.check_closure(expr, expected_kind, decl, body, gen, expected_sig)
60 #[instrument(skip(self, expr, body, decl), level = "debug")]
64 opt_kind: Option<ty::ClosureKind>,
65 decl: &'tcx hir::FnDecl<'tcx>,
66 body: &'tcx hir::Body<'tcx>,
67 gen: Option<hir::Movability>,
68 expected_sig: Option<ExpectedSig<'tcx>>,
70 trace!("decl = {:#?}", decl);
71 let expr_def_id = self.tcx.hir().local_def_id(expr.hir_id);
74 let ClosureSignatures { bound_sig, liberated_sig } =
75 self.sig_of_closure(expr.hir_id, expr_def_id.to_def_id(), decl, body, expected_sig);
77 debug!(?bound_sig, ?liberated_sig);
79 let return_type_pre_known = !liberated_sig.output().is_ty_infer();
81 let generator_types = check_fn(
89 return_type_pre_known,
93 let parent_substs = InternalSubsts::identity_for_item(
95 self.tcx.typeck_root_def_id(expr_def_id.to_def_id()),
98 let tupled_upvars_ty = self.infcx.next_ty_var(TypeVariableOrigin {
99 kind: TypeVariableOriginKind::ClosureSynthetic,
100 span: self.tcx.hir().span(expr.hir_id),
103 if let Some(GeneratorTypes { resume_ty, yield_ty, interior, movability }) = generator_types
105 let generator_substs = ty::GeneratorSubsts::new(
107 ty::GeneratorSubstsParts {
111 return_ty: liberated_sig.output(),
117 return self.tcx.mk_generator(
118 expr_def_id.to_def_id(),
119 generator_substs.substs,
124 // Tuple up the arguments and insert the resulting function type into
125 // the `closures` table.
126 let sig = bound_sig.map_bound(|sig| {
128 iter::once(self.tcx.intern_tup(sig.inputs())),
136 debug!(?sig, ?opt_kind);
138 let closure_kind_ty = match opt_kind {
139 Some(kind) => kind.to_ty(self.tcx),
141 // Create a type variable (for now) to represent the closure kind.
142 // It will be unified during the upvar inference phase (`upvar.rs`)
143 None => self.infcx.next_ty_var(TypeVariableOrigin {
144 // FIXME(eddyb) distinguish closure kind inference variables from the rest.
145 kind: TypeVariableOriginKind::ClosureSynthetic,
150 let closure_substs = ty::ClosureSubsts::new(
152 ty::ClosureSubstsParts {
155 closure_sig_as_fn_ptr_ty: self.tcx.mk_fn_ptr(sig),
160 let closure_type = self.tcx.mk_closure(expr_def_id.to_def_id(), closure_substs.substs);
162 debug!(?expr.hir_id, ?closure_type);
167 /// Given the expected type, figures out what it can about this closure we
168 /// are about to type check:
169 #[instrument(skip(self), level = "debug")]
170 fn deduce_expectations_from_expected_type(
172 expected_ty: Ty<'tcx>,
173 ) -> (Option<ExpectedSig<'tcx>>, Option<ty::ClosureKind>) {
174 match *expected_ty.kind() {
175 ty::Dynamic(ref object_type, ..) => {
176 let sig = object_type.projection_bounds().find_map(|pb| {
177 let pb = pb.with_self_ty(self.tcx, self.tcx.types.trait_object_dummy_self);
178 self.deduce_sig_from_projection(None, pb)
180 let kind = object_type
182 .and_then(|did| self.tcx.fn_trait_kind_from_lang_item(did));
185 ty::Infer(ty::TyVar(vid)) => self.deduce_expectations_from_obligations(vid),
187 let expected_sig = ExpectedSig { cause_span: None, sig };
188 (Some(expected_sig), Some(ty::ClosureKind::Fn))
194 fn deduce_expectations_from_obligations(
196 expected_vid: ty::TyVid,
197 ) -> (Option<ExpectedSig<'tcx>>, Option<ty::ClosureKind>) {
199 self.obligations_for_self_ty(expected_vid).find_map(|(_, obligation)| {
201 "deduce_expectations_from_obligations: obligation.predicate={:?}",
205 let bound_predicate = obligation.predicate.kind();
206 if let ty::PredicateKind::Projection(proj_predicate) =
207 obligation.predicate.kind().skip_binder()
209 // Given a Projection predicate, we can potentially infer
210 // the complete signature.
211 self.deduce_sig_from_projection(
212 Some(obligation.cause.span),
213 bound_predicate.rebind(proj_predicate),
220 // Even if we can't infer the full signature, we may be able to
221 // infer the kind. This can occur when we elaborate a predicate
222 // like `F : Fn<A>`. Note that due to subtyping we could encounter
223 // many viable options, so pick the most restrictive.
224 let expected_kind = self
225 .obligations_for_self_ty(expected_vid)
226 .filter_map(|(tr, _)| self.tcx.fn_trait_kind_from_lang_item(tr.def_id()))
227 .fold(None, |best, cur| Some(best.map_or(cur, |best| cmp::min(best, cur))));
229 (expected_sig, expected_kind)
232 /// Given a projection like "<F as Fn(X)>::Result == Y", we can deduce
233 /// everything we need to know about a closure or generator.
235 /// The `cause_span` should be the span that caused us to
236 /// have this expected signature, or `None` if we can't readily
238 fn deduce_sig_from_projection(
240 cause_span: Option<Span>,
241 projection: ty::PolyProjectionPredicate<'tcx>,
242 ) -> Option<ExpectedSig<'tcx>> {
245 debug!("deduce_sig_from_projection({:?})", projection);
247 let trait_def_id = projection.trait_def_id(tcx);
249 let is_fn = tcx.fn_trait_kind_from_lang_item(trait_def_id).is_some();
250 let gen_trait = tcx.require_lang_item(LangItem::Generator, cause_span);
251 let is_gen = gen_trait == trait_def_id;
252 if !is_fn && !is_gen {
253 debug!("deduce_sig_from_projection: not fn or generator");
258 // Check that we deduce the signature from the `<_ as std::ops::Generator>::Return`
259 // associated item and not yield.
260 let return_assoc_item = self.tcx.associated_item_def_ids(gen_trait)[1];
261 if return_assoc_item != projection.projection_def_id() {
262 debug!("deduce_sig_from_projection: not return assoc item of generator");
267 let input_tys = if is_fn {
268 let arg_param_ty = projection.skip_binder().projection_ty.substs.type_at(1);
269 let arg_param_ty = self.resolve_vars_if_possible(arg_param_ty);
270 debug!("deduce_sig_from_projection: arg_param_ty={:?}", arg_param_ty);
272 match arg_param_ty.kind() {
273 ty::Tuple(tys) => tys.into_iter().map(|k| k.expect_ty()).collect::<Vec<_>>(),
277 // Generators with a `()` resume type may be defined with 0 or 1 explicit arguments,
278 // else they must have exactly 1 argument. For now though, just give up in this case.
282 let ret_param_ty = projection.skip_binder().ty;
283 let ret_param_ty = self.resolve_vars_if_possible(ret_param_ty);
284 debug!("deduce_sig_from_projection: ret_param_ty={:?}", ret_param_ty);
286 let sig = projection.rebind(self.tcx.mk_fn_sig(
290 hir::Unsafety::Normal,
293 debug!("deduce_sig_from_projection: sig={:?}", sig);
295 Some(ExpectedSig { cause_span, sig })
302 decl: &hir::FnDecl<'_>,
303 body: &hir::Body<'_>,
304 expected_sig: Option<ExpectedSig<'tcx>>,
305 ) -> ClosureSignatures<'tcx> {
306 if let Some(e) = expected_sig {
307 self.sig_of_closure_with_expectation(hir_id, expr_def_id, decl, body, e)
309 self.sig_of_closure_no_expectation(hir_id, expr_def_id, decl, body)
313 /// If there is no expected signature, then we will convert the
314 /// types that the user gave into a signature.
315 #[instrument(skip(self, hir_id, expr_def_id, decl, body), level = "debug")]
316 fn sig_of_closure_no_expectation(
320 decl: &hir::FnDecl<'_>,
321 body: &hir::Body<'_>,
322 ) -> ClosureSignatures<'tcx> {
323 let bound_sig = self.supplied_sig_of_closure(hir_id, expr_def_id, decl, body);
325 self.closure_sigs(expr_def_id, body, bound_sig)
328 /// Invoked to compute the signature of a closure expression. This
329 /// combines any user-provided type annotations (e.g., `|x: u32|
330 /// -> u32 { .. }`) with the expected signature.
332 /// The approach is as follows:
334 /// - Let `S` be the (higher-ranked) signature that we derive from the user's annotations.
335 /// - Let `E` be the (higher-ranked) signature that we derive from the expectations, if any.
336 /// - If we have no expectation `E`, then the signature of the closure is `S`.
337 /// - Otherwise, the signature of the closure is E. Moreover:
338 /// - Skolemize the late-bound regions in `E`, yielding `E'`.
339 /// - Instantiate all the late-bound regions bound in the closure within `S`
340 /// with fresh (existential) variables, yielding `S'`
341 /// - Require that `E' = S'`
342 /// - We could use some kind of subtyping relationship here,
343 /// I imagine, but equality is easier and works fine for
346 /// The key intuition here is that the user's types must be valid
347 /// from "the inside" of the closure, but the expectation
348 /// ultimately drives the overall signature.
353 /// fn with_closure<F>(_: F)
354 /// where F: Fn(&u32) -> &u32 { .. }
356 /// with_closure(|x: &u32| { ... })
360 /// - E would be `fn(&u32) -> &u32`.
361 /// - S would be `fn(&u32) ->
362 /// - E' is `&'!0 u32 -> &'!0 u32`
363 /// - S' is `&'?0 u32 -> ?T`
365 /// S' can be unified with E' with `['?0 = '!0, ?T = &'!10 u32]`.
369 /// - `expr_def_id`: the `DefId` of the closure expression
370 /// - `decl`: the HIR declaration of the closure
371 /// - `body`: the body of the closure
372 /// - `expected_sig`: the expected signature (if any). Note that
373 /// this is missing a binder: that is, there may be late-bound
374 /// regions with depth 1, which are bound then by the closure.
375 #[instrument(skip(self, hir_id, expr_def_id, decl, body), level = "debug")]
376 fn sig_of_closure_with_expectation(
380 decl: &hir::FnDecl<'_>,
381 body: &hir::Body<'_>,
382 expected_sig: ExpectedSig<'tcx>,
383 ) -> ClosureSignatures<'tcx> {
384 // Watch out for some surprises and just ignore the
385 // expectation if things don't see to match up with what we
387 if expected_sig.sig.c_variadic() != decl.c_variadic {
388 return self.sig_of_closure_no_expectation(hir_id, expr_def_id, decl, body);
389 } else if expected_sig.sig.skip_binder().inputs_and_output.len() != decl.inputs.len() + 1 {
390 return self.sig_of_closure_with_mismatched_number_of_arguments(
398 // Create a `PolyFnSig`. Note the oddity that late bound
399 // regions appearing free in `expected_sig` are now bound up
400 // in this binder we are creating.
401 assert!(!expected_sig.sig.skip_binder().has_vars_bound_above(ty::INNERMOST));
402 let bound_sig = expected_sig.sig.map_bound(|sig| {
404 sig.inputs().iter().cloned(),
407 hir::Unsafety::Normal,
412 // `deduce_expectations_from_expected_type` introduces
413 // late-bound lifetimes defined elsewhere, which we now
414 // anonymize away, so as not to confuse the user.
415 let bound_sig = self.tcx.anonymize_late_bound_regions(bound_sig);
417 let closure_sigs = self.closure_sigs(expr_def_id, body, bound_sig);
419 // Up till this point, we have ignored the annotations that the user
420 // gave. This function will check that they unify successfully.
421 // Along the way, it also writes out entries for types that the user
422 // wrote into our typeck results, which are then later used by the privacy
424 match self.check_supplied_sig_against_expectation(
431 Ok(infer_ok) => self.register_infer_ok_obligations(infer_ok),
432 Err(_) => return self.sig_of_closure_no_expectation(hir_id, expr_def_id, decl, body),
438 fn sig_of_closure_with_mismatched_number_of_arguments(
441 decl: &hir::FnDecl<'_>,
442 body: &hir::Body<'_>,
443 expected_sig: ExpectedSig<'tcx>,
444 ) -> ClosureSignatures<'tcx> {
445 let hir = self.tcx.hir();
446 let expr_map_node = hir.get_if_local(expr_def_id).unwrap();
447 let expected_args: Vec<_> = expected_sig
452 .map(|ty| ArgKind::from_expected_ty(ty, None))
454 let (closure_span, found_args) = match self.get_fn_like_arguments(expr_map_node) {
455 Some((sp, args)) => (Some(sp), args),
456 None => (None, Vec::new()),
459 expected_sig.cause_span.unwrap_or_else(|| hir.span_if_local(expr_def_id).unwrap());
460 self.report_arg_count_mismatch(
469 let error_sig = self.error_sig_of_closure(decl);
471 self.closure_sigs(expr_def_id, body, error_sig)
474 /// Enforce the user's types against the expectation. See
475 /// `sig_of_closure_with_expectation` for details on the overall
477 fn check_supplied_sig_against_expectation(
481 decl: &hir::FnDecl<'_>,
482 body: &hir::Body<'_>,
483 expected_sigs: &ClosureSignatures<'tcx>,
484 ) -> InferResult<'tcx, ()> {
485 // Get the signature S that the user gave.
487 // (See comment on `sig_of_closure_with_expectation` for the
488 // meaning of these letters.)
489 let supplied_sig = self.supplied_sig_of_closure(hir_id, expr_def_id, decl, body);
491 debug!("check_supplied_sig_against_expectation: supplied_sig={:?}", supplied_sig);
493 // FIXME(#45727): As discussed in [this comment][c1], naively
494 // forcing equality here actually results in suboptimal error
495 // messages in some cases. For now, if there would have been
496 // an obvious error, we fallback to declaring the type of the
497 // closure to be the one the user gave, which allows other
498 // error message code to trigger.
500 // However, I think [there is potential to do even better
501 // here][c2], since in *this* code we have the precise span of
502 // the type parameter in question in hand when we report the
505 // [c1]: https://github.com/rust-lang/rust/pull/45072#issuecomment-341089706
506 // [c2]: https://github.com/rust-lang/rust/pull/45072#issuecomment-341096796
507 self.infcx.commit_if_ok(|_| {
508 let mut all_obligations = vec![];
510 // The liberated version of this signature should be a subtype
511 // of the liberated form of the expectation.
512 for ((hir_ty, &supplied_ty), expected_ty) in iter::zip(
515 supplied_sig.inputs().skip_binder(), // binder moved to (*) below
517 expected_sigs.liberated_sig.inputs(), // `liberated_sig` is E'.
519 // Instantiate (this part of..) S to S', i.e., with fresh variables.
520 let (supplied_ty, _) = self.infcx.replace_bound_vars_with_fresh_vars(
522 LateBoundRegionConversionTime::FnCall,
523 supplied_sig.inputs().rebind(supplied_ty),
524 ); // recreated from (*) above
526 // Check that E' = S'.
527 let cause = self.misc(hir_ty.span);
528 let InferOk { value: (), obligations } =
529 self.at(&cause, self.param_env).eq(*expected_ty, supplied_ty)?;
530 all_obligations.extend(obligations);
533 let (supplied_output_ty, _) = self.infcx.replace_bound_vars_with_fresh_vars(
535 LateBoundRegionConversionTime::FnCall,
536 supplied_sig.output(),
538 let cause = &self.misc(decl.output.span());
539 let InferOk { value: (), obligations } = self
540 .at(cause, self.param_env)
541 .eq(expected_sigs.liberated_sig.output(), supplied_output_ty)?;
542 all_obligations.extend(obligations);
544 Ok(InferOk { value: (), obligations: all_obligations })
548 /// If there is no expected signature, then we will convert the
549 /// types that the user gave into a signature.
551 /// Also, record this closure signature for later.
552 #[instrument(skip(self, decl, body), level = "debug")]
553 fn supplied_sig_of_closure(
557 decl: &hir::FnDecl<'_>,
558 body: &hir::Body<'_>,
559 ) -> ty::PolyFnSig<'tcx> {
560 let astconv: &dyn AstConv<'_> = self;
562 trace!("decl = {:#?}", decl);
563 debug!(?body.generator_kind);
565 let bound_vars = self.tcx.late_bound_vars(hir_id);
567 // First, convert the types that the user supplied (if any).
568 let supplied_arguments = decl.inputs.iter().map(|a| astconv.ast_ty_to_ty(a));
569 let supplied_return = match decl.output {
570 hir::FnRetTy::Return(ref output) => astconv.ast_ty_to_ty(&output),
571 hir::FnRetTy::DefaultReturn(_) => match body.generator_kind {
572 // In the case of the async block that we create for a function body,
573 // we expect the return type of the block to match that of the enclosing
575 Some(hir::GeneratorKind::Async(hir::AsyncGeneratorKind::Fn)) => {
576 debug!("closure is async fn body");
577 self.deduce_future_output_from_obligations(expr_def_id).unwrap_or_else(|| {
578 // AFAIK, deducing the future output
579 // always succeeds *except* in error cases
580 // like #65159. I'd like to return Error
581 // here, but I can't because I can't
582 // easily (and locally) prove that we
583 // *have* reported an
584 // error. --nikomatsakis
585 astconv.ty_infer(None, decl.output.span())
589 _ => astconv.ty_infer(None, decl.output.span()),
593 let result = ty::Binder::bind_with_vars(
598 hir::Unsafety::Normal,
606 let c_result = self.inh.infcx.canonicalize_response(result);
607 self.typeck_results.borrow_mut().user_provided_sigs.insert(expr_def_id, c_result);
612 /// Invoked when we are translating the generator that results
613 /// from desugaring an `async fn`. Returns the "sugared" return
614 /// type of the `async fn` -- that is, the return type that the
615 /// user specified. The "desugared" return type is an `impl
616 /// Future<Output = T>`, so we do this by searching through the
617 /// obligations to extract the `T`.
618 fn deduce_future_output_from_obligations(&self, expr_def_id: DefId) -> Option<Ty<'tcx>> {
619 debug!("deduce_future_output_from_obligations(expr_def_id={:?})", expr_def_id);
621 let ret_coercion = self.ret_coercion.as_ref().unwrap_or_else(|| {
622 span_bug!(self.tcx.def_span(expr_def_id), "async fn generator outside of a fn")
625 // In practice, the return type of the surrounding function is
626 // always a (not yet resolved) inference variable, because it
627 // is the hidden type for an `impl Trait` that we are going to
629 let ret_ty = ret_coercion.borrow().expected_ty();
630 let ret_ty = self.inh.infcx.shallow_resolve(ret_ty);
631 let ret_vid = match *ret_ty.kind() {
632 ty::Infer(ty::TyVar(ret_vid)) => ret_vid,
633 ty::Error(_) => return None,
635 self.tcx.def_span(expr_def_id),
636 "async fn generator return type not an inference variable"
640 // Search for a pending obligation like
642 // `<R as Future>::Output = T`
644 // where R is the return type we are expecting. This type `T`
645 // will be our output.
646 let output_ty = self.obligations_for_self_ty(ret_vid).find_map(|(_, obligation)| {
647 let bound_predicate = obligation.predicate.kind();
648 if let ty::PredicateKind::Projection(proj_predicate) = bound_predicate.skip_binder() {
649 self.deduce_future_output_from_projection(
650 obligation.cause.span,
651 bound_predicate.rebind(proj_predicate),
658 debug!("deduce_future_output_from_obligations: output_ty={:?}", output_ty);
662 /// Given a projection like
664 /// `<X as Future>::Output = T`
666 /// where `X` is some type that has no late-bound regions, returns
667 /// `Some(T)`. If the projection is for some other trait, returns
669 fn deduce_future_output_from_projection(
672 predicate: ty::PolyProjectionPredicate<'tcx>,
673 ) -> Option<Ty<'tcx>> {
674 debug!("deduce_future_output_from_projection(predicate={:?})", predicate);
676 // We do not expect any bound regions in our predicate, so
677 // skip past the bound vars.
678 let predicate = match predicate.no_bound_vars() {
681 debug!("deduce_future_output_from_projection: has late-bound regions");
686 // Check that this is a projection from the `Future` trait.
687 let trait_def_id = predicate.projection_ty.trait_def_id(self.tcx);
688 let future_trait = self.tcx.require_lang_item(LangItem::Future, Some(cause_span));
689 if trait_def_id != future_trait {
690 debug!("deduce_future_output_from_projection: not a future");
694 // The `Future` trait has only one associted item, `Output`,
695 // so check that this is what we see.
696 let output_assoc_item = self.tcx.associated_item_def_ids(future_trait)[0];
697 if output_assoc_item != predicate.projection_ty.item_def_id {
700 "projecting associated item `{:?}` from future, which is not Output `{:?}`",
701 predicate.projection_ty.item_def_id,
706 // Extract the type from the projection. Note that there can
707 // be no bound variables in this type because the "self type"
708 // does not have any regions in it.
709 let output_ty = self.resolve_vars_if_possible(predicate.ty);
710 debug!("deduce_future_output_from_projection: output_ty={:?}", output_ty);
714 /// Converts the types that the user supplied, in case that doing
715 /// so should yield an error, but returns back a signature where
716 /// all parameters are of type `TyErr`.
717 fn error_sig_of_closure(&self, decl: &hir::FnDecl<'_>) -> ty::PolyFnSig<'tcx> {
718 let astconv: &dyn AstConv<'_> = self;
720 let supplied_arguments = decl.inputs.iter().map(|a| {
721 // Convert the types that the user supplied (if any), but ignore them.
722 astconv.ast_ty_to_ty(a);
726 if let hir::FnRetTy::Return(ref output) = decl.output {
727 astconv.ast_ty_to_ty(&output);
730 let result = ty::Binder::dummy(self.tcx.mk_fn_sig(
734 hir::Unsafety::Normal,
738 debug!("supplied_sig_of_closure: result={:?}", result);
746 body: &hir::Body<'_>,
747 bound_sig: ty::PolyFnSig<'tcx>,
748 ) -> ClosureSignatures<'tcx> {
749 let liberated_sig = self.tcx().liberate_late_bound_regions(expr_def_id, bound_sig);
750 let liberated_sig = self.inh.normalize_associated_types_in(
756 ClosureSignatures { bound_sig, liberated_sig }