Skip to content

Commit

Permalink
Rollup merge of rust-lang#78085 - wesleywiser:mir_validation_switch_i…
Browse files Browse the repository at this point in the history
…nt, r=oli-obk

MIR validation should check `SwitchInt` values are valid for the type

Fixes rust-lang#75440
  • Loading branch information
JohnTitor committed Oct 25, 2020
2 parents 569d29d + dd683e5 commit dbdc61f
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions compiler/rustc_mir/src/transform/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ use crate::dataflow::{Analysis, ResultsCursor};
use crate::util::storage::AlwaysLiveLocals;

use super::MirPass;
use rustc_middle::mir::visit::{PlaceContext, Visitor};
use rustc_middle::mir::{
interpret::Scalar,
visit::{PlaceContext, Visitor},
};
use rustc_middle::mir::{
AggregateKind, BasicBlock, Body, BorrowKind, Local, Location, MirPhase, Operand, Rvalue,
SourceScope, Statement, StatementKind, Terminator, TerminatorKind, VarDebugInfo,
};
use rustc_middle::ty::relate::{Relate, RelateResult, TypeRelation};
use rustc_middle::ty::{self, ParamEnv, Ty, TyCtxt};
use rustc_target::abi::Size;

#[derive(Copy, Clone, Debug)]
enum EdgeKind {
Expand Down Expand Up @@ -346,7 +350,25 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
),
);
}
for (_, target) in targets.iter() {

let target_width = self.tcx.sess.target.pointer_width;

let size = Size::from_bits(match switch_ty.kind() {
ty::Uint(uint) => uint.normalize(target_width).bit_width().unwrap(),
ty::Int(int) => int.normalize(target_width).bit_width().unwrap(),
ty::Char => 32,
ty::Bool => 1,
other => bug!("unhandled type: {:?}", other),
});

for (value, target) in targets.iter() {
if Scalar::<()>::try_from_uint(value, size).is_none() {
self.fail(
location,
format!("the value {:#x} is not a proper {:?}", value, switch_ty),
)
}

self.check_edge(location, target, EdgeKind::Normal);
}
self.check_edge(location, targets.otherwise(), EdgeKind::Normal);
Expand Down

0 comments on commit dbdc61f

Please sign in to comment.