Description
With tail calls on (the default in Release), the compiler emits tail. for a call that passes a Span<int> over a local, here to a delegate. The repro below reads 0 instead of 7443; Debug, and Release with tail calls off, both read 7443. The prefix permits removing the caller's frame while the span still points into it. The source uses no unsafe operations, and neither stores nor returns the span.
Repro steps
module Prog
open System
let run () : int =
let mutable x = 7443
let f = Func<Span<int>, int>(fun s -> s.[0])
f.Invoke(Span<int>(&x))
[<EntryPoint>]
let main _ =
printfn "planted=7443 read=%d" (run ())
0Build it as a console app (net10.0) and run it in Debug and in Release.
Expected behavior
planted=7443 read=7443 in both configurations, as with the equivalent C# program.
Actual behavior
| Build | Output (after planted=7443) |
tail. in run |
|---|---|---|
| Debug | read=7443 |
none |
| Release | read=0 |
before callvirt Func<Span<int32>,int32>::Invoke |
Release with <Tailcalls>false</Tailcalls> |
read=7443 |
none |
These are SDK 10.0.404 (F# 10) builds with dotnet build. A compiler built from main at c82fbeb (F# 11.2) gives the same three results with --optimize- --tailcalls-, --optimize+ --tailcalls+ and --optimize+ --tailcalls-. All were run on .NET 10.0.15.
ECMA-335, Partition III, III.2.4 (tail.), under Correctness: "no managed pointers can be passed to the method being called if they point into the stack frame that is about to be removed." A Span<T> over a local carries such a pointer.
CanTailcall in IlxGen.fs already refuses a tail call for a byref argument (hasByrefArg, via IsILTypeByref), for pinned locals (#18689) and for stack-allocated locals (#19338; see #13447). IsILTypeByref recognizes ILType.Byref, including through ILType.Modified, but not byref-like value types such as Span<T>.
Known workarounds
<Tailcalls>false</Tailcalls> (--tailcalls-).
Related information
- Tested only on Linux x64 with .NET 10.0.15. The discussion in Extra tail instruction corrupts stack? #13447 explains how whether such a bug shows can depend on the ABI and the tail-call sequence.
- Related: Segmentation fault when passing byref parameters to tailcall methods #12085 (byref arguments), fixed/pinned array escapes due to tail call #18689 (pinned locals), Extra tail instruction corrupts stack? #13447 (stack-allocated locals), [IL Emit] Improve analysis to reduce emit of tail. #12137 (emitting fewer
tail.prefixes).
I'd be glad to send a fix (no tail. when an argument is a byref-like type, with a test) if that approach is welcome.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsNew
Activity
pcshrosbree commentedon Dec 2, 2026
A related case: a self-recursive call compiled to a loop has the same problem, with a plain byref too.
go &h 3should return 7421, because at depth 0rpoints to the previous level'sx. Release compilesgoto a loop (br.s IL_0000), so there is onexslot, reset to 7420 before the read. It returns 7420. The Span variant (let mutable x = n + 7440, passingSpan<int>(&x)) returns 7440 instead of 7441.On
main(c82fbeb), Debug is correct since #20782 emits a call there, and Release is wrong. With SDK 10.0.404, both are wrong. Notail.is involved, so the jump path needs its own check. If the fix is welcome, I'd look at covering this too, with a guard against passing references to the current call's locals through the jump, aiming to keep loops such asloop (s.Slice 1)as loops.