Commit Graph

193 Commits

Author SHA1 Message Date
bors
f067fd6084 Auto merge of #120313 - Nadrieril:graceful-error, r=compiler-errors
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-05 21:36:25 +00:00
Matthias Krüger
ceeaa8a852
Rollup merge of #120517 - Nadrieril:lower-never-as-wildcard, r=compiler-errors
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-03 22:25:15 +01:00
Matthias Krüger
f3ebf1e50f
Rollup merge of #120516 - Nadrieril:cleanup-impls, r=compiler-errors
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-03 22:25:14 +01:00
Michael Goulet
6b2a8249c1 Remove dead args from functions 2024-02-02 22:47:26 +00:00
Nadrieril
f65fe3ba59 Remove pattern_arena from RustcMatchCheckCtxt 2024-01-31 19:25:40 +01:00
Nadrieril
be77cf86ba Use a Vec instead of a slice in DeconstructedPat 2024-01-31 19:25:40 +01:00
Nadrieril
400dc46a05 Gracefully abort on type incompatibility
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-31 19:22:48 +01:00
Nadrieril
ee2cddd8f2 It is correct to lower ! to _. 2024-01-31 01:43:41 +01:00
Nadrieril
40402cbada Manual Debug impls are not needed since TypeCx: Debug 2024-01-31 01:32:05 +01:00
Nadrieril
15b473451c Remove unused Constructor: PartialEq impl 2024-01-31 01:32:05 +01:00
Nadrieril
59031429c5 Separate PlaceCtxt from UsefulnessCtxt 2024-01-30 17:07:06 +01:00
Nadrieril
6ef836246b Make PatternColumn part of the public API 2024-01-30 17:06:52 +01:00
Nadrieril
83e88c6dfc Repurpose MatchCtxt for usefulness only 2024-01-30 17:06:51 +01:00
Nadrieril
cb0e8c508c Limit the use of PlaceCtxt 2024-01-30 17:06:30 +01:00
Nadrieril
0b2579a1b6 Make PatternColumn generic in Cx 2024-01-30 16:57:44 +01:00
Laurențiu Nicola
f5c78955c8 Stop using derivative in rustc_pattern_analysis 2024-01-27 14:21:01 +02:00
Matthias Krüger
a37fa37281
Rollup merge of #118803 - Nadrieril:min-exhaustive-patterns, r=compiler-errors
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-26 06:36:36 +01:00
Matthias Krüger
a1ecced532
Rollup merge of #120318 - Nadrieril:share-debug-impl, r=compiler-errors
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-25 08:39:45 +01:00
Nadrieril
95a14d43d7 Implement feature gate logic 2024-01-25 00:12:32 +01:00
Nadrieril
354b45f528 Improve Range: Debug impl 2024-01-24 20:09:30 +01:00
Nadrieril
bdab213993 Most of the DeconstructedPat Debug impl is reusable 2024-01-24 20:04:33 +01:00
Nadrieril
e088016f9d Let ctor_sub_tys return any Iterator they want
Since we always clone and allocate the types somewhere else ourselves,
no need to ask for `Cx` to do the allocation.
2024-01-24 16:55:26 +01:00
Nicholas Nethercote
e164cf30f8 Rename TyCtxt::emit_spanned_lint as TyCtxt::emit_node_span_lint. 2024-01-23 08:09:05 +11:00
Nadrieril
796cdc590c Remove Ty: Copy bound 2024-01-20 15:22:14 +01:00
Matthias Krüger
2587100a9b
Rollup merge of #119835 - Nadrieril:simplify-empty-logic, r=compiler-errors
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-19 19:27:00 +01:00
Matthias Krüger
3a3242a0f9
Rollup merge of #120039 - Nadrieril:remove-idx, r=compiler-errors
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-17 20:21:23 +01:00
Nadrieril
19d6f068ee Don't rely on contiguous VariantIds outside of rustc 2024-01-17 03:09:06 +01:00
Nadrieril
448c4a4efb Remove the unused overlapping_range_endpoints Vec 2024-01-15 19:27:06 +01:00
Nadrieril
8b66f497eb Lint overlapping ranges directly from exhaustiveness 2024-01-15 19:27:06 +01:00
Nadrieril
d95644d3ae Simplify empty pattern logic some more 2024-01-15 16:56:18 +01:00
Nadrieril
de77f1a86e Simplify empty pattern logic a bit 2024-01-15 16:56:18 +01:00
Nadrieril
edb27a306a Make all the empty pattern decisions in usefulness 2024-01-15 16:56:18 +01:00
Nadrieril
bf913ad0ae Simplify use of ValidityConstraint
We had reached a point where the shenanigans about omitting empty arms
are unnecessary.
2024-01-15 16:52:51 +01:00
Nadrieril
db36304102 rustc_pattern_analysis no longer needs to be passed an arena 2024-01-12 18:55:27 +01:00
Nadrieril
a24f4db41b Only lint ranges that really overlap 2024-01-11 14:04:11 +01:00
Nadrieril
6f6ba2571d Factor out collection of overlapping ranges 2024-01-11 13:59:41 +01:00
Nadrieril
89d01babe6 Track row intersections 2024-01-11 13:56:09 +01:00
bors
65b323b168 Auto merge of #119837 - matthiaskrgr:rollup-l2olpad, r=matthiaskrgr
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-11 02:10:34 +00:00
Nadrieril
4a1889e3fd Document the new expand_and_push method 2024-01-09 16:32:17 +01:00
Nadrieril
5c65e9fdaf Avoid PatOrWild glob import 2024-01-09 16:22:11 +01:00
Nadrieril
4b2e8bc841 Abort analysis on type error 2024-01-07 22:13:08 +01:00
Nadrieril
07d5f19426 Add an error path to the algorithm 2024-01-07 22:13:08 +01:00
Nadrieril
1a3edc169b We only need the arity of the subtype list now 2024-01-07 19:20:19 +01:00
Nadrieril
4ae2840e84 Use special enum to represent algorithm-generated wildcards in the matrix 2024-01-07 19:20:19 +01:00
Nadrieril
30ca1c0a5d Remove incorrect assert
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-07 16:45:44 +01:00
Nadrieril
4c2386137a Factor out pushing onto PatternColumn 2024-01-07 16:45:44 +01:00
Nadrieril
50b197c6ee Reuse ctor_sub_tys when we have one around 2024-01-06 18:03:13 +01:00
Nadrieril
d40f1b1172 Remove Matrix.wildcard_row
It was only used to track types and relevancy, so may as well store that
directly.
2024-01-06 17:56:54 +01:00
bors
5bcd86d89b Auto merge of #119329 - Nadrieril:reveal-opaques-early, r=compiler-errors
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-06 02:00:24 +00:00
Nicholas Nethercote
505c1371d0 Rename some Diagnostic setters.
`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-03 19:40:20 +11:00
Nadrieril
c35272058d Statically enforce revealing of opaques 2024-01-01 23:10:03 +01:00
Michael Goulet
fcb42b42d6 Remove movability from TyKind::Coroutine 2023-12-28 16:35:01 +00:00
Nadrieril
fc0be3c921 Keep reference to the original Pat in DeconstructedPat 2023-12-26 23:14:23 +01:00
Michael Goulet
e1be642b41
Rollup merge of #119307 - compiler-errors:pat-lifetimes, r=Nadrieril
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-26 13:29:14 -05:00
Michael Goulet
48d089a800 Merge 'thir and 'p 2023-12-26 03:15:41 +00:00
bors
2271c26e4a Auto merge of #119146 - nnethercote:rm-DiagCtxt-api-duplication, 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-26 02:24:39 +00:00
Michael Goulet
4ae024c754 Elide more lifetimes 2023-12-26 02:12:33 +00:00
Michael Goulet
ae40f6a7ff Clean up more lifetimes 2023-12-26 02:06:39 +00:00
Michael Goulet
b91a98ba10 Even more 2023-12-26 02:02:01 +00:00
Michael Goulet
eebb2abe0b Yeet some lifetimes 2023-12-26 01:59:18 +00:00
bors
1a086e49f1 Auto merge of #118796 - Nadrieril:fix-exponential-id-match-2, r=cjgillot
Exhaustiveness: Improve complexity on some wide matches

https://github.com/rust-lang/rust/issues/118437 revealed an exponential case in exhaustiveness checking. While [exponential cases are unavoidable](https://compilercrim.es/rust-np/), this one only showed up after my https://github.com/rust-lang/rust/pull/117611 rewrite of the algorithm. I remember anticipating a case like this and dismissing it as unrealistic, but here we are :').

The tricky match is as follows:
```rust
match command {
    BaseCommand { field01: true, .. } => {}
    BaseCommand { field02: true, .. } => {}
    BaseCommand { field03: true, .. } => {}
    BaseCommand { field04: true, .. } => {}
    BaseCommand { field05: true, .. } => {}
    BaseCommand { field06: true, .. } => {}
    BaseCommand { field07: true, .. } => {}
    BaseCommand { field08: true, .. } => {}
    BaseCommand { field09: true, .. } => {}
    BaseCommand { field10: true, .. } => {}
    // ...20 more of the same

    _ => {}
}
```

To fix this, this PR formalizes a concept of "relevancy" (naming is hard) that was already used to decide what patterns to report. Now we track it for every row, which in wide matches like the above can drastically cut on the number of cases we explore. After this fix, the above match is checked with linear-many cases instead of exponentially-many.

Fixes https://github.com/rust-lang/rust/issues/118437

r? `@cjgillot`
2023-12-24 14:40:36 +00:00
Nadrieril
efb04e6572 Rework the explanation of relevancy 2023-12-23 22:43:06 +01:00
Nicholas Nethercote
99472c7049 Remove Session methods that duplicate DiagCtxt methods.
Also add some `dcx` methods to types that wrap `TyCtxt`, for easier
access.
2023-12-24 08:05:28 +11:00
Nadrieril
34307ab7c5 Reveal empty opaques in depth 2023-12-23 14:59:12 +01:00
Nadrieril
71e83347bb Improve performance on wide matches 2023-12-23 13:11:38 +01:00
Nadrieril
5fccaee59c Clarify the situation with dummy patterns and PatData
Use an explicit `Option` instead of requiring a `Default` bound
2023-12-23 00:08:38 +01:00
Nadrieril
c6aa16c469 Use derivative for better derive bounds 2023-12-23 00:04:20 +01:00
Nadrieril
2a87bae48d Reveal opaque types in exhaustiveness checking 2023-12-20 14:43:00 +01:00
bors
3a539c0889 Auto merge of #118842 - Nadrieril:librarify-further, r=compiler-errors
Make exhaustiveness usable outside of rustc

With this PR, `rustc_pattern_analysis` compiles on stable (with the `stable` feature)! `rust-analyzer` will be able to use it to provide match-related diagnostics and refactors.

Two questions:
- Should I name the feature `nightly` instead of `rustc` for consistency with other crates? `rustc` makes more sense imo.
- `typed-arena` is an optional dependency but tidy made me add it to the allow-list anyway. Can I avoid that somehow?

r? `@compiler-errors`
2023-12-19 17:15:04 +00:00
Matthias Krüger
74d81d15b4 NFC: do not clone types that are copy 2023-12-15 23:19:51 +01:00
Nadrieril
3016c29628 s/MatchCx/TypeCx/ 2023-12-15 17:26:19 +01:00
Nadrieril
4bcf66f875 Introduce MatchCtxt 2023-12-15 16:58:38 +01:00
Nadrieril
60ea14bfaa s/PatCtxt/PlaceCtxt/ 2023-12-15 16:58:38 +01:00
Nadrieril
1e89a38423 pattern_analysis doesn't need to know what spans are 2023-12-15 16:58:38 +01:00
Nadrieril
8c5e89907c Address review comments 2023-12-15 16:58:38 +01:00
Nadrieril
e10b165775 s/RustcCtxt/RustcMatchCheckCtxt/ 2023-12-15 16:58:38 +01:00
Nadrieril
63c5b008e1 Make the crate compile on stable 2023-12-15 16:58:38 +01:00
Nadrieril
e646c9f723 Make the rustc_data_structures dependency optional 2023-12-15 16:58:38 +01:00
Nadrieril
16bd6ac3ed Gate rustc-specific code under a feature 2023-12-15 16:58:37 +01:00
Nadrieril
42f4393824 Iron out last rustc-specific details 2023-12-15 16:58:37 +01:00
Nadrieril
cb622f3994 Name rustc-specific things "rustc" 2023-12-15 16:58:37 +01:00
Nadrieril
3d7c4df326 Abstract MatchCheckCtxt into a trait 2023-12-15 16:58:36 +01:00
Nadrieril
3ad76f9325 Disentangle the arena from MatchCheckCtxt 2023-12-15 16:57:36 +01:00
Nadrieril
081c3dcf43 Remove all matching on ty.kind() outside cx 2023-12-15 16:57:36 +01:00
Nadrieril
b111b2e839 Split Single ctor into more specific variants 2023-12-15 16:57:36 +01:00
Matthias Krüger
2a1acc26a0
Update compiler/rustc_pattern_analysis/src/constructor.rs
add note that `missing_empty` is cleared now

Co-authored-by: Nadrieril <Nadrieril@users.noreply.github.com>
2023-12-12 21:12:19 +01:00
Matthias Krüger
6892fcd690 simplify merging of two vecs 2023-12-12 18:42:37 +01:00
Nadrieril
43714edb6f Fix doc links 2023-12-11 12:53:01 +01:00
Nadrieril
5d6c539c2d Fix item visibilities 2023-12-11 11:20:55 +01:00
Nadrieril
de3f983bcd Make MaybeInfiniteInt rustc-independent 2023-12-11 11:20:55 +01:00
Nadrieril
24adca0a26 Move lints to their own module 2023-12-11 11:20:55 +01:00
Nadrieril
3691a0aee5 Gather rustc-specific functions around MatchCheckCtxt 2023-12-11 11:20:55 +01:00
Nadrieril
281002d42c Extract exhaustiveness into its own crate 2023-12-11 11:20:55 +01:00