dotnet / fsharp

The F# compiler, F# core library, F# language service, and F# tooling integration for Visual Studio
https://dotnet.microsoft.com/languages/fsharp
MIT License
3.9k stars 783 forks source link

Type inference in task/backgroundTask CE can lead to "FS0073: internal error: Undefined or unsolved type variable" #14596

Open DunetsNM opened 1 year ago

DunetsNM commented 1 year ago

In some cases compiler is unable to resolve type variable inside backrgroundTask or task computation expressions, but manifests it in "FS0073: internal error: Undefined or unsolved type variable" on unrelated line.

Bug seems to be related only to task computation expression. It compiles fine for other monads e.g. Async

Repro steps

Try to compile following module (reproduces both in F# 6 with FSharp.Core 6.0.3, or F# 7 with FSharp.Core 7.0.0)

module UnresolvedTypeVarBug

type IMarker<'T> = interface end
type SomeAction<'T when 'T :> IMarker<'T>> = SomeAction of list<'T>
type Spec<'Action> = { Dummy: unit }

open System.Threading.Tasks

let dummyTask (_spec: Spec<'Action>) (_action: 'Action) : Task<Result<int, string>> = failwith "not implemented"

let repro (spec: Spec<SomeAction<'T>>)
    : Task<Result<unit, string>> =

    // reproduces with task or backgroundTask CE
    // compiles successfully with Async CE !
    backgroundTask {
        // bug is in next two lines
        let action = SomeAction []
        let! res = dummyTask spec action

        match res with
        | Ok _ ->
            return Ok ()
        | Error err ->
            return Error err
    }

Expected behavior

Code compiles successfully, like Intellisense suggests. Or compiler suggests to qualify type on the relevant line (see workarounds)

Actual behavior

Two identical errors emitted, one for each return line:

error FS0073: internal error: Undefined or unsolved type variable: 'T

Known workarounds

I found two:

  1. Specify type of action explicitly: let action : SomeAction<'T> = SomeAction []
  2. Inline action value into the call: let! res = dummyTask spec (SomeAction [])

Related information

Provide any related information (optional):

sonicbhoc commented 1 year ago

I am running into the same issue, but oddly enough if I compile using dotnet build I get the error above; if I build from Visual Studio I don't get an error. I've only tested with SDK version 6.0.400 though.