@@ -65,6 +65,7 @@ impl<'tcx> MirPass<'tcx> for Validator {
6565 storage_liveness,
6666 place_cache : Vec :: new ( ) ,
6767 value_cache : Vec :: new ( ) ,
68+ is_generator : tcx. generator_kind ( def_id) . is_some ( ) ,
6869 }
6970 . visit_body ( body) ;
7071 }
@@ -118,6 +119,7 @@ struct TypeChecker<'a, 'tcx> {
118119 storage_liveness : ResultsCursor < ' a , ' tcx , MaybeStorageLive > ,
119120 place_cache : Vec < PlaceRef < ' tcx > > ,
120121 value_cache : Vec < u128 > ,
122+ is_generator : bool ,
121123}
122124
123125impl < ' a , ' tcx > TypeChecker < ' a , ' tcx > {
@@ -323,16 +325,8 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
323325 }
324326 & ty:: Generator ( def_id, substs, _) => {
325327 let f_ty = if let Some ( var) = parent_ty. variant_index {
326- let gen_body = if def_id == self . body . source . def_id ( ) {
327- self . body
328- } else {
329- self . tcx . optimized_mir ( def_id)
330- } ;
331-
332- let Some ( layout) = gen_body. generator_layout ( ) else {
333- self . fail ( location, format ! ( "No generator layout for {:?}" , parent_ty) ) ;
334- return ;
335- } ;
328+ let generator_info = self . tcx . mir_generator_info ( def_id) ;
329+ let layout = & generator_info. generator_layout ;
336330
337331 let Some ( & local) = layout. variant_fields [ var] . get ( f) else {
338332 fail_out_of_bounds ( self , location) ;
@@ -861,7 +855,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
861855 }
862856 }
863857 TerminatorKind :: Yield { resume, drop, .. } => {
864- if self . body . generator . is_none ( ) {
858+ if ! self . is_generator {
865859 self . fail ( location, "`Yield` cannot appear outside generator bodies" ) ;
866860 }
867861 if self . mir_phase >= MirPhase :: Runtime ( RuntimePhase :: Initial ) {
@@ -903,7 +897,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
903897 }
904898 }
905899 TerminatorKind :: GeneratorDrop => {
906- if self . body . generator . is_none ( ) {
900+ if ! self . is_generator {
907901 self . fail ( location, "`GeneratorDrop` cannot appear outside generator bodies" ) ;
908902 }
909903 if self . mir_phase >= MirPhase :: Runtime ( RuntimePhase :: Initial ) {
0 commit comments