Skip to content

"Type doesn't implement trait" diagnostic is infectious #144074

@purplesyringa

Description

@purplesyringa

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

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-trait-systemArea: Trait systemD-papercutDiagnostics: An error or lint that needs small tweaks.D-verboseDiagnostics: Too much output caused by a single piece of incorrect code.T-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