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

Unable to infer type for union fields #5277

Closed
nicomem opened this issue Jul 9, 2020 · 1 comment · Fixed by #5326
Closed

Unable to infer type for union fields #5277

nicomem opened this issue Jul 9, 2020 · 1 comment · Fixed by #5326
Labels
A-ty type system / type inference / traits / method resolution E-easy E-has-instructions Issue has some instructions and pointers to code to get started good first issue

Comments

@nicomem
Copy link

nicomem commented Jul 9, 2020

rust-analyser is unable to infer type for union fields

union MyUnion {
    foo: u32,
    bar: f32,
}

unsafe fn baz(u: MyUnion) {
    let inner = u.foo;
    // inner is {unknown}
    // expected: u32
}
@bjorn3 bjorn3 added the A-ty type system / type inference / traits / method resolution label Jul 9, 2020
@flodiebold
Copy link
Member

The relevant code is here:
https://github.com/rust-analyzer/rust-analyzer/blob/5a195001d79995a6336988e18a2b78052917a02e/crates/ra_hir_ty/src/infer/expr.rs#L408-L409
It should be pretty straightforward to implement -- basically the same as for structs (in fact, it would be nice to handle structs and unions with mostly the same code there). A test should be added here.

@flodiebold flodiebold added E-easy E-has-instructions Issue has some instructions and pointers to code to get started good first issue labels Jul 10, 2020
bors bot added a commit that referenced this issue Jul 12, 2020
5326: infer: Add type inference support for Union types r=flodiebold a=otavio

This adds the type inference to Union types and add a small test case
for it, ensuring it keeps working in future.

Fixes: #5277
Signed-off-by: Otavio Salvador <otavio@ossystems.com.br>


----

#

Co-authored-by: Otavio Salvador <otavio@ossystems.com.br>
@bors bors bot closed this as completed in 9d114b9 Jul 12, 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 E-easy E-has-instructions Issue has some instructions and pointers to code to get started good first issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants