@@ -627,7 +627,7 @@ impl<'tcx> IntRange<'tcx> {
627
627
if let Some ( hi) = hi. assert_bits ( ty) {
628
628
// Perform a shift if the underlying types are signed,
629
629
// which makes the interval arithmetic simpler.
630
- let ( lo, hi) = Self :: encode ( tcx, ty, lo..=hi ) ;
630
+ let ( lo, hi) = Self :: encode ( tcx, ty, ( lo , hi ) ) ;
631
631
// Make sure the interval is well-formed.
632
632
return if lo > hi || lo == hi && * end == RangeEnd :: Excluded {
633
633
None
@@ -642,7 +642,7 @@ impl<'tcx> IntRange<'tcx> {
642
642
ConstantValue ( val) => {
643
643
let ty = val. ty ;
644
644
if let Some ( val) = val. assert_bits ( ty) {
645
- let ( lo, hi) = Self :: encode ( tcx, ty, val..= val) ;
645
+ let ( lo, hi) = Self :: encode ( tcx, ty, ( val, val) ) ;
646
646
Some ( IntRange { range : lo..=hi, ty } )
647
647
} else {
648
648
None
@@ -654,50 +654,44 @@ impl<'tcx> IntRange<'tcx> {
654
654
}
655
655
}
656
656
657
- fn convert ( tcx : TyCtxt < ' _ , ' tcx , ' tcx > ,
658
- ty : Ty < ' tcx > ,
659
- range : RangeInclusive < u128 > ,
660
- encode : bool )
661
- -> ( u128 , u128 ) {
662
- // We ensure that all integer values are contiguous: that is, that their
663
- // minimum value is represented by 0, so that comparisons and increments/
664
- // decrements on interval endpoints work consistently whether the endpoints
665
- // are signed or unsigned.
666
- let ( lo, hi) = range. into_inner ( ) ;
657
+ fn encode ( tcx : TyCtxt < ' _ , ' tcx , ' tcx > ,
658
+ ty : Ty < ' tcx > ,
659
+ ( lo, hi) : ( u128 , u128 ) )
660
+ -> ( u128 , u128 ) {
667
661
match ty. sty {
668
662
ty:: TyInt ( _) => {
669
663
// FIXME(49937): refactor these bit manipulations into interpret.
670
664
let bits = tcx. layout_of ( ty:: ParamEnv :: reveal_all ( ) . and ( ty) )
671
665
. unwrap ( ) . size . bits ( ) as u128 ;
672
666
let min = 1u128 << ( bits - 1 ) ;
673
667
let mask = !0u128 >> ( 128 - bits) ;
674
- if encode {
675
- let offset = |x : u128 | x. wrapping_sub ( min) & mask;
676
- ( offset ( lo) , offset ( hi) )
677
- } else {
678
- let offset = |x : u128 | x. wrapping_add ( min) & mask;
679
- ( offset ( lo) , offset ( hi) )
680
- }
668
+ let offset = |x : u128 | x. wrapping_sub ( min) & mask;
669
+ ( offset ( lo) , offset ( hi) )
681
670
}
682
- ty:: TyUint ( _) | ty:: TyChar => {
683
- ( lo, hi)
684
- }
685
- _ => bug ! ( "`IntRange` should only contain integer types" )
671
+ _ => ( lo, hi)
686
672
}
687
673
}
688
674
689
- fn encode ( tcx : TyCtxt < ' _ , ' tcx , ' tcx > ,
690
- ty : Ty < ' tcx > ,
691
- range : RangeInclusive < u128 > )
692
- -> ( u128 , u128 ) {
693
- Self :: convert ( tcx, ty, range, true )
694
- }
695
-
696
675
fn decode ( tcx : TyCtxt < ' _ , ' tcx , ' tcx > ,
697
676
ty : Ty < ' tcx > ,
698
677
range : RangeInclusive < u128 > )
699
- -> ( u128 , u128 ) {
700
- Self :: convert ( tcx, ty, range, false )
678
+ -> Constructor < ' tcx > {
679
+ let ( lo, hi) = range. into_inner ( ) ;
680
+ let ( lo, hi) = match ty. sty {
681
+ ty:: TyInt ( _) => {
682
+ // FIXME(49937): refactor these bit manipulations into interpret.
683
+ let bits = tcx. layout_of ( ty:: ParamEnv :: reveal_all ( ) . and ( ty) )
684
+ . unwrap ( ) . size . bits ( ) as u128 ;
685
+ let min = 1u128 << ( bits - 1 ) ;
686
+ let mask = !0u128 >> ( 128 - bits) ;
687
+ let offset = |x : u128 | x. wrapping_add ( min) & mask;
688
+ ( offset ( lo) , offset ( hi) )
689
+ }
690
+ _ => ( lo, hi)
691
+ } ;
692
+ ConstantRange ( ty:: Const :: from_bits ( tcx, lo, ty) ,
693
+ ty:: Const :: from_bits ( tcx, hi, ty) ,
694
+ RangeEnd :: Included )
701
695
}
702
696
703
697
fn into_inner ( self ) -> ( u128 , u128 ) {
@@ -739,10 +733,7 @@ fn ranges_subtract_pattern<'a, 'tcx>(cx: &mut MatchCheckCtxt<'a, 'tcx>,
739
733
}
740
734
// Convert the remaining ranges from pairs to inclusive `ConstantRange`s.
741
735
remaining_ranges. into_iter ( ) . map ( |r| {
742
- let ( lo, hi) = IntRange :: decode ( cx. tcx , ty, r) ;
743
- ConstantRange ( ty:: Const :: from_bits ( cx. tcx , lo, ty) ,
744
- ty:: Const :: from_bits ( cx. tcx , hi, ty) ,
745
- RangeEnd :: Included )
736
+ IntRange :: decode ( cx. tcx , ty, r)
746
737
} ) . collect ( )
747
738
} else {
748
739
ranges
0 commit comments