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

Closed
p-alik opened this issue May 15, 2024 · 3 comments · Fixed by #127692
Assignees
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).

@veera-sivarajan
Copy link
Contributor

@rustbot claim

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Sep 2, 2024
…stebank

Suggest `impl Trait` for References to Bare Trait in Function Header

Fixes rust-lang#125139

This PR suggests `impl Trait` when `&Trait` is found as a function parameter type or return type. This makes use of existing diagnostics by adding `peel_refs()` when checking for type equality.

Additionaly, it makes a few other improvements:
1. Checks if functions inside impl blocks have bare trait in their headers.
2. Introduces a trait `NextLifetimeParamName` similar to the existing `NextTypeParamName` for suggesting a lifetime name. Also, abstracts out the common logic between the two trait impls.

### Related Issues
I ran into a bunch of related diagnostic issues but couldn't fix them within the scope of this PR. So, I have created the following issues:
1. [Misleading Suggestion when Returning a Reference to a Bare Trait from a Function](rust-lang#127689)
2. [Verbose Error When a Function Takes a Bare Trait as Parameter](rust-lang#127690)
3. [Incorrect Suggestion when Returning a Bare Trait from a Function](rust-lang#127691)

r​? `@estebank` since you implemented  rust-lang#119148
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Sep 3, 2024
…stebank

Suggest `impl Trait` for References to Bare Trait in Function Header

Fixes rust-lang#125139

This PR suggests `impl Trait` when `&Trait` is found as a function parameter type or return type. This makes use of existing diagnostics by adding `peel_refs()` when checking for type equality.

Additionaly, it makes a few other improvements:
1. Checks if functions inside impl blocks have bare trait in their headers.
2. Introduces a trait `NextLifetimeParamName` similar to the existing `NextTypeParamName` for suggesting a lifetime name. Also, abstracts out the common logic between the two trait impls.

### Related Issues
I ran into a bunch of related diagnostic issues but couldn't fix them within the scope of this PR. So, I have created the following issues:
1. [Misleading Suggestion when Returning a Reference to a Bare Trait from a Function](rust-lang#127689)
2. [Verbose Error When a Function Takes a Bare Trait as Parameter](rust-lang#127690)
3. [Incorrect Suggestion when Returning a Bare Trait from a Function](rust-lang#127691)

r​? ``@estebank`` since you implemented  rust-lang#119148
@bors bors closed this as completed in f75a195 Sep 5, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Sep 5, 2024
Rollup merge of rust-lang#127692 - veera-sivarajan:bugfix-125139, r=estebank

Suggest `impl Trait` for References to Bare Trait in Function Header

Fixes rust-lang#125139

This PR suggests `impl Trait` when `&Trait` is found as a function parameter type or return type. This makes use of existing diagnostics by adding `peel_refs()` when checking for type equality.

Additionaly, it makes a few other improvements:
1. Checks if functions inside impl blocks have bare trait in their headers.
2. Introduces a trait `NextLifetimeParamName` similar to the existing `NextTypeParamName` for suggesting a lifetime name. Also, abstracts out the common logic between the two trait impls.

### Related Issues
I ran into a bunch of related diagnostic issues but couldn't fix them within the scope of this PR. So, I have created the following issues:
1. [Misleading Suggestion when Returning a Reference to a Bare Trait from a Function](rust-lang#127689)
2. [Verbose Error When a Function Takes a Bare Trait as Parameter](rust-lang#127690)
3. [Incorrect Suggestion when Returning a Bare Trait from a Function](rust-lang#127691)

r​? ```@estebank``` since you implemented  rust-lang#119148
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

Successfully merging a pull request may close this issue.

4 participants