Skip to content

Commit 5e53d76

Browse files
committed
shrink TestBranch::Constant and PatRangeBoundary::Finite
1 parent 9f3f015 commit 5e53d76

File tree

5 files changed

+23
-17
lines changed

5 files changed

+23
-17
lines changed

compiler/rustc_middle/src/thir.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -957,9 +957,9 @@ impl<'tcx> PatRange<'tcx> {
957957
#[inline]
958958
pub fn contains(&self, value: ty::Value<'tcx>, tcx: TyCtxt<'tcx>) -> Option<bool> {
959959
use Ordering::*;
960-
debug_assert_eq!(self.ty, value.ty);
960+
debug_assert_eq!(value.ty, self.ty);
961961
let ty = self.ty;
962-
let value = PatRangeBoundary::Finite(value);
962+
let value = PatRangeBoundary::Finite(value.valtree);
963963
// For performance, it's important to only do the second comparison if necessary.
964964
Some(
965965
match self.lo.compare_with(value, ty, tcx)? {
@@ -994,11 +994,13 @@ impl<'tcx> PatRange<'tcx> {
994994

995995
impl<'tcx> fmt::Display for PatRange<'tcx> {
996996
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
997-
if let PatRangeBoundary::Finite(value) = &self.lo {
997+
if let &PatRangeBoundary::Finite(valtree) = &self.lo {
998+
let value = ty::Value { ty: self.ty, valtree };
998999
write!(f, "{value}")?;
9991000
}
1000-
if let PatRangeBoundary::Finite(value) = &self.hi {
1001+
if let &PatRangeBoundary::Finite(valtree) = &self.hi {
10011002
write!(f, "{}", self.end)?;
1003+
let value = ty::Value { ty: self.ty, valtree };
10021004
write!(f, "{value}")?;
10031005
} else {
10041006
// `0..` is parsed as an inclusive range, we must display it correctly.
@@ -1012,7 +1014,8 @@ impl<'tcx> fmt::Display for PatRange<'tcx> {
10121014
/// If present, the const must be of a numeric type.
10131015
#[derive(Copy, Clone, Debug, PartialEq, HashStable, TypeVisitable)]
10141016
pub enum PatRangeBoundary<'tcx> {
1015-
Finite(ty::Value<'tcx>),
1017+
/// The type of this valtree is stored in the surrounding `PatRange`.
1018+
Finite(ty::ValTree<'tcx>),
10161019
NegInfinity,
10171020
PosInfinity,
10181021
}
@@ -1023,7 +1026,7 @@ impl<'tcx> PatRangeBoundary<'tcx> {
10231026
matches!(self, Self::Finite(..))
10241027
}
10251028
#[inline]
1026-
pub fn as_finite(self) -> Option<ty::Value<'tcx>> {
1029+
pub fn as_finite(self) -> Option<ty::ValTree<'tcx>> {
10271030
match self {
10281031
Self::Finite(value) => Some(value),
10291032
Self::NegInfinity | Self::PosInfinity => None,

compiler/rustc_mir_build/src/builder/matches/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,8 +1372,8 @@ pub(crate) struct Test<'tcx> {
13721372
enum TestBranch<'tcx> {
13731373
/// Success branch, used for tests with two possible outcomes.
13741374
Success,
1375-
/// Branch corresponding to this constant.
1376-
Constant(ty::Value<'tcx>, u128),
1375+
/// Branch corresponding to this constant. Must be a scalar.
1376+
Constant(ty::Value<'tcx>),
13771377
/// Branch corresponding to this variant.
13781378
Variant(VariantIdx),
13791379
/// Failure branch for tests with two possible outcomes, and "otherwise" branch for other tests.
@@ -1382,7 +1382,7 @@ enum TestBranch<'tcx> {
13821382

13831383
impl<'tcx> TestBranch<'tcx> {
13841384
fn as_constant(&self) -> Option<ty::Value<'tcx>> {
1385-
if let Self::Constant(v, _) = self { Some(*v) } else { None }
1385+
if let Self::Constant(v) = self { Some(*v) } else { None }
13861386
}
13871387
}
13881388

compiler/rustc_mir_build/src/builder/matches/test.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
112112
let otherwise_block = target_block(TestBranch::Failure);
113113
let switch_targets = SwitchTargets::new(
114114
target_blocks.iter().filter_map(|(&branch, &block)| {
115-
if let TestBranch::Constant(_, bits) = branch {
115+
if let TestBranch::Constant(value) = branch {
116+
let bits = value.try_to_scalar_int().unwrap().to_bits_unchecked();
116117
Some((bits, block))
117118
} else {
118119
None
@@ -279,6 +280,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
279280
};
280281

281282
if let Some(lo) = range.lo.as_finite() {
283+
let lo = ty::Value { ty: range.ty, valtree: lo };
282284
let lo = self.literal_operand(test.span, Const::from_ty_value(self.tcx, lo));
283285
self.compare(
284286
block,
@@ -292,6 +294,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
292294
};
293295

294296
if let Some(hi) = range.hi.as_finite() {
297+
let hi = ty::Value { ty: range.ty, valtree: hi };
295298
let hi = self.literal_operand(test.span, Const::from_ty_value(self.tcx, hi));
296299
let op = match range.end {
297300
RangeEnd::Included => BinOp::Le,
@@ -575,8 +578,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
575578
None
576579
} else {
577580
fully_matched = true;
578-
let bits = value.try_to_scalar_int().unwrap().to_bits_unchecked();
579-
Some(TestBranch::Constant(value, bits))
581+
Some(TestBranch::Constant(value))
580582
}
581583
}
582584
(TestKind::SwitchInt, TestCase::Range(range)) => {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
161161
format!("found bad range pattern endpoint `{expr:?}` outside of error recovery");
162162
return Err(self.tcx.dcx().span_delayed_bug(expr.span, msg));
163163
};
164-
Ok(Some(PatRangeBoundary::Finite(value)))
164+
Ok(Some(PatRangeBoundary::Finite(value.valtree)))
165165
}
166166

167167
/// Overflowing literals are linted against in a late pass. This is mostly fine, except when we
@@ -243,7 +243,8 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
243243
(RangeEnd::Included, Some(Ordering::Less)) => {}
244244
// `x..=y` where `x == y` and `x` and `y` are finite.
245245
(RangeEnd::Included, Some(Ordering::Equal)) if lo.is_finite() && hi.is_finite() => {
246-
kind = PatKind::Constant { value: lo.as_finite().unwrap() };
246+
let value = ty::Value { ty, valtree: lo.as_finite().unwrap() };
247+
kind = PatKind::Constant { value };
247248
}
248249
// `..=x` where `x == ty::MIN`.
249250
(RangeEnd::Included, Some(Ordering::Equal)) if !lo.is_finite() => {}

compiler/rustc_pattern_analysis/src/rustc.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,7 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> {
722722
match ScalarInt::try_from_uint(bits, size) {
723723
Some(scalar) => {
724724
let valtree = ty::ValTree::from_scalar_int(tcx, scalar);
725-
PatRangeBoundary::Finite(ty::Value { ty: ty.inner(), valtree })
725+
PatRangeBoundary::Finite(valtree)
726726
}
727727
// The value doesn't fit. Since `x >= 0` and 0 always encodes the minimum value
728728
// for a type, the problem isn't that the value is too small. So it must be too
@@ -742,7 +742,7 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> {
742742
"_".to_string()
743743
} else if range.is_singleton() {
744744
let lo = cx.hoist_pat_range_bdy(range.lo, ty);
745-
let value = lo.as_finite().unwrap();
745+
let value = ty::Value { ty: ty.inner(), valtree: lo.as_finite().unwrap() };
746746
value.to_string()
747747
} else {
748748
// We convert to an inclusive range for diagnostics.
@@ -756,7 +756,7 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> {
756756
// probably clear enough.
757757
let max = ty.numeric_max_val(cx.tcx).unwrap();
758758
let max = ty::ValTree::from_scalar_int(cx.tcx, max.try_to_scalar_int().unwrap());
759-
lo = PatRangeBoundary::Finite(ty::Value { ty: ty.inner(), valtree: max });
759+
lo = PatRangeBoundary::Finite(max);
760760
}
761761
let hi = if let Some(hi) = range.hi.minus_one() {
762762
hi

0 commit comments

Comments
 (0)