@@ -34,6 +34,7 @@ crate enum RegionNameSource {
3434 MatchedAdtAndSegment ( Span ) ,
3535 AnonRegionFromUpvar ( Span , String ) ,
3636 AnonRegionFromOutput ( Span , String , String ) ,
37+ AnonRegionFromYieldTy ( Span , String ) ,
3738}
3839
3940impl RegionName {
@@ -48,7 +49,8 @@ impl RegionName {
4849 RegionNameSource :: MatchedHirTy ( ..) |
4950 RegionNameSource :: MatchedAdtAndSegment ( ..) |
5051 RegionNameSource :: AnonRegionFromUpvar ( ..) |
51- RegionNameSource :: AnonRegionFromOutput ( ..) => false ,
52+ RegionNameSource :: AnonRegionFromOutput ( ..) |
53+ RegionNameSource :: AnonRegionFromYieldTy ( ..) => false ,
5254 }
5355 }
5456
@@ -105,6 +107,12 @@ impl RegionName {
105107 format ! ( "return type{} is {}" , mir_description, type_name) ,
106108 ) ;
107109 } ,
110+ RegionNameSource :: AnonRegionFromYieldTy ( span, type_name) => {
111+ diag. span_label (
112+ * span,
113+ format ! ( "yield type is {}" , type_name) ,
114+ ) ;
115+ }
108116 RegionNameSource :: Static => { } ,
109117 }
110118 }
@@ -170,6 +178,11 @@ impl<'tcx> RegionInferenceContext<'tcx> {
170178 self . give_name_if_anonymous_region_appears_in_output (
171179 infcx, mir, mir_def_id, fr, counter,
172180 )
181+ } )
182+ . or_else ( || {
183+ self . give_name_if_anonymous_region_appears_in_yield_ty (
184+ infcx, mir, mir_def_id, fr, counter,
185+ )
173186 } ) ;
174187
175188 debug ! ( "give_region_a_name: gave name {:?}" , value) ;
@@ -676,10 +689,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
676689 "give_name_if_anonymous_region_appears_in_output: return_ty = {:?}" ,
677690 return_ty
678691 ) ;
679- if !infcx
680- . tcx
681- . any_free_region_meets ( & return_ty, |r| r. to_region_vid ( ) == fr)
682- {
692+ if !tcx. any_free_region_meets ( & return_ty, |r| r. to_region_vid ( ) == fr) {
683693 return None ;
684694 }
685695
@@ -724,6 +734,57 @@ impl<'tcx> RegionInferenceContext<'tcx> {
724734 } )
725735 }
726736
737+ fn give_name_if_anonymous_region_appears_in_yield_ty (
738+ & self ,
739+ infcx : & InferCtxt < ' _ , ' _ , ' tcx > ,
740+ mir : & Mir < ' tcx > ,
741+ mir_def_id : DefId ,
742+ fr : RegionVid ,
743+ counter : & mut usize ,
744+ ) -> Option < RegionName > {
745+ // Note: generators from `async fn` yield `()`, so we don't have to
746+ // worry about them here.
747+ let yield_ty = self . universal_regions . yield_ty ?;
748+ debug ! (
749+ "give_name_if_anonymous_region_appears_in_yield_ty: yield_ty = {:?}" ,
750+ yield_ty,
751+ ) ;
752+
753+ let tcx = infcx. tcx ;
754+
755+ if !tcx. any_free_region_meets ( & yield_ty, |r| r. to_region_vid ( ) == fr) {
756+ return None ;
757+ }
758+
759+ let mut highlight = RegionHighlightMode :: default ( ) ;
760+ highlight. highlighting_region_vid ( fr, * counter) ;
761+ let type_name = infcx. extract_type_name ( & yield_ty, Some ( highlight) ) ;
762+
763+ let mir_node_id = tcx. hir ( ) . as_local_node_id ( mir_def_id) . expect ( "non-local mir" ) ;
764+
765+ let yield_span = match tcx. hir ( ) . get ( mir_node_id) {
766+ hir:: Node :: Expr ( hir:: Expr {
767+ node : hir:: ExprKind :: Closure ( _, _, _, span, _) ,
768+ ..
769+ } ) => (
770+ tcx. sess . source_map ( ) . end_point ( * span)
771+ ) ,
772+ _ => mir. span ,
773+ } ;
774+
775+ debug ! (
776+ "give_name_if_anonymous_region_appears_in_yield_ty: \
777+ type_name = {:?}, yield_span = {:?}",
778+ yield_span,
779+ type_name,
780+ ) ;
781+
782+ Some ( RegionName {
783+ name : self . synthesize_region_name ( counter) ,
784+ source : RegionNameSource :: AnonRegionFromYieldTy ( yield_span, type_name) ,
785+ } )
786+ }
787+
727788 /// Creates a synthetic region named `'1`, incrementing the
728789 /// counter.
729790 fn synthesize_region_name ( & self , counter : & mut usize ) -> InternedString {
0 commit comments