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

Incorrect inference of match expression result #3046

Closed
0x7CFE opened this issue Feb 7, 2020 · 12 comments
Closed

Incorrect inference of match expression result #3046

0x7CFE opened this issue Feb 7, 2020 · 12 comments
Labels
A-ty type system / type inference / traits / method resolution

Comments

@0x7CFE
Copy link

0x7CFE commented Feb 7, 2020

commit 5aba5a756a19a54d5c4edd51d8055db36182688b
Merge: a4431e830 56e3fbe58
Author: bors[bot] <26634292+bors[bot]@users.noreply.github.com>
Date:   Thu Feb 6 23:00:07 2020 +0000

    Merge #3042

The following snippet from wasmtime repo is mistakenly inferred as ! instead of FrameUnwindKind:

image

Update: aside of local discussion about known issue with local imports, this issue boils down to

let x = match 0 {
    _ => y,
};

…where x is inferred as ! when it probably should be {unknown}.

@lnicola
Copy link
Member

lnicola commented Feb 7, 2020

The following works:

enum E {
    A,
    B,
}

fn foo(e: E) -> E {
    let x = match e {
        E::A => 1,
        _ => {
            return E::B;
        }
    };
    E::A
}

Does "go to definition" work for CallConv and FrameUnwindKind?

@0x7CFE
Copy link
Author

0x7CFE commented Feb 7, 2020

Update: if I move corresponding use imports to the topmost scope it all starts working as expected:

image

@0x7CFE
Copy link
Author

0x7CFE commented Feb 7, 2020

Does "go to definition" work for CallConv and FrameUnwindKind?

Nope, it wasn't working before the import change. As you may see, on the first screenshot CallConv and FrameUnwindKind are not highlighted as types.

@lnicola
Copy link
Member

lnicola commented Feb 7, 2020

Yeah, use in local scopes doesn't work too great: #1165.

@0x7CFE
Copy link
Author

0x7CFE commented Feb 7, 2020

Yeah, use in local scopes doesn't work too great: #1165.

Oh, indeed. And @matklad's reply there states that probably this will not be resolved anytime soon. That's unfortunate :(

@lnicola
Copy link
Member

lnicola commented Feb 7, 2020

Still, you could argue that the inference should work better. I think the issue is that FrameUnwindKind can't be resolved. The type of match should probably be {unknown}, not !.

It's probably a bug in #1945, but I don't really understand the code 🐱.

@0x7CFE
Copy link
Author

0x7CFE commented Feb 7, 2020

The type of match should probably be {unknown}, not !.

Agree. It's very strange that type is inferred that way given that analyzer clearly does not have enough information.

@lnicola
Copy link
Member

lnicola commented Feb 7, 2020

let x = match 0 {
    _ => y,
};

x gets inferred as !.

@lnicola
Copy link
Member

lnicola commented Feb 7, 2020

Maybe you can update the first post with my example above, since it's simpler?

@flodiebold flodiebold added the A-ty type system / type inference / traits / method resolution label Feb 7, 2020
@flodiebold
Copy link
Member

#3114 fixes the reduced case where there are no match arms that diverge, but if there's a diverging match arm (like in the original case), we'll still fall back to !. Interestingly, rustc seems to fall back to () in at least some of these cases. This will require some further thought 🤔

@0x7CFE
Copy link
Author

0x7CFE commented Feb 18, 2020

@flodiebold, as for the () issue recent reddit post discusses similar behavior.

@lnicola
Copy link
Member

lnicola commented May 23, 2020

Type inference works fine now for:

enum E {
    A,
    B,
}

fn foo(e: E) -> E {
    let x = match e {
        E::A => 1,
        _ => {
            return E::B;
        }
    };
    E::A
}

I think this can be closed.

@lnicola lnicola closed this as completed May 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ty type system / type inference / traits / method resolution
Projects
None yet
Development

No branches or pull requests

3 participants