Fsharp: The generic type 'System.Tuple`2' was used with an invalid instantiation (likely byref issue)

Created on 22 Jul 2017  路  4Comments  路  Source: dotnet/fsharp

Repro steps

  1. Create an empty F# project on .NET Core:

    $ dotnet new console -lang F#
    $ dotnet restore
    
  2. Write the following code in Program.fs:

    open System
    open System.Runtime.InteropServices
    
    module private SetupAPI =
        // note that's not the actual signature from SetupAPI.dll, but it's already enough to crash the program
        [<DllImport("Setupapi")>]
        extern bool SetupDiGetDeviceInterfaceDetail(
            nativeint DeviceInfoSet,
            uint32& RequiredSize)
    
    let private setupDiGetDeviceInterfaceDetail args : unit =
        let result = SetupAPI.SetupDiGetDeviceInterfaceDetail args
        ignore result
    
    let sizeFromSetupDiGetDeviceInterfaceDetail (deviceInfoSet : nativeint) : uint32 =
        let mutable requiredSize = 0u
        setupDiGetDeviceInterfaceDetail(IntPtr.Zero,
                                        &requiredSize)
        requiredSize
    
    [<EntryPoint>]
    let main argv =
        let x = sizeFromSetupDiGetDeviceInterfaceDetail IntPtr.Zero
        0
    
  3. Start the program:

    $ dotnet run
    
    Unhandled Exception: System.TypeLoadException: The generic type 'System.Tuple`2' was used with an invalid instantiation in assembly 'System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'.
    at Program.setupDiGetDeviceInterfaceDetail(IntPtr args_0, UInt32& args_1)
    at Program.sizeFromSetupDiGetDeviceInterfaceDetail(IntPtr deviceInfoSet) in T:\Temp\testproject\Program.fs:line 16
    at Program.main(String[] argv) in T:\Temp\testproject\Program.fs:line 22
    

Expected behavior

The program should not crash with TypeLoadException.

Actual behavior

The program crashes.

Known workarounds

Replace setupDiGetDeviceInterfaceDetail call with SetupAPI.SetupDiGetDeviceInterfaceDetail (e.g. remove the intermediate function that passes around the argument tuple).

Related information

Probably linked with https://github.com/Microsoft/visualfsharp/issues/862. I found a similar issue https://github.com/Microsoft/visualfsharp/issues/558 but it's closed.

$ dotnet --info
.NET Command Line Tools (1.0.4)

Product Information:
 Version:            1.0.4
 Commit SHA-1 hash:  af1e6684fd

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.15063
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\1.0.4
Area-Compiler Severity-Medium bug

All 4 comments

Thanks for this report. This is another case for #862

As a workaround you can also use explicit arguments:

let private setupDiGetDeviceInterfaceDetail (x,y: byref<uint32>) : unit =
    let result = SetupAPI.SetupDiGetDeviceInterfaceDetail (x,&y)
    ignore result

@dsyme, I assume you mean you track it through #862? You closed this one (which is #3362) ;).

@abelbraaksma Yes, thanks!

Was this page helpful?
0 / 5 - 0 ratings