Enum rustc_mir_build::thir::pattern::usefulness::SubPatSet[−][src]

enum SubPatSet<'p, 'tcx> {
    Empty,
    Full,
    Seq {
        subpats: FxHashMap<usize, SubPatSet<'p, 'tcx>>,
    },
    Alt {
        subpats: FxHashMap<usize, SubPatSet<'p, 'tcx>>,
        alt_count: usize,
        pat: &'p Pat<'tcx>,
    },
}
Expand description

Given a pattern or a pattern-stack, this struct captures a set of its subpatterns. We use that to track reachable sub-patterns arising from or-patterns. In the absence of or-patterns this will always be either Empty (the whole pattern is unreachable) or Full (the whole pattern is reachable). When there are or-patterns, some subpatterns may be reachable while others aren’t. In this case the whole pattern still counts as reachable, but we will lint the unreachable subpatterns.

This supports a limited set of operations, so not all possible sets of subpatterns can be represented. That’s ok, we only want the ones that make sense for our usage.

What we’re doing is illustrated by this:

match (true, 0) {
    (true, 0) => {}
    (_, 1) => {}
    (true | false, 0 | 1) => {}
}

When we try the alternatives of the true | false or-pattern, the last 0 is reachable in the false alternative but not the true. So overall it is reachable. By contrast, the last 1 is not reachable in either alternative, so we want to signal this to the user. Therefore we take the union of sets of reachable patterns coming from different alternatives in order to figure out which subpatterns are overall reachable.

Invariant: we try to construct the smallest representation we can. In particular if self.is_empty() we ensure that self is Empty, and same with Full. This is not important for correctness currently.

Variants

Empty

The empty set. This means the pattern is unreachable.

Full

The set containing the full pattern.

Seq

If the pattern is a pattern with a constructor or a pattern-stack, we store a set for each of its subpatterns. Missing entries in the map are implicitly full, because that’s the common case.

Show fields

Fields of Seq

subpats: FxHashMap<usize, SubPatSet<'p, 'tcx>>
Alt

If the pattern is an or-pattern, we store a set for each of its alternatives. Missing entries in the map are implicitly empty. Note: we always flatten nested or-patterns.

Show fields

Fields of Alt

subpats: FxHashMap<usize, SubPatSet<'p, 'tcx>>alt_count: usize

Counts the total number of alternatives in the pattern

pat: &'p Pat<'tcx>

We keep the pattern around to retrieve spans.

Implementations

Union self with other, mutating self.

Returns a list of the spans of the unreachable subpatterns. If self is empty (i.e. the whole pattern is unreachable) we return None.

When self refers to a patstack that was obtained from specialization, after running unspecialize it will refer to the original patstack before specialization.

When self refers to a patstack that was obtained from splitting an or-pattern, after running unspecialize it will refer to the original patstack before splitting.

For example:

match Some(true) {
    Some(true) => {}
    None | Some(true | false) => {}
}

Here None would return the full set and Some(true | false) would return the set containing false. After unsplit_or_pat, we want the set to contain None and false. This is what this function does.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Layout

Note: Most layout information is completely unstable and may be different between compiler versions and platforms. The only exception is types with certain repr(...) attributes. Please see the Rust Reference’s “Type Layout” chapter for details on type layout guarantees.

Size: 56 bytes