Skip to content

Keep a delegate closure when its receiver uses a peeled binding - #21033

Draft
majocha wants to merge 2 commits into
dotnet:mainfrom
majocha:fix-21032-direct-delegate-receiver
Draft

majocha wants to merge 2 commits into
dotnet:mainfrom
majocha:fix-21032-direct-delegate-receiver

Conversation

@majocha

@majocha majocha commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Fixes #21032

Description

The compiler reports FS0971 "Undefined value" for this code in Release builds with F# 11:

let call (f: unit -> unit) : unit = f ()

let mk () =
    System.Action(fun () ->
        let conv (a, b, c, d) = a + b + c + d
        call (fun () -> printfn "%d" (conv (1, 2, 3, 4))))

The direct-delegate recognizer (DirectDelegateConstruction, #19993) removes side-effect-free let bindings from the delegate body to find the forwarded call. Here, it finds call (closure) and uses closure as the delegate Target, which is evaluated at the construction site. The closure uses conv, but the construction site does not bind conv, so code generation reports FS0971.

Fix

matchForwarding now rejects a leading argument that uses a value that the recognizer removed. The delegate then stays a closure. The optimizer and IlxGen both use this function, so they make the same decision.

Tests

  • New test: Issue 21032 - receiver capturing a local of the delegate body compiles (preview) in EmittedIL/DirectDelegates/DirectDelegates.fs. Without the fix, its source fails with FS0971.
  • All DirectDelegates tests pass in Release on net11.0 and net472. No IL baseline changed.

Checklist

  • Test cases added
  • Release notes entry updated

🤖 Generated with Claude Code

The direct-delegate recognizer removes side-effect-free let bindings from
the delegate body to find the forwarded call. A leading argument of that
call becomes the delegate Target, which is evaluated at the construction
site. When that argument used a removed binding, for example a closure over
a local function of the delegate body, code generation reported FS0971
"Undefined value".

matchForwarding now rejects a leading argument that uses a removed binding.
The delegate then stays a closure.

Fixes dotnet#21032

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: New

Development

Successfully merging this pull request may close these issues.

FS0971 "Undefined value" for a local closure in a delegate lambda (regression in SDK 11.0.100)

1 participant