about summary refs log tree commit diff
path: root/compiler/rustc_pattern_analysis/src/usefulness.rs
AgeCommit message (Collapse)AuthorLines
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
2024-01-15Simplify use of `ValidityConstraint`Nadrieril-23/+4
We had reached a point where the shenanigans about omitting empty arms are unnecessary.
2024-01-12rustc_pattern_analysis no longer needs to be passed an arenaNadrieril-9/+9
2024-01-11Only lint ranges that really overlapNadrieril-4/+113
2024-01-11Factor out collection of overlapping rangesNadrieril-1/+10
2024-01-11Track row intersectionsNadrieril-18/+36
2024-01-11Auto merge of #119837 - matthiaskrgr:rollup-l2olpad, r=matthiaskrgrbors-12/+13
Rollup of 11 pull requests Successful merges: - #115046 (Use version-sorting for all sorting) - #118915 (Add some comments, add `can_define_opaque_ty` check to `try_normalize_ty_recur`) - #119006 (Fix is_global special address handling) - #119637 (Pass LLVM error message back to pass wrapper.) - #119715 (Exhaustiveness: abort on type error) - #119763 (Cleanup things in and around `Diagnostic`) - #119788 (change function name in comments) - #119790 (Fix all_trait* methods to return all traits available in StableMIR) - #119803 (Silence some follow-up errors [1/x]) - #119804 (Stabilize mutex_unpoison feature) - #119832 (Meta: Add project const traits to triagebot config) r? `@ghost` `@rustbot` modify labels: rollup
2024-01-07Abort analysis on type errorNadrieril-2/+2
2024-01-07Add an error path to the algorithmNadrieril-10/+11
2024-01-07We only need the arity of the subtype list nowNadrieril-9/+7
2024-01-07Use special enum to represent algorithm-generated wildcards in the matrixNadrieril-12/+9
2024-01-06Reuse `ctor_sub_tys` when we have one aroundNadrieril-3/+6
2024-01-06Remove `Matrix.wildcard_row`Nadrieril-18/+24
It was only used to track types and relevancy, so may as well store that directly.
2024-01-01Statically enforce revealing of opaquesNadrieril-10/+7
2023-12-26Elide more lifetimesMichael Goulet-11/+11
2023-12-26Even moreMichael Goulet-12/+9
2023-12-26Yeet some lifetimesMichael Goulet-28/+28
2023-12-23Rework the explanation of relevancyNadrieril-48/+138
2023-12-23Improve performance on wide matchesNadrieril-29/+115
2023-12-23Clarify the situation with dummy patterns and `PatData`Nadrieril-2/+1
Use an explicit `Option` instead of requiring a `Default` bound
2023-12-23Use `derivative` for better derive boundsNadrieril-12/+9
2023-12-20Reveal opaque types in exhaustiveness checkingNadrieril-15/+5
2023-12-15s/MatchCx/TypeCx/Nadrieril-24/+24
2023-12-15Introduce `MatchCtxt`Nadrieril-29/+23
2023-12-15s/PatCtxt/PlaceCtxt/Nadrieril-18/+18
2023-12-15`pattern_analysis` doesn't need to know what spans areNadrieril-5/+5
2023-12-15Make the crate compile on stableNadrieril-0/+5
2023-12-15Make the `rustc_data_structures` dependency optionalNadrieril-2/+8
2023-12-15Gate rustc-specific code under a featureNadrieril-3/+4
2023-12-15Abstract `MatchCheckCtxt` into a traitNadrieril-90/+87
2023-12-15Disentangle the arena from `MatchCheckCtxt`Nadrieril-57/+71
2023-12-15Remove all matching on `ty.kind()` outside `cx`Nadrieril-4/+3
2023-12-15Split `Single` ctor into more specific variantsNadrieril-6/+3
2023-12-11Fix doc linksNadrieril-5/+6
2023-12-11Fix item visibilitiesNadrieril-11/+7
2023-12-11Move lints to their own moduleNadrieril-297/+10