Commit Graph

140 Commits

Author SHA1 Message Date
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
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
Nicholas Nethercote
0ac1195ee0 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 be converted to use translated diagnostics.

This commit removes more `deny` attributes than it adds `allow`
attributes, which proves that this change is warranted.
2024-02-06 13:12:33 +11: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