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
Unexpected behavior with struct multicase unions #1678
The latest F# 4.1 compiler fails to compile either of the following code programs on .NET Core:
[<Struct>]typeFoo= Bar | Baz
[<EntryPoint>]letmain argv =
printfn "Hello World from F#!"0// return an integer exit code
[<Struct>]typeFoo=| Bar ofint| Baz ofint[<EntryPoint>]letmain argv =
printfn "Hello World from F#!"0// return an integer exit code
With this error:
FSC : error FS2014: A problem occurred writing the binary '/Users/phillip/scratch/test/obj/Debug/netcoreapp2.0/test.dll': Error in pass2 for type Program, error: Error in pass2 for type Foo, error: duplicate entry '.ctor' in method table [/Users/phillip/scratch/test/test.fsproj]
This is one rel/2.0.0 of the .NET CLI.
--- Old Issue ---
In VS "15" Preview 5, if you have the following:
[<Struct>]typeShape=| Circle ofradius:float| Square ofside:double| Rectangle ofsides:double*double
The following error occurs:
Error in pass2 for type Program, error: Error in pass2 for type Shape, error: duplicate entry '.ctor' in method table
If I remove the Square case , or change it to be of type int, it compiles.
You also get the same in the case with something like this
[<Struct>]
type DU = A | B
I've not taken a look at the implementation of struct multi case unions, but I'm guessing that a unique constructor is built per signature of each case? That would explain why it fails for cases with duplicate signatures ?
I just installed F# support for the latest VS2017 RC and hit this issue. It fails every time two or more cases have the same type.
Inspecting the IL emitted for struct unions I see a private constructor for each case. I think the easiest fix would be having a single constructor, with a parameter for every field.
C:\temp\acme>dotnet --info
.NET Command Line Tools (1.1.0)
Product Information:
Version: 1.1.0
Commit SHA-1 hash: d6f4336106
Runtime Environment:
OS Name: Windows
OS Version: 10.0.14393
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\1.1.0
C:\temp\acme>type Program.fs
// Learn more about F# at http://fsharp.org
open System
[<Struct>]
type Shape =
| Circle of radius: float
| Square of side: double
| Rectangle of sides: double*double
[<EntryPoint>]
let main argv =
printfn "Hello World from F#!"
0 // return an integer exit code
C:\temp\acme>dotnet restore
Restore completed in 43.16 ms for C:\temp\acme\acme.fsproj.
C:\temp\acme>dotnet build
Microsoft (R) Build Engine version 15.3.409.57025 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.
acme -> C:\temp\acme\bin\Debug\netcoreapp1.1\acme.dll
Build succeeded.
0 Warning(s)
0 Error(s)
Time Elapsed 00:00:01.05
C:\temp\acme>
You need to use release/2.0.0 branch or rel/1.1.0 for attest bits. master is a bit whacky on dotnet/cli
MacBook-Pro:/Users/kevinr/temp/one> cat Program.fs
// Learn more about F# at http://fsharp.org
open System
[<Struct>]
type Shape =
| Circle of radius: float
| Square of side: double
| Rectangle of sides: double*double
[<EntryPoint>]
let main argv =
printfn "Hello World from F#!"
0 // return an integer exit code
MacBook-Pro:/Users/kevinr/temp/one> dotnet build
Microsoft (R) Build Engine version 15.3.409.57025 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.
one -> /Users/kevinr/temp/one/bin/Debug/netcoreapp2.0/one.dll
Build succeeded.
0 Warning(s)
0 Error(s)
Time Elapsed 00:00:01.35
MacBook-Pro:/Users/kevinr/temp/one> ````
@KevinRansom The example you posted (struct du with different unique type for each case) works fine with .NET Core preview2 (2.0.0-preview2-006497) running on Ubuntu 16.04. However it seems that the struct du the typeless/parameterless cases are not handled properly in the preview2 version of the sdk.
The compiler for this example at least provide an error why the struct du is not compilable (current struct du limitation):
openSystem[<Struct>]typeDU=| Case1 ofint| Case2 ofint[<EntryPoint>]letmain argv =
printfn "Hello World from F#!"0// return an integer exit code
dotnet run -c Release
/tmp/du/Program.fs(4,6): error FS3204: If a union type has more than one case and is a struct, then all fields within the union type must be given unique names. [/tmp/du/du.fsproj]
The build failed. Please fix the build errors and run again.
However for the following case, the compiler does not give any information why the compilation fails (current struct du limitations):
openSystem[<Struct>]typeDU=| Case1
| Case2
[<EntryPoint>]letmain argv =
printfn "Hello World from F#!"0// return an integer exit code
dotnet run -c Release
FSC : error FS2014: A problem occurred writing the binary '/tmp/du/obj/Release/netcoreapp2.0/du.dll': Error in pass2 for type Program, error: Error in pass2 for type DU, error: duplicate entry '.ctor' in method table [/tmp/du/du.fsproj]
The build failed. Please fix the build errors and run again.
You need to use release/2.0.0 branch or rel/1.1.0 for attest bits
That's what I'm using. On rel/2.0.0:
[<Struct>]typeFoo= Bar | Baz
[<EntryPoint>]letmain argv =
printfn "Hello World from F#!"0// return an integer exit code
Fails:
FSC : error FS2014: A problem occurred writing the binary '/Users/phillip/scratch/test/obj/Debug/netcoreapp2.0/test.dll': Error in pass2 for type Program, error: Error in pass2 for type Foo, error: duplicate entry '.ctor' in method table [/Users/phillip/scratch/test/test.fsproj]
Activity
[-]Unexpected behavior with multicase union structs[/-][+]Unexpected behavior with struct multicase unions[/+]neildanson commentedon Nov 24, 2016
You also get the same in the case with something like this
I've not taken a look at the implementation of struct multi case unions, but I'm guessing that a unique constructor is built per signature of each case? That would explain why it fails for cases with duplicate signatures ?
cloudRoutine commentedon Nov 29, 2016
I just ran into this issue too.
While it shows up in the build output, it does not in the error list
amongonz commentedon Jan 28, 2017
I just installed F# support for the latest VS2017 RC and hit this issue. It fails every time two or more cases have the same type.
Inspecting the IL emitted for struct unions I see a private constructor for each case. I think the easiest fix would be having a single constructor, with a parameter for every field.
dsyme commentedon Jan 28, 2017
@mistiara Yes, we need to document this limitation, it is a bug in F# 4.1. We will fix it.
9 remaining items
cartermp commentedon Jul 29, 2017
Hmmmm. Then it appears that @dsyme's PR didn't fix this duplicate ctor bug.
KevinRansom commentedon Jul 29, 2017
It's all fixed in VS2017.3

Desktop
Coreclr
cartermp commentedon Jul 29, 2017
Does that work cross platform? I tested with the latest build of SDK 2.0 on macOS and repro'd.
KevinRansom commentedon Jul 30, 2017
Should work fine cross platform.
KevinRansom commentedon Jul 30, 2017
Works on MAC-OS with the latest bits.
You need to use release/2.0.0 branch or rel/1.1.0 for attest bits. master is a bit whacky on dotnet/cli
zpodlovics commentedon Jul 30, 2017
@KevinRansom The example you posted (struct du with different unique type for each case) works fine with .NET Core preview2 (2.0.0-preview2-006497) running on Ubuntu 16.04. However it seems that the struct du the typeless/parameterless cases are not handled properly in the preview2 version of the sdk.
The compiler for this example at least provide an error why the struct du is not compilable (current struct du limitation):
However for the following case, the compiler does not give any information why the compilation fails (current struct du limitations):
cartermp commentedon Jul 30, 2017
@KevinRansom
That's what I'm using. On rel/2.0.0:
Fails:
dsyme commentedon Aug 30, 2017
@cartermp I tried
on master and it works correctly for me