summary refs log tree commit diff
path: root/compiler/rustc_pattern_analysis/src/lib.rs
AgeCommit message (Collapse)AuthorLines
2024-08-29Add `warn(unreachable_pub)` to `rustc_pattern_analysis`.Nicholas Nethercote-0/+1
2024-08-11Rollup merge of #128536 - Zalathar:print-cleanup, r=NadrierilMatthias Krüger-0/+1
Preliminary cleanup of `WitnessPat` hoisting/printing Follow-up to #128430. The eventual goal is to remove `print::Pat` entirely, but in the course of working towards that I made so many small improvements that it seems wise to let those be reviewed/merged on their own first. Best reviewed commit-by-commit, most of which should be pretty simple and straightforward. r? ``@Nadrieril``
2024-08-10Stabilize `min_exhaustive_patterns`Nadrieril-1/+0
2024-08-07Simplify hoisting of array/slice patternsZalathar-0/+1
We can replace some tricky iterator-mutation code with a much simpler version that uses `while let` to shrink a slice. We also check whether a subpattern would be a wildcard _before_ hoisting it, which will be very useful when trying to get rid of `print::PatKind` later.
2024-07-24Move rustc-specific entrypoint to the `rustc` moduleNadrieril-34/+3
2024-07-17Avoid comments that describe multiple `use` items.Nicholas Nethercote-3/+1
There are some comments describing multiple subsequent `use` items. When the big `use` reformatting happens some of these `use` items will be reordered, possibly moving them away from the comment. With this additional level of formatting it's not really feasible to have comments of this type. This commit removes them in various ways: - merging separate `use` items when appropriate; - inserting blank lines between the comment and the first `use` item; - outright deletion (for comments that are relatively low-value); - adding a separate "top-level" comment. We also entirely skip formatting for four library files that contain nothing but `pub use` re-exports, where reordering would be painful.
2024-06-12Use `tidy` to sort crate attributes for all compiler crates.Nicholas Nethercote-1/+3
We already do this for a number of crates, e.g. `rustc_middle`, `rustc_span`, `rustc_metadata`, `rustc_span`, `rustc_errors`. For the ones we don't, in many cases the attributes are a mess. - There is no consistency about order of attribute kinds (e.g. `allow`/`deny`/`feature`). - Within attribute kind groups (e.g. the `feature` attributes), sometimes the order is alphabetical, and sometimes there is no particular order. - Sometimes the attributes of a particular kind aren't even grouped all together, e.g. there might be a `feature`, then an `allow`, then another `feature`. This commit extends the existing sorting to all compiler crates, increasing consistency. If any new attribute line is added there is now only one place it can go -- no need for arbitrary decisions. Exceptions: - `rustc_log`, `rustc_next_trait_solver` and `rustc_type_ir_macros`, because they have no crate attributes. - `rustc_codegen_gcc`, because it's quasi-external to rustc (e.g. it's ignored in `rustfmt.toml`).
2024-04-30Remove `extern crate tracing` from numerous crates.Nicholas Nethercote-3/+0
2024-04-29Remove `extern crate rustc_middle` from numerous crates.Nicholas Nethercote-3/+0
2024-03-30Require enum indices to be contiguousNadrieril-45/+4
2024-03-19Add a crate-custom test harnessNadrieril-0/+6
2024-03-19Improve the `WitnessPat: Debug` implNadrieril-1/+2
2024-03-13Rename `RustcMatchCheckCtxt` -> `RustcPatCtxt`Nadrieril-1/+1
2024-03-13Rename `TypeCx` -> `PatCx`Nadrieril-4/+4
2024-03-13Rename `ValidityConstraint` -> `PlaceValidity`Nadrieril-2/+2
The old name came from a time where I wanted to reuse it for differentiating wildcards from bindings. I don't plan to do this anymore.
2024-03-09Lint small gaps between rangesNadrieril-7/+16
2024-03-03Add new `pattern_complexity` attribute to add possibility to limit and check ↵Guillaume Gomez-1/+6
recursion in pattern matching
2024-02-28Rename `Skip` to `PrivateUninhabited`Nadrieril-3/+4
2024-02-28Don't filter out skipped fieldsNadrieril-2/+1
2024-02-28Push the decision to skip fields further downNadrieril-3/+7
2024-02-09Rollup merge of #120693 - nnethercote:invert-diagnostic-lints, r=davidtwcoMatthias Krüger-0/+3
Invert diagnostic lints. That is, change `diagnostic_outside_of_impl` and `untranslatable_diagnostic` from `allow` to `deny`, because more than half of the compiler has been converted to use translated diagnostics. This commit removes more `deny` attributes than it adds `allow` attributes, which proves that this change is warranted. r? ````@davidtwco````
2024-02-06Rollup merge of #120331 - Nadrieril:no-arena, r=compiler-errorsMatthias Krüger-4/+4
pattern_analysis: use a plain `Vec` in `DeconstructedPat` The use of an arena-allocated slice in `DeconstructedPat` dates to when we needed the arena anyway for lifetime reasons. Now that we don't, I'm thinking that if `thir::Pat` can use plain old `Vec`s, maybe so can I. r? ```@ghost```
2024-02-06Invert diagnostic lints.Nicholas Nethercote-0/+3
That is, change `diagnostic_outside_of_impl` and `untranslatable_diagnostic` from `allow` to `deny`, because more than half of the compiler has be converted to use translated diagnostics. This commit removes more `deny` attributes than it adds `allow` attributes, which proves that this change is warranted.
2024-01-31Use a `Vec` instead of a slice in `DeconstructedPat`Nadrieril-4/+4
2024-01-31Gracefully abort on type incompatibilityNadrieril-1/+1
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-30Make `PatternColumn` part of the public APINadrieril-1/+3
2024-01-30Repurpose `MatchCtxt` for usefulness onlyNadrieril-17/+1
2024-01-30Limit the use of `PlaceCtxt`Nadrieril-1/+1
2024-01-27Stop using derivative in rustc_pattern_analysisLaurențiu Nicola-4/+16
2024-01-26Rollup merge of #118803 - Nadrieril:min-exhaustive-patterns, r=compiler-errorsMatthias Krüger-0/+1
Add the `min_exhaustive_patterns` feature gate ## Motivation Pattern-matching on empty types is tricky around unsafe code. For that reason, current stable rust conservatively requires arms for empty types in all but the simplest case. It has long been the intention to allow omitting empty arms when it's safe to do so. The [`exhaustive_patterns`](https://github.com/rust-lang/rust/issues/51085) feature allows the omission of all empty arms, but hasn't been stabilized because that was deemed dangerous around unsafe code. ## Proposal This feature aims to stabilize an uncontroversial subset of exhaustive_patterns. Namely: when `min_exhaustive_patterns` is enabled and the data we're matching on is guaranteed to be valid by rust's operational semantics, then we allow empty arms to be omitted. E.g.: ```rust let x: Result<T, !> = foo(); match x { // ok Ok(y) => ..., } let Ok(y) = x; // ok ``` If the place is not guaranteed to hold valid data (namely ptr dereferences, ref dereferences (conservatively) and union field accesses), then we keep stable behavior i.e. we (usually) require arms for the empty cases. ```rust unsafe { let ptr: *const Result<u32, !> = ...; match *ptr { Ok(x) => { ... } Err(_) => { ... } // still required } } let foo: Result<u32, &!> = ...; match foo { Ok(x) => { ... } Err(&_) => { ... } // still required because of the dereference } unsafe { let ptr: *const ! = ...; match *ptr {} // already allowed on stable } ``` Note that we conservatively consider that a valid reference can point to invalid data, hence we don't allow arms of type `&!` and similar cases to be omitted. This could eventually change depending on [opsem decisions](https://github.com/rust-lang/unsafe-code-guidelines/issues/413). Whenever opsem is undecided on a case, we conservatively keep today's stable behavior. I proposed this behavior in the [`never_patterns`](https://github.com/rust-lang/rust/issues/118155) feature gate but it makes sense on its own and could be stabilized more quickly. The two proposals nicely complement each other. ## Unresolved Questions Part of the question is whether this requires an RFC. I'd argue this doesn't need one since there is no design question beyond the intent to omit unreachable patterns, but I'm aware the problem can be framed in ways that require design (I'm thinking of the [original never patterns proposal](https://smallcultfollowing.com/babysteps/blog/2018/08/13/never-patterns-exhaustive-matching-and-uninhabited-types-oh-my/), which would frame this behavior as "auto-nevering" happening). EDIT: I initially proposed a future-compatibility lint as part of this feature, I don't anymore.
2024-01-25Rollup merge of #120318 - Nadrieril:share-debug-impl, r=compiler-errorsMatthias Krüger-2/+6
pattern_analysis: Reuse most of the `DeconstructedPat` `Debug` impl The `DeconstructedPat: Debug` is best-effort because we'd need `tcx` to get things like field names etc. Since rust-analyzer has a similar constraint, this PR moves most the impl to be shared between the two. While I was at it I also fixed a nit in the `IntRange: Debug` impl. r? `@compiler-errors`
2024-01-25Implement feature gate logicNadrieril-0/+1
2024-01-24Most of the `DeconstructedPat` `Debug` impl is reusableNadrieril-2/+6
2024-01-24Let `ctor_sub_tys` return any Iterator they wantNadrieril-1/+5
Since we always clone and allocate the types somewhere else ourselves, no need to ask for `Cx` to do the allocation.
2024-01-20Remove Ty: Copy boundNadrieril-4/+4
2024-01-17Rollup merge of #120039 - Nadrieril:remove-idx, r=compiler-errorsMatthias Krüger-2/+40
pat_analysis: Don't rely on contiguous `VariantId`s outside of rustc Today's pattern_analysis uses `BitSet` and `IndexVec` on the provided enum variant ids, which only makes sense if these ids count the variants from 0. In rust-analyzer, the variant ids are global interning ids, which would make `BitSet` and `IndexVec` ridiculously wasteful. In this PR I add some shims to use `FxHashSet`/`FxHashMap` instead outside of rustc. r? ```@compiler-errors```
2024-01-17Don't rely on contiguous `VariantId`s outside of rustcNadrieril-2/+40
2024-01-15Lint overlapping ranges directly from exhaustivenessNadrieril-11/+14
2024-01-12rustc_pattern_analysis no longer needs to be passed an arenaNadrieril-13/+2
2024-01-11Only lint ranges that really overlapNadrieril-1/+3
2024-01-11Factor out collection of overlapping rangesNadrieril-1/+1
2024-01-07Abort analysis on type errorNadrieril-1/+1
2024-01-07Add an error path to the algorithmNadrieril-5/+9
2024-01-01Statically enforce revealing of opaquesNadrieril-2/+1
2023-12-26Yeet some lifetimesMichael Goulet-1/+1
2023-12-23Clarify the situation with dummy patterns and `PatData`Nadrieril-3/+2
Use an explicit `Option` instead of requiring a `Default` bound
2023-12-23Use `derivative` for better derive boundsNadrieril-7/+6
2023-12-20Reveal opaque types in exhaustiveness checkingNadrieril-1/+2
2023-12-15s/MatchCx/TypeCx/Nadrieril-7/+7
2023-12-15Introduce `MatchCtxt`Nadrieril-12/+34