Skip to content

[UnsafeAccessor] throws MissingFieldException on .NET 9 when used on unspeakable fields #109943

Description

@Sergio0694

Description

Spotted while porting my ComputeSharp samples to .NET 9 (see here). It seems that [UnsafeAccessor] is broken in .NET 9 when used to access unspeakable fields (captured primary constructor parameters being a common example).

Reproduction Steps

using System.Runtime.CompilerServices;

Blah blah = new("Bob");

string name = Blah.GetName(in blah);

Console.WriteLine(name);

public readonly struct Blah(string name)
{
    public string EnsureCaptured => name;

    [UnsafeAccessor(UnsafeAccessorKind.Field, Name = "<name>P")]
    public static extern ref readonly string GetName(ref readonly Blah value);
}

Expected behavior

Should work as expected.

Actual behavior

Sergio0694/ComputeSharp#871

Regression?

Yes. Works fine on .NET 8.

Known Workarounds

None that I can think of.

Configuration

  • .NET SDK 9.0.100

Metadata

Metadata

Assignees

No one assigned

    Type

    Fields

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions