Skip to content

Commit

Permalink
Ignore modopts/modreqs for UnsafeAccessor field targets
Browse files Browse the repository at this point in the history
The generated IL remains correct and doesn't
require a .volatile prefix for the generated accessor.
This is based off of what Roslyn currently emits if a
getter was available.
  • Loading branch information
AaronRobinsonMSFT authored and github-actions committed Nov 11, 2024
1 parent 940e395 commit f013d85
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/coreclr/vm/prestub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1497,7 +1497,7 @@ namespace

TokenPairList list { nullptr };
MetaSig::CompareState state{ &list };
state.IgnoreCustomModifiers = false;
state.IgnoreCustomModifiers = true;
if (!DoesFieldMatchUnsafeAccessorDeclaration(cxt, pField, state))
continue;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,30 @@ public static void Verify_AccessAllFields_CorElementType()
extern static ref delegate*<void> GetFPtr(ref AllFields f);
}

// Contains fields that have modopts/modreqs
struct FieldsWithModifiers
{
private static volatile int s_vInt;
private volatile int _vInt;
}

[Fact]
public static void Verify_AccessFieldsWithModifiers()
{
Console.WriteLine($"Running {nameof(Verify_AccessFieldsWithModifiers)}");

FieldsWithModifiers fieldsWithModifiers = default;

GetStaticVolatileInt(ref fieldsWithModifiers) = default;
GetVolatileInt(ref fieldsWithModifiers) = default;

[UnsafeAccessor(UnsafeAccessorKind.StaticField, Name="s_vInt")]
extern static ref int GetStaticVolatileInt(ref FieldsWithModifiers f);

[UnsafeAccessor(UnsafeAccessorKind.Field, Name="_vInt")]
extern static ref int GetVolatileInt(ref FieldsWithModifiers f);
}

[Fact]
public static void Verify_AccessStaticMethodClass()
{
Expand Down

0 comments on commit f013d85

Please sign in to comment.