2023-12-11 11:53:01 +00:00
|
|
|
//! As explained in [`crate::usefulness`], values and patterns are made from constructors applied to
|
2023-12-10 19:42:30 +00:00
|
|
|
//! fields. This file defines a `Constructor` enum and various operations to manipulate them.
|
2020-12-20 13:29:39 +00:00
|
|
|
//!
|
2023-10-23 06:19:10 +00:00
|
|
|
//! There are two important bits of core logic in this file: constructor inclusion and constructor
|
|
|
|
//! splitting. Constructor inclusion, i.e. whether a constructor is included in/covered by another,
|
|
|
|
//! is straightforward and defined in [`Constructor::is_covered_by`].
|
2020-12-20 13:29:39 +00:00
|
|
|
//!
|
2023-12-11 11:53:01 +00:00
|
|
|
//! Constructor splitting is mentioned in [`crate::usefulness`] but not detailed. We describe it
|
2023-10-23 06:19:10 +00:00
|
|
|
//! precisely here.
|
2020-12-20 13:29:39 +00:00
|
|
|
//!
|
|
|
|
//!
|
2023-10-29 18:18:18 +00:00
|
|
|
//!
|
2023-10-23 06:19:10 +00:00
|
|
|
//! # Constructor grouping and splitting
|
|
|
|
//!
|
2023-12-11 11:53:01 +00:00
|
|
|
//! As explained in the corresponding section in [`crate::usefulness`], to make usefulness tractable
|
2023-10-23 06:19:10 +00:00
|
|
|
//! we need to group together constructors that have the same effect when they are used to
|
|
|
|
//! specialize the matrix.
|
|
|
|
//!
|
|
|
|
//! Example:
|
2022-04-15 22:04:34 +00:00
|
|
|
//! ```compile_fail,E0004
|
2020-12-20 13:29:39 +00:00
|
|
|
//! match (0, false) {
|
2023-10-23 06:19:10 +00:00
|
|
|
//! (0 ..=100, true) => {}
|
|
|
|
//! (50..=150, false) => {}
|
|
|
|
//! (0 ..=200, _) => {}
|
2020-12-20 13:29:39 +00:00
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
//!
|
2023-10-23 06:19:10 +00:00
|
|
|
//! In this example we can restrict specialization to 5 cases: `0..50`, `50..=100`, `101..=150`,
|
|
|
|
//! `151..=200` and `200..`.
|
|
|
|
//!
|
2023-12-11 11:53:01 +00:00
|
|
|
//! In [`crate::usefulness`], we had said that `specialize` only takes value-only constructors. We
|
2023-10-23 06:19:10 +00:00
|
|
|
//! now relax this restriction: we allow `specialize` to take constructors like `0..50` as long as
|
|
|
|
//! we're careful to only do that with constructors that make sense. For example, `specialize(0..50,
|
|
|
|
//! (0..=100, true))` is sensible, but `specialize(50..=200, (0..=100, true))` is not.
|
|
|
|
//!
|
|
|
|
//! Constructor splitting looks at the constructors in the first column of the matrix and constructs
|
|
|
|
//! such a sensible set of constructors. Formally, we want to find a smallest disjoint set of
|
|
|
|
//! constructors:
|
|
|
|
//! - Whose union covers the whole type, and
|
|
|
|
//! - That have no non-trivial intersection with any of the constructors in the column (i.e. they're
|
|
|
|
//! each either disjoint with or covered by any given column constructor).
|
|
|
|
//!
|
2023-12-11 19:01:02 +00:00
|
|
|
//! We compute this in two steps: first [`MatchCx::ctors_for_ty`] determines the
|
2023-12-11 11:53:01 +00:00
|
|
|
//! set of all possible constructors for the type. Then [`ConstructorSet::split`] looks at the
|
|
|
|
//! column of constructors and splits the set into groups accordingly. The precise invariants of
|
2023-10-23 06:19:10 +00:00
|
|
|
//! [`ConstructorSet::split`] is described in [`SplitConstructorSet`].
|
|
|
|
//!
|
|
|
|
//! Constructor splitting has two interesting special cases: integer range splitting (see
|
|
|
|
//! [`IntRange::split`]) and slice splitting (see [`Slice::split`]).
|
2020-12-20 13:29:39 +00:00
|
|
|
//!
|
|
|
|
//!
|
2023-10-29 18:18:18 +00:00
|
|
|
//!
|
2023-10-23 06:19:10 +00:00
|
|
|
//! # The `Missing` constructor
|
|
|
|
//!
|
|
|
|
//! We detail a special case of constructor splitting that is a bit subtle. Take the following:
|
|
|
|
//!
|
|
|
|
//! ```
|
|
|
|
//! enum Direction { North, South, East, West }
|
|
|
|
//! # let wind = (Direction::North, 0u8);
|
|
|
|
//! match wind {
|
|
|
|
//! (Direction::North, 50..) => {}
|
|
|
|
//! (_, _) => {}
|
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! Here we expect constructor splitting to output two cases: `North`, and "everything else". This
|
|
|
|
//! "everything else" is represented by [`Constructor::Missing`]. Unlike other constructors, it's a
|
|
|
|
//! bit contextual: to know the exact list of constructors it represents we have to look at the
|
|
|
|
//! column. In practice however we don't need to, because by construction it only matches rows that
|
|
|
|
//! have wildcards. This is how this constructor is special: the only constructor that covers it is
|
|
|
|
//! `Wildcard`.
|
|
|
|
//!
|
|
|
|
//! The only place where we care about which constructors `Missing` represents is in diagnostics
|
2023-12-11 11:53:01 +00:00
|
|
|
//! (see `crate::usefulness::WitnessMatrix::apply_constructor`).
|
2023-10-23 06:19:10 +00:00
|
|
|
//!
|
2023-11-05 14:00:46 +00:00
|
|
|
//! We choose whether to specialize with `Missing` in
|
2023-12-11 11:53:01 +00:00
|
|
|
//! `crate::usefulness::compute_exhaustiveness_and_usefulness`.
|
2023-10-23 06:19:10 +00:00
|
|
|
//!
|
2020-12-20 13:29:39 +00:00
|
|
|
//!
|
2023-10-30 23:40:41 +00:00
|
|
|
//!
|
2023-10-29 18:18:18 +00:00
|
|
|
//! ## Empty types, empty constructors, and the `exhaustive_patterns` feature
|
|
|
|
//!
|
|
|
|
//! An empty type is a type that has no valid value, like `!`, `enum Void {}`, or `Result<!, !>`.
|
|
|
|
//! They require careful handling.
|
|
|
|
//!
|
|
|
|
//! First, for soundness reasons related to the possible existence of invalid values, by default we
|
|
|
|
//! don't treat empty types as empty. We force them to be matched with wildcards. Except if the
|
|
|
|
//! `exhaustive_patterns` feature is turned on, in which case we do treat them as empty. And also
|
|
|
|
//! except if the type has no constructors (like `enum Void {}` but not like `Result<!, !>`), we
|
|
|
|
//! specifically allow `match void {}` to be exhaustive. There are additionally considerations of
|
2023-12-11 11:53:01 +00:00
|
|
|
//! place validity that are handled in `crate::usefulness`. Yes this is a bit tricky.
|
2023-10-29 18:18:18 +00:00
|
|
|
//!
|
|
|
|
//! The second thing is that regardless of the above, it is always allowed to use all the
|
|
|
|
//! constructors of a type. For example, all the following is ok:
|
|
|
|
//!
|
|
|
|
//! ```rust,ignore(example)
|
|
|
|
//! # #![feature(never_type)]
|
|
|
|
//! # #![feature(exhaustive_patterns)]
|
|
|
|
//! fn foo(x: Option<!>) {
|
|
|
|
//! match x {
|
|
|
|
//! None => {}
|
|
|
|
//! Some(_) => {}
|
|
|
|
//! }
|
|
|
|
//! }
|
|
|
|
//! fn bar(x: &[!]) -> u32 {
|
|
|
|
//! match x {
|
|
|
|
//! [] => 1,
|
|
|
|
//! [_] => 2,
|
|
|
|
//! [_, _] => 3,
|
|
|
|
//! }
|
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! Moreover, take the following:
|
|
|
|
//!
|
|
|
|
//! ```rust
|
|
|
|
//! # #![feature(never_type)]
|
|
|
|
//! # #![feature(exhaustive_patterns)]
|
|
|
|
//! # let x = None::<!>;
|
|
|
|
//! match x {
|
|
|
|
//! None => {}
|
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! On a normal type, we would identify `Some` as missing and tell the user. If `x: Option<!>`
|
|
|
|
//! however (and `exhaustive_patterns` is on), it's ok to omit `Some`. When listing the constructors
|
|
|
|
//! of a type, we must therefore track which can be omitted.
|
|
|
|
//!
|
|
|
|
//! Let's call "empty" a constructor that matches no valid value for the type, like `Some` for the
|
|
|
|
//! type `Option<!>`. What this all means is that `ConstructorSet` must know which constructors are
|
|
|
|
//! empty. The difference between empty and nonempty constructors is that empty constructors need
|
|
|
|
//! not be present for the match to be exhaustive.
|
|
|
|
//!
|
|
|
|
//! A final remark: empty constructors of arity 0 break specialization, we must avoid them. The
|
|
|
|
//! reason is that if we specialize by them, nothing remains to witness the emptiness; the rest of
|
|
|
|
//! the algorithm can't distinguish them from a nonempty constructor. The only known case where this
|
|
|
|
//! could happen is the `[..]` pattern on `[!; N]` with `N > 0` so we must take care to not emit it.
|
|
|
|
//!
|
2023-12-11 19:01:02 +00:00
|
|
|
//! This is all handled by [`MatchCx::ctors_for_ty`] and
|
2023-12-11 11:53:01 +00:00
|
|
|
//! [`ConstructorSet::split`]. The invariants of [`SplitConstructorSet`] are also of interest.
|
2023-10-29 18:18:18 +00:00
|
|
|
//!
|
|
|
|
//!
|
|
|
|
//!
|
2023-10-30 23:40:41 +00:00
|
|
|
//! ## Opaque patterns
|
|
|
|
//!
|
2023-10-23 06:19:10 +00:00
|
|
|
//! Some patterns, such as constants that are not allowed to be matched structurally, cannot be
|
|
|
|
//! inspected, which we handle with `Constructor::Opaque`. Since we know nothing of these patterns,
|
|
|
|
//! we assume they never cover each other. In order to respect the invariants of
|
|
|
|
//! [`SplitConstructorSet`], we give each `Opaque` constructor a unique id so we can recognize it.
|
2020-12-20 13:29:39 +00:00
|
|
|
|
2022-11-18 09:18:32 +00:00
|
|
|
use std::cmp::{self, max, min, Ordering};
|
|
|
|
use std::fmt;
|
2022-08-09 00:14:43 +00:00
|
|
|
use std::iter::once;
|
2020-11-21 23:13:32 +00:00
|
|
|
|
2023-12-10 19:42:30 +00:00
|
|
|
use smallvec::SmallVec;
|
2020-11-21 23:13:32 +00:00
|
|
|
|
2023-09-29 16:33:07 +00:00
|
|
|
use rustc_apfloat::ieee::{DoubleS, IeeeFloat, SingleS};
|
2023-10-03 13:30:05 +00:00
|
|
|
use rustc_data_structures::fx::FxHashSet;
|
2023-10-21 16:08:09 +00:00
|
|
|
use rustc_hir::RangeEnd;
|
2023-12-10 19:42:30 +00:00
|
|
|
use rustc_index::IndexVec;
|
2020-11-21 23:13:32 +00:00
|
|
|
|
2022-11-18 09:18:32 +00:00
|
|
|
use self::Constructor::*;
|
2023-10-12 17:47:33 +00:00
|
|
|
use self::MaybeInfiniteInt::*;
|
2022-11-18 09:18:32 +00:00
|
|
|
use self::SliceKind::*;
|
|
|
|
|
2023-12-10 21:14:00 +00:00
|
|
|
use crate::usefulness::PatCtxt;
|
2023-12-11 19:01:02 +00:00
|
|
|
use crate::MatchCx;
|
2021-09-25 23:00:08 +00:00
|
|
|
|
2023-10-03 13:30:05 +00:00
|
|
|
/// Whether we have seen a constructor in the column or not.
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
|
|
|
|
enum Presence {
|
|
|
|
Unseen,
|
|
|
|
Seen,
|
|
|
|
}
|
|
|
|
|
2023-10-12 17:47:33 +00:00
|
|
|
/// A possibly infinite integer. Values are encoded such that the ordering on `u128` matches the
|
|
|
|
/// natural order on the original type. For example, `-128i8` is encoded as `0` and `127i8` as
|
|
|
|
/// `255`. See `signed_bias` for details.
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
|
2023-12-10 19:42:30 +00:00
|
|
|
pub enum MaybeInfiniteInt {
|
2023-10-12 17:47:33 +00:00
|
|
|
NegInfinity,
|
|
|
|
/// Encoded value. DO NOT CONSTRUCT BY HAND; use `new_finite`.
|
2023-12-11 10:10:19 +00:00
|
|
|
#[non_exhaustive]
|
2023-10-12 17:47:33 +00:00
|
|
|
Finite(u128),
|
2023-10-21 18:16:48 +00:00
|
|
|
/// The integer after `u128::MAX`. We need it to represent `x..=u128::MAX` as an exclusive range.
|
2023-10-12 17:47:33 +00:00
|
|
|
JustAfterMax,
|
|
|
|
PosInfinity,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MaybeInfiniteInt {
|
2023-12-11 10:10:19 +00:00
|
|
|
pub fn new_finite_uint(bits: u128) -> Self {
|
|
|
|
Finite(bits)
|
2023-10-12 17:47:33 +00:00
|
|
|
}
|
2023-12-11 10:10:19 +00:00
|
|
|
pub fn new_finite_int(bits: u128, size: u64) -> Self {
|
2023-10-12 17:47:33 +00:00
|
|
|
// Perform a shift if the underlying types are signed, which makes the interval arithmetic
|
|
|
|
// type-independent.
|
2023-12-11 10:10:19 +00:00
|
|
|
let bias = 1u128 << (size - 1);
|
|
|
|
Finite(bits ^ bias)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn as_finite_uint(self) -> Option<u128> {
|
|
|
|
match self {
|
|
|
|
Finite(bits) => Some(bits),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn as_finite_int(self, size: u64) -> Option<u128> {
|
|
|
|
// We decode the shift.
|
|
|
|
match self {
|
|
|
|
Finite(bits) => {
|
|
|
|
let bias = 1u128 << (size - 1);
|
|
|
|
Some(bits ^ bias)
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
}
|
2023-10-12 17:47:33 +00:00
|
|
|
}
|
|
|
|
|
2023-10-21 18:16:48 +00:00
|
|
|
/// Note: this will not turn a finite value into an infinite one or vice-versa.
|
2023-12-10 19:42:30 +00:00
|
|
|
pub fn minus_one(self) -> Self {
|
2023-10-12 17:47:33 +00:00
|
|
|
match self {
|
|
|
|
Finite(n) => match n.checked_sub(1) {
|
|
|
|
Some(m) => Finite(m),
|
2023-10-21 18:16:48 +00:00
|
|
|
None => bug!(),
|
2023-10-12 17:47:33 +00:00
|
|
|
},
|
|
|
|
JustAfterMax => Finite(u128::MAX),
|
|
|
|
x => x,
|
|
|
|
}
|
|
|
|
}
|
2023-10-21 18:16:48 +00:00
|
|
|
/// Note: this will not turn a finite value into an infinite one or vice-versa.
|
2023-12-10 19:42:30 +00:00
|
|
|
pub fn plus_one(self) -> Self {
|
2023-10-12 17:47:33 +00:00
|
|
|
match self {
|
|
|
|
Finite(n) => match n.checked_add(1) {
|
|
|
|
Some(m) => Finite(m),
|
|
|
|
None => JustAfterMax,
|
|
|
|
},
|
2023-10-12 22:20:06 +00:00
|
|
|
JustAfterMax => bug!(),
|
2023-10-12 17:47:33 +00:00
|
|
|
x => x,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-21 18:16:48 +00:00
|
|
|
/// An exclusive interval, used for precise integer exhaustiveness checking. `IntRange`s always
|
2023-10-12 22:20:06 +00:00
|
|
|
/// store a contiguous range.
|
2020-11-21 23:13:32 +00:00
|
|
|
///
|
2023-10-12 22:20:06 +00:00
|
|
|
/// `IntRange` is never used to encode an empty range or a "range" that wraps around the (offset)
|
2023-10-21 18:16:48 +00:00
|
|
|
/// space: i.e., `range.lo < range.hi`.
|
2023-10-12 14:19:02 +00:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq)]
|
2023-12-10 19:42:30 +00:00
|
|
|
pub struct IntRange {
|
2023-12-10 21:14:00 +00:00
|
|
|
pub lo: MaybeInfiniteInt, // Must not be `PosInfinity`.
|
|
|
|
pub hi: MaybeInfiniteInt, // Must not be `NegInfinity`.
|
2020-11-21 23:13:32 +00:00
|
|
|
}
|
|
|
|
|
2020-11-28 22:07:15 +00:00
|
|
|
impl IntRange {
|
2023-10-12 17:47:33 +00:00
|
|
|
/// Best effort; will not know that e.g. `255u8..` is a singleton.
|
2023-12-11 09:56:21 +00:00
|
|
|
pub(crate) fn is_singleton(&self) -> bool {
|
2023-10-21 18:16:48 +00:00
|
|
|
// Since `lo` and `hi` can't be the same `Infinity` and `plus_one` never changes from finite
|
|
|
|
// to infinite, this correctly only detects ranges that contain exacly one `Finite(x)`.
|
|
|
|
self.lo.plus_one() == self.hi
|
2020-11-21 23:13:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2023-12-11 10:10:19 +00:00
|
|
|
pub fn from_singleton(x: MaybeInfiniteInt) -> IntRange {
|
2023-10-21 18:16:48 +00:00
|
|
|
IntRange { lo: x, hi: x.plus_one() }
|
2020-11-21 23:13:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2023-12-10 19:42:30 +00:00
|
|
|
pub fn from_range(lo: MaybeInfiniteInt, mut hi: MaybeInfiniteInt, end: RangeEnd) -> IntRange {
|
2023-10-21 18:16:48 +00:00
|
|
|
if end == RangeEnd::Included {
|
|
|
|
hi = hi.plus_one();
|
2023-10-12 17:47:33 +00:00
|
|
|
}
|
2023-10-21 18:16:48 +00:00
|
|
|
if lo >= hi {
|
2023-09-29 17:44:12 +00:00
|
|
|
// This should have been caught earlier by E0030.
|
2023-10-21 18:16:48 +00:00
|
|
|
bug!("malformed range pattern: {lo:?}..{hi:?}");
|
2023-09-29 17:44:12 +00:00
|
|
|
}
|
2023-10-12 14:19:02 +00:00
|
|
|
IntRange { lo, hi }
|
2020-11-21 23:13:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn is_subrange(&self, other: &Self) -> bool {
|
2023-10-12 14:19:02 +00:00
|
|
|
other.lo <= self.lo && self.hi <= other.hi
|
2020-11-21 23:13:32 +00:00
|
|
|
}
|
|
|
|
|
2020-11-28 21:23:38 +00:00
|
|
|
fn intersection(&self, other: &Self) -> Option<Self> {
|
2023-10-21 18:16:48 +00:00
|
|
|
if self.lo < other.hi && other.lo < self.hi {
|
2023-10-12 14:19:02 +00:00
|
|
|
Some(IntRange { lo: max(self.lo, other.lo), hi: min(self.hi, other.hi) })
|
2020-11-21 23:13:32 +00:00
|
|
|
} else {
|
2020-11-28 21:23:38 +00:00
|
|
|
None
|
2020-11-21 23:13:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-03 13:17:52 +00:00
|
|
|
/// Partition a range of integers into disjoint subranges. This does constructor splitting for
|
|
|
|
/// integer ranges as explained at the top of the file.
|
|
|
|
///
|
|
|
|
/// This returns an output that covers `self`. The output is split so that the only
|
|
|
|
/// intersections between an output range and a column range are inclusions. No output range
|
|
|
|
/// straddles the boundary of one of the inputs.
|
|
|
|
///
|
2023-10-04 13:59:16 +00:00
|
|
|
/// Additionally, we track for each output range whether it is covered by one of the column ranges or not.
|
|
|
|
///
|
2023-10-03 13:17:52 +00:00
|
|
|
/// The following input:
|
|
|
|
/// ```text
|
2023-10-04 13:59:16 +00:00
|
|
|
/// (--------------------------) // `self`
|
|
|
|
/// (------) (----------) (-)
|
|
|
|
/// (------) (--------)
|
2023-10-03 13:17:52 +00:00
|
|
|
/// ```
|
2023-10-04 13:59:16 +00:00
|
|
|
/// is first intersected with `self`:
|
2023-10-03 13:17:52 +00:00
|
|
|
/// ```text
|
2023-10-04 13:59:16 +00:00
|
|
|
/// (--------------------------) // `self`
|
|
|
|
/// (----) (----------) (-)
|
|
|
|
/// (------) (--------)
|
2023-10-03 13:17:52 +00:00
|
|
|
/// ```
|
2023-10-04 13:59:16 +00:00
|
|
|
/// and then iterated over as follows:
|
|
|
|
/// ```text
|
|
|
|
/// (-(--)-(-)-(------)-)--(-)-
|
|
|
|
/// ```
|
|
|
|
/// where each sequence of dashes is an output range, and dashes outside parentheses are marked
|
|
|
|
/// as `Presence::Missing`.
|
2023-10-21 18:16:48 +00:00
|
|
|
///
|
|
|
|
/// ## `isize`/`usize`
|
|
|
|
///
|
|
|
|
/// Whereas a wildcard of type `i32` stands for the range `i32::MIN..=i32::MAX`, a `usize`
|
|
|
|
/// wildcard stands for `0..PosInfinity` and a `isize` wildcard stands for
|
|
|
|
/// `NegInfinity..PosInfinity`. In other words, as far as `IntRange` is concerned, there are
|
|
|
|
/// values before `isize::MIN` and after `usize::MAX`/`isize::MAX`.
|
|
|
|
/// This is to avoid e.g. `0..(u32::MAX as usize)` from being exhaustive on one architecture and
|
2023-12-04 10:22:35 +00:00
|
|
|
/// not others. This was decided in <https://github.com/rust-lang/rfcs/pull/2591>.
|
2023-10-21 18:16:48 +00:00
|
|
|
///
|
|
|
|
/// These infinities affect splitting subtly: it is possible to get `NegInfinity..0` and
|
|
|
|
/// `usize::MAX+1..PosInfinity` in the output. Diagnostics must be careful to handle these
|
|
|
|
/// fictitious ranges sensibly.
|
2023-10-03 13:17:52 +00:00
|
|
|
fn split(
|
|
|
|
&self,
|
|
|
|
column_ranges: impl Iterator<Item = IntRange>,
|
2023-10-03 13:30:05 +00:00
|
|
|
) -> impl Iterator<Item = (Presence, IntRange)> {
|
2023-10-03 13:17:52 +00:00
|
|
|
// The boundaries of ranges in `column_ranges` intersected with `self`.
|
2023-10-03 13:30:05 +00:00
|
|
|
// We do parenthesis matching for input ranges. A boundary counts as +1 if it starts
|
|
|
|
// a range and -1 if it ends it. When the count is > 0 between two boundaries, we
|
|
|
|
// are within an input range.
|
2023-10-12 17:47:33 +00:00
|
|
|
let mut boundaries: Vec<(MaybeInfiniteInt, isize)> = column_ranges
|
2023-10-03 13:17:52 +00:00
|
|
|
.filter_map(|r| self.intersection(&r))
|
2023-10-21 18:16:48 +00:00
|
|
|
.flat_map(|r| [(r.lo, 1), (r.hi, -1)])
|
2023-10-03 13:17:52 +00:00
|
|
|
.collect();
|
2023-10-04 13:59:16 +00:00
|
|
|
// We sort by boundary, and for each boundary we sort the "closing parentheses" first. The
|
|
|
|
// order of +1/-1 for a same boundary value is actually irrelevant, because we only look at
|
|
|
|
// the accumulated count between distinct boundary values.
|
2023-10-03 13:17:52 +00:00
|
|
|
boundaries.sort_unstable();
|
|
|
|
|
2023-10-04 13:59:16 +00:00
|
|
|
// Accumulate parenthesis counts.
|
|
|
|
let mut paren_counter = 0isize;
|
2023-10-03 13:17:52 +00:00
|
|
|
// Gather pairs of adjacent boundaries.
|
2023-10-21 18:16:48 +00:00
|
|
|
let mut prev_bdy = self.lo;
|
2023-10-04 13:59:16 +00:00
|
|
|
boundaries
|
|
|
|
.into_iter()
|
|
|
|
// End with the end of the range. The count is ignored.
|
2023-10-21 18:16:48 +00:00
|
|
|
.chain(once((self.hi, 0)))
|
2023-10-04 13:59:16 +00:00
|
|
|
// List pairs of adjacent boundaries and the count between them.
|
|
|
|
.map(move |(bdy, delta)| {
|
|
|
|
// `delta` affects the count as we cross `bdy`, so the relevant count between
|
|
|
|
// `prev_bdy` and `bdy` is untouched by `delta`.
|
|
|
|
let ret = (prev_bdy, paren_counter, bdy);
|
2023-10-03 13:17:52 +00:00
|
|
|
prev_bdy = bdy;
|
2023-10-04 13:59:16 +00:00
|
|
|
paren_counter += delta;
|
2023-10-03 13:17:52 +00:00
|
|
|
ret
|
|
|
|
})
|
2023-10-04 13:59:16 +00:00
|
|
|
// Skip empty ranges.
|
2023-10-03 13:30:05 +00:00
|
|
|
.filter(|&(prev_bdy, _, bdy)| prev_bdy != bdy)
|
2023-10-03 13:17:52 +00:00
|
|
|
// Convert back to ranges.
|
2023-10-03 13:30:05 +00:00
|
|
|
.map(move |(prev_bdy, paren_count, bdy)| {
|
|
|
|
use Presence::*;
|
|
|
|
let presence = if paren_count > 0 { Seen } else { Unseen };
|
2023-10-21 18:16:48 +00:00
|
|
|
let range = IntRange { lo: prev_bdy, hi: bdy };
|
2023-10-12 17:47:33 +00:00
|
|
|
(presence, range)
|
2023-10-03 13:17:52 +00:00
|
|
|
})
|
|
|
|
}
|
2020-11-21 23:13:32 +00:00
|
|
|
}
|
|
|
|
|
2023-10-12 14:51:27 +00:00
|
|
|
/// Note: this will render signed ranges incorrectly. To render properly, convert to a pattern
|
|
|
|
/// first.
|
2021-09-25 23:00:08 +00:00
|
|
|
impl fmt::Debug for IntRange {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2023-10-12 17:47:33 +00:00
|
|
|
if let Finite(lo) = self.lo {
|
|
|
|
write!(f, "{lo}")?;
|
|
|
|
}
|
2023-10-21 18:16:48 +00:00
|
|
|
write!(f, "{}", RangeEnd::Excluded)?;
|
2023-10-12 17:47:33 +00:00
|
|
|
if let Finite(hi) = self.hi {
|
|
|
|
write!(f, "{hi}")?;
|
|
|
|
}
|
|
|
|
Ok(())
|
2021-09-25 23:00:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-21 23:13:32 +00:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
2023-12-10 19:42:30 +00:00
|
|
|
pub enum SliceKind {
|
2020-11-21 23:13:32 +00:00
|
|
|
/// Patterns of length `n` (`[x, y]`).
|
2021-09-25 23:00:05 +00:00
|
|
|
FixedLen(usize),
|
2020-11-21 23:13:32 +00:00
|
|
|
/// Patterns using the `..` notation (`[x, .., y]`).
|
|
|
|
/// Captures any array constructor of `length >= i + j`.
|
|
|
|
/// In the case where `array_len` is `Some(_)`,
|
|
|
|
/// this indicates that we only care about the first `i` and the last `j` values of the array,
|
|
|
|
/// and everything in between is a wildcard `_`.
|
2021-09-25 23:00:05 +00:00
|
|
|
VarLen(usize, usize),
|
2020-11-21 23:13:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl SliceKind {
|
2021-09-25 23:00:05 +00:00
|
|
|
fn arity(self) -> usize {
|
2020-11-21 23:13:32 +00:00
|
|
|
match self {
|
|
|
|
FixedLen(length) => length,
|
|
|
|
VarLen(prefix, suffix) => prefix + suffix,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Whether this pattern includes patterns of length `other_len`.
|
2021-09-25 23:00:05 +00:00
|
|
|
fn covers_length(self, other_len: usize) -> bool {
|
2020-11-21 23:13:32 +00:00
|
|
|
match self {
|
|
|
|
FixedLen(len) => len == other_len,
|
|
|
|
VarLen(prefix, suffix) => prefix + suffix <= other_len,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A constructor for array and slice patterns.
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
2023-12-10 19:42:30 +00:00
|
|
|
pub struct Slice {
|
2020-11-21 23:13:32 +00:00
|
|
|
/// `None` if the matched value is a slice, `Some(n)` if it is an array of size `n`.
|
2023-12-10 19:42:30 +00:00
|
|
|
pub(crate) array_len: Option<usize>,
|
2020-11-21 23:13:32 +00:00
|
|
|
/// The kind of pattern it is: fixed-length `[x, y]` or variable length `[x, .., y]`.
|
2023-12-10 19:42:30 +00:00
|
|
|
pub(crate) kind: SliceKind,
|
2020-11-21 23:13:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Slice {
|
2023-12-10 19:42:30 +00:00
|
|
|
pub fn new(array_len: Option<usize>, kind: SliceKind) -> Self {
|
2020-11-21 23:13:32 +00:00
|
|
|
let kind = match (array_len, kind) {
|
2023-10-29 18:18:18 +00:00
|
|
|
// If the middle `..` has length 0, we effectively have a fixed-length pattern.
|
|
|
|
(Some(len), VarLen(prefix, suffix)) if prefix + suffix == len => FixedLen(len),
|
|
|
|
(Some(len), VarLen(prefix, suffix)) if prefix + suffix > len => bug!(
|
|
|
|
"Slice pattern of length {} longer than its array length {len}",
|
|
|
|
prefix + suffix
|
|
|
|
),
|
2020-11-21 23:13:32 +00:00
|
|
|
_ => kind,
|
|
|
|
};
|
|
|
|
Slice { array_len, kind }
|
|
|
|
}
|
|
|
|
|
2023-12-10 19:42:30 +00:00
|
|
|
pub(crate) fn arity(self) -> usize {
|
2020-11-21 23:13:32 +00:00
|
|
|
self.kind.arity()
|
|
|
|
}
|
|
|
|
|
2020-12-11 22:20:14 +00:00
|
|
|
/// See `Constructor::is_covered_by`
|
|
|
|
fn is_covered_by(self, other: Self) -> bool {
|
|
|
|
other.kind.covers_length(self.arity())
|
|
|
|
}
|
|
|
|
|
2023-10-03 12:38:40 +00:00
|
|
|
/// This computes constructor splitting for variable-length slices, as explained at the top of
|
|
|
|
/// the file.
|
|
|
|
///
|
|
|
|
/// A slice pattern `[x, .., y]` behaves like the infinite or-pattern `[x, y] | [x, _, y] | [x,
|
|
|
|
/// _, _, y] | etc`. The corresponding value constructors are fixed-length array constructors of
|
|
|
|
/// corresponding lengths. We obviously can't list this infinitude of constructors.
|
|
|
|
/// Thankfully, it turns out that for each finite set of slice patterns, all sufficiently large
|
|
|
|
/// array lengths are equivalent.
|
|
|
|
///
|
|
|
|
/// Let's look at an example, where we are trying to split the last pattern:
|
|
|
|
/// ```
|
|
|
|
/// # fn foo(x: &[bool]) {
|
|
|
|
/// match x {
|
|
|
|
/// [true, true, ..] => {}
|
|
|
|
/// [.., false, false] => {}
|
2023-10-03 13:30:05 +00:00
|
|
|
/// [..] => {}
|
2023-10-03 12:38:40 +00:00
|
|
|
/// }
|
|
|
|
/// # }
|
|
|
|
/// ```
|
|
|
|
/// Here are the results of specialization for the first few lengths:
|
|
|
|
/// ```
|
|
|
|
/// # fn foo(x: &[bool]) { match x {
|
|
|
|
/// // length 0
|
|
|
|
/// [] => {}
|
|
|
|
/// // length 1
|
|
|
|
/// [_] => {}
|
|
|
|
/// // length 2
|
|
|
|
/// [true, true] => {}
|
|
|
|
/// [false, false] => {}
|
|
|
|
/// [_, _] => {}
|
|
|
|
/// // length 3
|
|
|
|
/// [true, true, _ ] => {}
|
|
|
|
/// [_, false, false] => {}
|
|
|
|
/// [_, _, _ ] => {}
|
|
|
|
/// // length 4
|
|
|
|
/// [true, true, _, _ ] => {}
|
|
|
|
/// [_, _, false, false] => {}
|
|
|
|
/// [_, _, _, _ ] => {}
|
|
|
|
/// // length 5
|
|
|
|
/// [true, true, _, _, _ ] => {}
|
|
|
|
/// [_, _, _, false, false] => {}
|
|
|
|
/// [_, _, _, _, _ ] => {}
|
|
|
|
/// # _ => {}
|
|
|
|
/// # }}
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// We see that above length 4, we are simply inserting columns full of wildcards in the middle.
|
|
|
|
/// This means that specialization and witness computation with slices of length `l >= 4` will
|
2023-10-03 13:30:05 +00:00
|
|
|
/// give equivalent results regardless of `l`. This applies to any set of slice patterns: there
|
|
|
|
/// will be a length `L` above which all lengths behave the same. This is exactly what we need
|
|
|
|
/// for constructor splitting.
|
2023-10-03 12:38:40 +00:00
|
|
|
///
|
|
|
|
/// A variable-length slice pattern covers all lengths from its arity up to infinity. As we just
|
|
|
|
/// saw, we can split this in two: lengths below `L` are treated individually with a
|
|
|
|
/// fixed-length slice each; lengths above `L` are grouped into a single variable-length slice
|
|
|
|
/// constructor.
|
|
|
|
///
|
|
|
|
/// For each variable-length slice pattern `p` with a prefix of length `plₚ` and suffix of
|
|
|
|
/// length `slₚ`, only the first `plₚ` and the last `slₚ` elements are examined. Therefore, as
|
|
|
|
/// long as `L` is positive (to avoid concerns about empty types), all elements after the
|
|
|
|
/// maximum prefix length and before the maximum suffix length are not examined by any
|
|
|
|
/// variable-length pattern, and therefore can be ignored. This gives us a way to compute `L`.
|
|
|
|
///
|
|
|
|
/// Additionally, if fixed-length patterns exist, we must pick an `L` large enough to miss them,
|
|
|
|
/// so we can pick `L = max(max(FIXED_LEN)+1, max(PREFIX_LEN) + max(SUFFIX_LEN))`.
|
|
|
|
/// `max_slice` below will be made to have this arity `L`.
|
|
|
|
///
|
|
|
|
/// If `self` is fixed-length, it is returned as-is.
|
2023-10-03 13:30:05 +00:00
|
|
|
///
|
|
|
|
/// Additionally, we track for each output slice whether it is covered by one of the column slices or not.
|
|
|
|
fn split(
|
|
|
|
self,
|
|
|
|
column_slices: impl Iterator<Item = Slice>,
|
|
|
|
) -> impl Iterator<Item = (Presence, Slice)> {
|
2023-10-03 12:38:40 +00:00
|
|
|
// Range of lengths below `L`.
|
|
|
|
let smaller_lengths;
|
2023-10-03 13:30:05 +00:00
|
|
|
let arity = self.arity();
|
2023-10-03 12:38:40 +00:00
|
|
|
let mut max_slice = self.kind;
|
2023-10-04 13:59:16 +00:00
|
|
|
// Tracks the smallest variable-length slice we've seen. Any slice arity above it is
|
|
|
|
// therefore `Presence::Seen` in the column.
|
2023-10-03 13:30:05 +00:00
|
|
|
let mut min_var_len = usize::MAX;
|
2023-10-04 13:59:16 +00:00
|
|
|
// Tracks the fixed-length slices we've seen, to mark them as `Presence::Seen`.
|
2023-10-03 13:30:05 +00:00
|
|
|
let mut seen_fixed_lens = FxHashSet::default();
|
2023-10-03 12:38:40 +00:00
|
|
|
match &mut max_slice {
|
|
|
|
VarLen(max_prefix_len, max_suffix_len) => {
|
2023-10-29 18:18:18 +00:00
|
|
|
// A length larger than any fixed-length slice encountered.
|
|
|
|
// We start at 1 in case the subtype is empty because in that case the zero-length
|
|
|
|
// slice must be treated separately from the rest.
|
|
|
|
let mut fixed_len_upper_bound = 1;
|
2023-10-03 12:38:40 +00:00
|
|
|
// We grow `max_slice` to be larger than all slices encountered, as described above.
|
2023-10-29 18:18:18 +00:00
|
|
|
// `L` is `max_slice.arity()`. For diagnostics, we keep the prefix and suffix
|
|
|
|
// lengths separate.
|
2023-10-03 12:38:40 +00:00
|
|
|
for slice in column_slices {
|
|
|
|
match slice.kind {
|
|
|
|
FixedLen(len) => {
|
2023-10-29 18:18:18 +00:00
|
|
|
fixed_len_upper_bound = cmp::max(fixed_len_upper_bound, len + 1);
|
|
|
|
seen_fixed_lens.insert(len);
|
2023-10-03 12:38:40 +00:00
|
|
|
}
|
|
|
|
VarLen(prefix, suffix) => {
|
|
|
|
*max_prefix_len = cmp::max(*max_prefix_len, prefix);
|
|
|
|
*max_suffix_len = cmp::max(*max_suffix_len, suffix);
|
2023-10-03 13:30:05 +00:00
|
|
|
min_var_len = cmp::min(min_var_len, prefix + suffix);
|
2023-10-03 12:38:40 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-11 22:20:14 +00:00
|
|
|
}
|
2023-10-29 18:18:18 +00:00
|
|
|
// If `fixed_len_upper_bound >= L`, we set `L` to `fixed_len_upper_bound`.
|
|
|
|
if let Some(delta) =
|
|
|
|
fixed_len_upper_bound.checked_sub(*max_prefix_len + *max_suffix_len)
|
|
|
|
{
|
|
|
|
*max_prefix_len += delta
|
2020-11-21 23:13:32 +00:00
|
|
|
}
|
|
|
|
|
2023-10-03 12:38:40 +00:00
|
|
|
// We cap the arity of `max_slice` at the array size.
|
|
|
|
match self.array_len {
|
|
|
|
Some(len) if max_slice.arity() >= len => max_slice = FixedLen(len),
|
|
|
|
_ => {}
|
|
|
|
}
|
2020-11-21 23:13:32 +00:00
|
|
|
|
2023-10-03 12:38:40 +00:00
|
|
|
smaller_lengths = match self.array_len {
|
|
|
|
// The only admissible fixed-length slice is one of the array size. Whether `max_slice`
|
|
|
|
// is fixed-length or variable-length, it will be the only relevant slice to output
|
|
|
|
// here.
|
|
|
|
Some(_) => 0..0, // empty range
|
|
|
|
// We need to cover all arities in the range `(arity..infinity)`. We split that
|
|
|
|
// range into two: lengths smaller than `max_slice.arity()` are treated
|
|
|
|
// independently as fixed-lengths slices, and lengths above are captured by
|
|
|
|
// `max_slice`.
|
|
|
|
None => self.arity()..max_slice.arity(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
FixedLen(_) => {
|
2023-10-03 13:30:05 +00:00
|
|
|
// No need to split here. We only track presence.
|
|
|
|
for slice in column_slices {
|
|
|
|
match slice.kind {
|
|
|
|
FixedLen(len) => {
|
|
|
|
if len == arity {
|
|
|
|
seen_fixed_lens.insert(len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
VarLen(prefix, suffix) => {
|
|
|
|
min_var_len = cmp::min(min_var_len, prefix + suffix);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-10-03 12:38:40 +00:00
|
|
|
smaller_lengths = 0..0;
|
|
|
|
}
|
2020-12-11 22:20:14 +00:00
|
|
|
};
|
2023-10-03 13:30:05 +00:00
|
|
|
|
|
|
|
smaller_lengths.map(FixedLen).chain(once(max_slice)).map(move |kind| {
|
|
|
|
let arity = kind.arity();
|
|
|
|
let seen = if min_var_len <= arity || seen_fixed_lens.contains(&arity) {
|
|
|
|
Presence::Seen
|
|
|
|
} else {
|
|
|
|
Presence::Unseen
|
|
|
|
};
|
|
|
|
(seen, Slice::new(self.array_len, kind))
|
|
|
|
})
|
2020-11-21 23:13:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-30 23:40:41 +00:00
|
|
|
/// A globally unique id to distinguish `Opaque` patterns.
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
2023-12-10 19:42:30 +00:00
|
|
|
pub struct OpaqueId(u32);
|
2023-10-30 23:40:41 +00:00
|
|
|
|
|
|
|
impl OpaqueId {
|
2023-12-10 19:42:30 +00:00
|
|
|
pub fn new() -> Self {
|
2023-10-30 23:40:41 +00:00
|
|
|
use std::sync::atomic::{AtomicU32, Ordering};
|
|
|
|
static OPAQUE_ID: AtomicU32 = AtomicU32::new(0);
|
|
|
|
OpaqueId(OPAQUE_ID.fetch_add(1, Ordering::SeqCst))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-21 23:13:32 +00:00
|
|
|
/// A value can be decomposed into a constructor applied to some fields. This struct represents
|
|
|
|
/// the constructor. See also `Fields`.
|
|
|
|
///
|
|
|
|
/// `pat_constructor` retrieves the constructor corresponding to a pattern.
|
|
|
|
/// `specialize_constructor` returns the list of fields corresponding to a pattern, given a
|
|
|
|
/// constructor. `Constructor::apply` reconstructs the pattern from a pair of `Constructor` and
|
|
|
|
/// `Fields`.
|
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
2023-12-11 19:01:02 +00:00
|
|
|
pub enum Constructor<Cx: MatchCx> {
|
2023-12-11 12:32:34 +00:00
|
|
|
/// Tuples and structs.
|
|
|
|
Struct,
|
2020-11-21 23:13:32 +00:00
|
|
|
/// Enum variants.
|
2023-12-11 19:01:02 +00:00
|
|
|
Variant(Cx::VariantIdx),
|
2023-12-11 12:32:34 +00:00
|
|
|
/// References
|
|
|
|
Ref,
|
|
|
|
/// Array and slice patterns.
|
|
|
|
Slice(Slice),
|
|
|
|
/// Union field accesses.
|
|
|
|
UnionField,
|
2023-10-12 14:51:27 +00:00
|
|
|
/// Booleans
|
|
|
|
Bool(bool),
|
2020-11-21 23:13:32 +00:00
|
|
|
/// Ranges of integer literal values (`2`, `2..=5` or `2..5`).
|
2020-11-28 22:07:15 +00:00
|
|
|
IntRange(IntRange),
|
2020-11-21 23:13:32 +00:00
|
|
|
/// Ranges of floating-point literal values (`2.0..=5.2`).
|
2023-09-29 16:33:07 +00:00
|
|
|
F32Range(IeeeFloat<SingleS>, IeeeFloat<SingleS>, RangeEnd),
|
|
|
|
F64Range(IeeeFloat<DoubleS>, IeeeFloat<DoubleS>, RangeEnd),
|
2020-11-21 23:13:32 +00:00
|
|
|
/// String literals. Strings are not quite the same as `&[u8]` so we treat them separately.
|
2023-12-11 19:01:02 +00:00
|
|
|
Str(Cx::StrLit),
|
2023-10-30 23:40:41 +00:00
|
|
|
/// Constants that must not be matched structurally. They are treated as black boxes for the
|
|
|
|
/// purposes of exhaustiveness: we must not inspect them, and they don't count towards making a
|
|
|
|
/// match exhaustive.
|
|
|
|
/// Carries an id that must be unique within a match. We need this to ensure the invariants of
|
|
|
|
/// [`SplitConstructorSet`].
|
|
|
|
Opaque(OpaqueId),
|
2023-10-03 15:09:20 +00:00
|
|
|
/// Or-pattern.
|
|
|
|
Or,
|
|
|
|
/// Wildcard pattern.
|
|
|
|
Wildcard,
|
2020-11-21 23:13:32 +00:00
|
|
|
/// Fake extra constructor for enums that aren't allowed to be matched exhaustively. Also used
|
|
|
|
/// for those types for which we cannot list constructors explicitly, like `f64` and `str`.
|
|
|
|
NonExhaustive,
|
2023-10-03 15:09:20 +00:00
|
|
|
/// Fake extra constructor for variants that should not be mentioned in diagnostics.
|
|
|
|
/// We use this for variants behind an unstable gate as well as
|
|
|
|
/// `#[doc(hidden)]` ones.
|
|
|
|
Hidden,
|
2023-10-23 06:19:10 +00:00
|
|
|
/// Fake extra constructor for constructors that are not seen in the matrix, as explained at the
|
|
|
|
/// top of the file.
|
2023-10-14 16:25:10 +00:00
|
|
|
Missing,
|
2020-11-21 23:13:32 +00:00
|
|
|
}
|
|
|
|
|
2023-12-11 19:01:02 +00:00
|
|
|
impl<Cx: MatchCx> Constructor<Cx> {
|
2023-12-11 09:56:21 +00:00
|
|
|
pub(crate) fn is_non_exhaustive(&self) -> bool {
|
2021-09-10 20:45:04 +00:00
|
|
|
matches!(self, NonExhaustive)
|
|
|
|
}
|
|
|
|
|
2023-12-11 19:01:02 +00:00
|
|
|
pub(crate) fn as_variant(&self) -> Option<Cx::VariantIdx> {
|
2023-10-03 13:30:05 +00:00
|
|
|
match self {
|
|
|
|
Variant(i) => Some(*i),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2023-10-12 14:51:27 +00:00
|
|
|
fn as_bool(&self) -> Option<bool> {
|
|
|
|
match self {
|
|
|
|
Bool(b) => Some(*b),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2023-12-11 09:56:21 +00:00
|
|
|
pub(crate) fn as_int_range(&self) -> Option<&IntRange> {
|
2020-11-21 23:13:32 +00:00
|
|
|
match self {
|
|
|
|
IntRange(range) => Some(range),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn as_slice(&self) -> Option<Slice> {
|
|
|
|
match self {
|
|
|
|
Slice(slice) => Some(*slice),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-25 23:00:08 +00:00
|
|
|
/// The number of fields for this constructor. This must be kept in sync with
|
|
|
|
/// `Fields::wildcards`.
|
2023-12-11 19:01:02 +00:00
|
|
|
pub(crate) fn arity(&self, pcx: &PatCtxt<'_, '_, Cx>) -> usize {
|
2023-12-10 21:14:00 +00:00
|
|
|
pcx.cx.ctor_arity(self, pcx.ty)
|
2020-11-21 23:26:53 +00:00
|
|
|
}
|
|
|
|
|
2020-11-21 23:13:32 +00:00
|
|
|
/// Returns whether `self` is covered by `other`, i.e. whether `self` is a subset of `other`.
|
|
|
|
/// For the simple cases, this is simply checking for equality. For the "grouped" constructors,
|
|
|
|
/// this checks for inclusion.
|
2020-12-03 22:22:57 +00:00
|
|
|
// We inline because this has a single call site in `Matrix::specialize_constructor`.
|
|
|
|
#[inline]
|
2023-12-11 19:01:02 +00:00
|
|
|
pub(crate) fn is_covered_by<'p>(&self, pcx: &PatCtxt<'_, 'p, Cx>, other: &Self) -> bool {
|
2020-11-21 23:13:32 +00:00
|
|
|
match (self, other) {
|
2023-12-11 19:01:02 +00:00
|
|
|
(Wildcard, _) => pcx
|
|
|
|
.cx
|
|
|
|
.bug(format_args!("Constructor splitting should not have returned `Wildcard`")),
|
2020-11-21 23:13:32 +00:00
|
|
|
// Wildcards cover anything
|
|
|
|
(_, Wildcard) => true,
|
2023-10-03 15:09:20 +00:00
|
|
|
// Only a wildcard pattern can match these special constructors.
|
2023-11-05 14:00:46 +00:00
|
|
|
(Missing { .. } | NonExhaustive | Hidden, _) => false,
|
2020-11-21 23:13:32 +00:00
|
|
|
|
2023-12-11 12:32:34 +00:00
|
|
|
(Struct, Struct) => true,
|
|
|
|
(Ref, Ref) => true,
|
|
|
|
(UnionField, UnionField) => true,
|
2020-11-21 23:13:32 +00:00
|
|
|
(Variant(self_id), Variant(other_id)) => self_id == other_id,
|
2023-10-12 14:51:27 +00:00
|
|
|
(Bool(self_b), Bool(other_b)) => self_b == other_b,
|
2020-11-21 23:13:32 +00:00
|
|
|
|
2023-10-03 14:21:40 +00:00
|
|
|
(IntRange(self_range), IntRange(other_range)) => self_range.is_subrange(other_range),
|
2023-09-29 16:33:07 +00:00
|
|
|
(F32Range(self_from, self_to, self_end), F32Range(other_from, other_to, other_end)) => {
|
|
|
|
self_from.ge(other_from)
|
|
|
|
&& match self_to.partial_cmp(other_to) {
|
|
|
|
Some(Ordering::Less) => true,
|
|
|
|
Some(Ordering::Equal) => other_end == self_end,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(F64Range(self_from, self_to, self_end), F64Range(other_from, other_to, other_end)) => {
|
|
|
|
self_from.ge(other_from)
|
|
|
|
&& match self_to.partial_cmp(other_to) {
|
|
|
|
Some(Ordering::Less) => true,
|
|
|
|
Some(Ordering::Equal) => other_end == self_end,
|
|
|
|
_ => false,
|
2020-11-21 23:13:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
(Str(self_val), Str(other_val)) => {
|
2022-03-09 12:56:12 +00:00
|
|
|
// FIXME Once valtrees are available we can directly use the bytes
|
|
|
|
// in the `Str` variant of the valtree for the comparison here.
|
2022-06-08 23:27:12 +00:00
|
|
|
self_val == other_val
|
2020-11-21 23:13:32 +00:00
|
|
|
}
|
|
|
|
(Slice(self_slice), Slice(other_slice)) => self_slice.is_covered_by(*other_slice),
|
|
|
|
|
2023-10-30 23:40:41 +00:00
|
|
|
// Opaque constructors don't interact with anything unless they come from the
|
|
|
|
// syntactically identical pattern.
|
|
|
|
(Opaque(self_id), Opaque(other_id)) => self_id == other_id,
|
|
|
|
(Opaque(..), _) | (_, Opaque(..)) => false,
|
2020-11-21 23:13:32 +00:00
|
|
|
|
2023-12-11 19:01:02 +00:00
|
|
|
_ => pcx.cx.bug(format_args!(
|
|
|
|
"trying to compare incompatible constructors {self:?} and {other:?}"
|
|
|
|
)),
|
2020-11-21 23:13:32 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-03 13:30:05 +00:00
|
|
|
}
|
2020-11-21 23:13:32 +00:00
|
|
|
|
2023-10-29 18:18:18 +00:00
|
|
|
#[derive(Debug, Clone, Copy)]
|
2023-12-10 19:42:30 +00:00
|
|
|
pub enum VariantVisibility {
|
2023-10-29 18:18:18 +00:00
|
|
|
/// Variant that doesn't fit the other cases, i.e. most variants.
|
|
|
|
Visible,
|
|
|
|
/// Variant behind an unstable gate or with the `#[doc(hidden)]` attribute. It will not be
|
|
|
|
/// mentioned in diagnostics unless the user mentioned it first.
|
|
|
|
Hidden,
|
|
|
|
/// Variant that matches no value. E.g. `Some::<Option<!>>` if the `exhaustive_patterns` feature
|
|
|
|
/// is enabled. Like `Hidden`, it will not be mentioned in diagnostics unless the user mentioned
|
|
|
|
/// it first.
|
|
|
|
Empty,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Describes the set of all constructors for a type. For details, in particular about the emptiness
|
|
|
|
/// of constructors, see the top of the file.
|
|
|
|
///
|
|
|
|
/// In terms of division of responsibility, [`ConstructorSet::split`] handles all of the
|
|
|
|
/// `exhaustive_patterns` feature.
|
2023-10-04 22:58:14 +00:00
|
|
|
#[derive(Debug)]
|
2023-12-11 19:01:02 +00:00
|
|
|
pub enum ConstructorSet<Cx: MatchCx> {
|
2023-12-11 12:32:34 +00:00
|
|
|
/// The type is a tuple or struct. `empty` tracks whether the type is empty.
|
|
|
|
Struct { empty: bool },
|
2023-10-29 18:18:18 +00:00
|
|
|
/// This type has the following list of constructors. If `variants` is empty and
|
|
|
|
/// `non_exhaustive` is false, don't use this; use `NoConstructors` instead.
|
2023-12-11 19:01:02 +00:00
|
|
|
Variants { variants: IndexVec<Cx::VariantIdx, VariantVisibility>, non_exhaustive: bool },
|
2023-12-11 12:32:34 +00:00
|
|
|
/// The type is `&T`.
|
|
|
|
Ref,
|
|
|
|
/// The type is a union.
|
|
|
|
Union,
|
2023-10-12 14:51:27 +00:00
|
|
|
/// Booleans.
|
|
|
|
Bool,
|
2023-10-03 13:30:05 +00:00
|
|
|
/// The type is spanned by integer values. The range or ranges give the set of allowed values.
|
|
|
|
/// The second range is only useful for `char`.
|
2023-10-12 22:20:06 +00:00
|
|
|
Integers { range_1: IntRange, range_2: Option<IntRange> },
|
2023-10-29 18:18:18 +00:00
|
|
|
/// The type is matched by slices. `array_len` is the compile-time length of the array, if
|
|
|
|
/// known. If `subtype_is_empty`, all constructors are empty except possibly the zero-length
|
|
|
|
/// slice `[]`.
|
|
|
|
Slice { array_len: Option<usize>, subtype_is_empty: bool },
|
2023-10-03 13:30:05 +00:00
|
|
|
/// The constructors cannot be listed, and the type cannot be matched exhaustively. E.g. `str`,
|
|
|
|
/// floats.
|
|
|
|
Unlistable,
|
2023-10-29 18:18:18 +00:00
|
|
|
/// The type has no constructors (not even empty ones). This is `!` and empty enums.
|
|
|
|
NoConstructors,
|
2020-11-21 23:13:32 +00:00
|
|
|
}
|
|
|
|
|
2023-10-03 13:30:05 +00:00
|
|
|
/// Describes the result of analyzing the constructors in a column of a match.
|
2020-12-20 13:29:39 +00:00
|
|
|
///
|
2023-10-03 13:30:05 +00:00
|
|
|
/// `present` is morally the set of constructors present in the column, and `missing` is the set of
|
|
|
|
/// constructors that exist in the type but are not present in the column.
|
2020-12-20 13:29:39 +00:00
|
|
|
///
|
2023-10-23 06:19:10 +00:00
|
|
|
/// More formally, if we discard wildcards from the column, this respects the following constraints:
|
2023-11-18 20:39:57 +00:00
|
|
|
/// 1. the union of `present`, `missing` and `missing_empty` covers all the constructors of the type
|
2023-10-23 06:19:10 +00:00
|
|
|
/// 2. each constructor in `present` is covered by something in the column
|
2023-11-18 20:39:57 +00:00
|
|
|
/// 3. no constructor in `missing` or `missing_empty` is covered by anything in the column
|
2023-10-23 06:19:10 +00:00
|
|
|
/// 4. each constructor in the column is equal to the union of one or more constructors in `present`
|
|
|
|
/// 5. `missing` does not contain empty constructors (see discussion about emptiness at the top of
|
|
|
|
/// the file);
|
2023-11-18 20:39:57 +00:00
|
|
|
/// 6. `missing_empty` contains only empty constructors
|
|
|
|
/// 7. constructors in `present`, `missing` and `missing_empty` are split for the column; in other
|
|
|
|
/// words, they are either fully included in or fully disjoint from each constructor in the
|
|
|
|
/// column. In yet other words, there are no non-trivial intersections like between `0..10` and
|
|
|
|
/// `5..15`.
|
2023-10-23 06:19:10 +00:00
|
|
|
///
|
|
|
|
/// We must be particularly careful with weird constructors like `Opaque`: they're not formally part
|
|
|
|
/// of the `ConstructorSet` for the type, yet if we forgot to include them in `present` we would be
|
|
|
|
/// ignoring any row with `Opaque`s in the algorithm. Hence the importance of point 4.
|
2023-10-04 22:58:14 +00:00
|
|
|
#[derive(Debug)]
|
2023-12-11 19:01:02 +00:00
|
|
|
pub(crate) struct SplitConstructorSet<Cx: MatchCx> {
|
|
|
|
pub(crate) present: SmallVec<[Constructor<Cx>; 1]>,
|
|
|
|
pub(crate) missing: Vec<Constructor<Cx>>,
|
|
|
|
pub(crate) missing_empty: Vec<Constructor<Cx>>,
|
2020-11-21 23:13:32 +00:00
|
|
|
}
|
|
|
|
|
2023-12-11 19:01:02 +00:00
|
|
|
impl<Cx: MatchCx> ConstructorSet<Cx> {
|
2023-10-23 06:19:10 +00:00
|
|
|
/// This analyzes a column of constructors to 1/ determine which constructors of the type (if
|
|
|
|
/// any) are missing; 2/ split constructors to handle non-trivial intersections e.g. on ranges
|
|
|
|
/// or slices. This can get subtle; see [`SplitConstructorSet`] for details of this operation
|
|
|
|
/// and its invariants.
|
2023-10-04 22:58:14 +00:00
|
|
|
#[instrument(level = "debug", skip(self, pcx, ctors), ret)]
|
2023-12-11 19:01:02 +00:00
|
|
|
pub(crate) fn split<'a>(
|
2023-10-03 13:30:05 +00:00
|
|
|
&self,
|
2023-12-11 19:01:02 +00:00
|
|
|
pcx: &PatCtxt<'_, '_, Cx>,
|
|
|
|
ctors: impl Iterator<Item = &'a Constructor<Cx>> + Clone,
|
|
|
|
) -> SplitConstructorSet<Cx>
|
2023-10-03 13:30:05 +00:00
|
|
|
where
|
2023-12-11 19:01:02 +00:00
|
|
|
Cx: 'a,
|
2023-10-03 13:30:05 +00:00
|
|
|
{
|
|
|
|
let mut present: SmallVec<[_; 1]> = SmallVec::new();
|
2023-10-29 18:18:18 +00:00
|
|
|
// Empty constructors found missing.
|
|
|
|
let mut missing_empty = Vec::new();
|
|
|
|
// Nonempty constructors found missing.
|
2023-10-03 17:51:18 +00:00
|
|
|
let mut missing = Vec::new();
|
2023-10-30 23:40:41 +00:00
|
|
|
// Constructors in `ctors`, except wildcards and opaques.
|
|
|
|
let mut seen = Vec::new();
|
|
|
|
for ctor in ctors.cloned() {
|
2023-11-18 20:39:57 +00:00
|
|
|
match ctor {
|
|
|
|
Opaque(..) => present.push(ctor),
|
|
|
|
Wildcard => {} // discard wildcards
|
|
|
|
_ => seen.push(ctor),
|
2023-10-30 23:40:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-03 13:30:05 +00:00
|
|
|
match self {
|
2023-12-11 12:32:34 +00:00
|
|
|
ConstructorSet::Struct { empty } => {
|
2023-10-29 18:18:18 +00:00
|
|
|
if !seen.is_empty() {
|
2023-12-11 12:32:34 +00:00
|
|
|
present.push(Struct);
|
2023-10-29 18:18:18 +00:00
|
|
|
} else if *empty {
|
2023-12-11 12:32:34 +00:00
|
|
|
missing_empty.push(Struct);
|
|
|
|
} else {
|
|
|
|
missing.push(Struct);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ConstructorSet::Ref => {
|
|
|
|
if !seen.is_empty() {
|
|
|
|
present.push(Ref);
|
|
|
|
} else {
|
|
|
|
missing.push(Ref);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ConstructorSet::Union => {
|
|
|
|
if !seen.is_empty() {
|
|
|
|
present.push(UnionField);
|
2023-10-29 18:18:18 +00:00
|
|
|
} else {
|
2023-12-11 12:32:34 +00:00
|
|
|
missing.push(UnionField);
|
2023-10-03 13:30:05 +00:00
|
|
|
}
|
2020-12-14 01:09:06 +00:00
|
|
|
}
|
2023-10-29 18:18:18 +00:00
|
|
|
ConstructorSet::Variants { variants, non_exhaustive } => {
|
2023-10-30 23:40:41 +00:00
|
|
|
let seen_set: FxHashSet<_> = seen.iter().map(|c| c.as_variant().unwrap()).collect();
|
2023-10-03 13:30:05 +00:00
|
|
|
let mut skipped_a_hidden_variant = false;
|
2023-10-14 16:25:10 +00:00
|
|
|
|
2023-10-29 18:18:18 +00:00
|
|
|
for (idx, visibility) in variants.iter_enumerated() {
|
|
|
|
let ctor = Variant(idx);
|
|
|
|
if seen_set.contains(&idx) {
|
2023-10-03 13:30:05 +00:00
|
|
|
present.push(ctor);
|
|
|
|
} else {
|
2023-10-29 18:18:18 +00:00
|
|
|
// We only put visible variants directly into `missing`.
|
|
|
|
match visibility {
|
|
|
|
VariantVisibility::Visible => missing.push(ctor),
|
|
|
|
VariantVisibility::Hidden => skipped_a_hidden_variant = true,
|
|
|
|
VariantVisibility::Empty => missing_empty.push(ctor),
|
|
|
|
}
|
2023-10-03 13:30:05 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-03 15:09:20 +00:00
|
|
|
|
|
|
|
if skipped_a_hidden_variant {
|
|
|
|
missing.push(Hidden);
|
|
|
|
}
|
2023-10-03 13:30:05 +00:00
|
|
|
if *non_exhaustive {
|
|
|
|
missing.push(NonExhaustive);
|
|
|
|
}
|
2020-12-14 01:09:06 +00:00
|
|
|
}
|
2023-10-12 14:51:27 +00:00
|
|
|
ConstructorSet::Bool => {
|
|
|
|
let mut seen_false = false;
|
|
|
|
let mut seen_true = false;
|
2023-10-30 23:40:41 +00:00
|
|
|
for b in seen.iter().map(|ctor| ctor.as_bool().unwrap()) {
|
2023-10-12 14:51:27 +00:00
|
|
|
if b {
|
|
|
|
seen_true = true;
|
|
|
|
} else {
|
|
|
|
seen_false = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if seen_false {
|
|
|
|
present.push(Bool(false));
|
|
|
|
} else {
|
|
|
|
missing.push(Bool(false));
|
|
|
|
}
|
|
|
|
if seen_true {
|
|
|
|
present.push(Bool(true));
|
|
|
|
} else {
|
|
|
|
missing.push(Bool(true));
|
|
|
|
}
|
|
|
|
}
|
2023-10-12 22:20:06 +00:00
|
|
|
ConstructorSet::Integers { range_1, range_2 } => {
|
2023-10-03 17:51:18 +00:00
|
|
|
let seen_ranges: Vec<_> =
|
2023-10-30 23:40:41 +00:00
|
|
|
seen.iter().map(|ctor| ctor.as_int_range().unwrap().clone()).collect();
|
2023-10-03 17:51:18 +00:00
|
|
|
for (seen, splitted_range) in range_1.split(seen_ranges.iter().cloned()) {
|
2023-10-03 13:30:05 +00:00
|
|
|
match seen {
|
|
|
|
Presence::Unseen => missing.push(IntRange(splitted_range)),
|
|
|
|
Presence::Seen => present.push(IntRange(splitted_range)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Some(range_2) = range_2 {
|
2023-10-03 17:51:18 +00:00
|
|
|
for (seen, splitted_range) in range_2.split(seen_ranges.into_iter()) {
|
2023-10-03 13:30:05 +00:00
|
|
|
match seen {
|
|
|
|
Presence::Unseen => missing.push(IntRange(splitted_range)),
|
|
|
|
Presence::Seen => present.push(IntRange(splitted_range)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-10-29 18:18:18 +00:00
|
|
|
ConstructorSet::Slice { array_len, subtype_is_empty } => {
|
2023-10-30 23:40:41 +00:00
|
|
|
let seen_slices = seen.iter().map(|c| c.as_slice().unwrap());
|
2023-10-29 18:18:18 +00:00
|
|
|
let base_slice = Slice::new(*array_len, VarLen(0, 0));
|
2023-10-04 22:58:14 +00:00
|
|
|
for (seen, splitted_slice) in base_slice.split(seen_slices) {
|
|
|
|
let ctor = Slice(splitted_slice);
|
|
|
|
match seen {
|
|
|
|
Presence::Seen => present.push(ctor),
|
2023-10-29 18:18:18 +00:00
|
|
|
Presence::Unseen => {
|
|
|
|
if *subtype_is_empty && splitted_slice.arity() != 0 {
|
|
|
|
// We have subpatterns of an empty type, so the constructor is
|
|
|
|
// empty.
|
|
|
|
missing_empty.push(ctor);
|
|
|
|
} else {
|
|
|
|
missing.push(ctor);
|
|
|
|
}
|
2023-10-04 22:58:14 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-03 13:30:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ConstructorSet::Unlistable => {
|
|
|
|
// Since we can't list constructors, we take the ones in the column. This might list
|
|
|
|
// some constructors several times but there's not much we can do.
|
2023-10-30 23:40:41 +00:00
|
|
|
present.extend(seen);
|
2023-10-03 13:30:05 +00:00
|
|
|
missing.push(NonExhaustive);
|
|
|
|
}
|
2023-10-29 18:18:18 +00:00
|
|
|
ConstructorSet::NoConstructors => {
|
2023-11-18 20:39:57 +00:00
|
|
|
// In a `MaybeInvalid` place even an empty pattern may be reachable. We therefore
|
|
|
|
// add a dummy empty constructor here, which will be ignored if the place is
|
|
|
|
// `ValidOnly`.
|
|
|
|
missing_empty.push(NonExhaustive);
|
2020-12-14 01:09:06 +00:00
|
|
|
}
|
2023-10-03 13:30:05 +00:00
|
|
|
}
|
2021-09-10 20:45:04 +00:00
|
|
|
|
2023-11-18 20:39:57 +00:00
|
|
|
// We have now grouped all the constructors into 3 buckets: present, missing, missing_empty.
|
|
|
|
// In the absence of the `exhaustive_patterns` feature however, we don't count nested empty
|
|
|
|
// types as empty. Only non-nested `!` or `enum Foo {}` are considered empty.
|
2023-12-11 19:01:02 +00:00
|
|
|
if !pcx.cx.is_exhaustive_patterns_feature_on()
|
2023-11-18 20:39:57 +00:00
|
|
|
&& !(pcx.is_top_level && matches!(self, Self::NoConstructors))
|
|
|
|
{
|
|
|
|
// Treat all missing constructors as nonempty.
|
2023-12-12 20:12:19 +00:00
|
|
|
// This clears `missing_empty`.
|
2023-12-12 17:42:37 +00:00
|
|
|
missing.append(&mut missing_empty);
|
2023-10-29 18:18:18 +00:00
|
|
|
}
|
2023-11-18 20:39:57 +00:00
|
|
|
|
|
|
|
SplitConstructorSet { present, missing, missing_empty }
|
2020-12-13 23:56:13 +00:00
|
|
|
}
|
2020-11-21 23:13:32 +00:00
|
|
|
}
|