@@ -9,7 +9,7 @@ use crate::build::matches::{Candidate, MatchPair, Test, TestKind};
99use crate :: build:: Builder ;
1010use crate :: thir:: pattern:: compare_const_vals;
1111use crate :: thir:: * ;
12- use rustc_data_structures:: fx:: FxHashMap ;
12+ use rustc_data_structures:: fx:: FxIndexMap ;
1313use rustc_hir:: RangeEnd ;
1414use rustc_index:: bit_set:: BitSet ;
1515use 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