summary refs log tree commit diff
path: root/compiler/rustc_pattern_analysis/src
AgeCommit message (Collapse)AuthorLines
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-24/+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/+47
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/+47
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-28/+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
2024-01-07Remove incorrect `assert`Nadrieril-6/+0
It's incorrect because `CtorSet::split` returns a non-present constructor into `present` in one specific case: variable-length slices of an empty type. That's because empty constructors of arity 0 break the algorithm. This is a tricky corner case that's hard to do cleanly. The assert wasn't adding much anyway.
2024-01-07Factor out pushing onto `PatternColumn`Nadrieril-12/+12
2024-01-06Reuse `ctor_sub_tys` when we have one aroundNadrieril-6/+11
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-06Auto merge of #119329 - Nadrieril:reveal-opaques-early, r=compiler-errorsbors-90/+128
Exhaustiveness: Statically enforce revealing of opaques In https://github.com/rust-lang/rust/pull/116821 it was decided that exhaustiveness should operate on the hidden type of an opaque type when relevant. This PR makes sure we consistently reveal opaques within exhaustiveness. This makes it possible to remove `reveal_opaque_ty` from the `TypeCx` trait which was an unfortunate implementation detail. r? `@compiler-errors`
2024-01-03Rename some `Diagnostic` setters.Nicholas Nethercote-1/+1
`Diagnostic` has 40 methods that return `&mut Self` and could be considered setters. Four of them have a `set_` prefix. This doesn't seem necessary for a type that implements the builder pattern. This commit removes the `set_` prefixes on those four methods.
2024-01-01Statically enforce revealing of opaquesNadrieril-90/+128
2023-12-28Remove movability from TyKind::CoroutineMichael Goulet-1/+1
2023-12-26Keep reference to the original `Pat` in `DeconstructedPat`Nadrieril-5/+5
2023-12-26Rollup merge of #119307 - compiler-errors:pat-lifetimes, r=NadrierilMichael Goulet-69/+61
Clean up some lifetimes in `rustc_pattern_analysis` This PR removes some redundant lifetimes. I figured out that we were shortening the lifetime of an arena-allocated `&'p DeconstructedPat<'p>` to `'a DeconstructedPat<'p>`, which forced us to carry both lifetimes when we could otherwise carry just one. This PR also removes and elides some unnecessary lifetimes. I also cherry-picked 0292eb9bb9b897f5c0926c6a8530877f67e7cc9b, and then simplified more lifetimes in `MatchVisitor`, which should make #119233 a very simple PR! r? Nadrieril
2023-12-26Merge 'thir and 'pMichael Goulet-1/+1
2023-12-26Auto merge of #119146 - nnethercote:rm-DiagCtxt-api-duplication, ↵bors-1/+1
r=compiler-errors Remove `DiagCtxt` API duplication `DiagCtxt` defines the internal API for creating and emitting diagnostics: methods like `struct_err`, `struct_span_warn`, `note`, `create_fatal`, `emit_bug`. There are over 50 methods. Some of these methods are then duplicated across several other types: `Session`, `ParseSess`, `Parser`, `ExtCtxt`, and `MirBorrowckCtxt`. `Session` duplicates the most, though half the ones it does are unused. Each duplicated method just calls forward to the corresponding method in `DiagCtxt`. So this duplication exists to (in the best case) shorten chains like `ecx.tcx.sess.parse_sess.dcx.emit_err()` to `ecx.emit_err()`. This API duplication is ugly and has been bugging me for a while. And it's inconsistent: there's no real logic about which methods are duplicated, and the use of `#[rustc_lint_diagnostic]` and `#[track_caller]` attributes vary across the duplicates. This PR removes the duplicated API methods and makes all diagnostic creation and emission go through `DiagCtxt`. It also adds `dcx` getter methods to several types to shorten chains. This approach scales *much* better than API duplication; indeed, the PR adds `dcx()` to numerous types that didn't have API duplication: `TyCtxt`, `LoweringCtxt`, `ConstCx`, `FnCtxt`, `TypeErrCtxt`, `InferCtxt`, `CrateLoader`, `CheckAttrVisitor`, and `Resolver`. These result in a lot of changes from `foo.tcx.sess.emit_err()` to `foo.dcx().emit_err()`. (You could do this with more types, but it gets into diminishing returns territory for types that don't emit many diagnostics.) After all these changes, some call sites are more verbose, some are less verbose, and many are the same. The total number of lines is reduced, mostly because of the removed API duplication. And consistency is increased, because calls to `emit_err` and friends are always preceded with `.dcx()` or `.dcx`. r? `@compiler-errors`
2023-12-26Elide more lifetimesMichael Goulet-12/+12
2023-12-26Clean up more lifetimesMichael Goulet-15/+10
2023-12-26Even moreMichael Goulet-13/+10
2023-12-26Yeet some lifetimesMichael Goulet-40/+40