@@ -9,7 +9,7 @@ use crate::build::matches::{Candidate, MatchPair, Test, TestKind};
9
9
use crate :: build:: Builder ;
10
10
use crate :: thir:: pattern:: compare_const_vals;
11
11
use crate :: thir:: * ;
12
- use rustc_data_structures:: fx:: FxHashMap ;
12
+ use rustc_data_structures:: fx:: FxIndexMap ;
13
13
use rustc_hir:: RangeEnd ;
14
14
use rustc_index:: bit_set:: BitSet ;
15
15
use rustc_middle:: mir:: * ;
@@ -44,8 +44,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
44
44
45
45
// these maps are empty to start; cases are
46
46
// added below in add_cases_to_switch
47
- options : vec ! [ ] ,
48
- indices : Default :: default ( ) ,
47
+ options : Default :: default ( ) ,
49
48
} ,
50
49
}
51
50
}
@@ -83,8 +82,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
83
82
test_place : & Place < ' tcx > ,
84
83
candidate : & Candidate < ' pat , ' tcx > ,
85
84
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 > ,
88
86
) -> bool {
89
87
let match_pair = match candidate. match_pairs . iter ( ) . find ( |mp| mp. place == * test_place) {
90
88
Some ( match_pair) => match_pair,
@@ -95,9 +93,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
95
93
96
94
match * match_pair. pattern . kind {
97
95
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)
101
98
} ) ;
102
99
true
103
100
}
@@ -106,7 +103,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
106
103
}
107
104
PatKind :: Range ( range) => {
108
105
// 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 )
110
107
}
111
108
PatKind :: Slice { .. }
112
109
| PatKind :: Array { .. }
@@ -216,7 +213,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
216
213
) ;
217
214
}
218
215
219
- TestKind :: SwitchInt { switch_ty, ref options, indices : _ } => {
216
+ TestKind :: SwitchInt { switch_ty, ref options } => {
220
217
let target_blocks = make_target_blocks ( self ) ;
221
218
let terminator = if switch_ty. kind == ty:: Bool {
222
219
assert ! ( !options. is_empty( ) && options. len( ) <= 2 ) ;
@@ -236,7 +233,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
236
233
TerminatorKind :: SwitchInt {
237
234
discr : Operand :: Copy ( place) ,
238
235
switch_ty,
239
- values : options. clone ( ) . into ( ) ,
236
+ values : options. values ( ) . copied ( ) . collect ( ) ,
240
237
targets : target_blocks,
241
238
}
242
239
} ;
@@ -532,20 +529,20 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
532
529
// FIXME(#29623) we could use PatKind::Range to rule
533
530
// things out here, in some cases.
534
531
(
535
- & TestKind :: SwitchInt { switch_ty : _, options : _ , ref indices } ,
532
+ & TestKind :: SwitchInt { switch_ty : _, ref options } ,
536
533
& PatKind :: Constant { ref value } ,
537
534
) if is_switch_ty ( match_pair. pattern . ty ) => {
538
- let index = indices [ value] ;
535
+ let index = options . get_index_of ( value) . unwrap ( ) ;
539
536
self . candidate_without_match_pair ( match_pair_index, candidate) ;
540
537
Some ( index)
541
538
}
542
539
543
540
(
544
- & TestKind :: SwitchInt { switch_ty : _, ref options, ref indices } ,
541
+ & TestKind :: SwitchInt { switch_ty : _, ref options } ,
545
542
& PatKind :: Range ( range) ,
546
543
) => {
547
544
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 ) ;
549
546
550
547
if not_contained {
551
548
// No switch values are contained in the pattern range,
@@ -777,9 +774,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
777
774
fn values_not_contained_in_range (
778
775
& self ,
779
776
range : PatRange < ' tcx > ,
780
- indices : & FxHashMap < & ' tcx ty:: Const < ' tcx > , usize > ,
777
+ options : & FxIndexMap < & ' tcx ty:: Const < ' tcx > , u128 > ,
781
778
) -> Option < bool > {
782
- for & val in indices . keys ( ) {
779
+ for & val in options . keys ( ) {
783
780
if self . const_range_contains ( range, val) ? {
784
781
return Some ( false ) ;
785
782
}
0 commit comments