@@ -627,6 +627,9 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
627
627
}
628
628
}
629
629
TerminatorKind :: Yield { resume, drop, .. } => {
630
+ if self . body . generator . is_none ( ) {
631
+ self . fail ( location, "`Yield` cannot appear outside generator bodies" ) ;
632
+ }
630
633
if self . mir_phase >= MirPhase :: GeneratorsLowered {
631
634
self . fail ( location, "`Yield` should have been replaced by generator lowering" ) ;
632
635
}
@@ -666,18 +669,29 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
666
669
}
667
670
}
668
671
TerminatorKind :: GeneratorDrop => {
672
+ if self . body . generator . is_none ( ) {
673
+ self . fail ( location, "`GeneratorDrop` cannot appear outside generator bodies" ) ;
674
+ }
669
675
if self . mir_phase >= MirPhase :: GeneratorsLowered {
670
676
self . fail (
671
677
location,
672
678
"`GeneratorDrop` should have been replaced by generator lowering" ,
673
679
) ;
674
680
}
675
681
}
676
- // Nothing to validate for these.
677
- TerminatorKind :: Resume
678
- | TerminatorKind :: Abort
679
- | TerminatorKind :: Return
680
- | TerminatorKind :: Unreachable => { }
682
+ TerminatorKind :: Resume | TerminatorKind :: Abort => {
683
+ let bb = location. block ;
684
+ if !self . body . basic_blocks ( ) [ bb] . is_cleanup {
685
+ self . fail ( location, "Cannot `Resume` from non-cleanup basic block" )
686
+ }
687
+ }
688
+ TerminatorKind :: Return => {
689
+ let bb = location. block ;
690
+ if self . body . basic_blocks ( ) [ bb] . is_cleanup {
691
+ self . fail ( location, "Cannot `Return` from cleanup basic block" )
692
+ }
693
+ }
694
+ TerminatorKind :: Unreachable => { }
681
695
}
682
696
683
697
self . super_terminator ( terminator, location) ;
0 commit comments