Skip to content

UnsafeAccessorAttribute fails to access volatile field #109665

Closed
@filipnavara

Description

@filipnavara

Description

When trying to access volatile string private field through UnsafeAccessorAttribute, MissingFieldException is thrown.

Reproduction Steps

using System;
using System.Runtime.CompilerServices;

var a = new A();
Console.WriteLine(GetSetFoo(a));

[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "foo")]
extern static ref string GetSetFoo(A args);

class A { private volatile string? foo = "Bar"; }

Expected behavior

The program output is Bar.

Actual behavior

Unhandled exception. System.MissingFieldException: Field not found: 'A.foo'.
   at Program.<<Main>$>g__GetSetFoo|0_0(A args)
   at Program.<Main>$(String[] args) in D:\unsafeaccessor\Program.cs:line 5

Regression?

No response

Known Workarounds

No response

Configuration

This seems to be broken both in .NET 8 and .NET 9 with CoreCLR runtime. Tested on Windows and macOS.

Other information

The question of modreqs/modopts came up in the original feature proposal and this was the answer (#81741 (comment)):

And how would this interact with modreqs and modopts that can't be specified in C# (e.g. the IsExternalInit modifier on init property setters / volatile fields etc)?

Yeah, modreqs and modopts would be complicated. I think we will skip modreqs and modopts for the signature match.

This originally came up in KirillOsenkov/MSBuildStructuredLog#834.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions