dotnet / dotnet/fsharp

Type Inference Regression

Open
#12,929 3 comments 0 reactions 0 assignees View on GitHub
Area-Compiler-Checking Bug Impact-Medium Regression
Dominant language
F#
Stars
4.3k
Forks
876
Avg merge
4d 22h
Merged PRs (30d)
144

Description

Switching to F# 6 I found a regression in F#+ tests, which needed a type annotation as a workaround.
Then while trying to minimize the repro, the error switched to another point where no type annotation helps.

**Repro steps**

Try running this in fsi

```fsharp
open System.Threading.Tasks

type Return =
static member inline Invoke (x: 'T) : 'A =
let inline call (mthd: ^M, output: ^R) = ((^M or ^R) : (static member Return : _*_ -> _) output, mthd)
call (Unchecked.defaultof, Unchecked.defaultof<'A>) x

static member Return (_: Lazy<'a>, _: Return) = fun x -> Lazy<_>.CreateFromValue x : Lazy<'a>
static member Return (_: 'T Task , _: Return) = fun x -> Task.FromResult x : 'T Task

type Builder () =
member _.ReturnFrom (expr) = expr : Task<_>
member inline _.Return (x: 'T) = Return.Invoke x : 'A
member _.Bind (source: Task<'T>, f: 'T->Task<'U>) = source.ContinueWith(fun (t' : Task<_>) -> f (t'.Result)).Unwrap() : Task<'U>

let ce = new Builder ()

let testDefaultInferenceForReturnFrom () =
let t: Task<_> = ce { return Some "x" }
ce {
let! r = t
if r = None then
return! failwithf "Could not find x"
else
return r
}
```

**Expected behavior**

Compile ok

**Actual behavior**

It doesn't compile, it fails with

```
~vs2061.fsx(25,13): error FS0071: Type constraint mismatch when applying the default type 'Task<'a>' for a type inference variable. Type mismatch. Expecting a
'string option -> Task<'a>'
but given a
''a -> Task<'a>'
The type 'string option' does not match the type ''a' Consider adding further type constraints
```

**Known workarounds**

None

**Related information**

This works in VS 2019 but it fails in VS 2022

The original failure was detected here: https://github.com/fsprojects/FSharpPlus/pull/474/files/3760a645170cf33299775c748e5e8b9530273668#diff-1f7465fc6ec880ee491c917a3bd34dece56d34d2bfedfdd20bfd199bafd4b7ac

* Operating system : Windows
* .NET Runtime kind (.NET Core, .NET Framework, Mono)
* Editing Tools (e.g. Visual Studio Version, Visual Studio) : Visual Studio 2022

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.