diff --git a/src/coreclr/nativeaot/Common/src/Internal/Runtime/MethodTable.cs b/src/coreclr/nativeaot/Common/src/Internal/Runtime/MethodTable.cs index 2bc4c76613848b..6aabd800f2915a 100644 --- a/src/coreclr/nativeaot/Common/src/Internal/Runtime/MethodTable.cs +++ b/src/coreclr/nativeaot/Common/src/Internal/Runtime/MethodTable.cs @@ -1327,7 +1327,7 @@ internal readonly struct RelativePointer { private readonly int _value; - public unsafe IntPtr Value => (IntPtr)((byte*)Unsafe.AsPointer(ref Unsafe.AsRef(in _value)) + _value); + public unsafe IntPtr Value => (IntPtr)((byte*)Unsafe.AsPointer(in _value) + _value); } // Wrapper around relative pointers @@ -1336,7 +1336,7 @@ internal readonly struct RelativePointer { private readonly int _value; - public T* Value => (T*)((byte*)Unsafe.AsPointer(ref Unsafe.AsRef(in _value)) + _value); + public T* Value => (T*)((byte*)Unsafe.AsPointer(in _value) + _value); } // Abstracts a list of MethodTable pointers that could either be relative diff --git a/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/CompilerServices/Unsafe.cs b/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/CompilerServices/Unsafe.cs index 7f95b9bd401fca..8b941d4feedb3c 100644 --- a/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/CompilerServices/Unsafe.cs +++ b/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/CompilerServices/Unsafe.cs @@ -36,7 +36,7 @@ public static IntPtr ByteOffset(ref readonly T origin, ref readonly T target) /// [Intrinsic] [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void* AsPointer(ref T value) + public static void* AsPointer(ref readonly T value) { throw new PlatformNotSupportedException(); diff --git a/src/coreclr/nativeaot/System.Private.TypeLoader/src/Internal/Runtime/TypeLoader/MethodTable.Runtime.cs b/src/coreclr/nativeaot/System.Private.TypeLoader/src/Internal/Runtime/TypeLoader/MethodTable.Runtime.cs index 49b75a220bf003..5826be0d3e6fb8 100644 --- a/src/coreclr/nativeaot/System.Private.TypeLoader/src/Internal/Runtime/TypeLoader/MethodTable.Runtime.cs +++ b/src/coreclr/nativeaot/System.Private.TypeLoader/src/Internal/Runtime/TypeLoader/MethodTable.Runtime.cs @@ -18,9 +18,9 @@ internal partial struct MethodTable return MethodTable.Of(); } - internal unsafe RuntimeTypeHandle ToRuntimeTypeHandle() + internal readonly unsafe RuntimeTypeHandle ToRuntimeTypeHandle() { - IntPtr result = (IntPtr)Unsafe.AsPointer(ref this); + IntPtr result = (IntPtr)Unsafe.AsPointer(in this); return *(RuntimeTypeHandle*)&result; } } diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/Unsafe.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/Unsafe.cs index 1664dd02e0e5fa..32fd9366f4ab8c 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/Unsafe.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/Unsafe.cs @@ -26,7 +26,7 @@ public static unsafe partial class Unsafe [NonVersionable] [CLSCompliant(false)] [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void* AsPointer(ref T value) + public static void* AsPointer(ref readonly T value) where T : allows ref struct { throw new PlatformNotSupportedException(); @@ -748,7 +748,7 @@ public static ref T NullRef() public static bool IsNullRef(ref readonly T source) where T : allows ref struct { - return AsPointer(ref Unsafe.AsRef(in source)) == null; + return AsPointer(in source) == null; // ldarg.0 // ldc.i4.0 @@ -949,7 +949,7 @@ internal static bool IsOpportunisticallyAligned(ref readonly T address, nuint // GC will keep alignment when moving objects (up to sizeof(void*)), // otherwise alignment should be considered a hint if not pinned. Debug.Assert(nuint.IsPow2(alignment)); - return ((nuint)AsPointer(ref AsRef(in address)) & (alignment - 1)) == 0; + return ((nuint)AsPointer(in address) & (alignment - 1)) == 0; } // Determines the misalignment of the address with respect to the specified `alignment`. @@ -961,7 +961,7 @@ internal static nuint OpportunisticMisalignment(ref readonly T address, nuint // GC will keep alignment when moving objects (up to sizeof(void*)), // otherwise alignment should be considered a hint if not pinned. Debug.Assert(nuint.IsPow2(alignment)); - return (nuint)AsPointer(ref AsRef(in address)) & (alignment - 1); + return (nuint)AsPointer(in address) & (alignment - 1); } } } diff --git a/src/libraries/System.Runtime/ref/System.Runtime.cs b/src/libraries/System.Runtime/ref/System.Runtime.cs index aaff8b1c5cb654..521d4284f62d3e 100644 --- a/src/libraries/System.Runtime/ref/System.Runtime.cs +++ b/src/libraries/System.Runtime/ref/System.Runtime.cs @@ -13933,7 +13933,7 @@ public static partial class Unsafe public static ref T Add(ref T source, nuint elementOffset) where T : allows ref struct { throw null; } public static bool AreSame([System.Diagnostics.CodeAnalysis.AllowNull] ref readonly T left, [System.Diagnostics.CodeAnalysis.AllowNull] ref readonly T right) where T : allows ref struct { throw null; } [System.CLSCompliantAttribute(false)] - public unsafe static void* AsPointer(ref T value) where T : allows ref struct { throw null; } + public unsafe static void* AsPointer(ref readonly T value) where T : allows ref struct { throw null; } [System.CLSCompliantAttribute(false)] public unsafe static ref T AsRef(void* source) where T : allows ref struct { throw null; } public static ref T AsRef(scoped ref readonly T source) where T : allows ref struct { throw null; } diff --git a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/StringTests.cs b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/StringTests.cs index 0dd89ccdc5ee82..4d7258fb4c0985 100644 --- a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/StringTests.cs +++ b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/StringTests.cs @@ -1043,7 +1043,7 @@ public static unsafe void GetPinnableReference_ReturnsSameAsGCHandleAndLegacyFix try { // Unsafe.AsPointer is safe since it's pinned by the gc handle - Assert.Equal((IntPtr)Unsafe.AsPointer(ref Unsafe.AsRef(in rChar)), gcHandle.AddrOfPinnedObject()); + Assert.Equal((IntPtr)Unsafe.AsPointer(in rChar), gcHandle.AddrOfPinnedObject()); } finally {