From 5d8c35ab1d4278df0f37045504fed9436046e2e8 Mon Sep 17 00:00:00 2001 From: Robert Peele Date: Thu, 8 Oct 2026 02:38:36 -0400 Subject: [PATCH 1/3] Red test for issue 20732: Release-only wrong result in integer `for` loop: local bound inside loop range expression clobbers a local slot --- .../CodeGenRegressions_Crashes.fs | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/tests/FSharp.Compiler.ComponentTests/EmittedIL/CodeGenRegressions/CodeGenRegressions_Crashes.fs b/tests/FSharp.Compiler.ComponentTests/EmittedIL/CodeGenRegressions/CodeGenRegressions_Crashes.fs index e49c3a3b211..fd380174b48 100644 --- a/tests/FSharp.Compiler.ComponentTests/EmittedIL/CodeGenRegressions/CodeGenRegressions_Crashes.fs +++ b/tests/FSharp.Compiler.ComponentTests/EmittedIL/CodeGenRegressions/CodeGenRegressions_Crashes.fs @@ -426,3 +426,36 @@ let main _ = Test.run(); 0 |> shouldSucceed |> ignore + // https://github.com/dotnet/fsharp/issues/20732 + [] + let ``Issue_20732_ForLoopBoundLocalReusesLiveLocal`` () = + FSharp """ +module Test + +let add (a: int) (b: int) = a + b + +let lastFromStart k = + add (k * 100) + (let mutable last = -1 + for i in (let d = k - 1 in if d > 0 then d else 0) .. 3 do last <- i + last) + +let lastBeforeLength (xs: int[]) (ys: int[]) k = + add (k * 100) + (let mutable last = -1 + for i in 0 .. (let d = k - 1 in if d > 0 then xs else ys).Length - 1 do last <- i + last) + +[] +let main _ = + for k in 0 .. 2 do + if lastFromStart k <> k * 100 + 3 then failwithf "start, k = %d" k + if lastBeforeLength [| 1; 2; 3 |] [| 1; 2 |] k <> k * 100 + (if k > 1 then 2 else 1) then failwithf "length, k = %d" k + 0 +""" + |> withOptimize + |> asExe + |> compileAndRun + |> shouldSucceed + |> ignore + From d8e21d0744cfd74208b9d2d01c77714ee592bbaf Mon Sep 17 00:00:00 2001 From: Robert Peele Date: Thu, 8 Oct 2026 02:38:36 -0400 Subject: [PATCH 2/3] Fix for issue 20732: Release-only wrong result in integer `for` loop: local bound inside loop range expression clobbers a local slot --- src/Compiler/CodeGen/IlxGen.fs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Compiler/CodeGen/IlxGen.fs b/src/Compiler/CodeGen/IlxGen.fs index dbed844ca00..15a8755e219 100644 --- a/src/Compiler/CodeGen/IlxGen.fs +++ b/src/Compiler/CodeGen/IlxGen.fs @@ -5472,6 +5472,7 @@ and GenIntegerForLoop cenv cgbuf eenv (spFor, spTo, v, e1, dir, e2, loopBody, m) let inner = CG.GenerateDelayMark cgbuf "for_inner" let test = CG.GenerateDelayMark cgbuf "for_test" let stack, eenvinner = EmitSaveStack cenv cgbuf eenv m (start, finish) + let eenvstack = eenvinner let isUp = (match dir with @@ -5505,7 +5506,7 @@ and GenIntegerForLoop cenv cgbuf eenv (spFor, spTo, v, e1, dir, e2, loopBody, m) | DebugPointAtFor.Yes spStart -> CG.EmitDebugPoint cgbuf spStart | DebugPointAtFor.No -> () - GenExpr cenv cgbuf eenv e1 Continue + GenExpr cenv cgbuf eenvstack e1 Continue GenStoreVal cgbuf eenvinner m v if isFSharpStyle then @@ -5560,7 +5561,7 @@ and GenIntegerForLoop cenv cgbuf eenv (spFor, spTo, v, e1, dir, e2, loopBody, m) CG.EmitInstr cgbuf (pop 1) Push0 (if isUp then AI_add else AI_sub) GenSequel cenv eenv.cloc cgbuf e2Sequel else - GenExpr cenv cgbuf eenv e2 e2Sequel + GenExpr cenv cgbuf eenvinner e2 e2Sequel // .finish - loop-exit here CG.SetMarkToHere cgbuf finish From 46ee36128fe1c128104b0f53b23d3218e16fa048 Mon Sep 17 00:00:00 2001 From: Robert Peele Date: Thu, 8 Oct 2026 03:28:16 -0400 Subject: [PATCH 3/3] Release notes entry for PR 20733 --- docs/release-notes/.FSharp.Compiler.Service/11.0.200.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/release-notes/.FSharp.Compiler.Service/11.0.200.md b/docs/release-notes/.FSharp.Compiler.Service/11.0.200.md index 5a3649f260b..a6614032f7c 100644 --- a/docs/release-notes/.FSharp.Compiler.Service/11.0.200.md +++ b/docs/release-notes/.FSharp.Compiler.Service/11.0.200.md @@ -5,6 +5,7 @@ ### Fixed * Split Linux CoreCLR CI into two parallel test batches to reduce test turnaround time. ([PR #20587](https://github.com/dotnet/fsharp/pull/20587)) +* Fix wrong results or non-terminating loops in optimized code when an integer `for` loop's start or end expression binds a local. ([Issue #20732](https://github.com/dotnet/fsharp/issues/20732), [PR #20733](https://github.com/dotnet/fsharp/pull/20733)) * Fix `NativePtr.stackalloc` used inside the `with` handler, filter or `finally` block of a `try` expression producing an assembly that throws `InvalidProgramException` at method load. Direct or mandatory-inline allocations are now reported at compile time as error FS3924; calls to non-inline allocation helpers, including handler-local helpers, remain legal and retain their method boundary during inlining and let-binding elimination. ([Issue #20295](https://github.com/dotnet/fsharp/issues/20295), [PR #20491](https://github.com/dotnet/fsharp/pull/20491)) * `FsiEvaluationSession.EvalInteraction` and `EvalInteractionNonThrowing` evaluate every `;;`-separated interaction in the text they are given, as their documentation has always said, instead of silently discarding everything after the first. ([PR #20396](https://github.com/dotnet/fsharp/pull/20396)) * Preserve constraint inference order when explicit generic arguments share a caller type parameter, avoiding spurious FS0001 errors with multiply implemented generic interfaces. ([PR #20491](https://github.com/dotnet/fsharp/pull/20491))