Skip to content

Commit

Permalink
Inline RangeInclusive into IntRange
Browse files Browse the repository at this point in the history
  • Loading branch information
Nadrieril committed Oct 21, 2023
1 parent e48f4c6 commit 39a20f7
Showing 1 changed file with 27 additions and 35 deletions.
62 changes: 27 additions & 35 deletions compiler/rustc_mir_build/src/thir/pattern/deconstruct_pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ use std::cell::Cell;
use std::cmp::{self, max, min, Ordering};
use std::fmt;
use std::iter::once;
use std::ops::RangeInclusive;

use smallvec::{smallvec, SmallVec};

Expand Down Expand Up @@ -104,9 +103,10 @@ enum Presence {
///
/// `IntRange` is never used to encode an empty range or a "range" that wraps
/// around the (offset) space: i.e., `range.lo <= range.hi`.
#[derive(Clone, PartialEq, Eq)]
#[derive(Clone, Copy, PartialEq, Eq)]
pub(crate) struct IntRange {
range: RangeInclusive<u128>,
lo: u128,
hi: u128,
}

impl IntRange {
Expand All @@ -116,20 +116,16 @@ impl IntRange {
}

fn is_singleton(&self) -> bool {
self.range.start() == self.range.end()
}

fn boundaries(&self) -> (u128, u128) {
(*self.range.start(), *self.range.end())
self.lo == self.hi
}

#[inline]
fn from_bits<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, bits: u128) -> IntRange {
let bias = IntRange::signed_bias(tcx, ty);
// Perform a shift if the underlying types are signed,
// which makes the interval arithmetic simpler.
// Perform a shift if the underlying types are signed, which makes the interval arithmetic
// type-independent.
let val = bits ^ bias;
IntRange { range: val..=val }
IntRange { lo: val, hi: val }
}

#[inline]
Expand All @@ -140,16 +136,17 @@ impl IntRange {
ty: Ty<'tcx>,
end: RangeEnd,
) -> IntRange {
// Perform a shift if the underlying types are signed,
// which makes the interval arithmetic simpler.
// Perform a shift if the underlying types are signed, which makes the interval arithmetic
// type-independent.
let bias = IntRange::signed_bias(tcx, ty);
let (lo, hi) = (lo ^ bias, hi ^ bias);
let offset = (end == RangeEnd::Excluded) as u128;
if lo > hi || (lo == hi && end == RangeEnd::Excluded) {
let hi = hi - offset;
if lo > hi {
// This should have been caught earlier by E0030.
bug!("malformed range pattern: {}..={}", lo, (hi - offset));
bug!("malformed range pattern: {lo}..={hi}");
}
IntRange { range: lo..=(hi - offset) }
IntRange { lo, hi }
}

// The return value of `signed_bias` should be XORed with an endpoint to encode/decode it.
Expand All @@ -164,14 +161,12 @@ impl IntRange {
}

fn is_subrange(&self, other: &Self) -> bool {
other.range.start() <= self.range.start() && self.range.end() <= other.range.end()
other.lo <= self.lo && self.hi <= other.hi
}

fn intersection(&self, other: &Self) -> Option<Self> {
let (lo, hi) = self.boundaries();
let (other_lo, other_hi) = other.boundaries();
if lo <= other_hi && other_lo <= hi {
Some(IntRange { range: max(lo, other_lo)..=min(hi, other_hi) })
if self.lo <= other.hi && other.lo <= self.hi {
Some(IntRange { lo: max(self.lo, other.lo), hi: min(self.hi, other.hi) })
} else {
None
}
Expand All @@ -189,9 +184,9 @@ impl IntRange {
// `true` in the following cases:
// 1 ------- // 1 -------
// 2 -------- // 2 -------
let (lo, hi) = self.boundaries();
let (other_lo, other_hi) = other.boundaries();
(lo == other_hi || hi == other_lo) && !self.is_singleton() && !other.is_singleton()
(self.lo == other.hi || self.hi == other.lo)
&& !self.is_singleton()
&& !other.is_singleton()
}

/// Partition a range of integers into disjoint subranges. This does constructor splitting for
Expand Down Expand Up @@ -235,9 +230,8 @@ impl IntRange {

fn unpack_intrange(range: IntRange) -> [IntBoundary; 2] {
use IntBoundary::*;
let (lo, hi) = range.boundaries();
let lo = JustBefore(lo);
let hi = match hi.checked_add(1) {
let lo = JustBefore(range.lo);
let hi = match range.hi.checked_add(1) {
Some(m) => JustBefore(m),
None => AfterMax,
};
Expand Down Expand Up @@ -283,21 +277,19 @@ impl IntRange {
use IntBoundary::*;
use Presence::*;
let presence = if paren_count > 0 { Seen } else { Unseen };
let range = match (prev_bdy, bdy) {
(JustBefore(n), JustBefore(m)) if n < m => n..=(m - 1),
(JustBefore(n), AfterMax) => n..=u128::MAX,
let (lo, hi) = match (prev_bdy, bdy) {
(JustBefore(n), JustBefore(m)) if n < m => (n, m - 1),
(JustBefore(n), AfterMax) => (n, u128::MAX),
_ => unreachable!(), // Ruled out by the sorting and filtering we did
};
(presence, IntRange { range })
(presence, IntRange { lo, hi })
})
}

/// Only used for displaying the range.
fn to_pat<'tcx>(&self, tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Pat<'tcx> {
let (lo, hi) = self.boundaries();

let bias = IntRange::signed_bias(tcx, ty);
let (lo_bits, hi_bits) = (lo ^ bias, hi ^ bias);
let (lo_bits, hi_bits) = (self.lo ^ bias, self.hi ^ bias);

let env = ty::ParamEnv::empty().and(ty);
let lo_const = mir::Const::from_bits(tcx, lo_bits, env);
Expand Down Expand Up @@ -367,7 +359,7 @@ impl IntRange {
/// first.
impl fmt::Debug for IntRange {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let (lo, hi) = self.boundaries();
let (lo, hi) = (self.lo, self.hi);
write!(f, "{lo}")?;
write!(f, "{}", RangeEnd::Included)?;
write!(f, "{hi}")
Expand Down

0 comments on commit 39a20f7

Please sign in to comment.