@@ -60,25 +60,26 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
60
60
}
61
61
None => ( None , None ) ,
62
62
} ;
63
- let body = self . tcx . hir ( ) . body ( closure . body ) ;
64
- self . check_closure ( closure, expr_span, expected_kind, body , expected_sig)
63
+
64
+ self . check_closure ( closure, expr_span, expected_kind, expected_sig)
65
65
}
66
66
67
- #[ instrument( skip( self , closure, body ) , level = "debug" , ret) ]
67
+ #[ instrument( skip( self , closure) , level = "debug" , ret) ]
68
68
fn check_closure (
69
69
& self ,
70
70
closure : & hir:: Closure < ' tcx > ,
71
71
expr_span : Span ,
72
72
opt_kind : Option < ty:: ClosureKind > ,
73
- body : & ' tcx hir:: Body < ' tcx > ,
74
73
expected_sig : Option < ExpectedSig < ' tcx > > ,
75
74
) -> Ty < ' tcx > {
75
+ let body = self . tcx . hir ( ) . body ( closure. body ) ;
76
+
76
77
trace ! ( "decl = {:#?}" , closure. fn_decl) ;
77
78
let expr_def_id = closure. def_id ;
78
79
debug ! ( ?expr_def_id) ;
79
80
80
81
let ClosureSignatures { bound_sig, liberated_sig } =
81
- self . sig_of_closure ( expr_def_id, closure. fn_decl , body, expected_sig) ;
82
+ self . sig_of_closure ( expr_def_id, closure. fn_decl , body. coroutine_kind , expected_sig) ;
82
83
83
84
debug ! ( ?bound_sig, ?liberated_sig) ;
84
85
@@ -351,28 +352,28 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
351
352
& self ,
352
353
expr_def_id : LocalDefId ,
353
354
decl : & hir:: FnDecl < ' _ > ,
354
- body : & hir:: Body < ' _ > ,
355
+ coroutine_kind : Option < hir:: CoroutineKind > ,
355
356
expected_sig : Option < ExpectedSig < ' tcx > > ,
356
357
) -> ClosureSignatures < ' tcx > {
357
358
if let Some ( e) = expected_sig {
358
- self . sig_of_closure_with_expectation ( expr_def_id, decl, body , e)
359
+ self . sig_of_closure_with_expectation ( expr_def_id, decl, coroutine_kind , e)
359
360
} else {
360
- self . sig_of_closure_no_expectation ( expr_def_id, decl, body )
361
+ self . sig_of_closure_no_expectation ( expr_def_id, decl, coroutine_kind )
361
362
}
362
363
}
363
364
364
365
/// If there is no expected signature, then we will convert the
365
366
/// types that the user gave into a signature.
366
- #[ instrument( skip( self , expr_def_id, decl, body ) , level = "debug" ) ]
367
+ #[ instrument( skip( self , expr_def_id, decl) , level = "debug" ) ]
367
368
fn sig_of_closure_no_expectation (
368
369
& self ,
369
370
expr_def_id : LocalDefId ,
370
371
decl : & hir:: FnDecl < ' _ > ,
371
- body : & hir:: Body < ' _ > ,
372
+ coroutine_kind : Option < hir:: CoroutineKind > ,
372
373
) -> ClosureSignatures < ' tcx > {
373
- let bound_sig = self . supplied_sig_of_closure ( expr_def_id, decl, body ) ;
374
+ let bound_sig = self . supplied_sig_of_closure ( expr_def_id, decl, coroutine_kind ) ;
374
375
375
- self . closure_sigs ( expr_def_id, body , bound_sig)
376
+ self . closure_sigs ( expr_def_id, bound_sig)
376
377
}
377
378
378
379
/// Invoked to compute the signature of a closure expression. This
@@ -422,24 +423,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
422
423
/// - `expected_sig`: the expected signature (if any). Note that
423
424
/// this is missing a binder: that is, there may be late-bound
424
425
/// regions with depth 1, which are bound then by the closure.
425
- #[ instrument( skip( self , expr_def_id, decl, body ) , level = "debug" ) ]
426
+ #[ instrument( skip( self , expr_def_id, decl) , level = "debug" ) ]
426
427
fn sig_of_closure_with_expectation (
427
428
& self ,
428
429
expr_def_id : LocalDefId ,
429
430
decl : & hir:: FnDecl < ' _ > ,
430
- body : & hir:: Body < ' _ > ,
431
+ coroutine_kind : Option < hir:: CoroutineKind > ,
431
432
expected_sig : ExpectedSig < ' tcx > ,
432
433
) -> ClosureSignatures < ' tcx > {
433
434
// Watch out for some surprises and just ignore the
434
435
// expectation if things don't see to match up with what we
435
436
// expect.
436
437
if expected_sig. sig . c_variadic ( ) != decl. c_variadic {
437
- return self . sig_of_closure_no_expectation ( expr_def_id, decl, body ) ;
438
+ return self . sig_of_closure_no_expectation ( expr_def_id, decl, coroutine_kind ) ;
438
439
} else if expected_sig. sig . skip_binder ( ) . inputs_and_output . len ( ) != decl. inputs . len ( ) + 1 {
439
440
return self . sig_of_closure_with_mismatched_number_of_arguments (
440
441
expr_def_id,
441
442
decl,
442
- body,
443
443
expected_sig,
444
444
) ;
445
445
}
@@ -463,24 +463,28 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
463
463
// anonymize away, so as not to confuse the user.
464
464
let bound_sig = self . tcx . anonymize_bound_vars ( bound_sig) ;
465
465
466
- let closure_sigs = self . closure_sigs ( expr_def_id, body , bound_sig) ;
466
+ let closure_sigs = self . closure_sigs ( expr_def_id, bound_sig) ;
467
467
468
468
// Up till this point, we have ignored the annotations that the user
469
469
// gave. This function will check that they unify successfully.
470
470
// Along the way, it also writes out entries for types that the user
471
471
// wrote into our typeck results, which are then later used by the privacy
472
472
// check.
473
- match self . merge_supplied_sig_with_expectation ( expr_def_id, decl, body, closure_sigs) {
473
+ match self . merge_supplied_sig_with_expectation (
474
+ expr_def_id,
475
+ decl,
476
+ coroutine_kind,
477
+ closure_sigs,
478
+ ) {
474
479
Ok ( infer_ok) => self . register_infer_ok_obligations ( infer_ok) ,
475
- Err ( _) => self . sig_of_closure_no_expectation ( expr_def_id, decl, body ) ,
480
+ Err ( _) => self . sig_of_closure_no_expectation ( expr_def_id, decl, coroutine_kind ) ,
476
481
}
477
482
}
478
483
479
484
fn sig_of_closure_with_mismatched_number_of_arguments (
480
485
& self ,
481
486
expr_def_id : LocalDefId ,
482
487
decl : & hir:: FnDecl < ' _ > ,
483
- body : & hir:: Body < ' _ > ,
484
488
expected_sig : ExpectedSig < ' tcx > ,
485
489
) -> ClosureSignatures < ' tcx > {
486
490
let expr_map_node = self . tcx . hir_node_by_def_id ( expr_def_id) ;
@@ -511,25 +515,25 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
511
515
512
516
let error_sig = self . error_sig_of_closure ( decl, guar) ;
513
517
514
- self . closure_sigs ( expr_def_id, body , error_sig)
518
+ self . closure_sigs ( expr_def_id, error_sig)
515
519
}
516
520
517
521
/// Enforce the user's types against the expectation. See
518
522
/// `sig_of_closure_with_expectation` for details on the overall
519
523
/// strategy.
520
- #[ instrument( level = "debug" , skip( self , expr_def_id, decl, body , expected_sigs) ) ]
524
+ #[ instrument( level = "debug" , skip( self , expr_def_id, decl, expected_sigs) ) ]
521
525
fn merge_supplied_sig_with_expectation (
522
526
& self ,
523
527
expr_def_id : LocalDefId ,
524
528
decl : & hir:: FnDecl < ' _ > ,
525
- body : & hir:: Body < ' _ > ,
529
+ coroutine_kind : Option < hir:: CoroutineKind > ,
526
530
mut expected_sigs : ClosureSignatures < ' tcx > ,
527
531
) -> InferResult < ' tcx , ClosureSignatures < ' tcx > > {
528
532
// Get the signature S that the user gave.
529
533
//
530
534
// (See comment on `sig_of_closure_with_expectation` for the
531
535
// meaning of these letters.)
532
- let supplied_sig = self . supplied_sig_of_closure ( expr_def_id, decl, body ) ;
536
+ let supplied_sig = self . supplied_sig_of_closure ( expr_def_id, decl, coroutine_kind ) ;
533
537
534
538
debug ! ( ?supplied_sig) ;
535
539
@@ -611,17 +615,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
611
615
/// types that the user gave into a signature.
612
616
///
613
617
/// Also, record this closure signature for later.
614
- #[ instrument( skip( self , decl, body ) , level = "debug" , ret) ]
618
+ #[ instrument( skip( self , decl) , level = "debug" , ret) ]
615
619
fn supplied_sig_of_closure (
616
620
& self ,
617
621
expr_def_id : LocalDefId ,
618
622
decl : & hir:: FnDecl < ' _ > ,
619
- body : & hir:: Body < ' _ > ,
623
+ coroutine_kind : Option < hir:: CoroutineKind > ,
620
624
) -> ty:: PolyFnSig < ' tcx > {
621
625
let astconv: & dyn AstConv < ' _ > = self ;
622
626
623
627
trace ! ( "decl = {:#?}" , decl) ;
624
- debug ! ( ?body . coroutine_kind) ;
628
+ debug ! ( ?coroutine_kind) ;
625
629
626
630
let hir_id = self . tcx . local_def_id_to_hir_id ( expr_def_id) ;
627
631
let bound_vars = self . tcx . late_bound_vars ( hir_id) ;
@@ -630,7 +634,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
630
634
let supplied_arguments = decl. inputs . iter ( ) . map ( |a| astconv. ast_ty_to_ty ( a) ) ;
631
635
let supplied_return = match decl. output {
632
636
hir:: FnRetTy :: Return ( ref output) => astconv. ast_ty_to_ty ( output) ,
633
- hir:: FnRetTy :: DefaultReturn ( _) => match body . coroutine_kind {
637
+ hir:: FnRetTy :: DefaultReturn ( _) => match coroutine_kind {
634
638
// In the case of the async block that we create for a function body,
635
639
// we expect the return type of the block to match that of the enclosing
636
640
// function.
@@ -639,19 +643,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
639
643
hir:: CoroutineSource :: Fn ,
640
644
) ) => {
641
645
debug ! ( "closure is async fn body" ) ;
642
- let def_id = self . tcx . hir ( ) . body_owner_def_id ( body. id ( ) ) ;
643
- self . deduce_future_output_from_obligations ( expr_def_id, def_id) . unwrap_or_else (
644
- || {
645
- // AFAIK, deducing the future output
646
- // always succeeds *except* in error cases
647
- // like #65159. I'd like to return Error
648
- // here, but I can't because I can't
649
- // easily (and locally) prove that we
650
- // *have* reported an
651
- // error. --nikomatsakis
652
- astconv. ty_infer ( None , decl. output . span ( ) )
653
- } ,
654
- )
646
+ self . deduce_future_output_from_obligations ( expr_def_id) . unwrap_or_else ( || {
647
+ // AFAIK, deducing the future output
648
+ // always succeeds *except* in error cases
649
+ // like #65159. I'd like to return Error
650
+ // here, but I can't because I can't
651
+ // easily (and locally) prove that we
652
+ // *have* reported an
653
+ // error. --nikomatsakis
654
+ astconv. ty_infer ( None , decl. output . span ( ) )
655
+ } )
655
656
}
656
657
// All `gen {}` and `async gen {}` must return unit.
657
658
Some (
@@ -688,16 +689,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
688
689
/// Future<Output = T>`, so we do this by searching through the
689
690
/// obligations to extract the `T`.
690
691
#[ instrument( skip( self ) , level = "debug" , ret) ]
691
- fn deduce_future_output_from_obligations (
692
- & self ,
693
- expr_def_id : LocalDefId ,
694
- body_def_id : LocalDefId ,
695
- ) -> Option < Ty < ' tcx > > {
692
+ fn deduce_future_output_from_obligations ( & self , body_def_id : LocalDefId ) -> Option < Ty < ' tcx > > {
696
693
let ret_coercion = self . ret_coercion . as_ref ( ) . unwrap_or_else ( || {
697
- span_bug ! ( self . tcx. def_span( expr_def_id ) , "async fn coroutine outside of a fn" )
694
+ span_bug ! ( self . tcx. def_span( body_def_id ) , "async fn coroutine outside of a fn" )
698
695
} ) ;
699
696
700
- let closure_span = self . tcx . def_span ( expr_def_id ) ;
697
+ let closure_span = self . tcx . def_span ( body_def_id ) ;
701
698
let ret_ty = ret_coercion. borrow ( ) . expected_ty ( ) ;
702
699
let ret_ty = self . try_structurally_resolve_type ( closure_span, ret_ty) ;
703
700
@@ -842,12 +839,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
842
839
fn closure_sigs (
843
840
& self ,
844
841
expr_def_id : LocalDefId ,
845
- body : & hir:: Body < ' _ > ,
846
842
bound_sig : ty:: PolyFnSig < ' tcx > ,
847
843
) -> ClosureSignatures < ' tcx > {
848
844
let liberated_sig =
849
845
self . tcx ( ) . liberate_late_bound_regions ( expr_def_id. to_def_id ( ) , bound_sig) ;
850
- let liberated_sig = self . normalize ( body . value . span , liberated_sig) ;
846
+ let liberated_sig = self . normalize ( self . tcx . def_span ( expr_def_id ) , liberated_sig) ;
851
847
ClosureSignatures { bound_sig, liberated_sig }
852
848
}
853
849
}
0 commit comments