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

FSharpValue.GetUnionFields returns empty array for non-first cases of struct unions with shared fields #18121

Open
cmeeren opened this issue Dec 9, 2024 · 2 comments
Labels
Area-Compiler-CodeGen IlxGen, ilwrite and things at the backend Bug Impact-Medium (Internal MS Team use only) Describes an issue with moderate impact on existing code. Regression
Milestone

Comments

@cmeeren
Copy link
Contributor

cmeeren commented Dec 9, 2024

In F# 9, FSharpValue.GetUnionFields only returns fields for the first case of struct unions with shared fields.

Repro: Repro.zip

open Microsoft.FSharp.Reflection

[<Struct>]
type MySharedStructDu =
    | A of int64
    | B of int64

for value in [A 1L; B 2L] do
    let caseInfo, inner = FSharpValue.GetUnionFields(value, typeof<MySharedStructDu>)
    printfn $"Inner value for case %s{caseInfo.Name} is %A{inner}"

Expected behavior

Inner value for case A is [|1L|]
Inner value for case B is [|2L|]

Actual behavior

Inner value for case A is [|1L|]
Inner value for case B is [||]

Known workarounds

I don't know of any, though to be fair I haven't looked for any either.

@github-actions github-actions bot added this to the Backlog milestone Dec 9, 2024
@T-Gro T-Gro added Regression Impact-Medium (Internal MS Team use only) Describes an issue with moderate impact on existing code. Area-Compiler-CodeGen IlxGen, ilwrite and things at the backend and removed Needs-Triage Regression labels Dec 9, 2024
@T-Gro
Copy link
Member

T-Gro commented Dec 10, 2024

Thank you for spotting this @cmeeren .
The workaround would be to provide two distinct names to the int64 fields, i.e. reverting to pre-9 behavior (and not making use of the field sharing).

@T-Gro
Copy link
Member

T-Gro commented Dec 10, 2024

F# reflection retrieves the mapping between fields and union cases from IL attributes, fixing this will require emitting additional CompilationMapping attributes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compiler-CodeGen IlxGen, ilwrite and things at the backend Bug Impact-Medium (Internal MS Team use only) Describes an issue with moderate impact on existing code. Regression
Projects
Status: New
Development

No branches or pull requests

2 participants