Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

switch_ty is redundant #73586

Merged
merged 3 commits into from
Jun 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/librustc_codegen_ssa/mir/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
targets: &Vec<mir::BasicBlock>,
) {
let discr = self.codegen_operand(&mut bx, &discr);
// `switch_ty` is redundant, sanity-check that.
assert_eq!(discr.layout.ty, switch_ty);
if targets.len() == 2 {
// If there are two targets, emit br instead of switch
let lltrue = helper.llblock(self, targets[0]);
Expand Down
2 changes: 2 additions & 0 deletions src/librustc_middle/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1075,6 +1075,8 @@ pub enum TerminatorKind<'tcx> {
discr: Operand<'tcx>,

/// The type of value being tested.
/// This is always the same as the type of `discr`.
/// FIXME: remove this redundant information. Currently, it is relied on by pretty-printing.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to be more aggressive about it you can #[deprecate] it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what that would help with?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would nudge people away from new usages of this field.

switch_ty: Ty<'tcx>,

/// Possible values. The locations to branch to in each case
Expand Down
12 changes: 3 additions & 9 deletions src/librustc_mir/interpret/terminator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {

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

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

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

Call {
ref func,
ref args,
destination,
ref cleanup,
from_hir_call: _from_hir_call,
fn_span: _,
} => {
Call { ref func, ref args, destination, ref cleanup, from_hir_call: _, fn_span: _ } => {
let old_stack = self.frame_idx();
let old_loc = self.frame().loc;
let func = self.eval_operand(func, None)?;
Expand Down
12 changes: 11 additions & 1 deletion src/librustc_mir/transform/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,17 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
TerminatorKind::Goto { target } => {
self.check_edge(location, *target, EdgeKind::Normal);
}
TerminatorKind::SwitchInt { targets, values, .. } => {
TerminatorKind::SwitchInt { targets, values, switch_ty, discr } => {
let ty = discr.ty(&self.body.local_decls, self.tcx);
if ty != *switch_ty {
self.fail(
location,
format!(
"encountered `SwitchInt` terminator with type mismatch: {:?} != {:?}",
ty, switch_ty,
),
);
}
if targets.len() != values.len() + 1 {
self.fail(
location,
Expand Down