Rollup merge of #93546 - tmiasko:validate-switch-int, r=oli-obk

Validate that values in switch int terminator are unique
This commit is contained in:
Matthias Krüger 2022-02-02 19:34:04 +01:00 committed by GitHub
commit b53eaf7ce5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -55,6 +55,7 @@ impl<'tcx> MirPass<'tcx> for Validator {
reachable_blocks: traversal::reachable_as_bitset(body),
storage_liveness,
place_cache: Vec::new(),
value_cache: Vec::new(),
}
.visit_body(body);
}
@ -109,6 +110,7 @@ struct TypeChecker<'a, 'tcx> {
reachable_blocks: BitSet<BasicBlock>,
storage_liveness: ResultsCursor<'a, 'tcx, MaybeStorageLive>,
place_cache: Vec<PlaceRef<'tcx>>,
value_cache: Vec<u128>,
}
impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
@ -398,6 +400,22 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
self.check_edge(location, target, EdgeKind::Normal);
}
self.check_edge(location, targets.otherwise(), EdgeKind::Normal);
self.value_cache.clear();
self.value_cache.extend(targets.iter().map(|(value, _)| value));
let all_len = self.value_cache.len();
self.value_cache.sort_unstable();
self.value_cache.dedup();
let has_duplicates = all_len != self.value_cache.len();
if has_duplicates {
self.fail(
location,
format!(
"duplicated values in `SwitchInt` terminator: {:?}",
terminator.kind,
),
);
}
}
TerminatorKind::Drop { target, unwind, .. } => {
self.check_edge(location, *target, EdgeKind::Normal);