@@ -46,7 +46,6 @@ use std::cell::Cell;
46
46
use std:: cmp:: { self , max, min, Ordering } ;
47
47
use std:: fmt;
48
48
use std:: iter:: once;
49
- use std:: ops:: RangeInclusive ;
50
49
51
50
use smallvec:: { smallvec, SmallVec } ;
52
51
@@ -102,9 +101,10 @@ enum Presence {
102
101
///
103
102
/// `IntRange` is never used to encode an empty range or a "range" that wraps
104
103
/// around the (offset) space: i.e., `range.lo <= range.hi`.
105
- #[ derive( Clone , PartialEq , Eq ) ]
104
+ #[ derive( Clone , Copy , PartialEq , Eq ) ]
106
105
pub ( crate ) struct IntRange {
107
- range : RangeInclusive < u128 > ,
106
+ pub ( crate ) lo : u128 ,
107
+ pub ( crate ) hi : u128 ,
108
108
}
109
109
110
110
impl IntRange {
@@ -114,20 +114,16 @@ impl IntRange {
114
114
}
115
115
116
116
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
122
118
}
123
119
124
120
#[ inline]
125
121
fn from_bits < ' tcx > ( tcx : TyCtxt < ' tcx > , ty : Ty < ' tcx > , bits : u128 ) -> IntRange {
126
122
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 .
129
125
let val = bits ^ bias;
130
- IntRange { range : val..= val }
126
+ IntRange { lo : val, hi : val }
131
127
}
132
128
133
129
#[ inline]
@@ -138,16 +134,17 @@ impl IntRange {
138
134
ty : Ty < ' tcx > ,
139
135
end : RangeEnd ,
140
136
) -> 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 .
143
139
let bias = IntRange :: signed_bias ( tcx, ty) ;
144
140
let ( lo, hi) = ( lo ^ bias, hi ^ bias) ;
145
141
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 {
147
144
// This should have been caught earlier by E0030.
148
- bug ! ( "malformed range pattern: {}..={}" , lo , ( hi - offset ) ) ;
145
+ bug ! ( "malformed range pattern: {lo }..={hi}" ) ;
149
146
}
150
- IntRange { range : lo..= ( hi - offset ) }
147
+ IntRange { lo , hi }
151
148
}
152
149
153
150
// The return value of `signed_bias` should be XORed with an endpoint to encode/decode it.
@@ -162,14 +159,12 @@ impl IntRange {
162
159
}
163
160
164
161
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
166
163
}
167
164
168
165
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 ) } )
173
168
} else {
174
169
None
175
170
}
@@ -216,9 +211,8 @@ impl IntRange {
216
211
217
212
fn unpack_intrange ( range : IntRange ) -> [ IntBoundary ; 2 ] {
218
213
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 ) {
222
216
Some ( m) => JustBefore ( m) ,
223
217
None => AfterMax ,
224
218
} ;
@@ -264,21 +258,19 @@ impl IntRange {
264
258
use IntBoundary :: * ;
265
259
use Presence :: * ;
266
260
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 ) ,
270
264
_ => unreachable ! ( ) , // Ruled out by the sorting and filtering we did
271
265
} ;
272
- ( presence, IntRange { range } )
266
+ ( presence, IntRange { lo , hi } )
273
267
} )
274
268
}
275
269
276
270
/// Only used for displaying the range.
277
271
pub ( super ) fn to_pat < ' tcx > ( & self , tcx : TyCtxt < ' tcx > , ty : Ty < ' tcx > ) -> Pat < ' tcx > {
278
- let ( lo, hi) = self . boundaries ( ) ;
279
-
280
272
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) ;
282
274
283
275
let env = ty:: ParamEnv :: empty ( ) . and ( ty) ;
284
276
let lo_const = mir:: Const :: from_bits ( tcx, lo_bits, env) ;
@@ -303,7 +295,7 @@ impl IntRange {
303
295
/// first.
304
296
impl fmt:: Debug for IntRange {
305
297
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
306
- let ( lo, hi) = self . boundaries ( ) ;
298
+ let ( lo, hi) = ( self . lo , self . hi ) ;
307
299
write ! ( f, "{lo}" ) ?;
308
300
write ! ( f, "{}" , RangeEnd :: Included ) ?;
309
301
write ! ( f, "{hi}" )
0 commit comments