Skip to content

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

@bdonlan

Description

@bdonlan

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions