Skip to content

Commit 9fb8da8

Browse files
committed
Remove unneeded field from SwitchTargets
1 parent 14ca83a commit 9fb8da8

File tree

131 files changed

+304
-388
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

131 files changed

+304
-388
lines changed

compiler/rustc_borrowck/src/invalidation.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ impl<'cx, 'tcx> Visitor<'tcx> for InvalidationGenerator<'cx, 'tcx> {
106106
self.check_activations(location);
107107

108108
match &terminator.kind {
109-
TerminatorKind::SwitchInt { discr, switch_ty: _, targets: _ } => {
109+
TerminatorKind::SwitchInt { discr, targets: _ } => {
110110
self.consume_operand(location, discr);
111111
}
112112
TerminatorKind::Drop { place: drop_place, target: _, unwind: _ } => {

compiler/rustc_borrowck/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ impl<'cx, 'tcx> rustc_mir_dataflow::ResultsVisitor<'cx, 'tcx> for MirBorrowckCtx
644644
self.check_activations(loc, span, flow_state);
645645

646646
match &term.kind {
647-
TerminatorKind::SwitchInt { discr, switch_ty: _, targets: _ } => {
647+
TerminatorKind::SwitchInt { discr, targets: _ } => {
648648
self.consume_operand(loc, (discr, span), flow_state);
649649
}
650650
TerminatorKind::Drop { place, target: _, unwind: _ } => {

compiler/rustc_borrowck/src/type_check/mod.rs

+2-17
Original file line numberDiff line numberDiff line change
@@ -1360,25 +1360,10 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
13601360
);
13611361
}
13621362
}
1363-
TerminatorKind::SwitchInt { discr, switch_ty, .. } => {
1363+
TerminatorKind::SwitchInt { discr, .. } => {
13641364
self.check_operand(discr, term_location);
13651365

1366-
let discr_ty = discr.ty(body, tcx);
1367-
if let Err(terr) = self.sub_types(
1368-
discr_ty,
1369-
*switch_ty,
1370-
term_location.to_locations(),
1371-
ConstraintCategory::Assignment,
1372-
) {
1373-
span_mirbug!(
1374-
self,
1375-
term,
1376-
"bad SwitchInt ({:?} on {:?}): {:?}",
1377-
switch_ty,
1378-
discr_ty,
1379-
terr
1380-
);
1381-
}
1366+
let switch_ty = discr.ty(body, tcx);
13821367
if !switch_ty.is_integral() && !switch_ty.is_char() && !switch_ty.is_bool() {
13831368
span_mirbug!(self, term, "bad SwitchInt discr ty {:?}", switch_ty);
13841369
}

compiler/rustc_codegen_cranelift/src/base.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -372,8 +372,10 @@ fn codegen_fn_body(fx: &mut FunctionCx<'_, '_, '_>, start_block: Block) {
372372
}
373373
}
374374

375-
TerminatorKind::SwitchInt { discr, switch_ty, targets } => {
376-
let discr = codegen_operand(fx, discr).load_scalar(fx);
375+
TerminatorKind::SwitchInt { discr, targets } => {
376+
let discr = codegen_operand(fx, discr);
377+
let switch_ty = discr.layout().ty;
378+
let discr = discr.load_scalar(fx);
377379

378380
let use_bool_opt = switch_ty.kind() == fx.tcx.types.bool.kind()
379381
|| (targets.iter().count() == 1 && targets.iter().next().unwrap().0 == 0);

compiler/rustc_codegen_ssa/src/mir/block.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -307,12 +307,10 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
307307
helper: TerminatorCodegenHelper<'tcx>,
308308
bx: &mut Bx,
309309
discr: &mir::Operand<'tcx>,
310-
switch_ty: Ty<'tcx>,
311310
targets: &SwitchTargets,
312311
) {
313312
let discr = self.codegen_operand(bx, &discr);
314-
// `switch_ty` is redundant, sanity-check that.
315-
assert_eq!(discr.layout.ty, switch_ty);
313+
let switch_ty = discr.layout.ty;
316314
let mut target_iter = targets.iter();
317315
if target_iter.len() == 1 {
318316
// If there are two targets (one conditional, one fallback), emit `br` instead of
@@ -1293,8 +1291,8 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
12931291
helper.funclet_br(self, bx, target, mergeable_succ())
12941292
}
12951293

1296-
mir::TerminatorKind::SwitchInt { ref discr, switch_ty, ref targets } => {
1297-
self.codegen_switchint_terminator(helper, bx, discr, switch_ty, targets);
1294+
mir::TerminatorKind::SwitchInt { ref discr, ref targets } => {
1295+
self.codegen_switchint_terminator(helper, bx, discr, targets);
12981296
MergingSucc::False
12991297
}
13001298

compiler/rustc_const_eval/src/interpret/terminator.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,9 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
2929

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

32-
SwitchInt { ref discr, ref targets, switch_ty } => {
32+
SwitchInt { ref discr, ref targets } => {
3333
let discr = self.read_immediate(&self.eval_operand(discr, None)?)?;
3434
trace!("SwitchInt({:?})", *discr);
35-
assert_eq!(discr.layout.ty, switch_ty);
3635

3736
// Branch to the `otherwise` case by default, if no match is found.
3837
let mut target_block = targets.otherwise();

compiler/rustc_const_eval/src/transform/validate.rs

+2-11
Original file line numberDiff line numberDiff line change
@@ -686,17 +686,8 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
686686
TerminatorKind::Goto { target } => {
687687
self.check_edge(location, *target, EdgeKind::Normal);
688688
}
689-
TerminatorKind::SwitchInt { targets, switch_ty, discr } => {
690-
let ty = discr.ty(&self.body.local_decls, self.tcx);
691-
if ty != *switch_ty {
692-
self.fail(
693-
location,
694-
format!(
695-
"encountered `SwitchInt` terminator with type mismatch: {:?} != {:?}",
696-
ty, switch_ty,
697-
),
698-
);
699-
}
689+
TerminatorKind::SwitchInt { targets, discr } => {
690+
let switch_ty = discr.ty(&self.body.local_decls, self.tcx);
700691

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

compiler/rustc_middle/src/mir/syntax.rs

-6
Original file line numberDiff line numberDiff line change
@@ -526,12 +526,6 @@ pub enum TerminatorKind<'tcx> {
526526
SwitchInt {
527527
/// The discriminant value being tested.
528528
discr: Operand<'tcx>,
529-
530-
/// The type of value being tested.
531-
/// This is always the same as the type of `discr`.
532-
/// FIXME: remove this redundant information. Currently, it is relied on by pretty-printing.
533-
switch_ty: Ty<'tcx>,
534-
535529
targets: SwitchTargets,
536530
},
537531

compiler/rustc_middle/src/mir/terminator.rs

+10-33
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
use crate::mir;
2-
use crate::mir::interpret::Scalar;
3-
use crate::ty::{self, Ty, TyCtxt};
41
use smallvec::{smallvec, SmallVec};
52

63
use super::{BasicBlock, InlineAsmOperand, Operand, SourceInfo, TerminatorKind};
@@ -131,17 +128,8 @@ impl<'tcx> Terminator<'tcx> {
131128
}
132129

133130
impl<'tcx> TerminatorKind<'tcx> {
134-
pub fn if_(
135-
tcx: TyCtxt<'tcx>,
136-
cond: Operand<'tcx>,
137-
t: BasicBlock,
138-
f: BasicBlock,
139-
) -> TerminatorKind<'tcx> {
140-
TerminatorKind::SwitchInt {
141-
discr: cond,
142-
switch_ty: tcx.types.bool,
143-
targets: SwitchTargets::static_if(0, f, t),
144-
}
131+
pub fn if_(cond: Operand<'tcx>, t: BasicBlock, f: BasicBlock) -> TerminatorKind<'tcx> {
132+
TerminatorKind::SwitchInt { discr: cond, targets: SwitchTargets::static_if(0, f, t) }
145133
}
146134

147135
pub fn successors(&self) -> Successors<'_> {
@@ -264,11 +252,9 @@ impl<'tcx> TerminatorKind<'tcx> {
264252
}
265253
}
266254

267-
pub fn as_switch(&self) -> Option<(&Operand<'tcx>, Ty<'tcx>, &SwitchTargets)> {
255+
pub fn as_switch(&self) -> Option<(&Operand<'tcx>, &SwitchTargets)> {
268256
match self {
269-
TerminatorKind::SwitchInt { discr, switch_ty, targets } => {
270-
Some((discr, *switch_ty, targets))
271-
}
257+
TerminatorKind::SwitchInt { discr, targets } => Some((discr, targets)),
272258
_ => None,
273259
}
274260
}
@@ -403,21 +389,12 @@ impl<'tcx> TerminatorKind<'tcx> {
403389
match *self {
404390
Return | Resume | Abort | Unreachable | GeneratorDrop => vec![],
405391
Goto { .. } => vec!["".into()],
406-
SwitchInt { ref targets, switch_ty, .. } => ty::tls::with(|tcx| {
407-
let param_env = ty::ParamEnv::empty();
408-
let switch_ty = tcx.lift(switch_ty).unwrap();
409-
let size = tcx.layout_of(param_env.and(switch_ty)).unwrap().size;
410-
targets
411-
.values
412-
.iter()
413-
.map(|&u| {
414-
mir::ConstantKind::from_scalar(tcx, Scalar::from_uint(u, size), switch_ty)
415-
.to_string()
416-
.into()
417-
})
418-
.chain(iter::once("otherwise".into()))
419-
.collect()
420-
}),
392+
SwitchInt { ref targets, .. } => targets
393+
.values
394+
.iter()
395+
.map(|&u| Cow::Owned(u.to_string()))
396+
.chain(iter::once("otherwise".into()))
397+
.collect(),
421398
Call { target: Some(_), cleanup: Some(_), .. } => {
422399
vec!["return".into(), "unwind".into()]
423400
}

compiler/rustc_middle/src/mir/visit.rs

-2
Original file line numberDiff line numberDiff line change
@@ -477,11 +477,9 @@ macro_rules! make_mir_visitor {
477477

478478
TerminatorKind::SwitchInt {
479479
discr,
480-
switch_ty,
481480
targets: _
482481
} => {
483482
self.visit_operand(discr, location);
484-
self.visit_ty($(& $mutability)? *switch_ty, TyContext::Location(location));
485483
}
486484

487485
TerminatorKind::Drop {

compiler/rustc_mir_build/src/build/expr/into.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
183183
LogicalOp::And => (else_block, shortcircuit_block),
184184
LogicalOp::Or => (shortcircuit_block, else_block),
185185
};
186-
let term = TerminatorKind::if_(this.tcx, lhs, blocks.0, blocks.1);
186+
let term = TerminatorKind::if_(lhs, blocks.0, blocks.1);
187187
this.cfg.terminate(block, source_info, term);
188188

189189
this.cfg.push_assign_constant(

compiler/rustc_mir_build/src/build/matches/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
9595

9696
let then_block = this.cfg.start_new_block();
9797
let else_block = this.cfg.start_new_block();
98-
let term = TerminatorKind::if_(this.tcx, operand, then_block, else_block);
98+
let term = TerminatorKind::if_(operand, then_block, else_block);
9999

100100
let source_info = this.source_info(expr_span);
101101
this.cfg.terminate(block, source_info, term);

compiler/rustc_mir_build/src/build/matches/test.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
203203
self.source_info(match_start_span),
204204
TerminatorKind::SwitchInt {
205205
discr: Operand::Move(discr),
206-
switch_ty: discr_ty,
207206
targets: switch_targets,
208207
},
209208
);
@@ -221,7 +220,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
221220
0 => (second_bb, first_bb),
222221
v => span_bug!(test.span, "expected boolean value but got {:?}", v),
223222
};
224-
TerminatorKind::if_(self.tcx, Operand::Copy(place), true_bb, false_bb)
223+
TerminatorKind::if_(Operand::Copy(place), true_bb, false_bb)
225224
} else {
226225
// The switch may be inexhaustive so we have a catch all block
227226
debug_assert_eq!(options.len() + 1, target_blocks.len());
@@ -232,7 +231,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
232231
);
233232
TerminatorKind::SwitchInt {
234233
discr: Operand::Copy(place),
235-
switch_ty,
236234
targets: switch_targets,
237235
}
238236
};
@@ -378,7 +376,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
378376
self.cfg.terminate(
379377
block,
380378
source_info,
381-
TerminatorKind::if_(self.tcx, Operand::Move(result), success_block, fail_block),
379+
TerminatorKind::if_(Operand::Move(result), success_block, fail_block),
382380
);
383381
}
384382

@@ -482,7 +480,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
482480
self.cfg.terminate(
483481
eq_block,
484482
source_info,
485-
TerminatorKind::if_(self.tcx, Operand::Move(eq_result), success_block, fail_block),
483+
TerminatorKind::if_(Operand::Move(eq_result), success_block, fail_block),
486484
);
487485
}
488486

compiler/rustc_mir_dataflow/src/elaborate_drops.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,6 @@ where
596596
source_info: self.source_info,
597597
kind: TerminatorKind::SwitchInt {
598598
discr: Operand::Move(discr),
599-
switch_ty: discr_ty,
600599
targets: SwitchTargets::new(
601600
values.iter().copied().zip(blocks.iter().copied()),
602601
*blocks.last().unwrap(),
@@ -716,7 +715,7 @@ where
716715
is_cleanup: unwind.is_cleanup(),
717716
terminator: Some(Terminator {
718717
source_info: self.source_info,
719-
kind: TerminatorKind::if_(tcx, move_(can_go), succ, drop_block),
718+
kind: TerminatorKind::if_(move_(can_go), succ, drop_block),
720719
}),
721720
};
722721
let loop_block = self.elaborator.patch().new_block(loop_block);
@@ -781,7 +780,6 @@ where
781780
source_info: self.source_info,
782781
kind: TerminatorKind::SwitchInt {
783782
discr: move_(elem_size),
784-
switch_ty: tcx.types.usize,
785783
targets: SwitchTargets::static_if(
786784
0,
787785
self.drop_loop_pair(ety, false, len),
@@ -1021,7 +1019,7 @@ where
10211019
DropStyle::Static => on_set,
10221020
DropStyle::Conditional | DropStyle::Open => {
10231021
let flag = self.elaborator.get_drop_flag(self.path).unwrap();
1024-
let term = TerminatorKind::if_(self.tcx(), flag, on_set, on_unset);
1022+
let term = TerminatorKind::if_(flag, on_set, on_unset);
10251023
self.new_block(unwind, term)
10261024
}
10271025
}

compiler/rustc_mir_dataflow/src/framework/direction.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ impl Direction for Backward {
261261
propagate(pred, &tmp);
262262
}
263263

264-
mir::TerminatorKind::SwitchInt { targets: _, ref discr, switch_ty: _ } => {
264+
mir::TerminatorKind::SwitchInt { targets: _, ref discr } => {
265265
let mut applier = BackwardSwitchIntEdgeEffectsApplier {
266266
body,
267267
pred,
@@ -577,7 +577,7 @@ impl Direction for Forward {
577577
}
578578
}
579579

580-
SwitchInt { ref targets, ref discr, switch_ty: _ } => {
580+
SwitchInt { ref targets, ref discr } => {
581581
let mut applier = ForwardSwitchIntEdgeEffectsApplier {
582582
exit_state,
583583
targets,

compiler/rustc_mir_transform/src/const_goto.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,9 @@ impl<'tcx> Visitor<'tcx> for ConstGotoOptimizationFinder<'_, 'tcx> {
8282
}
8383

8484
let target_bb_terminator = target_bb.terminator();
85-
let (discr, switch_ty, targets) = target_bb_terminator.kind.as_switch()?;
85+
let (discr, targets) = target_bb_terminator.kind.as_switch()?;
8686
if discr.place() == Some(*place) {
87+
let switch_ty = place.ty(self.body.local_decls(), self.tcx).ty;
8788
// We now know that the Switch matches on the const place, and it is statementless
8889
// Now find which value in the Switch matches the const value.
8990
let const_value =

compiler/rustc_mir_transform/src/coverage/tests.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use rustc_data_structures::graph::WithSuccessors;
3737
use rustc_index::vec::{Idx, IndexVec};
3838
use rustc_middle::mir::coverage::CoverageKind;
3939
use rustc_middle::mir::*;
40-
use rustc_middle::ty::{self, Ty, TyCtxt};
40+
use rustc_middle::ty;
4141
use rustc_span::{self, BytePos, Pos, Span, DUMMY_SP};
4242

4343
// All `TEMP_BLOCK` targets should be replaced before calling `to_body() -> mir::Body`.
@@ -47,7 +47,6 @@ struct MockBlocks<'tcx> {
4747
blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
4848
dummy_place: Place<'tcx>,
4949
next_local: usize,
50-
bool_ty: Ty<'tcx>,
5150
}
5251

5352
impl<'tcx> MockBlocks<'tcx> {
@@ -56,7 +55,6 @@ impl<'tcx> MockBlocks<'tcx> {
5655
blocks: IndexVec::new(),
5756
dummy_place: Place { local: RETURN_PLACE, projection: ty::List::empty() },
5857
next_local: 0,
59-
bool_ty: TyCtxt::BOOL_TY_FOR_UNIT_TESTING,
6058
}
6159
}
6260

@@ -157,7 +155,6 @@ impl<'tcx> MockBlocks<'tcx> {
157155
fn switchint(&mut self, some_from_block: Option<BasicBlock>) -> BasicBlock {
158156
let switchint_kind = TerminatorKind::SwitchInt {
159157
discr: Operand::Move(Place::from(self.new_temp())),
160-
switch_ty: self.bool_ty, // just a dummy value
161158
targets: SwitchTargets::static_if(0, TEMP_BLOCK, TEMP_BLOCK),
162159
};
163160
self.add_block_from(some_from_block, switchint_kind)

0 commit comments

Comments
 (0)