Skip to content

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

Closed
@p-alik

Description

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

Metadata

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`A-trait-objectsArea: trait objects, vtable layoutT-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