Skip to content

Commit 9bc4c37

Browse files
committed
Inline RangeInclusive into IntRange
1 parent a5c67f4 commit 9bc4c37

File tree

2 files changed

+33
-41
lines changed

2 files changed

+33
-41
lines changed

compiler/rustc_mir_build/src/thir/pattern/deconstruct_pat.rs

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ use std::cell::Cell;
4646
use std::cmp::{self, max, min, Ordering};
4747
use std::fmt;
4848
use std::iter::once;
49-
use std::ops::RangeInclusive;
5049

5150
use 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)]
106105
pub(crate) struct IntRange {
107-
range: RangeInclusive<u128>,
106+
pub(crate) lo: u128,
107+
pub(crate) hi: u128,
108108
}
109109

110110
impl 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.
304296
impl 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}")

compiler/rustc_mir_build/src/thir/pattern/usefulness.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,9 +1031,10 @@ fn lint_overlapping_range_endpoints<'p, 'tcx>(
10311031
let split_int_ranges = set.present.iter().filter_map(|c| c.as_int_range());
10321032
for overlap_range in split_int_ranges.clone() {
10331033
if overlap_range.is_singleton() {
1034-
let overlap: u128 = overlap_range.boundaries().0;
1035-
// Spans of ranges that start or end with the overlap.
1034+
let overlap: u128 = overlap_range.lo;
1035+
// Ranges that look like `lo..=overlap`.
10361036
let mut prefixes: SmallVec<[_; 1]> = Default::default();
1037+
// Ranges that look like `overlap..=hi`.
10371038
let mut suffixes: SmallVec<[_; 1]> = Default::default();
10381039
// Iterate on patterns that contained `overlap`.
10391040
for pat in column.iter() {
@@ -1043,17 +1044,16 @@ fn lint_overlapping_range_endpoints<'p, 'tcx>(
10431044
// Don't lint when one of the ranges is a singleton.
10441045
continue;
10451046
}
1046-
let (start, end) = this_range.boundaries();
1047-
if start == overlap {
1048-
// `this_range` looks like `overlap..=end`; it overlaps with any ranges that
1049-
// look like `start..=overlap`.
1047+
if this_range.lo == overlap {
1048+
// `this_range` looks like `overlap..=this_range.hi`; it overlaps with any
1049+
// ranges that look like `lo..=overlap`.
10501050
if !prefixes.is_empty() {
10511051
emit_lint(overlap_range, this_span, &prefixes);
10521052
}
10531053
suffixes.push(this_span)
1054-
} else if end == overlap {
1055-
// `this_range` looks like `start..=overlap`; it overlaps with any ranges
1056-
// that look like `overlap..=end`.
1054+
} else if this_range.hi == overlap {
1055+
// `this_range` looks like `this_range.lo..=overlap`; it overlaps with any
1056+
// ranges that look like `overlap..=hi`.
10571057
if !suffixes.is_empty() {
10581058
emit_lint(overlap_range, this_span, &suffixes);
10591059
}

0 commit comments

Comments
 (0)