Skip to content

Use 'ref readonly' in 'Unsafe.AsPointer' #114406

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static IntPtr ByteOffset<T>(ref readonly T origin, ref readonly T target)
/// </summary>
[Intrinsic]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void* AsPointer<T>(ref T value)
public static void* AsPointer<T>(ref readonly T value)
{
throw new PlatformNotSupportedException();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ internal partial struct MethodTable
return MethodTable.Of<Array>();
}

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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static unsafe partial class Unsafe
[NonVersionable]
[CLSCompliant(false)]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void* AsPointer<T>(ref T value)
public static void* AsPointer<T>(ref readonly T value)
where T : allows ref struct
{
throw new PlatformNotSupportedException();
Expand Down Expand Up @@ -748,7 +748,7 @@ public static ref T NullRef<T>()
public static bool IsNullRef<T>(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
Expand Down Expand Up @@ -949,7 +949,7 @@ internal static bool IsOpportunisticallyAligned<T>(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`.
Expand All @@ -961,7 +961,7 @@ internal static nuint OpportunisticMisalignment<T>(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);
}
}
}
2 changes: 1 addition & 1 deletion src/libraries/System.Runtime/ref/System.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13933,7 +13933,7 @@ public static partial class Unsafe
public static ref T Add<T>(ref T source, nuint elementOffset) where T : allows ref struct { throw null; }
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; }
[System.CLSCompliantAttribute(false)]
public unsafe static void* AsPointer<T>(ref T value) where T : allows ref struct { throw null; }
public unsafe static void* AsPointer<T>(ref readonly T value) where T : allows ref struct { throw null; }
[System.CLSCompliantAttribute(false)]
public unsafe static ref T AsRef<T>(void* source) where T : allows ref struct { throw null; }
public static ref T AsRef<T>(scoped ref readonly T source) where T : allows ref struct { throw null; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down