Skip to content

Commit 3c169f3

Browse files
committed
Adjust MIR validator to check a few more things for terminators
1 parent c996bc0 commit 3c169f3

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

compiler/rustc_const_eval/src/transform/validate.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,9 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
627627
}
628628
}
629629
TerminatorKind::Yield { resume, drop, .. } => {
630+
if self.body.generator.is_none() {
631+
self.fail(location, "`Yield` cannot appear outside generator bodies");
632+
}
630633
if self.mir_phase >= MirPhase::GeneratorsLowered {
631634
self.fail(location, "`Yield` should have been replaced by generator lowering");
632635
}
@@ -666,18 +669,29 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
666669
}
667670
}
668671
TerminatorKind::GeneratorDrop => {
672+
if self.body.generator.is_none() {
673+
self.fail(location, "`GeneratorDrop` cannot appear outside generator bodies");
674+
}
669675
if self.mir_phase >= MirPhase::GeneratorsLowered {
670676
self.fail(
671677
location,
672678
"`GeneratorDrop` should have been replaced by generator lowering",
673679
);
674680
}
675681
}
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 => {}
681695
}
682696

683697
self.super_terminator(terminator, location);

0 commit comments

Comments
 (0)