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

Enum variants are suggested in RHS of match arm #12264

Closed
jonas-schievink opened this issue May 15, 2022 · 3 comments · Fixed by #12309
Closed

Enum variants are suggested in RHS of match arm #12264

jonas-schievink opened this issue May 15, 2022 · 3 comments · Fixed by #12309
Labels
A-completion autocompletion C-bug Category: bug S-actionable Someone could pick this issue up and work on it right now

Comments

@jonas-schievink
Copy link
Contributor

enum MyEnum {
    VariantA,
    VariantB,
}

fn f(e: MyEnum) -> Option<u8> {
    match e {
        MyEnum::VariantA => Some(0),
        MyEnum::VariantB => $0
    }
}

We suggest both MyEnum::VariantA and MyEnum::VariantB here, but they don't really make any sense, since the expected type is Option<u8>.

@jonas-schievink jonas-schievink added A-completion autocompletion S-actionable Someone could pick this issue up and work on it right now C-bug Category: bug labels May 15, 2022
@jonas-schievink
Copy link
Contributor Author

Huh, apparently we don't complete VariantB here at all, I thought that was the misfiring completion:

enum MyEnum {
    VariantA,
    VariantB,
}

fn f(e: MyEnum) -> Option<u8> {
    match e {
        MyEnum::VariantA => Some(0),
        Varia$0
    }
}

@jonas-schievink
Copy link
Contributor Author

Yeah the original issue was just that this code was triggering because for some reason ctx.expected_type is MyEnum there instead of the correct Option<u8>:

if let Some(hir::Adt::Enum(e)) =
ctx.expected_type.as_ref().and_then(|ty| ty.strip_references().as_adt())
{
super::enum_variants_with_paths(acc, ctx, e, |acc, ctx, variant, path| {
acc.add_qualified_enum_variant(ctx, variant, path)
});

@flodiebold
Copy link
Member

Yeah, I actually looked into this a bit when you posted it first, should have written some notes. We get the wrong expected type because we treat this like the match e { $0 } case. It wasn't super trivial to fix because in match e { Variant => $0 }, the cursor is not 'in' the match arm. I'm a bit rusty on handling syntax trees though, so maybe there's some nice easy way to detect this case 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-completion autocompletion C-bug Category: bug S-actionable Someone could pick this issue up and work on it right now
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants