Skip to content

Commit 72cc4bd

Browse files
committed
Inline encode and decode methods
1 parent be12b24 commit 72cc4bd

File tree

1 file changed

+27
-36
lines changed

1 file changed

+27
-36
lines changed

src/librustc_mir/hair/pattern/_match.rs

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ impl<'tcx> IntRange<'tcx> {
627627
if let Some(hi) = hi.assert_bits(ty) {
628628
// Perform a shift if the underlying types are signed,
629629
// which makes the interval arithmetic simpler.
630-
let (lo, hi) = Self::encode(tcx, ty, lo..=hi);
630+
let (lo, hi) = Self::encode(tcx, ty, (lo, hi));
631631
// Make sure the interval is well-formed.
632632
return if lo > hi || lo == hi && *end == RangeEnd::Excluded {
633633
None
@@ -642,7 +642,7 @@ impl<'tcx> IntRange<'tcx> {
642642
ConstantValue(val) => {
643643
let ty = val.ty;
644644
if let Some(val) = val.assert_bits(ty) {
645-
let (lo, hi) = Self::encode(tcx, ty, val..=val);
645+
let (lo, hi) = Self::encode(tcx, ty, (val, val));
646646
Some(IntRange { range: lo..=hi, ty })
647647
} else {
648648
None
@@ -654,50 +654,44 @@ impl<'tcx> IntRange<'tcx> {
654654
}
655655
}
656656

657-
fn convert(tcx: TyCtxt<'_, 'tcx, 'tcx>,
658-
ty: Ty<'tcx>,
659-
range: RangeInclusive<u128>,
660-
encode: bool)
661-
-> (u128, u128) {
662-
// We ensure that all integer values are contiguous: that is, that their
663-
// minimum value is represented by 0, so that comparisons and increments/
664-
// decrements on interval endpoints work consistently whether the endpoints
665-
// are signed or unsigned.
666-
let (lo, hi) = range.into_inner();
657+
fn encode(tcx: TyCtxt<'_, 'tcx, 'tcx>,
658+
ty: Ty<'tcx>,
659+
(lo, hi): (u128, u128))
660+
-> (u128, u128) {
667661
match ty.sty {
668662
ty::TyInt(_) => {
669663
// FIXME(49937): refactor these bit manipulations into interpret.
670664
let bits = tcx.layout_of(ty::ParamEnv::reveal_all().and(ty))
671665
.unwrap().size.bits() as u128;
672666
let min = 1u128 << (bits - 1);
673667
let mask = !0u128 >> (128 - bits);
674-
if encode {
675-
let offset = |x: u128| x.wrapping_sub(min) & mask;
676-
(offset(lo), offset(hi))
677-
} else {
678-
let offset = |x: u128| x.wrapping_add(min) & mask;
679-
(offset(lo), offset(hi))
680-
}
668+
let offset = |x: u128| x.wrapping_sub(min) & mask;
669+
(offset(lo), offset(hi))
681670
}
682-
ty::TyUint(_) | ty::TyChar => {
683-
(lo, hi)
684-
}
685-
_ => bug!("`IntRange` should only contain integer types")
671+
_ => (lo, hi)
686672
}
687673
}
688674

689-
fn encode(tcx: TyCtxt<'_, 'tcx, 'tcx>,
690-
ty: Ty<'tcx>,
691-
range: RangeInclusive<u128>)
692-
-> (u128, u128) {
693-
Self::convert(tcx, ty, range, true)
694-
}
695-
696675
fn decode(tcx: TyCtxt<'_, 'tcx, 'tcx>,
697676
ty: Ty<'tcx>,
698677
range: RangeInclusive<u128>)
699-
-> (u128, u128) {
700-
Self::convert(tcx, ty, range, false)
678+
-> Constructor<'tcx> {
679+
let (lo, hi) = range.into_inner();
680+
let (lo, hi) = match ty.sty {
681+
ty::TyInt(_) => {
682+
// FIXME(49937): refactor these bit manipulations into interpret.
683+
let bits = tcx.layout_of(ty::ParamEnv::reveal_all().and(ty))
684+
.unwrap().size.bits() as u128;
685+
let min = 1u128 << (bits - 1);
686+
let mask = !0u128 >> (128 - bits);
687+
let offset = |x: u128| x.wrapping_add(min) & mask;
688+
(offset(lo), offset(hi))
689+
}
690+
_ => (lo, hi)
691+
};
692+
ConstantRange(ty::Const::from_bits(tcx, lo, ty),
693+
ty::Const::from_bits(tcx, hi, ty),
694+
RangeEnd::Included)
701695
}
702696

703697
fn into_inner(self) -> (u128, u128) {
@@ -739,10 +733,7 @@ fn ranges_subtract_pattern<'a, 'tcx>(cx: &mut MatchCheckCtxt<'a, 'tcx>,
739733
}
740734
// Convert the remaining ranges from pairs to inclusive `ConstantRange`s.
741735
remaining_ranges.into_iter().map(|r| {
742-
let (lo, hi) = IntRange::decode(cx.tcx, ty, r);
743-
ConstantRange(ty::Const::from_bits(cx.tcx, lo, ty),
744-
ty::Const::from_bits(cx.tcx, hi, ty),
745-
RangeEnd::Included)
736+
IntRange::decode(cx.tcx, ty, r)
746737
}).collect()
747738
} else {
748739
ranges

0 commit comments

Comments
 (0)