Commit Graph

222 Commits

Author SHA1 Message Date
Nadrieril 5b7be148ea Revert warning empty patterns as unreachable 2024-09-11 18:36:45 +02:00
Matthias Krüger e7504ac704
Rollup merge of #128934 - Nadrieril:fix-empty-non-exhaustive, r=compiler-errors
Non-exhaustive structs may be empty

This is a follow-up to a discrepancy noticed in https://github.com/rust-lang/rust/pull/122792: today, the following struct is considered inhabited (non-empty) outside its defining crate:
```rust
#[non_exhaustive]
pub struct UninhabitedStruct {
    pub never: !,
    // other fields
}
```

`#[non_exhaustive]` on a struct should mean that adding fields to it isn't a breaking change. There is no way that adding fields to this struct could make it non-empty since the `never` field must stay and is inconstructible. I suspect this was implemented this way due to confusion with `#[non_exhaustive]` enums, which indeed should be considered non-empty outside their defining crate.

I propose that we consider such a struct uninhabited (empty), just like it would be without the `#[non_exhaustive]` annotation.

Code that doesn't pass today and will pass after this:
```rust
// In a different crate
fn empty_match_on_empty_struct<T>(x: UninhabitedStruct) -> T {
    match x {}
}
```

This is not a breaking change.

r? ``@compiler-errors``
2024-09-03 19:13:24 +02:00
Nadrieril 6f6a6bc710 Non-exhaustive structs may be empty 2024-09-02 21:16:37 +02:00
Alexander Cyon 00de006f22
chore: Fix typos in 'compiler' (batch 2) 2024-09-02 07:50:22 +02:00
Nicholas Nethercote 653ee7bc3a Add `warn(unreachable_pub)` to `rustc_pattern_analysis`. 2024-08-29 20:18:44 +10:00
Jubilee 5858329f1a
Rollup merge of #128965 - Zalathar:no-pat, r=Nadrieril
Remove `print::Pat` from the printing of `WitnessPat`

After the preliminary work done in #128536, we can now get rid of `print::Pat` entirely.

- First, we introduce a variant `PatKind::Print(String)`.
- Then we incrementally remove each other variant of `PatKind`, by having the relevant code produce `PatKind::Print` instead.
- Once `PatKind::Print` is the only remaining variant, it becomes easy to remove `print::Pat` and replace it with `String`.

There is more cleanup that I have in mind, but this seemed like a natural stopping point for one PR.

r? ```@Nadrieril```
2024-08-15 18:44:16 -07:00
bors e9c965df7b Auto merge of #128812 - nnethercote:shrink-TyKind-FnPtr, r=compiler-errors
Shrink `TyKind::FnPtr`.

By splitting the `FnSig` within `TyKind::FnPtr` into `FnSigTys` and `FnHeader`, which can be packed more efficiently. This reduces the size of the hot `TyKind` type from 32 bytes to 24 bytes on 64-bit platforms. This reduces peak memory usage by a few percent on some benchmarks. It also reduces cache misses and page faults similarly, though this doesn't translate to clear cycles or wall-time improvements on CI.

r? `@compiler-errors`
2024-08-14 00:56:53 +00:00
Zalathar fc55129774 Remove `print::Pat` entirely, replacing it with `String` 2024-08-11 20:25:03 +10:00
Zalathar bfe88a3bd0 Remove `PatKind::Never` 2024-08-11 19:57:30 +10:00
Zalathar ec1483bf2e Remove `PatKind::Slice` 2024-08-11 19:57:30 +10:00
Zalathar 2b6f4386eb Remove `PatKind::Range` 2024-08-11 19:57:30 +10:00
Zalathar 9952e4d4c8 Remove `PatKind::Constant` 2024-08-11 19:57:30 +10:00
Zalathar 283243ac5a Remove `PatKind::Ref` 2024-08-11 19:57:30 +10:00
Zalathar 15cc0e1b5c Remove `PatKind::Box` 2024-08-11 19:57:30 +10:00
Zalathar ed3e38f336 Remove `PatKind::StructLike` 2024-08-11 19:57:30 +10:00
Zalathar 92eb159d04 Remove `PatKind::Wild` 2024-08-11 19:57:30 +10:00
Zalathar f53eb2724d Add `print::PatKind::Print`
This will allow for the gradual removal of all other variants.
2024-08-11 19:57:30 +10:00
Zalathar 0a777090d8 Avoid matching on `PatKind::Wild` in `write_struct_like` 2024-08-11 19:57:30 +10:00
Matthias Krüger 853255e28d
Rollup merge of #128536 - Zalathar:print-cleanup, r=Nadrieril
Preliminary cleanup of `WitnessPat` hoisting/printing

Follow-up to #128430.

The eventual goal is to remove `print::Pat` entirely, but in the course of working towards that I made so many small improvements that it seems wise to let those be reviewed/merged on their own first.

Best reviewed commit-by-commit, most of which should be pretty simple and straightforward.

r? ``@Nadrieril``
2024-08-11 07:51:50 +02:00
Nadrieril cd40769c02 Stabilize `min_exhaustive_patterns` 2024-08-10 12:07:17 +02:00
Nicholas Nethercote c4717cc9d1 Shrink `TyKind::FnPtr`.
By splitting the `FnSig` within `TyKind::FnPtr` into `FnSigTys` and
`FnHeader`, which can be packed more efficiently. This reduces the size
of the hot `TyKind` type from 32 bytes to 24 bytes on 64-bit platforms.
This reduces peak memory usage by a few percent on some benchmarks. It
also reduces cache misses and page faults similarly, though this doesn't
translate to clear cycles or wall-time improvements on CI.
2024-08-09 14:33:25 +10:00
Zalathar 482412c98a Use `TyCtxt::is_diagnostic_item` 2024-08-07 21:44:53 +10:00
Zalathar 29245ec759 Avoid using `ty::tls::with` in `write_struct_like` 2024-08-07 21:44:53 +10:00
Zalathar 582208b0f2 Simplify hoisting of ref patterns (`&` and `&mut`) 2024-08-07 21:44:53 +10:00
Zalathar a245bfa617 Simplify hoisting of array/slice patterns
We can replace some tricky iterator-mutation code with a much simpler version
that uses `while let` to shrink a slice.

We also check whether a subpattern would be a wildcard _before_ hoisting it,
which will be very useful when trying to get rid of `print::PatKind` later.
2024-08-07 21:44:52 +10:00
Zalathar c764bea0c3 Simplify hoisting of struct-like patterns 2024-08-07 20:52:47 +10:00
Zalathar a5ed6fb646 Split out hoisting/printing of `box` patterns 2024-08-07 20:52:47 +10:00
Zalathar 7f48851416 Split out a `hoist` helper in `hoist_witness_pat` 2024-08-07 20:52:46 +10:00
Zalathar e98e19e491 Replace an unnecessary slice pattern with `has_dot_dot: bool` 2024-08-07 20:52:46 +10:00
Zalathar 4cd800503f Remove an impossible case under `EnumInfo::NotEnum` 2024-08-07 20:52:46 +10:00
Zalathar 74f76ae5ea Unify `Variant` and `Leaf` into `print::PatKind::StructLike` 2024-08-07 20:52:46 +10:00
Zalathar ccfd94e334 Break up `print::Pat` printing into several helper functions 2024-08-07 20:52:46 +10:00
Zalathar dd5a8d7714 Use a separate pattern type for `rustc_pattern_analysis` diagnostics
The pattern-analysis code needs to print patterns, as part of its user-visible
diagnostics. But it never actually tries to print "real" patterns! Instead, it
only ever prints synthetic patterns that it has reconstructed from its own
internal represenations.

We can therefore simultaneously remove two obstacles to changing `thir::Pat`,
by having the pattern-analysis code use its own dedicated type for building
printable patterns, and then making `thir::Pat` not printable at all.
2024-07-31 16:03:27 +10:00
Zalathar a2b3256374 Print `thir::PatRange`, not its surrounding `thir::Pat`
This further reduces the amount of code that relies on `thir::Pat` being
printable.
2024-07-31 16:00:52 +10:00
Matthias Krüger d73decdaad
Rollup merge of #128304 - Zalathar:thir-pat-display, r=Nadrieril
Isolate the diagnostic code that expects `thir::Pat` to be printable

Currently, `thir::Pat` implements `fmt::Display` (and `IntoDiagArg`) directly, for use by a few diagnostics.

That makes it tricky to experiment with alternate representations for THIR patterns, because the patterns currently need to be printable on their own. That immediately rules out possibilities like storing subpatterns as a `PatId` index into a central list (instead of the current directly-owned `Box<Pat>`).

This PR therefore takes an incremental step away from that obstacle, by removing `thir::Pat` from diagnostic structs in `rustc_pattern_analysis`, and hiding the pattern-printing process behind a single public `Pat::to_string` method. Doing so makes it easier to identify and update the code that wants to print patterns, and gives a place to pass in additional context in the future if necessary.

---

I'm currently not sure whether switching over to `PatId` is actually desirable or not, but I think this change makes sense on its own merits, by reducing the coupling between `thir::Pat` and the pattern-analysis error types.
2024-07-29 11:42:34 +02:00
Zalathar db05b0fd34 Encapsulate the printing of `WitnessPat`
This hides the fact that we print `WitnessPat` by converting it to `thir::Pat`
and then printing that.
2024-07-29 14:56:50 +10:00
Nicholas Nethercote 84ac80f192 Reformat `use` declarations.
The previous commit updated `rustfmt.toml` appropriately. This commit is
the outcome of running `x fmt --all` with the new formatting options.
2024-07-29 08:26:52 +10:00
Zalathar e1fc4a997d Don't store `thir::Pat` in error structs
In several cases this avoids the need to clone the underlying pattern, and then
print the clone later.
2024-07-28 21:58:44 +10:00
Nadrieril 64ac2b8082 Explain why a given pattern is considered unreachable 2024-07-24 08:02:55 +02:00
Nadrieril c4d6a4a7e4 Add some tests 2024-07-24 08:02:55 +02:00
Nadrieril bab8ede761 Move rustc-specific entrypoint to the `rustc` module 2024-07-24 08:02:55 +02:00
bors 49649bf3c5 Auto merge of #128015 - Nadrieril:two-step-or-expansion, r=compiler-errors
match exhaustiveness: Expand or-patterns as a separate step

To compute exhaustiveness, we must expand or-patterns. Previously, we expanded them at the same time that we pushed patterns into the matrix. This made it harder to track pattern reachability, because the or-pattern itself would never show up in the matrix so we had to recover missing information.

This PR changes that: we no longer expand or-patterns as we push them into the matrix. Instead, if we find an or-pattern in the matrix we expand them in a step very much like the specialization we already do. This simplifies a bunch of things, and should greatly simplify the implementation of https://github.com/rust-lang/rust/issues/127870.

r? `@compiler-errors`
2024-07-23 06:35:42 +00:00
Nadrieril 710add58e2 Tweak `collect_non_exhaustive_tys` 2024-07-21 15:24:27 +02:00
Nadrieril 670723e6fb Expand or-patterns as a separate step 2024-07-20 22:28:54 +02:00
Yuri Astrakhan aef0e346de Avoid ref when using format! in compiler
Clean up a few minor refs in `format!` macro, as it has a performance cost. Apparently the compiler is unable to inline `format!("{}", &variable)`, and does a run-time double-reference instead (format macro already does one level referencing).  Inlining format args prevents accidental `&` misuse.
2024-07-19 14:52:07 -04:00
Ralf Jung 86ce911f90 pattern lowering: make sure we never call user-defined PartialEq instances 2024-07-18 11:58:16 +02:00
Nicholas Nethercote 75b6ec9800 Avoid comments that describe multiple `use` items.
There are some comments describing multiple subsequent `use` items. When
the big `use` reformatting happens some of these `use` items will be
reordered, possibly moving them away from the comment. With this
additional level of formatting it's not really feasible to have comments
of this type. This commit removes them in various ways:

- merging separate `use` items when appropriate;

- inserting blank lines between the comment and the first `use` item;

- outright deletion (for comments that are relatively low-value);

- adding a separate "top-level" comment.

We also entirely skip formatting for four library files that contain
nothing but `pub use` re-exports, where reordering would be painful.
2024-07-17 08:02:46 +10:00
Trevor Gross 6fb6c19c96 Replace `f16` and `f128` pattern matching stubs with real implementations
This section of code depends on `rustc_apfloat` rather than our internal
types, so this is one potential ICE that we should be able to melt now.

This also fixes some missing range and match handling in `rustc_middle`.
2024-06-23 04:28:42 -05:00
Nicholas Nethercote 665821cb60 Add blank lines after module-level `//!` comments.
Most modules have such a blank line, but some don't. Inserting the blank
line makes it clearer that the `//!` comments are describing the entire
module, rather than the `use` declaration(s) that immediately follows.
2024-06-20 09:23:20 +10:00
Nicholas Nethercote 75b164d836 Use `tidy` to sort crate attributes for all compiler crates.
We already do this for a number of crates, e.g. `rustc_middle`,
`rustc_span`, `rustc_metadata`, `rustc_span`, `rustc_errors`.

For the ones we don't, in many cases the attributes are a mess.
- There is no consistency about order of attribute kinds (e.g.
  `allow`/`deny`/`feature`).
- Within attribute kind groups (e.g. the `feature` attributes),
  sometimes the order is alphabetical, and sometimes there is no
  particular order.
- Sometimes the attributes of a particular kind aren't even grouped
  all together, e.g. there might be a `feature`, then an `allow`, then
  another `feature`.

This commit extends the existing sorting to all compiler crates,
increasing consistency. If any new attribute line is added there is now
only one place it can go -- no need for arbitrary decisions.

Exceptions:
- `rustc_log`, `rustc_next_trait_solver` and `rustc_type_ir_macros`,
  because they have no crate attributes.
- `rustc_codegen_gcc`, because it's quasi-external to rustc (e.g. it's
  ignored in `rustfmt.toml`).
2024-06-12 15:49:10 +10:00