Skip to content

Commit fd3ae61

Browse files
authored
Improve nullable annotation on Volatile.Read (#44410)
Make it clear to the compiler that Volatile.Read doesn't write to the ref argument, such that if the input was non-null before the call, it'll still be non-null after the call.
1 parent 24cc3d6 commit fd3ae61

File tree

4 files changed

+4
-4
lines changed

4 files changed

+4
-4
lines changed

src/libraries/System.Private.CoreLib/src/System/Threading/Thread.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ public void SetCompressedStack(CompressedStack stack)
337337
public static long VolatileRead(ref long address) => Volatile.Read(ref address);
338338
public static IntPtr VolatileRead(ref IntPtr address) => Volatile.Read(ref address);
339339
[return: NotNullIfNotNull("address")]
340-
public static object? VolatileRead(ref object? address) => Volatile.Read(ref address);
340+
public static object? VolatileRead([NotNullIfNotNull("address")] ref object? address) => Volatile.Read(ref address);
341341
[CLSCompliant(false)]
342342
public static sbyte VolatileRead(ref sbyte address) => Volatile.Read(ref address);
343343
public static float VolatileRead(ref float address) => Volatile.Read(ref address);

src/libraries/System.Private.CoreLib/src/System/Threading/Volatile.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ private struct VolatileObject { public volatile object? Value; }
219219
[Intrinsic]
220220
[NonVersionable]
221221
[return: NotNullIfNotNull("location")]
222-
public static T Read<T>(ref T location) where T : class? =>
222+
public static T Read<T>([NotNullIfNotNull("location")] ref T location) where T : class? =>
223223
Unsafe.As<T>(Unsafe.As<T, VolatileObject>(ref location).Value);
224224

225225
[Intrinsic]

src/libraries/System.Threading.Thread/ref/System.Threading.Thread.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public void Suspend() { }
103103
public static long VolatileRead(ref long address) { throw null; }
104104
public static System.IntPtr VolatileRead(ref System.IntPtr address) { throw null; }
105105
[return: System.Diagnostics.CodeAnalysis.NotNullIfNotNullAttribute("address")]
106-
public static object? VolatileRead(ref object? address) { throw null; }
106+
public static object? VolatileRead([System.Diagnostics.CodeAnalysis.NotNullIfNotNullAttribute("address")] ref object? address) { throw null; }
107107
[System.CLSCompliantAttribute(false)]
108108
public static sbyte VolatileRead(ref sbyte address) { throw null; }
109109
public static float VolatileRead(ref float address) { throw null; }

src/libraries/System.Threading/ref/System.Threading.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ public static partial class Volatile
478478
[System.CLSCompliantAttribute(false)]
479479
public static System.UIntPtr Read(ref System.UIntPtr location) { throw null; }
480480
[return: System.Diagnostics.CodeAnalysis.NotNullIfNotNullAttribute("location")]
481-
public static T Read<T>(ref T location) where T : class? { throw null; }
481+
public static T Read<T>([System.Diagnostics.CodeAnalysis.NotNullIfNotNullAttribute("location")] ref T location) where T : class? { throw null; }
482482
public static void Write(ref bool location, bool value) { }
483483
public static void Write(ref byte location, byte value) { }
484484
public static void Write(ref double location, double value) { }

0 commit comments

Comments
 (0)