Closed
Description
Code
trait Trait {}
struct Foo {
a: Trait,
b: u32,
}
Current output
error[E0782]: expected a type, found a trait
--> src/lib.rs:4:8
|
4 | a: Trait,
| ^^^^^
|
help: you can add the `dyn` keyword if you want a trait object
|
4 | a: dyn Trait,
| +++
For more information about this error, try `rustc --explain E0782`.
error: could not compile `trait-in-struct` (lib) due to 1 previous error
Desired output
error[E0782]: expected a type, found a trait
--> src/lib.rs:4:8
|
4 | a: Trait,
| ^^^^^
|
help: you might be missing a type parameter
|
3 | struct Foo<T: Trait> {
| +++
4 | a: T,
| ^
For more information about this error, try `rustc --explain E0782`.
error: could not compile `playground` (lib) due to 1 previous error
Rationale and extra context
Currently this just suggests using dyn
which also works if the field is the last field int the struct so can be unsized. However the suggestion is also given when the field is not in the last position, so applying the fix fails.
For most cases I think it makes more sense to either add a generic or Box the value.
Other cases
Rust Version
rustc 1.86.0-nightly (8361aef0d 2025-01-14)
binary: rustc
commit-hash: 8361aef0d7c29b1501a316a208ed84cd8a2ae5da
commit-date: 2025-01-14
host: x86_64-unknown-linux-gnu
release: 1.86.0-nightly
LLVM version: 19.1.6
Anything else?
No response