Commit Graph

1361 Commits

Author SHA1 Message Date
Nicholas Nethercote
434157508f Improve diagnostic deduplication
By making it ignore the parent field of spans within the diagnostic.
This is done with a new `DedupHashEncoder` type that uses the existing
`Encoder`/`SpanEncoder` traits to traverse the fields within `DiagInner`
and `Subdiag`.

Fixes #162901.
2026-10-01 21:46:52 +10:00
Nicholas Nethercote
38c6e9545d Clarify OnceNote/OnceHelp subdiag deduplication
- Introduce `Subdiag::dedup_hash`, which mirrors
  `DiagInner::dedup_hash` and is necessary for the subsequent commits.
- Change the name and structure of `not_yet_emitted` to be clearer.
- Change `retain_mut` to `retain`; mutability isn't required.
2026-10-01 20:07:26 +10:00
Jonathan Brouwer
773bab3921 Rollup merge of #163303 - nnethercote:fix-Span-eq-ord, r=oli-obk
Remove `PartialOrd`/`Ord` impls for `Span`/`SpanData`

Because they are inconsistent with the `PartialEq`/`Eq` impls, and span ordering is inherently dubious. Details in individual commits.

r? @oli-obk
2026-09-28 14:19:39 +02:00
Nicholas Nethercote
4326cf73a3 Remove PartialOrd/Ord impls for Span/SpanData
`Span` and `SpanData` encode four fields: `lo`/`hi`, `ctxt`, and
`parent`. `ctxt` and `parent` have unorderable types.

Both types ignore `ctxt` and `parent` for `PartialOrd`/`Ord`. But their
`PartialEq`/`Eq` impls do *not* ignore those fields. This eq/ord
inconsistency is a bug. It was introduced in #123165.

The idea of ordering spans in general is dubious, because of `ctxt` and
`parent`. But the idea of ordering spans just with `lo`/`hi` is fine.
Therefore, this commit does the following.

- Removes the `PartialOrd`/`Ord` impls for `Span`/`SpanData`

- Adds a `Span::lo_hi` method which can be used in lots of places where
  span locations are involved in sorting. E.g.
  `xs.sort_by_key(|span| span.lo_hi())`

- Adds `OrdSpan`, a newtype around `Span` that impls
  `PartialEq`/`Eq`/`PartialOrd`/`Ord` using `lo_hi`. This is for storing
  spans in ordered types like `BTreeMap<OrdSpan, T>`.

Note also that some `sort`+`dedup` combinations might not remove all
duplicates with the old eq/ord inconsistency. These now all do the right
thing, which could affect some error messages, though in practice
nothing in the test suite is affected.
2026-09-26 09:05:21 +10:00
Nicholas Nethercote
fcf74ea737 Merge ForceWarning/Allow/Expect into Warning
Because they're all just special cases of warnings. This requires
introducing a new `Option<EmissionOverride>` field to `Warning` that
describes the special case behaviour; the `DiagInner::lint_id` field
also gets merged in.

Specific nice things about this:

- Removes some unreachable match arms for `Allow`/`Expect`.

- Removes the hacky upgrading of `Allow`/`Expect` to `Warning` in
  `emit_future_breakage_report`.

- The types now have structure that used to be maintained by comments
  and assertions. E.g. it's now impossible to not have a `lint_id` for
  an `expect` lint. (I always found the `DiagInner::lint_id` field
  confusing; it's clearer now.)

- There's a nice comment on `EmissionOverride` summarizing all the
  different cases.

- A little less code overall.
2026-09-26 07:47:13 +10:00
Nicholas Nethercote
4fc112e765 Remove an out-of-date FIXME comment
I think the ship has sailed on the "every error should have an error
code" idea and it's not worth pretending otherwise.
2026-09-25 08:09:20 +10:00
Nicholas Nethercote
e01dc94337 Some emit_diagnostic improvements
There is one match arm handling some `ForceWarning` cases and another
arm handling the remaining `ForceWarning` case and also `Expect`. They
can be rearranged into one arm handling `ForceWarning` and one arm
handling `Expect`.

Also fix some comments:
- `ForceWarning` no longer has a field.
- Clarify the `LintExpectationId` location.
- The one about deduplication was inverted.
2026-09-25 08:09:20 +10:00
Nicholas Nethercote
1e185bdebc Remove StashKey::AssociatedTypeSuggestion
Currently it's used in a really hacky way: a diagnostic with an empty
message and some suggestions is stashed. Later on it might be retrieved
and its suggestions extracted. It uses `Level::Allow` so that it isn't
printed by `emit_stashed_diagnostics` at shutdown if it hasn't been
retrieved.

This isn't a normal stashed diagnostic, more just a case of "save some
information that might be useful for diagnostics later". We can record
the information more simply and directly.

This commit adds `paths_matching_assoc_types` fields to `Resolver` and
`ResolverGlobalCtxt`. These store the spans of the paths of interest.
The new subdiagnostic `AssocTypeWithSameName` is used to construct the
relevant suggestions when they are needed. Much nicer. The old approach
removed the stashed diagnostic and the new approach doesn't remove the
set entry, so a single span could conceivably match more than once,
presumably a good thing, though this doesn't show up in the test suite.
2026-09-24 08:47:00 +10:00
Jonathan Brouwer
856b213184 Rollup merge of #163196 - nnethercote:rm-DiagInner-sort_span, r=lqd
Remove `DiagInner::sort_span`

It's a field that can be used for sorting diagnostics. By default it is set to the primary span. It's only used for `BorrowckDiagnosticsBuffer`.

This commit removes it and adds a `sort_span` to each diagnostic recorded in `BorrowckDiagnosticsBuffer`. This avoids various other pieces of code having to deal with it.

r? @lqd
2026-09-23 13:43:26 +02:00
Nicholas Nethercote
71d4ef88c6 Remove unused StashKey::UnderscoreForArrayLengths
It's no longer ever used for stashing, so the code looking for it is
dead.

It became dead in PR 141610 when `generic_arg_infer` was stabilized and
`[T; _]` became valid.
2026-09-23 20:37:23 +10:00
Nicholas Nethercote
8084d720c5 Remove DiagInner::sort_span
It's a field that can be used for sorting diagnostics. By default it is
set to the primary span. It's only used for `BorrowckDiagnosticsBuffer`.

This commit removes it and adds a `sort_span` to each diagnostic
recorded in `BorrowckDiagnosticsBuffer`. This avoids various other
pieces of code having to deal with it.
2026-09-23 15:57:35 +10:00
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