dotnet / dotnet/fsharp

SRTP resolution uses wrong witness, allows unreachable code reached

Open
#13,537 4 comments 1 reaction 0 assignees View on GitHub
Area-Compiler-SRTP Bug Impact-Low
Dominant language
F#
Stars
4.3k
Forks
876
Avg merge
4d 22h
Merged PRs (30d)
144

Description

Using the `CompilerMessage` attribute is a technique I use a lot and normally it works, in order to give customized compile error messages. Unfortunately there are some isolated cases where the code is reached, despite the attribute.

**Repro steps**

```fsharp
type Default2 = class end
type Default1 = class inherit Default2 end

type T =
inherit Default1

[]
static member M (_: unit -> '``Monad<'T>`` , _: Default2) = invalidOp "this code is unreachable"

static member inline M (x: unit -> 'R , _: Default1) = (^R : (static member M : _->_) x) : 'R
static member inline M (_: unit -> ^t when ^t: null and ^t: struct, _: Default1) = id
static member M (_: unit -> list<_> , _: T ) = []

static member inline Invoke (source: unit ->'R) : 'R =
let inline call (mthd: 'M, input: unit -> 'I, _output: 'R) = ((^M or ^I) : (static member M : _*_ -> _) input, mthd)
call (Unchecked.defaultof, source, Unchecked.defaultof<'R>)

let f () = T.Invoke (fun () -> [1])
```
**Expected behavior**

The above code should not compile.

**Actual behavior**

It compiles. Then when calling `f ()` you get the exception **at runtime** which in theory should be unreachable.

**Known workarounds**

None that I know.

**Related information**

Tested in F# 6.0

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.