Skip to content

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

Description

@rukai

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?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsD-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.D-papercutDiagnostics: An error or lint that needs small tweaks.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