@@ -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
@@ -104,9 +103,10 @@ enum Presence {
104
103
///
105
104
/// `IntRange` is never used to encode an empty range or a "range" that wraps
106
105
/// around the (offset) space: i.e., `range.lo <= range.hi`.
107
- #[ derive( Clone , PartialEq , Eq ) ]
106
+ #[ derive( Clone , Copy , PartialEq , Eq ) ]
108
107
pub ( crate ) struct IntRange {
109
- range : RangeInclusive < u128 > ,
108
+ lo : u128 ,
109
+ hi : u128 ,
110
110
}
111
111
112
112
impl IntRange {
@@ -116,20 +116,16 @@ impl IntRange {
116
116
}
117
117
118
118
fn is_singleton ( & self ) -> bool {
119
- self . range . start ( ) == self . range . end ( )
120
- }
121
-
122
- fn boundaries ( & self ) -> ( u128 , u128 ) {
123
- ( * self . range . start ( ) , * self . range . end ( ) )
119
+ self . lo == self . hi
124
120
}
125
121
126
122
#[ inline]
127
123
fn from_bits < ' tcx > ( tcx : TyCtxt < ' tcx > , ty : Ty < ' tcx > , bits : u128 ) -> IntRange {
128
124
let bias = IntRange :: signed_bias ( tcx, ty) ;
129
- // Perform a shift if the underlying types are signed,
130
- // which makes the interval arithmetic simpler .
125
+ // Perform a shift if the underlying types are signed, which makes the interval arithmetic
126
+ // type-independent .
131
127
let val = bits ^ bias;
132
- IntRange { range : val..= val }
128
+ IntRange { lo : val, hi : val }
133
129
}
134
130
135
131
#[ inline]
@@ -140,16 +136,17 @@ impl IntRange {
140
136
ty : Ty < ' tcx > ,
141
137
end : RangeEnd ,
142
138
) -> IntRange {
143
- // Perform a shift if the underlying types are signed,
144
- // which makes the interval arithmetic simpler .
139
+ // Perform a shift if the underlying types are signed, which makes the interval arithmetic
140
+ // type-independent .
145
141
let bias = IntRange :: signed_bias ( tcx, ty) ;
146
142
let ( lo, hi) = ( lo ^ bias, hi ^ bias) ;
147
143
let offset = ( end == RangeEnd :: Excluded ) as u128 ;
148
- if lo > hi || ( lo == hi && end == RangeEnd :: Excluded ) {
144
+ let hi = hi - offset;
145
+ if lo > hi {
149
146
// This should have been caught earlier by E0030.
150
- bug ! ( "malformed range pattern: {}..={}" , lo , ( hi - offset ) ) ;
147
+ bug ! ( "malformed range pattern: {lo }..={hi}" ) ;
151
148
}
152
- IntRange { range : lo..= ( hi - offset ) }
149
+ IntRange { lo , hi }
153
150
}
154
151
155
152
// The return value of `signed_bias` should be XORed with an endpoint to encode/decode it.
@@ -164,14 +161,12 @@ impl IntRange {
164
161
}
165
162
166
163
fn is_subrange ( & self , other : & Self ) -> bool {
167
- other. range . start ( ) <= self . range . start ( ) && self . range . end ( ) <= other. range . end ( )
164
+ other. lo <= self . lo && self . hi <= other. hi
168
165
}
169
166
170
167
fn intersection ( & self , other : & Self ) -> Option < Self > {
171
- let ( lo, hi) = self . boundaries ( ) ;
172
- let ( other_lo, other_hi) = other. boundaries ( ) ;
173
- if lo <= other_hi && other_lo <= hi {
174
- Some ( IntRange { range : max ( lo, other_lo) ..=min ( hi, other_hi) } )
168
+ if self . lo <= other. hi && other. lo <= self . hi {
169
+ Some ( IntRange { lo : max ( self . lo , other. lo ) , hi : min ( self . hi , other. hi ) } )
175
170
} else {
176
171
None
177
172
}
@@ -189,9 +184,9 @@ impl IntRange {
189
184
// `true` in the following cases:
190
185
// 1 ------- // 1 -------
191
186
// 2 -------- // 2 -------
192
- let ( lo , hi ) = self . boundaries ( ) ;
193
- let ( other_lo , other_hi ) = other . boundaries ( ) ;
194
- ( lo == other_hi || hi == other_lo ) && ! self . is_singleton ( ) && !other. is_singleton ( )
187
+ ( self . lo == other . hi || self . hi == other . lo )
188
+ && ! self . is_singleton ( )
189
+ && !other. is_singleton ( )
195
190
}
196
191
197
192
/// Partition a range of integers into disjoint subranges. This does constructor splitting for
@@ -235,9 +230,8 @@ impl IntRange {
235
230
236
231
fn unpack_intrange ( range : IntRange ) -> [ IntBoundary ; 2 ] {
237
232
use IntBoundary :: * ;
238
- let ( lo, hi) = range. boundaries ( ) ;
239
- let lo = JustBefore ( lo) ;
240
- let hi = match hi. checked_add ( 1 ) {
233
+ let lo = JustBefore ( range. lo ) ;
234
+ let hi = match range. hi . checked_add ( 1 ) {
241
235
Some ( m) => JustBefore ( m) ,
242
236
None => AfterMax ,
243
237
} ;
@@ -283,21 +277,19 @@ impl IntRange {
283
277
use IntBoundary :: * ;
284
278
use Presence :: * ;
285
279
let presence = if paren_count > 0 { Seen } else { Unseen } ;
286
- let range = match ( prev_bdy, bdy) {
287
- ( JustBefore ( n) , JustBefore ( m) ) if n < m => n..= ( m - 1 ) ,
288
- ( JustBefore ( n) , AfterMax ) => n..= u128:: MAX ,
280
+ let ( lo , hi ) = match ( prev_bdy, bdy) {
281
+ ( JustBefore ( n) , JustBefore ( m) ) if n < m => ( n , m - 1 ) ,
282
+ ( JustBefore ( n) , AfterMax ) => ( n , u128:: MAX ) ,
289
283
_ => unreachable ! ( ) , // Ruled out by the sorting and filtering we did
290
284
} ;
291
- ( presence, IntRange { range } )
285
+ ( presence, IntRange { lo , hi } )
292
286
} )
293
287
}
294
288
295
289
/// Only used for displaying the range.
296
290
fn to_pat < ' tcx > ( & self , tcx : TyCtxt < ' tcx > , ty : Ty < ' tcx > ) -> Pat < ' tcx > {
297
- let ( lo, hi) = self . boundaries ( ) ;
298
-
299
291
let bias = IntRange :: signed_bias ( tcx, ty) ;
300
- let ( lo_bits, hi_bits) = ( lo ^ bias, hi ^ bias) ;
292
+ let ( lo_bits, hi_bits) = ( self . lo ^ bias, self . hi ^ bias) ;
301
293
302
294
let env = ty:: ParamEnv :: empty ( ) . and ( ty) ;
303
295
let lo_const = mir:: Const :: from_bits ( tcx, lo_bits, env) ;
@@ -367,7 +359,7 @@ impl IntRange {
367
359
/// first.
368
360
impl fmt:: Debug for IntRange {
369
361
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
370
- let ( lo, hi) = self . boundaries ( ) ;
362
+ let ( lo, hi) = ( self . lo , self . hi ) ;
371
363
write ! ( f, "{lo}" ) ?;
372
364
write ! ( f, "{}" , RangeEnd :: Included ) ?;
373
365
write ! ( f, "{hi}" )
0 commit comments