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

E0404 suggestion is correct but E0782 is applied first, preventig autofixing the code #106861

Open
matthiaskrgr opened this issue Jan 14, 2023 · 1 comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@matthiaskrgr
Copy link
Member

Given the following code:

// Regression test of #43913.

// run-rustfix

#![feature(trait_alias)]
#![allow(bare_trait_objects, dead_code)]

type Strings = Iterator<Item=String>;

struct Struct<S: Strings>(S);
//~^ ERROR: expected trait, found type alias `Strings`

fn main() {}

The current output is:

error[E0404]: expected trait, found type alias `Strings`
  --> src/main.rs:10:18
   |
10 | struct Struct<S: Strings>(S);
   |                  ^^^^^^^ type aliases cannot be used as traits
   |
help: you might have meant to use `#![feature(trait_alias)]` instead of a `type` alias
   |
8  | trait Strings = Iterator<Item=String>;
   |

error[E0782]: trait objects must include the `dyn` keyword
 --> src/main.rs:8:16
  |
8 | type Strings = Iterator<Item=String>;
  |                ^^^^^^^^^^^^^^^^^^^^^
  |
help: add `dyn` keyword before this trait
  |
8 | type Strings = dyn Iterator<Item=String>;
  |                +++

If I only apply the E0404 suggestion, so
type Strings = Iterator<Item=String>; => trait Strings = Iterator<Item=String>; , the code works, however, when runnig rustfix, it will apply the E0782 suggestion first which results in type Strings = dyn Iterator<Item=String>; which does not fix the problem.

So the code becomes

// Regression test of #43913.

// run-rustfix

#![feature(trait_alias)]
#![allow(bare_trait_objects, dead_code)]

type Strings = dyn Iterator<Item=String>;

struct Struct<S: Strings>(S);
//~^ ERROR: expected trait, found type alias `Strings`

fn main() {}

which will cause

error[E0404]: expected trait, found type alias `Strings`
  --> src/main.rs:10:18
   |
10 | struct Struct<S: Strings>(S);
   |                  ^^^^^^^ type aliases cannot be used as traits
   |
help: you might have meant to use `#![feature(trait_alias)]` instead of a `type` alias
   |
8  | trait Strings = dyn Iterator<Item=String>;
   |

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

but

trait Strings = dyn Iterator<Item=String>;

is not the solution here:

error: invalid `dyn` keyword
 --> src/main.rs:8:17
  |
8 | trait Strings = dyn Iterator<Item=String>;
  |                 ^^^ help: remove this keyword
  |
  = help: `dyn` is only needed at the start of a trait `+`-separated list
@matthiaskrgr matthiaskrgr 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. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. labels Jan 14, 2023
@chenyukang
Copy link
Member

Maybe, we should avoid E0782 appear in this scenario?

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 D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. 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