Skip to content

Bad suggestions when trying to initialize an enum as a struct. #93322

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

Open
rukai opened this issue Jan 26, 2022 · 2 comments
Open

Bad suggestions when trying to initialize an enum as a struct. #93322

rukai opened this issue Jan 26, 2022 · 2 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@rukai
Copy link
Contributor

rukai commented Jan 26, 2022

Occurs in nightly-2022-01-26

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

pub type Foos = i32;

pub enum Foo {
    A,
    B,
    C,
}

mod module0 {
    pub struct Foo {
        a: i32,
    }
}
mod module1 {
    pub struct Foo {}
}
mod module2 {
    pub enum Foo {} // This one actually gets correctly filtered out
}

fn main() {
    let foo = Foo { b: 0 };
}

The output is a mess of false suggestions, I managed to hit an error that looked very similar to this while refactoring:

error[E0574]: expected struct, variant or union type, found enum `Foo`
   --> src/main.rs:22:15
    |
 1  | pub type Foos = i32;
    | -------------------- similarly named type alias `Foos` defined here
 ...
 22 |     let foo = Foo { b: 0 };
    |               ^^^
    |
 help: a type alias with a similar name exists
    |
 22 |     let foo = Foos { b: 0 };
    |               ~~~~
 help: consider importing one of these items instead
    |
 1  | use crate::module0::Foo;
    |
 1  | use crate::module1::Foo;

At a minimum we should detect that these suggestions are invalid and not suggest them:

error[E0574]: expected struct, variant or union type, found enum `Foo`
   --> src/main.rs:22:15
    |
 22 |     let foo = Foo { b: 0 };
    |               ^^^
    |

However maybe we should instead suggest that the user try to properly use Foo as an enum?

@rukai rukai 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 Jan 26, 2022
@estebank estebank added D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. D-papercut Diagnostics: An error or lint that needs small tweaks. labels Jan 27, 2022
@danobi
Copy link
Contributor

danobi commented Jul 12, 2022

@rustbot claim

@danobi
Copy link
Contributor

danobi commented Jul 17, 2022

#99357 offers a potential fix for the bad import suggestions.

Once a fix is agreed upon I'll follow up with a fix for the bad type alias suggestion.

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. D-papercut Diagnostics: An error or lint that needs small tweaks. 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

3 participants