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.93k stars 786 forks source link

`use` binding for `IAsyncDisposable` not supported in F# interactive #14454

Open pedersorensen opened 1 year ago

pedersorensen commented 1 year ago

task computation expressions do not support use or use! bindings for the IAsyncDisposable interface when used in F# interactive.

Repro steps

Make a type that implements IAsyncDisposable and bind it with a use or use! within a task CE:

open System
open System.Threading.Tasks

let asyncDisposable =
  { new IAsyncDisposable with
      member _.DisposeAsync() =
        printfn "Disposing async"
        ValueTask() }

let t = task {
  use d = asyncDisposable
  return ()
}

Expected behavior

The code should compile.

Actual behavior

Error message in both the editor and F# interactive output window

image

>> 

Script1.fsx(11,11): error FS0001: The type 'IAsyncDisposable' is not compatible with the type 'IDisposable'

Known workarounds

Use a let binding and call DisposeAsync explicitly, although this is not possible within a finally block.

Related information

Compiles and runs without issue in regular .fs files.

>> dotnet --info
.NET SDK:
 Version:   7.0.100
 Commit:    e12b7af219

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.22621
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\7.0.100\

Host:
  Version:      7.0.0
  Architecture: x64
  Commit:       d099f075e4

.NET SDKs installed:
  7.0.100 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 7.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 7.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 7.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
vzarytovskii commented 1 year ago

It seems that ns2.0 version is loaded (well, and packed together with fsi.dll):

> typeof<TaskBuilder>.Assembly.Location;;
val it: string =
  "C:\Program Files\dotnet\sdk\7.0.100-preview.7.22377.5\FSharp\FSharp.Core.dll"

> typeof<TaskBuilder>.Assembly.CustomAttributes;;
val it:
  System.Collections.Generic.IEnumerable<System.Reflection.CustomAttributeData>
= seq
    [ 
     ...
     [System.Runtime.Versioning.TargetFrameworkAttribute(".NETStandard,Version=v2.0", FrameworkDisplayName = "")]
      ... 
    ]

cc @KevinRansom

T-Gro commented 1 year ago

Do we have any reasons for not shipping fsi.dll with the netstandard2.1 Fsharp.Core reference?