Skip to content

Commit 490d820

Browse files
authored
Rollup merge of #73586 - RalfJung:switch-ty, r=oli-obk
switch_ty is redundant This field is redundant, but we cannot remove it currently as pretty-printing relies on it (and it does not have access to `mir::Body` to compute the type itself). Cc @oli-obk @matthewjasper @jonas-schievink
2 parents 963a480 + 7447bf2 commit 490d820

File tree

4 files changed

+18
-10
lines changed

4 files changed

+18
-10
lines changed

src/librustc_codegen_ssa/mir/block.rs

+2
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
200200
targets: &Vec<mir::BasicBlock>,
201201
) {
202202
let discr = self.codegen_operand(&mut bx, &discr);
203+
// `switch_ty` is redundant, sanity-check that.
204+
assert_eq!(discr.layout.ty, switch_ty);
203205
if targets.len() == 2 {
204206
// If there are two targets, emit br instead of switch
205207
let lltrue = helper.llblock(self, targets[0]);

src/librustc_middle/mir/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1075,6 +1075,8 @@ pub enum TerminatorKind<'tcx> {
10751075
discr: Operand<'tcx>,
10761076

10771077
/// The type of value being tested.
1078+
/// This is always the same as the type of `discr`.
1079+
/// FIXME: remove this redundant information. Currently, it is relied on by pretty-printing.
10781080
switch_ty: Ty<'tcx>,
10791081

10801082
/// Possible values. The locations to branch to in each case

src/librustc_mir/interpret/terminator.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
2424

2525
Goto { target } => self.go_to_block(target),
2626

27-
SwitchInt { ref discr, ref values, ref targets, .. } => {
27+
SwitchInt { ref discr, ref values, ref targets, switch_ty } => {
2828
let discr = self.read_immediate(self.eval_operand(discr, None)?)?;
2929
trace!("SwitchInt({:?})", *discr);
30+
assert_eq!(discr.layout.ty, switch_ty);
3031

3132
// Branch to the `otherwise` case by default, if no match is found.
3233
assert!(!targets.is_empty());
@@ -50,14 +51,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
5051
self.go_to_block(target_block);
5152
}
5253

53-
Call {
54-
ref func,
55-
ref args,
56-
destination,
57-
ref cleanup,
58-
from_hir_call: _from_hir_call,
59-
fn_span: _,
60-
} => {
54+
Call { ref func, ref args, destination, ref cleanup, from_hir_call: _, fn_span: _ } => {
6155
let old_stack = self.frame_idx();
6256
let old_loc = self.frame().loc;
6357
let func = self.eval_operand(func, None)?;

src/librustc_mir/transform/validate.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,17 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
121121
TerminatorKind::Goto { target } => {
122122
self.check_edge(location, *target, EdgeKind::Normal);
123123
}
124-
TerminatorKind::SwitchInt { targets, values, .. } => {
124+
TerminatorKind::SwitchInt { targets, values, switch_ty, discr } => {
125+
let ty = discr.ty(&self.body.local_decls, self.tcx);
126+
if ty != *switch_ty {
127+
self.fail(
128+
location,
129+
format!(
130+
"encountered `SwitchInt` terminator with type mismatch: {:?} != {:?}",
131+
ty, switch_ty,
132+
),
133+
);
134+
}
125135
if targets.len() != values.len() + 1 {
126136
self.fail(
127137
location,

0 commit comments

Comments
 (0)