Duplicated "the trait bound [..] is not satisfied" errors when a struct definition uses a unsatisfied bound
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
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
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the duplicated E0277 diagnostics with the linked Rust Playground example on stable 1.88. Trace the compiler diagnostic path for the struct definition and its later uses, then add a regression test showing that only the definition-site error remains. Verify the repeated diagnostics are absent while the primary error is preserved.
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
- 48/100