+/// For exhaustive integer matching, some constructors are grouped within other constructors
+/// (namely integer typed values are grouped within ranges). However, when specialising these
+/// constructors, we want to be specialising for the underlying constructors (the integers), not
+/// the groups (the ranges). Thus we need to split the groups up. Splitting them up naïvely would
+/// mean creating a separate constructor for every single value in the range, which is clearly
+/// impractical. However, observe that for some ranges of integers, the specialisation will be
+/// identical across all values in that range (i.e. there are equivalence classes of ranges of
+/// constructors based on their `is_useful_specialised` outcome). These classes are grouped by
+/// the patterns that apply to them (both in the matrix `P` and in the new row `p_{m + 1}`). We
+/// can split the range whenever the patterns that apply to that range (specifically: the patterns
+/// that *intersect* with that range) change.
+/// Our solution, therefore, is to split the range constructor into subranges at every single point
+/// the group of intersecting patterns changes, which we can compute by converting each pattern to
+/// a range and recording its endpoints, then creating subranges between each consecutive pair of
+/// endpoints.
+/// And voilà! We're testing precisely those ranges that we need to, without any exhaustive matching
+/// on actual integers. The nice thing about this is that the number of subranges is linear in the
+/// number of rows in the matrix (i.e. the number of cases in the `match` statement), so we don't
+/// need to be worried about matching over gargantuan ranges.
+fn split_grouped_constructors<'p, 'a: 'p, 'tcx: 'a>(
+ tcx: TyCtxt<'a, 'tcx, 'tcx>,
+ ctors: Vec<Constructor<'tcx>>,
+ &Matrix(ref m): &Matrix<'p, 'tcx>,
+ p: &[&'p Pattern<'tcx>],
+ ty: Ty<'tcx>,
+) -> Vec<Constructor<'tcx>> {
+ let pat = &p[0];
+
+ let mut split_ctors = Vec::with_capacity(ctors.len());
+
+ for ctor in ctors.into_iter() {
+ match ctor {
+ // For now, only ranges may denote groups of "subconstructors", so we only need to
+ // special-case constant ranges.
+ ConstantRange(..) => {
+ // We only care about finding all the subranges within the range of the intersection
+ // of the new pattern `p_({m + 1},1)` (here `pat`) and the constructor range.
+ // Anything else is irrelevant, because it is guaranteed to result in `NotUseful`,
+ // which is the default case anyway, and can be ignored.
+ let mut ctor_range = IntRange::from_ctor(tcx, &ctor).unwrap();
+ if let Some(pat_range) = IntRange::from_pat(tcx, pat) {
+ if let Some(new_range) = ctor_range.intersection(&pat_range) {
+ ctor_range = new_range;
+ } else {
+ // If the intersection between `pat` and the constructor is empty, the
+ // entire range is `NotUseful`.
+ continue;
+ }
+ } else {
+ match pat.kind {
+ box PatternKind::Wild => {
+ // A wild pattern matches the entire range of values,
+ // so the current values are fine.
+ }
+ // If the pattern is not a value (i.e. a degenerate range), a range or a
+ // wildcard (which stands for the entire range), then it's guaranteed to
+ // be `NotUseful`.
+ _ => continue,
+ }
+ }
+ // We're going to collect all the endpoints in the new pattern so we can create
+ // subranges between them.
+ let mut points = FxHashSet::default();
+ let (lo, hi) = (*ctor_range.range.start(), *ctor_range.range.end());
+ points.insert(lo);
+ points.insert(hi);
+ // We're going to iterate through every row pattern, adding endpoints in.
+ for row in m.iter() {
+ if let Some(r) = IntRange::from_pat(tcx, row[0]) {
+ // We're only interested in endpoints that lie (at least partially)
+ // within the subrange domain.
+ if let Some(r) = ctor_range.intersection(&r) {
+ let (r_lo, r_hi) = r.range.into_inner();
+ // Insert the endpoints.
+ points.insert(r_lo);
+ points.insert(r_hi);
+ // There's a slight subtlety here, which involves the fact we're using
+ // inclusive ranges everywhere. When we subdivide the range into
+ // subranges, they can't overlap, or the subranges effectively
+ // coalesce. We need hard boundaries between subranges. The simplest
+ // way to do this is by adding extra "boundary points" to prevent this
+ // intersection. Technically this means we occasionally check a few more
+ // cases for usefulness than we need to (because they're part of another
+ // equivalence class), but it's still linear and very simple to verify,
+ // which is handy when it comes to matching, which can often be quite
+ // fiddly.
+ if r_lo > lo {
+ points.insert(r_lo - 1);
+ }
+ if r_hi < hi {
+ points.insert(r_hi + 1);
+ }
+ }
+ }
+ }
+
+ // The patterns were iterated in an arbitrary order (i.e. in the order the user
+ // wrote them), so we need to make sure our endpoints are sorted.
+ let mut points: Vec<_> = points.into_iter().collect();
+ points.sort();
+ let mut points = points.into_iter();
+ let mut start = points.next().unwrap();
+ // Iterate through pairs of points, adding the subranges to `split_ctors`.
+ while let Some(end) = points.next() {
+ split_ctors.push(IntRange::range_to_ctor(tcx, ty, start..=end));
+ start = end;
+ }
+ }
+ // Any other constructor can be used unchanged.
+ _ => split_ctors.push(ctor),
+ }
+ }
+
+ split_ctors
+}
+
+/// Check whether there exists any shared value in either `ctor` or `pat` by intersecting them.
+fn constructor_intersects_pattern<'p, 'a: 'p, 'tcx: 'a>(
+ tcx: TyCtxt<'a, 'tcx, 'tcx>,
+ ctor: &Constructor<'tcx>,
+ pat: &'p Pattern<'tcx>,
+) -> Option<Vec<&'p Pattern<'tcx>>> {
+ let mut integer_matching = false;
+ if let ConstantValue(value) | ConstantRange(value, _, _) = ctor {
+ if let ty::TyChar | ty::TyInt(_) | ty::TyUint(_) = value.ty.sty {
+ integer_matching = true;
+ }
+ }
+ if integer_matching {
+ match (IntRange::from_ctor(tcx, ctor), IntRange::from_pat(tcx, pat)) {
+ (Some(ctor), Some(pat)) => ctor.intersection(&pat).map(|_| vec![]),
+ _ => None,
+ }
+ } else {
+ // Fallback for non-ranges and ranges that involve floating-point numbers, which are not
+ // conveniently handled by `IntRange`. For these cases, the constructor may not be a range
+ // so intersection actually devolves into being covered by the pattern.
+ match constructor_covered_by_range(tcx, ctor, pat) {
+ Ok(true) => Some(vec![]),
+ Ok(false) | Err(ErrorReported) => None,
+ }
+ }
+}
+