You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
FSharpValue.GetUnionFields returns empty array for non-first cases of struct unions with shared fields #18121
openMicrosoft.FSharp.Reflection[<Struct>]typeMySharedStructDu=| A ofint64| B ofint64for value in[A 1L; B 2L]doletcaseInfo,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.
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).
F# reflection retrieves the mapping between fields and union cases from IL attributes, fixing this will require emitting additional CompilationMapping attributes.
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) ?
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.)
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 :(( )
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.
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
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:
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?
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?
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
Activity
T-Gro commentedon 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 commentedon Dec 10, 2024
F# reflection retrieves the mapping between fields and union cases from IL attributes, fixing this will require emitting additional CompilationMapping attributes.
T-Gro commentedon Jan 24, 2025
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) ?
cmeeren commentedon Jan 24, 2025
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.)
T-Gro commentedon Jan 27, 2025
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 :(( )
cmeeren commentedon Jan 27, 2025
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:
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 projectby 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.T-Gro commentedon Jan 27, 2025
Yes, the are 2 factors in place:
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
cmeeren commentedon Jan 27, 2025
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:
T-Gro commentedon Jan 27, 2025
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
cmeeren commentedon Jan 27, 2025
Ok! So, to summarize, the bug will only be fixed if BOTH of the following are true:
Is this correct?
T-Gro commentedon Jan 27, 2025
Yes, this is correct.