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

suggestion to use generic parameter in case of "error[E0782]: trait objects must include the dyn keyword" #125139

Open
p-alik opened this issue May 15, 2024 · 2 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. A-trait-objects Area: trait objects, vtable layout T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@p-alik
Copy link

p-alik commented May 15, 2024

Code

trait StateTrait {
  fn handle_state(&self);
}

trait StateView: Sized {
  fn view(_: &StateTrait) -> Vec<Self>;
}

fn main() {}

Current output

error[E0782]: trait objects must include the `dyn` keyword
 --> src/lib.rs:7:15
  |
7 |   fn view(_: &StateTrait) -> Vec<Self>;
  |               ^^^^^^^^^^
  |
help: add `dyn` keyword before this trait
  |
7 |   fn view(_: &dyn StateTrait) -> Vec<Self>;
  |               +++

error: aborting due to previous error

For more information about this error, try `rustc --explain E0782`.

Desired output

error[E0782]: trait objects must include the `dyn` keyword for dynamic dispatch
 --> src/lib.rs:7:15
  |
7 |   fn view(_: &StateTrait) -> Vec<Self>;
  |               ^^^^^^^^^^
  |
help: add `dyn` keyword before this trait
  |
7 |   fn view(_: &dyn StateTrait) -> Vec<Self>;
  |               +++

error: aborting due to previous error

For more information about this error, try `rustc --explain E0782`.


error[????]: use a generic parameter for the trait objects for static dispatch
 --> src/lib.rs:7:15
  |
7 |   fn view(_: &StateTrait) -> Vec<Self>;
  |               ^^^^^^^^^^
  |
help: add `dyn` keyword before this trait
  |
7 |   fn view<S: StateTrait>(_: &S) -> Vec<Self>;
  |               +++

error: aborting due to previous error

For more information about this error, try `rustc --explain ????`.

Rationale and extra context

The issue was created with regard to the suggestion made in the thread https://users.rust-lang.org/t/how-can-i-avoid-dynamic-dispatch/111395
The content of Current output is the result of rustc --crate-name foo --edition=2021 src/lib.rs

Other cases

No response

Rust Version

$ rustc --version --verbose
rustc 1.74.1 (a28077b28 2023-12-04)
binary: rustc
commit-hash: a28077b28a02b92985b3a3faecf92813155f1ea1
commit-date: 2023-12-04
host: x86_64-unknown-linux-gnu
release: 1.74.1
LLVM version: 17.0.4

Anything else?

No response

@p-alik p-alik 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 May 15, 2024
@p-alik p-alik changed the title error[E0782]: trait objects must include the dyn keyword suggestion to use generic parameter in case of "error[E0782]: trait objects must include the dyn keyword" May 15, 2024
@ScratchCat458
Copy link

Help text could be combined instead of throwing a separate error to increase readability and show association between suggestions.

error[E0782]: trait objects must include the `dyn` keyword for dynamic dispatch
 --> src/lib.rs:7:15
  |
7 |   fn view(_: &StateTrait) -> Vec<Self>;
  |               ^^^^^^^^^^
  |
help: add `dyn` keyword before this trait
  |
7 |   fn view(_: &dyn StateTrait) -> Vec<Self>;
  |               +++
help: or use a generic parameter for static dispatch
  |
7 |   fn view<S: StateTrait>(_: &S) -> Vec<Self>;
  |          +++++++++++++++

error: aborting due to previous error

For more information about this error, try `rustc --explain E0782`.

@fmease fmease added A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. A-trait-objects Area: trait objects, vtable layout labels May 15, 2024
@fmease
Copy link
Member

fmease commented May 15, 2024

Huh, I think we did suggest that at some point (well, at least suggesting adding impl next to existing dyn suggestion).

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 A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. A-trait-objects Area: trait objects, vtable layout T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants