summary refs log tree commit diff
path: root/compiler/rustc_pattern_analysis/src/lib.rs
AgeCommit message (Collapse)AuthorLines
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
2023-12-15`pattern_analysis` doesn't need to know what spans areNadrieril-1/+4
2023-12-15Address review commentsNadrieril-4/+0
2023-12-15s/RustcCtxt/RustcMatchCheckCtxt/Nadrieril-2/+2
2023-12-15Make the `rustc_data_structures` dependency optionalNadrieril-0/+7
2023-12-15Gate rustc-specific code under a featureNadrieril-5/+15
2023-12-15Iron out last rustc-specific detailsNadrieril-1/+5
2023-12-15Name rustc-specific things "rustc"Nadrieril-6/+6
2023-12-15Abstract `MatchCheckCtxt` into a traitNadrieril-7/+42
2023-12-15Disentangle the arena from `MatchCheckCtxt`Nadrieril-3/+5
2023-12-11Move lints to their own moduleNadrieril-0/+42
2023-12-11Gather rustc-specific functions around `MatchCheckCtxt`Nadrieril-0/+1
2023-12-11Extract exhaustiveness into its own crateNadrieril-0/+13