Commit Graph

1350 Commits

Author SHA1 Message Date
Nicholas Nethercote
9fecc5aefe Clean up diagnostic hashing
`DiagInner` impls `PartialEq` and `Hash`, as you'd expect for storing it
in a hash table. But there's a couple of strange things.

- We only store the hash value of the `DiagInner` to do deduplication,
  not the `DiagInner` itself, which means the `PartialEq` impl is
  unused.

- The `Hash` impl only considers some of the fields. Some of the ignored
  fields are clearly deliberate (there are comments) but for some it is
  unclear if it is deliberate.

This commit:

- Removes the unused `PartialEq` impl.

- Inlines and removes `keys` now that it's not needed for `PartialEq`.

- Uses struct deconstruction to ensure no fields can be accidentally
  ignored. I have preserved existing behaviour by assuming that all the
  ignored fields are supposed to be ignored.

- Renames `hash` as an inherent method `dedup_hash` to indicate that
  it's not a typical hash function, and simplifies it to just return
  `Hash128` instead of being generic.

- Replaces the unnecessary `collect` on `args` with `as_slice`.

- Improves the comment on `emitted_diagnostics`.
2026-09-22 16:44:10 +10:00
Jonathan Brouwer
a7be2941de Rollup merge of #163083 - nnethercote:rm-Diag-G, r=oli-obk
Remove `G` generic param from `Diag<'_, G>`

The benefits are outweighed by the costs. Details in invidual commits.

some minor earlier discussion about this change in https://github.com/rust-lang/rust/pull/162630#discussion_r3988468478
2026-09-21 15:19:21 +02:00
Nicholas Nethercote
30b8dfc353 Remove G generic param from Diag<'_, G>
It encodes the `emit` behaviour, one of:
- `BugAbort`: Abort as a bug.
- `FatalAbort`: Abort as a fatal error.
- `ErrorGuaranteed`: Return an `ErrorGuaranteed` (the default).
- `()`: return `()`.

This is useful but `Diag` is very widely used and it's arguably not
useful enough to be worth the generic parameter. This commit removes it.

Benefits:
- Many fewer generic parameters, including many functions where the
  genericity is entirely uninteresting. This includes every
  `Diagnostic::into_diag` and `Subdiagnostic::add_to_diag`. Removes the
  need for the comment about `<G>` on `trait Diagnostic`.
- No `PhantomData` in `Diag`.
- Makes bootstrapping a bit faster.

The downside is there is no longer a single `emit` method.
- Getting a `!` return type requires calling `emit_bug` or `emit_fatal`.
  This only affects a few call sites.
- Getting an `ErrorGuaranteed` return type requires calling `emit_err`.
  This affects a lot of call sites. (It arguably makes call sites more
  self-documenting.)
- Using the wrong `emit_*` on a diagnostic causes a runtime abort, which
  previously couldn't occur. Seems unlikely, but it's not impossible.
- Note: If no particular return type is needed, `emit` can be used for
  any diagnostic. (Bug/fatal diagnostics will still trigger abort.)
2026-09-21 09:57:42 +10:00
Esteban Küber
f1e9e5e4aa When error from local macro, include macro def span
- mention `-Zmacro-backtrace` only for non-local macros
- include span for local macro encompassing error
2026-09-20 04:11:09 +00:00
Jonathan Brouwer
1d8d9cadf2 Rollup merge of #162873 - nnethercote:fix-emit_bug, r=mejrs
Adjust `bug!`/`span_bug!` emission

PR rust-lang/rust#161873 moved these macros from `rustc_middle` to `rustc_span`. In doing so it made some undesirable changes.

- The internal `emit_producing_nothing` became public.

- The macros now have different behaviour to function-based ICEs like `dcx.bug(..)` or `dcx.struct_bug(..).emit()`.

This commit reverts those changes. This brings back `Box<dyn Any>` output in a few tests because the panic payload is `ExplicitBug` (which std doesn't know about) rather than `String`. That might be worth fixing again in the future, but if so, it should be done in a way that works with all aborts, not just those done via the macros.

r? @mejrs
2026-09-19 08:45:41 +02:00
Nicholas Nethercote
2598e89f8a Simplify query stack printing
Currently it uses `eprintln!` for the "query stack during panic" header
and "end of query stack" footer and
`struct_failure_note(..).with_span(..).emit()` for the stack frames. The
difference is curious.

Looking more closely:
- failure notes lack a leading label (e.g. no "note: ");
- although the failure note is given a span, the span never renders
  because the `AnnotateSnippetEmitter` used for the query stack has an
  empty source map field;
- the JSON emitter is never involved here.

Therefore, the only difference between using `struct_failure_note` and
`eprintln!` is that the former prints in bold. But boldness for frames
isn't important; indeed, normal backtrace frames don't use bold.

So the commit changes query stack frame printing to use `eprintln!`,
removes some now unnecessary `dcx` arguments, and also removes
`struct_failure_note`. That leaves "For more information about an error,
try `rustc --explain {}`" as the only use of `FailureNote`.
2026-09-18 18:47:07 +10:00
Nicholas Nethercote
0e617d448a Adjust bug!/span_bug! emission
PR #161873 moved these macros from `rustc_middle` to `rustc_span`. In
doing so it made some undesirable changes.

- The internal `emit_producing_nothing` became public.

- It gave the macros different behaviour to function-based ICEs like
  `dcx.bug(..)` or `dcx.struct_bug(..).emit()`.

- `ExplicitBug` is not used, so `report_ice` prints an extraneous,
  unintended "the compiler unexpectedly panicked" message.

- It broke the interaction between `span_bug!` and
  `-Ztrack-diagnostics`.

This commit adjusts things to avoid the undesirable changes. It required
moving `ExplicitBug` to `rustc_span`. Setting `diag.emitted_at` fixed
the `-Ztrack-diagnostics` problem, seen in the change from `callback.rs`
to `validate.rs` in `track7.stderr`.

`Box<dyn Any>` output now occurs again in a few tests because the panic
payload is `ExplicitBug` (which std doesn't know about) rather than
`String`. That might be worth addressing in the future but it would
require a different/additional mechanism and is beyond the scope of this
PR.
2026-09-18 15:44:23 +10:00
Jonathan Brouwer
d2aced1659 Rollup merge of #162885 - nnethercote:rm-Level-Help, r=estebank
Remove one remaining use of `Level::Help`

Unfortunately we can't yet get rid of `Level::Help` entirely because of `proc_macro::Help`. Details in individual commits.

r? @estebank
2026-09-17 21:00:11 +02:00
Nicholas Nethercote
31fafe3aed Clarify Level::{Help,Note,FailureNote} details
And remove `struct_help`, to discourage use of `Level::Help`.
(`Sublevel::Help` is fine.)
2026-09-17 16:53:21 +10:00
Esteban Küber
d38f60bd45 Add support for annotate_snippets::snippet::AnnotationKind::Visible
`annotate-snippets-rs` supports pointing at `Span`s without an underline. Start using that feature in `rustc`. We currently use secondary span labels with no text to point at context that should appear in the diagnostic, but that are merely context.
2026-09-16 22:11:50 +00:00
mejrs
784649bf5f use rustc_interface callback to emit span_bug diagnostic 2026-09-15 16:20:16 +02:00
Nicholas Nethercote
09bf6de39e Remove EmissionGuarantee
It's a trait that is used to give `Diag::emit` different return types
depending on the `G` in `Diag<'_, G>`. It works, but it's overkill.
There are only four different `G` types in practice (`BugAbort`,
`FatalAbort`, `ErrorGuaranteed`, and `()`) and having inherent `emit`
methods for each of those four concrete `Diag<'_, G>` types is good
enough.

This commit makes that change. This removes `EmissionGuarantee`
trait bounds from many places, which is nice.
2026-09-12 11:21:03 +10:00
Nicholas Nethercote
6331ab69b4 Introduce Sublevel for errors
Currently `Level` is used for both diagnostics and subdiagnostics. But
both diagnostics and subdiagnostics only use some of the levels. This
commit introduces `Sublevel` to tighten up the representation and
prevent impossible combinations.
2026-09-11 06:46:46 +10:00
Nicholas Nethercote
996ef5d739 Replace a format_diag_messages use
When there's a single message, `format_diag_message` suffices.
2026-09-11 06:41:29 +10:00
Nicholas Nethercote
025e029824 Reduce unnecessary Cow use in error formatting
`format_diag_messages` always constructs a `String`, so it can just
return that. This simplifies various call sites.
2026-09-11 06:41:29 +10:00
Nicholas Nethercote
63fddb3657 Remove Style::HeaderMsg
It has a single use, where it is passed in to `format_diag_messages`
which then discards it. So that use can be replaced with
`Style::NoStyle`.
2026-09-11 06:41:29 +10:00
Nicholas Nethercote
7bc87d508d Remove most Style variants
These are dead since the old emitter was removed in Jan 2026
(3ccabc6a8d).

Also removed: `BRIGHT_BLUE`, `Level::color`, and a couple of `level`
fn parameters.
2026-09-11 06:41:29 +10:00
Jonathan Brouwer
bd0cf87c2c Rollup merge of #162057 - bjorn3:fatal_cleanup, r=mejrs
Some cleanups around fatal errors and delayed bugs

With the aim of making it easier to move some compiler passes to different locations. And in particular to allow moving them across `has_errors_or_delayed_bugs` calls. This is a prerequisite for a local change that improves the effectiveness of cargo build pipelining.

Fixes https://github.com/rust-lang/rust/issues/152204
2026-09-10 14:22:30 +02:00
bjorn3
54b751d55a Remove emit_almost_fatal
A fatal error that doesn't actually abort is indistinguishable from a
regular error. And the only places where emit_almost_fatal is called,
the produced FatalError is ignored.
2026-09-09 14:03:57 +02:00
Nicholas Nethercote
d571cae83e Minimize DiagCtxt methods
We have `DiagCtxt`, `DiagCtxtInner`, and `DiagCtxtHandle`. They're all
necessary, but the method placement is inconsistent. This commit
establishes a clearer rule: methods should *not* go on `DiagCtxt`
whenever possible.

Benefits:

- Simpler rule for deciding where to put a method.

- `impl Deref for DiagCtxtHandle` can be removed, including its
  non-standard double-reference `&&'a DiagCtxt` return value.
  This makes it impossible to accidentally discard a taint target by
  calling `handle` on a `DiagCtxtHandle`.

- Catches some places where we were calling `dcx.handle()` when `dcx`
  was already a handle.
2026-09-08 15:25:09 +10:00
Waffle Lapkin
f34322b0dc remove #![feature(never_type)] from the compiler
The reason `rustc_ast_ir` and `rustc_pattern_analysis` have nested
`cfg_attr`s is so that they don't mention `cfg(bootstrap)` when nightly
or rustc cargo feature turned on. If you don't do it like that, they stop
compiling on stable (or rather, they emit unexpected cfg warnings...).
2026-08-24 19:04:00 +02:00
lcnr
89c2f92806 only emit a single recursion_depth_exceeding_limit 2026-08-22 18:11:25 +02:00
Jonathan Brouwer
8e0aaa4858 Rollup merge of #160747 - mejrs:unused_impls, r=GuillaumeGomez,JonathanBrouwer
rustc_errors: remove unused code

r? @GuillaumeGomez
2026-08-08 16:32:49 +02:00
mejrs
a74c62289a Remove unused diagnostic items and features 2026-08-08 13:29:26 +02:00
mejrs
f3ad95670f Remove unused Box diagnostic impl 2026-08-08 13:17:01 +02:00
mejrs
b5c330c30e snippet emitter: rework debug impls 2026-08-06 23:38:00 +02:00
Esteban Küber
d0cfd66652 When shortened type would look the same, use textual shortening on the highlighted string instead
Introduce a mechanism to textually trim highlighted strings, so that we can print out "expected/found" note with shorter output while still keeping the highlighted inner parts.

```
error[E0308]: mismatched types
  --> $DIR/long-type-E0308.rs:30:5
   |
LL |   ) -> <
   |  ______-
LL | |     <
LL | |         <T::Wrapper<T> as FooBar>::Assoc as FooBar
LL | |     >::Assoc as FooBar
LL | | >::Wrapper<<T::Assoc as FooBar>::Assoc>
   | |_______________________________________- expected `<_ as FooBar>::Wrapper<_>` because of return type
...
LL |       value...oc())
   |       ^^^^^...^^^^^ expected `really_really_really_long_module::FooBar::Wrapper`, found `really_really_really_long_module::FooBar::Assoc`
   |
   = note: expected associated type `<<<<_ as FooBar>::Wrapper<_> as...pper<_>`
              found associated type `<<<<_ as FooBar>::Assoc as Fo...pper<_>`
   = note: an associated type was expected, but a different one was found
   = note: the full name for the type has been written to '$TEST_BUILD_DIR/long-type-E0308.long-type-$LONG_TYPE_HASH.txt'
   = note: consider using `--verbose` to print the full type name to the console
```
2026-07-20 04:51:11 +00:00
mejrs
6a17b7948e Remove hide_backtrace 2026-07-05 15:11:37 +02:00
mejrs
7f3569b174 Implement diagnostic::opaque attribute 2026-07-05 15:11:36 +02:00
Jonathan Brouwer
a3d0568203 Rollup merge of #158134 - clubby789:update-msrv-flag, r=JonathanBrouwer
Rename `lint-rust-version` to `hint-msrv`

As suggested here: https://github.com/rust-lang/rust/issues/157574#issuecomment-4719742758. I agree that this version is more clear about semantics, and reflects the consensus of the previous MCP.

Tracking issue: rust-lang/rust#157574
2026-06-19 19:43:46 +02:00
Jamie Hill-Daniel
c06ec63c2f Rename lint-rust-version to hint-msrv 2026-06-19 17:46:53 +01:00
Jonathan Brouwer
d50d1db7e5 Move UnusedDuplicate diag struct to rustc_attr_parsing 2026-06-17 13:46:30 +02:00
Jamie Hill-Daniel
68f4e76e87 Don't emit lints that would violate MSRV 2026-06-15 20:36:27 +01:00
Jacob Pratt
0831b34e2e Rollup merge of #157282 - xmakro:fix/parallel-pme-note, r=petrochenkov
Fix post-monomorphization error note race in the parallel frontend

Fixes rust-lang/rust#154260. Part of https://github.com/rust-lang/rust/issues/154314.

`collect_items_rec` decides whether to attach the note "the above error was encountered while instantiating `fn ...`" by snapshotting the error count before processing a mono item and comparing it afterwards. The mono item graph is walked in parallel (the `par_for_each_in` in `collect_crate_mono_items`), so the global error count can be bumped by an error emitted while collecting a different item on another thread between the two reads. That makes an unrelated item get blamed, which showed up as a spurious note when using parallel frontend:

```
note: the above error was encountered while instantiating `fn std::rt::lang_start::<()>`
```

This PR records the thread that emits each error alongside its `ErrorGuaranteed` in `err_guars`, `lint_err_guars`, and the stashed-diagnostics map, and add `err_count_on_current_thread`, which counts only the errors whose recorded thread is the calling thread. The collector uses it for the snapshot and comparison, so the delta is not affected by errors emitted on other threads. In serial compilation it is equivalent to a delta of `err_count`.
2026-06-11 00:34:00 -04:00
Mark Z. Ding
4e26424d8b chore: Update annotate-snippets to 0.12.16 2026-06-07 01:06:01 -04:00
xmakro
c1cd23675f Attribute post-monomorphization error notes by emitting thread 2026-06-05 09:52:21 -07:00
Nicholas Nethercote
33cd10a881 Rename rustc_data_structures::stable_hasher as stable_hash.
Part of MCP 983.
2026-05-08 15:10:33 +10:00
Nicholas Nethercote
9119225b7f Remove unnecessary trait bound on Subdiagnostic.
FWIW, note that `Diagnostic` doesn't have this bound.
2026-05-01 13:32:54 +10:00
Guillaume Gomez
e7b9d94435 Remove unused diagnostic types 2026-04-25 04:20:04 +02:00
Guillaume Gomez
36459d9914 Make it possible to have access to Session and crates name in attr_parsing lints 2026-04-25 04:12:23 +02:00
Guillaume Gomez
ded2eea3b2 Remove AttributeLintKind::UnusedDuplicate 2026-04-17 23:43:08 +02:00
Guillaume Gomez
c5b9918540 Set up API to make it possible to pass closures instead of AttributeLint.
The end goal being to completely remove `AttributeLint`.
2026-04-17 23:43:06 +02:00
Esteban Küber
b849e10d38 Make span_suggestions always verbose
`span_suggestions` is to provide mutually exclusive suggestions. When it was introduced, we made its behavior be that if a single suggestion is given to it, we present the suggestion inline, otherwise in patch format. Changing this to make all of its uses be verbose, as that is closer in intent of output.
2026-04-15 01:38:14 +00:00
thebabalola
fdfdb0837c Replace custom trim_ascii_start with the standard library method
The local trim_ascii_start function in the markdown parser duplicates
<[u8]>::trim_ascii_start() from the standard library (stable since 1.80).
Remove the custom function and call the stdlib method directly.

No behaviour change.

Fixes https://github.com/rustfoundation/interop-initiative/issues/53
2026-04-13 23:07:39 +01:00
thebabalola
1e02a2b10b Add test for list item leading whitespace trimming 2026-04-13 23:07:39 +01:00
bors
02c7f9bec0 Auto merge of #155115 - JonathanBrouwer:rollup-RePrRPQ, r=JonathanBrouwer
Rollup of 6 pull requests

Successful merges:

 - rust-lang/rust#152901 (Introduce a `#[diagnostic::on_unknown]` attribute)
 - rust-lang/rust#155078 (Reject dangling attributes in where clauses)
 - rust-lang/rust#154449 (Invert dependency between `rustc_errors` and `rustc_abi`.)
 - rust-lang/rust#154646 (Add suggestion to `.to_owned()` used on `Cow` when borrowing)
 - rust-lang/rust#154993 (compiletest: pass -Zunstable-options for unpretty and no-codegen paths)
 - rust-lang/rust#155097 (Make `rustc_attr_parsing::SharedContext::emit_lint` take a `MultiSpan` instead of a `Span`)
2026-04-10 18:19:03 +00:00
Jonathan Brouwer
197fb1b0d7 Rollup merge of #154449 - nnethercote:rustc_errors-dep-rustc_abi, r=davidtwco
Invert dependency between `rustc_errors` and `rustc_abi`.

Currently, `rustc_errors` depends on `rustc_abi`, which depends on `rustc_error_messages`. This is a bit odd.

`rustc_errors` depends on `rustc_abi` for a single reason: `rustc_abi` defines a type `TargetDataLayoutErrors` and `rustc_errors` impls `Diagnostic` for that type.

We can get a more natural relationship by inverting the dependency, moving the `Diagnostic` trait upstream. Then `rustc_abi` defines `TargetDataLayoutErrors` and also impls `Diagnostic` for it. `rustc_errors` is already pretty far upstream in the crate graph, it doesn't hurt to push it a little further because errors are a very low-level concept.

r? @davidtwco
2026-04-10 18:38:13 +02:00
Pavan
ef3a89c346 Fix tabs in test file to satisfy tidy check 2026-04-09 19:59:04 +05:30
Pavan
1e593417b3 Fix code block whitespace handling 2026-04-09 18:55:04 +05:30
Jonathan Brouwer
1d0d17207c Rollup merge of #154912 - GuillaumeGomez:migrate-diag, r=JonathanBrouwer
Remove `BuiltinLintDiag`

Part of https://github.com/rust-lang/rust/issues/153099.

We're finally getting rid of `BuiltinLintDiag`! \o/

Next step, `AttributeLint`. :3

r? @JonathanBrouwer
2026-04-08 23:04:32 +02:00