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.
This commit is contained in:
Nicholas Nethercote
2026-09-28 15:42:03 +10:00
parent 0efd8697df
commit 434157508f
7 changed files with 92 additions and 61 deletions

View File

@@ -0,0 +1,63 @@
use std::hash::Hash;
use rustc_data_structures::stable_hash::StableHasher;
use rustc_serialize::Encoder;
use rustc_span::def_id::{CrateNum, DefId, DefIndex};
use rustc_span::{ByteSymbol, ExpnId, Span, SpanEncoder, Symbol, SyntaxContext};
/// Hasher used to determine if two diagnostics are the same and should be deduplicated. Used by
/// `DiagCtxtInner::emitted_diagnostics`. It's a stable hash with one special behaviour: parents of
/// spans within the diagnostics are ignored so that incremental and non-incremental compilation
/// get the same behaviour.
///
/// Although this has nothing to do with encoding data to/from file, it is implemented on top of
/// `Encoder`/`SpanEncoder` because they provide traversals of all the relevant types used within
/// diagnostics. There is no corresponding decoder.
pub(crate) struct DedupHashEncoder(pub(crate) StableHasher);
macro_rules! encoder_methods {
($($name:ident($ty:ty);)*) => {
$(
#[inline]
fn $name(&mut self, value: $ty) {
value.hash(&mut self.0)
}
)*
}
}
impl Encoder for DedupHashEncoder {
encoder_methods! {
emit_usize(usize);
emit_u128(u128);
emit_u64(u64);
emit_u32(u32);
emit_u16(u16);
emit_u8(u8);
emit_isize(isize);
emit_i128(i128);
emit_i64(i64);
emit_i32(i32);
emit_i16(i16);
emit_raw_bytes(&[u8]);
}
}
impl SpanEncoder for DedupHashEncoder {
fn encode_span(&mut self, span: Span) {
// The raison d'être of `DedupHashEncoder` is to ignore `parent` here.
span.with_parent(None).hash(&mut self.0);
}
encoder_methods! {
encode_symbol(Symbol);
encode_byte_symbol(ByteSymbol);
encode_expn_id(ExpnId);
encode_syntax_context(SyntaxContext);
encode_crate_num(CrateNum);
encode_def_index(DefIndex);
encode_def_id(DefId);
}
}

View File

@@ -12,9 +12,11 @@ use rustc_error_messages::{DiagArgMap, DiagArgName, IntoDiagArg};
use rustc_hashes::Hash128;
use rustc_lint_defs::Applicability;
use rustc_macros::{Decodable, Encodable};
use rustc_serialize::Encodable;
use rustc_span::{Span, Spanned, Symbol};
use tracing::debug;
use crate::dedup_hash::DedupHashEncoder;
use crate::{
CodeSuggestion, DiagCtxtHandle, DiagMessage, ErrCode, ErrorGuaranteed, ExplicitBug, Level,
MultiSpan, StashKey, Style, Sublevel, Substitution, SubstitutionPart, SuggestionStyle,
@@ -298,20 +300,22 @@ impl DiagInner {
emitted_at: _, // ignore
} = self;
let hashed_parts = (
std::mem::discriminant(level), // ignore the field within `Warning`
messages,
code,
span,
children,
suggestions,
args.as_slice(),
is_lint,
);
let mut e = DedupHashEncoder(StableHasher::new());
let mut hasher = StableHasher::new();
hashed_parts.hash(&mut hasher);
hasher.finish()
// We use `discriminant` because we want to ignore the field within `Warning`.
// `Encoder`/`SpanEncoder` can't traverse a `Discriminant<T>` so we hash it directly.
std::mem::discriminant(level).hash(&mut e.0);
// All the other fields are hashed via `encode`.
messages.encode(&mut e);
code.encode(&mut e);
span.encode(&mut e);
children.encode(&mut e);
suggestions.encode(&mut e);
args.encode(&mut e);
is_lint.encode(&mut e);
e.0.finish()
}
}
@@ -328,14 +332,9 @@ impl Subdiag {
/// Hash used to determine if two subdiagnostics are the same. Used by
/// `DiagCtxtInner::emitted_diagnostics`.
pub(crate) fn dedup_hash(&self) -> Hash128 {
// Deconstruct to ensure all fields are considered.
let Subdiag { level, messages, span } = self;
let hashed_parts = (level, messages, span);
let mut hasher = StableHasher::new();
hashed_parts.hash(&mut hasher);
hasher.finish()
let mut e = DedupHashEncoder(StableHasher::new());
self.encode(&mut e);
e.0.finish()
}
}

View File

@@ -69,6 +69,7 @@ use crate::timings::TimingRecord;
pub mod annotate_snippet_emitter_writer;
pub mod codes;
mod decorate_diag;
mod dedup_hash;
mod diagnostic;
mod diagnostic_impls;
pub mod emitter;

View File

@@ -10,42 +10,6 @@ help: the trait `GlobalAllocator` is implemented for `System`
--> $SRC_DIR/std/src/alloc.rs:LL:COL
= note: required for `usize` to implement `GlobalAlloc`
error[E0277]: the trait bound `usize: GlobalAlloc` is not satisfied
--> $DIR/deduplicate-diagnostics-incr.rs:11:11
|
LL | #[global_allocator]
| ------------------- in this attribute macro expansion
LL | static A: usize = 0;
| ^^^^^ the nightly-only, unstable trait `GlobalAllocator` is not implemented for `usize`
|
help: the trait `GlobalAllocator` is implemented for `System`
--> $SRC_DIR/std/src/alloc.rs:LL:COL
= note: required for `usize` to implement `GlobalAlloc`
error[E0277]: the trait bound `usize: GlobalAlloc` is not satisfied
--> $DIR/deduplicate-diagnostics-incr.rs:11:11
|
LL | #[global_allocator]
| ------------------- in this attribute macro expansion
LL | static A: usize = 0;
| ^^^^^ the nightly-only, unstable trait `GlobalAllocator` is not implemented for `usize`
|
help: the trait `GlobalAllocator` is implemented for `System`
--> $SRC_DIR/std/src/alloc.rs:LL:COL
= note: required for `usize` to implement `GlobalAlloc`
error[E0277]: the trait bound `usize: GlobalAlloc` is not satisfied
--> $DIR/deduplicate-diagnostics-incr.rs:11:11
|
LL | #[global_allocator]
| ------------------- in this attribute macro expansion
LL | static A: usize = 0;
| ^^^^^ the nightly-only, unstable trait `GlobalAllocator` is not implemented for `usize`
|
help: the trait `GlobalAllocator` is implemented for `System`
--> $SRC_DIR/std/src/alloc.rs:LL:COL
= note: required for `usize` to implement `GlobalAlloc`
error: aborting due to 4 previous errors
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0277`.

View File

@@ -21,6 +21,7 @@ LL | static A: usize = 0;
help: the trait `GlobalAllocator` is implemented for `System`
--> $SRC_DIR/std/src/alloc.rs:LL:COL
= note: required for `usize` to implement `GlobalAlloc`
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
error[E0277]: the trait bound `usize: GlobalAlloc` is not satisfied
--> $DIR/deduplicate-diagnostics-incr.rs:11:11
@@ -33,6 +34,7 @@ LL | static A: usize = 0;
help: the trait `GlobalAllocator` is implemented for `System`
--> $SRC_DIR/std/src/alloc.rs:LL:COL
= note: required for `usize` to implement `GlobalAlloc`
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
error[E0277]: the trait bound `usize: GlobalAlloc` is not satisfied
--> $DIR/deduplicate-diagnostics-incr.rs:11:11
@@ -45,6 +47,7 @@ LL | static A: usize = 0;
help: the trait `GlobalAllocator` is implemented for `System`
--> $SRC_DIR/std/src/alloc.rs:LL:COL
= note: required for `usize` to implement `GlobalAlloc`
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
error: aborting due to 4 previous errors

View File

@@ -10,8 +10,8 @@
#[global_allocator]
static A: usize = 0;
//[dedup,dup,incr-dedup,incr-dup]~^ ERROR E0277
//[dup,incr-dedup,incr-dup]~| ERROR E0277
//[dup,incr-dedup,incr-dup]~| ERROR E0277
//[dup,incr-dedup,incr-dup]~| ERROR E0277
//[dup,incr-dup]~| ERROR E0277
//[dup,incr-dup]~| ERROR E0277
//[dup,incr-dup]~| ERROR E0277
fn main() {}

View File

@@ -35,6 +35,7 @@ numer = "numer" # short for numerator, not a typo for "number"
old-skool = "old-skool" # variant spelling of "old-school"
optin = "optin" # short for opt-in
publically = "publically"
raison = "raison" # part of the French phrase "raison d'être"
rplace = "rplace" # short for R-place
splitted = "splitted"
sythetic = "sythetic" # typo in vendored LLVM sources