Skip to content

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

Closed

Description

@cmeeren

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.

Activity

  1. added this to the Backlog milestone on Dec 9, 2024
  2. added
    Impact-Medium
    Area-Compiler-CodeGen
    and removed on Dec 9, 2024
  3. T-Gro commented on Dec 10, 2024

    @T-Gro
    Member

    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).

  4. T-Gro commented on Dec 10, 2024

    @T-Gro
    Member

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

  5. self-assigned this
    on Jan 7, 2025
  6. modified the milestones: Backlog, January 2025 on Jan 7, 2025
  7. T-Gro commented on Jan 24, 2025

    @T-Gro
    Member

    I have researched the possibilities and one constellation which bothers me is newly compiled F# 9 code using shared fields struct DUs, and projects with old FSharp.Core reading it via reflection.

    Right now, such projects get wrong results.

    If I change the attribute emission in the compiler, I can teach fresh Fsharp.Core to understand it and be correct.
    But if I do that, if such newly-compiled unions is read with old FSharp.Core using reflection, it will fail with an exception (saying something like Multiple CompilationMappingAttributes, expected at most one.

    I can put in more complexity so that old FSharp.Core does not get an error, but still sees incorrect result like your issue shows - is that worth anything?
    Is there a big value in incorrect results over a runtime exception (or vice versa) ?

  8. cmeeren commented on Jan 24, 2025

    @cmeeren
    ContributorAuthor

    Thanks for looking into this!

    Unsure what you mean by "newly compiled F# 9" code". Do you mean code that references FSharp.Core 9, or code compiled with .NET SDK 9 regardless of the target FSharp.Core version?

    I have a library targeting an old FSharp.Core version. This is a deliberate decision, made to maximize compatibility. However, I have no problem re-compiling (with a recent .NET SDK) and re-releasing an otherwise identical package, if that would fix the issue.

    The distinction between runtime exceptions vs. incorrect results doesn't matter to me, because my library code will throw at that point anyway.

    For a fix to help me, I must see correct results when calling this code from a library targeting an old FSharp.Core version. (Again, requiring recompilation with a new .NET SDK and re-release of the NuGet package is OK if that helps.)

  9. T-Gro commented on Jan 27, 2025

    @T-Gro
    Member

    Thanks for the explanation @cmeeren . That might be tricky - the code responsible for reflection over unions and fields is part of FSharp.Core, and I can fix it only for new versions, not for an already shipped one.

    (I did analyze if there is any additional metadata I could sneak in to please existing FSharp.Core behavior, but it does throw an exception if 1 property has more than 1 mapping - and I cannot create a second property with the same name to please it either :(( )

  10. cmeeren commented on Jan 27, 2025

    @cmeeren
    ContributorAuthor

    Thanks! Are you saying that as long as the actual FSharp.Core version in use is 9 (or whatever new version gets the fix), then it will work? Because that would work for me.

    To be specific, I am talking about this hierarchy of references:

    - Some project
      - FSharp.Core vNext (9.x)
      - My library
        - FSharp.Core 5.0.2
    

    In the above scenario, the FSharp.Core version in use (also by my library) will be whatever is referenced by Some project, i.e., vNext (9.x).

    So if the issue would be fixed by Some project by updating their FSharp.Core dependency, and I can still target FSharp.Core 5.0.2 in my library, then that is the solution I was looking for. Sorry for not being entirely clear.

  11. T-Gro commented on Jan 27, 2025

    @T-Gro
    Member

    Yes, the are 2 factors in place:

    • SDK/Compiler used for the project containing the DU. That one must contain the fix (to be written yet) and be new. This will be new codegen of attributes when your DU is compiled.
    • Actual version of FSharp.Core library used at runtime

    What does not matter is FSharp.Core version used during compilation or for intermediate libraries.
    If the result app (some project) uses new fslib at runtime, your scenario is covered.

    => If that holds, a fix is possible and I will be on it

  12. cmeeren commented on Jan 27, 2025

    @cmeeren
    ContributorAuthor

    That's great!

    I'm a bit unclear on the requirements for the fix, though. Is either of your bullet points sufficient? Or are both required?

    In particular:

    1. For users hit by this issue when using my library with a DU that is defined in code they compile: Must they update the runtime FSharp.Core version? Must they recompile the project with the DU using a new SDK/compiler?
    2. For users hit by this issue when using my library with a DU that is defined in an assembly/package they do not compile: Will this be fixable at all? Is it sufficient to update the FSharp.Core version, or does it require the author of the external assembly/package to recompile using a new SDK/compiler?
  13. T-Gro commented on Jan 27, 2025

    @T-Gro
    Member

    Must they update the runtime FSharp.Core version? - yes
    Must they recompile the project with the DU using a new SDK/compiler? - yes (that is, after bugfix for this issue is implemented and released)

    Will this be fixable at all? - In general, the assembly/package defining a struct DU with shared fields will have to be recompiled

  14. cmeeren commented on Jan 27, 2025

    @cmeeren
    ContributorAuthor

    Ok! So, to summarize, the bug will only be fixed if BOTH of the following are true:

    • The entry project must use an FSharp.Core version that contains the fix
    • The project containing the DU must be compiled with an SDK/compiler that contains the fix

    Is this correct?

  15. T-Gro commented on Jan 27, 2025

    @T-Gro
    Member

    Is this correct?

    Yes, this is correct.

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

Metadata

Metadata

Assignees

Labels

Area-Compiler-CodeGenBugImpact-MediumRegression

Type

Projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions