diff options
| author | León Orell Valerian Liehr <me@fmease.dev> | 2024-06-19 09:52:00 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-06-19 09:52:00 +0200 |
| commit | e111e9925304307b04d1a9c94e7081c29b4582b6 (patch) | |
| tree | e84e8b6bc0e9951776e7a0a95083ba787d71419d /compiler/rustc_pattern_analysis/src/lib.rs | |
| parent | 11391115ccb9df452af63602a502fc563e3ae389 (diff) | |
| parent | 7b764be9f1dd4cde35c393ceaad891b7c22b2ebb (diff) | |
| download | rust-e111e9925304307b04d1a9c94e7081c29b4582b6.tar.gz rust-e111e9925304307b04d1a9c94e7081c29b4582b6.zip | |
Rollup merge of #126553 - Nadrieril:expand-or-pat-into-above, r=matthewjasper
match lowering: expand or-candidates mixed with candidates above
This PR tweaks match lowering of or-patterns. Consider this:
```rust
match (x, y) {
(1, true) => 1,
(2, false) => 2,
(1 | 2, true | false) => 3,
(3 | 4, true | false) => 4,
_ => 5,
}
```
One might hope that this can be compiled to a single `SwitchInt` on `x` followed by some boolean checks. Before this PR, we compile this to 3 `SwitchInt`s on `x`, because an arm that contains more than one or-pattern was compiled on its own. This PR groups branch `3` with the two branches above, getting us down to 2 `SwitchInt`s on `x`.
We can't in general expand or-patterns freely, because this interacts poorly with another optimization we do: or-pattern simplification. When an or-pattern doesn't involve bindings, we branch the success paths of all its alternatives to the same block. The drawback is that in a case like:
```rust
match (1, true) {
(1 | 2, false) => unreachable!(),
(2, _) => unreachable!(),
_ => {}
}
```
if we used a single `SwitchInt`, by the time we test `false` we don't know whether we came from the `1` case or the `2` case, so we don't know where to go if `false` doesn't match.
Hence the limitation: we can process or-pattern alternatives alongside candidates that precede it, but not candidates that follow it. (Unless the or-pattern is the only remaining match pair of its candidate, in which case we can process it alongside whatever).
This PR allows the processing of or-pattern alternatives alongside candidates that precede it. One benefit is that we now process or-patterns in a single place in `mod.rs`.
r? ``@matthewjasper``
Diffstat (limited to 'compiler/rustc_pattern_analysis/src/lib.rs')
0 files changed, 0 insertions, 0 deletions
