@@ -7,16 +7,13 @@ use crate::rustc_middle::ty::subst::Subst;
7
7
use rustc_hir as hir;
8
8
use rustc_hir:: def_id:: DefId ;
9
9
use rustc_hir:: lang_items:: LangItem ;
10
- use rustc_infer:: infer:: opaque_types:: ReplaceOpaqueTypes ;
11
10
use rustc_infer:: infer:: type_variable:: { TypeVariableOrigin , TypeVariableOriginKind } ;
12
11
use rustc_infer:: infer:: LateBoundRegionConversionTime ;
13
12
use rustc_infer:: infer:: { InferOk , InferResult } ;
14
- use rustc_infer:: traits:: ObligationCauseCode ;
15
13
use rustc_middle:: ty:: fold:: TypeFoldable ;
16
14
use rustc_middle:: ty:: subst:: InternalSubsts ;
17
15
use rustc_middle:: ty:: { self , Ty } ;
18
16
use rustc_span:: source_map:: Span ;
19
- use rustc_span:: DUMMY_SP ;
20
17
use rustc_target:: spec:: abi:: Abi ;
21
18
use rustc_trait_selection:: traits:: error_reporting:: ArgKind ;
22
19
use rustc_trait_selection:: traits:: error_reporting:: InferCtxtExt as _;
@@ -430,14 +427,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
430
427
// in this binder we are creating.
431
428
assert ! ( !expected_sig. sig. skip_binder( ) . has_vars_bound_above( ty:: INNERMOST ) ) ;
432
429
let bound_sig = expected_sig. sig . map_bound ( |sig| {
433
- let output = self . hide_parent_opaque_types (
434
- sig. output ( ) ,
435
- expected_sig. cause_span . unwrap_or ( DUMMY_SP ) ,
436
- body. id ( ) . hir_id ,
437
- ) ;
438
430
self . tcx . mk_fn_sig (
439
431
sig. inputs ( ) . iter ( ) . cloned ( ) ,
440
- output,
432
+ sig . output ( ) ,
441
433
sig. c_variadic ,
442
434
hir:: Unsafety :: Normal ,
443
435
Abi :: RustCall ,
@@ -609,23 +601,22 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
609
601
// function.
610
602
Some ( hir:: GeneratorKind :: Async ( hir:: AsyncGeneratorKind :: Fn ) ) => {
611
603
debug ! ( "closure is async fn body" ) ;
612
- self . deduce_future_output_from_obligations ( expr_def_id) . unwrap_or_else ( || {
613
- // AFAIK, deducing the future output
614
- // always succeeds *except* in error cases
615
- // like #65159. I'd like to return Error
616
- // here, but I can't because I can't
617
- // easily (and locally) prove that we
618
- // *have* reported an
619
- // error. --nikomatsakis
620
- astconv. ty_infer ( None , decl. output . span ( ) )
621
- } )
604
+ self . deduce_future_output_from_obligations ( expr_def_id, body. id ( ) . hir_id )
605
+ . unwrap_or_else ( || {
606
+ // AFAIK, deducing the future output
607
+ // always succeeds *except* in error cases
608
+ // like #65159. I'd like to return Error
609
+ // here, but I can't because I can't
610
+ // easily (and locally) prove that we
611
+ // *have* reported an
612
+ // error. --nikomatsakis
613
+ astconv. ty_infer ( None , decl. output . span ( ) )
614
+ } )
622
615
}
623
616
624
617
_ => astconv. ty_infer ( None , decl. output . span ( ) ) ,
625
618
} ,
626
619
} ;
627
- let supplied_return =
628
- self . hide_parent_opaque_types ( supplied_return, decl. output . span ( ) , body. id ( ) . hir_id ) ;
629
620
630
621
let result = ty:: Binder :: bind_with_vars (
631
622
self . tcx . mk_fn_sig (
@@ -646,31 +637,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
646
637
result
647
638
}
648
639
649
- /// Closures can't create hidden types for opaque types of their parent, as they
650
- /// do not have all the outlives information available. Also `type_of` looks for
651
- /// hidden types in the owner (so the closure's parent), so it would not find these
652
- /// definitions.
653
- fn hide_parent_opaque_types ( & self , ty : Ty < ' tcx > , span : Span , body_id : hir:: HirId ) -> Ty < ' tcx > {
654
- let InferOk { value, obligations } = self . replace_opaque_types_with_inference_vars (
655
- ty,
656
- body_id,
657
- span,
658
- ObligationCauseCode :: MiscObligation ,
659
- self . param_env ,
660
- ReplaceOpaqueTypes :: OnlyForRPIT ,
661
- ) ;
662
- self . register_predicates ( obligations) ;
663
- value
664
- }
665
-
666
640
/// Invoked when we are translating the generator that results
667
641
/// from desugaring an `async fn`. Returns the "sugared" return
668
642
/// type of the `async fn` -- that is, the return type that the
669
643
/// user specified. The "desugared" return type is an `impl
670
644
/// Future<Output = T>`, so we do this by searching through the
671
645
/// obligations to extract the `T`.
672
646
#[ instrument( skip( self ) , level = "debug" ) ]
673
- fn deduce_future_output_from_obligations ( & self , expr_def_id : DefId ) -> Option < Ty < ' tcx > > {
647
+ fn deduce_future_output_from_obligations (
648
+ & self ,
649
+ expr_def_id : DefId ,
650
+ body_id : hir:: HirId ,
651
+ ) -> Option < Ty < ' tcx > > {
674
652
let ret_coercion = self . ret_coercion . as_ref ( ) . unwrap_or_else ( || {
675
653
span_bug ! ( self . tcx. def_span( expr_def_id) , "async fn generator outside of a fn" )
676
654
} ) ;
@@ -700,23 +678,34 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
700
678
ty:: Infer ( ty:: TyVar ( ret_vid) ) => {
701
679
self . obligations_for_self_ty ( ret_vid) . find_map ( |( _, obligation) | {
702
680
get_future_output ( obligation. predicate , obligation. cause . span )
703
- } )
681
+ } ) ?
704
682
}
705
683
ty:: Opaque ( def_id, substs) => self
706
684
. tcx
707
685
. bound_explicit_item_bounds ( def_id)
708
686
. transpose_iter ( )
709
687
. map ( |e| e. map_bound ( |e| * e) . transpose_tuple2 ( ) )
710
- . find_map ( |( p, s) | get_future_output ( p. subst ( self . tcx , substs) , s. 0 ) ) ,
688
+ . find_map ( |( p, s) | get_future_output ( p. subst ( self . tcx , substs) , s. 0 ) ) ? ,
711
689
ty:: Error ( _) => return None ,
712
690
_ => span_bug ! (
713
691
self . tcx. def_span( expr_def_id) ,
714
692
"async fn generator return type not an inference variable"
715
693
) ,
716
694
} ;
717
695
696
+ // async fn that have opaque types in their return type need to redo the conversion to inference variables
697
+ // as they fetch the still opaque version from the signature.
698
+ let InferOk { value : output_ty, obligations } = self
699
+ . replace_opaque_types_with_inference_vars (
700
+ output_ty,
701
+ body_id,
702
+ self . tcx . def_span ( expr_def_id) ,
703
+ self . param_env ,
704
+ ) ;
705
+ self . register_predicates ( obligations) ;
706
+
718
707
debug ! ( "deduce_future_output_from_obligations: output_ty={:?}" , output_ty) ;
719
- output_ty
708
+ Some ( output_ty)
720
709
}
721
710
722
711
/// Given a projection like
0 commit comments