Skip to content

Runtime async: a consumer on LangVersion 11.0 in Debug calls a library builder without FS3350 and gets a wrong result #21001

Open

Description

@TheAngryByrd

Repro

SDK 11.0.100, FSharp.Core 11.0.101, net11.0. Two projects: a library, and a console app that references it with ProjectReference.

Library (LangVersion preview):

namespace Lib
open System.Threading.Tasks
open System.Runtime.CompilerServices
open Microsoft.FSharp.Core.CompilerServices
type B() =
    member inline _.Delay([<InlineIfLambda>] f: unit -> 'T) = f
    member inline _.Return(x) = x
    member inline _.Bind(t: Task<'T>, [<InlineIfLambda>] k: 'T -> 'U) = k (AsyncHelpers.Await t)
    member inline _.Run([<InlineIfLambda>] code: unit -> 'T) : Task<'T> = __runtimeAsyncReturn (code ())
[<AutoOpen>]
module M =
    let b = B()

Consumer (default LangVersion, so F# 11.0), Debug:

open System.Threading.Tasks
open Lib
let slow () = task { do! Task.Delay 50
                     return 20 }
type W =
    static member Work () =
        b {
            let! x = slow ()
            let! y = Task.FromResult 22
            return x + y
        }
[<EntryPoint>]
let main _ =
    printfn "result %d" (W.Work().Result)
    0

Expected

The consumer build fails with FS3350, as it does in Release:

error FS3350: Feature 'runtime async' is not available in F# 11.0. Please use language version 'PREVIEW' or greater.

Actual

In Debug, the build succeeds with no diagnostic. The consumer calls the compiled Lib.B::Run and Lib.B::Bind methods, so AsyncHelpers.Await runs outside a runtime-async method. The program prints result 1045044550 in place of result 42.

With [<NoDynamicInvocation>] on each builder member, the Debug build fails with FS3350 and FS3918, as expected.

Notes

  • The compiled non-inline copy of Bind calls AsyncHelpers.Await in a method that is not runtime async. Code generation does not reject it, because the docs treat explicitly inline bodies as templates.
  • With LangVersion preview in the consumer, Debug and Release both give 42.

This issue was written with help from an AI coding agent (automated post). I reproduced it on SDK 11.0.100 while I added runtime-async builders to IcedTasks.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions