Commit Graph

61 Commits

Author SHA1 Message Date
Chayim Refael Friedman 6bf3cbe39e In rustc_pattern_analysis, put true witnesses before false witnesses
In rustc it doesn't really matter what the order of the witnesses is, but I'm planning to use the witnesses for implementing the "add missing match arms" assist in rust-analyzer, and there `true` before `false` is the natural order (like `Some` before `None`), and also what the current assist does.

The current order doesn't seem to be intentional; the code was created when bool ctors became their own thing, not just int ctors, but for integer, 0 before 1 is indeed the natural order.
2025-07-28 02:01:39 +03:00
Nadrieril 2bb00741d4 pattern_analysis: add option to get a full set of witnesses 2025-07-20 18:27:30 +02:00
bjorn3 ba5556d239 Add #[loop_match] for improved DFA codegen
Co-authored-by: Folkert de Vries <folkert@folkertdev.nl>
2025-06-23 20:43:04 +02:00
dianne b41d8bde00 let deref patterns participate in usefulness/exhaustiveness
This does not yet handle the case of mixed deref patterns with normal
constructors; it'll ICE in `Constructor::is_covered_by`. That'll be
fixed in a later commit.
2025-05-06 18:53:55 -07:00
Rémy Rakic a13354bea0 rename BitSet to DenseBitSet
This should make it clearer that this bitset is dense, with the
advantages and disadvantages that it entails.
2025-01-11 11:34:01 +00:00
Matthias Krüger 71cd918dc7 cleanup: don't clone types that are Copy 2024-09-29 13:31:30 +02:00
Michael Goulet c682aa162b Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
Alexander Cyon 00de006f22 chore: Fix typos in 'compiler' (batch 2) 2024-09-02 07:50:22 +02:00
Nicholas Nethercote 84ac80f192 Reformat use declarations.
The previous commit updated `rustfmt.toml` appropriately. This commit is
the outcome of running `x fmt --all` with the new formatting options.
2024-07-29 08:26:52 +10:00
Yuri Astrakhan aef0e346de Avoid ref when using format! in compiler
Clean up a few minor refs in `format!` macro, as it has a performance cost. Apparently the compiler is unable to inline `format!("{}", &variable)`, and does a run-time double-reference instead (format macro already does one level referencing).  Inlining format args prevents accidental `&` misuse.
2024-07-19 14:52:07 -04:00
Trevor Gross 6fb6c19c96 Replace f16 and f128 pattern matching stubs with real implementations
This section of code depends on `rustc_apfloat` rather than our internal
types, so this is one potential ICE that we should be able to melt now.

This also fixes some missing range and match handling in `rustc_middle`.
2024-06-23 04:28:42 -05:00
Nadrieril 27704c7f9e Fix union handling in exhaustiveness 2024-04-01 00:01:46 +02:00
Nadrieril 8cf2c0dc67 Improve debugging experience 2024-03-31 23:12:20 +02:00
Nadrieril e1b8441899 Require enum indices to be contiguous 2024-03-30 16:22:43 +01:00
Nadrieril e4487ad391 Improve the WitnessPat: Debug impl 2024-03-19 02:22:43 +01:00
Matthias Krüger 2d3dcfaade Rollup merge of #121823 - Nadrieril:never-witnesses, r=compiler-errors
never patterns: suggest `!` patterns on non-exhaustive matches

When a match is non-exhaustive we now suggest never patterns whenever it makes sense.

r? ``@compiler-errors``
2024-03-18 22:24:36 +01:00
Matthias Krüger 5c2aa6dc6f Rollup merge of #122437 - Nadrieril:no-after-max, r=compiler-errors
pattern analysis: remove `MaybeInfiniteInt::JustAfterMax`

It was inherited from before half-open ranges, but it doesn't pull its weight anymore. We lose a tiny bit of diagnostic precision as can be seen in the test. I'm generally in favor of half-open ranges over explicit `x..=MAX` ranges anyway.
2024-03-13 20:01:57 +01:00
Nadrieril c4236785c7 Remove MaybeInfiniteInt::JustAfterMax
It was inherited from before half-open ranges, but it doesn't pull its
weight anymore. We lose a tiny bit of diagnostic precision.
2024-03-13 14:17:11 +01:00
Nadrieril 4fc35c46ff Rename TypeCx -> PatCx 2024-03-13 13:56:38 +01:00
Nadrieril 9f2aa5b85a Suggest never pattern instead of _ for empty types 2024-03-12 21:38:30 +01:00
Nadrieril 1ec73d70fa Add Constructor::Never 2024-03-12 21:38:30 +01:00
Nadrieril c1e68860d0 Store pattern arity in DeconstructedPat
Right now this is just `self.fields.len()` but that'll change in the
next commit. `arity` will be useful for the `Debug` impl.
2024-03-11 04:37:21 +01:00
Nadrieril a047284b5a Make MaybeInfiniteInt::plus_one/minus_one fallible 2024-03-09 01:13:42 +01:00
Guillaume Gomez 9df7f26b1b Rollup merge of #121000 - Nadrieril:keep_all_fields, r=compiler-errors
pattern_analysis: rework how we hide empty private fields

Consider this:
```rust
mod foo {
  pub struct Bar {
    pub a: bool,
    b: !,
  }
}

fn match_a_bar(bar: foo::Bar) -> bool {
  match bar {
    Bar { a, .. } => a,
  }
}
```

Because the field `b` is private, matches outside the module are not allowed to observe the fact that `Bar` is empty. In particular `match bar {}` is valid within the module `foo` but an error outside (assuming `exhaustive_patterns`).

We currently handle this by hiding the field `b` when it's both private and empty. This means that the pattern `Bar { a, .. }` is lowered to `Bar(a, _)` if we're inside of `foo` and to `Bar(a)` outside. This involves a bit of a dance to keep field indices straight. But most importantly this makes pattern lowering depend on the module.

In this PR, I instead do nothing special when lowering. Only during analysis do we track whether a place must be skipped.

r? `@compiler-errors`
2024-02-29 17:08:37 +01:00
Nadrieril c918893b63 Rename Skip to PrivateUninhabited 2024-02-28 17:56:01 +01:00
Nadrieril 4f7f06777b Add special Skip constructor 2024-02-28 17:47:19 +01:00
Nadrieril 12b991d9f8 Don't panic when encountering unexpected constructor 2024-02-28 13:28:27 +01:00
Matthias Krüger 86a7fc840f compiler: clippy::complexity fixes 2024-02-23 19:56:35 +01:00
bors f067fd6084 Auto merge of #120313 - Nadrieril:graceful-error, r=compiler-errors
pattern_analysis: Gracefully abort on type incompatibility

This leaves the option for a consumer of the crate to return `Err` instead of panicking on type error. rust-analyzer could use that (e.g. https://github.com/rust-lang/rust-analyzer/issues/15808).

Since the only use of `TypeCx::bug` is in `Constructor::is_covered_by`, it is tempting to return `false` instead of `Err()`, but that would cause "non-exhaustive match" false positives.

r? `@compiler-errors`
2024-02-05 21:36:25 +00:00
Matthias Krüger f3ebf1e50f Rollup merge of #120516 - Nadrieril:cleanup-impls, r=compiler-errors
pattern_analysis: cleanup manual impls

https://github.com/rust-lang/rust/pull/120420 introduced some unneeded manual impls. I remove them here.

r? ```@Nilstrieb```
2024-02-03 22:25:14 +01:00
Nadrieril 400dc46a05 Gracefully abort on type incompatibility
Since the only use of `TypeCx::bug` is in `Constructor::is_covered_by`,
it is tempting to return `false` instead of `Err()`, but that would
cause "non-exhaustive match" false positives.
2024-01-31 19:22:48 +01:00
Nadrieril 40402cbada Manual Debug impls are not needed since TypeCx: Debug 2024-01-31 01:32:05 +01:00
Nadrieril 15b473451c Remove unused Constructor: PartialEq impl 2024-01-31 01:32:05 +01:00
Nadrieril 6ef836246b Make PatternColumn part of the public API 2024-01-30 17:06:52 +01:00
Nadrieril cb0e8c508c Limit the use of PlaceCtxt 2024-01-30 17:06:30 +01:00
Laurențiu Nicola f5c78955c8 Stop using derivative in rustc_pattern_analysis 2024-01-27 14:21:01 +02:00
Nadrieril 354b45f528 Improve Range: Debug impl 2024-01-24 20:09:30 +01:00
Matthias Krüger 2587100a9b Rollup merge of #119835 - Nadrieril:simplify-empty-logic, r=compiler-errors
Exhaustiveness: simplify empty pattern logic

The logic that handles empty patterns had gotten quite convoluted. This PR simplifies it a lot. I tried to make the logic as easy as possible to follow; this only does logically equivalent changes.

The first commit is a drive-by comment clarification that was requested after another PR a while back.

r? `@compiler-errors`
2024-01-19 19:27:00 +01:00
Nadrieril 19d6f068ee Don't rely on contiguous VariantIds outside of rustc 2024-01-17 03:09:06 +01:00
Nadrieril edb27a306a Make all the empty pattern decisions in usefulness 2024-01-15 16:56:18 +01:00
Nadrieril db36304102 rustc_pattern_analysis no longer needs to be passed an arena 2024-01-12 18:55:27 +01:00
Michael Goulet ae40f6a7ff Clean up more lifetimes 2023-12-26 02:06:39 +00:00
Nadrieril c6aa16c469 Use derivative for better derive bounds 2023-12-23 00:04:20 +01:00
bors 3a539c0889 Auto merge of #118842 - Nadrieril:librarify-further, r=compiler-errors
Make exhaustiveness usable outside of rustc

With this PR, `rustc_pattern_analysis` compiles on stable (with the `stable` feature)! `rust-analyzer` will be able to use it to provide match-related diagnostics and refactors.

Two questions:
- Should I name the feature `nightly` instead of `rustc` for consistency with other crates? `rustc` makes more sense imo.
- `typed-arena` is an optional dependency but tidy made me add it to the allow-list anyway. Can I avoid that somehow?

r? `@compiler-errors`
2023-12-19 17:15:04 +00:00
Matthias Krüger 74d81d15b4 NFC: do not clone types that are copy 2023-12-15 23:19:51 +01:00
Nadrieril 3016c29628 s/MatchCx/TypeCx/ 2023-12-15 17:26:19 +01:00
Nadrieril 4bcf66f875 Introduce MatchCtxt 2023-12-15 16:58:38 +01:00
Nadrieril 60ea14bfaa s/PatCtxt/PlaceCtxt/ 2023-12-15 16:58:38 +01:00
Nadrieril 8c5e89907c Address review comments 2023-12-15 16:58:38 +01:00
Nadrieril e646c9f723 Make the rustc_data_structures dependency optional 2023-12-15 16:58:38 +01:00