Skip to content

Confusing type inference error in task expression #13789

Closed

Description

@jwosty

Not sure whether or not this is a bug, or should be a feature request, but I ran into this and found it incredibly confusing.

It seems that the task CE produces certain kinds of typing errors on the wrong lines, where the equivalent async expression produces no errors at all.

Repro Steps

Compile the following code:

open System.Threading.Tasks
open System.Collections.Generic
open System.Linq
let f () : Task<IList<string>> = task {
    let! x = task { return 42 }
    let! y = task { return 43 }
    return Seq.empty.ToList()
}

Expected behavior

This code should either compile with no problems, or at least produce a compiler error on the return line (as I understand that usually F# won't automatically upcast Task<List<string>> to Task<IList<string>>).

Actual behavior

A type error is emitted on the first let! line, which is NOT the actual source of the typing error:

      let! x = task { return 42 }
  ----^^^^^^^^^^^^^^^^^^^^^^^^^^^

/stdin(5,5): error FS0193: Type constraint mismatch. The type 
    'TaskCode<List<'a>,List<'a>>'    
is not compatible with type
    'TaskCode<IList<string>,IList<string>>'

This is particularly confusing when there's a lot of code between the first let! and the final return and/or if the let! is a very complex expression, as it leads you to look in the entirely wrong place for the problem.

Known workarounds

Add an extra annotation on the return line, like so:

open System.Threading.Tasks
open System.Collections.Generic
open System.Linq
let f () : Task<IList<string>> = task {
    let! x = task { return 42 }
    let! y = task { return 43 }
    return (Seq.empty.ToList() : IList<string>)
}

Related Information

It should be noted that this does not occur in equivalent async expressions:

open System.Threading.Tasks
open System.Collections.Generic
open System.Linq
let g () : Async<IList<string>> = async {
    let! x = async { return 42 }
    let! y = async { return 43 }
    return Seq.empty.ToList()
}

Activity

  1. jwosty commented on Aug 26, 2022

    @jwosty
    ContributorAuthor

    Potentially related to #12929

  2. added
    Area-Compiler-StateMachines
    on Aug 26, 2022
  3. added this to the Backlog milestone on Sep 9, 2022
  4. added
    Area-Diagnostics
    and removed
    Area-Compiler-StateMachines
    on Sep 23, 2022
  5. TheAngryByrd commented on Jun 14, 2023

    @TheAngryByrd
    Contributor

    I ended up hitting this yesterday while converting older task CE's to the F# built in one.

    Another example:

            // Works
            let foo () : int64 = 6
            let otherAsync () = async { return "lol"}
            let fooAsync () : Async<int64> = async { 
                let! _ = otherAsync ()
                return 6 
            }
            // Doesn't work
            //Type constraint mismatch. The type 
            //     'TaskCode<int,int>'    
            // is not compatible with type
            //     'TaskCode<int64,int64>'  
            let otherTask = task { return "lol"}
            let fooTask () : Task<int64> = task { 
                    let! _ = otherTask ()
                    return 6 
                }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-DiagnosticsFeature Improvement

    Type

    No type

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions