diff options
| author | Simonas Kazlauskas <git@kazlauskas.me> | 2017-01-31 05:32:08 +0200 |
|---|---|---|
| committer | Simonas Kazlauskas <git@kazlauskas.me> | 2017-02-10 19:31:37 +0200 |
| commit | 98d1db7fe36fe5454937f40d9fcb4fe97f36bbeb (patch) | |
| tree | 5719c195fd5b4ae6c3869860f356b0b99c45353b /src/librustc_mir | |
| parent | 779c6b6cb8f5db7d862723701b0d4dc01c7712c3 (diff) | |
| download | rust-98d1db7fe36fe5454937f40d9fcb4fe97f36bbeb.tar.gz rust-98d1db7fe36fe5454937f40d9fcb4fe97f36bbeb.zip | |
If is now always a SwitchInt in MIR
Diffstat (limited to 'src/librustc_mir')
| -rw-r--r-- | src/librustc_mir/build/expr/into.rs | 37 | ||||
| -rw-r--r-- | src/librustc_mir/build/matches/mod.rs | 9 | ||||
| -rw-r--r-- | src/librustc_mir/build/matches/test.rs | 35 | ||||
| -rw-r--r-- | src/librustc_mir/hair/mod.rs | 3 | ||||
| -rw-r--r-- | src/librustc_mir/transform/no_landing_pads.rs | 1 | ||||
| -rw-r--r-- | src/librustc_mir/transform/qualify_consts.rs | 1 | ||||
| -rw-r--r-- | src/librustc_mir/transform/simplify.rs | 1 | ||||
| -rw-r--r-- | src/librustc_mir/transform/simplify_branches.rs | 22 | ||||
| -rw-r--r-- | src/librustc_mir/transform/type_check.rs | 16 |
9 files changed, 65 insertions, 60 deletions
diff --git a/src/librustc_mir/build/expr/into.rs b/src/librustc_mir/build/expr/into.rs index 3d4af259ec9..2b4336ba66f 100644 --- a/src/librustc_mir/build/expr/into.rs +++ b/src/librustc_mir/build/expr/into.rs @@ -15,6 +15,7 @@ use build::expr::category::{Category, RvalueFunc}; use hair::*; use rustc::ty; use rustc::mir::*; +use rustc::middle::const_val::ConstVal; impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> { /// Compile `expr`, storing the result into `destination`, which @@ -69,9 +70,11 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> { let mut then_block = this.cfg.start_new_block(); let mut else_block = this.cfg.start_new_block(); - this.cfg.terminate(block, source_info, TerminatorKind::If { - cond: operand, - targets: (then_block, else_block) + this.cfg.terminate(block, source_info, TerminatorKind::SwitchInt { + discr: operand, + switch_ty: this.hir.bool_ty(), + values: vec![ConstVal::Bool(true)], + targets: vec![then_block, else_block], }); unpack!(then_block = this.into(destination, then_block, then_expr)); @@ -111,16 +114,22 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> { let lhs = unpack!(block = this.as_operand(block, lhs)); let blocks = match op { - LogicalOp::And => (else_block, false_block), - LogicalOp::Or => (true_block, else_block), + LogicalOp::And => vec![else_block, false_block], + LogicalOp::Or => vec![true_block, else_block], }; - this.cfg.terminate(block, source_info, - TerminatorKind::If { cond: lhs, targets: blocks }); + this.cfg.terminate(block, source_info, TerminatorKind::SwitchInt { + discr: lhs, + switch_ty: this.hir.bool_ty(), + values: vec![ConstVal::Bool(true)], + targets: blocks, + }); let rhs = unpack!(else_block = this.as_operand(else_block, rhs)); - this.cfg.terminate(else_block, source_info, TerminatorKind::If { - cond: rhs, - targets: (true_block, false_block) + this.cfg.terminate(else_block, source_info, TerminatorKind::SwitchInt { + discr: rhs, + switch_ty: this.hir.bool_ty(), + values: vec![ConstVal::Bool(true)], + targets: vec![true_block, false_block], }); this.cfg.push_assign_constant( @@ -180,9 +189,11 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> { loop_block_end = this.as_operand(loop_block, cond_expr)); body_block = this.cfg.start_new_block(); this.cfg.terminate(loop_block_end, source_info, - TerminatorKind::If { - cond: cond, - targets: (body_block, exit_block) + TerminatorKind::SwitchInt { + discr: cond, + switch_ty: this.hir.bool_ty(), + values: vec![ConstVal::Bool(true)], + targets: vec![body_block, exit_block], }); // if the test is false, there's no `break` to assign `destination`, so diff --git a/src/librustc_mir/build/matches/mod.rs b/src/librustc_mir/build/matches/mod.rs index 5c3d93ffda9..0898d06d2e4 100644 --- a/src/librustc_mir/build/matches/mod.rs +++ b/src/librustc_mir/build/matches/mod.rs @@ -672,9 +672,12 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> { let source_info = self.source_info(guard.span); let cond = unpack!(block = self.as_operand(block, guard)); let otherwise = self.cfg.start_new_block(); - self.cfg.terminate(block, source_info, - TerminatorKind::If { cond: cond, - targets: (arm_block, otherwise)}); + self.cfg.terminate(block, source_info, TerminatorKind::SwitchInt { + discr: cond, + switch_ty: self.hir.bool_ty(), + values: vec![ConstVal::Bool(true)], + targets: vec![arm_block, otherwise], + }); Some(otherwise) } else { let source_info = self.source_info(candidate.span); diff --git a/src/librustc_mir/build/matches/test.rs b/src/librustc_mir/build/matches/test.rs index a35b2925ae2..291bd65d577 100644 --- a/src/librustc_mir/build/matches/test.rs +++ b/src/librustc_mir/build/matches/test.rs @@ -221,10 +221,11 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> { v => span_bug!(test.span, "expected boolean value but got {:?}", v) }; - (targets, - TerminatorKind::If { - cond: Operand::Consume(lvalue.clone()), - targets: (true_bb, else_bb) + (targets, TerminatorKind::SwitchInt { + discr: Operand::Consume(lvalue.clone()), + switch_ty: self.hir.bool_ty(), + values: vec![ConstVal::Bool(true)], + targets: vec![true_bb, else_bb] }) } @@ -240,7 +241,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> { (targets.clone(), TerminatorKind::SwitchInt { - discr: lvalue.clone(), + discr: Operand::Consume(lvalue.clone()), switch_ty: switch_ty, values: options.clone(), targets: targets @@ -314,9 +315,11 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> { // check the result let block = self.cfg.start_new_block(); - self.cfg.terminate(eq_block, source_info, TerminatorKind::If { - cond: Operand::Consume(eq_result), - targets: (block, fail), + self.cfg.terminate(eq_block, source_info, TerminatorKind::SwitchInt { + discr: Operand::Consume(eq_result), + switch_ty: self.hir.bool_ty(), + values: vec![ConstVal::Bool(true)], + targets: vec![block, fail], }); vec![block, fail] @@ -362,9 +365,11 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> { // branch based on result let target_blocks: Vec<_> = vec![self.cfg.start_new_block(), self.cfg.start_new_block()]; - self.cfg.terminate(block, source_info, TerminatorKind::If { - cond: Operand::Consume(result), - targets: (target_blocks[0], target_blocks[1]) + self.cfg.terminate(block, source_info, TerminatorKind::SwitchInt { + discr: Operand::Consume(result), + switch_ty: self.hir.bool_ty(), + values: vec![ConstVal::Bool(true)], + targets: target_blocks.clone(), }); target_blocks @@ -389,9 +394,11 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> { // branch based on result let target_block = self.cfg.start_new_block(); - self.cfg.terminate(block, source_info, TerminatorKind::If { - cond: Operand::Consume(result), - targets: (target_block, fail_block) + self.cfg.terminate(block, source_info, TerminatorKind::SwitchInt { + discr: Operand::Consume(result), + switch_ty: self.hir.bool_ty(), + values: vec![ConstVal::Bool(true)], + targets: vec![target_block, fail_block] }); target_block diff --git a/src/librustc_mir/hair/mod.rs b/src/librustc_mir/hair/mod.rs index 01dc01c5ecf..4ac67cfb2fc 100644 --- a/src/librustc_mir/hair/mod.rs +++ b/src/librustc_mir/hair/mod.rs @@ -134,7 +134,8 @@ pub enum ExprKind<'tcx> { op: LogicalOp, lhs: ExprRef<'tcx>, rhs: ExprRef<'tcx>, - }, + }, // NOT overloaded! + // LogicalOp is distinct from BinaryOp because of lazy evaluation of the operands. Unary { op: UnOp, arg: ExprRef<'tcx>, diff --git a/src/librustc_mir/transform/no_landing_pads.rs b/src/librustc_mir/transform/no_landing_pads.rs index 6ef5720b330..425df65659c 100644 --- a/src/librustc_mir/transform/no_landing_pads.rs +++ b/src/librustc_mir/transform/no_landing_pads.rs @@ -28,7 +28,6 @@ impl<'tcx> MutVisitor<'tcx> for NoLandingPads { TerminatorKind::Resume | TerminatorKind::Return | TerminatorKind::Unreachable | - TerminatorKind::If { .. } | TerminatorKind::Switch { .. } | TerminatorKind::SwitchInt { .. } => { /* nothing to do */ diff --git a/src/librustc_mir/transform/qualify_consts.rs b/src/librustc_mir/transform/qualify_consts.rs index 16371be576b..bda4c94625f 100644 --- a/src/librustc_mir/transform/qualify_consts.rs +++ b/src/librustc_mir/transform/qualify_consts.rs @@ -394,7 +394,6 @@ impl<'a, 'tcx> Qualifier<'a, 'tcx, 'tcx> { return Qualif::empty(); } - TerminatorKind::If {..} | TerminatorKind::Switch {..} | TerminatorKind::SwitchInt {..} | TerminatorKind::DropAndReplace { .. } | diff --git a/src/librustc_mir/transform/simplify.rs b/src/librustc_mir/transform/simplify.rs index d5fc90289e2..1127f50fe50 100644 --- a/src/librustc_mir/transform/simplify.rs +++ b/src/librustc_mir/transform/simplify.rs @@ -209,7 +209,6 @@ impl<'a, 'tcx: 'a> CfgSimplifier<'a, 'tcx> { // turn a branch with all successors identical to a goto fn simplify_branch(&mut self, terminator: &mut Terminator<'tcx>) -> bool { match terminator.kind { - TerminatorKind::If { .. } | TerminatorKind::Switch { .. } | TerminatorKind::SwitchInt { .. } => {}, _ => return false diff --git a/src/librustc_mir/transform/simplify_branches.rs b/src/librustc_mir/transform/simplify_branches.rs index 8759a340d7e..424250586b1 100644 --- a/src/librustc_mir/transform/simplify_branches.rs +++ b/src/librustc_mir/transform/simplify_branches.rs @@ -30,17 +30,17 @@ impl<'l, 'tcx> MirPass<'tcx> for SimplifyBranches<'l> { for block in mir.basic_blocks_mut() { let terminator = block.terminator_mut(); terminator.kind = match terminator.kind { - TerminatorKind::If { ref targets, cond: Operand::Constant(Constant { - literal: Literal::Value { - value: ConstVal::Bool(cond) - }, .. - }) } => { - if cond { - TerminatorKind::Goto { target: targets.0 } - } else { - TerminatorKind::Goto { target: targets.1 } - } - } + // TerminatorKind::If { ref targets, cond: Operand::Constant(Constant { + // literal: Literal::Value { + // value: ConstVal::Bool(cond) + // }, .. + // }) } => { + // if cond { + // TerminatorKind::Goto { target: targets.0 } + // } else { + // TerminatorKind::Goto { target: targets.1 } + // } + // } TerminatorKind::Assert { target, cond: Operand::Constant(Constant { literal: Literal::Value { diff --git a/src/librustc_mir/transform/type_check.rs b/src/librustc_mir/transform/type_check.rs index 529fe564af0..9d2ad314386 100644 --- a/src/librustc_mir/transform/type_check.rs +++ b/src/librustc_mir/transform/type_check.rs @@ -423,18 +423,8 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> { lv_ty, rv_ty, terr); } } - - TerminatorKind::If { ref cond, .. } => { - let cond_ty = cond.ty(mir, tcx); - match cond_ty.sty { - ty::TyBool => {} - _ => { - span_mirbug!(self, term, "bad If ({:?}, not bool", cond_ty); - } - } - } TerminatorKind::SwitchInt { ref discr, switch_ty, .. } => { - let discr_ty = discr.ty(mir, tcx).to_ty(tcx); + let discr_ty = discr.ty(mir, tcx); if let Err(terr) = self.sub_types(discr_ty, switch_ty) { span_mirbug!(self, term, "bad SwitchInt ({:?} on {:?}): {:?}", switch_ty, discr_ty, terr); @@ -603,10 +593,6 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> { match block.terminator().kind { TerminatorKind::Goto { target } => self.assert_iscleanup(mir, block, target, is_cleanup), - TerminatorKind::If { targets: (on_true, on_false), .. } => { - self.assert_iscleanup(mir, block, on_true, is_cleanup); - self.assert_iscleanup(mir, block, on_false, is_cleanup); - } TerminatorKind::Switch { ref targets, .. } | TerminatorKind::SwitchInt { ref targets, .. } => { for target in targets { |
