about summary refs log tree commit diff
path: root/compiler/rustc_pattern_analysis
AgeCommit message (Collapse)AuthorLines
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-62/+60
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-06Add CoroutineClosure to TyKind, AggregateKind, UpvarArgsMichael Goulet-1/+2
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-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-14/+18
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 #120517 - Nadrieril:lower-never-as-wildcard, r=compiler-errorsMatthias Krüger-2/+3
never patterns: It is correct to lower `!` to `_`. This is just a comment update but a non-trivial one: it is correct to lower `!` patterns as `_`. The reasoning is that `!` matches all the possible values of the type, since the type is empty. Moreover, we do want to warn that the `Err` is redundant in: ```rust match x { !, Err(!), } ``` which is consistent with `!` behaving like a wildcard. I did try to introduce `Constructor::Never` and it ended up needing to behave exactly like `Constructor::Wildcard`. r? ```@compiler-errors```
2024-02-03Rollup merge of #120516 - Nadrieril:cleanup-impls, r=compiler-errorsMatthias Krüger-91/+4
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-02Remove dead args from functionsMichael Goulet-1/+1
2024-01-31Remove `pattern_arena` from `RustcMatchCheckCtxt`Nadrieril-8/+9
2024-01-31Use a `Vec` instead of a slice in `DeconstructedPat`Nadrieril-54/+51
2024-01-31Gracefully abort on type incompatibilityNadrieril-14/+18
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-31It is correct to lower `!` to `_`.Nadrieril-2/+3
2024-01-31Manual `Debug` impls are not needed since `TypeCx: Debug`Nadrieril-49/+4
2024-01-31Remove unused `Constructor: PartialEq` implNadrieril-42/+0
2024-01-30Separate `PlaceCtxt` from `UsefulnessCtxt`Nadrieril-8/+8
2024-01-30Make `PatternColumn` part of the public APINadrieril-90/+101
2024-01-30Repurpose `MatchCtxt` for usefulness onlyNadrieril-23/+21
2024-01-30Limit the use of `PlaceCtxt`Nadrieril-52/+42
2024-01-30Make `PatternColumn` generic in `Cx`Nadrieril-27/+20
2024-01-27Stop using derivative in rustc_pattern_analysisLaurențiu Nicola-25/+191
2024-01-26Rollup merge of #118803 - Nadrieril:min-exhaustive-patterns, r=compiler-errorsMatthias Krüger-10/+24
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-107/+97
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-10/+24
2024-01-24Improve `Range: Debug` implNadrieril-5/+11
2024-01-24Most of the `DeconstructedPat` `Debug` impl is reusableNadrieril-102/+86
2024-01-24Let `ctor_sub_tys` return any Iterator they wantNadrieril-19/+24
Since we always clone and allocate the types somewhere else ourselves, no need to ask for `Cx` to do the allocation.
2024-01-23Rename `TyCtxt::emit_spanned_lint` as `TyCtxt::emit_node_span_lint`.Nicholas Nethercote-2/+2
2024-01-20Remove Ty: Copy boundNadrieril-30/+31
2024-01-19Rollup merge of #119835 - Nadrieril:simplify-empty-logic, r=compiler-errorsMatthias Krüger-50/+28
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-17Rollup merge of #120039 - Nadrieril:remove-idx, r=compiler-errorsMatthias Krüger-6/+48
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-6/+48
2024-01-15Remove the unused `overlapping_range_endpoints` VecNadrieril-39/+5
2024-01-15Lint overlapping ranges directly from exhaustivenessNadrieril-59/+49
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-20/+19
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-34/+19
2024-01-11Only lint ranges that really overlapNadrieril-90/+118
2024-01-11Factor out collection of overlapping rangesNadrieril-31/+62
2024-01-11Track row intersectionsNadrieril-18/+36
2024-01-11Auto merge of #119837 - matthiaskrgr:rollup-l2olpad, r=matthiaskrgrbors-37/+59
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-09Document the new `expand_and_push` methodNadrieril-3/+5
2024-01-09Avoid `PatOrWild` glob importNadrieril-18/+18
2024-01-07Abort analysis on type errorNadrieril-11/+24
2024-01-07Add an error path to the algorithmNadrieril-26/+35
2024-01-07We only need the arity of the subtype list nowNadrieril-18/+13
2024-01-07Use special enum to represent algorithm-generated wildcards in the matrixNadrieril-42/+100