Skip to content

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

Description

@DunetsNM

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):

  • Windows
  • net6 or net7 , reproduces on latest SDK at time of report (7.0.100)

Metadata

Metadata

Assignees

No one assigned

    Labels

    AI-thinks-issue-fixedArea-Compiler-StateMachinesSequence, list, task and other state machine compilationBugImpact-Medium(Internal MS Team use only) Describes an issue with moderate impact on existing code.

    Type

    Projects

    Status
    Done

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions