Skip to content

Unexpected IL for a list pattern over an array #57728

@AlekseyTs

Description

@AlekseyTs
    static void Test1(int[] x)
    {
        switch (x)
        {
            case [.., 1]: break;
        }
    }

Observed IL:

    .method private hidebysig static 
        void Test1 (
            int32[] x
        ) cil managed 
    {
        // Method begins at RVA 0x2050
        // Code size 23 (0x17)
        .maxstack 3
        .locals init (
            [0] int32
        )

        // sequence point: hidden
        IL_0000: ldarg.0
        IL_0001: brfalse.s IL_0016

        IL_0003: ldarg.0
        IL_0004: callvirt instance int32 [System.Private.CoreLib]System.Array::get_Length()
        IL_0009: stloc.0
        // sequence point: hidden
        IL_000a: ldloc.0
        IL_000b: ldc.i4.1
        IL_000c: blt.s IL_0016

        IL_000e: ldarg.0
        IL_000f: ldloc.0
        IL_0010: ldc.i4.1
        IL_0011: sub
        IL_0012: ldelem.i4
        // sequence point: hidden
        IL_0013: ldc.i4.1
        IL_0014: pop
        IL_0015: pop

        IL_0016: ret
    } // end of method C::Test1

Note, callvirt instance int32 [System.Private.CoreLib]System.Array::get_Length() is used to obtain array's length.

Expected: ldlen instruction should be used instead.

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions