Skip to content

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

Closed

Description

@p-alik

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

Activity

  1. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on May 15, 2024
  2. 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"[/+] on May 15, 2024
  3. HaydenAtFoxHaven commented on May 15, 2024

    @HaydenAtFoxHaven

    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`.
    
  4. added
    A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`
    A-dyn-traitArea: trait objects, vtable layout
    on May 15, 2024
  5. fmease commented on May 15, 2024

    @fmease
    Member

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

  6. veera-sivarajan commented on Jul 9, 2024

    @veera-sivarajan
    Contributor

    @rustbot claim

  7. added 3 commits that reference this issue on Sep 2, 2024
    b409db4
    4b38ef6
    f75a195
  8. added a commit that references this issue on Sep 5, 2024
    325d32d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

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