1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Code for type-checking closure expressions.
13 use super::{check_fn, Expectation, FnCtxt, GeneratorTypes}
;
16 use rustc
::hir
::def_id
::DefId
;
17 use rustc
::infer
::{InferOk, InferResult}
;
18 use rustc
::infer
::LateBoundRegionConversionTime
;
19 use rustc
::infer
::type_variable
::TypeVariableOrigin
;
20 use rustc
::traits
::error_reporting
::ArgKind
;
21 use rustc
::ty
::{self, ToPolyTraitRef, Ty}
;
22 use rustc
::ty
::subst
::Substs
;
23 use rustc
::ty
::TypeFoldable
;
27 use syntax
::codemap
::Span
;
30 /// What signature do we *expect* the closure to have from context?
32 struct ExpectedSig
<'tcx
> {
33 /// Span that gave us this expectation, if we know that.
34 cause_span
: Option
<Span
>,
38 struct ClosureSignatures
<'tcx
> {
39 bound_sig
: ty
::PolyFnSig
<'tcx
>,
40 liberated_sig
: ty
::FnSig
<'tcx
>,
43 impl<'a
, 'gcx
, 'tcx
> FnCtxt
<'a
, 'gcx
, 'tcx
> {
44 pub fn check_expr_closure(
47 _capture
: hir
::CaptureClause
,
48 decl
: &'gcx hir
::FnDecl
,
50 gen
: Option
<hir
::GeneratorMovability
>,
51 expected
: Expectation
<'tcx
>,
54 "check_expr_closure(expr={:?},expected={:?})",
58 // It's always helpful for inference if we know the kind of
59 // closure sooner rather than later, so first examine the expected
60 // type, and see if can glean a closure kind from there.
61 let (expected_sig
, expected_kind
) = match expected
.to_option(self) {
62 Some(ty
) => self.deduce_expectations_from_expected_type(ty
),
65 let body
= self.tcx
.hir
.body(body_id
);
66 self.check_closure(expr
, expected_kind
, decl
, body
, gen
, expected_sig
)
72 opt_kind
: Option
<ty
::ClosureKind
>,
73 decl
: &'gcx hir
::FnDecl
,
74 body
: &'gcx hir
::Body
,
75 gen
: Option
<hir
::GeneratorMovability
>,
76 expected_sig
: Option
<ExpectedSig
<'tcx
>>,
79 "check_closure(opt_kind={:?}, expected_sig={:?})",
80 opt_kind
, expected_sig
83 let expr_def_id
= self.tcx
.hir
.local_def_id(expr
.id
);
85 let ClosureSignatures
{
88 } = self.sig_of_closure(expr_def_id
, decl
, body
, expected_sig
);
90 debug
!("check_closure: ty_of_closure returns {:?}", liberated_sig
);
92 let generator_types
= check_fn(
102 // Create type variables (for now) to represent the transformed
103 // types of upvars. These will be unified during the upvar
104 // inference phase (`upvar.rs`).
106 Substs
::identity_for_item(self.tcx
, self.tcx
.closure_base_def_id(expr_def_id
));
107 let substs
= base_substs
.extend_to(
110 |_
, _
| span_bug
!(expr
.span
, "closure has region param"),
113 .next_ty_var(TypeVariableOrigin
::ClosureSynthetic(expr
.span
))
116 let substs
= ty
::ClosureSubsts { substs }
;
117 let closure_type
= self.tcx
.mk_closure(expr_def_id
, substs
);
119 if let Some(GeneratorTypes { yield_ty, interior }
) = generator_types
{
123 substs
.generator_yield_ty(expr_def_id
, self.tcx
),
127 liberated_sig
.output(),
128 substs
.generator_return_ty(expr_def_id
, self.tcx
),
130 return self.tcx
.mk_generator(expr_def_id
, substs
, interior
);
134 "check_closure: expr.id={:?} closure_type={:?}",
135 expr
.id
, closure_type
138 // Tuple up the arguments and insert the resulting function type into
139 // the `closures` table.
140 let sig
= bound_sig
.map_bound(|sig
| {
142 iter
::once(self.tcx
.intern_tup(sig
.inputs())),
151 "check_closure: expr_def_id={:?}, sig={:?}, opt_kind={:?}",
152 expr_def_id
, sig
, opt_kind
155 let sig_fn_ptr_ty
= self.tcx
.mk_fn_ptr(sig
);
159 substs
.closure_sig_ty(expr_def_id
, self.tcx
),
162 if let Some(kind
) = opt_kind
{
165 kind
.to_ty(self.tcx
),
166 substs
.closure_kind_ty(expr_def_id
, self.tcx
),
173 /// Given the expected type, figures out what it can about this closure we
174 /// are about to type check:
175 fn deduce_expectations_from_expected_type(
177 expected_ty
: Ty
<'tcx
>,
178 ) -> (Option
<ExpectedSig
<'tcx
>>, Option
<ty
::ClosureKind
>) {
180 "deduce_expectations_from_expected_type(expected_ty={:?})",
184 match expected_ty
.sty
{
185 ty
::TyDynamic(ref object_type
, ..) => {
186 let sig
= object_type
189 let pb
= pb
.with_self_ty(self.tcx
, self.tcx
.types
.err
);
190 self.deduce_sig_from_projection(None
, &pb
)
193 let kind
= object_type
195 .and_then(|p
| self.tcx
.lang_items().fn_trait_kind(p
.def_id()));
198 ty
::TyInfer(ty
::TyVar(vid
)) => self.deduce_expectations_from_obligations(vid
),
199 ty
::TyFnPtr(sig
) => {
200 let expected_sig
= ExpectedSig
{
202 sig
: sig
.skip_binder().clone(),
204 (Some(expected_sig
), Some(ty
::ClosureKind
::Fn
))
210 fn deduce_expectations_from_obligations(
212 expected_vid
: ty
::TyVid
,
213 ) -> (Option
<ExpectedSig
<'tcx
>>, Option
<ty
::ClosureKind
>) {
214 let fulfillment_cx
= self.fulfillment_cx
.borrow();
215 // Here `expected_ty` is known to be a type inference variable.
217 let expected_sig
= fulfillment_cx
218 .pending_obligations()
220 .map(|obligation
| &obligation
.obligation
)
221 .filter_map(|obligation
| {
223 "deduce_expectations_from_obligations: obligation.predicate={:?}",
227 match obligation
.predicate
{
228 // Given a Projection predicate, we can potentially infer
229 // the complete signature.
230 ty
::Predicate
::Projection(ref proj_predicate
) => {
231 let trait_ref
= proj_predicate
.to_poly_trait_ref(self.tcx
);
232 self.self_type_matches_expected_vid(trait_ref
, expected_vid
)
234 self.deduce_sig_from_projection(
235 Some(obligation
.cause
.span
),
245 // Even if we can't infer the full signature, we may be able to
246 // infer the kind. This can occur if there is a trait-reference
247 // like `F : Fn<A>`. Note that due to subtyping we could encounter
248 // many viable options, so pick the most restrictive.
249 let expected_kind
= fulfillment_cx
250 .pending_obligations()
252 .map(|obligation
| &obligation
.obligation
)
253 .filter_map(|obligation
| {
254 let opt_trait_ref
= match obligation
.predicate
{
255 ty
::Predicate
::Projection(ref data
) => Some(data
.to_poly_trait_ref(self.tcx
)),
256 ty
::Predicate
::Trait(ref data
) => Some(data
.to_poly_trait_ref()),
257 ty
::Predicate
::Subtype(..) => None
,
258 ty
::Predicate
::RegionOutlives(..) => None
,
259 ty
::Predicate
::TypeOutlives(..) => None
,
260 ty
::Predicate
::WellFormed(..) => None
,
261 ty
::Predicate
::ObjectSafe(..) => None
,
262 ty
::Predicate
::ConstEvaluatable(..) => None
,
264 // NB: This predicate is created by breaking down a
265 // `ClosureType: FnFoo()` predicate, where
266 // `ClosureType` represents some `TyClosure`. It can't
267 // possibly be referring to the current closure,
268 // because we haven't produced the `TyClosure` for
269 // this closure yet; this is exactly why the other
270 // code is looking for a self type of a unresolved
271 // inference variable.
272 ty
::Predicate
::ClosureKind(..) => None
,
275 .and_then(|tr
| self.self_type_matches_expected_vid(tr
, expected_vid
))
276 .and_then(|tr
| self.tcx
.lang_items().fn_trait_kind(tr
.def_id()))
278 .fold(None
, |best
, cur
| {
279 Some(best
.map_or(cur
, |best
| cmp
::min(best
, cur
)))
282 (expected_sig
, expected_kind
)
285 /// Given a projection like "<F as Fn(X)>::Result == Y", we can deduce
286 /// everything we need to know about a closure.
288 /// The `cause_span` should be the span that caused us to
289 /// have this expected signature, or `None` if we can't readily
291 fn deduce_sig_from_projection(
293 cause_span
: Option
<Span
>,
294 projection
: &ty
::PolyProjectionPredicate
<'tcx
>,
295 ) -> Option
<ExpectedSig
<'tcx
>> {
298 debug
!("deduce_sig_from_projection({:?})", projection
);
300 let trait_ref
= projection
.to_poly_trait_ref(tcx
);
302 if tcx
.lang_items().fn_trait_kind(trait_ref
.def_id()).is_none() {
306 let arg_param_ty
= trait_ref
.substs().type_at(1);
307 let arg_param_ty
= self.resolve_type_vars_if_possible(&arg_param_ty
);
309 "deduce_sig_from_projection: arg_param_ty {:?}",
313 let input_tys
= match arg_param_ty
.sty
{
314 ty
::TyTuple(tys
) => tys
.into_iter(),
320 let ret_param_ty
= projection
.0.ty
;
321 let ret_param_ty
= self.resolve_type_vars_if_possible(&ret_param_ty
);
323 "deduce_sig_from_projection: ret_param_ty {:?}",
327 let sig
= self.tcx
.mk_fn_sig(
331 hir
::Unsafety
::Normal
,
334 debug
!("deduce_sig_from_projection: sig {:?}", sig
);
336 Some(ExpectedSig { cause_span, sig }
)
339 fn self_type_matches_expected_vid(
341 trait_ref
: ty
::PolyTraitRef
<'tcx
>,
342 expected_vid
: ty
::TyVid
,
343 ) -> Option
<ty
::PolyTraitRef
<'tcx
>> {
344 let self_ty
= self.shallow_resolve(trait_ref
.self_ty());
346 "self_type_matches_expected_vid(trait_ref={:?}, self_ty={:?})",
350 ty
::TyInfer(ty
::TyVar(v
)) if expected_vid
== v
=> Some(trait_ref
),
360 expected_sig
: Option
<ExpectedSig
<'tcx
>>,
361 ) -> ClosureSignatures
<'tcx
> {
362 if let Some(e
) = expected_sig
{
363 self.sig_of_closure_with_expectation(expr_def_id
, decl
, body
, e
)
365 self.sig_of_closure_no_expectation(expr_def_id
, decl
, body
)
369 /// If there is no expected signature, then we will convert the
370 /// types that the user gave into a signature.
371 fn sig_of_closure_no_expectation(
376 ) -> ClosureSignatures
<'tcx
> {
377 debug
!("sig_of_closure_no_expectation()");
379 let bound_sig
= self.supplied_sig_of_closure(decl
);
381 self.closure_sigs(expr_def_id
, body
, bound_sig
)
384 /// Invoked to compute the signature of a closure expression. This
385 /// combines any user-provided type annotations (e.g., `|x: u32|
386 /// -> u32 { .. }`) with the expected signature.
388 /// The approach is as follows:
390 /// - Let `S` be the (higher-ranked) signature that we derive from the user's annotations.
391 /// - Let `E` be the (higher-ranked) signature that we derive from the expectations, if any.
392 /// - If we have no expectation `E`, then the signature of the closure is `S`.
393 /// - Otherwise, the signature of the closure is E. Moreover:
394 /// - Skolemize the late-bound regions in `E`, yielding `E'`.
395 /// - Instantiate all the late-bound regions bound in the closure within `S`
396 /// with fresh (existential) variables, yielding `S'`
397 /// - Require that `E' = S'`
398 /// - We could use some kind of subtyping relationship here,
399 /// I imagine, but equality is easier and works fine for
402 /// The key intuition here is that the user's types must be valid
403 /// from "the inside" of the closure, but the expectation
404 /// ultimately drives the overall signature.
409 /// fn with_closure<F>(_: F)
410 /// where F: Fn(&u32) -> &u32 { .. }
412 /// with_closure(|x: &u32| { ... })
416 /// - E would be `fn(&u32) -> &u32`.
417 /// - S would be `fn(&u32) ->
418 /// - E' is `&'!0 u32 -> &'!0 u32`
419 /// - S' is `&'?0 u32 -> ?T`
421 /// S' can be unified with E' with `['?0 = '!0, ?T = &'!10 u32]`.
425 /// - `expr_def_id`: the def-id of the closure expression
426 /// - `decl`: the HIR declaration of the closure
427 /// - `body`: the body of the closure
428 /// - `expected_sig`: the expected signature (if any). Note that
429 /// this is missing a binder: that is, there may be late-bound
430 /// regions with depth 1, which are bound then by the closure.
431 fn sig_of_closure_with_expectation(
436 expected_sig
: ExpectedSig
<'tcx
>,
437 ) -> ClosureSignatures
<'tcx
> {
439 "sig_of_closure_with_expectation(expected_sig={:?})",
443 // Watch out for some surprises and just ignore the
444 // expectation if things don't see to match up with what we
446 if expected_sig
.sig
.variadic
!= decl
.variadic
{
447 return self.sig_of_closure_no_expectation(expr_def_id
, decl
, body
);
448 } else if expected_sig
.sig
.inputs_and_output
.len() != decl
.inputs
.len() + 1 {
449 return self.sig_of_closure_with_mismatched_number_of_arguments(
457 // Create a `PolyFnSig`. Note the oddity that late bound
458 // regions appearing free in `expected_sig` are now bound up
459 // in this binder we are creating.
460 assert
!(!expected_sig
.sig
.has_regions_escaping_depth(1));
461 let bound_sig
= ty
::Binder(self.tcx
.mk_fn_sig(
462 expected_sig
.sig
.inputs().iter().cloned(),
463 expected_sig
.sig
.output(),
465 hir
::Unsafety
::Normal
,
469 // `deduce_expectations_from_expected_type` introduces
470 // late-bound lifetimes defined elsewhere, which we now
471 // anonymize away, so as not to confuse the user.
472 let bound_sig
= self.tcx
.anonymize_late_bound_regions(&bound_sig
);
474 let closure_sigs
= self.closure_sigs(expr_def_id
, body
, bound_sig
);
476 // Up till this point, we have ignored the annotations that the user
477 // gave. This function will check that they unify successfully.
478 // Along the way, it also writes out entries for types that the user
479 // wrote into our tables, which are then later used by the privacy
481 match self.check_supplied_sig_against_expectation(decl
, &closure_sigs
) {
482 Ok(infer_ok
) => self.register_infer_ok_obligations(infer_ok
),
483 Err(_
) => return self.sig_of_closure_no_expectation(expr_def_id
, decl
, body
),
489 fn sig_of_closure_with_mismatched_number_of_arguments(
494 expected_sig
: ExpectedSig
<'tcx
>,
495 ) -> ClosureSignatures
<'tcx
> {
496 let expr_map_node
= self.tcx
.hir
.get_if_local(expr_def_id
).unwrap();
497 let expected_args
: Vec
<_
> = expected_sig
501 .map(|ty
| ArgKind
::from_expected_ty(ty
))
503 let (closure_span
, found_args
) = self.get_fn_like_arguments(expr_map_node
);
504 let expected_span
= expected_sig
.cause_span
.unwrap_or(closure_span
);
505 self.report_arg_count_mismatch(
513 let error_sig
= self.error_sig_of_closure(decl
);
515 self.closure_sigs(expr_def_id
, body
, error_sig
)
518 /// Enforce the user's types against the expectation. See
519 /// `sig_of_closure_with_expectation` for details on the overall
521 fn check_supplied_sig_against_expectation(
524 expected_sigs
: &ClosureSignatures
<'tcx
>,
525 ) -> InferResult
<'tcx
, ()> {
526 // Get the signature S that the user gave.
528 // (See comment on `sig_of_closure_with_expectation` for the
529 // meaning of these letters.)
530 let supplied_sig
= self.supplied_sig_of_closure(decl
);
533 "check_supplied_sig_against_expectation: supplied_sig={:?}",
537 // FIXME(#45727): As discussed in [this comment][c1], naively
538 // forcing equality here actually results in suboptimal error
539 // messages in some cases. For now, if there would have been
540 // an obvious error, we fallback to declaring the type of the
541 // closure to be the one the user gave, which allows other
542 // error message code to trigger.
544 // However, I think [there is potential to do even better
545 // here][c2], since in *this* code we have the precise span of
546 // the type parameter in question in hand when we report the
549 // [c1]: https://github.com/rust-lang/rust/pull/45072#issuecomment-341089706
550 // [c2]: https://github.com/rust-lang/rust/pull/45072#issuecomment-341096796
551 self.infcx
.commit_if_ok(|_
| {
552 let mut all_obligations
= vec
![];
554 // The liberated version of this signature should be be a subtype
555 // of the liberated form of the expectation.
556 for ((hir_ty
, &supplied_ty
), expected_ty
) in decl
.inputs
.iter()
557 .zip(*supplied_sig
.inputs().skip_binder()) // binder moved to (*) below
558 .zip(expected_sigs
.liberated_sig
.inputs())
559 // `liberated_sig` is E'.
561 // Instantiate (this part of..) S to S', i.e., with fresh variables.
562 let (supplied_ty
, _
) = self.infcx
.replace_late_bound_regions_with_fresh_var(
564 LateBoundRegionConversionTime
::FnCall
,
565 &ty
::Binder(supplied_ty
),
566 ); // recreated from (*) above
568 // Check that E' = S'.
569 let cause
= &self.misc(hir_ty
.span
);
573 } = self.at(cause
, self.param_env
)
574 .eq(*expected_ty
, supplied_ty
)?
;
575 all_obligations
.extend(obligations
);
578 let (supplied_output_ty
, _
) = self.infcx
.replace_late_bound_regions_with_fresh_var(
580 LateBoundRegionConversionTime
::FnCall
,
581 &supplied_sig
.output(),
583 let cause
= &self.misc(decl
.output
.span());
587 } = self.at(cause
, self.param_env
)
588 .eq(expected_sigs
.liberated_sig
.output(), supplied_output_ty
)?
;
589 all_obligations
.extend(obligations
);
593 obligations
: all_obligations
,
598 /// If there is no expected signature, then we will convert the
599 /// types that the user gave into a signature.
600 fn supplied_sig_of_closure(&self, decl
: &hir
::FnDecl
) -> ty
::PolyFnSig
<'tcx
> {
601 let astconv
: &AstConv
= self;
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(_
) => astconv
.ty_infer(decl
.output
.span()),
610 let result
= ty
::Binder(self.tcx
.mk_fn_sig(
614 hir
::Unsafety
::Normal
,
618 debug
!("supplied_sig_of_closure: result={:?}", result
);
623 /// Converts the types that the user supplied, in case that doing
624 /// so should yield an error, but returns back a signature where
625 /// all parameters are of type `TyErr`.
626 fn error_sig_of_closure(&self, decl
: &hir
::FnDecl
) -> ty
::PolyFnSig
<'tcx
> {
627 let astconv
: &AstConv
= self;
629 let supplied_arguments
= decl
.inputs
.iter().map(|a
| {
630 // Convert the types that the user supplied (if any), but ignore them.
631 astconv
.ast_ty_to_ty(a
);
636 hir
::Return(ref output
) => {
637 astconv
.ast_ty_to_ty(&output
);
639 hir
::DefaultReturn(_
) => {}
642 let result
= ty
::Binder(self.tcx
.mk_fn_sig(
646 hir
::Unsafety
::Normal
,
650 debug
!("supplied_sig_of_closure: result={:?}", result
);
659 bound_sig
: ty
::PolyFnSig
<'tcx
>,
660 ) -> ClosureSignatures
<'tcx
> {
661 let liberated_sig
= self.tcx()
662 .liberate_late_bound_regions(expr_def_id
, &bound_sig
);
663 let liberated_sig
= self.inh
.normalize_associated_types_in(