Skip to content

Commit 073b0f9

Browse files
committed
Auto merge of #30337 - wesleywiser:mir_switch, r=nikomatsakis
Fixes #29574
2 parents d4ffaf6 + 2b15361 commit 073b0f9

File tree

2 files changed

+71
-2
lines changed

2 files changed

+71
-2
lines changed

src/librustc_trans/trans/mir/block.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use llvm::BasicBlockRef;
1212
use rustc::mir::repr as mir;
13+
use trans::adt;
1314
use trans::base;
1415
use trans::build;
1516
use trans::common::Block;
@@ -46,8 +47,28 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
4647
build::CondBr(bcx, cond.immediate(), lltrue, llfalse, DebugLoc::None);
4748
}
4849

49-
mir::Terminator::Switch { .. } => {
50-
unimplemented!()
50+
mir::Terminator::Switch { ref discr, ref adt_def, ref targets } => {
51+
let adt_ty = bcx.tcx().lookup_item_type(adt_def.did).ty;
52+
let represented_ty = adt::represent_type(bcx.ccx(), adt_ty);
53+
54+
let discr_lvalue = self.trans_lvalue(bcx, discr);
55+
let discr = adt::trans_get_discr(bcx, &represented_ty, discr_lvalue.llval, None);
56+
57+
// The else branch of the Switch can't be hit, so branch to an unreachable
58+
// instruction so LLVM knows that
59+
// FIXME it might be nice to have just one such block (created lazilly), we could
60+
// store it in the "MIR trans" state.
61+
let unreachable_blk = bcx.fcx.new_temp_block("enum-variant-unreachable");
62+
build::Unreachable(unreachable_blk);
63+
64+
let switch = build::Switch(bcx, discr, unreachable_blk.llbb, targets.len());
65+
assert_eq!(adt_def.variants.len(), targets.len());
66+
for (adt_variant, target) in adt_def.variants.iter().zip(targets) {
67+
let llval = adt::trans_case(bcx, &*represented_ty, adt_variant.disr_val);
68+
let llbb = self.llblock(*target);
69+
70+
build::AddCase(switch, llval, llbb)
71+
}
5172
}
5273

5374
mir::Terminator::SwitchInt { ref discr, switch_ty, ref values, ref targets } => {

src/test/run-pass/mir_trans_switch.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(rustc_attrs)]
12+
13+
enum Abc {
14+
A(u8),
15+
B(i8),
16+
C,
17+
D,
18+
}
19+
20+
#[rustc_mir]
21+
fn foo(x: Abc) -> i32 {
22+
match x {
23+
Abc::C => 3,
24+
Abc::D => 4,
25+
Abc::B(_) => 2,
26+
Abc::A(_) => 1,
27+
}
28+
}
29+
30+
#[rustc_mir]
31+
fn foo2(x: Abc) -> bool {
32+
match x {
33+
Abc::D => true,
34+
_ => false
35+
}
36+
}
37+
38+
fn main() {
39+
assert_eq!(1, foo(Abc::A(42)));
40+
assert_eq!(2, foo(Abc::B(-100)));
41+
assert_eq!(3, foo(Abc::C));
42+
assert_eq!(4, foo(Abc::D));
43+
44+
assert_eq!(false, foo2(Abc::A(1)));
45+
assert_eq!(false, foo2(Abc::B(2)));
46+
assert_eq!(false, foo2(Abc::C));
47+
assert_eq!(true, foo2(Abc::D));
48+
}

0 commit comments

Comments
 (0)