]> git.lizzy.rs Git - rust.git/blob - src/librustc_typeck/check/closure.rs
Auto merge of #61492 - RalfJung:const-qualif-comments, r=eddyb
[rust.git] / src / librustc_typeck / check / closure.rs
1 //! Code for type-checking closure expressions.
2
3 use super::{check_fn, Expectation, FnCtxt, GeneratorTypes};
4
5 use crate::astconv::AstConv;
6 use crate::middle::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;
16 use std::cmp;
17 use std::iter;
18 use rustc_target::spec::abi::Abi;
19 use syntax::source_map::Span;
20 use rustc::hir;
21
22 /// What signature do we *expect* the closure to have from context?
23 #[derive(Debug)]
24 struct ExpectedSig<'tcx> {
25     /// Span that gave us this expectation, if we know that.
26     cause_span: Option<Span>,
27     sig: ty::FnSig<'tcx>,
28 }
29
30 struct ClosureSignatures<'tcx> {
31     bound_sig: ty::PolyFnSig<'tcx>,
32     liberated_sig: ty::FnSig<'tcx>,
33 }
34
35 impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
36     pub fn check_expr_closure(
37         &self,
38         expr: &hir::Expr,
39         _capture: hir::CaptureClause,
40         decl: &'gcx hir::FnDecl,
41         body_id: hir::BodyId,
42         gen: Option<hir::GeneratorMovability>,
43         expected: Expectation<'tcx>,
44     ) -> Ty<'tcx> {
45         debug!(
46             "check_expr_closure(expr={:?},expected={:?})",
47             expr, expected
48         );
49
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),
55             None => (None, None),
56         };
57         let body = self.tcx.hir().body(body_id);
58         self.check_closure(expr, expected_kind, decl, body, gen, expected_sig)
59     }
60
61     fn check_closure(
62         &self,
63         expr: &hir::Expr,
64         opt_kind: Option<ty::ClosureKind>,
65         decl: &'gcx hir::FnDecl,
66         body: &'gcx hir::Body,
67         gen: Option<hir::GeneratorMovability>,
68         expected_sig: Option<ExpectedSig<'tcx>>,
69     ) -> Ty<'tcx> {
70         debug!(
71             "check_closure(opt_kind={:?}, expected_sig={:?})",
72             opt_kind, expected_sig
73         );
74
75         let expr_def_id = self.tcx.hir().local_def_id_from_hir_id(expr.hir_id);
76
77         let ClosureSignatures {
78             bound_sig,
79             liberated_sig,
80         } = self.sig_of_closure(expr_def_id, decl, body, expected_sig);
81
82         debug!("check_closure: ty_of_closure returns {:?}", liberated_sig);
83
84         let generator_types = check_fn(
85             self,
86             self.param_env,
87             liberated_sig,
88             decl,
89             expr.hir_id,
90             body,
91             gen,
92         ).1;
93
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`).
97         let base_substs =
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, _| {
100             match param.kind {
101                 GenericParamDefKind::Lifetime => {
102                     span_bug!(expr.span, "closure has lifetime param")
103                 }
104                 GenericParamDefKind::Type { .. } => {
105                     self.infcx.next_ty_var(TypeVariableOrigin {
106                         kind: TypeVariableOriginKind::ClosureSynthetic,
107                         span: expr.span,
108                     }).into()
109                 }
110                 GenericParamDefKind::Const => {
111                     span_bug!(expr.span, "closure has const param")
112                 }
113             }
114         });
115         if let Some(GeneratorTypes { yield_ty, interior, movability }) = generator_types {
116             let substs = ty::GeneratorSubsts { substs };
117             self.demand_eqtype(
118                 expr.span,
119                 yield_ty,
120                 substs.yield_ty(expr_def_id, self.tcx),
121             );
122             self.demand_eqtype(
123                 expr.span,
124                 liberated_sig.output(),
125                 substs.return_ty(expr_def_id, self.tcx),
126             );
127             self.demand_eqtype(
128                 expr.span,
129                 interior,
130                 substs.witness(expr_def_id, self.tcx),
131             );
132             return self.tcx.mk_generator(expr_def_id, substs, movability);
133         }
134
135         let substs = ty::ClosureSubsts { substs };
136         let closure_type = self.tcx.mk_closure(expr_def_id, substs);
137
138         debug!(
139             "check_closure: expr.hir_id={:?} closure_type={:?}",
140             expr.hir_id, closure_type
141         );
142
143         // Tuple up the arguments and insert the resulting function type into
144         // the `closures` table.
145         let sig = bound_sig.map_bound(|sig| {
146             self.tcx.mk_fn_sig(
147                 iter::once(self.tcx.intern_tup(sig.inputs())),
148                 sig.output(),
149                 sig.c_variadic,
150                 sig.unsafety,
151                 sig.abi,
152             )
153         });
154
155         debug!(
156             "check_closure: expr_def_id={:?}, sig={:?}, opt_kind={:?}",
157             expr_def_id, sig, opt_kind
158         );
159
160         let sig_fn_ptr_ty = self.tcx.mk_fn_ptr(sig);
161         self.demand_eqtype(
162             expr.span,
163             sig_fn_ptr_ty,
164             substs.closure_sig_ty(expr_def_id, self.tcx),
165         );
166
167         if let Some(kind) = opt_kind {
168             self.demand_eqtype(
169                 expr.span,
170                 kind.to_ty(self.tcx),
171                 substs.closure_kind_ty(expr_def_id, self.tcx),
172             );
173         }
174
175         closure_type
176     }
177
178     /// Given the expected type, figures out what it can about this closure we
179     /// are about to type check:
180     fn deduce_expectations_from_expected_type(
181         &self,
182         expected_ty: Ty<'tcx>,
183     ) -> (Option<ExpectedSig<'tcx>>, Option<ty::ClosureKind>) {
184         debug!(
185             "deduce_expectations_from_expected_type(expected_ty={:?})",
186             expected_ty
187         );
188
189         match expected_ty.sty {
190             ty::Dynamic(ref object_type, ..) => {
191                 let sig = object_type
192                     .projection_bounds()
193                     .filter_map(|pb| {
194                         let pb = pb.with_self_ty(self.tcx, self.tcx.types.err);
195                         self.deduce_sig_from_projection(None, &pb)
196                     })
197                     .next();
198                 let kind = object_type.principal_def_id().and_then(|did| {
199                     self.tcx.lang_items().fn_trait_kind(did)
200                 });
201                 (sig, kind)
202             }
203             ty::Infer(ty::TyVar(vid)) => self.deduce_expectations_from_obligations(vid),
204             ty::FnPtr(sig) => {
205                 let expected_sig = ExpectedSig {
206                     cause_span: None,
207                     sig: sig.skip_binder().clone(),
208                 };
209                 (Some(expected_sig), Some(ty::ClosureKind::Fn))
210             }
211             _ => (None, None),
212         }
213     }
214
215     fn deduce_expectations_from_obligations(
216         &self,
217         expected_vid: ty::TyVid,
218     ) -> (Option<ExpectedSig<'tcx>>, Option<ty::ClosureKind>) {
219         let expected_sig = self.obligations_for_self_ty(expected_vid)
220             .find_map(|(_, obligation)| {
221                 debug!(
222                     "deduce_expectations_from_obligations: obligation.predicate={:?}",
223                     obligation.predicate
224                 );
225
226                 if let ty::Predicate::Projection(ref proj_predicate) = obligation.predicate {
227                     // Given a Projection predicate, we can potentially infer
228                     // the complete signature.
229                     self.deduce_sig_from_projection(
230                         Some(obligation.cause.span),
231                         proj_predicate
232                     )
233                 } else {
234                     None
235                 }
236             });
237
238         // Even if we can't infer the full signature, we may be able to
239         // infer the kind. This can occur if there is a trait-reference
240         // like `F : Fn<A>`. Note that due to subtyping we could encounter
241         // many viable options, so pick the most restrictive.
242         let expected_kind = self.obligations_for_self_ty(expected_vid)
243             .filter_map(|(tr, _)| self.tcx.lang_items().fn_trait_kind(tr.def_id()))
244             .fold(None, |best, cur| {
245                 Some(best.map_or(cur, |best| cmp::min(best, cur)))
246             });
247
248         (expected_sig, expected_kind)
249     }
250
251     /// Given a projection like "<F as Fn(X)>::Result == Y", we can deduce
252     /// everything we need to know about a closure or generator.
253     ///
254     /// The `cause_span` should be the span that caused us to
255     /// have this expected signature, or `None` if we can't readily
256     /// know that.
257     fn deduce_sig_from_projection(
258         &self,
259         cause_span: Option<Span>,
260         projection: &ty::PolyProjectionPredicate<'tcx>,
261     ) -> Option<ExpectedSig<'tcx>> {
262         let tcx = self.tcx;
263
264         debug!("deduce_sig_from_projection({:?})", projection);
265
266         let trait_ref = projection.to_poly_trait_ref(tcx);
267
268         let is_fn = tcx.lang_items().fn_trait_kind(trait_ref.def_id()).is_some();
269         let gen_trait = tcx.lang_items().gen_trait().unwrap();
270         let is_gen = gen_trait == trait_ref.def_id();
271         if !is_fn && !is_gen {
272             debug!("deduce_sig_from_projection: not fn or generator");
273             return None;
274         }
275
276         if is_gen {
277             // Check that we deduce the signature from the `<_ as std::ops::Generator>::Return`
278             // associated item and not yield.
279             let return_assoc_item = self.tcx.associated_items(gen_trait).nth(1).unwrap().def_id;
280             if return_assoc_item != projection.projection_def_id() {
281                 debug!("deduce_sig_from_projection: not return assoc item of generator");
282                 return None;
283             }
284         }
285
286         let input_tys = if is_fn {
287             let arg_param_ty = trait_ref.skip_binder().substs.type_at(1);
288             let arg_param_ty = self.resolve_vars_if_possible(&arg_param_ty);
289             debug!("deduce_sig_from_projection: arg_param_ty={:?}", arg_param_ty);
290
291             match arg_param_ty.sty {
292                 ty::Tuple(tys) => tys.into_iter().map(|k| k.expect_ty()).collect::<Vec<_>>(),
293                 _ => return None,
294             }
295         } else {
296             // Generators cannot have explicit arguments.
297             vec![]
298         };
299
300         let ret_param_ty = projection.skip_binder().ty;
301         let ret_param_ty = self.resolve_vars_if_possible(&ret_param_ty);
302         debug!("deduce_sig_from_projection: ret_param_ty={:?}", ret_param_ty);
303
304         let sig = self.tcx.mk_fn_sig(
305             input_tys.iter(),
306             &ret_param_ty,
307             false,
308             hir::Unsafety::Normal,
309             Abi::Rust,
310         );
311         debug!("deduce_sig_from_projection: sig={:?}", sig);
312
313         Some(ExpectedSig { cause_span, sig })
314     }
315
316     fn sig_of_closure(
317         &self,
318         expr_def_id: DefId,
319         decl: &hir::FnDecl,
320         body: &hir::Body,
321         expected_sig: Option<ExpectedSig<'tcx>>,
322     ) -> ClosureSignatures<'tcx> {
323         if let Some(e) = expected_sig {
324             self.sig_of_closure_with_expectation(expr_def_id, decl, body, e)
325         } else {
326             self.sig_of_closure_no_expectation(expr_def_id, decl, body)
327         }
328     }
329
330     /// If there is no expected signature, then we will convert the
331     /// types that the user gave into a signature.
332     fn sig_of_closure_no_expectation(
333         &self,
334         expr_def_id: DefId,
335         decl: &hir::FnDecl,
336         body: &hir::Body,
337     ) -> ClosureSignatures<'tcx> {
338         debug!("sig_of_closure_no_expectation()");
339
340         let bound_sig = self.supplied_sig_of_closure(expr_def_id, decl);
341
342         self.closure_sigs(expr_def_id, body, bound_sig)
343     }
344
345     /// Invoked to compute the signature of a closure expression. This
346     /// combines any user-provided type annotations (e.g., `|x: u32|
347     /// -> u32 { .. }`) with the expected signature.
348     ///
349     /// The approach is as follows:
350     ///
351     /// - Let `S` be the (higher-ranked) signature that we derive from the user's annotations.
352     /// - Let `E` be the (higher-ranked) signature that we derive from the expectations, if any.
353     ///   - If we have no expectation `E`, then the signature of the closure is `S`.
354     ///   - Otherwise, the signature of the closure is E. Moreover:
355     ///     - Skolemize the late-bound regions in `E`, yielding `E'`.
356     ///     - Instantiate all the late-bound regions bound in the closure within `S`
357     ///       with fresh (existential) variables, yielding `S'`
358     ///     - Require that `E' = S'`
359     ///       - We could use some kind of subtyping relationship here,
360     ///         I imagine, but equality is easier and works fine for
361     ///         our purposes.
362     ///
363     /// The key intuition here is that the user's types must be valid
364     /// from "the inside" of the closure, but the expectation
365     /// ultimately drives the overall signature.
366     ///
367     /// # Examples
368     ///
369     /// ```
370     /// fn with_closure<F>(_: F)
371     ///   where F: Fn(&u32) -> &u32 { .. }
372     ///
373     /// with_closure(|x: &u32| { ... })
374     /// ```
375     ///
376     /// Here:
377     /// - E would be `fn(&u32) -> &u32`.
378     /// - S would be `fn(&u32) ->
379     /// - E' is `&'!0 u32 -> &'!0 u32`
380     /// - S' is `&'?0 u32 -> ?T`
381     ///
382     /// S' can be unified with E' with `['?0 = '!0, ?T = &'!10 u32]`.
383     ///
384     /// # Arguments
385     ///
386     /// - `expr_def_id`: the `DefId` of the closure expression
387     /// - `decl`: the HIR declaration of the closure
388     /// - `body`: the body of the closure
389     /// - `expected_sig`: the expected signature (if any). Note that
390     ///   this is missing a binder: that is, there may be late-bound
391     ///   regions with depth 1, which are bound then by the closure.
392     fn sig_of_closure_with_expectation(
393         &self,
394         expr_def_id: DefId,
395         decl: &hir::FnDecl,
396         body: &hir::Body,
397         expected_sig: ExpectedSig<'tcx>,
398     ) -> ClosureSignatures<'tcx> {
399         debug!(
400             "sig_of_closure_with_expectation(expected_sig={:?})",
401             expected_sig
402         );
403
404         // Watch out for some surprises and just ignore the
405         // expectation if things don't see to match up with what we
406         // expect.
407         if expected_sig.sig.c_variadic != decl.c_variadic {
408             return self.sig_of_closure_no_expectation(expr_def_id, decl, body);
409         } else if expected_sig.sig.inputs_and_output.len() != decl.inputs.len() + 1 {
410             return self.sig_of_closure_with_mismatched_number_of_arguments(
411                 expr_def_id,
412                 decl,
413                 body,
414                 expected_sig,
415             );
416         }
417
418         // Create a `PolyFnSig`. Note the oddity that late bound
419         // regions appearing free in `expected_sig` are now bound up
420         // in this binder we are creating.
421         assert!(!expected_sig.sig.has_vars_bound_above(ty::INNERMOST));
422         let bound_sig = ty::Binder::bind(self.tcx.mk_fn_sig(
423             expected_sig.sig.inputs().iter().cloned(),
424             expected_sig.sig.output(),
425             decl.c_variadic,
426             hir::Unsafety::Normal,
427             Abi::RustCall,
428         ));
429
430         // `deduce_expectations_from_expected_type` introduces
431         // late-bound lifetimes defined elsewhere, which we now
432         // anonymize away, so as not to confuse the user.
433         let bound_sig = self.tcx.anonymize_late_bound_regions(&bound_sig);
434
435         let closure_sigs = self.closure_sigs(expr_def_id, body, bound_sig);
436
437         // Up till this point, we have ignored the annotations that the user
438         // gave. This function will check that they unify successfully.
439         // Along the way, it also writes out entries for types that the user
440         // wrote into our tables, which are then later used by the privacy
441         // check.
442         match self.check_supplied_sig_against_expectation(expr_def_id, decl, body, &closure_sigs) {
443             Ok(infer_ok) => self.register_infer_ok_obligations(infer_ok),
444             Err(_) => return self.sig_of_closure_no_expectation(expr_def_id, decl, body),
445         }
446
447         closure_sigs
448     }
449
450     fn sig_of_closure_with_mismatched_number_of_arguments(
451         &self,
452         expr_def_id: DefId,
453         decl: &hir::FnDecl,
454         body: &hir::Body,
455         expected_sig: ExpectedSig<'tcx>,
456     ) -> ClosureSignatures<'tcx> {
457         let expr_map_node = self.tcx.hir().get_if_local(expr_def_id).unwrap();
458         let expected_args: Vec<_> = expected_sig
459             .sig
460             .inputs()
461             .iter()
462             .map(|ty| ArgKind::from_expected_ty(ty, None))
463             .collect();
464         let (closure_span, found_args) = self.get_fn_like_arguments(expr_map_node);
465         let expected_span = expected_sig.cause_span.unwrap_or(closure_span);
466         self.report_arg_count_mismatch(
467             expected_span,
468             Some(closure_span),
469             expected_args,
470             found_args,
471             true,
472         ).emit();
473
474         let error_sig = self.error_sig_of_closure(decl);
475
476         self.closure_sigs(expr_def_id, body, error_sig)
477     }
478
479     /// Enforce the user's types against the expectation. See
480     /// `sig_of_closure_with_expectation` for details on the overall
481     /// strategy.
482     fn check_supplied_sig_against_expectation(
483         &self,
484         expr_def_id: DefId,
485         decl: &hir::FnDecl,
486         body: &hir::Body,
487         expected_sigs: &ClosureSignatures<'tcx>,
488     ) -> InferResult<'tcx, ()> {
489         // Get the signature S that the user gave.
490         //
491         // (See comment on `sig_of_closure_with_expectation` for the
492         // meaning of these letters.)
493         let supplied_sig = self.supplied_sig_of_closure(expr_def_id, decl);
494
495         debug!(
496             "check_supplied_sig_against_expectation: supplied_sig={:?}",
497             supplied_sig
498         );
499
500         // FIXME(#45727): As discussed in [this comment][c1], naively
501         // forcing equality here actually results in suboptimal error
502         // messages in some cases.  For now, if there would have been
503         // an obvious error, we fallback to declaring the type of the
504         // closure to be the one the user gave, which allows other
505         // error message code to trigger.
506         //
507         // However, I think [there is potential to do even better
508         // here][c2], since in *this* code we have the precise span of
509         // the type parameter in question in hand when we report the
510         // error.
511         //
512         // [c1]: https://github.com/rust-lang/rust/pull/45072#issuecomment-341089706
513         // [c2]: https://github.com/rust-lang/rust/pull/45072#issuecomment-341096796
514         self.infcx.commit_if_ok(|_| {
515             let mut all_obligations = vec![];
516
517             // The liberated version of this signature should be a subtype
518             // of the liberated form of the expectation.
519             for ((hir_ty, &supplied_ty), expected_ty) in decl.inputs.iter()
520                .zip(*supplied_sig.inputs().skip_binder()) // binder moved to (*) below
521                .zip(expected_sigs.liberated_sig.inputs())
522             // `liberated_sig` is E'.
523             {
524                 // Instantiate (this part of..) S to S', i.e., with fresh variables.
525                 let (supplied_ty, _) = self.infcx.replace_bound_vars_with_fresh_vars(
526                     hir_ty.span,
527                     LateBoundRegionConversionTime::FnCall,
528                     &ty::Binder::bind(supplied_ty),
529                 ); // recreated from (*) above
530
531                 // Check that E' = S'.
532                 let cause = &self.misc(hir_ty.span);
533                 let InferOk {
534                     value: (),
535                     obligations,
536                 } = self.at(cause, self.param_env)
537                     .eq(*expected_ty, supplied_ty)?;
538                 all_obligations.extend(obligations);
539
540                 // Also, require that the supplied type must outlive
541                 // the closure body.
542                 let closure_body_region = self.tcx.mk_region(
543                     ty::ReScope(
544                         region::Scope {
545                             id: body.value.hir_id.local_id,
546                             data: region::ScopeData::Node,
547                         },
548                     ),
549                 );
550                 all_obligations.push(
551                     Obligation::new(
552                         cause.clone(),
553                         self.param_env,
554                         ty::Predicate::TypeOutlives(
555                             ty::Binder::dummy(
556                                 ty::OutlivesPredicate(
557                                     supplied_ty,
558                                     closure_body_region,
559                                 ),
560                             ),
561                         ),
562                     ),
563                 );
564             }
565
566             let (supplied_output_ty, _) = self.infcx.replace_bound_vars_with_fresh_vars(
567                 decl.output.span(),
568                 LateBoundRegionConversionTime::FnCall,
569                 &supplied_sig.output(),
570             );
571             let cause = &self.misc(decl.output.span());
572             let InferOk {
573                 value: (),
574                 obligations,
575             } = self.at(cause, self.param_env)
576                 .eq(expected_sigs.liberated_sig.output(), supplied_output_ty)?;
577             all_obligations.extend(obligations);
578
579             Ok(InferOk {
580                 value: (),
581                 obligations: all_obligations,
582             })
583         })
584     }
585
586     /// If there is no expected signature, then we will convert the
587     /// types that the user gave into a signature.
588     ///
589     /// Also, record this closure signature for later.
590     fn supplied_sig_of_closure(
591         &self,
592         expr_def_id: DefId,
593         decl: &hir::FnDecl,
594     ) -> ty::PolyFnSig<'tcx> {
595         let astconv: &dyn AstConv<'_, '_> = self;
596
597         // First, convert the types that the user supplied (if any).
598         let supplied_arguments = decl.inputs.iter().map(|a| astconv.ast_ty_to_ty(a));
599         let supplied_return = match decl.output {
600             hir::Return(ref output) => astconv.ast_ty_to_ty(&output),
601             hir::DefaultReturn(_) => astconv.ty_infer(None, decl.output.span()),
602         };
603
604         let result = ty::Binder::bind(self.tcx.mk_fn_sig(
605             supplied_arguments,
606             supplied_return,
607             decl.c_variadic,
608             hir::Unsafety::Normal,
609             Abi::RustCall,
610         ));
611
612         debug!("supplied_sig_of_closure: result={:?}", result);
613
614         let c_result = self.inh.infcx.canonicalize_response(&result);
615         self.tables.borrow_mut().user_provided_sigs.insert(
616             expr_def_id,
617             c_result,
618         );
619
620         result
621     }
622
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: &dyn AstConv<'_, '_> = self;
628
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);
632             self.tcx.types.err
633         });
634
635         if let hir::Return(ref output) = decl.output {
636             astconv.ast_ty_to_ty(&output);
637         }
638
639         let result = ty::Binder::bind(self.tcx.mk_fn_sig(
640             supplied_arguments,
641             self.tcx.types.err,
642             decl.c_variadic,
643             hir::Unsafety::Normal,
644             Abi::RustCall,
645         ));
646
647         debug!("supplied_sig_of_closure: result={:?}", result);
648
649         result
650     }
651
652     fn closure_sigs(
653         &self,
654         expr_def_id: DefId,
655         body: &hir::Body,
656         bound_sig: ty::PolyFnSig<'tcx>,
657     ) -> ClosureSignatures<'tcx> {
658         let liberated_sig = self.tcx()
659             .liberate_late_bound_regions(expr_def_id, &bound_sig);
660         let liberated_sig = self.inh.normalize_associated_types_in(
661             body.value.span,
662             body.value.hir_id,
663             self.param_env,
664             &liberated_sig,
665         );
666         ClosureSignatures {
667             bound_sig,
668             liberated_sig,
669         }
670     }
671 }