Skip to content

Commit 6d3a3b9

Browse files
committed
Use 'ref readonly' in 'Unsafe.AsPointer'
1 parent c272e34 commit 6d3a3b9

File tree

6 files changed

+11
-11
lines changed

6 files changed

+11
-11
lines changed

src/coreclr/nativeaot/Common/src/Internal/Runtime/MethodTable.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,7 +1327,7 @@ internal readonly struct RelativePointer
13271327
{
13281328
private readonly int _value;
13291329

1330-
public unsafe IntPtr Value => (IntPtr)((byte*)Unsafe.AsPointer(ref Unsafe.AsRef(in _value)) + _value);
1330+
public unsafe IntPtr Value => (IntPtr)((byte*)Unsafe.AsPointer(in _value) + _value);
13311331
}
13321332

13331333
// Wrapper around relative pointers
@@ -1336,7 +1336,7 @@ internal readonly struct RelativePointer
13361336
{
13371337
private readonly int _value;
13381338

1339-
public T* Value => (T*)((byte*)Unsafe.AsPointer(ref Unsafe.AsRef(in _value)) + _value);
1339+
public T* Value => (T*)((byte*)Unsafe.AsPointer(in _value) + _value);
13401340
}
13411341

13421342
// Abstracts a list of MethodTable pointers that could either be relative

src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/CompilerServices/Unsafe.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public static IntPtr ByteOffset<T>(ref readonly T origin, ref readonly T target)
3636
/// </summary>
3737
[Intrinsic]
3838
[MethodImpl(MethodImplOptions.AggressiveInlining)]
39-
public static void* AsPointer<T>(ref T value)
39+
public static void* AsPointer<T>(ref readonly T value)
4040
{
4141
throw new PlatformNotSupportedException();
4242

src/coreclr/nativeaot/System.Private.TypeLoader/src/Internal/Runtime/TypeLoader/MethodTable.Runtime.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ internal partial struct MethodTable
1818
return MethodTable.Of<Array>();
1919
}
2020

21-
internal unsafe RuntimeTypeHandle ToRuntimeTypeHandle()
21+
internal readonly unsafe RuntimeTypeHandle ToRuntimeTypeHandle()
2222
{
23-
IntPtr result = (IntPtr)Unsafe.AsPointer(ref this);
23+
IntPtr result = (IntPtr)Unsafe.AsPointer(in this);
2424
return *(RuntimeTypeHandle*)&result;
2525
}
2626
}

src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/Unsafe.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public static unsafe partial class Unsafe
2626
[NonVersionable]
2727
[CLSCompliant(false)]
2828
[MethodImpl(MethodImplOptions.AggressiveInlining)]
29-
public static void* AsPointer<T>(ref T value)
29+
public static void* AsPointer<T>(ref readonly T value)
3030
where T : allows ref struct
3131
{
3232
throw new PlatformNotSupportedException();
@@ -748,7 +748,7 @@ public static ref T NullRef<T>()
748748
public static bool IsNullRef<T>(ref readonly T source)
749749
where T : allows ref struct
750750
{
751-
return AsPointer(ref Unsafe.AsRef(in source)) == null;
751+
return AsPointer(in source) == null;
752752

753753
// ldarg.0
754754
// ldc.i4.0
@@ -949,7 +949,7 @@ internal static bool IsOpportunisticallyAligned<T>(ref readonly T address, nuint
949949
// GC will keep alignment when moving objects (up to sizeof(void*)),
950950
// otherwise alignment should be considered a hint if not pinned.
951951
Debug.Assert(nuint.IsPow2(alignment));
952-
return ((nuint)AsPointer(ref AsRef(in address)) & (alignment - 1)) == 0;
952+
return ((nuint)AsPointer(in address) & (alignment - 1)) == 0;
953953
}
954954

955955
// Determines the misalignment of the address with respect to the specified `alignment`.
@@ -961,7 +961,7 @@ internal static nuint OpportunisticMisalignment<T>(ref readonly T address, nuint
961961
// GC will keep alignment when moving objects (up to sizeof(void*)),
962962
// otherwise alignment should be considered a hint if not pinned.
963963
Debug.Assert(nuint.IsPow2(alignment));
964-
return (nuint)AsPointer(ref AsRef(in address)) & (alignment - 1);
964+
return (nuint)AsPointer(in address) & (alignment - 1);
965965
}
966966
}
967967
}

src/libraries/System.Runtime/ref/System.Runtime.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13933,7 +13933,7 @@ public static partial class Unsafe
1393313933
public static ref T Add<T>(ref T source, nuint elementOffset) where T : allows ref struct { throw null; }
1393413934
public static bool AreSame<T>([System.Diagnostics.CodeAnalysis.AllowNull] ref readonly T left, [System.Diagnostics.CodeAnalysis.AllowNull] ref readonly T right) where T : allows ref struct { throw null; }
1393513935
[System.CLSCompliantAttribute(false)]
13936-
public unsafe static void* AsPointer<T>(ref T value) where T : allows ref struct { throw null; }
13936+
public unsafe static void* AsPointer<T>(ref readonly T value) where T : allows ref struct { throw null; }
1393713937
[System.CLSCompliantAttribute(false)]
1393813938
public unsafe static ref T AsRef<T>(void* source) where T : allows ref struct { throw null; }
1393913939
public static ref T AsRef<T>(scoped ref readonly T source) where T : allows ref struct { throw null; }

src/libraries/System.Runtime/tests/System.Runtime.Tests/System/StringTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1043,7 +1043,7 @@ public static unsafe void GetPinnableReference_ReturnsSameAsGCHandleAndLegacyFix
10431043
try
10441044
{
10451045
// Unsafe.AsPointer is safe since it's pinned by the gc handle
1046-
Assert.Equal((IntPtr)Unsafe.AsPointer(ref Unsafe.AsRef(in rChar)), gcHandle.AddrOfPinnedObject());
1046+
Assert.Equal((IntPtr)Unsafe.AsPointer(in rChar), gcHandle.AddrOfPinnedObject());
10471047
}
10481048
finally
10491049
{

0 commit comments

Comments
 (0)