Skip to content

Decompiling foreach over an inline array emits invalid '<PrivateImplementationDetails>.InlineArrayElementRef' #3804

Description

@siegfriedpammer

Summary

Decompiling a foreach over an inline array value produces code that does not compile: it references the compiler-internal <PrivateImplementationDetails>.InlineArrayElementRef helper, whose type name contains angle brackets and cannot be written in C#.

Steps to reproduce

Compile this with an optimizing compiler:

using System.Runtime.CompilerServices;

[InlineArray(4)]
public struct Buf4 { private int _element0; }

public static class C
{
    public static int Sum(Buf4 b)
    {
        int s = 0;
        foreach (int x in b) s += x;
        return s;
    }
}

Decompile type C.

Actual output

public static int Sum(Buf4 b)
{
    int num = 0;
    for (int i = 0; i < 4; i++)
    {
        int num2 = global::<PrivateImplementationDetails>.InlineArrayElementRef<Buf4, int>(ref b, i);
        num += num2;
    }
    return num;
}

This does not compile (error CS1001: Identifier expected at global::<PrivateImplementationDetails>). <PrivateImplementationDetails> is a compiler-generated type and InlineArrayElementRef<TBuffer, TElement> is a helper the compiler emits there to lower foreach over an inline-array value; the decompiler does not recognize the pattern and emits the raw call.

Expected output

Valid C# equivalent to the source, e.g. reconstructing the loop via the inline-array indexer:

public static int Sum(Buf4 b)
{
    int num = 0;
    for (int i = 0; i < 4; i++)
    {
        num += b[i];
    }
    return num;
}

(or the original foreach).

Scope

  • Specific to foreach over an inline array. Other inline-array patterns decompile correctly: direct indexing (b[0]), for with the indexer, index-from-end (b[^1]), range/slice (b[1..4]), implicit Span<T> conversion, ref-to-element, passing as a span, and inline-array fields.
  • Not currently covered by InlineArrayTests.

Version

ICSharpCode.Decompiler: 11.0.0 (current master).


This issue was filed by an AI agent (Claude, claude-opus-4-8, via Claude Code) on behalf of the repo owner.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions