Skip to content

Commit 4efc7e9

Browse files
committed
rustc_mir_build: use IndexMap in TestKind::SwitchInt
1 parent c61f1c8 commit 4efc7e9

File tree

2 files changed

+17
-23
lines changed

2 files changed

+17
-23
lines changed

src/librustc_mir_build/build/matches/mod.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::build::{BlockAnd, BlockAndExtension, Builder};
1111
use crate::build::{GuardFrame, GuardFrameLocal, LocalsForNode};
1212
use crate::thir::{self, *};
1313
use rustc_data_structures::{
14-
fx::{FxHashMap, FxHashSet},
14+
fx::{FxHashSet, FxIndexMap},
1515
stack::ensure_sufficient_stack,
1616
};
1717
use rustc_hir::HirId;
@@ -817,9 +817,7 @@ enum TestKind<'tcx> {
817817
///
818818
/// For `bool` we always generate two edges, one for `true` and one for
819819
/// `false`.
820-
options: Vec<u128>,
821-
/// Reverse map used to ensure that the values in `options` are unique.
822-
indices: FxHashMap<&'tcx ty::Const<'tcx>, usize>,
820+
options: FxIndexMap<&'tcx ty::Const<'tcx>, u128>,
823821
},
824822

825823
/// Test for equality with value, possibly after an unsizing coercion to
@@ -1396,14 +1394,13 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
13961394
// may want to add cases based on the candidates that are
13971395
// available
13981396
match test.kind {
1399-
TestKind::SwitchInt { switch_ty, ref mut options, ref mut indices } => {
1397+
TestKind::SwitchInt { switch_ty, ref mut options } => {
14001398
for candidate in candidates.iter() {
14011399
if !self.add_cases_to_switch(
14021400
&match_place,
14031401
candidate,
14041402
switch_ty,
14051403
options,
1406-
indices,
14071404
) {
14081405
break;
14091406
}

src/librustc_mir_build/build/matches/test.rs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::build::matches::{Candidate, MatchPair, Test, TestKind};
99
use crate::build::Builder;
1010
use crate::thir::pattern::compare_const_vals;
1111
use crate::thir::*;
12-
use rustc_data_structures::fx::FxHashMap;
12+
use rustc_data_structures::fx::FxIndexMap;
1313
use rustc_hir::RangeEnd;
1414
use rustc_index::bit_set::BitSet;
1515
use rustc_middle::mir::*;
@@ -44,8 +44,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
4444

4545
// these maps are empty to start; cases are
4646
// added below in add_cases_to_switch
47-
options: vec![],
48-
indices: Default::default(),
47+
options: Default::default(),
4948
},
5049
}
5150
}
@@ -83,8 +82,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
8382
test_place: &Place<'tcx>,
8483
candidate: &Candidate<'pat, 'tcx>,
8584
switch_ty: Ty<'tcx>,
86-
options: &mut Vec<u128>,
87-
indices: &mut FxHashMap<&'tcx ty::Const<'tcx>, usize>,
85+
options: &mut FxIndexMap<&'tcx ty::Const<'tcx>, u128>,
8886
) -> bool {
8987
let match_pair = match candidate.match_pairs.iter().find(|mp| mp.place == *test_place) {
9088
Some(match_pair) => match_pair,
@@ -95,9 +93,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
9593

9694
match *match_pair.pattern.kind {
9795
PatKind::Constant { value } => {
98-
indices.entry(value).or_insert_with(|| {
99-
options.push(value.eval_bits(self.hir.tcx(), self.hir.param_env, switch_ty));
100-
options.len() - 1
96+
options.entry(value).or_insert_with(|| {
97+
value.eval_bits(self.hir.tcx(), self.hir.param_env, switch_ty)
10198
});
10299
true
103100
}
@@ -106,7 +103,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
106103
}
107104
PatKind::Range(range) => {
108105
// Check that none of the switch values are in the range.
109-
self.values_not_contained_in_range(range, indices).unwrap_or(false)
106+
self.values_not_contained_in_range(range, options).unwrap_or(false)
110107
}
111108
PatKind::Slice { .. }
112109
| PatKind::Array { .. }
@@ -216,7 +213,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
216213
);
217214
}
218215

219-
TestKind::SwitchInt { switch_ty, ref options, indices: _ } => {
216+
TestKind::SwitchInt { switch_ty, ref options } => {
220217
let target_blocks = make_target_blocks(self);
221218
let terminator = if switch_ty.kind == ty::Bool {
222219
assert!(!options.is_empty() && options.len() <= 2);
@@ -236,7 +233,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
236233
TerminatorKind::SwitchInt {
237234
discr: Operand::Copy(place),
238235
switch_ty,
239-
values: options.clone().into(),
236+
values: options.values().copied().collect(),
240237
targets: target_blocks,
241238
}
242239
};
@@ -532,20 +529,20 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
532529
// FIXME(#29623) we could use PatKind::Range to rule
533530
// things out here, in some cases.
534531
(
535-
&TestKind::SwitchInt { switch_ty: _, options: _, ref indices },
532+
&TestKind::SwitchInt { switch_ty: _, ref options },
536533
&PatKind::Constant { ref value },
537534
) if is_switch_ty(match_pair.pattern.ty) => {
538-
let index = indices[value];
535+
let index = options.get_index_of(value).unwrap();
539536
self.candidate_without_match_pair(match_pair_index, candidate);
540537
Some(index)
541538
}
542539

543540
(
544-
&TestKind::SwitchInt { switch_ty: _, ref options, ref indices },
541+
&TestKind::SwitchInt { switch_ty: _, ref options },
545542
&PatKind::Range(range),
546543
) => {
547544
let not_contained =
548-
self.values_not_contained_in_range(range, indices).unwrap_or(false);
545+
self.values_not_contained_in_range(range, options).unwrap_or(false);
549546

550547
if not_contained {
551548
// No switch values are contained in the pattern range,
@@ -777,9 +774,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
777774
fn values_not_contained_in_range(
778775
&self,
779776
range: PatRange<'tcx>,
780-
indices: &FxHashMap<&'tcx ty::Const<'tcx>, usize>,
777+
options: &FxIndexMap<&'tcx ty::Const<'tcx>, u128>,
781778
) -> Option<bool> {
782-
for &val in indices.keys() {
779+
for &val in options.keys() {
783780
if self.const_range_contains(range, val)? {
784781
return Some(false);
785782
}

0 commit comments

Comments
 (0)