Skip to content

Commit f2d7908

Browse files
committed
Adjust MIR validator to check a few more things for terminators
1 parent f1f25c0 commit f2d7908

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
@@ -642,6 +642,9 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
642642
}
643643
}
644644
TerminatorKind::Yield { resume, drop, .. } => {
645+
if self.body.generator.is_none() {
646+
self.fail(location, "`Yield` cannot appear outside generator bodies");
647+
}
645648
if self.mir_phase >= MirPhase::GeneratorsLowered {
646649
self.fail(location, "`Yield` should have been replaced by generator lowering");
647650
}
@@ -681,18 +684,29 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
681684
}
682685
}
683686
TerminatorKind::GeneratorDrop => {
687+
if self.body.generator.is_none() {
688+
self.fail(location, "`GeneratorDrop` cannot appear outside generator bodies");
689+
}
684690
if self.mir_phase >= MirPhase::GeneratorsLowered {
685691
self.fail(
686692
location,
687693
"`GeneratorDrop` should have been replaced by generator lowering",
688694
);
689695
}
690696
}
691-
// Nothing to validate for these.
692-
TerminatorKind::Resume
693-
| TerminatorKind::Abort
694-
| TerminatorKind::Return
695-
| TerminatorKind::Unreachable => {}
697+
TerminatorKind::Resume | TerminatorKind::Abort => {
698+
let bb = location.block;
699+
if !self.body.basic_blocks()[bb].is_cleanup {
700+
self.fail(location, "Cannot `Resume` from non-cleanup basic block")
701+
}
702+
}
703+
TerminatorKind::Return => {
704+
let bb = location.block;
705+
if self.body.basic_blocks()[bb].is_cleanup {
706+
self.fail(location, "Cannot `Return` from cleanup basic block")
707+
}
708+
}
709+
TerminatorKind::Unreachable => {}
696710
}
697711

698712
self.super_terminator(terminator, location);

0 commit comments

Comments
 (0)