rust-lang / rust-lang/rust

Traits with many supertraits result in highly verbose diagnostic output when used as an associated type bound

Open
#146,947 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
trait Foo1 {}
trait Foo2 {}
trait Foo3 {}

// Hide the "this trait has no implementations" warning
impl Foo1 for u32 {}
impl Foo2 for u32 {}
impl Foo3 for u32 {}

trait Bar : Foo1 + Foo2 + Foo3 {}

#[diagnostic::do_not_recommend]
impl<T: Foo1 + Foo2 + Foo3> Bar for T {}

trait T1 {
    type X: Bar;
}

impl T1 for () {
    type X = ();
}

pub fn main() {
}
Current output
Compiling playground v0.0.1 (/playground)
error[E0277]: the trait bound `(): Foo3` is not satisfied
  --> src/main.rs:21:14
   |
21 |     type X = ();
   |              ^^ the trait `Foo3` is not implemented for `()`
   |
   = help: the trait `Foo3` is implemented for `u32`
note: required for `<() as T1>::X` to implement `Bar`
  --> src/main.rs:11:7
   |
11 | trait Bar : Foo1 + Foo2 + Foo3 {}
   |       ^^^
note: required by a bound in `T1::X`
  --> src/main.rs:17:13
   |
17 |     type X: Bar;
   |             ^^^ required by this bound in `T1::X`

error[E0277]: the trait bound `(): Foo2` is not satisfied
  --> src/main.rs:21:14
   |
21 |     type X = ();
   |              ^^ the trait `Foo2` is not implemented for `()`
   |
   = help: the trait `Foo2` is implemented for `u32`
note: required for `<() as T1>::X` to implement `Bar`
  --> src/main.rs:11:7
   |
11 | trait Bar : Foo1 + Foo2 + Foo3 {}
   |       ^^^
note: required by a bound in `T1::X`
  --> src/main.rs:17:13
   |
17 |     type X: Bar;
   |             ^^^ required by this bound in `T1::X`

error[E0277]: the trait bound `(): Foo1` is not satisfied
  --> src/main.rs:21:14
   |
21 |     type X = ();
   |              ^^ the trait `Foo1` is not implemented for `()`
   |
   = help: the trait `Foo1` is implemented for `u32`
note: required for `<() as T1>::X` to implement `Bar`
  --> src/main.rs:11:7
   |
11 | trait Bar : Foo1 + Foo2 + Foo3 {}
   |       ^^^
note: required by a bound in `T1::X`
  --> src/main.rs:17:13
   |
17 |     type X: Bar;
   |             ^^^ required by this bound in `T1::X`

For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground` (bin "playground") due to 3 previous errors
Desired output
Compiling playground v0.0.1 (/playground)
error[E0277]: the trait bound `(): Bar` is not satisfied
  --> src/main.rs:21:14
   |
21 |     type X = ();
   |              ^^ the trait `Bar` is not implemented for `()`
   |
   = help: the trait `Bar` is implemented for `u32`

For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground` (bin "playground") due to 1 previous errors
Rationale and extra context

It's common to define a meta-trait that combines multiple other traits into a single object-safe package. Indeed, the #[diagnostic::do_not_recommend] attribute is ideal for this use case, suppressing diagnostic spam from potential impls when attempting to resolve one of these meta-traits. However, when using a meta-trait as a constraint on a trait associated type, there is no way to limit the diagnostic spam that occurs for each of the parent trait constraints that fail to match.

While this hint may be helpful in some cases, having a diagnostic attribute to suppress this for specific traits would be very handy.

Other cases

Rust Version
rustc 1.92.0-nightly (f6092f224 2025-09-22)
binary: rustc
commit-hash: f6092f224d2b1774b31033f12d0bee626943b02f
commit-date: 2025-09-22
host: x86_64-unknown-linux-gnu
release: 1.92.0-nightly
LLVM version: 21.1.1
Anything else?

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by compiling the provided Rust example and compare its diagnostics with the desired output. Investigate how the associated type bound type X: Bar is diagnosed and how #[diagnostic::do_not_recommend] affects the parent-trait obligations. Done means the example reports one concise Bar error instead of separate errors for each supertrait.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.