Skip to content

Add missing readonly modifiers to GCHandle and DependentHandle #93178

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 @@ -71,7 +71,7 @@ public DependentHandle(object? target, object? dependent)
/// <see cref="DependentHandle(object?, object?)"/> and has not yet been disposed.
/// </summary>
/// <remarks>This property is thread-safe.</remarks>
public bool IsAllocated => (nint)_handle != 0;
public readonly bool IsAllocated => (nint)_handle != 0;

/// <summary>
/// Gets or sets the target object instance for the current handle. The target can only be set to a <see langword="null"/> value
Expand All @@ -83,7 +83,7 @@ public DependentHandle(object? target, object? dependent)
/// <remarks>This property is thread-safe.</remarks>
public object? Target
{
get
readonly get
{
IntPtr handle = _handle;

Expand Down Expand Up @@ -120,7 +120,7 @@ public object? Target
/// <remarks>This property is thread-safe.</remarks>
public object? Dependent
{
get
readonly get
{
IntPtr handle = _handle;

Expand Down Expand Up @@ -154,7 +154,7 @@ public object? Dependent
/// <returns>The values of <see cref="Target"/> and <see cref="Dependent"/>.</returns>
/// <exception cref="InvalidOperationException">Thrown if <see cref="IsAllocated"/> is <see langword="false"/>.</exception>
/// <remarks>This property is thread-safe.</remarks>
public (object? Target, object? Dependent) TargetAndDependent
public readonly (object? Target, object? Dependent) TargetAndDependent
{
get
{
Expand All @@ -176,7 +176,7 @@ public object? Dependent
/// </summary>
/// <returns>The target object instance, if present.</returns>
/// <remarks>This method mirrors <see cref="Target"/>, but without the allocation check.</remarks>
internal object? UnsafeGetTarget()
internal readonly object? UnsafeGetTarget()
{
return InternalGetTarget(_handle);
}
Expand All @@ -191,7 +191,7 @@ public object? Dependent
/// The signature is also kept the same as the one for the internal call, to improve the codegen.
/// Note that <paramref name="dependent"/> is required to be on the stack (or it might not be tracked).
/// </remarks>
internal object? UnsafeGetTargetAndDependent(out object? dependent)
internal readonly object? UnsafeGetTargetAndDependent(out object? dependent)
{
return InternalGetTargetAndDependent(_handle, out dependent);
}
Expand All @@ -200,7 +200,7 @@ public object? Dependent
/// Sets the dependent object instance for the current handle to <see langword="null"/>.
/// </summary>
/// <remarks>This method mirrors the <see cref="Target"/> setter, but without allocation and input checks.</remarks>
internal void UnsafeSetTargetToNull()
internal readonly void UnsafeSetTargetToNull()
{
InternalSetTargetToNull(_handle);
}
Expand All @@ -209,7 +209,7 @@ internal void UnsafeSetTargetToNull()
/// Sets the dependent object instance for the current handle.
/// </summary>
/// <remarks>This method mirrors <see cref="Dependent"/>, but without the allocation check.</remarks>
internal void UnsafeSetDependent(object? dependent)
internal readonly void UnsafeSetDependent(object? dependent)
{
InternalSetDependent(_handle, dependent);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void Free()
// Target property - allows getting / updating of the handle's referent.
public object? Target
{
get
readonly get
{
IntPtr handle = _handle;
ThrowIfInvalid(handle);
Expand All @@ -103,7 +103,7 @@ public object? Target
/// Retrieve the address of an object in a Pinned handle. This throws
/// an exception if the handle is any type other than Pinned.
/// </summary>
public IntPtr AddrOfPinnedObject()
public readonly IntPtr AddrOfPinnedObject()
{
// Check if the handle was not a pinned handle.
// You can only get the address of pinned handles.
Expand Down Expand Up @@ -141,7 +141,7 @@ public IntPtr AddrOfPinnedObject()
}

/// <summary>Determine whether this handle has been allocated or not.</summary>
public bool IsAllocated => (nint)_handle != 0;
public readonly bool IsAllocated => (nint)_handle != 0;

/// <summary>
/// Used to create a GCHandle from an int. This is intended to
Expand All @@ -160,14 +160,14 @@ public static GCHandle FromIntPtr(IntPtr value)

public static IntPtr ToIntPtr(GCHandle value) => value._handle;

public override int GetHashCode() => _handle.GetHashCode();
public override readonly int GetHashCode() => _handle.GetHashCode();

public override bool Equals([NotNullWhen(true)] object? o) => o is GCHandle other && Equals(other);
public override readonly bool Equals([NotNullWhen(true)] object? o) => o is GCHandle other && Equals(other);

/// <summary>Indicates whether the current instance is equal to another instance of the same type.</summary>
/// <param name="other">An instance to compare with this instance.</param>
/// <returns>true if the current instance is equal to the other instance; otherwise, false.</returns>
public bool Equals(GCHandle other) => _handle == other._handle;
public readonly bool Equals(GCHandle other) => _handle == other._handle;

public static bool operator ==(GCHandle a, GCHandle b) => (nint)a._handle == (nint)b._handle;

Expand Down
20 changes: 10 additions & 10 deletions src/libraries/System.Runtime/ref/System.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12428,10 +12428,10 @@ public partial struct DependentHandle : System.IDisposable
private object _dummy;
private int _dummyPrimitive;
public DependentHandle(object? target, object? dependent) { throw null; }
public object? Dependent { get { throw null; } set { } }
public bool IsAllocated { get { throw null; } }
public object? Target { get { throw null; } set { } }
public (object? Target, object? Dependent) TargetAndDependent { get { throw null; } }
public object? Dependent { readonly get { throw null; } set { } }
public readonly bool IsAllocated { get { throw null; } }
public object? Target { readonly get { throw null; } set { } }
public readonly (object? Target, object? Dependent) TargetAndDependent { get { throw null; } }
public void Dispose() { }
}
public enum GCLargeObjectHeapCompactionMode
Expand Down Expand Up @@ -13455,16 +13455,16 @@ public FieldOffsetAttribute(int offset) { }
public partial struct GCHandle : System.IEquatable<System.Runtime.InteropServices.GCHandle>
{
private int _dummyPrimitive;
public bool IsAllocated { get { throw null; } }
public object? Target { get { throw null; } set { } }
public System.IntPtr AddrOfPinnedObject() { throw null; }
public readonly bool IsAllocated { get { throw null; } }
public object? Target { readonly get { throw null; } set { } }
public readonly System.IntPtr AddrOfPinnedObject() { throw null; }
public static System.Runtime.InteropServices.GCHandle Alloc(object? value) { throw null; }
public static System.Runtime.InteropServices.GCHandle Alloc(object? value, System.Runtime.InteropServices.GCHandleType type) { throw null; }
public override bool Equals([System.Diagnostics.CodeAnalysis.NotNullWhenAttribute(true)] object? o) { throw null; }
public bool Equals(System.Runtime.InteropServices.GCHandle other) { throw null; }
public override readonly bool Equals([System.Diagnostics.CodeAnalysis.NotNullWhenAttribute(true)] object? o) { throw null; }
public readonly bool Equals(System.Runtime.InteropServices.GCHandle other) { throw null; }
public void Free() { }
public static System.Runtime.InteropServices.GCHandle FromIntPtr(System.IntPtr value) { throw null; }
public override int GetHashCode() { throw null; }
public override readonly int GetHashCode() { throw null; }
public static bool operator ==(System.Runtime.InteropServices.GCHandle a, System.Runtime.InteropServices.GCHandle b) { throw null; }
public static explicit operator System.Runtime.InteropServices.GCHandle (System.IntPtr value) { throw null; }
public static explicit operator System.IntPtr (System.Runtime.InteropServices.GCHandle value) { throw null; }
Expand Down