Commit Graph

101 Commits

Author SHA1 Message Date
Nadrieril
be29cd173a Use a unique id instead of by-address indexing 2024-02-07 23:16:47 +01:00
Nadrieril
8465c82b64 Cleanup comments and dead code 2024-02-07 23:16:47 +01:00
Nadrieril
9715df3f44 Track redundant subpatterns without interior mutability 2024-02-07 23:16:47 +01:00
Nadrieril
cb3ce6645f Move usefulness-specific pattern computations to usefulness 2024-02-07 23:10:51 +01:00
Guillaume Boisseau
3328ee86bb
Rollup merge of #120633 - Nadrieril:place_info, r=compiler-errors
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-07 18:24:44 +01:00
Matthias Krüger
ce32d4862b
Rollup merge of #120331 - Nadrieril:no-arena, r=compiler-errors
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-06 22:45:40 +01:00
Michael Goulet
c567eddec2 Add CoroutineClosure to TyKind, AggregateKind, UpvarArgs 2024-02-06 02:22:58 +00:00
Nadrieril
6cac1c459e Track is_top_level via PlaceInfo 2024-02-06 00:54:39 +01:00
Nadrieril
411967c078 Zip together place_ty and place_validity 2024-02-06 00:54:39 +01:00
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
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