Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jack-pappas
Created December 19, 2016 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jack-pappas/339d3ad4e5e45d7184a5c66d2ca2c5f7 to your computer and use it in GitHub Desktop.
Save jack-pappas/339d3ad4e5e45d7184a5c66d2ca2c5f7 to your computer and use it in GitHub Desktop.
Repro for type inference bug (or non-specific compiler error message) related to use of byref
namespace FSharpByrefTypeInferBug
open System.Runtime.InteropServices
#nowarn "9"
type LONG = int32
[<AutoOpen>]
module NativeMethods =
//
[<Struct; StructLayout(LayoutKind.Sequential)>]
type RECT =
val mutable public left : LONG
val mutable public top : LONG
val mutable public right : LONG
val mutable public bottom : LONG
[<DllImport("foo.dll")>]
extern [<MarshalAs(UnmanagedType.Bool)>] bool FooBar (nativeint xyz, RECT& rect)
type Class1() =
member private __.MyMethod (r : byref<RECT>) =
printfn "Top: %i" r.top
()
member this.DoStuff () =
let mutable myRect = Unchecked.defaultof<RECT>
if FooBar (1234n, &myRect) then
#if ENABLE_WORKAROUND
// Adding parentheses around the method argument fixes
// the type resolution for `&myRect`` and allows the
// program to compile/run as expected.
this.MyMethod (&myRect)
#else
// Even though the type of `myRect` is known above, and the compiler
// infers the type of `&myRect` correctly when calling the FooBar function,
// attempting to call `MyMethod` with the same argument results in a
// compiler error:
// error FS0001: This expression was expected to have type RECT ref but here has type byref<'a>
this.MyMethod &myRect
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment