diff --git a/src/CommunityToolkit.HighPerformance/Buffers/StringPool.cs b/src/CommunityToolkit.HighPerformance/Buffers/StringPool.cs index a7aa0c21c..3cb8f12e1 100644 --- a/src/CommunityToolkit.HighPerformance/Buffers/StringPool.cs +++ b/src/CommunityToolkit.HighPerformance/Buffers/StringPool.cs @@ -493,7 +493,7 @@ public void Reset() private unsafe ref string TryGet(ReadOnlySpan span, int hashcode) { ref MapEntry mapEntriesRef = ref this.mapEntries.DangerousGetReference(); - ref MapEntry entry = ref Unsafe.NullRef(); + ref MapEntry entry = ref *(MapEntry*)null; int length = this.buckets.Length; int bucketIndex = hashcode & (length - 1); @@ -512,7 +512,7 @@ private unsafe ref string TryGet(ReadOnlySpan span, int hashcode) } } - return ref Unsafe.NullRef(); + return ref *(string*)null; } /// diff --git a/src/CommunityToolkit.HighPerformance/Extensions/NullableExtensions.cs b/src/CommunityToolkit.HighPerformance/Extensions/NullableExtensions.cs index 875042c7a..98fe1f38b 100644 --- a/src/CommunityToolkit.HighPerformance/Extensions/NullableExtensions.cs +++ b/src/CommunityToolkit.HighPerformance/Extensions/NullableExtensions.cs @@ -50,11 +50,11 @@ public static ref T DangerousGetValueOrDefaultReference(this ref T? value) /// A reference to the value of the input instance, or a reference. /// The returned reference can be tested for using . [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static ref T DangerousGetValueOrNullReference(ref this T? value) + public static unsafe ref T DangerousGetValueOrNullReference(ref this T? value) where T : struct { #if NET7_0_OR_GREATER - ref T resultRef = ref Unsafe.NullRef(); + ref T resultRef = ref *(T*)null; // This pattern ensures that the resulting code ends up having a single return, and a single // forward branch (the one where the value is null) that is predicted non taken. That is, @@ -80,7 +80,7 @@ public static ref T DangerousGetValueOrNullReference(ref this T? value) return ref Unsafe.As>(ref value).Value; } - return ref Unsafe.NullRef(); + return ref *(T*)null; #endif } diff --git a/src/CommunityToolkit.Mvvm/Messaging/Internals/System/Collections.Generic/Dictionary2.cs b/src/CommunityToolkit.Mvvm/Messaging/Internals/System/Collections.Generic/Dictionary2.cs index 15fa9ef22..c3ac541a7 100644 --- a/src/CommunityToolkit.Mvvm/Messaging/Internals/System/Collections.Generic/Dictionary2.cs +++ b/src/CommunityToolkit.Mvvm/Messaging/Internals/System/Collections.Generic/Dictionary2.cs @@ -340,9 +340,9 @@ public TValue GetValue() /// /// Key to look for. /// Reference to the existing value. - private ref TValue FindValue(TKey key) + private unsafe ref TValue FindValue(TKey key) { - ref Entry entry = ref Unsafe.NullRef(); + ref Entry entry = ref *(Entry*)null; uint hashCode = (uint)key.GetHashCode(); int i = GetBucket(hashCode); Entry[] entries = this.entries; @@ -373,7 +373,7 @@ private ref TValue FindValue(TKey key) return ref value; ReturnNotFound: - value = ref Unsafe.NullRef(); + value = ref *(TValue*)null; goto Return; } diff --git a/tests/CommunityToolkit.HighPerformance.UnitTests/Enumerables/Test_ReadOnlyRefEnumerable{T}.cs b/tests/CommunityToolkit.HighPerformance.UnitTests/Enumerables/Test_ReadOnlyRefEnumerable{T}.cs index 9012fcab7..964914d6a 100644 --- a/tests/CommunityToolkit.HighPerformance.UnitTests/Enumerables/Test_ReadOnlyRefEnumerable{T}.cs +++ b/tests/CommunityToolkit.HighPerformance.UnitTests/Enumerables/Test_ReadOnlyRefEnumerable{T}.cs @@ -5,7 +5,6 @@ #if NET6_0_OR_GREATER using System; -using System.Runtime.CompilerServices; using CommunityToolkit.HighPerformance.Enumerables; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -41,9 +40,9 @@ public void Test_ReadOnlyRefEnumerable_DangerousCreate_Ok(int length, int step, [DataRow(10, -14)] [DataRow(-32, -1)] [ExpectedException(typeof(ArgumentOutOfRangeException))] - public void Test_ReadOnlyRefEnumerable_DangerousCreate_BelowZero(int length, int step) + public unsafe void Test_ReadOnlyRefEnumerable_DangerousCreate_BelowZero(int length, int step) { - _ = ReadOnlyRefEnumerable.DangerousCreate(in Unsafe.NullRef(), length, step); + _ = ReadOnlyRefEnumerable.DangerousCreate(in *(int*)null, length, step); } [TestMethod] diff --git a/tests/CommunityToolkit.HighPerformance.UnitTests/Enumerables/Test_RefEnumerable{T}.cs b/tests/CommunityToolkit.HighPerformance.UnitTests/Enumerables/Test_RefEnumerable{T}.cs index 6deb4afcc..589e7d650 100644 --- a/tests/CommunityToolkit.HighPerformance.UnitTests/Enumerables/Test_RefEnumerable{T}.cs +++ b/tests/CommunityToolkit.HighPerformance.UnitTests/Enumerables/Test_RefEnumerable{T}.cs @@ -5,7 +5,6 @@ #if NET6_0_OR_GREATER using System; -using System.Runtime.CompilerServices; using CommunityToolkit.HighPerformance.Enumerables; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -41,9 +40,9 @@ public void Test_RefEnumerable_DangerousCreate_Ok(int length, int step, int[] va [DataRow(10, -14)] [DataRow(-32, -1)] [ExpectedException(typeof(ArgumentOutOfRangeException))] - public void Test_RefEnumerable_DangerousCreate_BelowZero(int length, int step) + public unsafe void Test_RefEnumerable_DangerousCreate_BelowZero(int length, int step) { - _ = RefEnumerable.DangerousCreate(ref Unsafe.NullRef(), length, step); + _ = RefEnumerable.DangerousCreate(ref *(int*)null, length, step); } [TestMethod]