Commit Graph

193 Commits

Author SHA1 Message Date
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
Ralf Jung
3c57ea0df7 ScalarInt: size mismatches are a bug, do not delay the panic 2024-06-10 13:43:16 +02:00
Boxy
60a5bebbe5 Add Ty to mir::Const::Ty 2024-06-05 22:25:41 +01:00
lcnr
97d2c3a6a7 remove tracing tree indent lines 2024-05-30 15:26:48 +02:00
León Orell Valerian Liehr
06bc4fc671
Remove LintDiagnostic::msg
* instead simply set the primary message inside the lint decorator functions
* it used to be this way before [#]101986 which introduced `msg` to prevent
  good path delayed bugs (which no longer exist) from firing under certain
  circumstances when lints were suppressed / silenced
* this is no longer necessary for various reasons I presume
* it shaves off complexity and makes further changes easier to implement
2024-05-23 04:08:35 +02:00
Ross Smyth
6967d1c0fc Stabilize exclusive_range 2024-05-02 19:42:31 -04:00
Nicholas Nethercote
6341935a13 Remove extern crate tracing from numerous crates. 2024-04-30 16:47:49 +10:00
Nicholas Nethercote
99e036bd21 Remove extern crate rustc_middle from numerous crates. 2024-04-29 14:50:45 +10:00
Xiretza
5646b65cf5 Pass translation closure to add_to_diag_with() as reference 2024-04-21 07:45:03 +00:00
Oli Scherer
84acfe86de Actually create ranged int types in the type system. 2024-04-08 12:02:19 +00:00
Nadrieril
27704c7f9e Fix union handling in exhaustiveness 2024-04-01 00:01:46 +02:00
Nadrieril
8cf2c0dc67 Improve debugging experience 2024-03-31 23:12:20 +02:00
Matthias Krüger
f04d068adc
Rollup merge of #123242 - Nadrieril:require-contiguous-enum-indices, r=compiler-errors
pattern analysis: Require enum indices to be contiguous

We had a cfg-hack to allow rust-analyzer to use non-contiguous indices for its enum variants. Unfortunately this no longer works if r-a uses the in-tree version of the crate.

This PR removes the hack, and on the r-a side we'll have to use contiguous indices but that's not too hard.

r? `@compiler-errors`
2024-03-31 11:50:41 +02:00
Nadrieril
e1b8441899 Require enum indices to be contiguous 2024-03-30 16:22:43 +01:00
klensy
71ea506d3d bump tracing-tree to 0.3
Only change is https://github.com/davidbarsky/tracing-tree/pull/76
dedupes tracing-log
dupes nu-ansi-term
2024-03-30 17:39:43 +03:00
Michael Goulet
ff0c31e6b9 Programmatically convert some of the pat ctors 2024-03-22 11:13:29 -04:00
Matthias Krüger
8d12621181
Rollup merge of #122644 - Nadrieril:complexity-tests, r=compiler-errors
pattern analysis: add a custom test harness

There are two features of the pattern analysis code that are hard to test: the newly-added pattern complexity limit, and the computation of arm intersections. This PR adds some crate-specific tests for that, including an unmaintainable but pretty macro to help construct patterns.

r? `````@compiler-errors`````
2024-03-21 17:46:48 +01:00
Nadrieril
120d3570aa Add barest-bones deref patterns
Co-authored-by: Deadbeef <ent3rm4n@gmail.com>
2024-03-20 22:30:27 +01:00
Nadrieril
d697dd44d1 Add a crate-custom test harness 2024-03-19 02:22:43 +01:00
Nadrieril
e4487ad391 Improve the WitnessPat: Debug impl 2024-03-19 02:22:43 +01:00
Nadrieril
3dfd0fd858 Report arm intersections 2024-03-19 02:20:36 +01:00
Matthias Krüger
2d3dcfaade
Rollup merge of #121823 - Nadrieril:never-witnesses, r=compiler-errors
never patterns: suggest `!` patterns on non-exhaustive matches

When a match is non-exhaustive we now suggest never patterns whenever it makes sense.

r? ``@compiler-errors``
2024-03-18 22:24:36 +01:00
Matthias Krüger
5c2aa6dc6f
Rollup merge of #122437 - Nadrieril:no-after-max, r=compiler-errors
pattern analysis: remove `MaybeInfiniteInt::JustAfterMax`

It was inherited from before half-open ranges, but it doesn't pull its weight anymore. We lose a tiny bit of diagnostic precision as can be seen in the test. I'm generally in favor of half-open ranges over explicit `x..=MAX` ranges anyway.
2024-03-13 20:01:57 +01:00
Nadrieril
c4236785c7 Remove MaybeInfiniteInt::JustAfterMax
It was inherited from before half-open ranges, but it doesn't pull its
weight anymore. We lose a tiny bit of diagnostic precision.
2024-03-13 14:17:11 +01:00
Nadrieril
f27540697e Rename RustcMatchCheckCtxt -> RustcPatCtxt 2024-03-13 14:07:44 +01:00
Nadrieril
4fc35c46ff Rename TypeCx -> PatCx 2024-03-13 13:56:38 +01:00
Nadrieril
cb15bf6256 Rename ValidityConstraint -> PlaceValidity
The old name came from a time where I wanted to reuse it for
differentiating wildcards from bindings. I don't plan to do this
anymore.
2024-03-13 13:53:18 +01:00
Nadrieril
b878ab6a27 Don't suggest an arm when suggesting a never pattern 2024-03-12 21:38:31 +01:00
Nadrieril
9f2aa5b85a Suggest never pattern instead of _ for empty types 2024-03-12 21:38:30 +01:00
Nadrieril
1ec73d70fa Add Constructor::Never 2024-03-12 21:38:30 +01:00