Skip to content

Commit

Permalink
Nullable: Comparers, Dictionary and Friends (dotnet/coreclr#23971)
Browse files Browse the repository at this point in the history
* Nullable: Comparers, Dictionary and Friends

* Add object constraint to Dictionary

* Fix warning from new compiler and annotating dictionary

* PR Feedback

Signed-off-by: dotnet-bot <[email protected]>
  • Loading branch information
safern authored and krwq committed May 1, 2019
1 parent 2c833a5 commit 60118d4
Show file tree
Hide file tree
Showing 13 changed files with 223 additions and 207 deletions.
4 changes: 2 additions & 2 deletions src/Common/src/CoreLib/System/Array.cs
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ int IStructuralEquatable.GetHashCode(IEqualityComparer comparer)

for (int i = (this.Length >= 8 ? this.Length - 8 : 0); i < this.Length; i++)
{
ret = CombineHashCodes(ret, comparer!.GetHashCode(GetValue(i))); // TODO-NULLABLE: https://github.com/dotnet/csharplang/issues/538
ret = CombineHashCodes(ret, comparer!.GetHashCode(GetValue(i)!)); // TODO-NULLABLE: https://github.com/dotnet/csharplang/issues/538
}

return ret;
Expand Down Expand Up @@ -1592,7 +1592,7 @@ public static void Sort<T>(T[] array, Comparison<T> comparison)
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.comparison);
}

ArraySortHelper<T>.Sort(array!, 0, array!.Length, comparison); // TODO-NULLABLE: https://github.com/dotnet/csharplang/issues/538
ArraySortHelper<T>.Sort(array!, 0, array!.Length, comparison!); // TODO-NULLABLE: https://github.com/dotnet/csharplang/issues/538
}

public static bool TrueForAll<T>(T[] array, Predicate<T> match)
Expand Down
9 changes: 5 additions & 4 deletions src/Common/src/CoreLib/System/Collections/DictionaryEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable
using System.ComponentModel;

namespace System.Collections
Expand All @@ -13,11 +14,11 @@ namespace System.Collections
public struct DictionaryEntry
{
private object _key; // Do not rename (binary serialization)
private object _value; // Do not rename (binary serialization)
private object? _value; // Do not rename (binary serialization)

// Constructs a new DictionaryEnumerator by setting the Key
// and Value fields appropriately.
public DictionaryEntry(object key, object value)
public DictionaryEntry(object key, object? value)
{
_key = key;
_value = value;
Expand All @@ -36,7 +37,7 @@ public object Key
}
}

public object Value
public object? Value
{
get
{
Expand All @@ -50,7 +51,7 @@ public object Value
}

[EditorBrowsable(EditorBrowsableState.Never)]
public void Deconstruct(out object key, out object value)
public void Deconstruct(out object key, out object? value)
{
key = Key;
value = Value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
**
===========================================================*/

#nullable enable
using System.Diagnostics;
using System.Runtime.CompilerServices;

Expand All @@ -38,7 +39,7 @@ internal static int FloorLog2PlusOne(int n)
return result;
}

internal static void ThrowOrIgnoreBadComparer(object comparer)
internal static void ThrowOrIgnoreBadComparer(object? comparer)
{
throw new ArgumentException(SR.Format(SR.Arg_BogusIComparer, comparer));
}
Expand All @@ -48,7 +49,7 @@ internal partial class ArraySortHelper<T>
{
#region IArraySortHelper<T> Members

public void Sort(T[] keys, int index, int length, IComparer<T> comparer)
public void Sort(T[] keys, int index, int length, IComparer<T>? comparer)
{
Debug.Assert(keys != null, "Check the arguments in the caller!");
Debug.Assert(index >= 0 && length >= 0 && (keys.Length - index >= length), "Check the arguments in the caller!");
Expand All @@ -74,7 +75,7 @@ public void Sort(T[] keys, int index, int length, IComparer<T> comparer)
}
}

public int BinarySearch(T[] array, int index, int length, T value, IComparer<T> comparer)
public int BinarySearch(T[] array, int index, int length, T value, IComparer<T>? comparer)
{
try
{
Expand Down Expand Up @@ -335,7 +336,7 @@ internal partial class GenericArraySortHelper<T>

#region IArraySortHelper<T> Members

public void Sort(T[] keys, int index, int length, IComparer<T> comparer)
public void Sort(T[] keys, int index, int length, IComparer<T>? comparer)
{
Debug.Assert(keys != null, "Check the arguments in the caller!");
Debug.Assert(index >= 0 && length >= 0 && (keys.Length - index >= length), "Check the arguments in the caller!");
Expand All @@ -361,7 +362,7 @@ public void Sort(T[] keys, int index, int length, IComparer<T> comparer)
}
}

public int BinarySearch(T[] array, int index, int length, T value, IComparer<T> comparer)
public int BinarySearch(T[] array, int index, int length, T value, IComparer<T>? comparer)
{
Debug.Assert(array != null, "Check the arguments in the caller!");
Debug.Assert(index >= 0 && length >= 0 && (array.Length - index >= length), "Check the arguments in the caller!");
Expand Down Expand Up @@ -624,7 +625,7 @@ private static void InsertionSort(T[] keys, int lo, int hi)

internal partial class ArraySortHelper<TKey, TValue>
{
public void Sort(TKey[] keys, TValue[] values, int index, int length, IComparer<TKey> comparer)
public void Sort(TKey[] keys, TValue[] values, int index, int length, IComparer<TKey>? comparer)
{
Debug.Assert(keys != null, "Check the arguments in the caller!"); // Precondition on interface method
Debug.Assert(values != null, "Check the arguments in the caller!");
Expand Down Expand Up @@ -871,7 +872,7 @@ private static void InsertionSort(TKey[] keys, TValue[] values, int lo, int hi,
internal partial class GenericArraySortHelper<TKey, TValue>
where TKey : IComparable<TKey>
{
public void Sort(TKey[] keys, TValue[] values, int index, int length, IComparer<TKey> comparer)
public void Sort(TKey[] keys, TValue[] values, int index, int length, IComparer<TKey>? comparer)
{
Debug.Assert(keys != null, "Check the arguments in the caller!");
Debug.Assert(index >= 0 && length >= 0 && (keys.Length - index >= length), "Check the arguments in the caller!");
Expand Down
17 changes: 9 additions & 8 deletions src/Common/src/CoreLib/System/Collections/Generic/Comparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;

Expand All @@ -21,9 +22,9 @@ public static Comparer<T> Create(Comparison<T> comparison)
return new ComparisonComparer<T>(comparison);
}

public abstract int Compare(T x, T y);
public abstract int Compare(T x, T y); // TODO-NULLABLE-GENERIC: x and y must be marked as nullable

int IComparer.Compare(object x, object y)
int IComparer.Compare(object? x, object? y)
{
if (x == null) return y == null ? 0 : -1;
if (y == null) return 1;
Expand Down Expand Up @@ -58,7 +59,7 @@ public override int Compare(T x, T y)
// Needs to be public to support binary serialization compatibility
public sealed partial class GenericComparer<T> : Comparer<T> where T : IComparable<T>
{
public override int Compare(T x, T y)
public override int Compare(T x, T y) // TODO-NULLABLE-GENERIC: x and y must be marked as nullable
{
if (x != null)
{
Expand All @@ -70,7 +71,7 @@ public override int Compare(T x, T y)
}

// Equals method for the comparer itself.
public override bool Equals(object obj) =>
public override bool Equals(object? obj) =>
obj != null && GetType() == obj.GetType();

public override int GetHashCode() =>
Expand All @@ -94,7 +95,7 @@ public override int Compare(T? x, T? y)
}

// Equals method for the comparer itself.
public override bool Equals(object obj) =>
public override bool Equals(object? obj) =>
obj != null && GetType() == obj.GetType();

public override int GetHashCode() =>
Expand All @@ -106,13 +107,13 @@ public override int GetHashCode() =>
// Needs to be public to support binary serialization compatibility
public sealed partial class ObjectComparer<T> : Comparer<T>
{
public override int Compare(T x, T y)
public override int Compare(T x, T y) // TODO-NULLABLE-GENERIC: x and y must be marked as nullable
{
return System.Collections.Comparer.Default.Compare(x, y);
}

// Equals method for the comparer itself.
public override bool Equals(object obj) =>
public override bool Equals(object? obj) =>
obj != null && GetType() == obj.GetType();

public override int GetHashCode() =>
Expand All @@ -130,7 +131,7 @@ private EnumComparer(SerializationInfo info, StreamingContext context) { }
// public override int Compare(T x, T y) is runtime-specific

// Equals method for the comparer itself.
public override bool Equals(object obj) =>
public override bool Equals(object? obj) =>
obj != null && GetType() == obj.GetType();

public override int GetHashCode() =>
Expand Down
Loading

0 comments on commit 60118d4

Please sign in to comment.