rustc_pattern_analysis/
constructor.rs

1//! As explained in [`crate::usefulness`], values and patterns are made from constructors applied to
2//! fields. This file defines a `Constructor` enum and various operations to manipulate them.
3//!
4//! There are two important bits of core logic in this file: constructor inclusion and constructor
5//! splitting. Constructor inclusion, i.e. whether a constructor is included in/covered by another,
6//! is straightforward and defined in [`Constructor::is_covered_by`].
7//!
8//! Constructor splitting is mentioned in [`crate::usefulness`] but not detailed. We describe it
9//! precisely here.
10//!
11//!
12//!
13//! # Constructor grouping and splitting
14//!
15//! As explained in the corresponding section in [`crate::usefulness`], to make usefulness tractable
16//! we need to group together constructors that have the same effect when they are used to
17//! specialize the matrix.
18//!
19//! Example:
20//! ```compile_fail,E0004
21//! match (0, false) {
22//!     (0 ..=100, true) => {}
23//!     (50..=150, false) => {}
24//!     (0 ..=200, _) => {}
25//! }
26//! ```
27//!
28//! In this example we can restrict specialization to 5 cases: `0..50`, `50..=100`, `101..=150`,
29//! `151..=200` and `200..`.
30//!
31//! In [`crate::usefulness`], we had said that `specialize` only takes value-only constructors. We
32//! now relax this restriction: we allow `specialize` to take constructors like `0..50` as long as
33//! we're careful to only do that with constructors that make sense. For example, `specialize(0..50,
34//! (0..=100, true))` is sensible, but `specialize(50..=200, (0..=100, true))` is not.
35//!
36//! Constructor splitting looks at the constructors in the first column of the matrix and constructs
37//! such a sensible set of constructors. Formally, we want to find a smallest disjoint set of
38//! constructors:
39//! - Whose union covers the whole type, and
40//! - That have no non-trivial intersection with any of the constructors in the column (i.e. they're
41//!     each either disjoint with or covered by any given column constructor).
42//!
43//! We compute this in two steps: first [`PatCx::ctors_for_ty`] determines the
44//! set of all possible constructors for the type. Then [`ConstructorSet::split`] looks at the
45//! column of constructors and splits the set into groups accordingly. The precise invariants of
46//! [`ConstructorSet::split`] is described in [`SplitConstructorSet`].
47//!
48//! Constructor splitting has two interesting special cases: integer range splitting (see
49//! [`IntRange::split`]) and slice splitting (see [`Slice::split`]).
50//!
51//!
52//!
53//! # The `Missing` constructor
54//!
55//! We detail a special case of constructor splitting that is a bit subtle. Take the following:
56//!
57//! ```
58//! enum Direction { North, South, East, West }
59//! # let wind = (Direction::North, 0u8);
60//! match wind {
61//!     (Direction::North, 50..) => {}
62//!     (_, _) => {}
63//! }
64//! ```
65//!
66//! Here we expect constructor splitting to output two cases: `North`, and "everything else". This
67//! "everything else" is represented by [`Constructor::Missing`]. Unlike other constructors, it's a
68//! bit contextual: to know the exact list of constructors it represents we have to look at the
69//! column. In practice however we don't need to, because by construction it only matches rows that
70//! have wildcards. This is how this constructor is special: the only constructor that covers it is
71//! `Wildcard`.
72//!
73//! The only place where we care about which constructors `Missing` represents is in diagnostics
74//! (see `crate::usefulness::WitnessMatrix::apply_constructor`).
75//!
76//! We choose whether to specialize with `Missing` in
77//! `crate::usefulness::compute_exhaustiveness_and_usefulness`.
78//!
79//!
80//!
81//! ## Empty types, empty constructors, and the `exhaustive_patterns` feature
82//!
83//! An empty type is a type that has no valid value, like `!`, `enum Void {}`, or `Result<!, !>`.
84//! They require careful handling.
85//!
86//! First, for soundness reasons related to the possible existence of invalid values, by default we
87//! don't treat empty types as empty. We force them to be matched with wildcards. Except if the
88//! `exhaustive_patterns` feature is turned on, in which case we do treat them as empty. And also
89//! except if the type has no constructors (like `enum Void {}` but not like `Result<!, !>`), we
90//! specifically allow `match void {}` to be exhaustive. There are additionally considerations of
91//! place validity that are handled in `crate::usefulness`. Yes this is a bit tricky.
92//!
93//! The second thing is that regardless of the above, it is always allowed to use all the
94//! constructors of a type. For example, all the following is ok:
95//!
96//! ```rust,ignore(example)
97//! # #![feature(never_type)]
98//! # #![feature(exhaustive_patterns)]
99//! fn foo(x: Option<!>) {
100//!   match x {
101//!     None => {}
102//!     Some(_) => {}
103//!   }
104//! }
105//! fn bar(x: &[!]) -> u32 {
106//!   match x {
107//!     [] => 1,
108//!     [_] => 2,
109//!     [_, _] => 3,
110//!   }
111//! }
112//! ```
113//!
114//! Moreover, take the following:
115//!
116//! ```rust
117//! # #![feature(never_type)]
118//! # #![feature(exhaustive_patterns)]
119//! # let x = None::<!>;
120//! match x {
121//!   None => {}
122//! }
123//! ```
124//!
125//! On a normal type, we would identify `Some` as missing and tell the user. If `x: Option<!>`
126//! however (and `exhaustive_patterns` is on), it's ok to omit `Some`. When listing the constructors
127//! of a type, we must therefore track which can be omitted.
128//!
129//! Let's call "empty" a constructor that matches no valid value for the type, like `Some` for the
130//! type `Option<!>`. What this all means is that `ConstructorSet` must know which constructors are
131//! empty. The difference between empty and nonempty constructors is that empty constructors need
132//! not be present for the match to be exhaustive.
133//!
134//! A final remark: empty constructors of arity 0 break specialization, we must avoid them. The
135//! reason is that if we specialize by them, nothing remains to witness the emptiness; the rest of
136//! the algorithm can't distinguish them from a nonempty constructor. The only known case where this
137//! could happen is the `[..]` pattern on `[!; N]` with `N > 0` so we must take care to not emit it.
138//!
139//! This is all handled by [`PatCx::ctors_for_ty`] and
140//! [`ConstructorSet::split`]. The invariants of [`SplitConstructorSet`] are also of interest.
141//!
142//!
143//! ## Unions
144//!
145//! Unions allow us to match a value via several overlapping representations at the same time. For
146//! example, the following is exhaustive because when seeing the value as a boolean we handled all
147//! possible cases (other cases such as `n == 3` would trigger UB).
148//!
149//! ```rust
150//! # fn main() {
151//! union U8AsBool {
152//!     n: u8,
153//!     b: bool,
154//! }
155//! let x = U8AsBool { n: 1 };
156//! unsafe {
157//!     match x {
158//!         U8AsBool { n: 2 } => {}
159//!         U8AsBool { b: true } => {}
160//!         U8AsBool { b: false } => {}
161//!     }
162//! }
163//! # }
164//! ```
165//!
166//! Pattern-matching has no knowledge that e.g. `false as u8 == 0`, so the values we consider in the
167//! algorithm look like `U8AsBool { b: true, n: 2 }`. In other words, for the most part a union is
168//! treated like a struct with the same fields. The difference lies in how we construct witnesses of
169//! non-exhaustiveness.
170//!
171//!
172//! ## Opaque patterns
173//!
174//! Some patterns, such as constants that are not allowed to be matched structurally, cannot be
175//! inspected, which we handle with `Constructor::Opaque`. Since we know nothing of these patterns,
176//! we assume they never cover each other. In order to respect the invariants of
177//! [`SplitConstructorSet`], we give each `Opaque` constructor a unique id so we can recognize it.
178
179use std::cmp::{self, Ordering, max, min};
180use std::fmt;
181use std::iter::once;
182
183use rustc_apfloat::ieee::{DoubleS, HalfS, IeeeFloat, QuadS, SingleS};
184use rustc_index::IndexVec;
185use rustc_index::bit_set::{DenseBitSet, GrowableBitSet};
186use smallvec::SmallVec;
187
188use self::Constructor::*;
189use self::MaybeInfiniteInt::*;
190use self::SliceKind::*;
191use crate::PatCx;
192
193/// Whether we have seen a constructor in the column or not.
194#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
195enum Presence {
196    Unseen,
197    Seen,
198}
199
200#[derive(Debug, Copy, Clone, PartialEq, Eq)]
201pub enum RangeEnd {
202    Included,
203    Excluded,
204}
205
206impl fmt::Display for RangeEnd {
207    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
208        f.write_str(match self {
209            RangeEnd::Included => "..=",
210            RangeEnd::Excluded => "..",
211        })
212    }
213}
214
215/// A possibly infinite integer. Values are encoded such that the ordering on `u128` matches the
216/// natural order on the original type. For example, `-128i8` is encoded as `0` and `127i8` as
217/// `255`. See `signed_bias` for details.
218#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
219pub enum MaybeInfiniteInt {
220    NegInfinity,
221    /// Encoded value. DO NOT CONSTRUCT BY HAND; use `new_finite_{int,uint}`.
222    #[non_exhaustive]
223    Finite(u128),
224    PosInfinity,
225}
226
227impl MaybeInfiniteInt {
228    pub fn new_finite_uint(bits: u128) -> Self {
229        Finite(bits)
230    }
231    pub fn new_finite_int(bits: u128, size: u64) -> Self {
232        // Perform a shift if the underlying types are signed, which makes the interval arithmetic
233        // type-independent.
234        let bias = 1u128 << (size - 1);
235        Finite(bits ^ bias)
236    }
237
238    pub fn as_finite_uint(self) -> Option<u128> {
239        match self {
240            Finite(bits) => Some(bits),
241            _ => None,
242        }
243    }
244    pub fn as_finite_int(self, size: u64) -> Option<u128> {
245        // We decode the shift.
246        match self {
247            Finite(bits) => {
248                let bias = 1u128 << (size - 1);
249                Some(bits ^ bias)
250            }
251            _ => None,
252        }
253    }
254
255    /// Note: this will not turn a finite value into an infinite one or vice-versa.
256    pub fn minus_one(self) -> Option<Self> {
257        match self {
258            Finite(n) => n.checked_sub(1).map(Finite),
259            x => Some(x),
260        }
261    }
262    /// Note: this will turn `u128::MAX` into `PosInfinity`. This means `plus_one` and `minus_one`
263    /// are not strictly inverses, but that poses no problem in our use of them.
264    /// this will not turn a finite value into an infinite one or vice-versa.
265    pub fn plus_one(self) -> Option<Self> {
266        match self {
267            Finite(n) => match n.checked_add(1) {
268                Some(m) => Some(Finite(m)),
269                None => Some(PosInfinity),
270            },
271            x => Some(x),
272        }
273    }
274}
275
276/// An exclusive interval, used for precise integer exhaustiveness checking. `IntRange`s always
277/// store a contiguous range.
278///
279/// `IntRange` is never used to encode an empty range or a "range" that wraps around the (offset)
280/// space: i.e., `range.lo < range.hi`.
281#[derive(Clone, Copy, PartialEq, Eq)]
282pub struct IntRange {
283    pub lo: MaybeInfiniteInt, // Must not be `PosInfinity`.
284    pub hi: MaybeInfiniteInt, // Must not be `NegInfinity`.
285}
286
287impl IntRange {
288    /// Best effort; will not know that e.g. `255u8..` is a singleton.
289    pub fn is_singleton(&self) -> bool {
290        // Since `lo` and `hi` can't be the same `Infinity` and `plus_one` never changes from finite
291        // to infinite, this correctly only detects ranges that contain exactly one `Finite(x)`.
292        self.lo.plus_one() == Some(self.hi)
293    }
294
295    /// Construct a singleton range.
296    /// `x` must be a `Finite(_)` value.
297    #[inline]
298    pub fn from_singleton(x: MaybeInfiniteInt) -> IntRange {
299        // `unwrap()` is ok on a finite value
300        IntRange { lo: x, hi: x.plus_one().unwrap() }
301    }
302
303    /// Construct a range with these boundaries.
304    /// `lo` must not be `PosInfinity`. `hi` must not be `NegInfinity`.
305    #[inline]
306    pub fn from_range(lo: MaybeInfiniteInt, mut hi: MaybeInfiniteInt, end: RangeEnd) -> IntRange {
307        if end == RangeEnd::Included {
308            hi = hi.plus_one().unwrap();
309        }
310        if lo >= hi {
311            // This should have been caught earlier by E0030.
312            panic!("malformed range pattern: {lo:?}..{hi:?}");
313        }
314        IntRange { lo, hi }
315    }
316
317    fn is_subrange(&self, other: &Self) -> bool {
318        other.lo <= self.lo && self.hi <= other.hi
319    }
320
321    fn intersection(&self, other: &Self) -> Option<Self> {
322        if self.lo < other.hi && other.lo < self.hi {
323            Some(IntRange { lo: max(self.lo, other.lo), hi: min(self.hi, other.hi) })
324        } else {
325            None
326        }
327    }
328
329    /// Partition a range of integers into disjoint subranges. This does constructor splitting for
330    /// integer ranges as explained at the top of the file.
331    ///
332    /// This returns an output that covers `self`. The output is split so that the only
333    /// intersections between an output range and a column range are inclusions. No output range
334    /// straddles the boundary of one of the inputs.
335    ///
336    /// Additionally, we track for each output range whether it is covered by one of the column ranges or not.
337    ///
338    /// The following input:
339    /// ```text
340    ///   (--------------------------) // `self`
341    /// (------) (----------)    (-)
342    ///     (------) (--------)
343    /// ```
344    /// is first intersected with `self`:
345    /// ```text
346    ///   (--------------------------) // `self`
347    ///   (----) (----------)    (-)
348    ///     (------) (--------)
349    /// ```
350    /// and then iterated over as follows:
351    /// ```text
352    ///   (-(--)-(-)-(------)-)--(-)-
353    /// ```
354    /// where each sequence of dashes is an output range, and dashes outside parentheses are marked
355    /// as `Presence::Missing`.
356    ///
357    /// ## `isize`/`usize`
358    ///
359    /// Whereas a wildcard of type `i32` stands for the range `i32::MIN..=i32::MAX`, a `usize`
360    /// wildcard stands for `0..PosInfinity` and a `isize` wildcard stands for
361    /// `NegInfinity..PosInfinity`. In other words, as far as `IntRange` is concerned, there are
362    /// values before `isize::MIN` and after `usize::MAX`/`isize::MAX`.
363    /// This is to avoid e.g. `0..(u32::MAX as usize)` from being exhaustive on one architecture and
364    /// not others. This was decided in <https://github.com/rust-lang/rfcs/pull/2591>.
365    ///
366    /// These infinities affect splitting subtly: it is possible to get `NegInfinity..0` and
367    /// `usize::MAX+1..PosInfinity` in the output. Diagnostics must be careful to handle these
368    /// fictitious ranges sensibly.
369    fn split(
370        &self,
371        column_ranges: impl Iterator<Item = IntRange>,
372    ) -> impl Iterator<Item = (Presence, IntRange)> {
373        // The boundaries of ranges in `column_ranges` intersected with `self`.
374        // We do parenthesis matching for input ranges. A boundary counts as +1 if it starts
375        // a range and -1 if it ends it. When the count is > 0 between two boundaries, we
376        // are within an input range.
377        let mut boundaries: Vec<(MaybeInfiniteInt, isize)> = column_ranges
378            .filter_map(|r| self.intersection(&r))
379            .flat_map(|r| [(r.lo, 1), (r.hi, -1)])
380            .collect();
381        // We sort by boundary, and for each boundary we sort the "closing parentheses" first. The
382        // order of +1/-1 for a same boundary value is actually irrelevant, because we only look at
383        // the accumulated count between distinct boundary values.
384        boundaries.sort_unstable();
385
386        // Accumulate parenthesis counts.
387        let mut paren_counter = 0isize;
388        // Gather pairs of adjacent boundaries.
389        let mut prev_bdy = self.lo;
390        boundaries
391            .into_iter()
392            // End with the end of the range. The count is ignored.
393            .chain(once((self.hi, 0)))
394            // List pairs of adjacent boundaries and the count between them.
395            .map(move |(bdy, delta)| {
396                // `delta` affects the count as we cross `bdy`, so the relevant count between
397                // `prev_bdy` and `bdy` is untouched by `delta`.
398                let ret = (prev_bdy, paren_counter, bdy);
399                prev_bdy = bdy;
400                paren_counter += delta;
401                ret
402            })
403            // Skip empty ranges.
404            .filter(|&(prev_bdy, _, bdy)| prev_bdy != bdy)
405            // Convert back to ranges.
406            .map(move |(prev_bdy, paren_count, bdy)| {
407                use Presence::*;
408                let presence = if paren_count > 0 { Seen } else { Unseen };
409                let range = IntRange { lo: prev_bdy, hi: bdy };
410                (presence, range)
411            })
412    }
413}
414
415/// Note: this will render signed ranges incorrectly. To render properly, convert to a pattern
416/// first.
417impl fmt::Debug for IntRange {
418    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
419        if self.is_singleton() {
420            // Only finite ranges can be singletons.
421            let Finite(lo) = self.lo else { unreachable!() };
422            write!(f, "{lo}")?;
423        } else {
424            if let Finite(lo) = self.lo {
425                write!(f, "{lo}")?;
426            }
427            write!(f, "{}", RangeEnd::Excluded)?;
428            if let Finite(hi) = self.hi {
429                write!(f, "{hi}")?;
430            }
431        }
432        Ok(())
433    }
434}
435
436#[derive(Copy, Clone, Debug, PartialEq, Eq)]
437pub enum SliceKind {
438    /// Patterns of length `n` (`[x, y]`).
439    FixedLen(usize),
440    /// Patterns using the `..` notation (`[x, .., y]`).
441    /// Captures any array constructor of `length >= i + j`.
442    /// In the case where `array_len` is `Some(_)`,
443    /// this indicates that we only care about the first `i` and the last `j` values of the array,
444    /// and everything in between is a wildcard `_`.
445    VarLen(usize, usize),
446}
447
448impl SliceKind {
449    pub fn arity(self) -> usize {
450        match self {
451            FixedLen(length) => length,
452            VarLen(prefix, suffix) => prefix + suffix,
453        }
454    }
455
456    /// Whether this pattern includes patterns of length `other_len`.
457    fn covers_length(self, other_len: usize) -> bool {
458        match self {
459            FixedLen(len) => len == other_len,
460            VarLen(prefix, suffix) => prefix + suffix <= other_len,
461        }
462    }
463}
464
465/// A constructor for array and slice patterns.
466#[derive(Copy, Clone, Debug, PartialEq, Eq)]
467pub struct Slice {
468    /// `None` if the matched value is a slice, `Some(n)` if it is an array of size `n`.
469    pub(crate) array_len: Option<usize>,
470    /// The kind of pattern it is: fixed-length `[x, y]` or variable length `[x, .., y]`.
471    pub(crate) kind: SliceKind,
472}
473
474impl Slice {
475    pub fn new(array_len: Option<usize>, kind: SliceKind) -> Self {
476        let kind = match (array_len, kind) {
477            // If the middle `..` has length 0, we effectively have a fixed-length pattern.
478            (Some(len), VarLen(prefix, suffix)) if prefix + suffix == len => FixedLen(len),
479            (Some(len), VarLen(prefix, suffix)) if prefix + suffix > len => panic!(
480                "Slice pattern of length {} longer than its array length {len}",
481                prefix + suffix
482            ),
483            _ => kind,
484        };
485        Slice { array_len, kind }
486    }
487
488    pub fn arity(self) -> usize {
489        self.kind.arity()
490    }
491
492    /// See `Constructor::is_covered_by`
493    fn is_covered_by(self, other: Self) -> bool {
494        other.kind.covers_length(self.arity())
495    }
496
497    /// This computes constructor splitting for variable-length slices, as explained at the top of
498    /// the file.
499    ///
500    /// A slice pattern `[x, .., y]` behaves like the infinite or-pattern `[x, y] | [x, _, y] | [x,
501    /// _, _, y] | etc`. The corresponding value constructors are fixed-length array constructors of
502    /// corresponding lengths. We obviously can't list this infinitude of constructors.
503    /// Thankfully, it turns out that for each finite set of slice patterns, all sufficiently large
504    /// array lengths are equivalent.
505    ///
506    /// Let's look at an example, where we are trying to split the last pattern:
507    /// ```
508    /// # fn foo(x: &[bool]) {
509    /// match x {
510    ///     [true, true, ..] => {}
511    ///     [.., false, false] => {}
512    ///     [..] => {}
513    /// }
514    /// # }
515    /// ```
516    /// Here are the results of specialization for the first few lengths:
517    /// ```
518    /// # fn foo(x: &[bool]) { match x {
519    /// // length 0
520    /// [] => {}
521    /// // length 1
522    /// [_] => {}
523    /// // length 2
524    /// [true, true] => {}
525    /// [false, false] => {}
526    /// [_, _] => {}
527    /// // length 3
528    /// [true, true,  _    ] => {}
529    /// [_,    false, false] => {}
530    /// [_,    _,     _    ] => {}
531    /// // length 4
532    /// [true, true, _,     _    ] => {}
533    /// [_,    _,    false, false] => {}
534    /// [_,    _,    _,     _    ] => {}
535    /// // length 5
536    /// [true, true, _, _,     _    ] => {}
537    /// [_,    _,    _, false, false] => {}
538    /// [_,    _,    _, _,     _    ] => {}
539    /// # _ => {}
540    /// # }}
541    /// ```
542    ///
543    /// We see that above length 4, we are simply inserting columns full of wildcards in the middle.
544    /// This means that specialization and witness computation with slices of length `l >= 4` will
545    /// give equivalent results regardless of `l`. This applies to any set of slice patterns: there
546    /// will be a length `L` above which all lengths behave the same. This is exactly what we need
547    /// for constructor splitting.
548    ///
549    /// A variable-length slice pattern covers all lengths from its arity up to infinity. As we just
550    /// saw, we can split this in two: lengths below `L` are treated individually with a
551    /// fixed-length slice each; lengths above `L` are grouped into a single variable-length slice
552    /// constructor.
553    ///
554    /// For each variable-length slice pattern `p` with a prefix of length `plₚ` and suffix of
555    /// length `slₚ`, only the first `plₚ` and the last `slₚ` elements are examined. Therefore, as
556    /// long as `L` is positive (to avoid concerns about empty types), all elements after the
557    /// maximum prefix length and before the maximum suffix length are not examined by any
558    /// variable-length pattern, and therefore can be ignored. This gives us a way to compute `L`.
559    ///
560    /// Additionally, if fixed-length patterns exist, we must pick an `L` large enough to miss them,
561    /// so we can pick `L = max(max(FIXED_LEN)+1, max(PREFIX_LEN) + max(SUFFIX_LEN))`.
562    /// `max_slice` below will be made to have this arity `L`.
563    ///
564    /// If `self` is fixed-length, it is returned as-is.
565    ///
566    /// Additionally, we track for each output slice whether it is covered by one of the column slices or not.
567    fn split(
568        self,
569        column_slices: impl Iterator<Item = Slice>,
570    ) -> impl Iterator<Item = (Presence, Slice)> {
571        // Range of lengths below `L`.
572        let smaller_lengths;
573        let arity = self.arity();
574        let mut max_slice = self.kind;
575        // Tracks the smallest variable-length slice we've seen. Any slice arity above it is
576        // therefore `Presence::Seen` in the column.
577        let mut min_var_len = usize::MAX;
578        // Tracks the fixed-length slices we've seen, to mark them as `Presence::Seen`.
579        let mut seen_fixed_lens = GrowableBitSet::new_empty();
580        match &mut max_slice {
581            VarLen(max_prefix_len, max_suffix_len) => {
582                // A length larger than any fixed-length slice encountered.
583                // We start at 1 in case the subtype is empty because in that case the zero-length
584                // slice must be treated separately from the rest.
585                let mut fixed_len_upper_bound = 1;
586                // We grow `max_slice` to be larger than all slices encountered, as described above.
587                // `L` is `max_slice.arity()`. For diagnostics, we keep the prefix and suffix
588                // lengths separate.
589                for slice in column_slices {
590                    match slice.kind {
591                        FixedLen(len) => {
592                            fixed_len_upper_bound = cmp::max(fixed_len_upper_bound, len + 1);
593                            seen_fixed_lens.insert(len);
594                        }
595                        VarLen(prefix, suffix) => {
596                            *max_prefix_len = cmp::max(*max_prefix_len, prefix);
597                            *max_suffix_len = cmp::max(*max_suffix_len, suffix);
598                            min_var_len = cmp::min(min_var_len, prefix + suffix);
599                        }
600                    }
601                }
602                // If `fixed_len_upper_bound >= L`, we set `L` to `fixed_len_upper_bound`.
603                if let Some(delta) =
604                    fixed_len_upper_bound.checked_sub(*max_prefix_len + *max_suffix_len)
605                {
606                    *max_prefix_len += delta
607                }
608
609                // We cap the arity of `max_slice` at the array size.
610                match self.array_len {
611                    Some(len) if max_slice.arity() >= len => max_slice = FixedLen(len),
612                    _ => {}
613                }
614
615                smaller_lengths = match self.array_len {
616                    // The only admissible fixed-length slice is one of the array size. Whether `max_slice`
617                    // is fixed-length or variable-length, it will be the only relevant slice to output
618                    // here.
619                    Some(_) => 0..0, // empty range
620                    // We need to cover all arities in the range `(arity..infinity)`. We split that
621                    // range into two: lengths smaller than `max_slice.arity()` are treated
622                    // independently as fixed-lengths slices, and lengths above are captured by
623                    // `max_slice`.
624                    None => self.arity()..max_slice.arity(),
625                };
626            }
627            FixedLen(_) => {
628                // No need to split here. We only track presence.
629                for slice in column_slices {
630                    match slice.kind {
631                        FixedLen(len) => {
632                            if len == arity {
633                                seen_fixed_lens.insert(len);
634                            }
635                        }
636                        VarLen(prefix, suffix) => {
637                            min_var_len = cmp::min(min_var_len, prefix + suffix);
638                        }
639                    }
640                }
641                smaller_lengths = 0..0;
642            }
643        };
644
645        smaller_lengths.map(FixedLen).chain(once(max_slice)).map(move |kind| {
646            let arity = kind.arity();
647            let seen = if min_var_len <= arity || seen_fixed_lens.contains(arity) {
648                Presence::Seen
649            } else {
650                Presence::Unseen
651            };
652            (seen, Slice::new(self.array_len, kind))
653        })
654    }
655}
656
657/// A globally unique id to distinguish `Opaque` patterns.
658#[derive(Clone, Debug, PartialEq, Eq)]
659pub struct OpaqueId(u32);
660
661impl OpaqueId {
662    pub fn new() -> Self {
663        use std::sync::atomic::{AtomicU32, Ordering};
664        static OPAQUE_ID: AtomicU32 = AtomicU32::new(0);
665        OpaqueId(OPAQUE_ID.fetch_add(1, Ordering::SeqCst))
666    }
667}
668
669/// A value can be decomposed into a constructor applied to some fields. This struct represents
670/// the constructor. See also `Fields`.
671///
672/// `pat_constructor` retrieves the constructor corresponding to a pattern.
673/// `specialize_constructor` returns the list of fields corresponding to a pattern, given a
674/// constructor. `Constructor::apply` reconstructs the pattern from a pair of `Constructor` and
675/// `Fields`.
676#[derive(Debug)]
677pub enum Constructor<Cx: PatCx> {
678    /// Tuples and structs.
679    Struct,
680    /// Enum variants.
681    Variant(Cx::VariantIdx),
682    /// References
683    Ref,
684    /// Array and slice patterns.
685    Slice(Slice),
686    /// Union field accesses.
687    UnionField,
688    /// Booleans
689    Bool(bool),
690    /// Ranges of integer literal values (`2`, `2..=5` or `2..5`).
691    IntRange(IntRange),
692    /// Ranges of floating-point literal values (`2.0..=5.2`).
693    F16Range(IeeeFloat<HalfS>, IeeeFloat<HalfS>, RangeEnd),
694    F32Range(IeeeFloat<SingleS>, IeeeFloat<SingleS>, RangeEnd),
695    F64Range(IeeeFloat<DoubleS>, IeeeFloat<DoubleS>, RangeEnd),
696    F128Range(IeeeFloat<QuadS>, IeeeFloat<QuadS>, RangeEnd),
697    /// String literals. Strings are not quite the same as `&[u8]` so we treat them separately.
698    Str(Cx::StrLit),
699    /// Deref patterns (enabled by the `deref_patterns` feature) provide a way of matching on a
700    /// smart pointer ADT through its pointee. They don't directly correspond to ADT constructors,
701    /// and currently are not supported alongside them. Carries the type of the pointee.
702    DerefPattern(Cx::Ty),
703    /// Constants that must not be matched structurally. They are treated as black boxes for the
704    /// purposes of exhaustiveness: we must not inspect them, and they don't count towards making a
705    /// match exhaustive.
706    /// Carries an id that must be unique within a match. We need this to ensure the invariants of
707    /// [`SplitConstructorSet`].
708    Opaque(OpaqueId),
709    /// Or-pattern.
710    Or,
711    /// Wildcard pattern.
712    Wildcard,
713    /// Never pattern. Only used in `WitnessPat`. An actual never pattern should be lowered as
714    /// `Wildcard`.
715    Never,
716    /// Fake extra constructor for enums that aren't allowed to be matched exhaustively. Also used
717    /// for those types for which we cannot list constructors explicitly, like `f64` and `str`. Only
718    /// used in `WitnessPat`.
719    NonExhaustive,
720    /// Fake extra constructor for variants that should not be mentioned in diagnostics. We use this
721    /// for variants behind an unstable gate as well as `#[doc(hidden)]` ones. Only used in
722    /// `WitnessPat`.
723    Hidden,
724    /// Fake extra constructor for constructors that are not seen in the matrix, as explained at the
725    /// top of the file. Only used for specialization.
726    Missing,
727    /// Fake extra constructor that indicates and empty field that is private. When we encounter one
728    /// we skip the column entirely so we don't observe its emptiness. Only used for specialization.
729    PrivateUninhabited,
730}
731
732impl<Cx: PatCx> Clone for Constructor<Cx> {
733    fn clone(&self) -> Self {
734        match self {
735            Constructor::Struct => Constructor::Struct,
736            Constructor::Variant(idx) => Constructor::Variant(*idx),
737            Constructor::Ref => Constructor::Ref,
738            Constructor::Slice(slice) => Constructor::Slice(*slice),
739            Constructor::UnionField => Constructor::UnionField,
740            Constructor::Bool(b) => Constructor::Bool(*b),
741            Constructor::IntRange(range) => Constructor::IntRange(*range),
742            Constructor::F16Range(lo, hi, end) => Constructor::F16Range(*lo, *hi, *end),
743            Constructor::F32Range(lo, hi, end) => Constructor::F32Range(*lo, *hi, *end),
744            Constructor::F64Range(lo, hi, end) => Constructor::F64Range(*lo, *hi, *end),
745            Constructor::F128Range(lo, hi, end) => Constructor::F128Range(*lo, *hi, *end),
746            Constructor::Str(value) => Constructor::Str(value.clone()),
747            Constructor::DerefPattern(ty) => Constructor::DerefPattern(ty.clone()),
748            Constructor::Opaque(inner) => Constructor::Opaque(inner.clone()),
749            Constructor::Or => Constructor::Or,
750            Constructor::Never => Constructor::Never,
751            Constructor::Wildcard => Constructor::Wildcard,
752            Constructor::NonExhaustive => Constructor::NonExhaustive,
753            Constructor::Hidden => Constructor::Hidden,
754            Constructor::Missing => Constructor::Missing,
755            Constructor::PrivateUninhabited => Constructor::PrivateUninhabited,
756        }
757    }
758}
759
760impl<Cx: PatCx> Constructor<Cx> {
761    pub(crate) fn is_non_exhaustive(&self) -> bool {
762        matches!(self, NonExhaustive)
763    }
764
765    pub(crate) fn as_variant(&self) -> Option<Cx::VariantIdx> {
766        match self {
767            Variant(i) => Some(*i),
768            _ => None,
769        }
770    }
771    fn as_bool(&self) -> Option<bool> {
772        match self {
773            Bool(b) => Some(*b),
774            _ => None,
775        }
776    }
777    pub(crate) fn as_int_range(&self) -> Option<&IntRange> {
778        match self {
779            IntRange(range) => Some(range),
780            _ => None,
781        }
782    }
783    fn as_slice(&self) -> Option<Slice> {
784        match self {
785            Slice(slice) => Some(*slice),
786            _ => None,
787        }
788    }
789
790    /// The number of fields for this constructor. This must be kept in sync with
791    /// `Fields::wildcards`.
792    pub(crate) fn arity(&self, cx: &Cx, ty: &Cx::Ty) -> usize {
793        cx.ctor_arity(self, ty)
794    }
795
796    /// Returns whether `self` is covered by `other`, i.e. whether `self` is a subset of `other`.
797    /// For the simple cases, this is simply checking for equality. For the "grouped" constructors,
798    /// this checks for inclusion.
799    // We inline because this has a single call site in `Matrix::specialize_constructor`.
800    #[inline]
801    pub(crate) fn is_covered_by(&self, cx: &Cx, other: &Self) -> Result<bool, Cx::Error> {
802        Ok(match (self, other) {
803            (Wildcard, _) => {
804                return Err(cx.bug(format_args!(
805                    "Constructor splitting should not have returned `Wildcard`"
806                )));
807            }
808            // Wildcards cover anything
809            (_, Wildcard) => true,
810            // `PrivateUninhabited` skips everything.
811            (PrivateUninhabited, _) => true,
812            // Only a wildcard pattern can match these special constructors.
813            (Missing { .. } | NonExhaustive | Hidden, _) => false,
814
815            (Struct, Struct) => true,
816            (Ref, Ref) => true,
817            (UnionField, UnionField) => true,
818            (Variant(self_id), Variant(other_id)) => self_id == other_id,
819            (Bool(self_b), Bool(other_b)) => self_b == other_b,
820
821            (IntRange(self_range), IntRange(other_range)) => self_range.is_subrange(other_range),
822            (F16Range(self_from, self_to, self_end), F16Range(other_from, other_to, other_end)) => {
823                self_from.ge(other_from)
824                    && match self_to.partial_cmp(other_to) {
825                        Some(Ordering::Less) => true,
826                        Some(Ordering::Equal) => other_end == self_end,
827                        _ => false,
828                    }
829            }
830            (F32Range(self_from, self_to, self_end), F32Range(other_from, other_to, other_end)) => {
831                self_from.ge(other_from)
832                    && match self_to.partial_cmp(other_to) {
833                        Some(Ordering::Less) => true,
834                        Some(Ordering::Equal) => other_end == self_end,
835                        _ => false,
836                    }
837            }
838            (F64Range(self_from, self_to, self_end), F64Range(other_from, other_to, other_end)) => {
839                self_from.ge(other_from)
840                    && match self_to.partial_cmp(other_to) {
841                        Some(Ordering::Less) => true,
842                        Some(Ordering::Equal) => other_end == self_end,
843                        _ => false,
844                    }
845            }
846            (
847                F128Range(self_from, self_to, self_end),
848                F128Range(other_from, other_to, other_end),
849            ) => {
850                self_from.ge(other_from)
851                    && match self_to.partial_cmp(other_to) {
852                        Some(Ordering::Less) => true,
853                        Some(Ordering::Equal) => other_end == self_end,
854                        _ => false,
855                    }
856            }
857            (Str(self_val), Str(other_val)) => {
858                // FIXME Once valtrees are available we can directly use the bytes
859                // in the `Str` variant of the valtree for the comparison here.
860                self_val == other_val
861            }
862            (Slice(self_slice), Slice(other_slice)) => self_slice.is_covered_by(*other_slice),
863
864            // Deref patterns only interact with other deref patterns. Prior to usefulness analysis,
865            // we ensure they don't appear alongside any other non-wild non-opaque constructors.
866            (DerefPattern(_), DerefPattern(_)) => true,
867
868            // Opaque constructors don't interact with anything unless they come from the
869            // syntactically identical pattern.
870            (Opaque(self_id), Opaque(other_id)) => self_id == other_id,
871            (Opaque(..), _) | (_, Opaque(..)) => false,
872
873            _ => {
874                return Err(cx.bug(format_args!(
875                    "trying to compare incompatible constructors {self:?} and {other:?}"
876                )));
877            }
878        })
879    }
880
881    pub(crate) fn fmt_fields(
882        &self,
883        f: &mut fmt::Formatter<'_>,
884        ty: &Cx::Ty,
885        mut fields: impl Iterator<Item = impl fmt::Debug>,
886    ) -> fmt::Result {
887        let mut first = true;
888        let mut start_or_continue = |s| {
889            if first {
890                first = false;
891                ""
892            } else {
893                s
894            }
895        };
896        let mut start_or_comma = || start_or_continue(", ");
897
898        match self {
899            Struct | Variant(_) | UnionField => {
900                Cx::write_variant_name(f, self, ty)?;
901                // Without `cx`, we can't know which field corresponds to which, so we can't
902                // get the names of the fields. Instead we just display everything as a tuple
903                // struct, which should be good enough.
904                write!(f, "(")?;
905                for p in fields {
906                    write!(f, "{}{:?}", start_or_comma(), p)?;
907                }
908                write!(f, ")")?;
909            }
910            // Note: given the expansion of `&str` patterns done in `expand_pattern`, we should
911            // be careful to detect strings here. However a string literal pattern will never
912            // be reported as a non-exhaustiveness witness, so we can ignore this issue.
913            Ref => {
914                write!(f, "&{:?}", fields.next().unwrap())?;
915            }
916            Slice(slice) => {
917                write!(f, "[")?;
918                match slice.kind {
919                    SliceKind::FixedLen(_) => {
920                        for p in fields {
921                            write!(f, "{}{:?}", start_or_comma(), p)?;
922                        }
923                    }
924                    SliceKind::VarLen(prefix_len, _) => {
925                        for p in fields.by_ref().take(prefix_len) {
926                            write!(f, "{}{:?}", start_or_comma(), p)?;
927                        }
928                        write!(f, "{}..", start_or_comma())?;
929                        for p in fields {
930                            write!(f, "{}{:?}", start_or_comma(), p)?;
931                        }
932                    }
933                }
934                write!(f, "]")?;
935            }
936            Bool(b) => write!(f, "{b}")?,
937            // Best-effort, will render signed ranges incorrectly
938            IntRange(range) => write!(f, "{range:?}")?,
939            F16Range(lo, hi, end) => write!(f, "{lo}{end}{hi}")?,
940            F32Range(lo, hi, end) => write!(f, "{lo}{end}{hi}")?,
941            F64Range(lo, hi, end) => write!(f, "{lo}{end}{hi}")?,
942            F128Range(lo, hi, end) => write!(f, "{lo}{end}{hi}")?,
943            Str(value) => write!(f, "{value:?}")?,
944            DerefPattern(_) => write!(f, "deref!({:?})", fields.next().unwrap())?,
945            Opaque(..) => write!(f, "<constant pattern>")?,
946            Or => {
947                for pat in fields {
948                    write!(f, "{}{:?}", start_or_continue(" | "), pat)?;
949                }
950            }
951            Never => write!(f, "!")?,
952            Wildcard | Missing | NonExhaustive | Hidden | PrivateUninhabited => {
953                write!(f, "_ : {:?}", ty)?
954            }
955        }
956        Ok(())
957    }
958}
959
960#[derive(Debug, Clone, Copy)]
961pub enum VariantVisibility {
962    /// Variant that doesn't fit the other cases, i.e. most variants.
963    Visible,
964    /// Variant behind an unstable gate or with the `#[doc(hidden)]` attribute. It will not be
965    /// mentioned in diagnostics unless the user mentioned it first.
966    Hidden,
967    /// Variant that matches no value. E.g. `Some::<Option<!>>` if the `exhaustive_patterns` feature
968    /// is enabled. Like `Hidden`, it will not be mentioned in diagnostics unless the user mentioned
969    /// it first.
970    Empty,
971}
972
973/// Describes the set of all constructors for a type. For details, in particular about the emptiness
974/// of constructors, see the top of the file.
975///
976/// In terms of division of responsibility, [`ConstructorSet::split`] handles all of the
977/// `exhaustive_patterns` feature.
978#[derive(Debug)]
979pub enum ConstructorSet<Cx: PatCx> {
980    /// The type is a tuple or struct. `empty` tracks whether the type is empty.
981    Struct { empty: bool },
982    /// This type has the following list of constructors. If `variants` is empty and
983    /// `non_exhaustive` is false, don't use this; use `NoConstructors` instead.
984    Variants { variants: IndexVec<Cx::VariantIdx, VariantVisibility>, non_exhaustive: bool },
985    /// The type is `&T`.
986    Ref,
987    /// The type is a union.
988    Union,
989    /// Booleans.
990    Bool,
991    /// The type is spanned by integer values. The range or ranges give the set of allowed values.
992    /// The second range is only useful for `char`.
993    Integers { range_1: IntRange, range_2: Option<IntRange> },
994    /// The type is matched by slices. `array_len` is the compile-time length of the array, if
995    /// known. If `subtype_is_empty`, all constructors are empty except possibly the zero-length
996    /// slice `[]`.
997    Slice { array_len: Option<usize>, subtype_is_empty: bool },
998    /// The constructors cannot be listed, and the type cannot be matched exhaustively. E.g. `str`,
999    /// floats.
1000    Unlistable,
1001    /// The type has no constructors (not even empty ones). This is `!` and empty enums.
1002    NoConstructors,
1003}
1004
1005/// Describes the result of analyzing the constructors in a column of a match.
1006///
1007/// `present` is morally the set of constructors present in the column, and `missing` is the set of
1008/// constructors that exist in the type but are not present in the column.
1009///
1010/// More formally, if we discard wildcards from the column, this respects the following constraints:
1011/// 1. the union of `present`, `missing` and `missing_empty` covers all the constructors of the type
1012/// 2. each constructor in `present` is covered by something in the column
1013/// 3. no constructor in `missing` or `missing_empty` is covered by anything in the column
1014/// 4. each constructor in the column is equal to the union of one or more constructors in `present`
1015/// 5. `missing` does not contain empty constructors (see discussion about emptiness at the top of
1016///    the file);
1017/// 6. `missing_empty` contains only empty constructors
1018/// 7. constructors in `present`, `missing` and `missing_empty` are split for the column; in other
1019///    words, they are either fully included in or fully disjoint from each constructor in the
1020///    column. In yet other words, there are no non-trivial intersections like between `0..10` and
1021///    `5..15`.
1022///
1023/// We must be particularly careful with weird constructors like `Opaque`: they're not formally part
1024/// of the `ConstructorSet` for the type, yet if we forgot to include them in `present` we would be
1025/// ignoring any row with `Opaque`s in the algorithm. Hence the importance of point 4.
1026#[derive(Debug)]
1027pub struct SplitConstructorSet<Cx: PatCx> {
1028    pub present: SmallVec<[Constructor<Cx>; 1]>,
1029    pub missing: Vec<Constructor<Cx>>,
1030    pub missing_empty: Vec<Constructor<Cx>>,
1031}
1032
1033impl<Cx: PatCx> ConstructorSet<Cx> {
1034    /// This analyzes a column of constructors to 1/ determine which constructors of the type (if
1035    /// any) are missing; 2/ split constructors to handle non-trivial intersections e.g. on ranges
1036    /// or slices. This can get subtle; see [`SplitConstructorSet`] for details of this operation
1037    /// and its invariants.
1038    pub fn split<'a>(
1039        &self,
1040        ctors: impl Iterator<Item = &'a Constructor<Cx>> + Clone,
1041    ) -> SplitConstructorSet<Cx>
1042    where
1043        Cx: 'a,
1044    {
1045        let mut present: SmallVec<[_; 1]> = SmallVec::new();
1046        // Empty constructors found missing.
1047        let mut missing_empty = Vec::new();
1048        // Nonempty constructors found missing.
1049        let mut missing = Vec::new();
1050        // Constructors in `ctors`, except wildcards and opaques.
1051        let mut seen = Vec::new();
1052        // If we see a deref pattern, it must be the only non-wildcard non-opaque constructor; we
1053        // ensure this prior to analysis.
1054        let mut deref_pat_present = false;
1055        for ctor in ctors.cloned() {
1056            match ctor {
1057                DerefPattern(..) => {
1058                    if !deref_pat_present {
1059                        deref_pat_present = true;
1060                        present.push(ctor);
1061                    }
1062                }
1063                Opaque(..) => present.push(ctor),
1064                Wildcard => {} // discard wildcards
1065                _ => seen.push(ctor),
1066            }
1067        }
1068
1069        match self {
1070            _ if deref_pat_present => {
1071                // Deref patterns are the only constructor; nothing is missing.
1072            }
1073            ConstructorSet::Struct { empty } => {
1074                if !seen.is_empty() {
1075                    present.push(Struct);
1076                } else if *empty {
1077                    missing_empty.push(Struct);
1078                } else {
1079                    missing.push(Struct);
1080                }
1081            }
1082            ConstructorSet::Ref => {
1083                if !seen.is_empty() {
1084                    present.push(Ref);
1085                } else {
1086                    missing.push(Ref);
1087                }
1088            }
1089            ConstructorSet::Union => {
1090                if !seen.is_empty() {
1091                    present.push(UnionField);
1092                } else {
1093                    missing.push(UnionField);
1094                }
1095            }
1096            ConstructorSet::Variants { variants, non_exhaustive } => {
1097                let mut seen_set = DenseBitSet::new_empty(variants.len());
1098                for idx in seen.iter().filter_map(|c| c.as_variant()) {
1099                    seen_set.insert(idx);
1100                }
1101                let mut skipped_a_hidden_variant = false;
1102
1103                for (idx, visibility) in variants.iter_enumerated() {
1104                    let ctor = Variant(idx);
1105                    if seen_set.contains(idx) {
1106                        present.push(ctor);
1107                    } else {
1108                        // We only put visible variants directly into `missing`.
1109                        match visibility {
1110                            VariantVisibility::Visible => missing.push(ctor),
1111                            VariantVisibility::Hidden => skipped_a_hidden_variant = true,
1112                            VariantVisibility::Empty => missing_empty.push(ctor),
1113                        }
1114                    }
1115                }
1116
1117                if skipped_a_hidden_variant {
1118                    missing.push(Hidden);
1119                }
1120                if *non_exhaustive {
1121                    missing.push(NonExhaustive);
1122                }
1123            }
1124            ConstructorSet::Bool => {
1125                let mut seen_false = false;
1126                let mut seen_true = false;
1127                for b in seen.iter().filter_map(|ctor| ctor.as_bool()) {
1128                    if b {
1129                        seen_true = true;
1130                    } else {
1131                        seen_false = true;
1132                    }
1133                }
1134                if seen_false {
1135                    present.push(Bool(false));
1136                } else {
1137                    missing.push(Bool(false));
1138                }
1139                if seen_true {
1140                    present.push(Bool(true));
1141                } else {
1142                    missing.push(Bool(true));
1143                }
1144            }
1145            ConstructorSet::Integers { range_1, range_2 } => {
1146                let seen_ranges: Vec<_> =
1147                    seen.iter().filter_map(|ctor| ctor.as_int_range()).copied().collect();
1148                for (seen, splitted_range) in range_1.split(seen_ranges.iter().cloned()) {
1149                    match seen {
1150                        Presence::Unseen => missing.push(IntRange(splitted_range)),
1151                        Presence::Seen => present.push(IntRange(splitted_range)),
1152                    }
1153                }
1154                if let Some(range_2) = range_2 {
1155                    for (seen, splitted_range) in range_2.split(seen_ranges.into_iter()) {
1156                        match seen {
1157                            Presence::Unseen => missing.push(IntRange(splitted_range)),
1158                            Presence::Seen => present.push(IntRange(splitted_range)),
1159                        }
1160                    }
1161                }
1162            }
1163            ConstructorSet::Slice { array_len, subtype_is_empty } => {
1164                let seen_slices = seen.iter().filter_map(|c| c.as_slice());
1165                let base_slice = Slice::new(*array_len, VarLen(0, 0));
1166                for (seen, splitted_slice) in base_slice.split(seen_slices) {
1167                    let ctor = Slice(splitted_slice);
1168                    match seen {
1169                        Presence::Seen => present.push(ctor),
1170                        Presence::Unseen => {
1171                            if *subtype_is_empty && splitted_slice.arity() != 0 {
1172                                // We have subpatterns of an empty type, so the constructor is
1173                                // empty.
1174                                missing_empty.push(ctor);
1175                            } else {
1176                                missing.push(ctor);
1177                            }
1178                        }
1179                    }
1180                }
1181            }
1182            ConstructorSet::Unlistable => {
1183                // Since we can't list constructors, we take the ones in the column. This might list
1184                // some constructors several times but there's not much we can do.
1185                present.extend(seen);
1186                missing.push(NonExhaustive);
1187            }
1188            ConstructorSet::NoConstructors => {
1189                // In a `MaybeInvalid` place even an empty pattern may be reachable. We therefore
1190                // add a dummy empty constructor here, which will be ignored if the place is
1191                // `ValidOnly`.
1192                missing_empty.push(Never);
1193            }
1194        }
1195
1196        SplitConstructorSet { present, missing, missing_empty }
1197    }
1198
1199    /// Whether this set only contains empty constructors.
1200    pub(crate) fn all_empty(&self) -> bool {
1201        match self {
1202            ConstructorSet::Bool
1203            | ConstructorSet::Integers { .. }
1204            | ConstructorSet::Ref
1205            | ConstructorSet::Union
1206            | ConstructorSet::Unlistable => false,
1207            ConstructorSet::NoConstructors => true,
1208            ConstructorSet::Struct { empty } => *empty,
1209            ConstructorSet::Variants { variants, non_exhaustive } => {
1210                !*non_exhaustive
1211                    && variants
1212                        .iter()
1213                        .all(|visibility| matches!(visibility, VariantVisibility::Empty))
1214            }
1215            ConstructorSet::Slice { array_len, subtype_is_empty } => {
1216                *subtype_is_empty && matches!(array_len, Some(1..))
1217            }
1218        }
1219    }
1220}