@@ -65,6 +65,7 @@ impl<'tcx> MirPass<'tcx> for Validator {
65
65
storage_liveness,
66
66
place_cache : Vec :: new ( ) ,
67
67
value_cache : Vec :: new ( ) ,
68
+ is_generator : tcx. generator_kind ( def_id) . is_some ( ) ,
68
69
}
69
70
. visit_body ( body) ;
70
71
}
@@ -118,6 +119,7 @@ struct TypeChecker<'a, 'tcx> {
118
119
storage_liveness : ResultsCursor < ' a , ' tcx , MaybeStorageLive > ,
119
120
place_cache : Vec < PlaceRef < ' tcx > > ,
120
121
value_cache : Vec < u128 > ,
122
+ is_generator : bool ,
121
123
}
122
124
123
125
impl < ' a , ' tcx > TypeChecker < ' a , ' tcx > {
@@ -323,16 +325,8 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
323
325
}
324
326
& ty:: Generator ( def_id, substs, _) => {
325
327
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 ;
336
330
337
331
let Some ( & local) = layout. variant_fields [ var] . get ( f) else {
338
332
fail_out_of_bounds ( self , location) ;
@@ -861,7 +855,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
861
855
}
862
856
}
863
857
TerminatorKind :: Yield { resume, drop, .. } => {
864
- if self . body . generator . is_none ( ) {
858
+ if ! self . is_generator {
865
859
self . fail ( location, "`Yield` cannot appear outside generator bodies" ) ;
866
860
}
867
861
if self . mir_phase >= MirPhase :: Runtime ( RuntimePhase :: Initial ) {
@@ -903,7 +897,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
903
897
}
904
898
}
905
899
TerminatorKind :: GeneratorDrop => {
906
- if self . body . generator . is_none ( ) {
900
+ if ! self . is_generator {
907
901
self . fail ( location, "`GeneratorDrop` cannot appear outside generator bodies" ) ;
908
902
}
909
903
if self . mir_phase >= MirPhase :: Runtime ( RuntimePhase :: Initial ) {
0 commit comments