2023-11-18 03:17:50 +00:00
|
|
|
//! # Match exhaustiveness and redundancy algorithm
|
2020-11-25 20:05:04 +00:00
|
|
|
//!
|
2023-11-18 03:17:50 +00:00
|
|
|
//! This file contains the logic for exhaustiveness and usefulness checking for pattern-matching.
|
2023-10-23 06:19:10 +00:00
|
|
|
//! Specifically, given a list of patterns in a match, we can tell whether:
|
2023-11-18 03:17:50 +00:00
|
|
|
//! (a) a given pattern is redundant
|
2023-10-23 06:19:10 +00:00
|
|
|
//! (b) the patterns cover every possible value for the type (exhaustiveness)
|
2020-11-25 20:05:04 +00:00
|
|
|
//!
|
2023-10-23 06:19:10 +00:00
|
|
|
//! The algorithm implemented here is inspired from the one described in [this
|
|
|
|
//! paper](http://moscova.inria.fr/~maranget/papers/warn/index.html). We have however changed it in
|
|
|
|
//! various ways to accommodate the variety of patterns that Rust supports. We thus explain our
|
|
|
|
//! version here, without being as precise.
|
2020-11-25 20:05:04 +00:00
|
|
|
//!
|
2023-10-23 06:19:10 +00:00
|
|
|
//! Fun fact: computing exhaustiveness is NP-complete, because we can encode a SAT problem as an
|
|
|
|
//! exhaustiveness problem. See [here](https://niedzejkob.p4.team/rust-np) for the fun details.
|
2020-06-14 12:53:36 +00:00
|
|
|
//!
|
|
|
|
//!
|
2023-10-23 06:19:10 +00:00
|
|
|
//! # Summary
|
2020-06-14 12:53:36 +00:00
|
|
|
//!
|
2023-10-23 06:19:10 +00:00
|
|
|
//! The algorithm is given as input a list of patterns, one for each arm of a match, and computes
|
|
|
|
//! the following:
|
|
|
|
//! - a set of values that match none of the patterns (if any),
|
2023-11-18 03:17:50 +00:00
|
|
|
//! - for each subpattern (taking into account or-patterns), whether removing it would change
|
|
|
|
//! anything about how the match executes, i.e. whether it is useful/not redundant.
|
2020-06-14 12:53:36 +00:00
|
|
|
//!
|
2023-10-23 06:19:10 +00:00
|
|
|
//! To a first approximation, the algorithm works by exploring all possible values for the type
|
|
|
|
//! being matched on, and determining which arm(s) catch which value. To make this tractable we
|
|
|
|
//! cleverly group together values, as we'll see below.
|
2020-06-14 12:53:36 +00:00
|
|
|
//!
|
2023-10-23 06:19:10 +00:00
|
|
|
//! The entrypoint of this file is the [`compute_match_usefulness`] function, which computes
|
2023-11-18 03:17:50 +00:00
|
|
|
//! usefulness for each subpattern and exhaustiveness for the whole match.
|
2023-10-23 06:19:10 +00:00
|
|
|
//!
|
|
|
|
//! In this page we explain the necessary concepts to understand how the algorithm works.
|
|
|
|
//!
|
|
|
|
//!
|
|
|
|
//! # Usefulness
|
|
|
|
//!
|
|
|
|
//! The central concept of this file is the notion of "usefulness". Given some patterns `p_1 ..
|
|
|
|
//! p_n`, a pattern `q` is said to be *useful* if there is a value that is matched by `q` and by
|
|
|
|
//! none of the `p_i`. We write `usefulness(p_1 .. p_n, q)` for a function that returns a list of
|
|
|
|
//! such values. The aim of this file is to compute it efficiently.
|
|
|
|
//!
|
2023-11-18 03:17:50 +00:00
|
|
|
//! This is enough to compute usefulness: a pattern in a `match` expression is redundant iff it is
|
|
|
|
//! not useful w.r.t. the patterns above it:
|
2023-10-23 06:19:10 +00:00
|
|
|
//! ```compile_fail,E0004
|
|
|
|
//! # fn foo() {
|
|
|
|
//! match Some(0u32) {
|
|
|
|
//! Some(0..100) => {},
|
2023-11-18 03:17:50 +00:00
|
|
|
//! Some(90..190) => {}, // useful: `Some(150)` is matched by this but not the branch above
|
|
|
|
//! Some(50..150) => {}, // redundant: all the values this matches are already matched by
|
2023-10-23 06:19:10 +00:00
|
|
|
//! // the branches above
|
2023-11-18 03:17:50 +00:00
|
|
|
//! None => {}, // useful: `None` is matched by this but not the branches above
|
2020-12-20 13:29:39 +00:00
|
|
|
//! }
|
2022-04-15 22:04:34 +00:00
|
|
|
//! # }
|
2020-12-20 13:29:39 +00:00
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! This is also enough to compute exhaustiveness: a match is exhaustive iff the wildcard `_`
|
2020-12-22 06:09:54 +00:00
|
|
|
//! pattern is _not_ useful w.r.t. the patterns in the match. The values returned by `usefulness`
|
|
|
|
//! are used to tell the user which values are missing.
|
2022-04-15 22:04:34 +00:00
|
|
|
//! ```compile_fail,E0004
|
2023-10-23 06:19:10 +00:00
|
|
|
//! # fn foo(x: Option<u32>) {
|
2020-12-20 13:29:39 +00:00
|
|
|
//! match x {
|
2022-04-15 22:04:34 +00:00
|
|
|
//! None => {},
|
2023-10-23 06:19:10 +00:00
|
|
|
//! Some(0) => {},
|
2020-12-20 13:29:39 +00:00
|
|
|
//! // not exhaustive: `_` is useful because it matches `Some(1)`
|
|
|
|
//! }
|
2022-04-15 22:04:34 +00:00
|
|
|
//! # }
|
2020-12-20 13:29:39 +00:00
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//!
|
|
|
|
//! # Constructors and fields
|
|
|
|
//!
|
2023-10-23 06:19:10 +00:00
|
|
|
//! In the value `Pair(Some(0), true)`, `Pair` is called the constructor of the value, and `Some(0)`
|
|
|
|
//! and `true` are its fields. Every matcheable value can be decomposed in this way. Examples of
|
|
|
|
//! constructors are: `Some`, `None`, `(,)` (the 2-tuple constructor), `Foo {..}` (the constructor
|
|
|
|
//! for a struct `Foo`), and `2` (the constructor for the number `2`).
|
|
|
|
//!
|
|
|
|
//! Each constructor takes a fixed number of fields; this is called its arity. `Pair` and `(,)` have
|
|
|
|
//! arity 2, `Some` has arity 1, `None` and `42` have arity 0. Each type has a known set of
|
|
|
|
//! constructors. Some types have many constructors (like `u64`) or even an infinitely many (like
|
|
|
|
//! `&str` and `&[T]`).
|
|
|
|
//!
|
|
|
|
//! Patterns are similar: `Pair(Some(_), _)` has constructor `Pair` and two fields. The difference
|
|
|
|
//! is that we get some extra pattern-only constructors, namely: the wildcard `_`, variable
|
|
|
|
//! bindings, integer ranges like `0..=10`, and variable-length slices like `[_, .., _]`. We treat
|
|
|
|
//! or-patterns separately, see the dedicated section below.
|
|
|
|
//!
|
|
|
|
//! Now to check if a value `v` matches a pattern `p`, we check if `v`'s constructor matches `p`'s
|
|
|
|
//! constructor, then recursively compare their fields if necessary. A few representative examples:
|
2020-06-14 12:53:36 +00:00
|
|
|
//!
|
2020-12-20 13:29:39 +00:00
|
|
|
//! - `matches!(v, _) := true`
|
|
|
|
//! - `matches!((v0, v1), (p0, p1)) := matches!(v0, p0) && matches!(v1, p1)`
|
|
|
|
//! - `matches!(Foo { bar: v0, baz: v1 }, Foo { bar: p0, baz: p1 }) := matches!(v0, p0) && matches!(v1, p1)`
|
|
|
|
//! - `matches!(Ok(v0), Ok(p0)) := matches!(v0, p0)`
|
|
|
|
//! - `matches!(Ok(v0), Err(p0)) := false` (incompatible variants)
|
|
|
|
//! - `matches!(v, 1..=100) := matches!(v, 1) || ... || matches!(v, 100)`
|
|
|
|
//! - `matches!([v0], [p0, .., p1]) := false` (incompatible lengths)
|
|
|
|
//! - `matches!([v0, v1, v2], [p0, .., p1]) := matches!(v0, p0) && matches!(v2, p1)`
|
2020-06-14 12:53:36 +00:00
|
|
|
//!
|
2023-12-11 11:53:01 +00:00
|
|
|
//! Constructors and relevant operations are defined in the [`crate::constructor`] module. A
|
|
|
|
//! representation of patterns that uses constructors is available in [`crate::pat`]. The question
|
|
|
|
//! of whether a constructor is matched by another one is answered by
|
2023-10-23 06:19:10 +00:00
|
|
|
//! [`Constructor::is_covered_by`].
|
2020-06-14 12:53:36 +00:00
|
|
|
//!
|
2023-10-23 06:19:10 +00:00
|
|
|
//! Note 1: variable bindings (like the `x` in `Some(x)`) match anything, so we treat them as wildcards.
|
|
|
|
//! Note 2: this only applies to matcheable values. For example a value of type `Rc<u64>` can't be
|
|
|
|
//! deconstructed that way.
|
2020-06-14 12:53:36 +00:00
|
|
|
//!
|
|
|
|
//!
|
|
|
|
//!
|
2020-12-20 13:29:39 +00:00
|
|
|
//! # Specialization
|
2020-10-19 04:54:10 +00:00
|
|
|
//!
|
2023-10-23 06:19:10 +00:00
|
|
|
//! The examples in the previous section motivate the operation at the heart of the algorithm:
|
|
|
|
//! "specialization". It captures this idea of "removing one layer of constructor".
|
|
|
|
//!
|
|
|
|
//! `specialize(c, p)` takes a value-only constructor `c` and a pattern `p`, and returns a
|
|
|
|
//! pattern-tuple or nothing. It works as follows:
|
|
|
|
//!
|
|
|
|
//! - Specializing for the wrong constructor returns nothing
|
|
|
|
//!
|
|
|
|
//! - `specialize(None, Some(p0)) := <nothing>`
|
|
|
|
//! - `specialize([,,,], [p0]) := <nothing>`
|
|
|
|
//!
|
|
|
|
//! - Specializing for the correct constructor returns a tuple of the fields
|
|
|
|
//!
|
|
|
|
//! - `specialize(Variant1, Variant1(p0, p1, p2)) := (p0, p1, p2)`
|
|
|
|
//! - `specialize(Foo{ bar, baz, quz }, Foo { bar: p0, baz: p1, .. }) := (p0, p1, _)`
|
|
|
|
//! - `specialize([,,,], [p0, .., p1]) := (p0, _, _, p1)`
|
|
|
|
//!
|
|
|
|
//! We get the following property: for any values `v_1, .., v_n` of appropriate types, we have:
|
|
|
|
//! ```text
|
|
|
|
//! matches!(c(v_1, .., v_n), p)
|
|
|
|
//! <=> specialize(c, p) returns something
|
|
|
|
//! && matches!((v_1, .., v_n), specialize(c, p))
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! We also extend specialization to pattern-tuples by applying it to the first pattern:
|
|
|
|
//! `specialize(c, (p_0, .., p_n)) := specialize(c, p_0) ++ (p_1, .., p_m)`
|
|
|
|
//! where `++` is concatenation of tuples.
|
|
|
|
//!
|
|
|
|
//!
|
|
|
|
//! The previous property extends to pattern-tuples:
|
|
|
|
//! ```text
|
|
|
|
//! matches!((c(v_1, .., v_n), w_1, .., w_m), (p_0, p_1, .., p_m))
|
|
|
|
//! <=> specialize(c, p_0) does not error
|
|
|
|
//! && matches!((v_1, .., v_n, w_1, .., w_m), specialize(c, (p_0, p_1, .., p_m)))
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! Whether specialization returns something or not is given by [`Constructor::is_covered_by`].
|
|
|
|
//! Specialization of a pattern is computed in [`DeconstructedPat::specialize`]. Specialization for
|
|
|
|
//! a pattern-tuple is computed in [`PatStack::pop_head_constructor`]. Finally, specialization for a
|
|
|
|
//! set of pattern-tuples is computed in [`Matrix::specialize_constructor`].
|
|
|
|
//!
|
|
|
|
//!
|
|
|
|
//!
|
|
|
|
//! # Undoing specialization
|
|
|
|
//!
|
|
|
|
//! To construct witnesses we will need an inverse of specialization. If `c` is a constructor of
|
|
|
|
//! arity `n`, we define `unspecialize` as:
|
|
|
|
//! `unspecialize(c, (p_1, .., p_n, q_1, .., q_m)) := (c(p_1, .., p_n), q_1, .., q_m)`.
|
|
|
|
//!
|
|
|
|
//! This is done for a single witness-tuple in [`WitnessStack::apply_constructor`], and for a set of
|
|
|
|
//! witness-tuples in [`WitnessMatrix::apply_constructor`].
|
|
|
|
//!
|
|
|
|
//!
|
|
|
|
//!
|
|
|
|
//! # Computing usefulness
|
|
|
|
//!
|
|
|
|
//! We now present a naive version of the algorithm for computing usefulness. From now on we operate
|
|
|
|
//! on pattern-tuples.
|
|
|
|
//!
|
|
|
|
//! Let `pt_1, .., pt_n` and `qt` be length-m tuples of patterns for the same type `(T_1, .., T_m)`.
|
|
|
|
//! We compute `usefulness(tp_1, .., tp_n, tq)` as follows:
|
|
|
|
//!
|
|
|
|
//! - Base case: `m == 0`.
|
|
|
|
//! The pattern-tuples are all empty, i.e. they're all `()`. Thus `tq` is useful iff there are
|
|
|
|
//! no rows above it, i.e. if `n == 0`. In that case we return `()` as a witness-tuple of
|
|
|
|
//! usefulness of `tq`.
|
|
|
|
//!
|
|
|
|
//! - Inductive case: `m > 0`.
|
|
|
|
//! In this naive version, we list all the possible constructors for values of type `T1` (we
|
|
|
|
//! will be more clever in the next section).
|
|
|
|
//!
|
|
|
|
//! - For each such constructor `c` for which `specialize(c, tq)` is not nothing:
|
|
|
|
//! - We recursively compute `usefulness(specialize(c, tp_1) ... specialize(c, tp_n), specialize(c, tq))`,
|
|
|
|
//! where we discard any `specialize(c, p_i)` that returns nothing.
|
|
|
|
//! - For each witness-tuple `w` found, we apply `unspecialize(c, w)` to it.
|
|
|
|
//!
|
|
|
|
//! - We return the all the witnesses found, if any.
|
|
|
|
//!
|
2020-12-20 13:29:39 +00:00
|
|
|
//!
|
|
|
|
//! Let's take the following example:
|
2022-04-15 22:04:34 +00:00
|
|
|
//! ```compile_fail,E0004
|
|
|
|
//! # enum Enum { Variant1(()), Variant2(Option<bool>, u32)}
|
2023-10-23 06:19:10 +00:00
|
|
|
//! # use Enum::*;
|
2022-04-15 22:04:34 +00:00
|
|
|
//! # fn foo(x: Enum) {
|
2020-10-19 04:54:10 +00:00
|
|
|
//! match x {
|
2023-10-23 06:19:10 +00:00
|
|
|
//! Variant1(_) => {} // `p1`
|
|
|
|
//! Variant2(None, 0) => {} // `p2`
|
|
|
|
//! Variant2(Some(_), 0) => {} // `q`
|
2020-10-19 04:54:10 +00:00
|
|
|
//! }
|
2022-04-15 22:04:34 +00:00
|
|
|
//! # }
|
2020-06-14 12:53:36 +00:00
|
|
|
//! ```
|
2020-10-19 04:54:10 +00:00
|
|
|
//!
|
2023-10-23 06:19:10 +00:00
|
|
|
//! To compute the usefulness of `q`, we would proceed as follows:
|
|
|
|
//! ```text
|
|
|
|
//! Start:
|
|
|
|
//! `tp1 = [Variant1(_)]`
|
|
|
|
//! `tp2 = [Variant2(None, 0)]`
|
|
|
|
//! `tq = [Variant2(Some(true), 0)]`
|
|
|
|
//!
|
|
|
|
//! Constructors are `Variant1` and `Variant2`. Only `Variant2` can specialize `tq`.
|
|
|
|
//! Specialize with `Variant2`:
|
|
|
|
//! `tp2 = [None, 0]`
|
|
|
|
//! `tq = [Some(true), 0]`
|
|
|
|
//!
|
|
|
|
//! Constructors are `None` and `Some`. Only `Some` can specialize `tq`.
|
|
|
|
//! Specialize with `Some`:
|
|
|
|
//! `tq = [true, 0]`
|
|
|
|
//!
|
|
|
|
//! Constructors are `false` and `true`. Only `true` can specialize `tq`.
|
|
|
|
//! Specialize with `true`:
|
|
|
|
//! `tq = [0]`
|
|
|
|
//!
|
|
|
|
//! Constructors are `0`, `1`, .. up to infinity. Only `0` can specialize `tq`.
|
|
|
|
//! Specialize with `0`:
|
|
|
|
//! `tq = []`
|
|
|
|
//!
|
|
|
|
//! m == 0 and n == 0, so `tq` is useful with witness `[]`.
|
|
|
|
//! `witness = []`
|
|
|
|
//!
|
|
|
|
//! Unspecialize with `0`:
|
|
|
|
//! `witness = [0]`
|
|
|
|
//! Unspecialize with `true`:
|
|
|
|
//! `witness = [true, 0]`
|
|
|
|
//! Unspecialize with `Some`:
|
|
|
|
//! `witness = [Some(true), 0]`
|
|
|
|
//! Unspecialize with `Variant2`:
|
|
|
|
//! `witness = [Variant2(Some(true), 0)]`
|
2020-10-19 04:54:10 +00:00
|
|
|
//! ```
|
|
|
|
//!
|
2023-10-23 06:19:10 +00:00
|
|
|
//! Therefore `usefulness(tp_1, tp_2, tq)` returns the single witness-tuple `[Variant2(Some(true), 0)]`.
|
2020-06-14 12:53:36 +00:00
|
|
|
//!
|
2020-06-30 08:56:10 +00:00
|
|
|
//!
|
2024-03-13 12:56:06 +00:00
|
|
|
//! Computing the set of constructors for a type is done in [`PatCx::ctors_for_ty`]. See
|
2023-12-11 11:53:01 +00:00
|
|
|
//! the following sections for more accurate versions of the algorithm and corresponding links.
|
2020-06-30 08:56:10 +00:00
|
|
|
//!
|
|
|
|
//!
|
2020-11-25 20:05:04 +00:00
|
|
|
//!
|
2023-11-18 03:17:50 +00:00
|
|
|
//! # Computing usefulness and exhaustiveness in one go
|
2020-11-25 20:05:04 +00:00
|
|
|
//!
|
2023-11-18 03:17:50 +00:00
|
|
|
//! The algorithm we have described so far computes usefulness of each pattern in turn, and ends by
|
|
|
|
//! checking if `_` is useful to determine exhaustiveness of the whole match. In practice, instead
|
|
|
|
//! of doing "for each pattern { for each constructor { ... } }", we do "for each constructor { for
|
|
|
|
//! each pattern { ... } }". This allows us to compute everything in one go.
|
2020-11-25 20:05:04 +00:00
|
|
|
//!
|
2023-11-18 03:17:50 +00:00
|
|
|
//! [`Matrix`] stores the set of pattern-tuples under consideration. We track usefulness of each
|
2023-10-23 06:19:10 +00:00
|
|
|
//! row mutably in the matrix as we go along. We ignore witnesses of usefulness of the match rows.
|
|
|
|
//! We gather witnesses of the usefulness of `_` in [`WitnessMatrix`]. The algorithm that computes
|
2023-11-18 03:17:50 +00:00
|
|
|
//! all this is in [`compute_exhaustiveness_and_usefulness`].
|
2020-11-25 20:05:04 +00:00
|
|
|
//!
|
2023-10-23 06:19:10 +00:00
|
|
|
//! See the full example at the bottom of this documentation.
|
2020-06-30 08:56:10 +00:00
|
|
|
//!
|
|
|
|
//!
|
|
|
|
//!
|
2023-10-23 06:19:10 +00:00
|
|
|
//! # Making usefulness tractable: constructor splitting
|
2020-06-14 12:53:36 +00:00
|
|
|
//!
|
2023-10-23 06:19:10 +00:00
|
|
|
//! We're missing one last detail: which constructors do we list? Naively listing all value
|
|
|
|
//! constructors cannot work for types like `u64` or `&str`, so we need to be more clever. The final
|
|
|
|
//! clever idea for this algorithm is that we can group together constructors that behave the same.
|
2020-06-14 12:53:36 +00:00
|
|
|
//!
|
2023-10-23 06:19:10 +00:00
|
|
|
//! Examples:
|
|
|
|
//! ```compile_fail,E0004
|
|
|
|
//! match (0, false) {
|
|
|
|
//! (0 ..=100, true) => {}
|
|
|
|
//! (50..=150, false) => {}
|
|
|
|
//! (0 ..=200, _) => {}
|
|
|
|
//! }
|
|
|
|
//! ```
|
2020-06-14 12:53:36 +00:00
|
|
|
//!
|
2023-10-23 06:19:10 +00:00
|
|
|
//! In this example, trying any of `0`, `1`, .., `49` will give the same specialized matrix, and
|
2023-11-18 03:17:50 +00:00
|
|
|
//! thus the same usefulness/exhaustiveness results. We can thus accelerate the algorithm by
|
2023-10-23 06:19:10 +00:00
|
|
|
//! trying them all at once. Here in fact, the only cases we need to consider are: `0..50`,
|
|
|
|
//! `50..=100`, `101..=150`,`151..=200` and `201..`.
|
2020-06-14 12:53:36 +00:00
|
|
|
//!
|
2023-10-23 06:19:10 +00:00
|
|
|
//! ```
|
|
|
|
//! enum Direction { North, South, East, West }
|
|
|
|
//! # let wind = (Direction::North, 0u8);
|
|
|
|
//! match wind {
|
|
|
|
//! (Direction::North, 50..) => {}
|
|
|
|
//! (_, _) => {}
|
|
|
|
//! }
|
|
|
|
//! ```
|
2020-06-14 12:53:36 +00:00
|
|
|
//!
|
2023-10-23 06:19:10 +00:00
|
|
|
//! In this example, trying any of `South`, `East`, `West` will give the same specialized matrix. By
|
|
|
|
//! the same reasoning, we only need to try two cases: `North`, and "everything else".
|
2020-06-30 08:56:10 +00:00
|
|
|
//!
|
2023-10-23 06:19:10 +00:00
|
|
|
//! We call _constructor splitting_ the operation that computes such a minimal set of cases to try.
|
2023-12-11 11:53:01 +00:00
|
|
|
//! This is done in [`ConstructorSet::split`] and explained in [`crate::constructor`].
|
2020-10-19 04:54:10 +00:00
|
|
|
//!
|
|
|
|
//!
|
2023-11-18 03:17:50 +00:00
|
|
|
//!
|
2023-12-23 21:15:15 +00:00
|
|
|
//! # `Missing` and relevancy
|
|
|
|
//!
|
|
|
|
//! ## Relevant values
|
2023-11-29 20:43:06 +00:00
|
|
|
//!
|
|
|
|
//! Take the following example:
|
|
|
|
//!
|
|
|
|
//! ```compile_fail,E0004
|
2023-12-23 21:15:15 +00:00
|
|
|
//! # let foo = (true, true);
|
|
|
|
//! match foo {
|
|
|
|
//! (true, _) => 1,
|
|
|
|
//! (_, true) => 2,
|
|
|
|
//! };
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! Consider the value `(true, true)`:
|
|
|
|
//! - Row 2 does not distinguish `(true, true)` and `(false, true)`;
|
|
|
|
//! - `false` does not show up in the first column of the match, so without knowing anything else we
|
|
|
|
//! can deduce that `(false, true)` matches the same or fewer rows than `(true, true)`.
|
|
|
|
//!
|
|
|
|
//! Using those two facts together, we deduce that `(true, true)` will not give us more usefulness
|
|
|
|
//! information about row 2 than `(false, true)` would. We say that "`(true, true)` is made
|
|
|
|
//! irrelevant for row 2 by `(false, true)`". We will use this idea to prune the search tree.
|
|
|
|
//!
|
|
|
|
//!
|
|
|
|
//! ## Computing relevancy
|
|
|
|
//!
|
|
|
|
//! We now generalize from the above example to approximate relevancy in a simple way. Note that we
|
|
|
|
//! will only compute an approximation: we can sometimes determine when a case is irrelevant, but
|
|
|
|
//! computing this precisely is at least as hard as computing usefulness.
|
|
|
|
//!
|
|
|
|
//! Our computation of relevancy relies on the `Missing` constructor. As explained in
|
|
|
|
//! [`crate::constructor`], `Missing` represents the constructors not present in a given column. For
|
|
|
|
//! example in the following:
|
|
|
|
//!
|
|
|
|
//! ```compile_fail,E0004
|
2023-11-29 20:43:06 +00:00
|
|
|
//! enum Direction { North, South, East, West }
|
|
|
|
//! # let wind = (Direction::North, 0u8);
|
|
|
|
//! match wind {
|
2023-12-23 21:15:15 +00:00
|
|
|
//! (Direction::North, _) => 1,
|
|
|
|
//! (_, 50..) => 2,
|
|
|
|
//! };
|
2023-11-29 20:43:06 +00:00
|
|
|
//! ```
|
|
|
|
//!
|
2023-12-23 21:15:15 +00:00
|
|
|
//! Here `South`, `East` and `West` are missing in the first column, and `0..50` is missing in the
|
|
|
|
//! second. Both of these sets are represented by `Constructor::Missing` in their corresponding
|
|
|
|
//! column.
|
2023-11-29 20:43:06 +00:00
|
|
|
//!
|
2023-12-23 21:15:15 +00:00
|
|
|
//! We then compute relevancy as follows: during the course of the algorithm, for a row `r`:
|
|
|
|
//! - if `r` has a wildcard in the first column;
|
|
|
|
//! - and some constructors are missing in that column;
|
|
|
|
//! - then any `c != Missing` is considered irrelevant for row `r`.
|
2023-11-29 20:43:06 +00:00
|
|
|
//!
|
2023-12-23 21:15:15 +00:00
|
|
|
//! By this we mean that continuing the algorithm by specializing with `c` is guaranteed not to
|
|
|
|
//! contribute more information about the usefulness of row `r` than what we would get by
|
|
|
|
//! specializing with `Missing`. The argument is the same as in the previous subsection.
|
2023-11-29 20:43:06 +00:00
|
|
|
//!
|
2023-12-23 21:15:15 +00:00
|
|
|
//! Once we've specialized by a constructor `c` that is irrelevant for row `r`, we're guaranteed to
|
|
|
|
//! only explore values irrelevant for `r`. If we then ever reach a point where we're only exploring
|
|
|
|
//! values that are irrelevant to all of the rows (including the virtual wildcard row used for
|
|
|
|
//! exhaustiveness), we skip that case entirely.
|
2023-11-29 20:43:06 +00:00
|
|
|
//!
|
2023-12-23 21:15:15 +00:00
|
|
|
//!
|
|
|
|
//! ## Example
|
|
|
|
//!
|
|
|
|
//! Let's go through a variation on the first example:
|
|
|
|
//!
|
|
|
|
//! ```compile_fail,E0004
|
|
|
|
//! # let foo = (true, true, true);
|
|
|
|
//! match foo {
|
|
|
|
//! (true, _, true) => 1,
|
|
|
|
//! (_, true, _) => 2,
|
|
|
|
//! };
|
2023-11-29 20:43:06 +00:00
|
|
|
//! ```
|
|
|
|
//!
|
2023-12-23 21:15:15 +00:00
|
|
|
//! ```text
|
|
|
|
//! ┐ Patterns:
|
|
|
|
//! │ 1. `[(true, _, true)]`
|
|
|
|
//! │ 2. `[(_, true, _)]`
|
|
|
|
//! │ 3. `[_]` // virtual extra wildcard row
|
|
|
|
//! │
|
|
|
|
//! │ Specialize with `(,,)`:
|
|
|
|
//! ├─┐ Patterns:
|
|
|
|
//! │ │ 1. `[true, _, true]`
|
|
|
|
//! │ │ 2. `[_, true, _]`
|
|
|
|
//! │ │ 3. `[_, _, _]`
|
|
|
|
//! │ │
|
|
|
|
//! │ │ There are missing constructors in the first column (namely `false`), hence
|
|
|
|
//! │ │ `true` is irrelevant for rows 2 and 3.
|
|
|
|
//! │ │
|
|
|
|
//! │ │ Specialize with `true`:
|
|
|
|
//! │ ├─┐ Patterns:
|
|
|
|
//! │ │ │ 1. `[_, true]`
|
|
|
|
//! │ │ │ 2. `[true, _]` // now exploring irrelevant cases
|
|
|
|
//! │ │ │ 3. `[_, _]` // now exploring irrelevant cases
|
|
|
|
//! │ │ │
|
|
|
|
//! │ │ │ There are missing constructors in the first column (namely `false`), hence
|
|
|
|
//! │ │ │ `true` is irrelevant for rows 1 and 3.
|
|
|
|
//! │ │ │
|
|
|
|
//! │ │ │ Specialize with `true`:
|
|
|
|
//! │ │ ├─┐ Patterns:
|
|
|
|
//! │ │ │ │ 1. `[true]` // now exploring irrelevant cases
|
|
|
|
//! │ │ │ │ 2. `[_]` // now exploring irrelevant cases
|
|
|
|
//! │ │ │ │ 3. `[_]` // now exploring irrelevant cases
|
|
|
|
//! │ │ │ │
|
|
|
|
//! │ │ │ │ The current case is irrelevant for all rows: we backtrack immediately.
|
|
|
|
//! │ │ ├─┘
|
|
|
|
//! │ │ │
|
|
|
|
//! │ │ │ Specialize with `false`:
|
|
|
|
//! │ │ ├─┐ Patterns:
|
|
|
|
//! │ │ │ │ 1. `[true]`
|
|
|
|
//! │ │ │ │ 3. `[_]` // now exploring irrelevant cases
|
|
|
|
//! │ │ │ │
|
|
|
|
//! │ │ │ │ Specialize with `true`:
|
|
|
|
//! │ │ │ ├─┐ Patterns:
|
|
|
|
//! │ │ │ │ │ 1. `[]`
|
|
|
|
//! │ │ │ │ │ 3. `[]` // now exploring irrelevant cases
|
|
|
|
//! │ │ │ │ │
|
|
|
|
//! │ │ │ │ │ Row 1 is therefore useful.
|
|
|
|
//! │ │ │ ├─┘
|
|
|
|
//! <etc...>
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! Relevancy allowed us to skip the case `(true, true, _)` entirely. In some cases this pruning can
|
|
|
|
//! give drastic speedups. The case this was built for is the following (#118437):
|
2023-11-29 20:43:06 +00:00
|
|
|
//!
|
2023-12-23 21:15:15 +00:00
|
|
|
//! ```ignore(illustrative)
|
|
|
|
//! match foo {
|
|
|
|
//! (true, _, _, _, ..) => 1,
|
|
|
|
//! (_, true, _, _, ..) => 2,
|
|
|
|
//! (_, _, true, _, ..) => 3,
|
|
|
|
//! (_, _, _, true, ..) => 4,
|
|
|
|
//! ...
|
|
|
|
//! }
|
|
|
|
//! ```
|
2023-11-29 20:43:06 +00:00
|
|
|
//!
|
2023-12-23 21:15:15 +00:00
|
|
|
//! Without considering relevancy, we would explore all 2^n combinations of the `true` and `Missing`
|
|
|
|
//! constructors. Relevancy tells us that e.g. `(true, true, false, false, false, ...)` is
|
|
|
|
//! irrelevant for all the rows. This allows us to skip all cases with more than one `true`
|
|
|
|
//! constructor, changing the runtime from exponential to linear.
|
2023-11-29 20:43:06 +00:00
|
|
|
//!
|
|
|
|
//!
|
2023-12-23 21:15:15 +00:00
|
|
|
//! ## Relevancy and exhaustiveness
|
2023-11-29 20:43:06 +00:00
|
|
|
//!
|
2023-12-23 21:15:15 +00:00
|
|
|
//! For exhaustiveness, we do something slightly different w.r.t relevancy: we do not report
|
|
|
|
//! witnesses of non-exhaustiveness that are irrelevant for the virtual wildcard row. For example,
|
|
|
|
//! in:
|
|
|
|
//!
|
|
|
|
//! ```ignore(illustrative)
|
2023-11-29 20:43:06 +00:00
|
|
|
//! match foo {
|
2023-12-23 21:15:15 +00:00
|
|
|
//! (true, true) => {}
|
2023-11-29 20:43:06 +00:00
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
//!
|
2023-12-23 21:15:15 +00:00
|
|
|
//! we only report `(false, _)` as missing. This was a deliberate choice made early in the
|
|
|
|
//! development of rust, for diagnostic and performance purposes. As showed in the previous section,
|
|
|
|
//! ignoring irrelevant cases preserves usefulness, so this choice still correctly computes whether
|
|
|
|
//! a match is exhaustive.
|
2023-11-29 20:43:06 +00:00
|
|
|
//!
|
|
|
|
//!
|
|
|
|
//!
|
2023-10-23 06:19:10 +00:00
|
|
|
//! # Or-patterns
|
2020-10-19 04:54:10 +00:00
|
|
|
//!
|
2023-10-23 06:19:10 +00:00
|
|
|
//! What we have described so far works well if there are no or-patterns. To handle them, if the
|
2024-07-20 20:18:35 +00:00
|
|
|
//! first pattern of any row in the matrix is an or-pattern, we expand it by duplicating the rest of
|
|
|
|
//! the row as necessary. For code reuse, this is implemented as "specializing with the `Or`
|
|
|
|
//! constructor".
|
2020-12-20 13:29:39 +00:00
|
|
|
//!
|
2024-01-24 22:23:14 +00:00
|
|
|
//! This makes usefulness tracking subtle, because we also want to compute whether an alternative of
|
|
|
|
//! an or-pattern is redundant, e.g. in `Some(_) | Some(0)`. We therefore track usefulness of each
|
|
|
|
//! subpattern of the match.
|
2020-06-14 12:53:36 +00:00
|
|
|
//!
|
|
|
|
//!
|
2023-05-16 09:45:56 +00:00
|
|
|
//!
|
2023-10-23 06:19:10 +00:00
|
|
|
//! # Constants and opaques
|
2023-05-16 09:45:56 +00:00
|
|
|
//!
|
|
|
|
//! There are two kinds of constants in patterns:
|
|
|
|
//!
|
|
|
|
//! * literals (`1`, `true`, `"foo"`)
|
|
|
|
//! * named or inline consts (`FOO`, `const { 5 + 6 }`)
|
|
|
|
//!
|
2023-10-23 06:19:10 +00:00
|
|
|
//! The latter are converted into the corresponding patterns by a previous phase. For example
|
2023-05-16 09:45:56 +00:00
|
|
|
//! `const_to_pat(const { [1, 2, 3] })` becomes an `Array(vec![Const(1), Const(2), Const(3)])`
|
|
|
|
//! pattern. This gets problematic when comparing the constant via `==` would behave differently
|
2023-10-23 06:19:10 +00:00
|
|
|
//! from matching on the constant converted to a pattern. The situation around this is currently
|
|
|
|
//! unclear and the lang team is working on clarifying what we want to do there. In any case, there
|
|
|
|
//! are constants we will not turn into patterns. We capture these with `Constructor::Opaque`. These
|
|
|
|
//! `Opaque` patterns do not participate in exhaustiveness, specialization or overlap checking.
|
|
|
|
//!
|
|
|
|
//!
|
|
|
|
//!
|
2023-11-18 03:17:50 +00:00
|
|
|
//! # Usefulness vs reachability, validity, and empty patterns
|
|
|
|
//!
|
|
|
|
//! This is likely the subtlest aspect of the algorithm. To be fully precise, a match doesn't
|
|
|
|
//! operate on a value, it operates on a place. In certain unsafe circumstances, it is possible for
|
|
|
|
//! a place to not contain valid data for its type. This has subtle consequences for empty types.
|
|
|
|
//! Take the following:
|
|
|
|
//!
|
|
|
|
//! ```rust
|
|
|
|
//! enum Void {}
|
|
|
|
//! let x: u8 = 0;
|
|
|
|
//! let ptr: *const Void = &x as *const u8 as *const Void;
|
|
|
|
//! unsafe {
|
|
|
|
//! match *ptr {
|
|
|
|
//! _ => println!("Reachable!"),
|
|
|
|
//! }
|
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! In this example, `ptr` is a valid pointer pointing to a place with invalid data. The `_` pattern
|
|
|
|
//! does not look at the contents of `*ptr`, so this is ok and the arm is taken. In other words,
|
|
|
|
//! despite the place we are inspecting being of type `Void`, there is a reachable arm. If the
|
|
|
|
//! arm had a binding however:
|
|
|
|
//!
|
|
|
|
//! ```rust
|
|
|
|
//! # #[derive(Copy, Clone)]
|
|
|
|
//! # enum Void {}
|
|
|
|
//! # let x: u8 = 0;
|
|
|
|
//! # let ptr: *const Void = &x as *const u8 as *const Void;
|
|
|
|
//! # unsafe {
|
|
|
|
//! match *ptr {
|
|
|
|
//! _a => println!("Unreachable!"),
|
|
|
|
//! }
|
|
|
|
//! # }
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! Here the binding loads the value of type `Void` from the `*ptr` place. In this example, this
|
|
|
|
//! causes UB since the data is not valid. In the general case, this asserts validity of the data at
|
|
|
|
//! `*ptr`. Either way, this arm will never be taken.
|
|
|
|
//!
|
|
|
|
//! Finally, let's consider the empty match `match *ptr {}`. If we consider this exhaustive, then
|
|
|
|
//! having invalid data at `*ptr` is invalid. In other words, the empty match is semantically
|
|
|
|
//! equivalent to the `_a => ...` match. In the interest of explicitness, we prefer the case with an
|
|
|
|
//! arm, hence we won't tell the user to remove the `_a` arm. In other words, the `_a` arm is
|
|
|
|
//! unreachable yet not redundant. This is why we lint on redundant arms rather than unreachable
|
|
|
|
//! arms, despite the fact that the lint says "unreachable".
|
|
|
|
//!
|
|
|
|
//! These considerations only affects certain places, namely those that can contain non-valid data
|
|
|
|
//! without UB. These are: pointer dereferences, reference dereferences, and union field accesses.
|
|
|
|
//! We track in the algorithm whether a given place is known to contain valid data. This is done
|
|
|
|
//! first by inspecting the scrutinee syntactically (which gives us `cx.known_valid_scrutinee`), and
|
|
|
|
//! then by tracking validity of each column of the matrix (which correspond to places) as we
|
2024-03-13 12:53:18 +00:00
|
|
|
//! recurse into subpatterns. That second part is done through [`PlaceValidity`], most notably
|
|
|
|
//! [`PlaceValidity::specialize`].
|
2023-11-18 03:17:50 +00:00
|
|
|
//!
|
2024-03-20 18:24:42 +00:00
|
|
|
//! Having said all that, we don't fully follow what's been presented in this section. For
|
|
|
|
//! backwards-compatibility, we ignore place validity when checking whether a pattern is required
|
|
|
|
//! for exhaustiveness in two cases: when the `exhaustive_patterns` feature gate is on, or when the
|
|
|
|
//! match scrutinee itself has type `!` or `EmptyEnum`. I (Nadrieril) hope to deprecate this
|
|
|
|
//! exception.
|
2023-11-18 03:17:50 +00:00
|
|
|
//!
|
|
|
|
//!
|
|
|
|
//!
|
2023-10-23 06:19:10 +00:00
|
|
|
//! # Full example
|
|
|
|
//!
|
|
|
|
//! We illustrate a full run of the algorithm on the following match.
|
|
|
|
//!
|
|
|
|
//! ```compile_fail,E0004
|
|
|
|
//! # struct Pair(Option<u32>, bool);
|
|
|
|
//! # fn foo(x: Pair) -> u32 {
|
|
|
|
//! match x {
|
|
|
|
//! Pair(Some(0), _) => 1,
|
|
|
|
//! Pair(_, false) => 2,
|
|
|
|
//! Pair(Some(0), false) => 3,
|
|
|
|
//! }
|
|
|
|
//! # }
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! We keep track of the original row for illustration purposes, this is not what the algorithm
|
2023-11-18 03:17:50 +00:00
|
|
|
//! actually does (it tracks usefulness as a boolean on each row).
|
2023-10-23 06:19:10 +00:00
|
|
|
//!
|
|
|
|
//! ```text
|
|
|
|
//! ┐ Patterns:
|
|
|
|
//! │ 1. `[Pair(Some(0), _)]`
|
|
|
|
//! │ 2. `[Pair(_, false)]`
|
|
|
|
//! │ 3. `[Pair(Some(0), false)]`
|
|
|
|
//! │
|
|
|
|
//! │ Specialize with `Pair`:
|
|
|
|
//! ├─┐ Patterns:
|
|
|
|
//! │ │ 1. `[Some(0), _]`
|
|
|
|
//! │ │ 2. `[_, false]`
|
|
|
|
//! │ │ 3. `[Some(0), false]`
|
|
|
|
//! │ │
|
|
|
|
//! │ │ Specialize with `Some`:
|
|
|
|
//! │ ├─┐ Patterns:
|
|
|
|
//! │ │ │ 1. `[0, _]`
|
|
|
|
//! │ │ │ 2. `[_, false]`
|
|
|
|
//! │ │ │ 3. `[0, false]`
|
|
|
|
//! │ │ │
|
|
|
|
//! │ │ │ Specialize with `0`:
|
|
|
|
//! │ │ ├─┐ Patterns:
|
|
|
|
//! │ │ │ │ 1. `[_]`
|
|
|
|
//! │ │ │ │ 3. `[false]`
|
|
|
|
//! │ │ │ │
|
|
|
|
//! │ │ │ │ Specialize with `true`:
|
|
|
|
//! │ │ │ ├─┐ Patterns:
|
|
|
|
//! │ │ │ │ │ 1. `[]`
|
|
|
|
//! │ │ │ │ │
|
2023-11-18 03:17:50 +00:00
|
|
|
//! │ │ │ │ │ We note arm 1 is useful (by `Pair(Some(0), true)`).
|
2023-10-23 06:19:10 +00:00
|
|
|
//! │ │ │ ├─┘
|
|
|
|
//! │ │ │ │
|
|
|
|
//! │ │ │ │ Specialize with `false`:
|
|
|
|
//! │ │ │ ├─┐ Patterns:
|
|
|
|
//! │ │ │ │ │ 1. `[]`
|
|
|
|
//! │ │ │ │ │ 3. `[]`
|
|
|
|
//! │ │ │ │ │
|
2023-11-18 03:17:50 +00:00
|
|
|
//! │ │ │ │ │ We note arm 1 is useful (by `Pair(Some(0), false)`).
|
2023-10-23 06:19:10 +00:00
|
|
|
//! │ │ │ ├─┘
|
|
|
|
//! │ │ ├─┘
|
|
|
|
//! │ │ │
|
|
|
|
//! │ │ │ Specialize with `1..`:
|
|
|
|
//! │ │ ├─┐ Patterns:
|
|
|
|
//! │ │ │ │ 2. `[false]`
|
|
|
|
//! │ │ │ │
|
|
|
|
//! │ │ │ │ Specialize with `true`:
|
|
|
|
//! │ │ │ ├─┐ Patterns:
|
|
|
|
//! │ │ │ │ │ // no rows left
|
|
|
|
//! │ │ │ │ │
|
|
|
|
//! │ │ │ │ │ We have found an unmatched value (`Pair(Some(1..), true)`)! This gives us a witness.
|
|
|
|
//! │ │ │ │ │ New witnesses:
|
|
|
|
//! │ │ │ │ │ `[]`
|
|
|
|
//! │ │ │ ├─┘
|
|
|
|
//! │ │ │ │ Unspecialize new witnesses with `true`:
|
|
|
|
//! │ │ │ │ `[true]`
|
|
|
|
//! │ │ │ │
|
|
|
|
//! │ │ │ │ Specialize with `false`:
|
|
|
|
//! │ │ │ ├─┐ Patterns:
|
|
|
|
//! │ │ │ │ │ 2. `[]`
|
|
|
|
//! │ │ │ │ │
|
2023-11-18 03:17:50 +00:00
|
|
|
//! │ │ │ │ │ We note arm 2 is useful (by `Pair(Some(1..), false)`).
|
2023-10-23 06:19:10 +00:00
|
|
|
//! │ │ │ ├─┘
|
|
|
|
//! │ │ │ │
|
|
|
|
//! │ │ │ │ Total witnesses for `1..`:
|
|
|
|
//! │ │ │ │ `[true]`
|
|
|
|
//! │ │ ├─┘
|
|
|
|
//! │ │ │ Unspecialize new witnesses with `1..`:
|
|
|
|
//! │ │ │ `[1.., true]`
|
|
|
|
//! │ │ │
|
|
|
|
//! │ │ │ Total witnesses for `Some`:
|
|
|
|
//! │ │ │ `[1.., true]`
|
|
|
|
//! │ ├─┘
|
|
|
|
//! │ │ Unspecialize new witnesses with `Some`:
|
|
|
|
//! │ │ `[Some(1..), true]`
|
|
|
|
//! │ │
|
|
|
|
//! │ │ Specialize with `None`:
|
|
|
|
//! │ ├─┐ Patterns:
|
|
|
|
//! │ │ │ 2. `[false]`
|
|
|
|
//! │ │ │
|
|
|
|
//! │ │ │ Specialize with `true`:
|
|
|
|
//! │ │ ├─┐ Patterns:
|
|
|
|
//! │ │ │ │ // no rows left
|
|
|
|
//! │ │ │ │
|
|
|
|
//! │ │ │ │ We have found an unmatched value (`Pair(None, true)`)! This gives us a witness.
|
|
|
|
//! │ │ │ │ New witnesses:
|
|
|
|
//! │ │ │ │ `[]`
|
|
|
|
//! │ │ ├─┘
|
|
|
|
//! │ │ │ Unspecialize new witnesses with `true`:
|
|
|
|
//! │ │ │ `[true]`
|
|
|
|
//! │ │ │
|
|
|
|
//! │ │ │ Specialize with `false`:
|
|
|
|
//! │ │ ├─┐ Patterns:
|
|
|
|
//! │ │ │ │ 2. `[]`
|
|
|
|
//! │ │ │ │
|
2023-11-18 03:17:50 +00:00
|
|
|
//! │ │ │ │ We note arm 2 is useful (by `Pair(None, false)`).
|
2023-10-23 06:19:10 +00:00
|
|
|
//! │ │ ├─┘
|
|
|
|
//! │ │ │
|
|
|
|
//! │ │ │ Total witnesses for `None`:
|
|
|
|
//! │ │ │ `[true]`
|
|
|
|
//! │ ├─┘
|
|
|
|
//! │ │ Unspecialize new witnesses with `None`:
|
|
|
|
//! │ │ `[None, true]`
|
|
|
|
//! │ │
|
|
|
|
//! │ │ Total witnesses for `Pair`:
|
|
|
|
//! │ │ `[Some(1..), true]`
|
|
|
|
//! │ │ `[None, true]`
|
|
|
|
//! ├─┘
|
|
|
|
//! │ Unspecialize new witnesses with `Pair`:
|
|
|
|
//! │ `[Pair(Some(1..), true)]`
|
|
|
|
//! │ `[Pair(None, true)]`
|
|
|
|
//! │
|
|
|
|
//! │ Final witnesses:
|
|
|
|
//! │ `[Pair(Some(1..), true)]`
|
|
|
|
//! │ `[Pair(None, true)]`
|
|
|
|
//! ┘
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! We conclude:
|
2023-11-18 03:17:50 +00:00
|
|
|
//! - Arm 3 is redundant (it was never marked as useful);
|
2023-10-23 06:19:10 +00:00
|
|
|
//! - The match is not exhaustive;
|
|
|
|
//! - Adding arms with `Pair(Some(1..), true)` and `Pair(None, true)` would make the match exhaustive.
|
|
|
|
//!
|
|
|
|
//! Note that when we're deep in the algorithm, we don't know what specialization steps got us here.
|
|
|
|
//! We can only figure out what our witnesses correspond to by unspecializing back up the stack.
|
|
|
|
//!
|
|
|
|
//!
|
|
|
|
//! # Tests
|
|
|
|
//!
|
|
|
|
//! Note: tests specific to this file can be found in:
|
|
|
|
//!
|
|
|
|
//! - `ui/pattern/usefulness`
|
|
|
|
//! - `ui/or-patterns`
|
|
|
|
//! - `ui/consts/const_in_pattern`
|
|
|
|
//! - `ui/rfc-2008-non-exhaustive`
|
|
|
|
//! - `ui/half-open-range-patterns`
|
|
|
|
//! - probably many others
|
|
|
|
//!
|
|
|
|
//! I (Nadrieril) prefer to put new tests in `ui/pattern/usefulness` unless there's a specific
|
|
|
|
//! reason not to, for example if they crucially depend on a particular feature like `or_patterns`.
|
|
|
|
|
2024-04-29 06:24:06 +00:00
|
|
|
use std::fmt;
|
2024-07-28 22:13:50 +00:00
|
|
|
|
2024-04-29 06:24:06 +00:00
|
|
|
#[cfg(feature = "rustc")]
|
|
|
|
use rustc_data_structures::stack::ensure_sufficient_stack;
|
2024-07-21 12:46:05 +00:00
|
|
|
use rustc_hash::{FxHashMap, FxHashSet};
|
2025-01-07 15:19:05 +00:00
|
|
|
use rustc_index::bit_set::DenseBitSet;
|
2023-12-10 21:14:00 +00:00
|
|
|
use smallvec::{SmallVec, smallvec};
|
2024-04-29 06:24:06 +00:00
|
|
|
use tracing::{debug, instrument};
|
2020-11-21 22:41:17 +00:00
|
|
|
|
2023-12-11 19:59:32 +00:00
|
|
|
use self::PlaceValidity::*;
|
|
|
|
use crate::constructor::{Constructor, ConstructorSet, IntRange};
|
|
|
|
use crate::pat::{DeconstructedPat, PatId, PatOrWild, WitnessPat};
|
|
|
|
use crate::{Captures, MatchArm, PatCx, PrivateUninhabitedField};
|
|
|
|
#[cfg(not(feature = "rustc"))]
|
|
|
|
pub fn ensure_sufficient_stack<R>(f: impl FnOnce() -> R) -> R {
|
|
|
|
f()
|
|
|
|
}
|
|
|
|
|
2024-07-21 12:46:05 +00:00
|
|
|
/// A pattern is a "branch" if it is the immediate child of an or-pattern, or if it is the whole
|
|
|
|
/// pattern of a match arm. These are the patterns that can be meaningfully considered "redundant",
|
|
|
|
/// since e.g. `0` in `(0, 1)` cannot be redundant on its own.
|
|
|
|
///
|
|
|
|
/// We track for each branch pattern whether it is useful, and if not why.
|
|
|
|
struct BranchPatUsefulness<'p, Cx: PatCx> {
|
|
|
|
/// Whether this pattern is useful.
|
|
|
|
useful: bool,
|
|
|
|
/// A set of patterns that:
|
|
|
|
/// - come before this one in the match;
|
|
|
|
/// - intersect this one;
|
|
|
|
/// - at the end of the algorithm, if `!self.useful`, their union covers this pattern.
|
|
|
|
covered_by: FxHashSet<&'p DeconstructedPat<Cx>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'p, Cx: PatCx> BranchPatUsefulness<'p, Cx> {
|
|
|
|
/// Update `self` with the usefulness information found in `row`.
|
|
|
|
fn update(&mut self, row: &MatrixRow<'p, Cx>, matrix: &Matrix<'p, Cx>) {
|
|
|
|
self.useful |= row.useful;
|
|
|
|
// This deserves an explanation: `intersects_at_least` does not contain all intersections
|
|
|
|
// because we skip irrelevant values (see the docs for `intersects_at_least` for an
|
|
|
|
// example). Yet we claim this suffices to build a covering set.
|
|
|
|
//
|
|
|
|
// Let `p` be our pattern. Assume it is found not useful. For a value `v`, if the value was
|
|
|
|
// relevant then we explored that value and found that there was another pattern `q` before
|
|
|
|
// `p` that matches it too. We therefore recorded an intersection with `q`. If `v` was
|
|
|
|
// irrelevant, we know there's another value `v2` that matches strictly fewer rows (while
|
|
|
|
// still matching our row) and is relevant. Since `p` is not useful, there must have been a
|
|
|
|
// `q` before `p` that matches `v2`, and we recorded that intersection. Since `v2` matches
|
|
|
|
// strictly fewer rows than `v`, `q` also matches `v`. In either case, we recorded in
|
|
|
|
// `intersects_at_least` a pattern that matches `v`. Hence using `intersects_at_least` is
|
|
|
|
// sufficient to build a covering set.
|
|
|
|
for row_id in row.intersects_at_least.iter() {
|
|
|
|
let row = &matrix.rows[row_id];
|
|
|
|
if row.useful && !row.is_under_guard {
|
|
|
|
if let PatOrWild::Pat(intersecting) = row.head() {
|
|
|
|
self.covered_by.insert(intersecting);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check whether this pattern is redundant, and if so explain why.
|
|
|
|
fn is_redundant(&self) -> Option<RedundancyExplanation<'p, Cx>> {
|
|
|
|
if self.useful {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
// We avoid instability by sorting by `uid`. The order of `uid`s only depends on the
|
|
|
|
// pattern structure.
|
|
|
|
#[cfg_attr(feature = "rustc", allow(rustc::potential_query_instability))]
|
|
|
|
let mut covered_by: Vec<_> = self.covered_by.iter().copied().collect();
|
|
|
|
covered_by.sort_by_key(|pat| pat.uid); // sort to avoid instability
|
|
|
|
Some(RedundancyExplanation { covered_by })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'p, Cx: PatCx> Default for BranchPatUsefulness<'p, Cx> {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self { useful: Default::default(), covered_by: Default::default() }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-24 19:57:11 +00:00
|
|
|
/// Context that provides information for usefulness checking.
|
2024-07-21 12:46:05 +00:00
|
|
|
struct UsefulnessCtxt<'a, 'p, Cx: PatCx> {
|
2024-01-24 19:57:11 +00:00
|
|
|
/// The context for type information.
|
2024-02-07 03:26:54 +00:00
|
|
|
tycx: &'a Cx,
|
2024-07-21 12:46:05 +00:00
|
|
|
/// Track information about the usefulness of branch patterns (see definition of "branch
|
|
|
|
/// pattern" at [`BranchPatUsefulness`]).
|
|
|
|
branch_usefulness: FxHashMap<PatId, BranchPatUsefulness<'p, Cx>>,
|
2024-03-02 21:48:41 +00:00
|
|
|
complexity_limit: Option<usize>,
|
|
|
|
complexity_level: usize,
|
|
|
|
}
|
|
|
|
|
2024-07-21 12:46:05 +00:00
|
|
|
impl<'a, 'p, Cx: PatCx> UsefulnessCtxt<'a, 'p, Cx> {
|
2024-03-02 21:48:41 +00:00
|
|
|
fn increase_complexity_level(&mut self, complexity_add: usize) -> Result<(), Cx::Error> {
|
|
|
|
self.complexity_level += complexity_add;
|
|
|
|
if self
|
|
|
|
.complexity_limit
|
|
|
|
.is_some_and(|complexity_limit| complexity_limit < self.complexity_level)
|
|
|
|
{
|
|
|
|
return self.tycx.complexity_exceeded();
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2024-01-24 19:57:11 +00:00
|
|
|
}
|
|
|
|
|
2023-12-15 15:32:44 +00:00
|
|
|
/// Context that provides information local to a place under investigation.
|
2024-03-13 12:56:06 +00:00
|
|
|
struct PlaceCtxt<'a, Cx: PatCx> {
|
2024-01-24 20:51:17 +00:00
|
|
|
cx: &'a Cx,
|
2023-12-15 15:32:44 +00:00
|
|
|
/// Type of the place under investigation.
|
2024-01-24 20:16:57 +00:00
|
|
|
ty: &'a Cx::Ty,
|
2020-11-21 22:41:17 +00:00
|
|
|
}
|
|
|
|
|
2024-03-13 12:56:06 +00:00
|
|
|
impl<'a, Cx: PatCx> Copy for PlaceCtxt<'a, Cx> {}
|
|
|
|
impl<'a, Cx: PatCx> Clone for PlaceCtxt<'a, Cx> {
|
2024-01-27 12:18:33 +00:00
|
|
|
fn clone(&self) -> Self {
|
2024-01-24 20:51:17 +00:00
|
|
|
Self { cx: self.cx, ty: self.ty }
|
2024-01-27 12:18:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-13 12:56:06 +00:00
|
|
|
impl<'a, Cx: PatCx> fmt::Debug for PlaceCtxt<'a, Cx> {
|
2024-01-27 12:18:33 +00:00
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
fmt.debug_struct("PlaceCtxt").field("ty", self.ty).finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-13 12:56:06 +00:00
|
|
|
impl<'a, Cx: PatCx> PlaceCtxt<'a, Cx> {
|
2024-01-24 20:16:57 +00:00
|
|
|
fn ctor_arity(&self, ctor: &Constructor<Cx>) -> usize {
|
2024-01-24 20:51:17 +00:00
|
|
|
self.cx.ctor_arity(ctor, self.ty)
|
2023-12-15 15:53:29 +00:00
|
|
|
}
|
2024-01-24 20:16:57 +00:00
|
|
|
fn wild_from_ctor(&self, ctor: Constructor<Cx>) -> WitnessPat<Cx> {
|
2024-01-24 20:51:17 +00:00
|
|
|
WitnessPat::wild_from_ctor(self.cx, ctor, self.ty.clone())
|
2024-01-24 20:16:57 +00:00
|
|
|
}
|
2023-11-18 02:52:54 +00:00
|
|
|
}
|
|
|
|
|
2024-03-13 12:53:18 +00:00
|
|
|
/// Track whether a given place (aka column) is known to contain a valid value or not.
|
2023-11-18 02:52:54 +00:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
2024-03-13 12:53:18 +00:00
|
|
|
pub enum PlaceValidity {
|
2023-11-18 02:52:54 +00:00
|
|
|
ValidOnly,
|
|
|
|
MaybeInvalid,
|
|
|
|
}
|
|
|
|
|
2024-03-13 12:53:18 +00:00
|
|
|
impl PlaceValidity {
|
2023-12-11 19:46:35 +00:00
|
|
|
pub fn from_bool(is_valid_only: bool) -> Self {
|
2023-11-18 02:52:54 +00:00
|
|
|
if is_valid_only { ValidOnly } else { MaybeInvalid }
|
|
|
|
}
|
|
|
|
|
2023-12-11 09:56:21 +00:00
|
|
|
fn is_known_valid(self) -> bool {
|
2023-11-18 20:39:57 +00:00
|
|
|
matches!(self, ValidOnly)
|
|
|
|
}
|
|
|
|
|
2023-11-18 02:52:54 +00:00
|
|
|
/// If the place has validity given by `self` and we read that the value at the place has
|
|
|
|
/// constructor `ctor`, this computes what we can assume about the validity of the constructor
|
|
|
|
/// fields.
|
|
|
|
///
|
|
|
|
/// Pending further opsem decisions, the current behavior is: validity is preserved, except
|
2023-11-18 20:39:57 +00:00
|
|
|
/// inside `&` and union fields where validity is reset to `MaybeInvalid`.
|
2024-03-13 12:56:06 +00:00
|
|
|
fn specialize<Cx: PatCx>(self, ctor: &Constructor<Cx>) -> Self {
|
2023-11-18 20:39:57 +00:00
|
|
|
// We preserve validity except when we go inside a reference or a union field.
|
2023-12-11 12:32:34 +00:00
|
|
|
if matches!(ctor, Constructor::Ref | Constructor::UnionField) {
|
2023-11-18 02:52:54 +00:00
|
|
|
// Validity of `x: &T` does not imply validity of `*x: T`.
|
|
|
|
MaybeInvalid
|
|
|
|
} else {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-13 12:53:18 +00:00
|
|
|
impl fmt::Display for PlaceValidity {
|
2023-11-18 02:52:54 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
let s = match self {
|
|
|
|
ValidOnly => "✓",
|
2024-01-10 21:42:23 +00:00
|
|
|
MaybeInvalid => "?",
|
2023-11-18 02:52:54 +00:00
|
|
|
};
|
|
|
|
write!(f, "{s}")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-06 02:52:46 +00:00
|
|
|
/// Data about a place under investigation. Its methods contain a lot of the logic used to analyze
|
|
|
|
/// the constructors in the matrix.
|
2024-03-13 12:56:06 +00:00
|
|
|
struct PlaceInfo<Cx: PatCx> {
|
2024-01-31 01:37:44 +00:00
|
|
|
/// The type of the place.
|
|
|
|
ty: Cx::Ty,
|
2024-02-28 16:56:01 +00:00
|
|
|
/// Whether the place is a private uninhabited field. If so we skip this field during analysis
|
2024-02-06 02:50:22 +00:00
|
|
|
/// so that we don't observe its emptiness.
|
2024-02-28 16:56:01 +00:00
|
|
|
private_uninhabited: bool,
|
2024-01-31 01:37:44 +00:00
|
|
|
/// Whether the place is known to contain valid data.
|
2024-03-13 12:53:18 +00:00
|
|
|
validity: PlaceValidity,
|
2024-01-31 01:46:10 +00:00
|
|
|
/// Whether the place is the scrutinee itself or a subplace of it.
|
|
|
|
is_scrutinee: bool,
|
2024-01-31 01:37:44 +00:00
|
|
|
}
|
|
|
|
|
2024-03-13 12:56:06 +00:00
|
|
|
impl<Cx: PatCx> PlaceInfo<Cx> {
|
2024-02-06 02:52:46 +00:00
|
|
|
/// Given a constructor for the current place, we return one `PlaceInfo` for each field of the
|
|
|
|
/// constructor.
|
2024-01-31 01:37:44 +00:00
|
|
|
fn specialize<'a>(
|
|
|
|
&'a self,
|
|
|
|
cx: &'a Cx,
|
|
|
|
ctor: &'a Constructor<Cx>,
|
|
|
|
) -> impl Iterator<Item = Self> + ExactSizeIterator + Captures<'a> {
|
|
|
|
let ctor_sub_tys = cx.ctor_sub_tys(ctor, &self.ty);
|
|
|
|
let ctor_sub_validity = self.validity.specialize(ctor);
|
2024-02-28 16:56:01 +00:00
|
|
|
ctor_sub_tys.map(move |(ty, PrivateUninhabitedField(private_uninhabited))| PlaceInfo {
|
2024-01-31 01:46:10 +00:00
|
|
|
ty,
|
2024-02-28 16:56:01 +00:00
|
|
|
private_uninhabited,
|
2024-01-31 01:46:10 +00:00
|
|
|
validity: ctor_sub_validity,
|
|
|
|
is_scrutinee: false,
|
|
|
|
})
|
2024-01-31 01:37:44 +00:00
|
|
|
}
|
2024-02-06 02:52:46 +00:00
|
|
|
|
|
|
|
/// This analyzes a column of constructors corresponding to the current place. It returns a pair
|
|
|
|
/// `(split_ctors, missing_ctors)`.
|
|
|
|
///
|
|
|
|
/// `split_ctors` is a splitted list of constructors that cover the whole type. This will be
|
|
|
|
/// used to specialize the matrix.
|
|
|
|
///
|
|
|
|
/// `missing_ctors` is a list of the constructors not found in the column, for reporting
|
|
|
|
/// purposes.
|
|
|
|
fn split_column_ctors<'a>(
|
|
|
|
&self,
|
|
|
|
cx: &Cx,
|
|
|
|
ctors: impl Iterator<Item = &'a Constructor<Cx>> + Clone,
|
|
|
|
) -> Result<(SmallVec<[Constructor<Cx>; 1]>, Vec<Constructor<Cx>>), Cx::Error>
|
|
|
|
where
|
|
|
|
Cx: 'a,
|
|
|
|
{
|
2024-03-29 18:40:54 +00:00
|
|
|
debug!(?self.ty);
|
2024-02-28 16:56:01 +00:00
|
|
|
if self.private_uninhabited {
|
2024-02-06 02:50:22 +00:00
|
|
|
// Skip the whole column
|
2024-02-28 16:56:01 +00:00
|
|
|
return Ok((smallvec![Constructor::PrivateUninhabited], vec![]));
|
2024-02-06 02:50:22 +00:00
|
|
|
}
|
|
|
|
|
2024-07-20 20:18:35 +00:00
|
|
|
if ctors.clone().any(|c| matches!(c, Constructor::Or)) {
|
|
|
|
// If any constructor is `Or`, we expand or-patterns.
|
|
|
|
return Ok((smallvec![Constructor::Or], vec![]));
|
|
|
|
}
|
|
|
|
|
2024-02-06 02:52:46 +00:00
|
|
|
let ctors_for_ty = cx.ctors_for_ty(&self.ty)?;
|
2024-03-29 18:40:54 +00:00
|
|
|
debug!(?ctors_for_ty);
|
2024-02-06 02:52:46 +00:00
|
|
|
|
|
|
|
// We treat match scrutinees of type `!` or `EmptyEnum` differently.
|
|
|
|
let is_toplevel_exception =
|
|
|
|
self.is_scrutinee && matches!(ctors_for_ty, ConstructorSet::NoConstructors);
|
|
|
|
// Whether empty patterns are counted as useful or not. We only warn an empty arm unreachable if
|
|
|
|
// it is guaranteed unreachable by the opsem (i.e. if the place is `known_valid`).
|
2024-08-28 18:10:26 +00:00
|
|
|
// We don't want to warn empty patterns as unreachable by default just yet. We will in a
|
|
|
|
// later version of rust or under a different lint name, see
|
|
|
|
// https://github.com/rust-lang/rust/pull/129103.
|
|
|
|
let empty_arms_are_unreachable = self.validity.is_known_valid()
|
|
|
|
&& (is_toplevel_exception || cx.is_exhaustive_patterns_feature_on());
|
2024-02-06 02:52:46 +00:00
|
|
|
// Whether empty patterns can be omitted for exhaustiveness. We ignore place validity in the
|
|
|
|
// toplevel exception and `exhaustive_patterns` cases for backwards compatibility.
|
2024-03-20 18:24:42 +00:00
|
|
|
let can_omit_empty_arms = self.validity.is_known_valid()
|
2024-02-06 02:52:46 +00:00
|
|
|
|| is_toplevel_exception
|
|
|
|
|| cx.is_exhaustive_patterns_feature_on();
|
|
|
|
|
|
|
|
// Analyze the constructors present in this column.
|
|
|
|
let mut split_set = ctors_for_ty.split(ctors);
|
2024-03-29 18:40:54 +00:00
|
|
|
debug!(?split_set);
|
2024-02-06 02:52:46 +00:00
|
|
|
let all_missing = split_set.present.is_empty();
|
|
|
|
|
|
|
|
// Build the set of constructors we will specialize with. It must cover the whole type, so
|
|
|
|
// we add `Missing` to represent the missing ones. This is explained under "Constructor
|
|
|
|
// Splitting" at the top of this file.
|
|
|
|
let mut split_ctors = split_set.present;
|
|
|
|
if !(split_set.missing.is_empty()
|
|
|
|
&& (split_set.missing_empty.is_empty() || empty_arms_are_unreachable))
|
|
|
|
{
|
|
|
|
split_ctors.push(Constructor::Missing);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Which empty constructors are considered missing. We ensure that
|
|
|
|
// `!missing_ctors.is_empty() => split_ctors.contains(Missing)`. The converse usually holds
|
|
|
|
// except when `!self.validity.is_known_valid()`.
|
|
|
|
let mut missing_ctors = split_set.missing;
|
|
|
|
if !can_omit_empty_arms {
|
|
|
|
missing_ctors.append(&mut split_set.missing_empty);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Whether we should report "Enum::A and Enum::C are missing" or "_ is missing". At the top
|
|
|
|
// level we prefer to list all constructors.
|
|
|
|
let report_individual_missing_ctors = self.is_scrutinee || !all_missing;
|
|
|
|
if !missing_ctors.is_empty() && !report_individual_missing_ctors {
|
|
|
|
// Report `_` as missing.
|
|
|
|
missing_ctors = vec![Constructor::Wildcard];
|
|
|
|
} else if missing_ctors.iter().any(|c| c.is_non_exhaustive()) {
|
|
|
|
// We need to report a `_` anyway, so listing other constructors would be redundant.
|
|
|
|
// `NonExhaustive` is displayed as `_` just like `Wildcard`, but it will be picked
|
|
|
|
// up by diagnostics to add a note about why `_` is required here.
|
|
|
|
missing_ctors = vec![Constructor::NonExhaustive];
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok((split_ctors, missing_ctors))
|
|
|
|
}
|
2024-01-31 01:37:44 +00:00
|
|
|
}
|
|
|
|
|
2024-03-13 12:56:06 +00:00
|
|
|
impl<Cx: PatCx> Clone for PlaceInfo<Cx> {
|
2024-01-31 01:37:44 +00:00
|
|
|
fn clone(&self) -> Self {
|
2024-02-06 02:50:22 +00:00
|
|
|
Self {
|
|
|
|
ty: self.ty.clone(),
|
2024-02-28 16:56:01 +00:00
|
|
|
private_uninhabited: self.private_uninhabited,
|
2024-02-06 02:50:22 +00:00
|
|
|
validity: self.validity,
|
|
|
|
is_scrutinee: self.is_scrutinee,
|
|
|
|
}
|
2024-01-31 01:37:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-19 22:56:27 +00:00
|
|
|
/// Represents a pattern-tuple under investigation.
|
2023-12-11 16:57:53 +00:00
|
|
|
// The three lifetimes are:
|
|
|
|
// - 'p coming from the input
|
2023-12-11 19:01:02 +00:00
|
|
|
// - Cx global compilation context
|
2024-03-13 12:56:06 +00:00
|
|
|
struct PatStack<'p, Cx: PatCx> {
|
2023-10-23 06:19:10 +00:00
|
|
|
// Rows of len 1 are very common, which is why `SmallVec[_; 2]` works well.
|
2024-01-07 10:03:40 +00:00
|
|
|
pats: SmallVec<[PatOrWild<'p, Cx>; 2]>,
|
2023-11-29 20:43:06 +00:00
|
|
|
/// Sometimes we know that as far as this row is concerned, the current case is already handled
|
2023-12-23 21:15:15 +00:00
|
|
|
/// by a different, more general, case. When the case is irrelevant for all rows this allows us
|
|
|
|
/// to skip a case entirely. This is purely an optimization. See at the top for details.
|
2023-11-29 20:43:06 +00:00
|
|
|
relevant: bool,
|
2020-10-23 21:49:26 +00:00
|
|
|
}
|
2019-11-01 15:44:58 +00:00
|
|
|
|
2024-03-13 12:56:06 +00:00
|
|
|
impl<'p, Cx: PatCx> Clone for PatStack<'p, Cx> {
|
2024-01-27 12:18:33 +00:00
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self { pats: self.pats.clone(), relevant: self.relevant }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-13 12:56:06 +00:00
|
|
|
impl<'p, Cx: PatCx> PatStack<'p, Cx> {
|
2024-01-25 02:37:24 +00:00
|
|
|
fn from_pattern(pat: &'p DeconstructedPat<Cx>) -> Self {
|
2024-01-07 10:03:40 +00:00
|
|
|
PatStack { pats: smallvec![PatOrWild::Pat(pat)], relevant: true }
|
2019-11-01 15:44:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn len(&self) -> usize {
|
2020-10-23 21:49:26 +00:00
|
|
|
self.pats.len()
|
2019-11-01 15:44:58 +00:00
|
|
|
}
|
|
|
|
|
2024-01-07 10:03:40 +00:00
|
|
|
fn head(&self) -> PatOrWild<'p, Cx> {
|
|
|
|
self.pats[0]
|
2020-10-23 21:49:26 +00:00
|
|
|
}
|
|
|
|
|
2024-01-07 10:03:40 +00:00
|
|
|
fn iter(&self) -> impl Iterator<Item = PatOrWild<'p, Cx>> + Captures<'_> {
|
2020-10-23 21:49:26 +00:00
|
|
|
self.pats.iter().copied()
|
2019-11-01 15:44:58 +00:00
|
|
|
}
|
2019-11-01 16:33:34 +00:00
|
|
|
|
2024-07-20 20:18:35 +00:00
|
|
|
// Expand the first or-pattern into its subpatterns. Only useful if the pattern is an
|
|
|
|
// or-pattern. Panics if `self` is empty.
|
2023-12-26 02:12:33 +00:00
|
|
|
fn expand_or_pat(&self) -> impl Iterator<Item = PatStack<'p, Cx>> + Captures<'_> {
|
2024-07-20 20:18:35 +00:00
|
|
|
self.head().expand_or_pat().into_iter().map(move |pat| {
|
2023-12-01 00:14:35 +00:00
|
|
|
let mut new = self.clone();
|
|
|
|
new.pats[0] = pat;
|
|
|
|
new
|
2021-01-01 22:14:22 +00:00
|
|
|
})
|
2019-11-21 18:45:28 +00:00
|
|
|
}
|
|
|
|
|
2023-10-23 06:19:10 +00:00
|
|
|
/// This computes `specialize(ctor, self)`. See top of the file for explanations.
|
|
|
|
/// Only call if `ctor.is_covered_by(self.head().ctor())` is true.
|
2021-09-22 17:16:07 +00:00
|
|
|
fn pop_head_constructor(
|
|
|
|
&self,
|
2024-03-04 18:35:33 +00:00
|
|
|
cx: &Cx,
|
2023-12-11 19:01:02 +00:00
|
|
|
ctor: &Constructor<Cx>,
|
2024-01-03 00:25:32 +00:00
|
|
|
ctor_arity: usize,
|
2023-11-29 20:43:06 +00:00
|
|
|
ctor_is_relevant: bool,
|
2024-03-04 18:35:33 +00:00
|
|
|
) -> Result<PatStack<'p, Cx>, Cx::Error> {
|
2024-02-29 22:34:57 +00:00
|
|
|
let head_pat = self.head();
|
|
|
|
if head_pat.as_pat().is_some_and(|pat| pat.arity() > ctor_arity) {
|
|
|
|
// Arity can be smaller in case of variable-length slices, but mustn't be larger.
|
2024-03-04 18:35:33 +00:00
|
|
|
return Err(cx.bug(format_args!(
|
2024-02-29 22:34:57 +00:00
|
|
|
"uncaught type error: pattern {:?} has inconsistent arity (expected arity <= {ctor_arity})",
|
|
|
|
head_pat.as_pat().unwrap()
|
2024-03-04 18:35:33 +00:00
|
|
|
)));
|
|
|
|
}
|
2024-02-29 22:34:57 +00:00
|
|
|
// We pop the head pattern and push the new fields extracted from the arguments of
|
|
|
|
// `self.head()`.
|
|
|
|
let mut new_pats = head_pat.specialize(ctor, ctor_arity);
|
2023-11-19 22:56:27 +00:00
|
|
|
new_pats.extend_from_slice(&self.pats[1..]);
|
2023-11-29 20:43:06 +00:00
|
|
|
// `ctor` is relevant for this row if it is the actual constructor of this row, or if the
|
|
|
|
// row has a wildcard and `ctor` is relevant for wildcards.
|
|
|
|
let ctor_is_relevant =
|
|
|
|
!matches!(self.head().ctor(), Constructor::Wildcard) || ctor_is_relevant;
|
2024-03-04 18:35:33 +00:00
|
|
|
Ok(PatStack { pats: new_pats, relevant: self.relevant && ctor_is_relevant })
|
2019-11-01 16:33:34 +00:00
|
|
|
}
|
2019-11-01 15:44:58 +00:00
|
|
|
}
|
|
|
|
|
2024-03-13 12:56:06 +00:00
|
|
|
impl<'p, Cx: PatCx> fmt::Debug for PatStack<'p, Cx> {
|
2020-12-31 18:48:08 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2023-11-19 22:56:27 +00:00
|
|
|
// We pretty-print similarly to the `Debug` impl of `Matrix`.
|
2020-12-31 18:48:08 +00:00
|
|
|
write!(f, "+")?;
|
|
|
|
for pat in self.iter() {
|
2023-07-25 21:17:39 +00:00
|
|
|
write!(f, " {pat:?} +")?;
|
2020-12-31 18:48:08 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-19 22:56:27 +00:00
|
|
|
/// A row of the matrix.
|
|
|
|
#[derive(Clone)]
|
2024-03-13 12:56:06 +00:00
|
|
|
struct MatrixRow<'p, Cx: PatCx> {
|
2023-11-19 22:56:27 +00:00
|
|
|
// The patterns in the row.
|
2023-12-26 01:59:18 +00:00
|
|
|
pats: PatStack<'p, Cx>,
|
2023-11-19 22:56:27 +00:00
|
|
|
/// Whether the original arm had a guard. This is inherited when specializing.
|
|
|
|
is_under_guard: bool,
|
|
|
|
/// When we specialize, we remember which row of the original matrix produced a given row of the
|
2023-11-18 03:17:50 +00:00
|
|
|
/// specialized matrix. When we unspecialize, we use this to propagate usefulness back up the
|
2024-03-17 14:48:27 +00:00
|
|
|
/// callstack. On creation, this stores the index of the original match arm.
|
2023-11-19 22:56:27 +00:00
|
|
|
parent_row: usize,
|
|
|
|
/// False when the matrix is just built. This is set to `true` by
|
2023-11-18 03:17:50 +00:00
|
|
|
/// [`compute_exhaustiveness_and_usefulness`] if the arm is found to be useful.
|
2023-11-19 22:56:27 +00:00
|
|
|
/// This is reset to `false` when specializing.
|
2023-11-18 03:17:50 +00:00
|
|
|
useful: bool,
|
2024-07-21 12:46:05 +00:00
|
|
|
/// Tracks some rows above this one that have an intersection with this one, i.e. such that
|
|
|
|
/// there is a value that matches both rows.
|
|
|
|
/// Because of relevancy we may miss some intersections. The intersections we do find are
|
|
|
|
/// correct. In other words, this is an underapproximation of the real set of intersections.
|
|
|
|
///
|
|
|
|
/// For example:
|
|
|
|
/// ```rust,ignore(illustrative)
|
|
|
|
/// match ... {
|
|
|
|
/// (true, _, _) => {} // `intersects_at_least = []`
|
|
|
|
/// (_, true, 0..=10) => {} // `intersects_at_least = []`
|
|
|
|
/// (_, true, 5..15) => {} // `intersects_at_least = [1]`
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
/// Here the `(true, true)` case is irrelevant. Since we skip it, we will not detect that row 0
|
|
|
|
/// intersects rows 1 and 2.
|
2025-01-07 15:19:05 +00:00
|
|
|
intersects_at_least: DenseBitSet<usize>,
|
2024-07-21 12:46:05 +00:00
|
|
|
/// Whether the head pattern is a branch (see definition of "branch pattern" at
|
|
|
|
/// [`BranchPatUsefulness`])
|
|
|
|
head_is_branch: bool,
|
2023-11-19 22:56:27 +00:00
|
|
|
}
|
|
|
|
|
2024-03-13 12:56:06 +00:00
|
|
|
impl<'p, Cx: PatCx> MatrixRow<'p, Cx> {
|
2024-07-21 12:46:05 +00:00
|
|
|
fn new(arm: &MatchArm<'p, Cx>, arm_id: usize) -> Self {
|
|
|
|
MatrixRow {
|
|
|
|
pats: PatStack::from_pattern(arm.pat),
|
|
|
|
parent_row: arm_id,
|
|
|
|
is_under_guard: arm.has_guard,
|
|
|
|
useful: false,
|
2025-01-07 15:19:05 +00:00
|
|
|
intersects_at_least: DenseBitSet::new_empty(0), // Initialized in `Matrix::push`.
|
2024-07-21 12:46:05 +00:00
|
|
|
// This pattern is a branch because it comes from a match arm.
|
|
|
|
head_is_branch: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-19 22:56:27 +00:00
|
|
|
fn len(&self) -> usize {
|
|
|
|
self.pats.len()
|
|
|
|
}
|
|
|
|
|
2024-01-07 10:03:40 +00:00
|
|
|
fn head(&self) -> PatOrWild<'p, Cx> {
|
2023-11-19 22:56:27 +00:00
|
|
|
self.pats.head()
|
|
|
|
}
|
|
|
|
|
2024-01-07 10:03:40 +00:00
|
|
|
fn iter(&self) -> impl Iterator<Item = PatOrWild<'p, Cx>> + Captures<'_> {
|
2023-11-19 22:56:27 +00:00
|
|
|
self.pats.iter()
|
|
|
|
}
|
|
|
|
|
2024-07-20 20:18:35 +00:00
|
|
|
// Expand the first or-pattern (if any) into its subpatterns. Panics if `self` is empty.
|
|
|
|
fn expand_or_pat(
|
|
|
|
&self,
|
|
|
|
parent_row: usize,
|
|
|
|
) -> impl Iterator<Item = MatrixRow<'p, Cx>> + Captures<'_> {
|
2024-07-21 12:46:05 +00:00
|
|
|
let is_or_pat = self.pats.head().is_or_pat();
|
2024-07-20 20:18:35 +00:00
|
|
|
self.pats.expand_or_pat().map(move |patstack| MatrixRow {
|
2023-11-19 22:56:27 +00:00
|
|
|
pats: patstack,
|
2024-07-20 20:18:35 +00:00
|
|
|
parent_row,
|
2023-11-19 22:56:27 +00:00
|
|
|
is_under_guard: self.is_under_guard,
|
2023-11-18 03:17:50 +00:00
|
|
|
useful: false,
|
2025-01-07 15:19:05 +00:00
|
|
|
intersects_at_least: DenseBitSet::new_empty(0), // Initialized in `Matrix::push`.
|
2024-07-21 12:46:05 +00:00
|
|
|
head_is_branch: is_or_pat,
|
2023-11-19 22:56:27 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This computes `specialize(ctor, self)`. See top of the file for explanations.
|
|
|
|
/// Only call if `ctor.is_covered_by(self.head().ctor())` is true.
|
|
|
|
fn pop_head_constructor(
|
|
|
|
&self,
|
2024-03-04 18:35:33 +00:00
|
|
|
cx: &Cx,
|
2023-12-11 19:01:02 +00:00
|
|
|
ctor: &Constructor<Cx>,
|
2024-01-03 00:25:32 +00:00
|
|
|
ctor_arity: usize,
|
2023-11-29 20:43:06 +00:00
|
|
|
ctor_is_relevant: bool,
|
2023-11-19 22:56:27 +00:00
|
|
|
parent_row: usize,
|
2024-03-04 18:35:33 +00:00
|
|
|
) -> Result<MatrixRow<'p, Cx>, Cx::Error> {
|
|
|
|
Ok(MatrixRow {
|
|
|
|
pats: self.pats.pop_head_constructor(cx, ctor, ctor_arity, ctor_is_relevant)?,
|
2023-11-19 22:56:27 +00:00
|
|
|
parent_row,
|
|
|
|
is_under_guard: self.is_under_guard,
|
2023-11-18 03:17:50 +00:00
|
|
|
useful: false,
|
2025-01-07 15:19:05 +00:00
|
|
|
intersects_at_least: DenseBitSet::new_empty(0), // Initialized in `Matrix::push`.
|
2024-07-21 12:46:05 +00:00
|
|
|
head_is_branch: false,
|
2024-03-04 18:35:33 +00:00
|
|
|
})
|
2023-11-19 22:56:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-13 12:56:06 +00:00
|
|
|
impl<'p, Cx: PatCx> fmt::Debug for MatrixRow<'p, Cx> {
|
2023-11-19 22:56:27 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
self.pats.fmt(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-23 06:19:10 +00:00
|
|
|
/// A 2D matrix. Represents a list of pattern-tuples under investigation.
|
|
|
|
///
|
|
|
|
/// Invariant: each row must have the same length, and each column must have the same type.
|
|
|
|
///
|
|
|
|
/// Invariant: the first column must not contain or-patterns. This is handled by
|
2024-07-20 20:18:35 +00:00
|
|
|
/// [`Matrix::push`].
|
2023-10-23 06:19:10 +00:00
|
|
|
///
|
|
|
|
/// In fact each column corresponds to a place inside the scrutinee of the match. E.g. after
|
|
|
|
/// specializing `(,)` and `Some` on a pattern of type `(Option<u32>, bool)`, the first column of
|
|
|
|
/// the matrix will correspond to `scrutinee.0.Some.0` and the second column to `scrutinee.1`.
|
2021-09-25 23:00:08 +00:00
|
|
|
#[derive(Clone)]
|
2024-03-13 12:56:06 +00:00
|
|
|
struct Matrix<'p, Cx: PatCx> {
|
2023-11-18 02:52:54 +00:00
|
|
|
/// Vector of rows. The rows must form a rectangular 2D array. Moreover, all the patterns of
|
|
|
|
/// each column must have the same type. Each column corresponds to a place within the
|
|
|
|
/// scrutinee.
|
2023-12-26 01:59:18 +00:00
|
|
|
rows: Vec<MatrixRow<'p, Cx>>,
|
2024-01-31 01:37:44 +00:00
|
|
|
/// Track info about each place. Each place corresponds to a column in `rows`, and their types
|
|
|
|
/// must match.
|
|
|
|
place_info: SmallVec<[PlaceInfo<Cx>; 2]>,
|
2024-01-06 16:52:47 +00:00
|
|
|
/// Track whether the virtual wildcard row used to compute exhaustiveness is relevant. See top
|
|
|
|
/// of the file for details on relevancy.
|
|
|
|
wildcard_row_is_relevant: bool,
|
2020-09-19 13:00:10 +00:00
|
|
|
}
|
2016-09-24 15:24:34 +00:00
|
|
|
|
2024-03-13 12:56:06 +00:00
|
|
|
impl<'p, Cx: PatCx> Matrix<'p, Cx> {
|
2024-07-20 20:18:35 +00:00
|
|
|
/// Pushes a new row to the matrix. Internal method, prefer [`Matrix::new`].
|
|
|
|
fn push(&mut self, mut row: MatrixRow<'p, Cx>) {
|
2025-01-07 15:19:05 +00:00
|
|
|
row.intersects_at_least = DenseBitSet::new_empty(self.rows.len());
|
2024-07-20 20:18:35 +00:00
|
|
|
self.rows.push(row);
|
2016-09-24 15:24:34 +00:00
|
|
|
}
|
2019-11-01 16:33:34 +00:00
|
|
|
|
2023-10-23 06:19:10 +00:00
|
|
|
/// Build a new matrix from an iterator of `MatchArm`s.
|
2024-03-13 12:53:18 +00:00
|
|
|
fn new(arms: &[MatchArm<'p, Cx>], scrut_ty: Cx::Ty, scrut_validity: PlaceValidity) -> Self {
|
2024-02-06 02:50:22 +00:00
|
|
|
let place_info = PlaceInfo {
|
|
|
|
ty: scrut_ty,
|
2024-02-28 16:56:01 +00:00
|
|
|
private_uninhabited: false,
|
2024-02-06 02:50:22 +00:00
|
|
|
validity: scrut_validity,
|
|
|
|
is_scrutinee: true,
|
|
|
|
};
|
2023-11-18 02:52:54 +00:00
|
|
|
let mut matrix = Matrix {
|
|
|
|
rows: Vec::with_capacity(arms.len()),
|
2024-01-31 01:37:44 +00:00
|
|
|
place_info: smallvec![place_info],
|
2024-01-06 16:52:47 +00:00
|
|
|
wildcard_row_is_relevant: true,
|
2023-11-18 02:52:54 +00:00
|
|
|
};
|
2024-03-17 14:48:27 +00:00
|
|
|
for (arm_id, arm) in arms.iter().enumerate() {
|
2024-07-21 12:46:05 +00:00
|
|
|
matrix.push(MatrixRow::new(arm, arm_id));
|
2023-10-23 06:19:10 +00:00
|
|
|
}
|
|
|
|
matrix
|
|
|
|
}
|
|
|
|
|
2024-01-31 01:37:44 +00:00
|
|
|
fn head_place(&self) -> Option<&PlaceInfo<Cx>> {
|
|
|
|
self.place_info.first()
|
2023-11-05 13:52:26 +00:00
|
|
|
}
|
|
|
|
fn column_count(&self) -> usize {
|
2024-01-31 01:37:44 +00:00
|
|
|
self.place_info.len()
|
2023-11-05 13:52:26 +00:00
|
|
|
}
|
|
|
|
|
2023-12-26 02:12:33 +00:00
|
|
|
fn rows(
|
|
|
|
&self,
|
|
|
|
) -> impl Iterator<Item = &MatrixRow<'p, Cx>> + Clone + DoubleEndedIterator + ExactSizeIterator
|
2023-12-11 19:01:02 +00:00
|
|
|
{
|
2023-10-15 15:36:36 +00:00
|
|
|
self.rows.iter()
|
|
|
|
}
|
2023-12-26 02:12:33 +00:00
|
|
|
fn rows_mut(
|
|
|
|
&mut self,
|
|
|
|
) -> impl Iterator<Item = &mut MatrixRow<'p, Cx>> + DoubleEndedIterator + ExactSizeIterator
|
2023-10-23 06:19:10 +00:00
|
|
|
{
|
|
|
|
self.rows.iter_mut()
|
|
|
|
}
|
2023-10-15 15:36:36 +00:00
|
|
|
|
2023-10-23 06:19:10 +00:00
|
|
|
/// Iterate over the first pattern of each row.
|
2024-01-07 10:03:40 +00:00
|
|
|
fn heads(&self) -> impl Iterator<Item = PatOrWild<'p, Cx>> + Clone + Captures<'_> {
|
2023-10-15 15:36:36 +00:00
|
|
|
self.rows().map(|r| r.head())
|
2020-11-28 22:07:15 +00:00
|
|
|
}
|
|
|
|
|
2023-10-23 06:19:10 +00:00
|
|
|
/// This computes `specialize(ctor, self)`. See top of the file for explanations.
|
2019-11-28 13:03:02 +00:00
|
|
|
fn specialize_constructor(
|
2019-11-01 16:33:34 +00:00
|
|
|
&self,
|
2024-01-03 00:34:38 +00:00
|
|
|
pcx: &PlaceCtxt<'_, Cx>,
|
2023-12-11 19:01:02 +00:00
|
|
|
ctor: &Constructor<Cx>,
|
2023-11-29 20:43:06 +00:00
|
|
|
ctor_is_relevant: bool,
|
2024-01-24 15:24:52 +00:00
|
|
|
) -> Result<Matrix<'p, Cx>, Cx::Error> {
|
2024-07-20 20:18:35 +00:00
|
|
|
if matches!(ctor, Constructor::Or) {
|
|
|
|
// Specializing with `Or` means expanding rows with or-patterns.
|
|
|
|
let mut matrix = Matrix {
|
|
|
|
rows: Vec::new(),
|
|
|
|
place_info: self.place_info.clone(),
|
|
|
|
wildcard_row_is_relevant: self.wildcard_row_is_relevant,
|
|
|
|
};
|
|
|
|
for (i, row) in self.rows().enumerate() {
|
|
|
|
for new_row in row.expand_or_pat(i) {
|
|
|
|
matrix.push(new_row);
|
|
|
|
}
|
2021-09-22 23:36:49 +00:00
|
|
|
}
|
2024-07-20 20:18:35 +00:00
|
|
|
Ok(matrix)
|
|
|
|
} else {
|
|
|
|
let subfield_place_info = self.place_info[0].specialize(pcx.cx, ctor);
|
|
|
|
let arity = subfield_place_info.len();
|
|
|
|
let specialized_place_info =
|
|
|
|
subfield_place_info.chain(self.place_info[1..].iter().cloned()).collect();
|
|
|
|
let mut matrix = Matrix {
|
|
|
|
rows: Vec::new(),
|
|
|
|
place_info: specialized_place_info,
|
|
|
|
wildcard_row_is_relevant: self.wildcard_row_is_relevant && ctor_is_relevant,
|
|
|
|
};
|
|
|
|
for (i, row) in self.rows().enumerate() {
|
|
|
|
if ctor.is_covered_by(pcx.cx, row.head().ctor())? {
|
|
|
|
let new_row =
|
|
|
|
row.pop_head_constructor(pcx.cx, ctor, arity, ctor_is_relevant, i)?;
|
|
|
|
matrix.push(new_row);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(matrix)
|
2021-09-22 23:36:49 +00:00
|
|
|
}
|
2019-11-01 16:33:34 +00:00
|
|
|
}
|
2024-01-31 02:24:24 +00:00
|
|
|
|
|
|
|
/// Recover row usefulness and intersection information from a processed specialized matrix.
|
|
|
|
/// `specialized` must come from `self.specialize_constructor`.
|
|
|
|
fn unspecialize(&mut self, specialized: Self) {
|
|
|
|
for child_row in specialized.rows() {
|
|
|
|
let parent_row_id = child_row.parent_row;
|
|
|
|
let parent_row = &mut self.rows[parent_row_id];
|
|
|
|
// A parent row is useful if any of its children is.
|
|
|
|
parent_row.useful |= child_row.useful;
|
2024-07-21 12:46:05 +00:00
|
|
|
for child_intersection in child_row.intersects_at_least.iter() {
|
2024-01-31 02:24:24 +00:00
|
|
|
// Convert the intersecting ids into ids for the parent matrix.
|
|
|
|
let parent_intersection = specialized.rows[child_intersection].parent_row;
|
|
|
|
// Note: self-intersection can happen with or-patterns.
|
|
|
|
if parent_intersection != parent_row_id {
|
2024-07-21 12:46:05 +00:00
|
|
|
parent_row.intersects_at_least.insert(parent_intersection);
|
2024-01-31 02:24:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-09-24 15:24:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Pretty-printer for matrices of patterns, example:
|
2020-05-01 20:28:15 +00:00
|
|
|
///
|
|
|
|
/// ```text
|
2019-09-26 18:47:05 +00:00
|
|
|
/// + _ + [] +
|
|
|
|
/// + true + [First] +
|
|
|
|
/// + true + [Second(true)] +
|
|
|
|
/// + false + [_] +
|
|
|
|
/// + _ + [_, _, tail @ ..] +
|
2024-03-29 18:40:54 +00:00
|
|
|
/// | ✓ | ? | // validity
|
2020-10-17 19:11:30 +00:00
|
|
|
/// ```
|
2024-03-13 12:56:06 +00:00
|
|
|
impl<'p, Cx: PatCx> fmt::Debug for Matrix<'p, Cx> {
|
2019-02-07 21:28:15 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2016-09-24 15:24:34 +00:00
|
|
|
write!(f, "\n")?;
|
|
|
|
|
2023-11-18 02:52:54 +00:00
|
|
|
let mut pretty_printed_matrix: Vec<Vec<String>> = self
|
|
|
|
.rows
|
|
|
|
.iter()
|
|
|
|
.map(|row| row.iter().map(|pat| format!("{pat:?}")).collect())
|
|
|
|
.collect();
|
|
|
|
pretty_printed_matrix
|
2024-01-31 01:37:44 +00:00
|
|
|
.push(self.place_info.iter().map(|place| format!("{}", place.validity)).collect());
|
2016-09-24 15:24:34 +00:00
|
|
|
|
2023-11-18 02:52:54 +00:00
|
|
|
let column_count = self.column_count();
|
|
|
|
assert!(self.rows.iter().all(|row| row.len() == column_count));
|
2024-01-31 01:37:44 +00:00
|
|
|
assert!(self.place_info.len() == column_count);
|
2019-09-21 11:49:14 +00:00
|
|
|
let column_widths: Vec<usize> = (0..column_count)
|
|
|
|
.map(|col| pretty_printed_matrix.iter().map(|row| row[col].len()).max().unwrap_or(0))
|
|
|
|
.collect();
|
2016-09-24 15:24:34 +00:00
|
|
|
|
2023-11-18 02:52:54 +00:00
|
|
|
for (row_i, row) in pretty_printed_matrix.into_iter().enumerate() {
|
|
|
|
let is_validity_row = row_i == self.rows.len();
|
|
|
|
let sep = if is_validity_row { "|" } else { "+" };
|
|
|
|
write!(f, "{sep}")?;
|
2016-09-24 15:24:34 +00:00
|
|
|
for (column, pat_str) in row.into_iter().enumerate() {
|
|
|
|
write!(f, " ")?;
|
|
|
|
write!(f, "{:1$}", pat_str, column_widths[column])?;
|
2023-11-18 02:52:54 +00:00
|
|
|
write!(f, " {sep}")?;
|
|
|
|
}
|
|
|
|
if is_validity_row {
|
2024-03-29 18:40:54 +00:00
|
|
|
write!(f, " // validity")?;
|
2016-09-24 15:24:34 +00:00
|
|
|
}
|
|
|
|
write!(f, "\n")?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-23 06:19:10 +00:00
|
|
|
/// A witness-tuple of non-exhaustiveness for error reporting, represented as a list of patterns (in
|
|
|
|
/// reverse order of construction).
|
2023-10-14 14:30:23 +00:00
|
|
|
///
|
|
|
|
/// This mirrors `PatStack`: they function similarly, except `PatStack` contains user patterns we
|
|
|
|
/// are inspecting, and `WitnessStack` contains witnesses we are constructing.
|
2023-10-23 06:19:10 +00:00
|
|
|
/// FIXME(Nadrieril): use the same order of patterns for both.
|
2018-08-12 10:43:42 +00:00
|
|
|
///
|
2023-10-23 06:19:10 +00:00
|
|
|
/// A `WitnessStack` should have the same types and length as the `PatStack`s we are inspecting
|
|
|
|
/// (except we store the patterns in reverse order). The same way `PatStack` starts with length 1,
|
|
|
|
/// at the end of the algorithm this will have length 1. In the middle of the algorithm, it can
|
|
|
|
/// contain multiple patterns.
|
2018-08-12 10:43:42 +00:00
|
|
|
///
|
|
|
|
/// For example, if we are constructing a witness for the match against
|
2020-10-19 04:54:10 +00:00
|
|
|
///
|
2022-04-15 22:04:34 +00:00
|
|
|
/// ```compile_fail,E0004
|
2018-08-12 10:43:42 +00:00
|
|
|
/// struct Pair(Option<(u32, u32)>, bool);
|
2022-04-15 22:04:34 +00:00
|
|
|
/// # fn foo(p: Pair) {
|
2023-03-15 23:00:55 +00:00
|
|
|
/// match p {
|
2018-08-12 10:43:42 +00:00
|
|
|
/// Pair(None, _) => {}
|
|
|
|
/// Pair(_, false) => {}
|
|
|
|
/// }
|
2022-04-15 22:04:34 +00:00
|
|
|
/// # }
|
2018-08-12 10:43:42 +00:00
|
|
|
/// ```
|
|
|
|
///
|
2023-10-14 14:30:23 +00:00
|
|
|
/// We'll perform the following steps (among others):
|
2023-10-23 06:19:10 +00:00
|
|
|
/// ```text
|
2023-10-14 14:30:23 +00:00
|
|
|
/// - Start with a matrix representing the match
|
|
|
|
/// `PatStack(vec![Pair(None, _)])`
|
|
|
|
/// `PatStack(vec![Pair(_, false)])`
|
|
|
|
/// - Specialize with `Pair`
|
|
|
|
/// `PatStack(vec![None, _])`
|
|
|
|
/// `PatStack(vec![_, false])`
|
|
|
|
/// - Specialize with `Some`
|
|
|
|
/// `PatStack(vec![_, false])`
|
|
|
|
/// - Specialize with `_`
|
|
|
|
/// `PatStack(vec![false])`
|
|
|
|
/// - Specialize with `true`
|
|
|
|
/// // no patstacks left
|
|
|
|
/// - This is a non-exhaustive match: we have the empty witness stack as a witness.
|
|
|
|
/// `WitnessStack(vec![])`
|
|
|
|
/// - Apply `true`
|
|
|
|
/// `WitnessStack(vec![true])`
|
|
|
|
/// - Apply `_`
|
|
|
|
/// `WitnessStack(vec![true, _])`
|
|
|
|
/// - Apply `Some`
|
|
|
|
/// `WitnessStack(vec![true, Some(_)])`
|
|
|
|
/// - Apply `Pair`
|
|
|
|
/// `WitnessStack(vec![Pair(Some(_), true)])`
|
2023-10-23 06:19:10 +00:00
|
|
|
/// ```
|
2018-08-12 10:43:42 +00:00
|
|
|
///
|
|
|
|
/// The final `Pair(Some(_), true)` is then the resulting witness.
|
2023-10-23 06:19:10 +00:00
|
|
|
///
|
|
|
|
/// See the top of the file for more detailed explanations and examples.
|
2024-01-31 00:29:21 +00:00
|
|
|
#[derive(Debug)]
|
2024-03-13 12:56:06 +00:00
|
|
|
struct WitnessStack<Cx: PatCx>(Vec<WitnessPat<Cx>>);
|
2016-09-24 15:24:34 +00:00
|
|
|
|
2024-03-13 12:56:06 +00:00
|
|
|
impl<Cx: PatCx> Clone for WitnessStack<Cx> {
|
2024-01-27 12:18:33 +00:00
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self(self.0.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-13 12:56:06 +00:00
|
|
|
impl<Cx: PatCx> WitnessStack<Cx> {
|
2020-11-12 18:16:46 +00:00
|
|
|
/// Asserts that the witness contains a single pattern, and returns it.
|
2023-12-11 19:01:02 +00:00
|
|
|
fn single_pattern(self) -> WitnessPat<Cx> {
|
2016-09-24 17:45:59 +00:00
|
|
|
assert_eq!(self.0.len(), 1);
|
2019-09-09 14:44:06 +00:00
|
|
|
self.0.into_iter().next().unwrap()
|
2016-09-24 17:45:59 +00:00
|
|
|
}
|
2016-09-24 15:24:34 +00:00
|
|
|
|
2023-11-22 02:25:10 +00:00
|
|
|
/// Reverses specialization by the `Missing` constructor by pushing a whole new pattern.
|
2023-12-11 19:01:02 +00:00
|
|
|
fn push_pattern(&mut self, pat: WitnessPat<Cx>) {
|
2023-11-22 02:25:10 +00:00
|
|
|
self.0.push(pat);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Reverses specialization. Given a witness obtained after specialization, this constructs a
|
2023-10-23 06:19:10 +00:00
|
|
|
/// new witness valid for before specialization. See the section on `unspecialize` at the top of
|
|
|
|
/// the file.
|
2016-09-24 17:45:59 +00:00
|
|
|
///
|
2023-10-23 06:19:10 +00:00
|
|
|
/// Examples:
|
|
|
|
/// ```text
|
2023-11-22 02:25:10 +00:00
|
|
|
/// ctor: tuple of 2 elements
|
|
|
|
/// pats: [false, "foo", _, true]
|
|
|
|
/// result: [(false, "foo"), _, true]
|
2016-09-24 17:45:59 +00:00
|
|
|
///
|
2023-11-22 02:25:10 +00:00
|
|
|
/// ctor: Enum::Variant { a: (bool, &'static str), b: usize}
|
|
|
|
/// pats: [(false, "foo"), _, true]
|
|
|
|
/// result: [Enum::Variant { a: (false, "foo"), b: _ }, true]
|
2023-10-23 06:19:10 +00:00
|
|
|
/// ```
|
2024-03-31 21:57:53 +00:00
|
|
|
fn apply_constructor(
|
|
|
|
mut self,
|
|
|
|
pcx: &PlaceCtxt<'_, Cx>,
|
|
|
|
ctor: &Constructor<Cx>,
|
|
|
|
) -> SmallVec<[Self; 1]> {
|
2023-11-22 02:25:10 +00:00
|
|
|
let len = self.0.len();
|
2024-01-24 20:16:57 +00:00
|
|
|
let arity = pcx.ctor_arity(ctor);
|
2024-03-31 21:57:53 +00:00
|
|
|
let fields: Vec<_> = self.0.drain((len - arity)..).rev().collect();
|
|
|
|
if matches!(ctor, Constructor::UnionField)
|
|
|
|
&& fields.iter().filter(|p| !matches!(p.ctor(), Constructor::Wildcard)).count() >= 2
|
|
|
|
{
|
|
|
|
// Convert a `Union { a: p, b: q }` witness into `Union { a: p }` and `Union { b: q }`.
|
|
|
|
// First add `Union { .. }` to `self`.
|
|
|
|
self.0.push(WitnessPat::wild_from_ctor(pcx.cx, ctor.clone(), pcx.ty.clone()));
|
|
|
|
fields
|
|
|
|
.into_iter()
|
|
|
|
.enumerate()
|
|
|
|
.filter(|(_, p)| !matches!(p.ctor(), Constructor::Wildcard))
|
|
|
|
.map(|(i, p)| {
|
|
|
|
let mut ret = self.clone();
|
|
|
|
// Fill the `i`th field of the union with `p`.
|
|
|
|
ret.0.last_mut().unwrap().fields[i] = p;
|
|
|
|
ret
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
} else {
|
|
|
|
self.0.push(WitnessPat::new(ctor.clone(), fields, pcx.ty.clone()));
|
|
|
|
smallvec![self]
|
|
|
|
}
|
2023-11-22 02:25:10 +00:00
|
|
|
}
|
|
|
|
}
|
2016-09-24 17:45:59 +00:00
|
|
|
|
2023-10-23 06:19:10 +00:00
|
|
|
/// Represents a set of pattern-tuples that are witnesses of non-exhaustiveness for error
|
|
|
|
/// reporting. This has similar invariants as `Matrix` does.
|
|
|
|
///
|
2023-11-18 03:17:50 +00:00
|
|
|
/// The `WitnessMatrix` returned by [`compute_exhaustiveness_and_usefulness`] obeys the invariant
|
2023-10-23 06:19:10 +00:00
|
|
|
/// that the union of the input `Matrix` and the output `WitnessMatrix` together matches the type
|
|
|
|
/// exhaustively.
|
|
|
|
///
|
|
|
|
/// Just as the `Matrix` starts with a single column, by the end of the algorithm, this has a single
|
|
|
|
/// column, which contains the patterns that are missing for the match to be exhaustive.
|
2024-01-31 00:29:21 +00:00
|
|
|
#[derive(Debug)]
|
2024-03-13 12:56:06 +00:00
|
|
|
struct WitnessMatrix<Cx: PatCx>(Vec<WitnessStack<Cx>>);
|
2023-11-22 02:25:10 +00:00
|
|
|
|
2024-03-13 12:56:06 +00:00
|
|
|
impl<Cx: PatCx> Clone for WitnessMatrix<Cx> {
|
2024-01-27 12:18:33 +00:00
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self(self.0.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-13 12:56:06 +00:00
|
|
|
impl<Cx: PatCx> WitnessMatrix<Cx> {
|
2023-10-23 06:19:10 +00:00
|
|
|
/// New matrix with no witnesses.
|
2023-11-22 02:25:10 +00:00
|
|
|
fn empty() -> Self {
|
2024-01-27 12:18:33 +00:00
|
|
|
WitnessMatrix(Vec::new())
|
2023-11-22 02:25:10 +00:00
|
|
|
}
|
2023-10-23 06:19:10 +00:00
|
|
|
/// New matrix with one `()` witness, i.e. with no columns.
|
2023-11-22 02:25:10 +00:00
|
|
|
fn unit_witness() -> Self {
|
2024-01-27 12:18:33 +00:00
|
|
|
WitnessMatrix(vec![WitnessStack(Vec::new())])
|
2023-11-22 02:25:10 +00:00
|
|
|
}
|
|
|
|
|
2023-10-23 06:19:10 +00:00
|
|
|
/// Whether this has any witnesses.
|
2023-11-22 02:25:10 +00:00
|
|
|
fn is_empty(&self) -> bool {
|
|
|
|
self.0.is_empty()
|
|
|
|
}
|
|
|
|
/// Asserts that there is a single column and returns the patterns in it.
|
2023-12-11 19:01:02 +00:00
|
|
|
fn single_column(self) -> Vec<WitnessPat<Cx>> {
|
2023-11-22 02:25:10 +00:00
|
|
|
self.0.into_iter().map(|w| w.single_pattern()).collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Reverses specialization by the `Missing` constructor by pushing a whole new pattern.
|
2023-12-11 19:01:02 +00:00
|
|
|
fn push_pattern(&mut self, pat: WitnessPat<Cx>) {
|
2023-11-22 02:25:10 +00:00
|
|
|
for witness in self.0.iter_mut() {
|
|
|
|
witness.push_pattern(pat.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-23 06:19:10 +00:00
|
|
|
/// Reverses specialization by `ctor`. See the section on `unspecialize` at the top of the file.
|
2023-11-22 02:25:10 +00:00
|
|
|
fn apply_constructor(
|
|
|
|
&mut self,
|
2024-01-03 00:34:38 +00:00
|
|
|
pcx: &PlaceCtxt<'_, Cx>,
|
2023-12-11 19:01:02 +00:00
|
|
|
missing_ctors: &[Constructor<Cx>],
|
|
|
|
ctor: &Constructor<Cx>,
|
2023-11-22 02:25:10 +00:00
|
|
|
) {
|
2024-07-20 20:18:35 +00:00
|
|
|
// The `Or` constructor indicates that we expanded or-patterns. This doesn't affect
|
|
|
|
// witnesses.
|
|
|
|
if self.is_empty() || matches!(ctor, Constructor::Or) {
|
2023-11-22 02:25:10 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-11-05 14:00:46 +00:00
|
|
|
if matches!(ctor, Constructor::Missing) {
|
|
|
|
// We got the special `Missing` constructor that stands for the constructors not present
|
2024-02-06 00:21:29 +00:00
|
|
|
// in the match. For each missing constructor `c`, we add a `c(_, _, _)` witness
|
|
|
|
// appropriately filled with wildcards.
|
2024-02-06 00:14:12 +00:00
|
|
|
let mut ret = Self::empty();
|
|
|
|
for ctor in missing_ctors {
|
|
|
|
let pat = pcx.wild_from_ctor(ctor.clone());
|
|
|
|
// Clone `self` and add `c(_, _, _)` to each of its witnesses.
|
|
|
|
let mut wit_matrix = self.clone();
|
|
|
|
wit_matrix.push_pattern(pat);
|
|
|
|
ret.extend(wit_matrix);
|
2023-11-22 02:25:10 +00:00
|
|
|
}
|
2024-02-06 00:14:12 +00:00
|
|
|
*self = ret;
|
2023-11-22 02:25:10 +00:00
|
|
|
} else {
|
2023-11-05 14:00:46 +00:00
|
|
|
// Any other constructor we unspecialize as expected.
|
2024-03-31 21:57:53 +00:00
|
|
|
for witness in std::mem::take(&mut self.0) {
|
|
|
|
self.0.extend(witness.apply_constructor(pcx, ctor));
|
2023-11-22 02:25:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-23 06:19:10 +00:00
|
|
|
/// Merges the witnesses of two matrices. Their column types must match.
|
2023-11-22 02:25:10 +00:00
|
|
|
fn extend(&mut self, other: Self) {
|
|
|
|
self.0.extend(other.0)
|
2016-09-24 17:45:59 +00:00
|
|
|
}
|
2016-09-24 15:24:34 +00:00
|
|
|
}
|
|
|
|
|
2023-12-29 18:21:43 +00:00
|
|
|
/// Collect ranges that overlap like `lo..=overlap`/`overlap..=hi`. Must be called during
|
|
|
|
/// exhaustiveness checking, if we find a singleton range after constructor splitting. This reuses
|
|
|
|
/// row intersection information to only detect ranges that truly overlap.
|
|
|
|
///
|
|
|
|
/// If two ranges overlapped, the split set will contain their intersection as a singleton.
|
|
|
|
/// Specialization will then select rows that match the overlap, and exhaustiveness will compute
|
|
|
|
/// which rows have an intersection that includes the overlap. That gives us all the info we need to
|
|
|
|
/// compute overlapping ranges without false positives.
|
|
|
|
///
|
|
|
|
/// We can however get false negatives because exhaustiveness does not explore all cases. See the
|
|
|
|
/// section on relevancy at the top of the file.
|
2024-03-13 12:56:06 +00:00
|
|
|
fn collect_overlapping_range_endpoints<'p, Cx: PatCx>(
|
2024-01-14 21:24:10 +00:00
|
|
|
cx: &Cx,
|
2023-12-29 18:21:43 +00:00
|
|
|
overlap_range: IntRange,
|
|
|
|
matrix: &Matrix<'p, Cx>,
|
|
|
|
specialized_matrix: &Matrix<'p, Cx>,
|
|
|
|
) {
|
|
|
|
let overlap = overlap_range.lo;
|
|
|
|
// Ranges that look like `lo..=overlap`.
|
|
|
|
let mut prefixes: SmallVec<[_; 1]> = Default::default();
|
|
|
|
// Ranges that look like `overlap..=hi`.
|
|
|
|
let mut suffixes: SmallVec<[_; 1]> = Default::default();
|
|
|
|
// Iterate on patterns that contained `overlap`. We iterate on `specialized_matrix` which
|
|
|
|
// contains only rows that matched the current `ctor` as well as accurate intersection
|
|
|
|
// information. It doesn't contain the column that contains the range; that can be found in
|
|
|
|
// `matrix`.
|
|
|
|
for (child_row_id, child_row) in specialized_matrix.rows().enumerate() {
|
|
|
|
let PatOrWild::Pat(pat) = matrix.rows[child_row.parent_row].head() else { continue };
|
|
|
|
let Constructor::IntRange(this_range) = pat.ctor() else { continue };
|
|
|
|
// Don't lint when one of the ranges is a singleton.
|
|
|
|
if this_range.is_singleton() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if this_range.lo == overlap {
|
|
|
|
// `this_range` looks like `overlap..=this_range.hi`; it overlaps with any
|
|
|
|
// ranges that look like `lo..=overlap`.
|
|
|
|
if !prefixes.is_empty() {
|
|
|
|
let overlaps_with: Vec<_> = prefixes
|
|
|
|
.iter()
|
|
|
|
.filter(|&&(other_child_row_id, _)| {
|
2024-07-21 12:46:05 +00:00
|
|
|
child_row.intersects_at_least.contains(other_child_row_id)
|
2023-12-29 18:21:43 +00:00
|
|
|
})
|
|
|
|
.map(|&(_, pat)| pat)
|
|
|
|
.collect();
|
|
|
|
if !overlaps_with.is_empty() {
|
2024-01-14 21:24:10 +00:00
|
|
|
cx.lint_overlapping_range_endpoints(pat, overlap_range, &overlaps_with);
|
2023-12-29 18:21:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
suffixes.push((child_row_id, pat))
|
2024-01-14 21:26:47 +00:00
|
|
|
} else if Some(this_range.hi) == overlap.plus_one() {
|
2023-12-29 18:21:43 +00:00
|
|
|
// `this_range` looks like `this_range.lo..=overlap`; it overlaps with any
|
|
|
|
// ranges that look like `overlap..=hi`.
|
|
|
|
if !suffixes.is_empty() {
|
|
|
|
let overlaps_with: Vec<_> = suffixes
|
|
|
|
.iter()
|
|
|
|
.filter(|&&(other_child_row_id, _)| {
|
2024-07-21 12:46:05 +00:00
|
|
|
child_row.intersects_at_least.contains(other_child_row_id)
|
2023-12-29 18:21:43 +00:00
|
|
|
})
|
|
|
|
.map(|&(_, pat)| pat)
|
|
|
|
.collect();
|
|
|
|
if !overlaps_with.is_empty() {
|
2024-01-14 21:24:10 +00:00
|
|
|
cx.lint_overlapping_range_endpoints(pat, overlap_range, &overlaps_with);
|
2023-12-29 18:21:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
prefixes.push((child_row_id, pat))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-14 21:24:10 +00:00
|
|
|
/// Collect ranges that have a singleton gap between them.
|
2024-03-13 12:56:06 +00:00
|
|
|
fn collect_non_contiguous_range_endpoints<'p, Cx: PatCx>(
|
2024-01-14 21:24:10 +00:00
|
|
|
cx: &Cx,
|
|
|
|
gap_range: &IntRange,
|
|
|
|
matrix: &Matrix<'p, Cx>,
|
|
|
|
) {
|
|
|
|
let gap = gap_range.lo;
|
|
|
|
// Ranges that look like `lo..gap`.
|
|
|
|
let mut onebefore: SmallVec<[_; 1]> = Default::default();
|
|
|
|
// Ranges that start on `gap+1` or singletons `gap+1`.
|
|
|
|
let mut oneafter: SmallVec<[_; 1]> = Default::default();
|
|
|
|
// Look through the column for ranges near the gap.
|
|
|
|
for pat in matrix.heads() {
|
|
|
|
let PatOrWild::Pat(pat) = pat else { continue };
|
|
|
|
let Constructor::IntRange(this_range) = pat.ctor() else { continue };
|
|
|
|
if gap == this_range.hi {
|
|
|
|
onebefore.push(pat)
|
|
|
|
} else if gap.plus_one() == Some(this_range.lo) {
|
|
|
|
oneafter.push(pat)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for pat_before in onebefore {
|
|
|
|
cx.lint_non_contiguous_range_endpoints(pat_before, *gap_range, oneafter.as_slice());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-23 06:19:10 +00:00
|
|
|
/// The core of the algorithm.
|
2016-09-25 23:53:26 +00:00
|
|
|
///
|
2023-10-23 06:19:10 +00:00
|
|
|
/// This recursively computes witnesses of the non-exhaustiveness of `matrix` (if any). Also tracks
|
2024-07-21 12:46:05 +00:00
|
|
|
/// usefulness of each row in the matrix (in `row.useful`). We track usefulness of subpatterns in
|
|
|
|
/// `mcx.branch_usefulness`.
|
2017-12-25 16:14:50 +00:00
|
|
|
///
|
2023-10-23 06:19:10 +00:00
|
|
|
/// The input `Matrix` and the output `WitnessMatrix` together match the type exhaustively.
|
2016-09-25 23:53:26 +00:00
|
|
|
///
|
2023-10-23 06:19:10 +00:00
|
|
|
/// The key steps are:
|
|
|
|
/// - specialization, where we dig into the rows that have a specific constructor and call ourselves
|
|
|
|
/// recursively;
|
|
|
|
/// - unspecialization, where we lift the results from the previous step into results for this step
|
2023-11-18 03:17:50 +00:00
|
|
|
/// (using `apply_constructor` and by updating `row.useful` for each parent row).
|
2023-10-23 06:19:10 +00:00
|
|
|
/// This is all explained at the top of the file.
|
2024-01-31 01:46:10 +00:00
|
|
|
#[instrument(level = "debug", skip(mcx), ret)]
|
2024-03-13 12:56:06 +00:00
|
|
|
fn compute_exhaustiveness_and_usefulness<'a, 'p, Cx: PatCx>(
|
2024-07-21 12:46:05 +00:00
|
|
|
mcx: &mut UsefulnessCtxt<'a, 'p, Cx>,
|
2023-12-26 01:59:18 +00:00
|
|
|
matrix: &mut Matrix<'p, Cx>,
|
2024-01-07 20:20:16 +00:00
|
|
|
) -> Result<WitnessMatrix<Cx>, Cx::Error> {
|
2023-11-05 13:52:26 +00:00
|
|
|
debug_assert!(matrix.rows().all(|r| r.len() == matrix.column_count()));
|
2023-10-23 06:19:10 +00:00
|
|
|
|
2024-01-06 16:52:47 +00:00
|
|
|
if !matrix.wildcard_row_is_relevant && matrix.rows().all(|r| !r.pats.relevant) {
|
2023-11-29 20:43:06 +00:00
|
|
|
// Here we know that nothing will contribute further to exhaustiveness or usefulness. This
|
2023-12-23 21:15:15 +00:00
|
|
|
// is purely an optimization: skipping this check doesn't affect correctness. See the top of
|
|
|
|
// the file for details.
|
2024-01-07 20:20:16 +00:00
|
|
|
return Ok(WitnessMatrix::empty());
|
2023-11-29 20:43:06 +00:00
|
|
|
}
|
|
|
|
|
2024-01-31 01:37:44 +00:00
|
|
|
let Some(place) = matrix.head_place() else {
|
2024-03-02 21:48:41 +00:00
|
|
|
mcx.increase_complexity_level(matrix.rows().len())?;
|
2023-11-05 13:52:26 +00:00
|
|
|
// The base case: there are no columns in the matrix. We are morally pattern-matching on ().
|
2023-11-18 03:17:50 +00:00
|
|
|
// A row is useful iff it has no (unguarded) rows above it.
|
2023-12-28 20:59:16 +00:00
|
|
|
let mut useful = true; // Whether the next row is useful.
|
|
|
|
for (i, row) in matrix.rows_mut().enumerate() {
|
|
|
|
row.useful = useful;
|
2024-07-21 12:46:05 +00:00
|
|
|
row.intersects_at_least.insert_range(0..i);
|
2023-12-28 20:59:16 +00:00
|
|
|
// The next rows stays useful if this one is under a guard.
|
|
|
|
useful &= row.is_under_guard;
|
2021-08-26 07:55:57 +00:00
|
|
|
}
|
2023-12-28 20:59:16 +00:00
|
|
|
return if useful && matrix.wildcard_row_is_relevant {
|
|
|
|
// The wildcard row is useful; the match is non-exhaustive.
|
2024-01-07 20:20:16 +00:00
|
|
|
Ok(WitnessMatrix::unit_witness())
|
2023-11-29 20:43:06 +00:00
|
|
|
} else {
|
2023-12-28 20:59:16 +00:00
|
|
|
// Either the match is exhaustive, or we choose not to report anything because of
|
|
|
|
// relevancy. See at the top for details.
|
2024-01-07 20:20:16 +00:00
|
|
|
Ok(WitnessMatrix::empty())
|
2023-11-29 20:43:06 +00:00
|
|
|
};
|
2023-11-05 13:52:26 +00:00
|
|
|
};
|
2021-09-10 20:45:04 +00:00
|
|
|
|
2023-10-23 06:19:10 +00:00
|
|
|
// Analyze the constructors present in this column.
|
|
|
|
let ctors = matrix.heads().map(|p| p.ctor());
|
2024-02-06 02:52:46 +00:00
|
|
|
let (split_ctors, missing_ctors) = place.split_column_ctors(mcx.tycx, ctors)?;
|
2023-10-23 06:19:10 +00:00
|
|
|
|
2024-02-06 02:52:46 +00:00
|
|
|
let ty = &place.ty.clone(); // Clone it out so we can mutate `matrix` later.
|
|
|
|
let pcx = &PlaceCtxt { cx: mcx.tycx, ty };
|
2023-10-23 06:19:10 +00:00
|
|
|
let mut ret = WitnessMatrix::empty();
|
|
|
|
for ctor in split_ctors {
|
|
|
|
// Dig into rows that match `ctor`.
|
2023-11-29 20:43:06 +00:00
|
|
|
debug!("specialize({:?})", ctor);
|
|
|
|
// `ctor` is *irrelevant* if there's another constructor in `split_ctors` that matches
|
|
|
|
// strictly fewer rows. In that case we can sometimes skip it. See the top of the file for
|
|
|
|
// details.
|
|
|
|
let ctor_is_relevant = matches!(ctor, Constructor::Missing) || missing_ctors.is_empty();
|
2024-01-24 15:24:52 +00:00
|
|
|
let mut spec_matrix = matrix.specialize_constructor(pcx, &ctor, ctor_is_relevant)?;
|
2023-10-23 06:19:10 +00:00
|
|
|
let mut witnesses = ensure_sufficient_stack(|| {
|
2024-01-31 01:46:10 +00:00
|
|
|
compute_exhaustiveness_and_usefulness(mcx, &mut spec_matrix)
|
2024-01-07 20:20:16 +00:00
|
|
|
})?;
|
2023-11-05 14:00:46 +00:00
|
|
|
|
2023-11-29 20:43:06 +00:00
|
|
|
// Transform witnesses for `spec_matrix` into witnesses for `matrix`.
|
2024-02-06 00:21:29 +00:00
|
|
|
witnesses.apply_constructor(pcx, &missing_ctors, &ctor);
|
2023-11-29 20:43:06 +00:00
|
|
|
// Accumulate the found witnesses.
|
|
|
|
ret.extend(witnesses);
|
2023-10-23 06:19:10 +00:00
|
|
|
|
2023-12-29 18:21:43 +00:00
|
|
|
// Detect ranges that overlap on their endpoints.
|
|
|
|
if let Constructor::IntRange(overlap_range) = ctor {
|
|
|
|
if overlap_range.is_singleton()
|
|
|
|
&& spec_matrix.rows.len() >= 2
|
2024-07-21 12:46:05 +00:00
|
|
|
&& spec_matrix.rows.iter().any(|row| !row.intersects_at_least.is_empty())
|
2023-12-29 18:21:43 +00:00
|
|
|
{
|
2024-01-14 21:24:10 +00:00
|
|
|
collect_overlapping_range_endpoints(mcx.tycx, overlap_range, matrix, &spec_matrix);
|
2023-12-29 18:21:43 +00:00
|
|
|
}
|
|
|
|
}
|
2024-01-31 02:24:24 +00:00
|
|
|
|
|
|
|
matrix.unspecialize(spec_matrix);
|
2021-09-25 20:48:50 +00:00
|
|
|
}
|
|
|
|
|
2024-01-14 21:24:10 +00:00
|
|
|
// Detect singleton gaps between ranges.
|
|
|
|
if missing_ctors.iter().any(|c| matches!(c, Constructor::IntRange(..))) {
|
|
|
|
for missing in &missing_ctors {
|
|
|
|
if let Constructor::IntRange(gap) = missing {
|
|
|
|
if gap.is_singleton() {
|
|
|
|
collect_non_contiguous_range_endpoints(mcx.tycx, gap, matrix);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-21 12:46:05 +00:00
|
|
|
// Record usefulness of the branch patterns.
|
2023-10-23 06:19:10 +00:00
|
|
|
for row in matrix.rows() {
|
2024-07-21 12:46:05 +00:00
|
|
|
if row.head_is_branch {
|
2024-02-07 03:26:54 +00:00
|
|
|
if let PatOrWild::Pat(pat) = row.head() {
|
2024-07-21 12:46:05 +00:00
|
|
|
mcx.branch_usefulness.entry(pat.uid).or_default().update(row, matrix);
|
2024-02-07 03:26:54 +00:00
|
|
|
}
|
2023-10-23 06:19:10 +00:00
|
|
|
}
|
|
|
|
}
|
2023-11-05 14:00:46 +00:00
|
|
|
|
2024-01-07 20:20:16 +00:00
|
|
|
Ok(ret)
|
2016-09-24 15:24:34 +00:00
|
|
|
}
|
|
|
|
|
2024-07-21 12:46:05 +00:00
|
|
|
/// Indicates why a given pattern is considered redundant.
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct RedundancyExplanation<'p, Cx: PatCx> {
|
|
|
|
/// All the values matched by this pattern are already matched by the given set of patterns.
|
|
|
|
/// This list is not guaranteed to be minimal but the contained patterns are at least guaranteed
|
|
|
|
/// to intersect this pattern.
|
|
|
|
pub covered_by: Vec<&'p DeconstructedPat<Cx>>,
|
|
|
|
}
|
|
|
|
|
2023-11-18 03:17:50 +00:00
|
|
|
/// Indicates whether or not a given arm is useful.
|
2020-12-22 11:21:34 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2024-03-13 12:56:06 +00:00
|
|
|
pub enum Usefulness<'p, Cx: PatCx> {
|
2023-11-18 03:17:50 +00:00
|
|
|
/// The arm is useful. This additionally carries a set of or-pattern branches that have been
|
|
|
|
/// found to be redundant despite the overall arm being useful. Used only in the presence of
|
|
|
|
/// or-patterns, otherwise it stays empty.
|
2024-07-21 12:46:05 +00:00
|
|
|
Useful(Vec<(&'p DeconstructedPat<Cx>, RedundancyExplanation<'p, Cx>)>),
|
2023-11-18 03:17:50 +00:00
|
|
|
/// The arm is redundant and can be removed without changing the behavior of the match
|
|
|
|
/// expression.
|
2024-07-21 12:46:05 +00:00
|
|
|
Redundant(RedundancyExplanation<'p, Cx>),
|
2020-12-22 11:21:34 +00:00
|
|
|
}
|
|
|
|
|
2023-11-18 03:17:50 +00:00
|
|
|
/// The output of checking a match for exhaustiveness and arm usefulness.
|
2024-03-13 12:56:06 +00:00
|
|
|
pub struct UsefulnessReport<'p, Cx: PatCx> {
|
2023-11-18 03:17:50 +00:00
|
|
|
/// For each arm of the input, whether that arm is useful after the arms above it.
|
2023-12-15 15:18:21 +00:00
|
|
|
pub arm_usefulness: Vec<(MatchArm<'p, Cx>, Usefulness<'p, Cx>)>,
|
2020-11-12 18:16:46 +00:00
|
|
|
/// If the match is exhaustive, this is empty. If not, this contains witnesses for the lack of
|
|
|
|
/// exhaustiveness.
|
2023-12-11 19:01:02 +00:00
|
|
|
pub non_exhaustiveness_witnesses: Vec<WitnessPat<Cx>>,
|
2024-03-17 14:48:27 +00:00
|
|
|
/// For each arm, a set of indices of arms above it that have non-empty intersection, i.e. there
|
|
|
|
/// is a value matched by both arms. This may miss real intersections.
|
2025-01-07 15:19:05 +00:00
|
|
|
pub arm_intersections: Vec<DenseBitSet<usize>>,
|
2020-11-12 18:16:46 +00:00
|
|
|
}
|
|
|
|
|
2023-12-11 09:40:31 +00:00
|
|
|
/// Computes whether a match is exhaustive and which of its arms are useful.
|
2024-01-24 19:57:11 +00:00
|
|
|
#[instrument(skip(tycx, arms), level = "debug")]
|
2024-03-13 12:56:06 +00:00
|
|
|
pub fn compute_match_usefulness<'p, Cx: PatCx>(
|
2024-01-24 19:57:11 +00:00
|
|
|
tycx: &Cx,
|
2023-12-11 19:01:02 +00:00
|
|
|
arms: &[MatchArm<'p, Cx>],
|
|
|
|
scrut_ty: Cx::Ty,
|
2024-03-13 12:53:18 +00:00
|
|
|
scrut_validity: PlaceValidity,
|
2024-03-02 21:48:41 +00:00
|
|
|
complexity_limit: Option<usize>,
|
2024-01-07 20:20:16 +00:00
|
|
|
) -> Result<UsefulnessReport<'p, Cx>, Cx::Error> {
|
2024-03-02 21:48:41 +00:00
|
|
|
let mut cx = UsefulnessCtxt {
|
|
|
|
tycx,
|
2024-07-21 12:46:05 +00:00
|
|
|
branch_usefulness: FxHashMap::default(),
|
2024-03-02 21:48:41 +00:00
|
|
|
complexity_limit,
|
|
|
|
complexity_level: 0,
|
|
|
|
};
|
2024-01-06 16:52:47 +00:00
|
|
|
let mut matrix = Matrix::new(arms, scrut_ty, scrut_validity);
|
2024-02-07 03:26:54 +00:00
|
|
|
let non_exhaustiveness_witnesses = compute_exhaustiveness_and_usefulness(&mut cx, &mut matrix)?;
|
2023-10-23 06:19:10 +00:00
|
|
|
|
|
|
|
let non_exhaustiveness_witnesses: Vec<_> = non_exhaustiveness_witnesses.single_column();
|
2020-11-12 18:16:46 +00:00
|
|
|
let arm_usefulness: Vec<_> = arms
|
|
|
|
.iter()
|
|
|
|
.copied()
|
|
|
|
.map(|arm| {
|
2022-03-09 12:56:12 +00:00
|
|
|
debug!(?arm);
|
2024-07-21 12:46:05 +00:00
|
|
|
let usefulness = cx.branch_usefulness.get(&arm.pat.uid).unwrap();
|
|
|
|
let usefulness = if let Some(explanation) = usefulness.is_redundant() {
|
|
|
|
Usefulness::Redundant(explanation)
|
|
|
|
} else {
|
2024-07-20 20:18:35 +00:00
|
|
|
let mut redundant_subpats = Vec::new();
|
|
|
|
arm.pat.walk(&mut |subpat| {
|
2024-07-21 12:46:05 +00:00
|
|
|
if let Some(u) = cx.branch_usefulness.get(&subpat.uid) {
|
|
|
|
if let Some(explanation) = u.is_redundant() {
|
|
|
|
redundant_subpats.push((subpat, explanation));
|
|
|
|
false // stop recursing
|
|
|
|
} else {
|
|
|
|
true // keep recursing
|
|
|
|
}
|
2024-07-20 20:18:35 +00:00
|
|
|
} else {
|
2024-07-21 12:46:05 +00:00
|
|
|
true // keep recursing
|
2024-07-20 20:18:35 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
Usefulness::Useful(redundant_subpats)
|
|
|
|
};
|
2024-03-29 18:40:54 +00:00
|
|
|
debug!(?usefulness);
|
2023-11-18 03:17:50 +00:00
|
|
|
(arm, usefulness)
|
2020-11-12 18:16:46 +00:00
|
|
|
})
|
|
|
|
.collect();
|
2023-12-29 18:21:43 +00:00
|
|
|
|
2024-07-21 12:46:05 +00:00
|
|
|
let arm_intersections: Vec<_> =
|
|
|
|
matrix.rows().map(|row| row.intersects_at_least.clone()).collect();
|
2024-03-17 14:48:27 +00:00
|
|
|
|
|
|
|
Ok(UsefulnessReport { arm_usefulness, non_exhaustiveness_witnesses, arm_intersections })
|
2020-11-12 18:16:46 +00:00
|
|
|
}
|