Skip to content

JIT: (bug) tuple swap of struct locals with a class-init static struct field reads the new value #135809

Closed

Description

@EgorBo

Description

A tuple swap of struct locals, where one source is a static struct field whose class needs initialization, reads the new value of a local instead of the old one. This is silent bad codegen at both Tier0 and FullOpts.

Reproduction Steps

using System;
using System.Runtime.CompilerServices;

public struct S { public long X, Y; }

public class C
{
    public static S F;
    static C() { F.X = 3; }
}

public class Program
{
    [MethodImpl(MethodImplOptions.NoInlining)]
    static long Test(S a, S b)
    {
        (a, b) = (b, C.F);
        return a.X;
    }

    public static void Main()
    {
        Console.WriteLine(Test(default, new S { X = 2 }));
    }
}

Expected behavior

2

Actual behavior

3

Regression?

Reproduces on main. The original investigation also saw it on .NET 10.0.13 (not re-verified), so it's likely not a recent regression.

Configuration

main, Checked, win-x64. Reproduces at Tier0 and FullOpts.

Other information

At block start, Roslyn emits ldarg.1; ldsfld C::F; starg.s 1; starg.s 0. The importer imports ldsfld C::F as COMMA(GET_GCSTATIC_BASE, BLK). Because impLastStmt == nullptr, impStoreStruct sinks the store under the COMMA. impAppendStmt doesn't treat the COMMA-rooted store as a local store, so it never calls impSpillLclRefs(V01). The pending LCL_VAR V01 on the stack is then evaluated after the store.

Activity

  1. added this to the 12.0.0 milestone on Oct 14, 2026
  2. added
    area-CodeGen-coreclr
    on Oct 14, 2026
  3. dotnet-policy-service commented on Oct 14, 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. added a commit that references this issue on Oct 20, 2026
    23aaf9f
  5. self-assigned this
    on Nov 17, 2026
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