Skip to content

Commit

Permalink
Add tests for [UnsafeAccessor] on fields with readonly (#109944)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio0694 authored Nov 19, 2024
1 parent e33be4d commit cae0e0f
Showing 1 changed file with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,30 @@ public static void Verify_AccessFieldsWithModifiers()
extern static ref int GetVolatileInt(ref FieldsWithModifiers f);
}

// Contains fields that are readonly
readonly struct ReadOnlyFields
{
public static readonly int s_rInt;
public readonly int _rInt;
}

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

ReadOnlyFields readOnlyFields = default;

Assert.True(Unsafe.AreSame(in GetStaticReadOnlyInt(in readOnlyFields), in ReadOnlyFields.s_rInt));
Assert.True(Unsafe.AreSame(in GetReadOnlyInt(in readOnlyFields), in readOnlyFields._rInt));

[UnsafeAccessor(UnsafeAccessorKind.StaticField, Name="s_rInt")]
extern static ref readonly int GetStaticReadOnlyInt(ref readonly ReadOnlyFields f);

[UnsafeAccessor(UnsafeAccessorKind.Field, Name="_rInt")]
extern static ref readonly int GetReadOnlyInt(ref readonly ReadOnlyFields f);
}

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

0 comments on commit cae0e0f

Please sign in to comment.