Skip to content

Duplicated "the trait bound [..] is not satisfied" errors when a struct definition uses a unsatisfied bound #144745

@oxalica

Description

@oxalica

Code

trait SomeTrait {
    type AssocTy;
}

struct SomeStruct {
    // The main error, `i32: SomeTrait` not satisfied.
    inner: <i32 as SomeTrait>::AssocTy,
}

impl SomeStruct {
    // Duplicated error.
    fn new() -> Self {
        todo!()
    }
}

// Duplicated error.
fn use_type(_: SomeStruct) {}

struct OtherStruct;
impl std::ops::Deref for OtherStruct {
    // Duplicated error.
    type Target = SomeStruct;
    fn deref(&self) -> &Self::Target {
        todo!()
    }
}

Current output

error[E0277]: the trait bound `i32: SomeTrait` is not satisfied
 --> src/lib.rs:7:12
  |
7 |     inner: <i32 as SomeTrait>::AssocTy,
  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `SomeTrait` is not implemented for `i32`
  |
help: this trait has no implementations, consider adding one
 --> src/lib.rs:1:1
  |
1 | trait SomeTrait {
  | ^^^^^^^^^^^^^^^

error[E0277]: the trait bound `i32: SomeTrait` is not satisfied in `SomeStruct`
  --> src/lib.rs:12:17
   |
12 |     fn new() -> Self {
   |                 ^^^^ within `SomeStruct`, the trait `SomeTrait` is not implemented for `i32`
   |
help: this trait has no implementations, consider adding one
  --> src/lib.rs:1:1
   |
1  | trait SomeTrait {
   | ^^^^^^^^^^^^^^^
note: required because it appears within the type `SomeStruct`
  --> src/lib.rs:5:8
   |
5  | struct SomeStruct {
   |        ^^^^^^^^^^
   = note: the return type of a function must have a statically known size

error[E0277]: the trait bound `i32: SomeTrait` is not satisfied in `SomeStruct`
  --> src/lib.rs:18:16
   |
18 | fn use_type(_: SomeStruct) {}
   |                ^^^^^^^^^^ within `SomeStruct`, the trait `SomeTrait` is not implemented for `i32`
   |
help: this trait has no implementations, consider adding one
  --> src/lib.rs:1:1
   |
1  | trait SomeTrait {
   | ^^^^^^^^^^^^^^^
note: required because it appears within the type `SomeStruct`
  --> src/lib.rs:5:8
   |
5  | struct SomeStruct {
   |        ^^^^^^^^^^
help: function arguments must have a statically known size, borrowed types always have a known size
   |
18 | fn use_type(_: &SomeStruct) {}
   |                +

error[E0277]: the trait bound `i32: SomeTrait` is not satisfied
  --> src/lib.rs:12:17
   |
12 |     fn new() -> Self {
   |                 ^^^^ the trait `SomeTrait` is not implemented for `i32`
   |
help: this trait has no implementations, consider adding one
  --> src/lib.rs:1:1
   |
1  | trait SomeTrait {
   | ^^^^^^^^^^^^^^^

For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground` (lib) due to 4 previous errors

Desired output

error[E0277]: the trait bound `i32: SomeTrait` is not satisfied
 --> src/lib.rs:7:12
  |
7 |     inner: <i32 as SomeTrait>::AssocTy,
  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `SomeTrait` is not implemented for `i32`
  |
help: this trait has no implementations, consider adding one
 --> src/lib.rs:1:1
  |
1 | trait SomeTrait {
  | ^^^^^^^^^^^^^^^

Rationale and extra context

playground link

It seems that the bound-not-satisfied error is emitted on each type uses, which is noisy and does not help. I'm expecting the message is only emitted once on the struct definition ___location.

I encountered this issue in my proc-macro. My macro generates a struct InternalState { state: <UserType as MyTrait>::MyType } and also many functions using InternalState. Currently, if UserType: MyTrait is not satisfied, tons of errors are emitted from my macro with the same message, for each InternalState type I mention. I could set spans so messages are emitted on UserType, but I cannot reduce the number of errors emitted.

Other cases

Some errors suggest that rustc assumes the unknown associated type to be !Sized. They can be suppressed by adding another unit: () field at the end of SomeStruct. But bound-not-satisfied errors are still emitted on each uses.

error[E0277]: the trait bound `i32: SomeTrait` is not satisfied
 --> src/lib.rs:7:12
  |
7 |     inner: <i32 as SomeTrait>::AssocTy,
  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `SomeTrait` is not implemented for `i32`
  |
help: this trait has no implementations, consider adding one
 --> src/lib.rs:1:1
  |
1 | trait SomeTrait {
  | ^^^^^^^^^^^^^^^

error[E0277]: the trait bound `i32: SomeTrait` is not satisfied
  --> src/lib.rs:13:17
   |
13 |     fn new() -> Self {
   |                 ^^^^ the trait `SomeTrait` is not implemented for `i32`
   |
help: this trait has no implementations, consider adding one
  --> src/lib.rs:1:1
   |
1  | trait SomeTrait {
   | ^^^^^^^^^^^^^^^

error[E0277]: the trait bound `i32: SomeTrait` is not satisfied
  --> src/lib.rs:19:13
   |
19 | fn use_type(_: SomeStruct) {}
   |             ^ the trait `SomeTrait` is not implemented for `i32`
   |
help: this trait has no implementations, consider adding one
  --> src/lib.rs:1:1
   |
1  | trait SomeTrait {
   | ^^^^^^^^^^^^^^^

For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground` (lib) due to 3 previous errors

Rust Version

Current stable on playground: 1.88

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