1 //! Code for type-checking closure expressions.
3 use super::{check_fn, Expectation, FnCtxt, GeneratorTypes};
5 use crate::astconv::AstConv;
6 use crate::middle::{lang_items, region};
7 use rustc::hir::def_id::DefId;
8 use rustc::infer::{InferOk, InferResult};
9 use rustc::infer::LateBoundRegionConversionTime;
10 use rustc::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
11 use rustc::traits::Obligation;
12 use rustc::traits::error_reporting::ArgKind;
13 use rustc::ty::{self, Ty, GenericParamDefKind};
14 use rustc::ty::fold::TypeFoldable;
15 use rustc::ty::subst::InternalSubsts;
18 use rustc_target::spec::abi::Abi;
19 use syntax::source_map::Span;
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>,
30 struct ClosureSignatures<'tcx> {
31 bound_sig: ty::PolyFnSig<'tcx>,
32 liberated_sig: ty::FnSig<'tcx>,
35 impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
36 pub fn check_expr_closure(
39 _capture: hir::CaptureClause,
40 decl: &'tcx hir::FnDecl,
42 gen: Option<hir::GeneratorMovability>,
43 expected: Expectation<'tcx>,
46 "check_expr_closure(expr={:?},expected={:?})",
50 // It's always helpful for inference if we know the kind of
51 // closure sooner rather than later, so first examine the expected
52 // type, and see if can glean a closure kind from there.
53 let (expected_sig, expected_kind) = match expected.to_option(self) {
54 Some(ty) => self.deduce_expectations_from_expected_type(ty),
57 let body = self.tcx.hir().body(body_id);
58 self.check_closure(expr, expected_kind, decl, body, gen, expected_sig)
64 opt_kind: Option<ty::ClosureKind>,
65 decl: &'tcx hir::FnDecl,
66 body: &'tcx hir::Body,
67 gen: Option<hir::GeneratorMovability>,
68 expected_sig: Option<ExpectedSig<'tcx>>,
71 "check_closure(opt_kind={:?}, expected_sig={:?})",
72 opt_kind, expected_sig
75 let expr_def_id = self.tcx.hir().local_def_id(expr.hir_id);
77 let ClosureSignatures {
80 } = self.sig_of_closure(expr_def_id, decl, body, expected_sig);
82 debug!("check_closure: ty_of_closure returns {:?}", liberated_sig);
84 let generator_types = check_fn(
94 // Create type variables (for now) to represent the transformed
95 // types of upvars. These will be unified during the upvar
96 // inference phase (`upvar.rs`).
98 InternalSubsts::identity_for_item(self.tcx, self.tcx.closure_base_def_id(expr_def_id));
99 let substs = base_substs.extend_to(self.tcx,expr_def_id, |param, _| {
101 GenericParamDefKind::Lifetime => {
102 span_bug!(expr.span, "closure has lifetime param")
104 GenericParamDefKind::Type { .. } => {
105 self.infcx.next_ty_var(TypeVariableOrigin {
106 kind: TypeVariableOriginKind::ClosureSynthetic,
110 GenericParamDefKind::Const => {
111 span_bug!(expr.span, "closure has const param")
115 if let Some(GeneratorTypes { yield_ty, interior, movability }) = generator_types {
116 let generator_substs = substs.as_generator();
120 generator_substs.yield_ty(expr_def_id, self.tcx),
124 liberated_sig.output(),
125 generator_substs.return_ty(expr_def_id, self.tcx),
130 generator_substs.witness(expr_def_id, self.tcx),
132 return self.tcx.mk_generator(expr_def_id, substs, movability);
135 let closure_type = self.tcx.mk_closure(expr_def_id, substs);
138 "check_closure: expr.hir_id={:?} closure_type={:?}",
139 expr.hir_id, closure_type
142 // Tuple up the arguments and insert the resulting function type into
143 // the `closures` table.
144 let sig = bound_sig.map_bound(|sig| {
146 iter::once(self.tcx.intern_tup(sig.inputs())),
155 "check_closure: expr_def_id={:?}, sig={:?}, opt_kind={:?}",
156 expr_def_id, sig, opt_kind
159 let sig_fn_ptr_ty = self.tcx.mk_fn_ptr(sig);
163 substs.as_closure().sig_ty(expr_def_id, self.tcx),
166 if let Some(kind) = opt_kind {
169 kind.to_ty(self.tcx),
170 substs.as_closure().kind_ty(expr_def_id, self.tcx),
177 /// Given the expected type, figures out what it can about this closure we
178 /// are about to type check:
179 fn deduce_expectations_from_expected_type(
181 expected_ty: Ty<'tcx>,
182 ) -> (Option<ExpectedSig<'tcx>>, Option<ty::ClosureKind>) {
184 "deduce_expectations_from_expected_type(expected_ty={:?})",
188 match expected_ty.kind {
189 ty::Dynamic(ref object_type, ..) => {
190 let sig = object_type
193 let pb = pb.with_self_ty(self.tcx, self.tcx.types.err);
194 self.deduce_sig_from_projection(None, &pb)
197 let kind = object_type.principal_def_id().and_then(|did| {
198 self.tcx.lang_items().fn_trait_kind(did)
202 ty::Infer(ty::TyVar(vid)) => self.deduce_expectations_from_obligations(vid),
204 let expected_sig = ExpectedSig {
206 sig: sig.skip_binder().clone(),
208 (Some(expected_sig), Some(ty::ClosureKind::Fn))
214 fn deduce_expectations_from_obligations(
216 expected_vid: ty::TyVid,
217 ) -> (Option<ExpectedSig<'tcx>>, Option<ty::ClosureKind>) {
218 let expected_sig = self.obligations_for_self_ty(expected_vid)
219 .find_map(|(_, obligation)| {
221 "deduce_expectations_from_obligations: obligation.predicate={:?}",
225 if let ty::Predicate::Projection(ref proj_predicate) = obligation.predicate {
226 // Given a Projection predicate, we can potentially infer
227 // the complete signature.
228 self.deduce_sig_from_projection(
229 Some(obligation.cause.span),
237 // Even if we can't infer the full signature, we may be able to
238 // infer the kind. This can occur if there is a trait-reference
239 // like `F : Fn<A>`. Note that due to subtyping we could encounter
240 // many viable options, so pick the most restrictive.
241 let expected_kind = self.obligations_for_self_ty(expected_vid)
242 .filter_map(|(tr, _)| self.tcx.lang_items().fn_trait_kind(tr.def_id()))
243 .fold(None, |best, cur| {
244 Some(best.map_or(cur, |best| cmp::min(best, cur)))
247 (expected_sig, expected_kind)
250 /// Given a projection like "<F as Fn(X)>::Result == Y", we can deduce
251 /// everything we need to know about a closure or generator.
253 /// The `cause_span` should be the span that caused us to
254 /// have this expected signature, or `None` if we can't readily
256 fn deduce_sig_from_projection(
258 cause_span: Option<Span>,
259 projection: &ty::PolyProjectionPredicate<'tcx>,
260 ) -> Option<ExpectedSig<'tcx>> {
263 debug!("deduce_sig_from_projection({:?})", projection);
265 let trait_ref = projection.to_poly_trait_ref(tcx);
267 let is_fn = tcx.lang_items().fn_trait_kind(trait_ref.def_id()).is_some();
268 let gen_trait = tcx.require_lang_item(lang_items::GeneratorTraitLangItem, cause_span);
269 let is_gen = gen_trait == trait_ref.def_id();
270 if !is_fn && !is_gen {
271 debug!("deduce_sig_from_projection: not fn or generator");
276 // Check that we deduce the signature from the `<_ as std::ops::Generator>::Return`
277 // associated item and not yield.
278 let return_assoc_item = self.tcx.associated_items(gen_trait).nth(1).unwrap().def_id;
279 if return_assoc_item != projection.projection_def_id() {
280 debug!("deduce_sig_from_projection: not return assoc item of generator");
285 let input_tys = if is_fn {
286 let arg_param_ty = trait_ref.skip_binder().substs.type_at(1);
287 let arg_param_ty = self.resolve_vars_if_possible(&arg_param_ty);
288 debug!("deduce_sig_from_projection: arg_param_ty={:?}", arg_param_ty);
290 match arg_param_ty.kind {
291 ty::Tuple(tys) => tys.into_iter().map(|k| k.expect_ty()).collect::<Vec<_>>(),
295 // Generators cannot have explicit arguments.
299 let ret_param_ty = projection.skip_binder().ty;
300 let ret_param_ty = self.resolve_vars_if_possible(&ret_param_ty);
301 debug!("deduce_sig_from_projection: ret_param_ty={:?}", ret_param_ty);
303 let sig = self.tcx.mk_fn_sig(
307 hir::Unsafety::Normal,
310 debug!("deduce_sig_from_projection: sig={:?}", sig);
312 Some(ExpectedSig { cause_span, sig })
320 expected_sig: Option<ExpectedSig<'tcx>>,
321 ) -> ClosureSignatures<'tcx> {
322 if let Some(e) = expected_sig {
323 self.sig_of_closure_with_expectation(expr_def_id, decl, body, e)
325 self.sig_of_closure_no_expectation(expr_def_id, decl, body)
329 /// If there is no expected signature, then we will convert the
330 /// types that the user gave into a signature.
331 fn sig_of_closure_no_expectation(
336 ) -> ClosureSignatures<'tcx> {
337 debug!("sig_of_closure_no_expectation()");
339 let bound_sig = self.supplied_sig_of_closure(expr_def_id, decl, body);
341 self.closure_sigs(expr_def_id, body, bound_sig)
344 /// Invoked to compute the signature of a closure expression. This
345 /// combines any user-provided type annotations (e.g., `|x: u32|
346 /// -> u32 { .. }`) with the expected signature.
348 /// The approach is as follows:
350 /// - Let `S` be the (higher-ranked) signature that we derive from the user's annotations.
351 /// - Let `E` be the (higher-ranked) signature that we derive from the expectations, if any.
352 /// - If we have no expectation `E`, then the signature of the closure is `S`.
353 /// - Otherwise, the signature of the closure is E. Moreover:
354 /// - Skolemize the late-bound regions in `E`, yielding `E'`.
355 /// - Instantiate all the late-bound regions bound in the closure within `S`
356 /// with fresh (existential) variables, yielding `S'`
357 /// - Require that `E' = S'`
358 /// - We could use some kind of subtyping relationship here,
359 /// I imagine, but equality is easier and works fine for
362 /// The key intuition here is that the user's types must be valid
363 /// from "the inside" of the closure, but the expectation
364 /// ultimately drives the overall signature.
369 /// fn with_closure<F>(_: F)
370 /// where F: Fn(&u32) -> &u32 { .. }
372 /// with_closure(|x: &u32| { ... })
376 /// - E would be `fn(&u32) -> &u32`.
377 /// - S would be `fn(&u32) ->
378 /// - E' is `&'!0 u32 -> &'!0 u32`
379 /// - S' is `&'?0 u32 -> ?T`
381 /// S' can be unified with E' with `['?0 = '!0, ?T = &'!10 u32]`.
385 /// - `expr_def_id`: the `DefId` of the closure expression
386 /// - `decl`: the HIR declaration of the closure
387 /// - `body`: the body of the closure
388 /// - `expected_sig`: the expected signature (if any). Note that
389 /// this is missing a binder: that is, there may be late-bound
390 /// regions with depth 1, which are bound then by the closure.
391 fn sig_of_closure_with_expectation(
396 expected_sig: ExpectedSig<'tcx>,
397 ) -> ClosureSignatures<'tcx> {
399 "sig_of_closure_with_expectation(expected_sig={:?})",
403 // Watch out for some surprises and just ignore the
404 // expectation if things don't see to match up with what we
406 if expected_sig.sig.c_variadic != decl.c_variadic {
407 return self.sig_of_closure_no_expectation(expr_def_id, decl, body);
408 } else if expected_sig.sig.inputs_and_output.len() != decl.inputs.len() + 1 {
409 return self.sig_of_closure_with_mismatched_number_of_arguments(
417 // Create a `PolyFnSig`. Note the oddity that late bound
418 // regions appearing free in `expected_sig` are now bound up
419 // in this binder we are creating.
420 assert!(!expected_sig.sig.has_vars_bound_above(ty::INNERMOST));
421 let bound_sig = ty::Binder::bind(self.tcx.mk_fn_sig(
422 expected_sig.sig.inputs().iter().cloned(),
423 expected_sig.sig.output(),
425 hir::Unsafety::Normal,
429 // `deduce_expectations_from_expected_type` introduces
430 // late-bound lifetimes defined elsewhere, which we now
431 // anonymize away, so as not to confuse the user.
432 let bound_sig = self.tcx.anonymize_late_bound_regions(&bound_sig);
434 let closure_sigs = self.closure_sigs(expr_def_id, body, bound_sig);
436 // Up till this point, we have ignored the annotations that the user
437 // gave. This function will check that they unify successfully.
438 // Along the way, it also writes out entries for types that the user
439 // wrote into our tables, which are then later used by the privacy
441 match self.check_supplied_sig_against_expectation(expr_def_id, decl, body, &closure_sigs) {
442 Ok(infer_ok) => self.register_infer_ok_obligations(infer_ok),
443 Err(_) => return self.sig_of_closure_no_expectation(expr_def_id, decl, body),
449 fn sig_of_closure_with_mismatched_number_of_arguments(
454 expected_sig: ExpectedSig<'tcx>,
455 ) -> ClosureSignatures<'tcx> {
456 let expr_map_node = self.tcx.hir().get_if_local(expr_def_id).unwrap();
457 let expected_args: Vec<_> = expected_sig
461 .map(|ty| ArgKind::from_expected_ty(ty, None))
463 let (closure_span, found_args) = self.get_fn_like_arguments(expr_map_node);
464 let expected_span = expected_sig.cause_span.unwrap_or(closure_span);
465 self.report_arg_count_mismatch(
473 let error_sig = self.error_sig_of_closure(decl);
475 self.closure_sigs(expr_def_id, body, error_sig)
478 /// Enforce the user's types against the expectation. See
479 /// `sig_of_closure_with_expectation` for details on the overall
481 fn check_supplied_sig_against_expectation(
486 expected_sigs: &ClosureSignatures<'tcx>,
487 ) -> InferResult<'tcx, ()> {
488 // Get the signature S that the user gave.
490 // (See comment on `sig_of_closure_with_expectation` for the
491 // meaning of these letters.)
492 let supplied_sig = self.supplied_sig_of_closure(expr_def_id, decl, body);
495 "check_supplied_sig_against_expectation: supplied_sig={:?}",
499 // FIXME(#45727): As discussed in [this comment][c1], naively
500 // forcing equality here actually results in suboptimal error
501 // messages in some cases. For now, if there would have been
502 // an obvious error, we fallback to declaring the type of the
503 // closure to be the one the user gave, which allows other
504 // error message code to trigger.
506 // However, I think [there is potential to do even better
507 // here][c2], since in *this* code we have the precise span of
508 // the type parameter in question in hand when we report the
511 // [c1]: https://github.com/rust-lang/rust/pull/45072#issuecomment-341089706
512 // [c2]: https://github.com/rust-lang/rust/pull/45072#issuecomment-341096796
513 self.infcx.commit_if_ok(|_| {
514 let mut all_obligations = vec![];
516 // The liberated version of this signature should be a subtype
517 // of the liberated form of the expectation.
518 for ((hir_ty, &supplied_ty), expected_ty) in decl.inputs.iter()
519 .zip(*supplied_sig.inputs().skip_binder()) // binder moved to (*) below
520 .zip(expected_sigs.liberated_sig.inputs())
521 // `liberated_sig` is E'.
523 // Instantiate (this part of..) S to S', i.e., with fresh variables.
524 let (supplied_ty, _) = self.infcx.replace_bound_vars_with_fresh_vars(
526 LateBoundRegionConversionTime::FnCall,
527 &ty::Binder::bind(supplied_ty),
528 ); // recreated from (*) above
530 // Check that E' = S'.
531 let cause = self.misc(hir_ty.span);
535 } = self.at(&cause, self.param_env)
536 .eq(*expected_ty, supplied_ty)?;
537 all_obligations.extend(obligations);
539 // Also, require that the supplied type must outlive
541 let closure_body_region = self.tcx.mk_region(
544 id: body.value.hir_id.local_id,
545 data: region::ScopeData::Node,
549 all_obligations.push(
553 ty::Predicate::TypeOutlives(
555 ty::OutlivesPredicate(
565 let (supplied_output_ty, _) = self.infcx.replace_bound_vars_with_fresh_vars(
567 LateBoundRegionConversionTime::FnCall,
568 &supplied_sig.output(),
570 let cause = &self.misc(decl.output.span());
574 } = self.at(cause, self.param_env)
575 .eq(expected_sigs.liberated_sig.output(), supplied_output_ty)?;
576 all_obligations.extend(obligations);
580 obligations: all_obligations,
585 /// If there is no expected signature, then we will convert the
586 /// types that the user gave into a signature.
588 /// Also, record this closure signature for later.
589 fn supplied_sig_of_closure(
594 ) -> ty::PolyFnSig<'tcx> {
595 let astconv: &dyn AstConv<'_> = self;
598 "supplied_sig_of_closure(decl={:?}, body.generator_kind={:?})",
603 // First, convert the types that the user supplied (if any).
604 let supplied_arguments = decl.inputs.iter().map(|a| astconv.ast_ty_to_ty(a));
605 let supplied_return = match decl.output {
606 hir::Return(ref output) => astconv.ast_ty_to_ty(&output),
607 hir::DefaultReturn(_) => match body.generator_kind {
608 // In the case of the async block that we create for a function body,
609 // we expect the return type of the block to match that of the enclosing
611 Some(hir::GeneratorKind::Async(hir::AsyncGeneratorKind::Fn)) => {
612 debug!("supplied_sig_of_closure: closure is async fn body");
613 self.deduce_future_output_from_obligations(expr_def_id)
615 // AFAIK, deducing the future output
616 // always succeeds *except* in error cases
617 // like #65159. I'd like to return Error
618 // here, but I can't because I can't
619 // easily (and locally) prove that we
620 // *have* reported an
621 // error. --nikomatsakis
622 astconv.ty_infer(None, decl.output.span())
626 _ => astconv.ty_infer(None, decl.output.span()),
630 let result = ty::Binder::bind(self.tcx.mk_fn_sig(
634 hir::Unsafety::Normal,
638 debug!("supplied_sig_of_closure: result={:?}", result);
640 let c_result = self.inh.infcx.canonicalize_response(&result);
641 self.tables.borrow_mut().user_provided_sigs.insert(
649 /// Invoked when we are translating the generator that results
650 /// from desugaring an `async fn`. Returns the "sugared" return
651 /// type of the `async fn` -- that is, the return type that the
652 /// user specified. The "desugared" return type is a `impl
653 /// Future<Output = T>`, so we do this by searching through the
654 /// obligations to extract the `T`.
655 fn deduce_future_output_from_obligations(
658 ) -> Option<Ty<'tcx>> {
659 debug!("deduce_future_output_from_obligations(expr_def_id={:?})", expr_def_id);
664 .unwrap_or_else(|| span_bug!(
665 self.tcx.def_span(expr_def_id),
666 "async fn generator outside of a fn"
669 // In practice, the return type of the surrounding function is
670 // always a (not yet resolved) inference variable, because it
671 // is the hidden type for an `impl Trait` that we are going to
673 let ret_ty = ret_coercion.borrow().expected_ty();
674 let ret_ty = self.inh.infcx.shallow_resolve(ret_ty);
675 let ret_vid = match ret_ty.kind {
676 ty::Infer(ty::TyVar(ret_vid)) => ret_vid,
679 self.tcx.def_span(expr_def_id),
680 "async fn generator return type not an inference variable"
685 // Search for a pending obligation like
687 // `<R as Future>::Output = T`
689 // where R is the return type we are expecting. This type `T`
690 // will be our output.
691 let output_ty = self.obligations_for_self_ty(ret_vid)
692 .find_map(|(_, obligation)| {
693 if let ty::Predicate::Projection(ref proj_predicate) = obligation.predicate {
694 self.deduce_future_output_from_projection(
695 obligation.cause.span,
703 debug!("deduce_future_output_from_obligations: output_ty={:?}", output_ty);
707 /// Given a projection like
709 /// `<X as Future>::Output = T`
711 /// where `X` is some type that has no late-bound regions, returns
712 /// `Some(T)`. If the projection is for some other trait, returns
714 fn deduce_future_output_from_projection(
717 predicate: &ty::PolyProjectionPredicate<'tcx>,
718 ) -> Option<Ty<'tcx>> {
719 debug!("deduce_future_output_from_projection(predicate={:?})", predicate);
721 // We do not expect any bound regions in our predicate, so
722 // skip past the bound vars.
723 let predicate = match predicate.no_bound_vars() {
726 debug!("deduce_future_output_from_projection: has late-bound regions");
731 // Check that this is a projection from the `Future` trait.
732 let trait_ref = predicate.projection_ty.trait_ref(self.tcx);
733 let future_trait = self.tcx.lang_items().future_trait().unwrap();
734 if trait_ref.def_id != future_trait {
735 debug!("deduce_future_output_from_projection: not a future");
739 // The `Future` trait has only one associted item, `Output`,
740 // so check that this is what we see.
741 let output_assoc_item = self.tcx.associated_items(future_trait).nth(0).unwrap().def_id;
742 if output_assoc_item != predicate.projection_ty.item_def_id {
745 "projecting associated item `{:?}` from future, which is not Output `{:?}`",
746 predicate.projection_ty.item_def_id,
751 // Extract the type from the projection. Note that there can
752 // be no bound variables in this type because the "self type"
753 // does not have any regions in it.
754 let output_ty = self.resolve_vars_if_possible(&predicate.ty);
755 debug!("deduce_future_output_from_projection: output_ty={:?}", output_ty);
759 /// Converts the types that the user supplied, in case that doing
760 /// so should yield an error, but returns back a signature where
761 /// all parameters are of type `TyErr`.
762 fn error_sig_of_closure(&self, decl: &hir::FnDecl) -> ty::PolyFnSig<'tcx> {
763 let astconv: &dyn AstConv<'_> = self;
765 let supplied_arguments = decl.inputs.iter().map(|a| {
766 // Convert the types that the user supplied (if any), but ignore them.
767 astconv.ast_ty_to_ty(a);
771 if let hir::Return(ref output) = decl.output {
772 astconv.ast_ty_to_ty(&output);
775 let result = ty::Binder::bind(self.tcx.mk_fn_sig(
779 hir::Unsafety::Normal,
783 debug!("supplied_sig_of_closure: result={:?}", result);
792 bound_sig: ty::PolyFnSig<'tcx>,
793 ) -> ClosureSignatures<'tcx> {
794 let liberated_sig = self.tcx()
795 .liberate_late_bound_regions(expr_def_id, &bound_sig);
796 let liberated_sig = self.inh.normalize_associated_types_in(