dotnet / dotnet/fsharp

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

Open
#13,674 0 comments 1 reaction 0 assignees View on GitHub
Area-Compiler-SRTP Feature Request Needs-RFC
Dominant language
F#
Stars
4.3k
Forks
876
Avg merge
4d 11h
Merged PRs (30d)
131

Description

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

Code examples ([link to tests](https://github.com/dotnet/fsharp/blob/0973fd35f8e5dbe593c5ed9ac6bfa9610288611c/tests/FSharp.Compiler.ComponentTests/Conformance/TypesAndTypeConstraints/IWSAMsAndSRTPs/IWSAMsAndSRTPsTests.fs#L490-L520)):

```fsharp
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
```
```fsharp
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
```

**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' but here has type 'C5'`

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.