Skip to content

Commit

Permalink
Fix nullability
Browse files Browse the repository at this point in the history
  • Loading branch information
jkotas committed Nov 21, 2020
1 parent 527c415 commit 8afaa9d
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public override sealed int GetHashCode()

// Force inline as the true/false ternary takes it above ALWAYS_INLINE size even though the asm ends up smaller
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(MulticastDelegate d1, MulticastDelegate d2)
public static bool operator ==(MulticastDelegate? d1, MulticastDelegate? d2)
{
// Test d2 first to allow branch elimination when inlined for null checks (== null)
// so it can become a simple test
Expand All @@ -122,7 +122,7 @@ public override sealed int GetHashCode()

// Force inline as the true/false ternary takes it above ALWAYS_INLINE size even though the asm ends up smaller
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator !=(MulticastDelegate d1, MulticastDelegate d2)
public static bool operator !=(MulticastDelegate? d1, MulticastDelegate? d2)
{
// Can't call the == operator as it will call object==

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,28 +60,28 @@ public bool Equals(RuntimeTypeHandle handle)
}
}

public static bool operator ==(object left, RuntimeTypeHandle right)
public static bool operator ==(object? left, RuntimeTypeHandle right)
{
if (left is RuntimeTypeHandle)
return right.Equals((RuntimeTypeHandle)left);
return false;
}

public static bool operator ==(RuntimeTypeHandle left, object right)
public static bool operator ==(RuntimeTypeHandle left, object? right)
{
if (right is RuntimeTypeHandle)
return left.Equals((RuntimeTypeHandle)right);
return false;
}

public static bool operator !=(object left, RuntimeTypeHandle right)
public static bool operator !=(object? left, RuntimeTypeHandle right)
{
if (left is RuntimeTypeHandle)
return !right.Equals((RuntimeTypeHandle)left);
return true;
}

public static bool operator !=(RuntimeTypeHandle left, object right)
public static bool operator !=(RuntimeTypeHandle left, object? right)
{
if (right is RuntimeTypeHandle)
return !left.Equals((RuntimeTypeHandle)right);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Runtime;
using System.Runtime.CompilerServices;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.Versioning;

using Internal.Runtime.CompilerServices;
Expand Down Expand Up @@ -49,13 +50,15 @@ public static unsafe double CompareExchange(ref double location1, double value,

[Intrinsic]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T CompareExchange<T>(ref T location1, T value, T comparand) where T : class
[return: NotNullIfNotNull("location1")]
public static T CompareExchange<T>(ref T location1, T value, T comparand) where T : class?
{
return Unsafe.As<T>(RuntimeImports.InterlockedCompareExchange(ref Unsafe.As<T, object>(ref location1), value, comparand));
}

[Intrinsic]
public static object CompareExchange(ref object location1, object value, object comparand)
[return: NotNullIfNotNull("location1")]
public static object? CompareExchange(ref object? location1, object? value, object? comparand)
{
return RuntimeImports.InterlockedCompareExchange(ref location1, value, comparand);
}
Expand Down Expand Up @@ -121,13 +124,15 @@ public static unsafe double Exchange(ref double location1, double value)

[Intrinsic]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T Exchange<T>(ref T location1, T value) where T : class
[return: NotNullIfNotNull("location1")]
public static T Exchange<T>([NotNullIfNotNull("value")] ref T location1, T value) where T : class?
{
return Unsafe.As<T>(RuntimeImports.InterlockedExchange(ref Unsafe.As<T, object>(ref location1), value));
return Unsafe.As<T>(RuntimeImports.InterlockedExchange(ref Unsafe.As<T, object>(ref location1!), value));
}

[Intrinsic]
public static object Exchange(ref object location1, object value)
[return: NotNullIfNotNull("location1")]
public static object? Exchange([NotNullIfNotNull("value")] ref object? location1, object? value)
{
return RuntimeImports.InterlockedExchange(ref location1, value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private static Type GetTypeFromEETypePtrSlow(EETypePtr eeType, ref GCHandle hand
public static Type GetType(string typeName, Func<AssemblyName, Assembly> assemblyResolver, Func<Assembly, string, bool, Type> typeResolver, bool throwOnError, bool ignoreCase) => RuntimeAugments.Callbacks.GetType(typeName, assemblyResolver, typeResolver, throwOnError: throwOnError, ignoreCase: ignoreCase, defaultAssembly: null);

[Intrinsic]
public static bool operator ==(Type left, Type right)
public static bool operator ==(Type? left, Type? right)
{
if (object.ReferenceEquals(left, right))
return true;
Expand All @@ -102,7 +102,7 @@ private static Type GetTypeFromEETypePtrSlow(EETypePtr eeType, ref GCHandle hand
}

[Intrinsic]
public static bool operator !=(Type left, Type right) => !(left == right);
public static bool operator !=(Type? left, Type? right) => !(left == right);

public bool IsRuntimeImplemented() => this is IRuntimeImplemented; // Not an api but needs to be public because of Reflection.Core/CoreLib divide.
}
Expand Down

0 comments on commit 8afaa9d

Please sign in to comment.