fsharp icon indicating copy to clipboard operation
fsharp copied to clipboard

Trait constraints that involve byref returns can never be satisfied by any method

Open 0101 opened this issue 3 years ago • 0 comments

Not very important/common use case, but still creating an issue to keep track of it.

Code examples (link to tests):

type C5() =
    static member X(p: C5 byref) = &p

let inline callX5<'T when 'T : (static member X: 'T byref -> 'T byref)> (x: 'T byref)  = 'T.X &x
let mutable c5 = C5()
let g5 () = callX5<C5> &c5
type C6() =
    static member X(p: C6 byref) = &p

// NOTE: you can declare trait call which returns the address of the thing provided, you just can't satisfy the constraint
let inline callX6<'T when 'T : (static member X: 'T byref -> 'T byref)> (x: 'T byref)  = &'T.X &x
let mutable c6 = C6()
let g6 () = callX6<C6> &c6

Expected behavior

Code compiles and runs.

Actual behavior

Code doesn't compile and we get this error message at callX invocation: This expression was expected to have type 'byref<C5>' but here has type 'C5'

0101 avatar Aug 11 '22 13:08 0101