Skip to content

Unexpected behavior with struct multicase unions #1678

Closed

Description

@cartermp

The latest F# 4.1 compiler fails to compile either of the following code programs on .NET Core:

[<Struct>]
type Foo = Bar | Baz

[<EntryPoint>]
let main argv =
    printfn "Hello World from F#!"
    0 // return an integer exit code
[<Struct>]
type Foo = 
    | Bar of int
    | Baz of int

[<EntryPoint>]
let main 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>]
type Shape =
    | Circle of radius: float
    | Square of side: double
    | Rectangle of sides: 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.

Activity

  1. changed the title [-]Unexpected behavior with multicase union structs[/-] [+]Unexpected behavior with struct multicase unions[/+] on Oct 31, 2016
  2. added
    Impact-High
    Impact-Medium
    and removed
    Impact-High
    on Nov 8, 2016
  3. neildanson commented on Nov 24, 2016

    @neildanson

    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 ?

  4. cloudRoutine commented on Nov 29, 2016

    @cloudRoutine
    Contributor

    I just ran into this issue too.

    While it shows up in the build output, it does not in the error list

  5. amongonz commented on Jan 28, 2017

    @amongonz

    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.

  6. dsyme commented on Jan 28, 2017

    @dsyme
    Contributor

    @mistiara Yes, we need to document this limitation, it is a bug in F# 4.1. We will fix it.

  7. added this to the milestone on Jan 28, 2017
  8. added
    Impact-High
    and removed
    Impact-Medium
    on Mar 14, 2017
  9. 9 remaining items

  10. cartermp commented on Jul 29, 2017

    @cartermp
    ContributorAuthor

    Hmmmm. Then it appears that @dsyme's PR didn't fix this duplicate ctor bug.

  11. KevinRansom commented on Jul 29, 2017

    @KevinRansom
    Contributor

    It's all fixed in VS2017.3
    Desktop
    image

    Coreclr

    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>
    
    
  12. cartermp commented on Jul 29, 2017

    @cartermp
    ContributorAuthor

    Does that work cross platform? I tested with the latest build of SDK 2.0 on macOS and repro'd.

  13. KevinRansom commented on Jul 30, 2017

    @KevinRansom
    Contributor

    Should work fine cross platform.

  14. KevinRansom commented on Jul 30, 2017

    @KevinRansom
    Contributor

    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

    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> ````
    
  15. zpodlovics commented on Jul 30, 2017

    @zpodlovics

    @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):

    open System
    
    [<Struct>]
    type DU =
        | Case1 of int
        | Case2 of int
    
    [<EntryPoint>]
    let main 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):

    open System
    
    [<Struct>]
    type DU =
        | Case1
        | Case2
    
    [<EntryPoint>]
    let main 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.
    
  16. cartermp commented on Jul 30, 2017

    @cartermp
    ContributorAuthor

    @KevinRansom

    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>]
    type Foo = Bar | Baz
    
    [<EntryPoint>]
    let main 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]
    
  17. dsyme commented on Aug 30, 2017

    @dsyme
    Contributor

    @cartermp I tried

    [<Struct>]
    type Foo = Bar | Baz
    

    on master and it works correctly for me

  18. modified the milestones: , .NET Core 2.0, 15.5 on Jun 9, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugImpact-High

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions