Skip to content
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

Replace default(T) != null with typeof(T).IsValueType #32098

Merged
merged 1 commit into from
Feb 12, 2020
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 @@ -1671,10 +1671,7 @@ private static int GetDefaultChunkSize<TSource>()
{
int chunkSize;

// Because of the lack of typeof(T).IsValueType we need two pieces of information
// to determine this. default(T) will return a non null for Value Types, except those
// using Nullable<>, that is why we need a second condition.
if (default(TSource) != null || Nullable.GetUnderlyingType(typeof(TSource)) != null)
if (typeof(TSource).IsValueType)
{
// Marshal.SizeOf fails for value types that don't have explicit layouts. We
// just fall back to some arbitrary constant in that case. Is there a better way?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ internal static partial class ImmutableExtensions
{
internal static bool IsValueType<T>()
{
#if NETCOREAPP
return typeof(T).IsValueType;
#else
jkotas marked this conversation as resolved.
Show resolved Hide resolved
if (default(T) != null)
{
return true;
Expand All @@ -29,6 +32,7 @@ internal static bool IsValueType<T>()
}

return false;
#endif
}

#if EqualsStructurally
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ public bool Contains(T item)
{
int hashCode = item == null ? 0 : InternalGetHashCode(item.GetHashCode());

if (default(T) != null)
if (typeof(T).IsValueType)
{
// see note at "HashSet" level describing why "- 1" appears in for loop
for (int i = buckets[hashCode % buckets.Length] - 1; i >= 0; i = slots[i].next)
Expand Down Expand Up @@ -368,7 +368,7 @@ public bool Remove(T item)
hashCode = item == null ? 0 : InternalGetHashCode(item.GetHashCode());
bucket = hashCode % _buckets!.Length;

if (default(T) != null)
if (typeof(T).IsValueType)
{
for (i = _buckets[bucket] - 1; i >= 0; last = i, i = slots[i].next)
{
Expand Down Expand Up @@ -1341,7 +1341,7 @@ private bool AddIfNotPresent(T value)
hashCode = value == null ? 0 : InternalGetHashCode(value.GetHashCode());
bucket = hashCode % _buckets!.Length;

if (default(T) != null)
if (typeof(T).IsValueType)
{
for (int i = _buckets[bucket] - 1; i >= 0; i = slots[i].next)
{
Expand Down Expand Up @@ -1577,7 +1577,7 @@ private int InternalIndexOf(T item)
{
int hashCode = item == null ? 0 : InternalGetHashCode(item.GetHashCode());

if (default(T) != null)
if (typeof(T).IsValueType)
{
// see note at "HashSet" level describing why "- 1" appears in for loop
for (int i = buckets[hashCode % buckets.Length] - 1; i >= 0; i = slots[i].next)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,7 @@ internal static int GetDefaultChunkSize<T>()
{
int chunkSize;

// Because of the lack of typeof(T).IsValueType we need two pieces of information
// to determine this. default(T) will return a non null for Value Types, except those
// using Nullable<>, that is why we need a second condition.
if (default(T) != null || Nullable.GetUnderlyingType(typeof(T)) != null)
if (typeof(T).IsValueType)
{
// Marshal.SizeOf fails for value types that don't have explicit layouts. We
// just fall back to some arbitrary constant in that case. Is there a better way?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ public bool ContainsValue(TValue value)
}
else
{
if (default(TValue) != null)
if (typeof(TValue).IsValueType)
{
// ValueType: Devirtualize with EqualityComparer<TValue>.Default intrinsic
for (int i = 0; i < _count; i++)
Expand Down Expand Up @@ -344,7 +344,7 @@ private ref TValue FindValue(TKey key)
int i = GetBucket(hashCode);
Entry[]? entries = _entries;
uint collisionCount = 0;
if (default(TKey) != null)
if (typeof(TKey).IsValueType)
{
// ValueType: Devirtualize with EqualityComparer<TValue>.Default intrinsic

Expand Down Expand Up @@ -495,7 +495,7 @@ private bool TryInsert(TKey key, TValue value, InsertionBehavior behavior)

if (comparer == null)
{
if (default(TKey) != null)
if (typeof(TKey).IsValueType)
{
// ValueType: Devirtualize with EqualityComparer<TValue>.Default intrinsic
while (true)
Expand Down Expand Up @@ -658,7 +658,7 @@ private bool TryInsert(TKey key, TValue value, InsertionBehavior behavior)
_version++;

// Value types never rehash
if (default(TKey) == null && collisionCount > HashHelpers.HashCollisionThreshold && comparer is NonRandomizedStringEqualityComparer)
if (!typeof(TKey).IsValueType && collisionCount > HashHelpers.HashCollisionThreshold && comparer is NonRandomizedStringEqualityComparer)
{
// If we hit the collision threshold we'll need to switch to the comparer which is using randomized string hashing
// i.e. EqualityComparer<string>.Default.
Expand Down Expand Up @@ -720,7 +720,7 @@ private void Resize()
private void Resize(int newSize, bool forceNewHashCodes)
{
// Value types never rehash
Debug.Assert(!forceNewHashCodes || default(TKey) == null);
Debug.Assert(!forceNewHashCodes || !typeof(TKey).IsValueType);
Debug.Assert(_entries != null, "_entries should be non-null");
Debug.Assert(newSize >= _entries.Length);

Expand All @@ -729,7 +729,7 @@ private void Resize(int newSize, bool forceNewHashCodes)
int count = _count;
Array.Copy(_entries, entries, count);

if (default(TKey) == null && forceNewHashCodes)
if (!typeof(TKey).IsValueType && forceNewHashCodes)
{
for (int i = 0; i < count; i++)
{
Expand Down
6 changes: 3 additions & 3 deletions src/libraries/System.Private.CoreLib/src/System/Memory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public Memory(T[]? array)
this = default;
return; // returns default
}
if (default(T) == null && array.GetType() != typeof(T[]))
if (!typeof(T).IsValueType && array.GetType() != typeof(T[]))
ThrowHelper.ThrowArrayTypeMismatchException();

_object = array;
Expand All @@ -71,7 +71,7 @@ internal Memory(T[]? array, int start)
this = default;
return; // returns default
}
if (default(T) == null && array.GetType() != typeof(T[]))
if (!typeof(T).IsValueType && array.GetType() != typeof(T[]))
ThrowHelper.ThrowArrayTypeMismatchException();
if ((uint)start > (uint)array.Length)
ThrowHelper.ThrowArgumentOutOfRangeException();
Expand Down Expand Up @@ -103,7 +103,7 @@ public Memory(T[]? array, int start, int length)
this = default;
return; // returns default
}
if (default(T) == null && array.GetType() != typeof(T[]))
if (!typeof(T).IsValueType && array.GetType() != typeof(T[]))
ThrowHelper.ThrowArrayTypeMismatchException();
#if TARGET_64BIT
// See comment in Span<T>.Slice for how this works.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static Span<T> AsSpan<T>(this T[]? array, int start)
ThrowHelper.ThrowArgumentOutOfRangeException();
return default;
}
if (default(T) == null && array.GetType() != typeof(T[]))
if (!typeof(T).IsValueType && array.GetType() != typeof(T[]))
ThrowHelper.ThrowArrayTypeMismatchException();
if ((uint)start > (uint)array.Length)
ThrowHelper.ThrowArgumentOutOfRangeException();
Expand All @@ -56,7 +56,7 @@ public static Span<T> AsSpan<T>(this T[]? array, Index startIndex)
return default;
}

if (default(T) == null && array.GetType() != typeof(T[]))
if (!typeof(T).IsValueType && array.GetType() != typeof(T[]))
ThrowHelper.ThrowArrayTypeMismatchException();

int actualIndex = startIndex.GetOffset(array.Length);
Expand All @@ -83,7 +83,7 @@ public static Span<T> AsSpan<T>(this T[]? array, Range range)
return default;
}

if (default(T) == null && array.GetType() != typeof(T[]))
if (!typeof(T).IsValueType && array.GetType() != typeof(T[]))
ThrowHelper.ThrowArrayTypeMismatchException();

(int start, int length) = range.GetOffsetAndLength(array.Length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static T[] GetSubArray<T>(T[] array, Range range)

(int offset, int length) = range.GetOffsetAndLength(array.Length);

if (default(T) != null || typeof(T[]) == array.GetType())
if (typeof(T).IsValueType || typeof(T[]) == array.GetType())
{
// We know the type of the array to be exactly T[].

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ public static Memory<T> CreateFromPinnedArray<T>(T[]? array, int start, int leng
ThrowHelper.ThrowArgumentOutOfRangeException();
return default;
}
if (default(T) == null && array.GetType() != typeof(T[]))
if (!typeof(T).IsValueType && array.GetType() != typeof(T[]))
ThrowHelper.ThrowArrayTypeMismatchException();
if ((uint)start > (uint)array.Length || (uint)length > (uint)(array.Length - start))
ThrowHelper.ThrowArgumentOutOfRangeException();
Expand Down
4 changes: 2 additions & 2 deletions src/libraries/System.Private.CoreLib/src/System/Span.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public Span(T[]? array)
this = default;
return; // returns default
}
if (default(T) == null && array.GetType() != typeof(T[]))
if (!typeof(T).IsValueType && array.GetType() != typeof(T[]))
ThrowHelper.ThrowArrayTypeMismatchException();

_pointer = new ByReference<T>(ref MemoryMarshal.GetArrayDataReference(array));
Expand Down Expand Up @@ -79,7 +79,7 @@ public Span(T[]? array, int start, int length)
this = default;
return; // returns default
}
if (default(T) == null && array.GetType() != typeof(T[]))
if (!typeof(T).IsValueType && array.GetType() != typeof(T[]))
ThrowHelper.ThrowArrayTypeMismatchException();
#if TARGET_64BIT
// See comment in Span<T>.Slice for how this works.
Expand Down