Skip to content

Commit b66e123

Browse files
authored
Rollup merge of #105234 - JakobDegen:unneeded-field, r=oli-obk
Remove unneeded field from `SwitchTargets` This had a fixme already. The only change in behavior is that the mir dumps now no longer contains labels for the types of the integers on the edges of a switchint: Before: ![image](https://user-images.githubusercontent.com/51179609/205467622-34401a68-dca6-43eb-915e-b9fda1988860.png) After: ![image](https://user-images.githubusercontent.com/51179609/205467634-b5b2a259-9cb4-4843-845c-592c500f0f9c.png) I don't think that's a problem though. The information is still available to a user that really cares by checking the type of `_2`, so it honestly feels like a bit of an improvement to me. r? mir
2 parents 7e1857d + 9fb8da8 commit b66e123

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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 17 deletions
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

Lines changed: 4 additions & 2 deletions
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

Lines changed: 3 additions & 5 deletions
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

Lines changed: 1 addition & 2 deletions
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

Lines changed: 2 additions & 11 deletions
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

Lines changed: 0 additions & 6 deletions
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

Lines changed: 10 additions & 33 deletions
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

Lines changed: 0 additions & 2 deletions
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 {

0 commit comments

Comments
 (0)