@@ -620,8 +620,7 @@ fn max_slice_length<'p, 'a: 'p, 'tcx: 'a, I>(
620
620
621
621
/// An inclusive interval, used for precise integer exhaustiveness checking.
622
622
struct Interval < ' tcx > {
623
- pub lo : u128 ,
624
- pub hi : u128 ,
623
+ pub range : RangeInclusive < u128 > ,
625
624
pub ty : Ty < ' tcx > ,
626
625
}
627
626
@@ -641,15 +640,15 @@ impl<'tcx> Interval<'tcx> {
641
640
None
642
641
} else {
643
642
let offset = ( * end == RangeEnd :: Excluded ) as u128 ;
644
- Some ( Interval { lo , hi : hi - offset, ty } )
643
+ Some ( Interval { range : lo..= ( hi - offset) , ty } )
645
644
} ;
646
645
}
647
646
}
648
647
None
649
648
}
650
649
ConstantValue ( val) => {
651
650
let ty = val. ty ;
652
- val. assert_bits ( ty) . map ( |val| Interval { lo : val, hi : val, ty } )
651
+ val. assert_bits ( ty) . map ( |val| Interval { range : val..= val, ty } )
653
652
}
654
653
Single | Variant ( _) | Slice ( _) => {
655
654
None
@@ -690,7 +689,7 @@ impl<'tcx> Interval<'tcx> {
690
689
}
691
690
692
691
fn into_inner ( self ) -> ( u128 , u128 ) {
693
- ( self . lo , self . hi )
692
+ self . range . into_inner ( )
694
693
}
695
694
}
696
695
@@ -706,31 +705,27 @@ fn ranges_subtract_pattern<'a, 'tcx>(cx: &mut MatchCheckCtxt<'a, 'tcx>,
706
705
let mut ranges: Vec < _ > = ranges. into_iter ( ) . filter_map ( |r| {
707
706
Interval :: from_ctor ( & r) . map ( |i| i. into_inner ( ) )
708
707
} ) . collect ( ) ;
708
+ let ty = pat_interval. ty ;
709
+ let ( pat_interval_lo, pat_interval_hi) = pat_interval. into_inner ( ) ;
709
710
for ( subrange_lo, subrange_hi) in ranges {
710
- if pat_interval . lo > subrange_hi || pat_interval . hi < subrange_lo {
711
+ if pat_interval_lo > subrange_hi || subrange_lo > pat_interval_hi {
711
712
// The pattern doesn't intersect with the subrange at all,
712
713
// so the subrange remains untouched.
713
714
remaining_ranges. push ( ( subrange_lo, subrange_hi) ) ;
714
- } else if pat_interval. lo <= subrange_lo && pat_interval. hi >= subrange_hi {
715
- // The pattern entirely covers the subrange of values,
716
- // so we no longer have to consider this subrange_
717
- } else if pat_interval. lo <= subrange_lo {
718
- // The pattern intersects a lower section of the subrange,
719
- // so only the upper section will remain.
720
- remaining_ranges. push ( ( pat_interval. hi + 1 , subrange_hi) ) ;
721
- } else if pat_interval. hi >= subrange_hi {
722
- // The pattern intersects an upper section of the subrange,
723
- // so only the lower section will remain.
724
- remaining_ranges. push ( ( subrange_lo, pat_interval. lo - 1 ) ) ;
725
715
} else {
726
- // The pattern intersects the middle of the subrange,
727
- // so we create two ranges either side of the intersection.)
728
- remaining_ranges. push ( ( subrange_lo, pat_interval. lo - 1 ) ) ;
729
- remaining_ranges. push ( ( pat_interval. hi + 1 , subrange_hi) ) ;
716
+ if pat_interval_lo > subrange_lo {
717
+ // The pattern intersects an upper section of the
718
+ // subrange, so a lower section will remain.
719
+ remaining_ranges. push ( ( subrange_lo, pat_interval_lo - 1 ) ) ;
720
+ }
721
+ if pat_interval_hi < subrange_hi {
722
+ // The pattern intersects a lower section of the
723
+ // subrange, so an upper section will remain.
724
+ remaining_ranges. push ( ( pat_interval_hi + 1 , subrange_hi) ) ;
725
+ }
730
726
}
731
727
}
732
728
// Convert the remaining ranges from pairs to inclusive `ConstantRange`s.
733
- let ty = pat_interval. ty ;
734
729
remaining_ranges. into_iter ( ) . map ( |( lo, hi) | {
735
730
let ( lo, hi) = Interval :: offset_sign ( ty, ( lo, hi) , false ) ;
736
731
ConstantRange ( ty:: Const :: from_bits ( cx. tcx , lo, ty) ,
@@ -839,7 +834,7 @@ pub fn is_useful<'p, 'a: 'p, 'tcx: 'a>(cx: &mut MatchCheckCtxt<'a, 'tcx>,
839
834
// `missing_ctors` are those that should have appeared
840
835
// as patterns in the `match` expression, but did not.
841
836
let mut missing_ctors = vec ! [ ] ;
842
- ' req: for req_ctor in all_ctors. clone ( ) {
837
+ ' req: for req_ctor in & all_ctors {
843
838
let mut sub_ctors = vec ! [ req_ctor. clone( ) ] ;
844
839
// The only constructor patterns for which it is valid to
845
840
// treat the values as constructors are ranges (see
@@ -861,7 +856,7 @@ pub fn is_useful<'p, 'a: 'p, 'tcx: 'a>(cx: &mut MatchCheckCtxt<'a, 'tcx>,
861
856
// If the pattern for the required constructor
862
857
// appears in the `match`, then it is not missing,
863
858
// and we can move on to the next one.
864
- if * used_ctor == req_ctor {
859
+ if used_ctor == req_ctor {
865
860
continue ' req;
866
861
}
867
862
}
0 commit comments