Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using a bare trait as a field type in a struct gives subpar suggestion #135759

Closed
tdittr opened this issue Jan 20, 2025 · 1 comment · Fixed by #138042
Closed

Using a bare trait as a field type in a struct gives subpar suggestion #135759

tdittr opened this issue Jan 20, 2025 · 1 comment · Fixed by #138042
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@tdittr
Copy link
Contributor

tdittr commented Jan 20, 2025

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

@tdittr tdittr added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 20, 2025
@xizheyin
Copy link
Contributor

xizheyin commented Mar 3, 2025

@rustbot claim

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Mar 7, 2025
Suggest struct or union to add generic that impls trait

Fixes rust-lang#135759

cc `@tdittr`
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Mar 7, 2025
Suggest struct or union to add generic that impls trait

Fixes rust-lang#135759

cc ``@tdittr``
@bors bors closed this as completed in e70adad Mar 7, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Mar 7, 2025
Rollup merge of rust-lang#138042 - xizheyin:issue-135759, r=nnethercote

Suggest struct or union to add generic that impls trait

Fixes rust-lang#135759

cc ```@tdittr```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants