Skip to content

JIT: (bug) recursive tail call to loop evaluates an arg setup after a parameter was already overwritten #134476

Closed

Description

@EgorBo

When a recursive tail call is turned into a loop, arr[e] is evaluated after parameter e has already been overwritten with e + 1.

Minimal Repro

using System;
using System.Runtime.CompilerServices;

public class Program
{
    [MethodImpl(MethodImplOptions.AggressiveOptimization)]
    static int Test(int[] arr, int x, int y, int n, int e, int f, int g)
    {
        if (n == 0)
            return f + g;
        return Test(arr, x, y, n - 1, e + 1, arr[e], arr[g]);
    }

    [MethodImpl(MethodImplOptions.NoOptimization)]
    public static void Main()
    {
        int[] arr = { 10, 20, 30, 40 };
        Console.WriteLine(Test(arr, 0, 0, 1, 0, 0, 0));
    }
}

Expected

20

Actual

30

Regression?

Not a regression. Reproduces on .NET 8, 9, 10, 11 RC2 and main (win-x64).

Notes

In fgMorphRecursiveFastTailCallIntoLoop, the setup nodes of early args (tmp = arr[e]) are inserted right before the call stmt, which is after the e = tmp(e+1) param store emitted for the earlier in-place stack arg (both use lastStmt as the insertion point).

Activity

  1. added this to the 12.0.0 milestone on Sep 22, 2026
  2. added
    area-CodeGen-coreclr
    on Sep 22, 2026
  3. dotnet-policy-service commented on Sep 22, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
    See info in area-owners.md if you want to be subscribed.

  4. self-assigned this
    on Nov 6, 2026
  5. added a commit that references this issue on Nov 7, 2026
    0de50b2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclr

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions