"Type doesn't implement trait" diagnostic is infectious
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
Code
struct Struct<T>(T);
fn assert_sized(_x: impl Sized) {}
fn f() {
assert_sized(Struct(*""));
}
Current output
error[E0277]: the size for values of type `str` cannot be known at compilation time
--> src/lib.rs:4:25
|
4 | assert_sized(Struct(*""));
| ------ ^^^ doesn't have a size known at compile-time
| |
| required by a bound introduced by this call
|
= help: the trait `Sized` is not implemented for `str`
note: required by a bound in `Struct`
--> src/lib.rs:1:15
|
1 | struct Struct<T>(T);
| ^ required by this bound in `Struct`
help: references are always `Sized`, even if they point to unsized data; consider not dereferencing the expression
|
4 - assert_sized(Struct(*""));
4 + assert_sized(Struct(""));
|
error[E0277]: the size for values of type `str` cannot be known at compilation time
--> src/lib.rs:4:18
|
4 | assert_sized(Struct(*""));
| ^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `str`
note: required by an implicit `Sized` bound in `Struct`
--> src/lib.rs:1:15
|
1 | struct Struct<T>(T);
| ^ required by the implicit `Sized` requirement on this type parameter in `Struct`
help: you could relax the implicit `Sized` bound on `T` if it were used through indirection like `&T` or `Box<T>`
--> src/lib.rs:1:15
|
1 | struct Struct<T>(T);
| ^ - ...if indirection were used here: `Box<T>`
| |
| this could be changed to `T: ?Sized`...
error[E0277]: the size for values of type `str` cannot be known at compilation time
--> src/lib.rs:4:5
|
4 | assert_sized(Struct(*""));
| ^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `str`
note: required by an implicit `Sized` bound in `Struct`
--> src/lib.rs:1:15
|
1 | struct Struct<T>(T);
| ^ required by the implicit `Sized` requirement on this type parameter in `Struct`
help: you could relax the implicit `Sized` bound on `T` if it were used through indirection like `&T` or `Box<T>`
--> src/lib.rs:1:15
|
1 | struct Struct<T>(T);
| ^ - ...if indirection were used here: `Box<T>`
| |
| this could be changed to `T: ?Sized`...
For more information about this error, try `rustc --explain E0277`.
Desired output
error[E0277]: the size for values of type `str` cannot be known at compilation time
--> src/lib.rs:4:25
|
4 | assert_sized(Struct(*""));
| ------ ^^^ doesn't have a size known at compile-time
| |
| required by a bound introduced by this call
|
= help: the trait `Sized` is not implemented for `str`
note: required by a bound in `Struct`
--> src/lib.rs:1:15
|
1 | struct Struct<T>(T);
| ^ required by this bound in `Struct`
help: references are always `Sized`, even if they point to unsized data; consider not dereferencing the expression
|
4 - assert_sized(Struct(*""));
4 + assert_sized(Struct(""));
|
For more information about this error, try `rustc --explain E0277`.
Rationale and extra context
Pointing at the bound that caused the issue is sufficient, there's no need to list all failed dependent obligations that would hold if the issue was resolved. Having so many diagnostics makes it hard to recognize the root cause.
I guess this is difficult to implement, but at least fixing this for auto traits would be great.
Other cases
// other auto traits experience the same problem
struct Struct<T: Sync>(T);
fn assert_sync<T: Sync>(x: T) -> T { x }
fn f() {
assert_sync(assert_sync(Struct(core::cell::UnsafeCell::new(()))));
}
// and non-auto traits too
trait Foo {}
struct Struct<T: Foo>(T);
impl<T: Foo> Foo for Struct<T> {}
fn assert_foo<T: Foo>(x: T) -> T { x}
fn f() {
assert_foo(assert_foo(Struct(1)));
}
Rust Version
rustc 1.90.0-nightly (a00149764 2025-07-14)
binary: rustc
commit-hash: a001497644bc229f1abcc5b2528733386591647f
commit-date: 2025-07-14
host: x86_64-unknown-linux-gnu
release: 1.90.0-nightly
LLVM version: 20.1.8
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 issue with the Rust code shown in src/lib.rs using the reported rustc version, then compare the current and desired diagnostics. Trace how the failed trait obligation produces dependent diagnostics; done means the root-cause diagnostic remains while the redundant dependent errors are omitted, including the auto-trait and non-auto-trait cases.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 32/100