about summary refs log tree commit diff
path: root/compiler/rustc_pattern_analysis/src/usefulness.rs
AgeCommit message (Collapse)AuthorLines
2024-05-02Stabilize exclusive_rangeRoss Smyth-1/+0
2024-04-30Remove `extern crate tracing` from numerous crates.Nicholas Nethercote-6/+5
2024-04-01Fix union handling in exhaustivenessNadrieril-6/+29
2024-03-31Improve debugging experienceNadrieril-4/+10
2024-03-19Report arm intersectionsNadrieril-4/+21
2024-03-13Rename `TypeCx` -> `PatCx`Nadrieril-37/+37
2024-03-13Rename `ValidityConstraint` -> `PlaceValidity`Nadrieril-18/+10
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-11Store field indices in `DeconstructedPat` to avoid virtual wildcardsNadrieril-7/+10
2024-03-09Lint small gaps between rangesNadrieril-4/+42
2024-03-09Make `MaybeInfiniteInt::plus_one/minus_one` fallibleNadrieril-1/+1
2024-03-04Abort on arity mismatchNadrieril-7/+15
As this can cause panics on array accesses later.
2024-03-03Add new `pattern_complexity` attribute to add possibility to limit and check ↵Guillaume Gomez-1/+23
recursion in pattern matching
2024-02-28Rename `Skip` to `PrivateUninhabited`Nadrieril-9/+9
2024-02-28Don't filter out skipped fieldsNadrieril-5/+22
2024-02-28Push the decision to skip fields further downNadrieril-2/+4
2024-02-25Rollup merge of #121324 - Nadrieril:unspecialize, r=cjgillotMatthias Krüger-15/+21
pattern_analysis: factor out unspecialization Just moving a dense bit of logic into its own method.
2024-02-20Auto merge of #120692 - Nadrieril:move-column-analysis-to-placeinfo, ↵bors-74/+90
r=compiler-errors pattern_analysis: Move constructor selection logic to `PlaceInfo` This is a small refactor PR. There was a dense bit of constructor-related logic in `compute_exhaustiveness_and_usefulness`. I'm moving it out into a `PlaceInfo` method to make it easier to follow both separately. I also have plans that will complicate it further so it's good that it's somewhat encapsulated. r? `@compiler-errors`
2024-02-20Factor out unspecializationNadrieril-15/+21
2024-02-12Auto merge of #120324 - Nadrieril:remove-interior-mutability, r=compiler-errorsbors-30/+61
pattern_analysis: track usefulness without interior mutability Because of or-patterns, exhaustiveness needs to be able to lint if a sub-pattern is redundant, e.g. in `Some(_) | Some(true)`. So far the only sane solution I had found was interior mutability. This is a bit of an abstraction leak, and would become a footgun if we ever reused the same `DeconstructedPat`. This PR replaces interior mutability with an address-indexed hashmap, which is logically equivalent.
2024-02-08Move constructor selection logic to `PlaceInfo`Nadrieril-56/+79
2024-02-08Decide which constructors to report earlier.Nadrieril-19/+17
This gets rid of `report_individual_missing_ctors`
2024-02-08Tweak how we record missing constructorsNadrieril-21/+16
This is slower but also not a performance-sensitive path.
2024-02-07Prefer "0..MAX not covered" to "_ not covered"Nadrieril-5/+3
2024-02-07Use a unique id instead of by-address indexingNadrieril-34/+10
2024-02-07Cleanup comments and dead codeNadrieril-9/+5
2024-02-07Track redundant subpatterns without interior mutabilityNadrieril-21/+55
2024-02-07Move usefulness-specific pattern computations to `usefulness`Nadrieril-6/+31
2024-02-07Rollup merge of #120633 - Nadrieril:place_info, r=compiler-errorsGuillaume Boisseau-40/+57
pattern_analysis: gather up place-relevant info We track 3 things about each place during exhaustiveness: its type, its (data) validity, and whether it's the scrutinee place. This PR gathers all three into a single struct. r? `````@compiler-errors`````
2024-02-06Rollup merge of #120331 - Nadrieril:no-arena, r=compiler-errorsMatthias Krüger-2/+2
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-06Track `is_top_level` via `PlaceInfo`Nadrieril-10/+14
2024-02-06Zip together `place_ty` and `place_validity`Nadrieril-33/+46
2024-02-05Auto merge of #120313 - Nadrieril:graceful-error, r=compiler-errorsbors-4/+4
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-03Rollup merge of #120516 - Nadrieril:cleanup-impls, r=compiler-errorsMatthias Krüger-12/+2
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-01-31Use a `Vec` instead of a slice in `DeconstructedPat`Nadrieril-2/+2
2024-01-31Gracefully abort on type incompatibilityNadrieril-4/+4
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-31Manual `Debug` impls are not needed since `TypeCx: Debug`Nadrieril-12/+2
2024-01-30Separate `PlaceCtxt` from `UsefulnessCtxt`Nadrieril-8/+8
2024-01-30Repurpose `MatchCtxt` for usefulness onlyNadrieril-6/+20
2024-01-30Limit the use of `PlaceCtxt`Nadrieril-19/+15
2024-01-27Stop using derivative in rustc_pattern_analysisLaurențiu Nicola-12/+46
2024-01-26Rollup merge of #118803 - Nadrieril:min-exhaustive-patterns, r=compiler-errorsMatthias Krüger-9/+17
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-25Implement feature gate logicNadrieril-9/+17
2024-01-24Let `ctor_sub_tys` return any Iterator they wantNadrieril-3/+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-8/+9
2024-01-19Rollup merge of #119835 - Nadrieril:simplify-empty-logic, r=compiler-errorsMatthias Krüger-35/+22
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-15Remove the unused `overlapping_range_endpoints` VecNadrieril-37/+5
2024-01-15Lint overlapping ranges directly from exhaustivenessNadrieril-11/+5
2024-01-15Simplify empty pattern logic some moreNadrieril-8/+7
2024-01-15Simplify empty pattern logic a bitNadrieril-14/+13
2024-01-15Make all the empty pattern decisions in `usefulness`Nadrieril-5/+13