Commit Graph

153 Commits

Author SHA1 Message Date
Nadrieril
120d3570aa Add barest-bones deref patterns
Co-authored-by: Deadbeef <ent3rm4n@gmail.com>
2024-03-20 22:30:27 +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
Nadrieril
d339bdaa07 DeconstructedPat.data is always present now 2024-03-11 04:38:57 +01:00
Nadrieril
6ae9fa31f0 Store field indices in DeconstructedPat to avoid virtual wildcards 2024-03-11 04:37:21 +01:00
Nadrieril
c1e68860d0 Store pattern arity in DeconstructedPat
Right now this is just `self.fields.len()` but that'll change in the
next commit. `arity` will be useful for the `Debug` impl.
2024-03-11 04:37:21 +01:00
Nicholas Nethercote
e9f0d9be0e Rename DecorateLint as LintDiagnostic.
To match `derive(LintDiagnostic)`.
2024-03-11 10:04:50 +11:00
Nicholas Nethercote
541d7cc65c Rename AddToDiagnostic as Subdiagnostic.
To match `derive(Subdiagnostic)`.

Also rename `add_to_diagnostic{,_with}` as `add_to_diag{,_with}`.
2024-03-11 10:04:49 +11:00
Nadrieril
8ac9a04257 Lint small gaps between ranges 2024-03-09 01:14:22 +01:00
Nadrieril
a047284b5a Make MaybeInfiniteInt::plus_one/minus_one fallible 2024-03-09 01:13:42 +01:00
Matthias Krüger
44bd2b5166
Rollup merge of #121987 - Nadrieril:abort-on-arity-mismatch, r=compiler-errors
pattern analysis: abort on arity mismatch

This is one more PR replacing panics by `Err()` aborts. I recently audited all the `unwrap()` calls, but I had forgotten about array accesses. (Again [discovered by rust-analyzer](https://github.com/rust-lang/rust-analyzer/issues/16746)).

r? ```@compiler-errors```
2024-03-05 06:40:33 +01:00
Nicholas Nethercote
573267cf3c Rename SubdiagnosticMessageOp as SubdiagMessageOp. 2024-03-05 12:14:49 +11:00
Nadrieril
2af01a2fef Abort on arity mismatch
As this can cause panics on array accesses later.
2024-03-04 19:35:33 +01:00
Guillaume Gomez
be31b6b6cd Add new pattern_complexity attribute to add possibility to limit and check recursion in pattern matching 2024-03-03 13:10:15 +01:00
bors
6cbf0926d5 Auto merge of #121728 - tgross35:f16-f128-step1-ty-updates, r=compiler-errors
Add stubs in IR and ABI for `f16` and `f128`

This is the very first step toward the changes in https://github.com/rust-lang/rust/pull/114607 and the [`f16` and `f128` RFC](https://rust-lang.github.io/rfcs/3453-f16-and-f128.html). It adds the types to `rustc_type_ir::FloatTy` and `rustc_abi::Primitive`, and just propagates those out as `unimplemented!` stubs where necessary.

These types do not parse yet so there is no feature gate, and it should be okay to use `unimplemented!`.

The next steps will probably be AST support with parsing and the feature gate.

r? `@compiler-errors`
cc `@Nilstrieb` suggested breaking the PR up in https://github.com/rust-lang/rust/pull/120645#issuecomment-1925900572
2024-03-01 03:36:11 +00:00
Guillaume Gomez
9df7f26b1b
Rollup merge of #121000 - Nadrieril:keep_all_fields, r=compiler-errors
pattern_analysis: rework how we hide empty private fields

Consider this:
```rust
mod foo {
  pub struct Bar {
    pub a: bool,
    b: !,
  }
}

fn match_a_bar(bar: foo::Bar) -> bool {
  match bar {
    Bar { a, .. } => a,
  }
}
```

Because the field `b` is private, matches outside the module are not allowed to observe the fact that `Bar` is empty. In particular `match bar {}` is valid within the module `foo` but an error outside (assuming `exhaustive_patterns`).

We currently handle this by hiding the field `b` when it's both private and empty. This means that the pattern `Bar { a, .. }` is lowered to `Bar(a, _)` if we're inside of `foo` and to `Bar(a)` outside. This involves a bit of a dance to keep field indices straight. But most importantly this makes pattern lowering depend on the module.

In this PR, I instead do nothing special when lowering. Only during analysis do we track whether a place must be skipped.

r? `@compiler-errors`
2024-02-29 17:08:37 +01:00
Matthias Krüger
eca6a249a6
Rollup merge of #121735 - Nadrieril:no-panic-on-type-error, r=compiler-errors
pattern analysis: Don't panic when encountering unexpected constructor

Tiny PR to fix https://github.com/rust-lang/rust-analyzer/issues/16656

r? ``@compiler-errors``
2024-02-29 00:17:00 +01:00
Trevor Gross
e3f63d9375 Add f16 and f128 to rustc_type_ir::FloatTy and rustc_abi::Primitive
Make changes necessary to support these types in the compiler.
2024-02-28 12:58:32 -05:00
Nadrieril
c918893b63 Rename Skip to PrivateUninhabited 2024-02-28 17:56:01 +01:00
Nadrieril
39441e4cdd Simplify 2024-02-28 17:47:19 +01:00
Nadrieril
ea38166390 Don't filter out skipped fields 2024-02-28 17:47:19 +01:00
Nadrieril
4f7f06777b Add special Skip constructor 2024-02-28 17:47:19 +01:00
Nadrieril
ab06037269 Push the decision to skip fields further down 2024-02-28 17:47:19 +01:00
Nadrieril
be01e28dce Push down the decision to skip fields 2024-02-28 17:47:19 +01:00
Nadrieril
12b991d9f8 Don't panic when encountering unexpected constructor 2024-02-28 13:28:27 +01:00
Nicholas Nethercote
899cb40809 Rename DiagnosticBuilder as Diag.
Much better!

Note that this involves renaming (and updating the value of)
`DIAGNOSTIC_BUILDER` in clippy.
2024-02-28 08:55:35 +11:00
Matthias Krüger
86a35c07b2
Rollup merge of #121324 - Nadrieril:unspecialize, r=cjgillot
pattern_analysis: factor out unspecialization

Just moving a dense bit of logic into its own method.
2024-02-25 17:05:21 +01:00
Matthias Krüger
86a7fc840f compiler: clippy::complexity fixes 2024-02-23 19:56:35 +01:00
bors
29f87ade9d Auto merge of #120576 - nnethercote:merge-Diagnostic-DiagnosticBuilder, r=davidtwco
Overhaul `Diagnostic` and `DiagnosticBuilder`

Implements the first part of https://github.com/rust-lang/compiler-team/issues/722, which moves functionality and use away from `Diagnostic`, onto `DiagnosticBuilder`.

Likely follow-ups:
- Move things around, because this PR was written to minimize diff size, so some things end up in sub-optimal places. E.g. `DiagnosticBuilder` has impls in both `diagnostic.rs` and `diagnostic_builder.rs`.
- Rename `Diagnostic` as `DiagInner` and `DiagnosticBuilder` as `Diag`.

r? `@davidtwco`
2024-02-20 12:05:09 +00:00
bors
bcea3cb748 Auto merge of #120692 - Nadrieril:move-column-analysis-to-placeinfo, r=compiler-errors
pattern_analysis: Move constructor selection logic to `PlaceInfo`

This is a small refactor PR. There was a dense bit of constructor-related logic in `compute_exhaustiveness_and_usefulness`. I'm moving it out into a `PlaceInfo` method to make it easier to follow both separately. I also have plans that will complicate it further so it's good that it's somewhat encapsulated.

r? `@compiler-errors`
2024-02-20 04:57:15 +00:00
Nadrieril
47b21730c4 Factor out unspecialization 2024-02-20 04:45:18 +01:00
Nicholas Nethercote
f6f8779843 Reduce capabilities of Diagnostic.
Currently many diagnostic modifier methods are available on both
`Diagnostic` and `DiagnosticBuilder`. This commit removes most of them
from `Diagnostic`. To minimize the diff size, it keeps them within
`diagnostic.rs` but changes the surrounding `impl Diagnostic` block to
`impl DiagnosticBuilder`. (I intend to move things around later, to give
a more sensible code layout.)

`Diagnostic` keeps a few methods that it still needs, like `sub`,
`arg`, and `replace_args`.

The `forward!` macro, which defined two additional methods per call
(e.g. `note` and `with_note`), is replaced by the `with_fn!` macro,
which defines one additional method per call (e.g. `with_note`). It's
now also only used when necessary -- not all modifier methods currently
need a `with_*` form. (New ones can be easily added as necessary.)

All this also requires changing `trait AddToDiagnostic` so its methods
take `DiagnosticBuilder` instead of `Diagnostic`, which leads to many
mechanical changes. `SubdiagnosticMessageOp` gains a type parameter `G`.

There are three subdiagnostics -- `DelayedAtWithoutNewline`,
`DelayedAtWithNewline`, and `InvalidFlushedDelayedDiagnosticLevel` --
that are created within the diagnostics machinery and appended to
external diagnostics. These are handled at the `Diagnostic` level, which
means it's now hard to construct them via `derive(Diagnostic)`, so
instead we construct them by hand. This has no effect on what they look
like when printed.

There are lots of new `allow` markers for `untranslatable_diagnostics`
and `diagnostics_outside_of_impl`. This is because
`#[rustc_lint_diagnostics]` annotations were present on the `Diagnostic`
modifier methods, but missing from the `DiagnosticBuilder` modifier
methods. They're now present.
2024-02-20 13:22:17 +11:00
bors
0a5b998c57 Auto merge of #120991 - matthiaskrgr:rollup-f8kw2st, r=matthiaskrgr
Rollup of 8 pull requests

Successful merges:

 - #118983 (Warn on references casting to bigger memory layout)
 - #119451 (Gate PR CI on clippy correctness lints)
 - #120273 (compiletest: few naive improvements)
 - #120950 (Fix async closures in CTFE)
 - #120958 (Dejargonize `subst`)
 - #120965 (Add lahfsahf and prfchw target feature)
 - #120970 (add another test for promoteds-in-static)
 - #120979 (Update books)

r? `@ghost`
`@rustbot` modify labels: rollup
2024-02-13 00:31:53 +00:00
bors
74c3f5a146 Auto merge of #120324 - Nadrieril:remove-interior-mutability, r=compiler-errors
pattern_analysis: track usefulness without interior mutability

Because of or-patterns, exhaustiveness needs to be able to lint if a sub-pattern is redundant, e.g. in `Some(_) | Some(true)`. So far the only sane solution I had found was interior mutability. This is a bit of an abstraction leak, and would become a footgun if we ever reused the same `DeconstructedPat`. This PR replaces interior mutability with an address-indexed hashmap, which is logically equivalent.
2024-02-12 22:16:58 +00:00
Shoyu Vanilla
3856df059e Dejargnonize subst 2024-02-12 15:46:35 +09:00
Matthias Krüger
46a0448405
Rollup merge of #120693 - nnethercote:invert-diagnostic-lints, r=davidtwco
Invert diagnostic lints.

That is, change `diagnostic_outside_of_impl` and `untranslatable_diagnostic` from `allow` to `deny`, because more than half of the compiler has been converted to use translated diagnostics.

This commit removes more `deny` attributes than it adds `allow` attributes, which proves that this change is warranted.

r? ````@davidtwco````
2024-02-09 14:41:50 +01:00
Matthias Krüger
4ffb1a7f3d
Rollup merge of #120590 - compiler-errors:dead, r=Nilstrieb
Remove unused args from functions

`#[instrument]` suppresses the unused arguments from a function, *and* suppresses unused methods too! This PR removes things which are only used via `#[instrument]` calls, and fixes some other errors (privacy?) that I will comment inline.

It's possible that some of these arguments were being passed in for the purposes of being instrumented, but I am unconvinced by most of them.
2024-02-08 20:34:57 +01:00
Nadrieril
778c7e1db4 Move constructor selection logic to PlaceInfo 2024-02-08 15:34:17 +01:00
Nadrieril
3602b9d817 Decide which constructors to report earlier.
This gets rid of `report_individual_missing_ctors`
2024-02-08 15:34:17 +01:00
Nadrieril
924d6cd1a6 Tweak how we record missing constructors
This is slower but also not a performance-sensitive path.
2024-02-08 15:34:17 +01:00
Matthias Krüger
87e1e05aa1
Rollup merge of #120734 - nnethercote:SubdiagnosticMessageOp, r=compiler-errors
Add `SubdiagnosticMessageOp` as a trait alias.

It avoids a lot of repetition.

r? matthewjasper
2024-02-08 09:06:36 +01:00
Nicholas Nethercote
6b175a848d Add SubdiagnosticMessageOp as a trait alias.
It avoids a lot of repetition.
2024-02-08 13:02:44 +11:00
Nadrieril
9dca6be7b8 Prefer "0..MAX not covered" to "_ not covered" 2024-02-07 23:25:11 +01:00