@@ -46,7 +46,6 @@ use std::cell::Cell;
4646use std:: cmp:: { self , max, min, Ordering } ;
4747use std:: fmt;
4848use std:: iter:: once;
49- use std:: ops:: RangeInclusive ;
5049
5150use smallvec:: { smallvec, SmallVec } ;
5251
@@ -102,9 +101,10 @@ enum Presence {
102101///
103102/// `IntRange` is never used to encode an empty range or a "range" that wraps
104103/// around the (offset) space: i.e., `range.lo <= range.hi`.
105- #[ derive( Clone , PartialEq , Eq ) ]
104+ #[ derive( Clone , Copy , PartialEq , Eq ) ]
106105pub ( crate ) struct IntRange {
107- range : RangeInclusive < u128 > ,
106+ pub ( crate ) lo : u128 ,
107+ pub ( crate ) hi : u128 ,
108108}
109109
110110impl IntRange {
@@ -114,20 +114,16 @@ impl IntRange {
114114 }
115115
116116 pub ( super ) fn is_singleton ( & self ) -> bool {
117- self . range . start ( ) == self . range . end ( )
118- }
119-
120- pub ( super ) fn boundaries ( & self ) -> ( u128 , u128 ) {
121- ( * self . range . start ( ) , * self . range . end ( ) )
117+ self . lo == self . hi
122118 }
123119
124120 #[ inline]
125121 fn from_bits < ' tcx > ( tcx : TyCtxt < ' tcx > , ty : Ty < ' tcx > , bits : u128 ) -> IntRange {
126122 let bias = IntRange :: signed_bias ( tcx, ty) ;
127- // Perform a shift if the underlying types are signed,
128- // which makes the interval arithmetic simpler .
123+ // Perform a shift if the underlying types are signed, which makes the interval arithmetic
124+ // type-independent .
129125 let val = bits ^ bias;
130- IntRange { range : val..= val }
126+ IntRange { lo : val, hi : val }
131127 }
132128
133129 #[ inline]
@@ -138,16 +134,17 @@ impl IntRange {
138134 ty : Ty < ' tcx > ,
139135 end : RangeEnd ,
140136 ) -> IntRange {
141- // Perform a shift if the underlying types are signed,
142- // which makes the interval arithmetic simpler .
137+ // Perform a shift if the underlying types are signed, which makes the interval arithmetic
138+ // type-independent .
143139 let bias = IntRange :: signed_bias ( tcx, ty) ;
144140 let ( lo, hi) = ( lo ^ bias, hi ^ bias) ;
145141 let offset = ( end == RangeEnd :: Excluded ) as u128 ;
146- if lo > hi || ( lo == hi && end == RangeEnd :: Excluded ) {
142+ let hi = hi - offset;
143+ if lo > hi {
147144 // This should have been caught earlier by E0030.
148- bug ! ( "malformed range pattern: {}..={}" , lo , ( hi - offset ) ) ;
145+ bug ! ( "malformed range pattern: {lo }..={hi}" ) ;
149146 }
150- IntRange { range : lo..= ( hi - offset ) }
147+ IntRange { lo , hi }
151148 }
152149
153150 // The return value of `signed_bias` should be XORed with an endpoint to encode/decode it.
@@ -162,14 +159,12 @@ impl IntRange {
162159 }
163160
164161 fn is_subrange ( & self , other : & Self ) -> bool {
165- other. range . start ( ) <= self . range . start ( ) && self . range . end ( ) <= other. range . end ( )
162+ other. lo <= self . lo && self . hi <= other. hi
166163 }
167164
168165 fn intersection ( & self , other : & Self ) -> Option < Self > {
169- let ( lo, hi) = self . boundaries ( ) ;
170- let ( other_lo, other_hi) = other. boundaries ( ) ;
171- if lo <= other_hi && other_lo <= hi {
172- Some ( IntRange { range : max ( lo, other_lo) ..=min ( hi, other_hi) } )
166+ if self . lo <= other. hi && other. lo <= self . hi {
167+ Some ( IntRange { lo : max ( self . lo , other. lo ) , hi : min ( self . hi , other. hi ) } )
173168 } else {
174169 None
175170 }
@@ -216,9 +211,8 @@ impl IntRange {
216211
217212 fn unpack_intrange ( range : IntRange ) -> [ IntBoundary ; 2 ] {
218213 use IntBoundary :: * ;
219- let ( lo, hi) = range. boundaries ( ) ;
220- let lo = JustBefore ( lo) ;
221- let hi = match hi. checked_add ( 1 ) {
214+ let lo = JustBefore ( range. lo ) ;
215+ let hi = match range. hi . checked_add ( 1 ) {
222216 Some ( m) => JustBefore ( m) ,
223217 None => AfterMax ,
224218 } ;
@@ -264,21 +258,19 @@ impl IntRange {
264258 use IntBoundary :: * ;
265259 use Presence :: * ;
266260 let presence = if paren_count > 0 { Seen } else { Unseen } ;
267- let range = match ( prev_bdy, bdy) {
268- ( JustBefore ( n) , JustBefore ( m) ) if n < m => n..= ( m - 1 ) ,
269- ( JustBefore ( n) , AfterMax ) => n..= u128:: MAX ,
261+ let ( lo , hi ) = match ( prev_bdy, bdy) {
262+ ( JustBefore ( n) , JustBefore ( m) ) if n < m => ( n , m - 1 ) ,
263+ ( JustBefore ( n) , AfterMax ) => ( n , u128:: MAX ) ,
270264 _ => unreachable ! ( ) , // Ruled out by the sorting and filtering we did
271265 } ;
272- ( presence, IntRange { range } )
266+ ( presence, IntRange { lo , hi } )
273267 } )
274268 }
275269
276270 /// Only used for displaying the range.
277271 pub ( super ) fn to_pat < ' tcx > ( & self , tcx : TyCtxt < ' tcx > , ty : Ty < ' tcx > ) -> Pat < ' tcx > {
278- let ( lo, hi) = self . boundaries ( ) ;
279-
280272 let bias = IntRange :: signed_bias ( tcx, ty) ;
281- let ( lo_bits, hi_bits) = ( lo ^ bias, hi ^ bias) ;
273+ let ( lo_bits, hi_bits) = ( self . lo ^ bias, self . hi ^ bias) ;
282274
283275 let env = ty:: ParamEnv :: empty ( ) . and ( ty) ;
284276 let lo_const = mir:: Const :: from_bits ( tcx, lo_bits, env) ;
@@ -303,7 +295,7 @@ impl IntRange {
303295/// first.
304296impl fmt:: Debug for IntRange {
305297 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
306- let ( lo, hi) = self . boundaries ( ) ;
298+ let ( lo, hi) = ( self . lo , self . hi ) ;
307299 write ! ( f, "{lo}" ) ?;
308300 write ! ( f, "{}" , RangeEnd :: Included ) ?;
309301 write ! ( f, "{hi}" )
0 commit comments