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

rustc should recommend impl Trait before dyn Trait in argument and return type positions #99304

Open
Runi-c opened this issue Jul 15, 2022 · 2 comments
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

@Runi-c
Copy link

Runi-c commented Jul 15, 2022

Given the following code:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=242a6ff23679df5d4ac2f718e0ce8415

fn make_iter<T>(vec: &Vec<T>) -> Iterator<Item = &T> {
    vec.iter()
}

The current output is:

error[[E0782]](https://doc.rust-lang.org/stable/error-index.html#E0782): trait objects must include the `dyn` keyword
 --> src/lib.rs:1:34
  |
1 | fn make_iter<T>(vec: &Vec<T>) -> Iterator<Item = &T> {
  |                                  ^^^^^^^^^^^^^^^^^^^
  |
help: add `dyn` keyword before this trait
  |
1 - fn make_iter<T>(vec: &Vec<T>) -> Iterator<Item = &T> {
1 + fn make_iter<T>(vec: &Vec<T>) -> dyn Iterator<Item = &T> {
  | 

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

Ideally the output should look like:

error[[E0???]](https://doc.rust-lang.org/stable/error-index.html#E0???): bare trait invalid in this position
 --> src/lib.rs:1:34
  |
1 | fn make_iter<T>(vec: &Vec<T>) -> Iterator<Item = &T> {
  |                                  ^^^^^^^^^^^^^^^^^^^
  |
help: try adding `impl` keyword before this trait
  |
1 - fn make_iter<T>(vec: &Vec<T>) -> Iterator<Item = &T> {
1 + fn make_iter<T>(vec: &Vec<T>) -> impl Iterator<Item = &T> {
  | 

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

During my journey learning Rust, I've made this mistake a number of types, and pretty much 90% of the time the compiler error was misleading because my actual intention was an impl Trait in that position. Suggesting dyn Trait in this situation leads to confusion and, if an unwitting programmer accepts it, another error immediately following it.

I think rustc should suggest impl Trait before dyn Trait in the argument and return type positions. Suggesting dyn Trait in type argument positions such as Box<Trait> and in non-function contexts would still make sense.

@Runi-c Runi-c 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 Jul 15, 2022
@est31
Copy link
Member

est31 commented Jul 17, 2022

This should maybe be edition dependent, and maybe one needs to wait for the 2024 edition.

The old meaning of Trait was dyn Trait, and legacy code was using it with that in mind. Later, for clarity's sake, the requirement to do dyn was introduced. The requirement became lint-enforced on the 2018 edition, and error-enforced on the 2021 edition. Which means that people who have for some reason turned off/disabled lints will only see the error once they switch their edition to 2021. For this audience, the dyn suggestion is the best.

However, I agree that impl Trait is better fitting if looking at it from a historical context free perspective. In 2024, enough time has passed that historical component is less relevant. People migrating from 2018 or 2015 editions to 2024+ will probably still exist, but it will be extremely rare, and it feels to me that making the error suggestion sub-optimal is fair in that situation, given that the option of migrating first to 2021 and then to 2024+ exists.

@Runi-c
Copy link
Author

Runi-c commented Jul 17, 2022

@est31 That makes sense, I didn't know the historical context behind this error. I understand now that it serves in part to help users migrating from old Rust versions.

In that case, how about altering the error message to suggest both fixes, but featuring the impl Trait version more prominently? Something like this:

error[[E0???]](https://doc.rust-lang.org/stable/error-index.html#E0???): bare trait invalid in this position
 --> src/lib.rs:1:34
  |
1 | fn make_iter<T>(vec: &Vec<T>) -> Iterator<Item = &T> {
  |                                  ^^^^^^^^^^^^^^^^^^^
  |
help: to indicate any concrete type implementing this trait, use the `impl` keyword
  |
1 - fn make_iter<T>(vec: &Vec<T>) -> Iterator<Item = &T> {
1 + fn make_iter<T>(vec: &Vec<T>) -> impl Iterator<Item = &T> {
  | 
help: to indicate a trait object with unknown type at compile time, use the `dyn` keyword
  |
1 - fn make_iter<T>(vec: &Vec<T>) -> Iterator<Item = &T> {
1 + fn make_iter<T>(vec: &Vec<T>) -> dyn Iterator<Item = &T> {
  | 

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

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

No branches or pull requests

2 participants