From: buyaa-n Date: Mon, 16 Dec 2019 21:09:45 +0000 (-0800) Subject: Annotate System.Collections.Immutable for nullable (#367) X-Git-Tag: submit/tizen/20210909.063632~10665 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=1983518fa4c901d35bb3ac7b1f76d7fa810c3543;p=platform%2Fupstream%2Fdotnet%2Fruntime.git Annotate System.Collections.Immutable for nullable (#367) * Annotate System.Collections.Immutable for nullable --- diff --git a/src/libraries/System.Collections.Immutable/ref/System.Collections.Immutable.cs b/src/libraries/System.Collections.Immutable/ref/System.Collections.Immutable.cs index 6805291..0772ea7 100644 --- a/src/libraries/System.Collections.Immutable/ref/System.Collections.Immutable.cs +++ b/src/libraries/System.Collections.Immutable/ref/System.Collections.Immutable.cs @@ -24,16 +24,16 @@ namespace System.Collections.Immutable System.Collections.Immutable.IImmutableList Add(T value); System.Collections.Immutable.IImmutableList AddRange(System.Collections.Generic.IEnumerable items); System.Collections.Immutable.IImmutableList Clear(); - int IndexOf(T item, int index, int count, System.Collections.Generic.IEqualityComparer equalityComparer); + int IndexOf(T item, int index, int count, System.Collections.Generic.IEqualityComparer? equalityComparer); System.Collections.Immutable.IImmutableList Insert(int index, T element); System.Collections.Immutable.IImmutableList InsertRange(int index, System.Collections.Generic.IEnumerable items); - int LastIndexOf(T item, int index, int count, System.Collections.Generic.IEqualityComparer equalityComparer); - System.Collections.Immutable.IImmutableList Remove(T value, System.Collections.Generic.IEqualityComparer equalityComparer); + int LastIndexOf(T item, int index, int count, System.Collections.Generic.IEqualityComparer? equalityComparer); + System.Collections.Immutable.IImmutableList Remove(T value, System.Collections.Generic.IEqualityComparer? equalityComparer); System.Collections.Immutable.IImmutableList RemoveAll(System.Predicate match); System.Collections.Immutable.IImmutableList RemoveAt(int index); - System.Collections.Immutable.IImmutableList RemoveRange(System.Collections.Generic.IEnumerable items, System.Collections.Generic.IEqualityComparer equalityComparer); + System.Collections.Immutable.IImmutableList RemoveRange(System.Collections.Generic.IEnumerable items, System.Collections.Generic.IEqualityComparer? equalityComparer); System.Collections.Immutable.IImmutableList RemoveRange(int index, int count); - System.Collections.Immutable.IImmutableList Replace(T oldValue, T newValue, System.Collections.Generic.IEqualityComparer equalityComparer); + System.Collections.Immutable.IImmutableList Replace(T oldValue, T newValue, System.Collections.Generic.IEqualityComparer? equalityComparer); System.Collections.Immutable.IImmutableList SetItem(int index, T value); } public partial interface IImmutableQueue : System.Collections.Generic.IEnumerable, System.Collections.IEnumerable @@ -73,9 +73,9 @@ namespace System.Collections.Immutable public static partial class ImmutableArray { public static int BinarySearch(this System.Collections.Immutable.ImmutableArray array, int index, int length, T value) { throw null; } - public static int BinarySearch(this System.Collections.Immutable.ImmutableArray array, int index, int length, T value, System.Collections.Generic.IComparer comparer) { throw null; } + public static int BinarySearch(this System.Collections.Immutable.ImmutableArray array, int index, int length, T value, System.Collections.Generic.IComparer? comparer) { throw null; } public static int BinarySearch(this System.Collections.Immutable.ImmutableArray array, T value) { throw null; } - public static int BinarySearch(this System.Collections.Immutable.ImmutableArray array, T value, System.Collections.Generic.IComparer comparer) { throw null; } + public static int BinarySearch(this System.Collections.Immutable.ImmutableArray array, T value, System.Collections.Generic.IComparer? comparer) { throw null; } public static System.Collections.Immutable.ImmutableArray.Builder CreateBuilder() { throw null; } public static System.Collections.Immutable.ImmutableArray.Builder CreateBuilder(int initialCapacity) { throw null; } public static System.Collections.Immutable.ImmutableArray CreateRange(System.Collections.Generic.IEnumerable items) { throw null; } @@ -115,7 +115,7 @@ namespace System.Collections.Immutable object System.Collections.ICollection.SyncRoot { get { throw null; } } bool System.Collections.IList.IsFixedSize { get { throw null; } } bool System.Collections.IList.IsReadOnly { get { throw null; } } - object System.Collections.IList.this[int index] { get { throw null; } set { } } + object? System.Collections.IList.this[int index] { get { throw null; } set { } } public System.Collections.Immutable.ImmutableArray Add(T item) { throw null; } public System.Collections.Immutable.ImmutableArray AddRange(System.Collections.Generic.IEnumerable items) { throw null; } public System.Collections.Immutable.ImmutableArray AddRange(System.Collections.Immutable.ImmutableArray items) { throw null; } @@ -132,14 +132,14 @@ namespace System.Collections.Immutable public void CopyTo(T[] destination) { } public void CopyTo(T[] destination, int destinationIndex) { } public bool Equals(System.Collections.Immutable.ImmutableArray other) { throw null; } - public override bool Equals(object obj) { throw null; } + public override bool Equals(object? obj) { throw null; } public System.Collections.Immutable.ImmutableArray.Enumerator GetEnumerator() { throw null; } public override int GetHashCode() { throw null; } public int IndexOf(T item) { throw null; } public int IndexOf(T item, int startIndex) { throw null; } - public int IndexOf(T item, int startIndex, System.Collections.Generic.IEqualityComparer equalityComparer) { throw null; } + public int IndexOf(T item, int startIndex, System.Collections.Generic.IEqualityComparer? equalityComparer) { throw null; } public int IndexOf(T item, int startIndex, int count) { throw null; } - public int IndexOf(T item, int startIndex, int count, System.Collections.Generic.IEqualityComparer equalityComparer) { throw null; } + public int IndexOf(T item, int startIndex, int count, System.Collections.Generic.IEqualityComparer? equalityComparer) { throw null; } public System.Collections.Immutable.ImmutableArray Insert(int index, T item) { throw null; } public System.Collections.Immutable.ImmutableArray InsertRange(int index, System.Collections.Generic.IEnumerable items) { throw null; } public System.Collections.Immutable.ImmutableArray InsertRange(int index, System.Collections.Immutable.ImmutableArray items) { throw null; } @@ -149,28 +149,28 @@ namespace System.Collections.Immutable public int LastIndexOf(T item) { throw null; } public int LastIndexOf(T item, int startIndex) { throw null; } public int LastIndexOf(T item, int startIndex, int count) { throw null; } - public int LastIndexOf(T item, int startIndex, int count, System.Collections.Generic.IEqualityComparer equalityComparer) { throw null; } + public int LastIndexOf(T item, int startIndex, int count, System.Collections.Generic.IEqualityComparer? equalityComparer) { throw null; } public System.Collections.Generic.IEnumerable OfType() { throw null; } public static bool operator ==(System.Collections.Immutable.ImmutableArray left, System.Collections.Immutable.ImmutableArray right) { throw null; } public static bool operator ==(System.Collections.Immutable.ImmutableArray? left, System.Collections.Immutable.ImmutableArray? right) { throw null; } public static bool operator !=(System.Collections.Immutable.ImmutableArray left, System.Collections.Immutable.ImmutableArray right) { throw null; } public static bool operator !=(System.Collections.Immutable.ImmutableArray? left, System.Collections.Immutable.ImmutableArray? right) { throw null; } public System.Collections.Immutable.ImmutableArray Remove(T item) { throw null; } - public System.Collections.Immutable.ImmutableArray Remove(T item, System.Collections.Generic.IEqualityComparer equalityComparer) { throw null; } + public System.Collections.Immutable.ImmutableArray Remove(T item, System.Collections.Generic.IEqualityComparer? equalityComparer) { throw null; } public System.Collections.Immutable.ImmutableArray RemoveAll(System.Predicate match) { throw null; } public System.Collections.Immutable.ImmutableArray RemoveAt(int index) { throw null; } public System.Collections.Immutable.ImmutableArray RemoveRange(System.Collections.Generic.IEnumerable items) { throw null; } - public System.Collections.Immutable.ImmutableArray RemoveRange(System.Collections.Generic.IEnumerable items, System.Collections.Generic.IEqualityComparer equalityComparer) { throw null; } + public System.Collections.Immutable.ImmutableArray RemoveRange(System.Collections.Generic.IEnumerable items, System.Collections.Generic.IEqualityComparer? equalityComparer) { throw null; } public System.Collections.Immutable.ImmutableArray RemoveRange(System.Collections.Immutable.ImmutableArray items) { throw null; } - public System.Collections.Immutable.ImmutableArray RemoveRange(System.Collections.Immutable.ImmutableArray items, System.Collections.Generic.IEqualityComparer equalityComparer) { throw null; } + public System.Collections.Immutable.ImmutableArray RemoveRange(System.Collections.Immutable.ImmutableArray items, System.Collections.Generic.IEqualityComparer? equalityComparer) { throw null; } public System.Collections.Immutable.ImmutableArray RemoveRange(int index, int length) { throw null; } public System.Collections.Immutable.ImmutableArray Replace(T oldValue, T newValue) { throw null; } - public System.Collections.Immutable.ImmutableArray Replace(T oldValue, T newValue, System.Collections.Generic.IEqualityComparer equalityComparer) { throw null; } + public System.Collections.Immutable.ImmutableArray Replace(T oldValue, T newValue, System.Collections.Generic.IEqualityComparer? equalityComparer) { throw null; } public System.Collections.Immutable.ImmutableArray SetItem(int index, T item) { throw null; } public System.Collections.Immutable.ImmutableArray Sort() { throw null; } - public System.Collections.Immutable.ImmutableArray Sort(System.Collections.Generic.IComparer comparer) { throw null; } + public System.Collections.Immutable.ImmutableArray Sort(System.Collections.Generic.IComparer? comparer) { throw null; } public System.Collections.Immutable.ImmutableArray Sort(System.Comparison comparison) { throw null; } - public System.Collections.Immutable.ImmutableArray Sort(int index, int count, System.Collections.Generic.IComparer comparer) { throw null; } + public System.Collections.Immutable.ImmutableArray Sort(int index, int count, System.Collections.Generic.IComparer? comparer) { throw null; } void System.Collections.Generic.ICollection.Add(T item) { } void System.Collections.Generic.ICollection.Clear() { } bool System.Collections.Generic.ICollection.Remove(T item) { throw null; } @@ -179,27 +179,27 @@ namespace System.Collections.Immutable void System.Collections.Generic.IList.RemoveAt(int index) { } void System.Collections.ICollection.CopyTo(System.Array array, int index) { } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { throw null; } - int System.Collections.IList.Add(object value) { throw null; } + int System.Collections.IList.Add(object? value) { throw null; } void System.Collections.IList.Clear() { } - bool System.Collections.IList.Contains(object value) { throw null; } - int System.Collections.IList.IndexOf(object value) { throw null; } - void System.Collections.IList.Insert(int index, object value) { } - void System.Collections.IList.Remove(object value) { } + bool System.Collections.IList.Contains(object? value) { throw null; } + int System.Collections.IList.IndexOf(object? value) { throw null; } + void System.Collections.IList.Insert(int index, object? value) { } + void System.Collections.IList.Remove(object? value) { } void System.Collections.IList.RemoveAt(int index) { } System.Collections.Immutable.IImmutableList System.Collections.Immutable.IImmutableList.Add(T value) { throw null; } System.Collections.Immutable.IImmutableList System.Collections.Immutable.IImmutableList.AddRange(System.Collections.Generic.IEnumerable items) { throw null; } System.Collections.Immutable.IImmutableList System.Collections.Immutable.IImmutableList.Clear() { throw null; } System.Collections.Immutable.IImmutableList System.Collections.Immutable.IImmutableList.Insert(int index, T element) { throw null; } System.Collections.Immutable.IImmutableList System.Collections.Immutable.IImmutableList.InsertRange(int index, System.Collections.Generic.IEnumerable items) { throw null; } - System.Collections.Immutable.IImmutableList System.Collections.Immutable.IImmutableList.Remove(T value, System.Collections.Generic.IEqualityComparer equalityComparer) { throw null; } + System.Collections.Immutable.IImmutableList System.Collections.Immutable.IImmutableList.Remove(T value, System.Collections.Generic.IEqualityComparer? equalityComparer) { throw null; } System.Collections.Immutable.IImmutableList System.Collections.Immutable.IImmutableList.RemoveAll(System.Predicate match) { throw null; } System.Collections.Immutable.IImmutableList System.Collections.Immutable.IImmutableList.RemoveAt(int index) { throw null; } - System.Collections.Immutable.IImmutableList System.Collections.Immutable.IImmutableList.RemoveRange(System.Collections.Generic.IEnumerable items, System.Collections.Generic.IEqualityComparer equalityComparer) { throw null; } + System.Collections.Immutable.IImmutableList System.Collections.Immutable.IImmutableList.RemoveRange(System.Collections.Generic.IEnumerable items, System.Collections.Generic.IEqualityComparer? equalityComparer) { throw null; } System.Collections.Immutable.IImmutableList System.Collections.Immutable.IImmutableList.RemoveRange(int index, int count) { throw null; } - System.Collections.Immutable.IImmutableList System.Collections.Immutable.IImmutableList.Replace(T oldValue, T newValue, System.Collections.Generic.IEqualityComparer equalityComparer) { throw null; } + System.Collections.Immutable.IImmutableList System.Collections.Immutable.IImmutableList.Replace(T oldValue, T newValue, System.Collections.Generic.IEqualityComparer? equalityComparer) { throw null; } System.Collections.Immutable.IImmutableList System.Collections.Immutable.IImmutableList.SetItem(int index, T value) { throw null; } - int System.Collections.IStructuralComparable.CompareTo(object other, System.Collections.IComparer comparer) { throw null; } - bool System.Collections.IStructuralEquatable.Equals(object other, System.Collections.IEqualityComparer comparer) { throw null; } + int System.Collections.IStructuralComparable.CompareTo(object? other, System.Collections.IComparer comparer) { throw null; } + bool System.Collections.IStructuralEquatable.Equals(object? other, System.Collections.IEqualityComparer comparer) { throw null; } int System.Collections.IStructuralEquatable.GetHashCode(System.Collections.IEqualityComparer comparer) { throw null; } public System.Collections.Immutable.ImmutableArray.Builder ToBuilder() { throw null; } public sealed partial class Builder : System.Collections.Generic.ICollection, System.Collections.Generic.IEnumerable, System.Collections.Generic.IList, System.Collections.Generic.IReadOnlyCollection, System.Collections.Generic.IReadOnlyList, System.Collections.IEnumerable @@ -226,7 +226,7 @@ namespace System.Collections.Immutable public int IndexOf(T item) { throw null; } public int IndexOf(T item, int startIndex) { throw null; } public int IndexOf(T item, int startIndex, int count) { throw null; } - public int IndexOf(T item, int startIndex, int count, System.Collections.Generic.IEqualityComparer equalityComparer) { throw null; } + public int IndexOf(T item, int startIndex, int count, System.Collections.Generic.IEqualityComparer? equalityComparer) { throw null; } public void Insert(int index, T item) { } #if !NETSTANDARD1_0 public ref readonly T ItemRef(int index) { throw null; } @@ -234,15 +234,15 @@ namespace System.Collections.Immutable public int LastIndexOf(T item) { throw null; } public int LastIndexOf(T item, int startIndex) { throw null; } public int LastIndexOf(T item, int startIndex, int count) { throw null; } - public int LastIndexOf(T item, int startIndex, int count, System.Collections.Generic.IEqualityComparer equalityComparer) { throw null; } + public int LastIndexOf(T item, int startIndex, int count, System.Collections.Generic.IEqualityComparer? equalityComparer) { throw null; } public System.Collections.Immutable.ImmutableArray MoveToImmutable() { throw null; } public bool Remove(T element) { throw null; } public void RemoveAt(int index) { } public void Reverse() { } public void Sort() { } - public void Sort(System.Collections.Generic.IComparer comparer) { } + public void Sort(System.Collections.Generic.IComparer? comparer) { } public void Sort(System.Comparison comparison) { } - public void Sort(int index, int count, System.Collections.Generic.IComparer comparer) { } + public void Sort(int index, int count, System.Collections.Generic.IComparer? comparer) { } System.Collections.Generic.IEnumerator System.Collections.Generic.IEnumerable.GetEnumerator() { throw null; } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { throw null; } public T[] ToArray() { throw null; } @@ -259,29 +259,30 @@ namespace System.Collections.Immutable } public static partial class ImmutableDictionary { - public static bool Contains(this System.Collections.Immutable.IImmutableDictionary map, TKey key, TValue value) { throw null; } - public static System.Collections.Immutable.ImmutableDictionary.Builder CreateBuilder() { throw null; } - public static System.Collections.Immutable.ImmutableDictionary.Builder CreateBuilder(System.Collections.Generic.IEqualityComparer keyComparer) { throw null; } - public static System.Collections.Immutable.ImmutableDictionary.Builder CreateBuilder(System.Collections.Generic.IEqualityComparer keyComparer, System.Collections.Generic.IEqualityComparer valueComparer) { throw null; } - public static System.Collections.Immutable.ImmutableDictionary CreateRange(System.Collections.Generic.IEnumerable> items) { throw null; } - public static System.Collections.Immutable.ImmutableDictionary CreateRange(System.Collections.Generic.IEqualityComparer keyComparer, System.Collections.Generic.IEnumerable> items) { throw null; } - public static System.Collections.Immutable.ImmutableDictionary CreateRange(System.Collections.Generic.IEqualityComparer keyComparer, System.Collections.Generic.IEqualityComparer valueComparer, System.Collections.Generic.IEnumerable> items) { throw null; } - public static System.Collections.Immutable.ImmutableDictionary Create() { throw null; } - public static System.Collections.Immutable.ImmutableDictionary Create(System.Collections.Generic.IEqualityComparer keyComparer) { throw null; } - public static System.Collections.Immutable.ImmutableDictionary Create(System.Collections.Generic.IEqualityComparer keyComparer, System.Collections.Generic.IEqualityComparer valueComparer) { throw null; } - public static TValue GetValueOrDefault(this System.Collections.Immutable.IImmutableDictionary dictionary, TKey key) { throw null; } - public static TValue GetValueOrDefault(this System.Collections.Immutable.IImmutableDictionary dictionary, TKey key, TValue defaultValue) { throw null; } - public static System.Collections.Immutable.ImmutableDictionary ToImmutableDictionary(this System.Collections.Generic.IEnumerable> source) { throw null; } - public static System.Collections.Immutable.ImmutableDictionary ToImmutableDictionary(this System.Collections.Generic.IEnumerable> source, System.Collections.Generic.IEqualityComparer keyComparer) { throw null; } - public static System.Collections.Immutable.ImmutableDictionary ToImmutableDictionary(this System.Collections.Generic.IEnumerable> source, System.Collections.Generic.IEqualityComparer keyComparer, System.Collections.Generic.IEqualityComparer valueComparer) { throw null; } - public static System.Collections.Immutable.ImmutableDictionary ToImmutableDictionary(this System.Collections.Generic.IEnumerable source, System.Func keySelector) { throw null; } - public static System.Collections.Immutable.ImmutableDictionary ToImmutableDictionary(this System.Collections.Generic.IEnumerable source, System.Func keySelector, System.Collections.Generic.IEqualityComparer keyComparer) { throw null; } - public static System.Collections.Immutable.ImmutableDictionary ToImmutableDictionary(this System.Collections.Immutable.ImmutableDictionary.Builder builder) { throw null; } - public static System.Collections.Immutable.ImmutableDictionary ToImmutableDictionary(this System.Collections.Generic.IEnumerable source, System.Func keySelector, System.Func elementSelector) { throw null; } - public static System.Collections.Immutable.ImmutableDictionary ToImmutableDictionary(this System.Collections.Generic.IEnumerable source, System.Func keySelector, System.Func elementSelector, System.Collections.Generic.IEqualityComparer keyComparer) { throw null; } - public static System.Collections.Immutable.ImmutableDictionary ToImmutableDictionary(this System.Collections.Generic.IEnumerable source, System.Func keySelector, System.Func elementSelector, System.Collections.Generic.IEqualityComparer keyComparer, System.Collections.Generic.IEqualityComparer valueComparer) { throw null; } + public static bool Contains(this System.Collections.Immutable.IImmutableDictionary map, TKey key, TValue value) where TKey : notnull { throw null; } + public static System.Collections.Immutable.ImmutableDictionary.Builder CreateBuilder() where TKey : notnull { throw null; } + public static System.Collections.Immutable.ImmutableDictionary.Builder CreateBuilder(System.Collections.Generic.IEqualityComparer? keyComparer) where TKey : notnull { throw null; } + public static System.Collections.Immutable.ImmutableDictionary.Builder CreateBuilder(System.Collections.Generic.IEqualityComparer? keyComparer, System.Collections.Generic.IEqualityComparer? valueComparer) where TKey : notnull { throw null; } + public static System.Collections.Immutable.ImmutableDictionary CreateRange(System.Collections.Generic.IEnumerable> items) where TKey : notnull { throw null; } + public static System.Collections.Immutable.ImmutableDictionary CreateRange(System.Collections.Generic.IEqualityComparer? keyComparer, System.Collections.Generic.IEnumerable> items) where TKey : notnull { throw null; } + public static System.Collections.Immutable.ImmutableDictionary CreateRange(System.Collections.Generic.IEqualityComparer? keyComparer, System.Collections.Generic.IEqualityComparer? valueComparer, System.Collections.Generic.IEnumerable> items) where TKey : notnull { throw null; } + public static System.Collections.Immutable.ImmutableDictionary Create() where TKey : notnull { throw null; } + public static System.Collections.Immutable.ImmutableDictionary Create(System.Collections.Generic.IEqualityComparer? keyComparer) where TKey : notnull { throw null; } + public static System.Collections.Immutable.ImmutableDictionary Create(System.Collections.Generic.IEqualityComparer? keyComparer, System.Collections.Generic.IEqualityComparer? valueComparer) where TKey : notnull { throw null; } + [return: System.Diagnostics.CodeAnalysis.MaybeNullAttribute] + public static TValue GetValueOrDefault(this System.Collections.Immutable.IImmutableDictionary dictionary, TKey key) where TKey : notnull { throw null; } + public static TValue GetValueOrDefault(this System.Collections.Immutable.IImmutableDictionary dictionary, TKey key, TValue defaultValue) where TKey : notnull { throw null; } + public static System.Collections.Immutable.ImmutableDictionary ToImmutableDictionary(this System.Collections.Generic.IEnumerable> source) where TKey : notnull { throw null; } + public static System.Collections.Immutable.ImmutableDictionary ToImmutableDictionary(this System.Collections.Generic.IEnumerable> source, System.Collections.Generic.IEqualityComparer? keyComparer) where TKey : notnull { throw null; } + public static System.Collections.Immutable.ImmutableDictionary ToImmutableDictionary(this System.Collections.Generic.IEnumerable> source, System.Collections.Generic.IEqualityComparer? keyComparer, System.Collections.Generic.IEqualityComparer? valueComparer) where TKey : notnull { throw null; } + public static System.Collections.Immutable.ImmutableDictionary ToImmutableDictionary(this System.Collections.Generic.IEnumerable source, System.Func keySelector) where TKey : notnull { throw null; } + public static System.Collections.Immutable.ImmutableDictionary ToImmutableDictionary(this System.Collections.Generic.IEnumerable source, System.Func keySelector, System.Collections.Generic.IEqualityComparer? keyComparer) where TKey : notnull { throw null; } + public static System.Collections.Immutable.ImmutableDictionary ToImmutableDictionary(this System.Collections.Immutable.ImmutableDictionary.Builder builder) where TKey : notnull { throw null; } + public static System.Collections.Immutable.ImmutableDictionary ToImmutableDictionary(this System.Collections.Generic.IEnumerable source, System.Func keySelector, System.Func elementSelector) where TKey : notnull { throw null; } + public static System.Collections.Immutable.ImmutableDictionary ToImmutableDictionary(this System.Collections.Generic.IEnumerable source, System.Func keySelector, System.Func elementSelector, System.Collections.Generic.IEqualityComparer? keyComparer) where TKey : notnull { throw null; } + public static System.Collections.Immutable.ImmutableDictionary ToImmutableDictionary(this System.Collections.Generic.IEnumerable source, System.Func keySelector, System.Func elementSelector, System.Collections.Generic.IEqualityComparer? keyComparer, System.Collections.Generic.IEqualityComparer? valueComparer) where TKey : notnull { throw null; } } - public sealed partial class ImmutableDictionary : System.Collections.Generic.ICollection>, System.Collections.Generic.IDictionary, System.Collections.Generic.IEnumerable>, System.Collections.Generic.IReadOnlyCollection>, System.Collections.Generic.IReadOnlyDictionary, System.Collections.ICollection, System.Collections.IDictionary, System.Collections.IEnumerable, System.Collections.Immutable.IImmutableDictionary + public sealed partial class ImmutableDictionary : System.Collections.Generic.ICollection>, System.Collections.Generic.IDictionary, System.Collections.Generic.IEnumerable>, System.Collections.Generic.IReadOnlyCollection>, System.Collections.Generic.IReadOnlyDictionary, System.Collections.ICollection, System.Collections.IDictionary, System.Collections.IEnumerable, System.Collections.Immutable.IImmutableDictionary where TKey : notnull { internal ImmutableDictionary() { } public static readonly System.Collections.Immutable.ImmutableDictionary Empty; @@ -298,7 +299,7 @@ namespace System.Collections.Immutable object System.Collections.ICollection.SyncRoot { get { throw null; } } bool System.Collections.IDictionary.IsFixedSize { get { throw null; } } bool System.Collections.IDictionary.IsReadOnly { get { throw null; } } - object System.Collections.IDictionary.this[object key] { get { throw null; } set { } } + object? System.Collections.IDictionary.this[object key] { get { throw null; } set { } } System.Collections.ICollection System.Collections.IDictionary.Keys { get { throw null; } } System.Collections.ICollection System.Collections.IDictionary.Values { get { throw null; } } public System.Collections.Generic.IEqualityComparer ValueComparer { get { throw null; } } @@ -322,7 +323,7 @@ namespace System.Collections.Immutable bool System.Collections.Generic.IDictionary.Remove(TKey key) { throw null; } System.Collections.Generic.IEnumerator> System.Collections.Generic.IEnumerable>.GetEnumerator() { throw null; } void System.Collections.ICollection.CopyTo(System.Array array, int arrayIndex) { } - void System.Collections.IDictionary.Add(object key, object value) { } + void System.Collections.IDictionary.Add(object key, object? value) { } void System.Collections.IDictionary.Clear() { } bool System.Collections.IDictionary.Contains(object key) { throw null; } System.Collections.IDictionaryEnumerator System.Collections.IDictionary.GetEnumerator() { throw null; } @@ -337,9 +338,9 @@ namespace System.Collections.Immutable System.Collections.Immutable.IImmutableDictionary System.Collections.Immutable.IImmutableDictionary.SetItems(System.Collections.Generic.IEnumerable> items) { throw null; } public System.Collections.Immutable.ImmutableDictionary.Builder ToBuilder() { throw null; } public bool TryGetKey(TKey equalKey, out TKey actualKey) { throw null; } - public bool TryGetValue(TKey key, out TValue value) { throw null; } - public System.Collections.Immutable.ImmutableDictionary WithComparers(System.Collections.Generic.IEqualityComparer keyComparer) { throw null; } - public System.Collections.Immutable.ImmutableDictionary WithComparers(System.Collections.Generic.IEqualityComparer keyComparer, System.Collections.Generic.IEqualityComparer valueComparer) { throw null; } + public bool TryGetValue(TKey key, [System.Diagnostics.CodeAnalysis.MaybeNullWhenAttribute(false)] out TValue value) { throw null; } + public System.Collections.Immutable.ImmutableDictionary WithComparers(System.Collections.Generic.IEqualityComparer? keyComparer) { throw null; } + public System.Collections.Immutable.ImmutableDictionary WithComparers(System.Collections.Generic.IEqualityComparer? keyComparer, System.Collections.Generic.IEqualityComparer? valueComparer) { throw null; } public sealed partial class Builder : System.Collections.Generic.ICollection>, System.Collections.Generic.IDictionary, System.Collections.Generic.IEnumerable>, System.Collections.Generic.IReadOnlyCollection>, System.Collections.Generic.IReadOnlyDictionary, System.Collections.ICollection, System.Collections.IDictionary, System.Collections.IEnumerable { internal Builder() { } @@ -354,7 +355,7 @@ namespace System.Collections.Immutable object System.Collections.ICollection.SyncRoot { get { throw null; } } bool System.Collections.IDictionary.IsFixedSize { get { throw null; } } bool System.Collections.IDictionary.IsReadOnly { get { throw null; } } - object System.Collections.IDictionary.this[object key] { get { throw null; } set { } } + object? System.Collections.IDictionary.this[object key] { get { throw null; } set { } } System.Collections.ICollection System.Collections.IDictionary.Keys { get { throw null; } } System.Collections.ICollection System.Collections.IDictionary.Values { get { throw null; } } public System.Collections.Generic.IEqualityComparer ValueComparer { get { throw null; } set { } } @@ -367,6 +368,7 @@ namespace System.Collections.Immutable public bool ContainsKey(TKey key) { throw null; } public bool ContainsValue(TValue value) { throw null; } public System.Collections.Immutable.ImmutableDictionary.Enumerator GetEnumerator() { throw null; } + [return: System.Diagnostics.CodeAnalysis.MaybeNullAttribute] public TValue GetValueOrDefault(TKey key) { throw null; } public TValue GetValueOrDefault(TKey key, TValue defaultValue) { throw null; } public bool Remove(System.Collections.Generic.KeyValuePair item) { throw null; } @@ -375,14 +377,14 @@ namespace System.Collections.Immutable void System.Collections.Generic.ICollection>.CopyTo(System.Collections.Generic.KeyValuePair[] array, int arrayIndex) { } System.Collections.Generic.IEnumerator> System.Collections.Generic.IEnumerable>.GetEnumerator() { throw null; } void System.Collections.ICollection.CopyTo(System.Array array, int arrayIndex) { } - void System.Collections.IDictionary.Add(object key, object value) { } + void System.Collections.IDictionary.Add(object key, object? value) { } bool System.Collections.IDictionary.Contains(object key) { throw null; } System.Collections.IDictionaryEnumerator System.Collections.IDictionary.GetEnumerator() { throw null; } void System.Collections.IDictionary.Remove(object key) { } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { throw null; } public System.Collections.Immutable.ImmutableDictionary ToImmutable() { throw null; } public bool TryGetKey(TKey equalKey, out TKey actualKey) { throw null; } - public bool TryGetValue(TKey key, out TValue value) { throw null; } + public bool TryGetValue(TKey key, [System.Diagnostics.CodeAnalysis.MaybeNullWhenAttribute(false)] out TValue value) { throw null; } } public partial struct Enumerator : System.Collections.Generic.IEnumerator>, System.Collections.IEnumerator, System.IDisposable { @@ -398,17 +400,17 @@ namespace System.Collections.Immutable public static partial class ImmutableHashSet { public static System.Collections.Immutable.ImmutableHashSet.Builder CreateBuilder() { throw null; } - public static System.Collections.Immutable.ImmutableHashSet.Builder CreateBuilder(System.Collections.Generic.IEqualityComparer equalityComparer) { throw null; } + public static System.Collections.Immutable.ImmutableHashSet.Builder CreateBuilder(System.Collections.Generic.IEqualityComparer? equalityComparer) { throw null; } public static System.Collections.Immutable.ImmutableHashSet CreateRange(System.Collections.Generic.IEnumerable items) { throw null; } - public static System.Collections.Immutable.ImmutableHashSet CreateRange(System.Collections.Generic.IEqualityComparer equalityComparer, System.Collections.Generic.IEnumerable items) { throw null; } + public static System.Collections.Immutable.ImmutableHashSet CreateRange(System.Collections.Generic.IEqualityComparer? equalityComparer, System.Collections.Generic.IEnumerable items) { throw null; } public static System.Collections.Immutable.ImmutableHashSet Create() { throw null; } - public static System.Collections.Immutable.ImmutableHashSet Create(System.Collections.Generic.IEqualityComparer equalityComparer) { throw null; } - public static System.Collections.Immutable.ImmutableHashSet Create(System.Collections.Generic.IEqualityComparer equalityComparer, T item) { throw null; } - public static System.Collections.Immutable.ImmutableHashSet Create(System.Collections.Generic.IEqualityComparer equalityComparer, params T[] items) { throw null; } + public static System.Collections.Immutable.ImmutableHashSet Create(System.Collections.Generic.IEqualityComparer? equalityComparer) { throw null; } + public static System.Collections.Immutable.ImmutableHashSet Create(System.Collections.Generic.IEqualityComparer? equalityComparer, T item) { throw null; } + public static System.Collections.Immutable.ImmutableHashSet Create(System.Collections.Generic.IEqualityComparer? equalityComparer, params T[] items) { throw null; } public static System.Collections.Immutable.ImmutableHashSet Create(T item) { throw null; } public static System.Collections.Immutable.ImmutableHashSet Create(params T[] items) { throw null; } public static System.Collections.Immutable.ImmutableHashSet ToImmutableHashSet(this System.Collections.Generic.IEnumerable source) { throw null; } - public static System.Collections.Immutable.ImmutableHashSet ToImmutableHashSet(this System.Collections.Generic.IEnumerable source, System.Collections.Generic.IEqualityComparer equalityComparer) { throw null; } + public static System.Collections.Immutable.ImmutableHashSet ToImmutableHashSet(this System.Collections.Generic.IEnumerable source, System.Collections.Generic.IEqualityComparer? equalityComparer) { throw null; } public static System.Collections.Immutable.ImmutableHashSet ToImmutableHashSet(this System.Collections.Immutable.ImmutableHashSet.Builder builder) { throw null; } } public sealed partial class ImmutableHashSet : System.Collections.Generic.ICollection, System.Collections.Generic.IEnumerable, System.Collections.Generic.IReadOnlyCollection, System.Collections.Generic.ISet, System.Collections.ICollection, System.Collections.IEnumerable, System.Collections.Immutable.IImmutableSet @@ -457,7 +459,7 @@ namespace System.Collections.Immutable public System.Collections.Immutable.ImmutableHashSet.Builder ToBuilder() { throw null; } public bool TryGetValue(T equalValue, out T actualValue) { throw null; } public System.Collections.Immutable.ImmutableHashSet Union(System.Collections.Generic.IEnumerable other) { throw null; } - public System.Collections.Immutable.ImmutableHashSet WithComparer(System.Collections.Generic.IEqualityComparer equalityComparer) { throw null; } + public System.Collections.Immutable.ImmutableHashSet WithComparer(System.Collections.Generic.IEqualityComparer? equalityComparer) { throw null; } public sealed partial class Builder : System.Collections.Generic.ICollection, System.Collections.Generic.IEnumerable, System.Collections.Generic.IReadOnlyCollection, System.Collections.Generic.ISet, System.Collections.IEnumerable { internal Builder() { } @@ -490,7 +492,7 @@ namespace System.Collections.Immutable private object _dummy; private int _dummyPrimitive; public T Current { get { throw null; } } - object System.Collections.IEnumerator.Current { get { throw null; } } + object? System.Collections.IEnumerator.Current { get { throw null; } } public void Dispose() { } public bool MoveNext() { throw null; } public void Reset() { } @@ -498,21 +500,21 @@ namespace System.Collections.Immutable } public static partial class ImmutableInterlocked { - public static TValue AddOrUpdate(ref System.Collections.Immutable.ImmutableDictionary location, TKey key, System.Func addValueFactory, System.Func updateValueFactory) { throw null; } - public static TValue AddOrUpdate(ref System.Collections.Immutable.ImmutableDictionary location, TKey key, TValue addValue, System.Func updateValueFactory) { throw null; } + public static TValue AddOrUpdate(ref System.Collections.Immutable.ImmutableDictionary location, TKey key, System.Func addValueFactory, System.Func updateValueFactory) where TKey : notnull { throw null; } + public static TValue AddOrUpdate(ref System.Collections.Immutable.ImmutableDictionary location, TKey key, TValue addValue, System.Func updateValueFactory) where TKey : notnull { throw null; } public static void Enqueue(ref System.Collections.Immutable.ImmutableQueue location, T value) { } - public static TValue GetOrAdd(ref System.Collections.Immutable.ImmutableDictionary location, TKey key, System.Func valueFactory) { throw null; } - public static TValue GetOrAdd(ref System.Collections.Immutable.ImmutableDictionary location, TKey key, TValue value) { throw null; } - public static TValue GetOrAdd(ref System.Collections.Immutable.ImmutableDictionary location, TKey key, System.Func valueFactory, TArg factoryArgument) { throw null; } + public static TValue GetOrAdd(ref System.Collections.Immutable.ImmutableDictionary location, TKey key, System.Func valueFactory) where TKey : notnull { throw null; } + public static TValue GetOrAdd(ref System.Collections.Immutable.ImmutableDictionary location, TKey key, TValue value) where TKey : notnull { throw null; } + public static TValue GetOrAdd(ref System.Collections.Immutable.ImmutableDictionary location, TKey key, System.Func valueFactory, TArg factoryArgument) where TKey : notnull { throw null; } public static System.Collections.Immutable.ImmutableArray InterlockedCompareExchange(ref System.Collections.Immutable.ImmutableArray location, System.Collections.Immutable.ImmutableArray value, System.Collections.Immutable.ImmutableArray comparand) { throw null; } public static System.Collections.Immutable.ImmutableArray InterlockedExchange(ref System.Collections.Immutable.ImmutableArray location, System.Collections.Immutable.ImmutableArray value) { throw null; } public static bool InterlockedInitialize(ref System.Collections.Immutable.ImmutableArray location, System.Collections.Immutable.ImmutableArray value) { throw null; } public static void Push(ref System.Collections.Immutable.ImmutableStack location, T value) { } - public static bool TryAdd(ref System.Collections.Immutable.ImmutableDictionary location, TKey key, TValue value) { throw null; } - public static bool TryDequeue(ref System.Collections.Immutable.ImmutableQueue location, out T value) { throw null; } - public static bool TryPop(ref System.Collections.Immutable.ImmutableStack location, out T value) { throw null; } - public static bool TryRemove(ref System.Collections.Immutable.ImmutableDictionary location, TKey key, out TValue value) { throw null; } - public static bool TryUpdate(ref System.Collections.Immutable.ImmutableDictionary location, TKey key, TValue newValue, TValue comparisonValue) { throw null; } + public static bool TryAdd(ref System.Collections.Immutable.ImmutableDictionary location, TKey key, TValue value) where TKey : notnull { throw null; } + public static bool TryDequeue(ref System.Collections.Immutable.ImmutableQueue location, [System.Diagnostics.CodeAnalysis.MaybeNullWhenAttribute(false)] out T value) { throw null; } + public static bool TryPop(ref System.Collections.Immutable.ImmutableStack location, [System.Diagnostics.CodeAnalysis.MaybeNullWhenAttribute(false)] out T value) { throw null; } + public static bool TryRemove(ref System.Collections.Immutable.ImmutableDictionary location, TKey key, [System.Diagnostics.CodeAnalysis.MaybeNullWhenAttribute(false)] out TValue value) where TKey : notnull { throw null; } + public static bool TryUpdate(ref System.Collections.Immutable.ImmutableDictionary location, TKey key, TValue newValue, TValue comparisonValue) where TKey : notnull { throw null; } public static bool Update(ref T location, System.Func transformer) where T : class { throw null; } public static bool Update(ref T location, System.Func transformer, TArg transformerArgument) where T : class { throw null; } public static bool Update(ref System.Collections.Immutable.ImmutableArray location, Func, System.Collections.Immutable.ImmutableArray> transformer) { throw null; } @@ -526,11 +528,11 @@ namespace System.Collections.Immutable public static System.Collections.Immutable.ImmutableList Create(T item) { throw null; } public static System.Collections.Immutable.ImmutableList Create(params T[] items) { throw null; } public static int IndexOf(this System.Collections.Immutable.IImmutableList list, T item) { throw null; } - public static int IndexOf(this System.Collections.Immutable.IImmutableList list, T item, System.Collections.Generic.IEqualityComparer equalityComparer) { throw null; } + public static int IndexOf(this System.Collections.Immutable.IImmutableList list, T item, System.Collections.Generic.IEqualityComparer? equalityComparer) { throw null; } public static int IndexOf(this System.Collections.Immutable.IImmutableList list, T item, int startIndex) { throw null; } public static int IndexOf(this System.Collections.Immutable.IImmutableList list, T item, int startIndex, int count) { throw null; } public static int LastIndexOf(this System.Collections.Immutable.IImmutableList list, T item) { throw null; } - public static int LastIndexOf(this System.Collections.Immutable.IImmutableList list, T item, System.Collections.Generic.IEqualityComparer equalityComparer) { throw null; } + public static int LastIndexOf(this System.Collections.Immutable.IImmutableList list, T item, System.Collections.Generic.IEqualityComparer? equalityComparer) { throw null; } public static int LastIndexOf(this System.Collections.Immutable.IImmutableList list, T item, int startIndex) { throw null; } public static int LastIndexOf(this System.Collections.Immutable.IImmutableList list, T item, int startIndex, int count) { throw null; } public static System.Collections.Immutable.IImmutableList RemoveRange(this System.Collections.Immutable.IImmutableList list, System.Collections.Generic.IEnumerable items) { throw null; } @@ -552,12 +554,12 @@ namespace System.Collections.Immutable object System.Collections.ICollection.SyncRoot { get { throw null; } } bool System.Collections.IList.IsFixedSize { get { throw null; } } bool System.Collections.IList.IsReadOnly { get { throw null; } } - object System.Collections.IList.this[int index] { get { throw null; } set { } } + object? System.Collections.IList.this[int index] { get { throw null; } set { } } public System.Collections.Immutable.ImmutableList Add(T value) { throw null; } public System.Collections.Immutable.ImmutableList AddRange(System.Collections.Generic.IEnumerable items) { throw null; } - public int BinarySearch(int index, int count, T item, System.Collections.Generic.IComparer comparer) { throw null; } + public int BinarySearch(int index, int count, T item, System.Collections.Generic.IComparer? comparer) { throw null; } public int BinarySearch(T item) { throw null; } - public int BinarySearch(T item, System.Collections.Generic.IComparer comparer) { throw null; } + public int BinarySearch(T item, System.Collections.Generic.IComparer? comparer) { throw null; } public System.Collections.Immutable.ImmutableList Clear() { throw null; } public bool Contains(T value) { throw null; } public System.Collections.Immutable.ImmutableList ConvertAll(System.Func converter) { throw null; } @@ -565,11 +567,13 @@ namespace System.Collections.Immutable public void CopyTo(T[] array) { } public void CopyTo(T[] array, int arrayIndex) { } public bool Exists(System.Predicate match) { throw null; } + [return: System.Diagnostics.CodeAnalysis.MaybeNullAttribute] public T Find(System.Predicate match) { throw null; } public System.Collections.Immutable.ImmutableList FindAll(System.Predicate match) { throw null; } public int FindIndex(int startIndex, int count, System.Predicate match) { throw null; } public int FindIndex(int startIndex, System.Predicate match) { throw null; } public int FindIndex(System.Predicate match) { throw null; } + [return: System.Diagnostics.CodeAnalysis.MaybeNullAttribute] public T FindLast(System.Predicate match) { throw null; } public int FindLastIndex(int startIndex, int count, System.Predicate match) { throw null; } public int FindLastIndex(int startIndex, System.Predicate match) { throw null; } @@ -578,29 +582,29 @@ namespace System.Collections.Immutable public System.Collections.Immutable.ImmutableList.Enumerator GetEnumerator() { throw null; } public System.Collections.Immutable.ImmutableList GetRange(int index, int count) { throw null; } public int IndexOf(T value) { throw null; } - public int IndexOf(T item, int index, int count, System.Collections.Generic.IEqualityComparer equalityComparer) { throw null; } + public int IndexOf(T item, int index, int count, System.Collections.Generic.IEqualityComparer? equalityComparer) { throw null; } public System.Collections.Immutable.ImmutableList Insert(int index, T item) { throw null; } public System.Collections.Immutable.ImmutableList InsertRange(int index, System.Collections.Generic.IEnumerable items) { throw null; } #if !NETSTANDARD1_0 public ref readonly T ItemRef(int index) { throw null; } #endif - public int LastIndexOf(T item, int index, int count, System.Collections.Generic.IEqualityComparer equalityComparer) { throw null; } + public int LastIndexOf(T item, int index, int count, System.Collections.Generic.IEqualityComparer? equalityComparer) { throw null; } public System.Collections.Immutable.ImmutableList Remove(T value) { throw null; } - public System.Collections.Immutable.ImmutableList Remove(T value, System.Collections.Generic.IEqualityComparer equalityComparer) { throw null; } + public System.Collections.Immutable.ImmutableList Remove(T value, System.Collections.Generic.IEqualityComparer? equalityComparer) { throw null; } public System.Collections.Immutable.ImmutableList RemoveAll(System.Predicate match) { throw null; } public System.Collections.Immutable.ImmutableList RemoveAt(int index) { throw null; } public System.Collections.Immutable.ImmutableList RemoveRange(System.Collections.Generic.IEnumerable items) { throw null; } - public System.Collections.Immutable.ImmutableList RemoveRange(System.Collections.Generic.IEnumerable items, System.Collections.Generic.IEqualityComparer equalityComparer) { throw null; } + public System.Collections.Immutable.ImmutableList RemoveRange(System.Collections.Generic.IEnumerable items, System.Collections.Generic.IEqualityComparer? equalityComparer) { throw null; } public System.Collections.Immutable.ImmutableList RemoveRange(int index, int count) { throw null; } public System.Collections.Immutable.ImmutableList Replace(T oldValue, T newValue) { throw null; } - public System.Collections.Immutable.ImmutableList Replace(T oldValue, T newValue, System.Collections.Generic.IEqualityComparer equalityComparer) { throw null; } + public System.Collections.Immutable.ImmutableList Replace(T oldValue, T newValue, System.Collections.Generic.IEqualityComparer? equalityComparer) { throw null; } public System.Collections.Immutable.ImmutableList Reverse() { throw null; } public System.Collections.Immutable.ImmutableList Reverse(int index, int count) { throw null; } public System.Collections.Immutable.ImmutableList SetItem(int index, T value) { throw null; } public System.Collections.Immutable.ImmutableList Sort() { throw null; } - public System.Collections.Immutable.ImmutableList Sort(System.Collections.Generic.IComparer comparer) { throw null; } + public System.Collections.Immutable.ImmutableList Sort(System.Collections.Generic.IComparer? comparer) { throw null; } public System.Collections.Immutable.ImmutableList Sort(System.Comparison comparison) { throw null; } - public System.Collections.Immutable.ImmutableList Sort(int index, int count, System.Collections.Generic.IComparer comparer) { throw null; } + public System.Collections.Immutable.ImmutableList Sort(int index, int count, System.Collections.Generic.IComparer? comparer) { throw null; } void System.Collections.Generic.ICollection.Add(T item) { } void System.Collections.Generic.ICollection.Clear() { } bool System.Collections.Generic.ICollection.Remove(T item) { throw null; } @@ -609,24 +613,24 @@ namespace System.Collections.Immutable void System.Collections.Generic.IList.RemoveAt(int index) { } void System.Collections.ICollection.CopyTo(System.Array array, int arrayIndex) { } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { throw null; } - int System.Collections.IList.Add(object value) { throw null; } + int System.Collections.IList.Add(object? value) { throw null; } void System.Collections.IList.Clear() { } - bool System.Collections.IList.Contains(object value) { throw null; } - int System.Collections.IList.IndexOf(object value) { throw null; } - void System.Collections.IList.Insert(int index, object value) { } - void System.Collections.IList.Remove(object value) { } + bool System.Collections.IList.Contains(object? value) { throw null; } + int System.Collections.IList.IndexOf(object? value) { throw null; } + void System.Collections.IList.Insert(int index, object? value) { } + void System.Collections.IList.Remove(object? value) { } void System.Collections.IList.RemoveAt(int index) { } System.Collections.Immutable.IImmutableList System.Collections.Immutable.IImmutableList.Add(T value) { throw null; } System.Collections.Immutable.IImmutableList System.Collections.Immutable.IImmutableList.AddRange(System.Collections.Generic.IEnumerable items) { throw null; } System.Collections.Immutable.IImmutableList System.Collections.Immutable.IImmutableList.Clear() { throw null; } System.Collections.Immutable.IImmutableList System.Collections.Immutable.IImmutableList.Insert(int index, T item) { throw null; } System.Collections.Immutable.IImmutableList System.Collections.Immutable.IImmutableList.InsertRange(int index, System.Collections.Generic.IEnumerable items) { throw null; } - System.Collections.Immutable.IImmutableList System.Collections.Immutable.IImmutableList.Remove(T value, System.Collections.Generic.IEqualityComparer equalityComparer) { throw null; } + System.Collections.Immutable.IImmutableList System.Collections.Immutable.IImmutableList.Remove(T value, System.Collections.Generic.IEqualityComparer? equalityComparer) { throw null; } System.Collections.Immutable.IImmutableList System.Collections.Immutable.IImmutableList.RemoveAll(System.Predicate match) { throw null; } System.Collections.Immutable.IImmutableList System.Collections.Immutable.IImmutableList.RemoveAt(int index) { throw null; } - System.Collections.Immutable.IImmutableList System.Collections.Immutable.IImmutableList.RemoveRange(System.Collections.Generic.IEnumerable items, System.Collections.Generic.IEqualityComparer equalityComparer) { throw null; } + System.Collections.Immutable.IImmutableList System.Collections.Immutable.IImmutableList.RemoveRange(System.Collections.Generic.IEnumerable items, System.Collections.Generic.IEqualityComparer? equalityComparer) { throw null; } System.Collections.Immutable.IImmutableList System.Collections.Immutable.IImmutableList.RemoveRange(int index, int count) { throw null; } - System.Collections.Immutable.IImmutableList System.Collections.Immutable.IImmutableList.Replace(T oldValue, T newValue, System.Collections.Generic.IEqualityComparer equalityComparer) { throw null; } + System.Collections.Immutable.IImmutableList System.Collections.Immutable.IImmutableList.Replace(T oldValue, T newValue, System.Collections.Generic.IEqualityComparer? equalityComparer) { throw null; } System.Collections.Immutable.IImmutableList System.Collections.Immutable.IImmutableList.SetItem(int index, T value) { throw null; } public System.Collections.Immutable.ImmutableList.Builder ToBuilder() { throw null; } public bool TrueForAll(System.Predicate match) { throw null; } @@ -640,12 +644,12 @@ namespace System.Collections.Immutable object System.Collections.ICollection.SyncRoot { get { throw null; } } bool System.Collections.IList.IsFixedSize { get { throw null; } } bool System.Collections.IList.IsReadOnly { get { throw null; } } - object System.Collections.IList.this[int index] { get { throw null; } set { } } + object? System.Collections.IList.this[int index] { get { throw null; } set { } } public void Add(T item) { } public void AddRange(System.Collections.Generic.IEnumerable items) { } - public int BinarySearch(int index, int count, T item, System.Collections.Generic.IComparer comparer) { throw null; } + public int BinarySearch(int index, int count, T item, System.Collections.Generic.IComparer? comparer) { throw null; } public int BinarySearch(T item) { throw null; } - public int BinarySearch(T item, System.Collections.Generic.IComparer comparer) { throw null; } + public int BinarySearch(T item, System.Collections.Generic.IComparer? comparer) { throw null; } public void Clear() { } public bool Contains(T item) { throw null; } public System.Collections.Immutable.ImmutableList ConvertAll(System.Func converter) { throw null; } @@ -653,11 +657,13 @@ namespace System.Collections.Immutable public void CopyTo(T[] array) { } public void CopyTo(T[] array, int arrayIndex) { } public bool Exists(System.Predicate match) { throw null; } + [return: System.Diagnostics.CodeAnalysis.MaybeNullAttribute] public T Find(System.Predicate match) { throw null; } public System.Collections.Immutable.ImmutableList FindAll(System.Predicate match) { throw null; } public int FindIndex(int startIndex, int count, System.Predicate match) { throw null; } public int FindIndex(int startIndex, System.Predicate match) { throw null; } public int FindIndex(System.Predicate match) { throw null; } + [return: System.Diagnostics.CodeAnalysis.MaybeNullAttribute] public T FindLast(System.Predicate match) { throw null; } public int FindLastIndex(int startIndex, int count, System.Predicate match) { throw null; } public int FindLastIndex(int startIndex, System.Predicate match) { throw null; } @@ -668,7 +674,7 @@ namespace System.Collections.Immutable public int IndexOf(T item) { throw null; } public int IndexOf(T item, int index) { throw null; } public int IndexOf(T item, int index, int count) { throw null; } - public int IndexOf(T item, int index, int count, System.Collections.Generic.IEqualityComparer equalityComparer) { throw null; } + public int IndexOf(T item, int index, int count, System.Collections.Generic.IEqualityComparer? equalityComparer) { throw null; } public void Insert(int index, T item) { } public void InsertRange(int index, System.Collections.Generic.IEnumerable items) { } #if !NETSTANDARD1_0 @@ -677,25 +683,25 @@ namespace System.Collections.Immutable public int LastIndexOf(T item) { throw null; } public int LastIndexOf(T item, int startIndex) { throw null; } public int LastIndexOf(T item, int startIndex, int count) { throw null; } - public int LastIndexOf(T item, int startIndex, int count, System.Collections.Generic.IEqualityComparer equalityComparer) { throw null; } + public int LastIndexOf(T item, int startIndex, int count, System.Collections.Generic.IEqualityComparer? equalityComparer) { throw null; } public bool Remove(T item) { throw null; } public int RemoveAll(System.Predicate match) { throw null; } public void RemoveAt(int index) { } public void Reverse() { } public void Reverse(int index, int count) { } public void Sort() { } - public void Sort(System.Collections.Generic.IComparer comparer) { } + public void Sort(System.Collections.Generic.IComparer? comparer) { } public void Sort(System.Comparison comparison) { } - public void Sort(int index, int count, System.Collections.Generic.IComparer comparer) { } + public void Sort(int index, int count, System.Collections.Generic.IComparer? comparer) { } System.Collections.Generic.IEnumerator System.Collections.Generic.IEnumerable.GetEnumerator() { throw null; } void System.Collections.ICollection.CopyTo(System.Array array, int arrayIndex) { } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { throw null; } int System.Collections.IList.Add(object value) { throw null; } void System.Collections.IList.Clear() { } - bool System.Collections.IList.Contains(object value) { throw null; } - int System.Collections.IList.IndexOf(object value) { throw null; } - void System.Collections.IList.Insert(int index, object value) { } - void System.Collections.IList.Remove(object value) { } + bool System.Collections.IList.Contains(object? value) { throw null; } + int System.Collections.IList.IndexOf(object? value) { throw null; } + void System.Collections.IList.Insert(int index, object? value) { } + void System.Collections.IList.Remove(object? value) { } public System.Collections.Immutable.ImmutableList ToImmutable() { throw null; } public bool TrueForAll(System.Predicate match) { throw null; } } @@ -705,7 +711,7 @@ namespace System.Collections.Immutable private object _dummy; private int _dummyPrimitive; public T Current { get { throw null; } } - object System.Collections.IEnumerator.Current { get { throw null; } } + object? System.Collections.IEnumerator.Current { get { throw null; } } public void Dispose() { } public bool MoveNext() { throw null; } public void Reset() { } @@ -749,24 +755,24 @@ namespace System.Collections.Immutable } public static partial class ImmutableSortedDictionary { - public static System.Collections.Immutable.ImmutableSortedDictionary.Builder CreateBuilder() { throw null; } - public static System.Collections.Immutable.ImmutableSortedDictionary.Builder CreateBuilder(System.Collections.Generic.IComparer keyComparer) { throw null; } - public static System.Collections.Immutable.ImmutableSortedDictionary.Builder CreateBuilder(System.Collections.Generic.IComparer keyComparer, System.Collections.Generic.IEqualityComparer valueComparer) { throw null; } - public static System.Collections.Immutable.ImmutableSortedDictionary CreateRange(System.Collections.Generic.IComparer keyComparer, System.Collections.Generic.IEnumerable> items) { throw null; } - public static System.Collections.Immutable.ImmutableSortedDictionary CreateRange(System.Collections.Generic.IComparer keyComparer, System.Collections.Generic.IEqualityComparer valueComparer, System.Collections.Generic.IEnumerable> items) { throw null; } - public static System.Collections.Immutable.ImmutableSortedDictionary CreateRange(System.Collections.Generic.IEnumerable> items) { throw null; } - public static System.Collections.Immutable.ImmutableSortedDictionary Create() { throw null; } - public static System.Collections.Immutable.ImmutableSortedDictionary Create(System.Collections.Generic.IComparer keyComparer) { throw null; } - public static System.Collections.Immutable.ImmutableSortedDictionary Create(System.Collections.Generic.IComparer keyComparer, System.Collections.Generic.IEqualityComparer valueComparer) { throw null; } - public static System.Collections.Immutable.ImmutableSortedDictionary ToImmutableSortedDictionary(this System.Collections.Generic.IEnumerable> source) { throw null; } - public static System.Collections.Immutable.ImmutableSortedDictionary ToImmutableSortedDictionary(this System.Collections.Generic.IEnumerable> source, System.Collections.Generic.IComparer keyComparer) { throw null; } - public static System.Collections.Immutable.ImmutableSortedDictionary ToImmutableSortedDictionary(this System.Collections.Generic.IEnumerable> source, System.Collections.Generic.IComparer keyComparer, System.Collections.Generic.IEqualityComparer valueComparer) { throw null; } - public static System.Collections.Immutable.ImmutableSortedDictionary ToImmutableSortedDictionary(this System.Collections.Immutable.ImmutableSortedDictionary.Builder builder) { throw null; } - public static System.Collections.Immutable.ImmutableSortedDictionary ToImmutableSortedDictionary(this System.Collections.Generic.IEnumerable source, System.Func keySelector, System.Func elementSelector) { throw null; } - public static System.Collections.Immutable.ImmutableSortedDictionary ToImmutableSortedDictionary(this System.Collections.Generic.IEnumerable source, System.Func keySelector, System.Func elementSelector, System.Collections.Generic.IComparer keyComparer) { throw null; } - public static System.Collections.Immutable.ImmutableSortedDictionary ToImmutableSortedDictionary(this System.Collections.Generic.IEnumerable source, System.Func keySelector, System.Func elementSelector, System.Collections.Generic.IComparer keyComparer, System.Collections.Generic.IEqualityComparer valueComparer) { throw null; } + public static System.Collections.Immutable.ImmutableSortedDictionary.Builder CreateBuilder() where TKey : notnull { throw null; } + public static System.Collections.Immutable.ImmutableSortedDictionary.Builder CreateBuilder(System.Collections.Generic.IComparer? keyComparer) where TKey : notnull { throw null; } + public static System.Collections.Immutable.ImmutableSortedDictionary.Builder CreateBuilder(System.Collections.Generic.IComparer? keyComparer, System.Collections.Generic.IEqualityComparer? valueComparer) where TKey : notnull { throw null; } + public static System.Collections.Immutable.ImmutableSortedDictionary CreateRange(System.Collections.Generic.IComparer? keyComparer, System.Collections.Generic.IEnumerable> items) where TKey : notnull { throw null; } + public static System.Collections.Immutable.ImmutableSortedDictionary CreateRange(System.Collections.Generic.IComparer? keyComparer, System.Collections.Generic.IEqualityComparer? valueComparer, System.Collections.Generic.IEnumerable> items) where TKey : notnull { throw null; } + public static System.Collections.Immutable.ImmutableSortedDictionary CreateRange(System.Collections.Generic.IEnumerable> items) where TKey : notnull { throw null; } + public static System.Collections.Immutable.ImmutableSortedDictionary Create() where TKey : notnull { throw null; } + public static System.Collections.Immutable.ImmutableSortedDictionary Create(System.Collections.Generic.IComparer? keyComparer) where TKey : notnull { throw null; } + public static System.Collections.Immutable.ImmutableSortedDictionary Create(System.Collections.Generic.IComparer? keyComparer, System.Collections.Generic.IEqualityComparer? valueComparer) where TKey : notnull { throw null; } + public static System.Collections.Immutable.ImmutableSortedDictionary ToImmutableSortedDictionary(this System.Collections.Generic.IEnumerable> source) where TKey : notnull { throw null; } + public static System.Collections.Immutable.ImmutableSortedDictionary ToImmutableSortedDictionary(this System.Collections.Generic.IEnumerable> source, System.Collections.Generic.IComparer? keyComparer) where TKey : notnull { throw null; } + public static System.Collections.Immutable.ImmutableSortedDictionary ToImmutableSortedDictionary(this System.Collections.Generic.IEnumerable> source, System.Collections.Generic.IComparer? keyComparer, System.Collections.Generic.IEqualityComparer? valueComparer) where TKey : notnull { throw null; } + public static System.Collections.Immutable.ImmutableSortedDictionary ToImmutableSortedDictionary(this System.Collections.Immutable.ImmutableSortedDictionary.Builder builder) where TKey : notnull { throw null; } + public static System.Collections.Immutable.ImmutableSortedDictionary ToImmutableSortedDictionary(this System.Collections.Generic.IEnumerable source, System.Func keySelector, System.Func elementSelector) where TKey : notnull { throw null; } + public static System.Collections.Immutable.ImmutableSortedDictionary ToImmutableSortedDictionary(this System.Collections.Generic.IEnumerable source, System.Func keySelector, System.Func elementSelector, System.Collections.Generic.IComparer? keyComparer) where TKey : notnull { throw null; } + public static System.Collections.Immutable.ImmutableSortedDictionary ToImmutableSortedDictionary(this System.Collections.Generic.IEnumerable source, System.Func keySelector, System.Func elementSelector, System.Collections.Generic.IComparer? keyComparer, System.Collections.Generic.IEqualityComparer? valueComparer) where TKey : notnull { throw null; } } - public sealed partial class ImmutableSortedDictionary : System.Collections.Generic.ICollection>, System.Collections.Generic.IDictionary, System.Collections.Generic.IEnumerable>, System.Collections.Generic.IReadOnlyCollection>, System.Collections.Generic.IReadOnlyDictionary, System.Collections.ICollection, System.Collections.IDictionary, System.Collections.IEnumerable, System.Collections.Immutable.IImmutableDictionary + public sealed partial class ImmutableSortedDictionary : System.Collections.Generic.ICollection>, System.Collections.Generic.IDictionary, System.Collections.Generic.IEnumerable>, System.Collections.Generic.IReadOnlyCollection>, System.Collections.Generic.IReadOnlyDictionary, System.Collections.ICollection, System.Collections.IDictionary, System.Collections.IEnumerable, System.Collections.Immutable.IImmutableDictionary where TKey : notnull { internal ImmutableSortedDictionary() { } public static readonly System.Collections.Immutable.ImmutableSortedDictionary Empty; @@ -783,7 +789,7 @@ namespace System.Collections.Immutable object System.Collections.ICollection.SyncRoot { get { throw null; } } bool System.Collections.IDictionary.IsFixedSize { get { throw null; } } bool System.Collections.IDictionary.IsReadOnly { get { throw null; } } - object System.Collections.IDictionary.this[object key] { get { throw null; } set { } } + object? System.Collections.IDictionary.this[object key] { get { throw null; } set { } } System.Collections.ICollection System.Collections.IDictionary.Keys { get { throw null; } } System.Collections.ICollection System.Collections.IDictionary.Values { get { throw null; } } public System.Collections.Generic.IEqualityComparer ValueComparer { get { throw null; } } @@ -822,12 +828,12 @@ namespace System.Collections.Immutable System.Collections.Immutable.IImmutableDictionary System.Collections.Immutable.IImmutableDictionary.SetItems(System.Collections.Generic.IEnumerable> items) { throw null; } public System.Collections.Immutable.ImmutableSortedDictionary.Builder ToBuilder() { throw null; } public bool TryGetKey(TKey equalKey, out TKey actualKey) { throw null; } - public bool TryGetValue(TKey key, out TValue value) { throw null; } + public bool TryGetValue(TKey key, [System.Diagnostics.CodeAnalysis.MaybeNullWhenAttribute(false)] out TValue value) { throw null; } #if !NETSTANDARD1_0 public ref readonly TValue ValueRef(TKey key) { throw null; } #endif - public System.Collections.Immutable.ImmutableSortedDictionary WithComparers(System.Collections.Generic.IComparer keyComparer) { throw null; } - public System.Collections.Immutable.ImmutableSortedDictionary WithComparers(System.Collections.Generic.IComparer keyComparer, System.Collections.Generic.IEqualityComparer valueComparer) { throw null; } + public System.Collections.Immutable.ImmutableSortedDictionary WithComparers(System.Collections.Generic.IComparer? keyComparer) { throw null; } + public System.Collections.Immutable.ImmutableSortedDictionary WithComparers(System.Collections.Generic.IComparer? keyComparer, System.Collections.Generic.IEqualityComparer? valueComparer) { throw null; } public sealed partial class Builder : System.Collections.Generic.ICollection>, System.Collections.Generic.IDictionary, System.Collections.Generic.IEnumerable>, System.Collections.Generic.IReadOnlyCollection>, System.Collections.Generic.IReadOnlyDictionary, System.Collections.ICollection, System.Collections.IDictionary, System.Collections.IEnumerable { internal Builder() { } @@ -842,7 +848,7 @@ namespace System.Collections.Immutable object System.Collections.ICollection.SyncRoot { get { throw null; } } bool System.Collections.IDictionary.IsFixedSize { get { throw null; } } bool System.Collections.IDictionary.IsReadOnly { get { throw null; } } - object System.Collections.IDictionary.this[object key] { get { throw null; } set { } } + object? System.Collections.IDictionary.this[object key] { get { throw null; } set { } } System.Collections.ICollection System.Collections.IDictionary.Keys { get { throw null; } } System.Collections.ICollection System.Collections.IDictionary.Values { get { throw null; } } public System.Collections.Generic.IEqualityComparer ValueComparer { get { throw null; } set { } } @@ -855,6 +861,7 @@ namespace System.Collections.Immutable public bool ContainsKey(TKey key) { throw null; } public bool ContainsValue(TValue value) { throw null; } public System.Collections.Immutable.ImmutableSortedDictionary.Enumerator GetEnumerator() { throw null; } + [return: System.Diagnostics.CodeAnalysis.MaybeNullAttribute] public TValue GetValueOrDefault(TKey key) { throw null; } public TValue GetValueOrDefault(TKey key, TValue defaultValue) { throw null; } public bool Remove(System.Collections.Generic.KeyValuePair item) { throw null; } @@ -863,14 +870,14 @@ namespace System.Collections.Immutable void System.Collections.Generic.ICollection>.CopyTo(System.Collections.Generic.KeyValuePair[] array, int arrayIndex) { } System.Collections.Generic.IEnumerator> System.Collections.Generic.IEnumerable>.GetEnumerator() { throw null; } void System.Collections.ICollection.CopyTo(System.Array array, int index) { } - void System.Collections.IDictionary.Add(object key, object value) { } + void System.Collections.IDictionary.Add(object key, object? value) { } bool System.Collections.IDictionary.Contains(object key) { throw null; } System.Collections.IDictionaryEnumerator System.Collections.IDictionary.GetEnumerator() { throw null; } void System.Collections.IDictionary.Remove(object key) { } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { throw null; } public System.Collections.Immutable.ImmutableSortedDictionary ToImmutable() { throw null; } public bool TryGetKey(TKey equalKey, out TKey actualKey) { throw null; } - public bool TryGetValue(TKey key, out TValue value) { throw null; } + public bool TryGetValue(TKey key, [System.Diagnostics.CodeAnalysis.MaybeNullWhenAttribute(false)] out TValue value) { throw null; } #if !NETSTANDARD1_0 public ref readonly TValue ValueRef(TKey key) { throw null; } #endif @@ -890,17 +897,17 @@ namespace System.Collections.Immutable public static partial class ImmutableSortedSet { public static System.Collections.Immutable.ImmutableSortedSet.Builder CreateBuilder() { throw null; } - public static System.Collections.Immutable.ImmutableSortedSet.Builder CreateBuilder(System.Collections.Generic.IComparer comparer) { throw null; } - public static System.Collections.Immutable.ImmutableSortedSet CreateRange(System.Collections.Generic.IComparer comparer, System.Collections.Generic.IEnumerable items) { throw null; } + public static System.Collections.Immutable.ImmutableSortedSet.Builder CreateBuilder(System.Collections.Generic.IComparer? comparer) { throw null; } + public static System.Collections.Immutable.ImmutableSortedSet CreateRange(System.Collections.Generic.IComparer? comparer, System.Collections.Generic.IEnumerable items) { throw null; } public static System.Collections.Immutable.ImmutableSortedSet CreateRange(System.Collections.Generic.IEnumerable items) { throw null; } public static System.Collections.Immutable.ImmutableSortedSet Create() { throw null; } - public static System.Collections.Immutable.ImmutableSortedSet Create(System.Collections.Generic.IComparer comparer) { throw null; } - public static System.Collections.Immutable.ImmutableSortedSet Create(System.Collections.Generic.IComparer comparer, T item) { throw null; } - public static System.Collections.Immutable.ImmutableSortedSet Create(System.Collections.Generic.IComparer comparer, params T[] items) { throw null; } + public static System.Collections.Immutable.ImmutableSortedSet Create(System.Collections.Generic.IComparer? comparer) { throw null; } + public static System.Collections.Immutable.ImmutableSortedSet Create(System.Collections.Generic.IComparer? comparer, T item) { throw null; } + public static System.Collections.Immutable.ImmutableSortedSet Create(System.Collections.Generic.IComparer? comparer, params T[] items) { throw null; } public static System.Collections.Immutable.ImmutableSortedSet Create(T item) { throw null; } public static System.Collections.Immutable.ImmutableSortedSet Create(params T[] items) { throw null; } public static System.Collections.Immutable.ImmutableSortedSet ToImmutableSortedSet(this System.Collections.Generic.IEnumerable source) { throw null; } - public static System.Collections.Immutable.ImmutableSortedSet ToImmutableSortedSet(this System.Collections.Generic.IEnumerable source, System.Collections.Generic.IComparer comparer) { throw null; } + public static System.Collections.Immutable.ImmutableSortedSet ToImmutableSortedSet(this System.Collections.Generic.IEnumerable source, System.Collections.Generic.IComparer? comparer) { throw null; } public static System.Collections.Immutable.ImmutableSortedSet ToImmutableSortedSet(this System.Collections.Immutable.ImmutableSortedSet.Builder builder) { throw null; } } public sealed partial class ImmutableSortedSet : System.Collections.Generic.ICollection, System.Collections.Generic.IEnumerable, System.Collections.Generic.IList, System.Collections.Generic.IReadOnlyCollection, System.Collections.Generic.IReadOnlyList, System.Collections.Generic.ISet, System.Collections.ICollection, System.Collections.IEnumerable, System.Collections.IList, System.Collections.Immutable.IImmutableSet @@ -911,7 +918,9 @@ namespace System.Collections.Immutable public bool IsEmpty { get { throw null; } } public T this[int index] { get { throw null; } } public System.Collections.Generic.IComparer KeyComparer { get { throw null; } } + [System.Diagnostics.CodeAnalysis.MaybeNullAttribute] public T Max { get { throw null; } } + [System.Diagnostics.CodeAnalysis.MaybeNullAttribute] public T Min { get { throw null; } } bool System.Collections.Generic.ICollection.IsReadOnly { get { throw null; } } T System.Collections.Generic.IList.this[int index] { get { throw null; } set { } } @@ -919,7 +928,7 @@ namespace System.Collections.Immutable object System.Collections.ICollection.SyncRoot { get { throw null; } } bool System.Collections.IList.IsFixedSize { get { throw null; } } bool System.Collections.IList.IsReadOnly { get { throw null; } } - object System.Collections.IList.this[int index] { get { throw null; } set { } } + object? System.Collections.IList.this[int index] { get { throw null; } set { } } public System.Collections.Immutable.ImmutableSortedSet Add(T value) { throw null; } public System.Collections.Immutable.ImmutableSortedSet Clear() { throw null; } public bool Contains(T value) { throw null; } @@ -953,12 +962,12 @@ namespace System.Collections.Immutable void System.Collections.Generic.ISet.UnionWith(System.Collections.Generic.IEnumerable other) { } void System.Collections.ICollection.CopyTo(System.Array array, int index) { } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { throw null; } - int System.Collections.IList.Add(object value) { throw null; } + int System.Collections.IList.Add(object? value) { throw null; } void System.Collections.IList.Clear() { } - bool System.Collections.IList.Contains(object value) { throw null; } - int System.Collections.IList.IndexOf(object value) { throw null; } - void System.Collections.IList.Insert(int index, object value) { } - void System.Collections.IList.Remove(object value) { } + bool System.Collections.IList.Contains(object? value) { throw null; } + int System.Collections.IList.IndexOf(object? value) { throw null; } + void System.Collections.IList.Insert(int index, object? value) { } + void System.Collections.IList.Remove(object? value) { } void System.Collections.IList.RemoveAt(int index) { } System.Collections.Immutable.IImmutableSet System.Collections.Immutable.IImmutableSet.Add(T value) { throw null; } System.Collections.Immutable.IImmutableSet System.Collections.Immutable.IImmutableSet.Clear() { throw null; } @@ -970,14 +979,16 @@ namespace System.Collections.Immutable public System.Collections.Immutable.ImmutableSortedSet.Builder ToBuilder() { throw null; } public bool TryGetValue(T equalValue, out T actualValue) { throw null; } public System.Collections.Immutable.ImmutableSortedSet Union(System.Collections.Generic.IEnumerable other) { throw null; } - public System.Collections.Immutable.ImmutableSortedSet WithComparer(System.Collections.Generic.IComparer comparer) { throw null; } + public System.Collections.Immutable.ImmutableSortedSet WithComparer(System.Collections.Generic.IComparer? comparer) { throw null; } public sealed partial class Builder : System.Collections.Generic.ICollection, System.Collections.Generic.IEnumerable, System.Collections.Generic.IReadOnlyCollection, System.Collections.Generic.ISet, System.Collections.ICollection, System.Collections.IEnumerable { internal Builder() { } public int Count { get { throw null; } } public T this[int index] { get { throw null; } } public System.Collections.Generic.IComparer KeyComparer { get { throw null; } set { } } + [System.Diagnostics.CodeAnalysis.MaybeNullAttribute] public T Max { get { throw null; } } + [System.Diagnostics.CodeAnalysis.MaybeNullAttribute] public T Min { get { throw null; } } bool System.Collections.Generic.ICollection.IsReadOnly { get { throw null; } } bool System.Collections.ICollection.IsSynchronized { get { throw null; } } @@ -1014,7 +1025,7 @@ namespace System.Collections.Immutable private object _dummy; private int _dummyPrimitive; public T Current { get { throw null; } } - object System.Collections.IEnumerator.Current { get { throw null; } } + object? System.Collections.IEnumerator.Current { get { throw null; } } public void Dispose() { } public bool MoveNext() { throw null; } public void Reset() { } @@ -1061,6 +1072,7 @@ namespace System.Linq { public static partial class ImmutableArrayExtensions { + [return: System.Diagnostics.CodeAnalysis.MaybeNullAttribute] public static T Aggregate(this System.Collections.Immutable.ImmutableArray immutableArray, System.Func func) { throw null; } public static TAccumulate Aggregate(this System.Collections.Immutable.ImmutableArray immutableArray, TAccumulate seed, System.Func func) { throw null; } public static TResult Aggregate(this System.Collections.Immutable.ImmutableArray immutableArray, TAccumulate seed, System.Func func, System.Func resultSelector) { throw null; } @@ -1068,34 +1080,43 @@ namespace System.Linq public static bool Any(this System.Collections.Immutable.ImmutableArray immutableArray) { throw null; } public static bool Any(this System.Collections.Immutable.ImmutableArray immutableArray, System.Func predicate) { throw null; } public static bool Any(this System.Collections.Immutable.ImmutableArray.Builder builder) { throw null; } + [return: System.Diagnostics.CodeAnalysis.MaybeNullAttribute] public static T ElementAtOrDefault(this System.Collections.Immutable.ImmutableArray immutableArray, int index) { throw null; } public static T ElementAt(this System.Collections.Immutable.ImmutableArray immutableArray, int index) { throw null; } + [return: System.Diagnostics.CodeAnalysis.MaybeNullAttribute] public static T FirstOrDefault(this System.Collections.Immutable.ImmutableArray immutableArray) { throw null; } + [return: System.Diagnostics.CodeAnalysis.MaybeNullAttribute] public static T FirstOrDefault(this System.Collections.Immutable.ImmutableArray immutableArray, System.Func predicate) { throw null; } + [return: System.Diagnostics.CodeAnalysis.MaybeNullAttribute] public static T FirstOrDefault(this System.Collections.Immutable.ImmutableArray.Builder builder) { throw null; } public static T First(this System.Collections.Immutable.ImmutableArray immutableArray) { throw null; } public static T First(this System.Collections.Immutable.ImmutableArray immutableArray, System.Func predicate) { throw null; } public static T First(this System.Collections.Immutable.ImmutableArray.Builder builder) { throw null; } + [return: System.Diagnostics.CodeAnalysis.MaybeNullAttribute] public static T LastOrDefault(this System.Collections.Immutable.ImmutableArray immutableArray) { throw null; } + [return: System.Diagnostics.CodeAnalysis.MaybeNullAttribute] public static T LastOrDefault(this System.Collections.Immutable.ImmutableArray immutableArray, System.Func predicate) { throw null; } + [return: System.Diagnostics.CodeAnalysis.MaybeNullAttribute] public static T LastOrDefault(this System.Collections.Immutable.ImmutableArray.Builder builder) { throw null; } public static T Last(this System.Collections.Immutable.ImmutableArray immutableArray) { throw null; } public static T Last(this System.Collections.Immutable.ImmutableArray immutableArray, System.Func predicate) { throw null; } public static T Last(this System.Collections.Immutable.ImmutableArray.Builder builder) { throw null; } public static System.Collections.Generic.IEnumerable SelectMany(this System.Collections.Immutable.ImmutableArray immutableArray, System.Func> collectionSelector, System.Func resultSelector) { throw null; } public static System.Collections.Generic.IEnumerable Select(this System.Collections.Immutable.ImmutableArray immutableArray, System.Func selector) { throw null; } - public static bool SequenceEqual(this System.Collections.Immutable.ImmutableArray immutableArray, System.Collections.Generic.IEnumerable items, System.Collections.Generic.IEqualityComparer comparer = null) where TDerived : TBase { throw null; } - public static bool SequenceEqual(this System.Collections.Immutable.ImmutableArray immutableArray, System.Collections.Immutable.ImmutableArray items, System.Collections.Generic.IEqualityComparer comparer = null) where TDerived : TBase { throw null; } + public static bool SequenceEqual(this System.Collections.Immutable.ImmutableArray immutableArray, System.Collections.Generic.IEnumerable items, System.Collections.Generic.IEqualityComparer? comparer = null) where TDerived : TBase { throw null; } + public static bool SequenceEqual(this System.Collections.Immutable.ImmutableArray immutableArray, System.Collections.Immutable.ImmutableArray items, System.Collections.Generic.IEqualityComparer? comparer = null) where TDerived : TBase { throw null; } public static bool SequenceEqual(this System.Collections.Immutable.ImmutableArray immutableArray, System.Collections.Immutable.ImmutableArray items, System.Func predicate) where TDerived : TBase { throw null; } + [return: System.Diagnostics.CodeAnalysis.MaybeNullAttribute] public static T SingleOrDefault(this System.Collections.Immutable.ImmutableArray immutableArray) { throw null; } + [return: System.Diagnostics.CodeAnalysis.MaybeNullAttribute] public static T SingleOrDefault(this System.Collections.Immutable.ImmutableArray immutableArray, System.Func predicate) { throw null; } public static T Single(this System.Collections.Immutable.ImmutableArray immutableArray) { throw null; } public static T Single(this System.Collections.Immutable.ImmutableArray immutableArray, System.Func predicate) { throw null; } public static T[] ToArray(this System.Collections.Immutable.ImmutableArray immutableArray) { throw null; } - public static System.Collections.Generic.Dictionary ToDictionary(this System.Collections.Immutable.ImmutableArray immutableArray, System.Func keySelector) { throw null; } - public static System.Collections.Generic.Dictionary ToDictionary(this System.Collections.Immutable.ImmutableArray immutableArray, System.Func keySelector, System.Collections.Generic.IEqualityComparer comparer) { throw null; } - public static System.Collections.Generic.Dictionary ToDictionary(this System.Collections.Immutable.ImmutableArray immutableArray, System.Func keySelector, System.Func elementSelector) { throw null; } - public static System.Collections.Generic.Dictionary ToDictionary(this System.Collections.Immutable.ImmutableArray immutableArray, System.Func keySelector, System.Func elementSelector, System.Collections.Generic.IEqualityComparer comparer) { throw null; } + public static System.Collections.Generic.Dictionary ToDictionary(this System.Collections.Immutable.ImmutableArray immutableArray, System.Func keySelector) where TKey : notnull { throw null; } + public static System.Collections.Generic.Dictionary ToDictionary(this System.Collections.Immutable.ImmutableArray immutableArray, System.Func keySelector, System.Collections.Generic.IEqualityComparer? comparer) where TKey : notnull { throw null; } + public static System.Collections.Generic.Dictionary ToDictionary(this System.Collections.Immutable.ImmutableArray immutableArray, System.Func keySelector, System.Func elementSelector) where TKey : notnull { throw null; } + public static System.Collections.Generic.Dictionary ToDictionary(this System.Collections.Immutable.ImmutableArray immutableArray, System.Func keySelector, System.Func elementSelector, System.Collections.Generic.IEqualityComparer? comparer) where TKey : notnull { throw null; } public static System.Collections.Generic.IEnumerable Where(this System.Collections.Immutable.ImmutableArray immutableArray, System.Func predicate) { throw null; } } } diff --git a/src/libraries/System.Collections.Immutable/ref/System.Collections.Immutable.csproj b/src/libraries/System.Collections.Immutable/ref/System.Collections.Immutable.csproj index 18c3b30..ec1a0ad 100644 --- a/src/libraries/System.Collections.Immutable/ref/System.Collections.Immutable.csproj +++ b/src/libraries/System.Collections.Immutable/ref/System.Collections.Immutable.csproj @@ -1,6 +1,7 @@ $(NetCoreAppCurrent)-Debug;$(NetCoreAppCurrent)-Release;netstandard2.0-Debug;netstandard2.0-Release;netstandard1.0-Debug;netstandard1.0-Release;netstandard1.3-Debug;netstandard1.3-Release + enable diff --git a/src/libraries/System.Collections.Immutable/src/System.Collections.Immutable.csproj b/src/libraries/System.Collections.Immutable/src/System.Collections.Immutable.csproj index 72d1a44..41fd39c 100644 --- a/src/libraries/System.Collections.Immutable/src/System.Collections.Immutable.csproj +++ b/src/libraries/System.Collections.Immutable/src/System.Collections.Immutable.csproj @@ -5,6 +5,7 @@ 512 netstandard1.0;portable-net45+win8+wp8+wpa81 $(NetCoreAppCurrent)-Debug;$(NetCoreAppCurrent)-Release;netstandard2.0-Debug;netstandard2.0-Release;netstandard1.0-Debug;netstandard1.0-Release;netstandard1.3-Debug;netstandard1.3-Release + enable diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/AllocFreeConcurrentStack.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/AllocFreeConcurrentStack.cs index 5f32156..7d68aba 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/AllocFreeConcurrentStack.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/AllocFreeConcurrentStack.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; namespace System.Collections.Immutable { @@ -22,7 +23,7 @@ namespace System.Collections.Immutable } } - public static bool TryTake(out T item) + public static bool TryTake([MaybeNullWhen(false)] out T item) { Stack> localStack = ThreadLocalStack; if (localStack != null && localStack.Count > 0) @@ -31,7 +32,7 @@ namespace System.Collections.Immutable return true; } - item = default(T); + item = default(T)!; return false; } @@ -40,15 +41,14 @@ namespace System.Collections.Immutable get { // Ensure the [ThreadStatic] is initialized to a dictionary - Dictionary typesToStacks = AllocFreeConcurrentStack.t_stacks; + Dictionary? typesToStacks = AllocFreeConcurrentStack.t_stacks; if (typesToStacks == null) { AllocFreeConcurrentStack.t_stacks = typesToStacks = new Dictionary(); } // Get the stack that corresponds to the T - object stackObj; - if (!typesToStacks.TryGetValue(s_typeOfT, out stackObj)) + if (!typesToStacks.TryGetValue(s_typeOfT, out object? stackObj)) { stackObj = new Stack>(MaxSize); typesToStacks.Add(s_typeOfT, stackObj); @@ -71,6 +71,6 @@ namespace System.Collections.Immutable // When that's fixed, a [ThreadStatic] Stack should be added back to AllocFreeConcurrentStack. [ThreadStatic] - internal static Dictionary t_stacks; + internal static Dictionary? t_stacks; } } diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/DictionaryEnumerator.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/DictionaryEnumerator.cs index 4ba86b3..5a5a369 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/DictionaryEnumerator.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/DictionaryEnumerator.cs @@ -6,7 +6,7 @@ using System.Collections.Generic; namespace System.Collections.Immutable { - internal sealed class DictionaryEnumerator : IDictionaryEnumerator + internal sealed class DictionaryEnumerator : IDictionaryEnumerator where TKey : notnull { private readonly IEnumerator> _inner; @@ -27,7 +27,7 @@ namespace System.Collections.Immutable get { return _inner.Current.Key; } } - public object Value + public object? Value { get { return _inner.Current.Value; } } diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/DisposableEnumeratorAdapter_2.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/DisposableEnumeratorAdapter_2.cs index d2dd253..51c2368 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/DisposableEnumeratorAdapter_2.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/DisposableEnumeratorAdapter_2.cs @@ -18,7 +18,7 @@ namespace System.Collections.Immutable /// /// The enumerator object to use if not null. /// - private readonly IEnumerator _enumeratorObject; + private readonly IEnumerator? _enumeratorObject; /// /// The enumerator struct to use if is null. diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/IBinaryTree.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/IBinaryTree.cs index 0ba2e2f..e4aecda 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/IBinaryTree.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/IBinaryTree.cs @@ -27,12 +27,12 @@ namespace System.Collections.Immutable /// /// Gets the left branch of this node. /// - IBinaryTree Left { get; } + IBinaryTree? Left { get; } /// /// Gets the right branch of this node. /// - IBinaryTree Right { get; } + IBinaryTree? Right { get; } } /// @@ -49,11 +49,11 @@ namespace System.Collections.Immutable /// /// Gets the left branch of this node. /// - new IBinaryTree Left { get; } + new IBinaryTree? Left { get; } /// /// Gets the right branch of this node. /// - new IBinaryTree Right { get; } + new IBinaryTree? Right { get; } } } diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/IImmutableArray.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/IImmutableArray.cs index 8e721e7..7baeb7f 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/IImmutableArray.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/IImmutableArray.cs @@ -21,6 +21,6 @@ namespace System.Collections.Immutable /// /// Gets an untyped reference to the array. /// - Array Array { get; } + Array? Array { get; } } } diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/IImmutableList.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/IImmutableList.cs index 9d75cf2..b6e4297 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/IImmutableList.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/IImmutableList.cs @@ -51,7 +51,7 @@ namespace System.Collections.Immutable /// contains count number of elements, if found; otherwise, -1. /// [Pure] - int IndexOf(T item, int index, int count, IEqualityComparer equalityComparer); + int IndexOf(T item, int index, int count, IEqualityComparer? equalityComparer); /// /// Searches for the specified object and returns the zero-based index of the @@ -75,7 +75,7 @@ namespace System.Collections.Immutable /// and ends at , if found; otherwise, -1. /// [Pure] - int LastIndexOf(T item, int index, int count, IEqualityComparer equalityComparer); + int LastIndexOf(T item, int index, int count, IEqualityComparer? equalityComparer); /// /// Adds the specified value to this list. @@ -121,7 +121,7 @@ namespace System.Collections.Immutable /// /// A new list with the element removed, or this list if the element is not in this list. [Pure] - IImmutableList Remove(T value, IEqualityComparer equalityComparer); + IImmutableList Remove(T value, IEqualityComparer? equalityComparer); /// /// Removes all the elements that match the conditions defined by the specified @@ -149,7 +149,7 @@ namespace System.Collections.Immutable /// A new list with the elements removed. /// [Pure] - IImmutableList RemoveRange(IEnumerable items, IEqualityComparer equalityComparer); + IImmutableList RemoveRange(IEnumerable items, IEqualityComparer? equalityComparer); /// /// Removes the specified values from this list. @@ -191,6 +191,6 @@ namespace System.Collections.Immutable /// The new list -- even if the value being replaced is equal to the new value for that position. /// Thrown when the old value does not exist in the list. [Pure] - IImmutableList Replace(T oldValue, T newValue, IEqualityComparer equalityComparer); + IImmutableList Replace(T oldValue, T newValue, IEqualityComparer? equalityComparer); } } diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/IImmutableListQueries.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/IImmutableListQueries.cs index c346077..36bcbe5 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/IImmutableListQueries.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/IImmutableListQueries.cs @@ -296,7 +296,7 @@ namespace System.Collections.Immutable /// cannot find an implementation of the generic interface /// or the interface for type . /// - int BinarySearch(T item, IComparer comparer); + int BinarySearch(T item, IComparer? comparer); /// /// Searches a range of elements in the sorted @@ -327,6 +327,6 @@ namespace System.Collections.Immutable /// cannot find an implementation of the generic interface /// or the interface for type . /// - int BinarySearch(int index, int count, T item, IComparer comparer); + int BinarySearch(int index, int count, T item, IComparer? comparer); } } diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray.cs index d112226..af69f23 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray.cs @@ -106,7 +106,7 @@ namespace System.Collections.Immutable var immutableArray = items as IImmutableArray; if (immutableArray != null) { - Array array = immutableArray.Array; + Array? array = immutableArray.Array; if (array == null) { throw new InvalidOperationException(SR.InvalidOperationOnDefaultArray); @@ -143,7 +143,7 @@ namespace System.Collections.Immutable /// The elements to store in the array. /// An immutable array. [Pure] - public static ImmutableArray Create(params T[] items) + public static ImmutableArray Create(params T[]? items) { if (items == null) { @@ -217,7 +217,7 @@ namespace System.Collections.Immutable } var array = new T[length]; - Array.Copy(items.array, start, array, 0, length); + Array.Copy(items.array!, start, array, 0, length); return new ImmutableArray(array); } @@ -432,7 +432,7 @@ namespace System.Collections.Immutable [Pure] public static int BinarySearch(this ImmutableArray array, T value) { - return Array.BinarySearch(array.array, value); + return Array.BinarySearch(array.array!, value); } /// @@ -461,9 +461,9 @@ namespace System.Collections.Immutable /// generic interface. /// [Pure] - public static int BinarySearch(this ImmutableArray array, T value, IComparer comparer) + public static int BinarySearch(this ImmutableArray array, T value, IComparer? comparer) { - return Array.BinarySearch(array.array, value, comparer); + return Array.BinarySearch(array.array!, value, comparer); } /// @@ -498,7 +498,7 @@ namespace System.Collections.Immutable [Pure] public static int BinarySearch(this ImmutableArray array, int index, int length, T value) { - return Array.BinarySearch(array.array, index, length, value); + return Array.BinarySearch(array.array!, index, length, value); } /// @@ -537,9 +537,9 @@ namespace System.Collections.Immutable /// is less than the lower bound of . -or- is less than zero. /// [Pure] - public static int BinarySearch(this ImmutableArray array, int index, int length, T value, IComparer comparer) + public static int BinarySearch(this ImmutableArray array, int index, int length, T value, IComparer? comparer) { - return Array.BinarySearch(array.array, index, length, value, comparer); + return Array.BinarySearch(array.array!, index, length, value, comparer); } /// diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray_1.Builder.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray_1.Builder.cs index db94802..ca27188 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray_1.Builder.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray_1.Builder.cs @@ -75,7 +75,7 @@ namespace System.Collections.Immutable } else { - _elements = ImmutableArray.Empty.array; + _elements = ImmutableArray.Empty.array!; } } } @@ -113,7 +113,7 @@ namespace System.Collections.Immutable { for (int i = value; i < this.Count; i++) { - _elements[i] = default(T); + _elements[i] = default(T)!; } } } @@ -211,7 +211,7 @@ namespace System.Collections.Immutable } T[] temp = _elements; - _elements = ImmutableArray.Empty.array; + _elements = ImmutableArray.Empty.array!; _count = 0; return new ImmutableArray(temp); } @@ -433,7 +433,7 @@ namespace System.Collections.Immutable { if (this.Count == 0) { - return Empty.array; + return Empty.array!; } T[] result = new T[this.Count]; @@ -516,7 +516,7 @@ namespace System.Collections.Immutable /// /// The 0-based index into the array where the item was found; or -1 if it could not be found. [Pure] - public int IndexOf(T item, int startIndex, int count, IEqualityComparer equalityComparer) + public int IndexOf(T item, int startIndex, int count, IEqualityComparer? equalityComparer) { if (count == 0 && startIndex == 0) { @@ -602,7 +602,7 @@ namespace System.Collections.Immutable /// The equality comparer to use in the search. /// The 0-based index into the array where the item was found; or -1 if it could not be found. [Pure] - public int LastIndexOf(T item, int startIndex, int count, IEqualityComparer equalityComparer) + public int LastIndexOf(T item, int startIndex, int count, IEqualityComparer? equalityComparer) { if (count == 0 && startIndex == 0) { @@ -691,7 +691,7 @@ namespace System.Collections.Immutable /// Sorts the array. /// /// The comparer to use in sorting. If null, the default comparer is used. - public void Sort(IComparer comparer) + public void Sort(IComparer? comparer) { if (Count > 1) { @@ -705,7 +705,7 @@ namespace System.Collections.Immutable /// The index of the first element to consider in the sort. /// The number of elements to include in the sort. /// The comparer to use in sorting. If null, the default comparer is used. - public void Sort(int index, int count, IComparer comparer) + public void Sort(int index, int count, IComparer? comparer) { // Don't rely on Array.Sort's argument validation since our internal array may exceed // the bounds of the publicly addressable region. diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray_1.Enumerator.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray_1.Enumerator.cs index 56638d5..98ff82f 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray_1.Enumerator.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray_1.Enumerator.cs @@ -75,7 +75,7 @@ namespace System.Collections.Immutable /// A shareable singleton for enumerating empty arrays. /// private static readonly IEnumerator s_EmptyEnumerator = - new EnumeratorObject(ImmutableArray.Empty.array); + new EnumeratorObject(ImmutableArray.Empty.array!); /// /// The array being enumerated. @@ -122,7 +122,7 @@ namespace System.Collections.Immutable /// /// Gets the currently enumerated value. /// - object IEnumerator.Current + object? IEnumerator.Current { get { return this.Current; } } diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray_1.Minimal.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray_1.Minimal.cs index 4b59ffc..c134fe0 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray_1.Minimal.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray_1.Minimal.cs @@ -50,14 +50,14 @@ namespace System.Collections.Immutable /// This would be private, but we make it internal so that our own extension methods can access it. /// [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)] - internal T[] array; + internal T[]? array; /// /// Initializes a new instance of the struct /// *without making a defensive copy*. /// /// The array to use. May be null for "default" arrays. - internal ImmutableArray(T[] items) + internal ImmutableArray(T[]? items) { this.array = items; } @@ -127,7 +127,7 @@ namespace System.Collections.Immutable // The reason for this is perf. // Length and the indexer must be absolutely trivially implemented for the JIT optimization // of removing array bounds checking to work. - return this.array[index]; + return this.array![index]; } } @@ -144,7 +144,7 @@ namespace System.Collections.Immutable // The reason for this is perf. // Length and the indexer must be absolutely trivially implemented for the JIT optimization // of removing array bounds checking to work. - return ref this.array[index]; + return ref this.array![index]; } #endif @@ -172,7 +172,7 @@ namespace System.Collections.Immutable // The reason for this is perf. // Length and the indexer must be absolutely trivially implemented for the JIT optimization // of removing array bounds checking to work. - return this.array.Length; + return this.array!.Length; } } @@ -202,7 +202,7 @@ namespace System.Collections.Immutable /// Gets an untyped reference to the array. /// [DebuggerBrowsable(DebuggerBrowsableState.Never)] - Array IImmutableArray.Array + Array? IImmutableArray.Array { get { return this.array; } } @@ -229,7 +229,7 @@ namespace System.Collections.Immutable { var self = this; self.ThrowNullRefIfNotInitialized(); - Array.Copy(self.array, 0, destination, 0, self.Length); + Array.Copy(self.array!, 0, destination, 0, self.Length); } /// @@ -242,7 +242,7 @@ namespace System.Collections.Immutable { var self = this; self.ThrowNullRefIfNotInitialized(); - Array.Copy(self.array, 0, destination, destinationIndex, self.Length); + Array.Copy(self.array!, 0, destination, destinationIndex, self.Length); } /// @@ -257,7 +257,7 @@ namespace System.Collections.Immutable { var self = this; self.ThrowNullRefIfNotInitialized(); - Array.Copy(self.array, sourceIndex, destination, destinationIndex, length); + Array.Copy(self.array!, sourceIndex, destination, destinationIndex, length); } /// @@ -287,7 +287,7 @@ namespace System.Collections.Immutable { var self = this; self.ThrowNullRefIfNotInitialized(); - return new Enumerator(self.array); + return new Enumerator(self.array!); } /// @@ -311,15 +311,9 @@ namespace System.Collections.Immutable /// true if the specified is equal to this instance; otherwise, false. /// [Pure] - public override bool Equals(object obj) + public override bool Equals(object? obj) { - IImmutableArray other = obj as IImmutableArray; - if (other != null) - { - return this.array == other.Array; - } - - return false; + return obj is IImmutableArray other && this.array == other.Array; } /// @@ -360,7 +354,7 @@ namespace System.Collections.Immutable [Pure] public ImmutableArray CastArray() where TOther : class { - return new ImmutableArray((TOther[])(object)array); + return new ImmutableArray((TOther[])(object)array!); } /// @@ -381,7 +375,7 @@ namespace System.Collections.Immutable [Pure] public ImmutableArray As() where TOther : class { - return new ImmutableArray(this.array as TOther[]); + return new ImmutableArray((this.array as TOther[])!); } /// @@ -394,7 +388,7 @@ namespace System.Collections.Immutable { var self = this; self.ThrowInvalidOperationIfNotInitialized(); - return EnumeratorObject.Create(self.array); + return EnumeratorObject.Create(self.array!); } /// @@ -407,7 +401,7 @@ namespace System.Collections.Immutable { var self = this; self.ThrowInvalidOperationIfNotInitialized(); - return EnumeratorObject.Create(self.array); + return EnumeratorObject.Create(self.array!); } /// @@ -423,7 +417,7 @@ namespace System.Collections.Immutable // if we are going to do anything with the array, we will need Length anyways // so touching it, and potentially causing a cache miss, is not going to be an // extra expense. - _ = this.array.Length; + _ = this.array!.Length; } /// diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray_1.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray_1.cs index 1805f9b..81f7fe1 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray_1.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray_1.cs @@ -117,7 +117,7 @@ namespace System.Collections.Immutable /// The equality comparer to use in the search. /// The 0-based index into the array where the item was found; or -1 if it could not be found. [Pure] - public int IndexOf(T item, int startIndex, IEqualityComparer equalityComparer) + public int IndexOf(T item, int startIndex, IEqualityComparer? equalityComparer) { var self = this; return self.IndexOf(item, startIndex, self.Length - startIndex, equalityComparer); @@ -161,7 +161,7 @@ namespace System.Collections.Immutable /// /// The 0-based index into the array where the item was found; or -1 if it could not be found. [Pure] - public int IndexOf(T item, int startIndex, int count, IEqualityComparer equalityComparer) + public int IndexOf(T item, int startIndex, int count, IEqualityComparer? equalityComparer) { var self = this; self.ThrowNullRefIfNotInitialized(); @@ -177,13 +177,13 @@ namespace System.Collections.Immutable equalityComparer = equalityComparer ?? EqualityComparer.Default; if (equalityComparer == EqualityComparer.Default) { - return Array.IndexOf(self.array, item, startIndex, count); + return Array.IndexOf(self.array!, item, startIndex, count); } else { for (int i = startIndex; i < startIndex + count; i++) { - if (equalityComparer.Equals(self.array[i], item)) + if (equalityComparer.Equals(self.array![i], item)) { return i; } @@ -250,7 +250,7 @@ namespace System.Collections.Immutable /// The equality comparer to use in the search. /// The 0-based index into the array where the item was found; or -1 if it could not be found. [Pure] - public int LastIndexOf(T item, int startIndex, int count, IEqualityComparer equalityComparer) + public int LastIndexOf(T item, int startIndex, int count, IEqualityComparer? equalityComparer) { var self = this; self.ThrowNullRefIfNotInitialized(); @@ -266,13 +266,13 @@ namespace System.Collections.Immutable equalityComparer = equalityComparer ?? EqualityComparer.Default; if (equalityComparer == EqualityComparer.Default) { - return Array.LastIndexOf(self.array, item, startIndex, count); + return Array.LastIndexOf(self.array!, item, startIndex, count); } else { for (int i = startIndex; i >= startIndex - count + 1; i--) { - if (equalityComparer.Equals(item, self.array[i])) + if (equalityComparer.Equals(item, self.array![i])) { return i; } @@ -316,11 +316,11 @@ namespace System.Collections.Immutable if (index != 0) { - Array.Copy(self.array, tmp, index); + Array.Copy(self.array!, tmp, index); } if (index != self.Length) { - Array.Copy(self.array, index, tmp, index + 1, self.Length - index); + Array.Copy(self.array!, index, tmp, index + 1, self.Length - index); } return new ImmutableArray(tmp); @@ -355,11 +355,11 @@ namespace System.Collections.Immutable if (index != 0) { - Array.Copy(self.array, tmp, index); + Array.Copy(self.array!, tmp, index); } if (index != self.Length) { - Array.Copy(self.array, index, tmp, index + count, self.Length - index); + Array.Copy(self.array!, index, tmp, index + count, self.Length - index); } // We want to copy over the items we need to insert. @@ -407,14 +407,14 @@ namespace System.Collections.Immutable if (index != 0) { - Array.Copy(self.array, tmp, index); + Array.Copy(self.array!, tmp, index); } if (index != self.Length) { - Array.Copy(self.array, index, tmp, index + items.Length, self.Length - index); + Array.Copy(self.array!, index, tmp, index + items.Length, self.Length - index); } - Array.Copy(items.array, 0, tmp, index, items.Length); + Array.Copy(items.array!, 0, tmp, index, items.Length); return new ImmutableArray(tmp); } @@ -474,7 +474,7 @@ namespace System.Collections.Immutable Requires.Range(index >= 0 && index < self.Length, nameof(index)); T[] tmp = new T[self.Length]; - Array.Copy(self.array, tmp, self.Length); + Array.Copy(self.array!, tmp, self.Length); tmp[index] = item; return new ImmutableArray(tmp); } @@ -504,7 +504,7 @@ namespace System.Collections.Immutable /// The new list -- even if the value being replaced is equal to the new value for that position. /// Thrown when the old value does not exist in the list. [Pure] - public ImmutableArray Replace(T oldValue, T newValue, IEqualityComparer equalityComparer) + public ImmutableArray Replace(T oldValue, T newValue, IEqualityComparer? equalityComparer) { var self = this; int index = self.IndexOf(oldValue, 0, self.Length, equalityComparer); @@ -539,7 +539,7 @@ namespace System.Collections.Immutable /// /// The new array. [Pure] - public ImmutableArray Remove(T item, IEqualityComparer equalityComparer) + public ImmutableArray Remove(T item, IEqualityComparer? equalityComparer) { var self = this; self.ThrowNullRefIfNotInitialized(); @@ -580,8 +580,8 @@ namespace System.Collections.Immutable } T[] tmp = new T[self.Length - length]; - Array.Copy(self.array, tmp, index); - Array.Copy(self.array, index + length, tmp, index, self.Length - index - length); + Array.Copy(self.array!, tmp, index); + Array.Copy(self.array!, index + length, tmp, index, self.Length - index - length); return new ImmutableArray(tmp); } @@ -610,7 +610,7 @@ namespace System.Collections.Immutable /// A new list with the elements removed. /// [Pure] - public ImmutableArray RemoveRange(IEnumerable items, IEqualityComparer equalityComparer) + public ImmutableArray RemoveRange(IEnumerable items, IEqualityComparer? equalityComparer) { var self = this; self.ThrowNullRefIfNotInitialized(); @@ -654,10 +654,10 @@ namespace System.Collections.Immutable /// A new list with the elements removed. /// [Pure] - public ImmutableArray RemoveRange(ImmutableArray items, IEqualityComparer equalityComparer) + public ImmutableArray RemoveRange(ImmutableArray items, IEqualityComparer? equalityComparer) { var self = this; - Requires.NotNull(items.array, nameof(items)); + Requires.NotNull(items.array!, nameof(items)); if (items.IsEmpty) { @@ -670,7 +670,7 @@ namespace System.Collections.Immutable } else { - return self.RemoveRange(items.array, equalityComparer); + return self.RemoveRange(items.array!, equalityComparer); } } @@ -697,8 +697,8 @@ namespace System.Collections.Immutable return self; } - List removeIndices = null; - for (int i = 0; i < self.array.Length; i++) + List? removeIndices = null; + for (int i = 0; i < self.array!.Length; i++) { if (match(self.array[i])) { @@ -758,7 +758,7 @@ namespace System.Collections.Immutable /// /// The comparer to use in sorting. If null, the default comparer is used. [Pure] - public ImmutableArray Sort(IComparer comparer) + public ImmutableArray Sort(IComparer? comparer) { var self = this; return self.Sort(0, self.Length, comparer); @@ -771,7 +771,7 @@ namespace System.Collections.Immutable /// The number of elements to include in the sort. /// The comparer to use in sorting. If null, the default comparer is used. [Pure] - public ImmutableArray Sort(int index, int count, IComparer comparer) + public ImmutableArray Sort(int index, int count, IComparer? comparer) { var self = this; self.ThrowNullRefIfNotInitialized(); @@ -790,7 +790,7 @@ namespace System.Collections.Immutable bool outOfOrder = false; for (int i = index + 1; i < index + count; i++) { - if (comparer.Compare(self.array[i - 1], self.array[i]) > 0) + if (comparer.Compare(self.array![i - 1], self.array[i]) > 0) { outOfOrder = true; break; @@ -800,7 +800,7 @@ namespace System.Collections.Immutable if (outOfOrder) { var tmp = new T[self.Length]; - Array.Copy(self.array, tmp, self.Length); + Array.Copy(self.array!, tmp, self.Length); Array.Sort(tmp, index, count, comparer); return new ImmutableArray(tmp); } @@ -919,7 +919,7 @@ namespace System.Collections.Immutable /// See /// [ExcludeFromCodeCoverage] - IImmutableList IImmutableList.Remove(T value, IEqualityComparer equalityComparer) + IImmutableList IImmutableList.Remove(T value, IEqualityComparer? equalityComparer) { var self = this; self.ThrowInvalidOperationIfNotInitialized(); @@ -941,7 +941,7 @@ namespace System.Collections.Immutable /// See /// [ExcludeFromCodeCoverage] - IImmutableList IImmutableList.RemoveRange(IEnumerable items, IEqualityComparer equalityComparer) + IImmutableList IImmutableList.RemoveRange(IEnumerable items, IEqualityComparer? equalityComparer) { var self = this; self.ThrowInvalidOperationIfNotInitialized(); @@ -984,7 +984,7 @@ namespace System.Collections.Immutable /// /// See /// - IImmutableList IImmutableList.Replace(T oldValue, T newValue, IEqualityComparer equalityComparer) + IImmutableList IImmutableList.Replace(T oldValue, T newValue, IEqualityComparer? equalityComparer) { var self = this; self.ThrowInvalidOperationIfNotInitialized(); @@ -1000,7 +1000,7 @@ namespace System.Collections.Immutable /// /// [ExcludeFromCodeCoverage] - int IList.Add(object value) + int IList.Add(object? value) { throw new NotSupportedException(); } @@ -1023,11 +1023,11 @@ namespace System.Collections.Immutable /// true if the is found in the ; otherwise, false. /// [ExcludeFromCodeCoverage] - bool IList.Contains(object value) + bool IList.Contains(object? value) { var self = this; self.ThrowInvalidOperationIfNotInitialized(); - return self.Contains((T)value); + return self.Contains((T)value!); } /// @@ -1038,11 +1038,11 @@ namespace System.Collections.Immutable /// The index of if found in the list; otherwise, -1. /// [ExcludeFromCodeCoverage] - int IList.IndexOf(object value) + int IList.IndexOf(object? value) { var self = this; self.ThrowInvalidOperationIfNotInitialized(); - return self.IndexOf((T)value); + return self.IndexOf((T)value!); } /// @@ -1052,7 +1052,7 @@ namespace System.Collections.Immutable /// The object to insert into the . /// [ExcludeFromCodeCoverage] - void IList.Insert(int index, object value) + void IList.Insert(int index, object? value) { throw new NotSupportedException(); } @@ -1129,7 +1129,7 @@ namespace System.Collections.Immutable /// The object to remove from the . /// [ExcludeFromCodeCoverage] - void IList.Remove(object value) + void IList.Remove(object? value) { throw new NotSupportedException(); } @@ -1156,7 +1156,7 @@ namespace System.Collections.Immutable /// Always thrown from the setter. /// Thrown if the property returns true. [ExcludeFromCodeCoverage] - object IList.this[int index] + object? IList.this[int index] { get { @@ -1177,7 +1177,7 @@ namespace System.Collections.Immutable { var self = this; self.ThrowInvalidOperationIfNotInitialized(); - Array.Copy(self.array, 0, array, index, self.Length); + Array.Copy(self.array!, 0, array, index, self.Length); } /// @@ -1186,14 +1186,13 @@ namespace System.Collections.Immutable /// The object to compare with the current instance. /// An object that determines whether the current instance and other are equal. /// true if the two objects are equal; otherwise, false. - bool IStructuralEquatable.Equals(object other, IEqualityComparer comparer) + bool IStructuralEquatable.Equals(object? other, IEqualityComparer comparer) { var self = this; - Array otherArray = other as Array; + Array? otherArray = other as Array; if (otherArray == null) { - var theirs = other as IImmutableArray; - if (theirs != null) + if (other is IImmutableArray theirs) { otherArray = theirs.Array; @@ -1208,7 +1207,7 @@ namespace System.Collections.Immutable } } - IStructuralEquatable ours = self.array; + IStructuralEquatable ours = self.array!; return ours.Equals(otherArray, comparer); } @@ -1220,7 +1219,7 @@ namespace System.Collections.Immutable int IStructuralEquatable.GetHashCode(IEqualityComparer comparer) { var self = this; - IStructuralEquatable ours = self.array; + IStructuralEquatable? ours = self.array; return ours != null ? ours.GetHashCode(comparer) : self.GetHashCode(); } @@ -1237,14 +1236,13 @@ namespace System.Collections.Immutable /// An integer that indicates the relationship of the current collection object /// to other. /// - int IStructuralComparable.CompareTo(object other, IComparer comparer) + int IStructuralComparable.CompareTo(object? other, IComparer comparer) { var self = this; - Array otherArray = other as Array; + Array? otherArray = other as Array; if (otherArray == null) { - var theirs = other as IImmutableArray; - if (theirs != null) + if (other is IImmutableArray theirs) { otherArray = theirs.Array; @@ -1261,7 +1259,7 @@ namespace System.Collections.Immutable if (otherArray != null) { - IStructuralComparable ours = self.array; + IStructuralComparable? ours = self.array; if (ours == null) { throw new ArgumentException(SR.ArrayInitializedStateNotEqual, nameof(other)); @@ -1301,13 +1299,13 @@ namespace System.Collections.Immutable { int copyLength = lastIndexRemoved == -1 ? indexToRemove : (indexToRemove - lastIndexRemoved - 1); Debug.Assert(indexToRemove > lastIndexRemoved); // We require that the input be a sorted set. - Array.Copy(self.array, copied + removed, newArray, copied, copyLength); + Array.Copy(self.array!, copied + removed, newArray, copied, copyLength); removed++; copied += copyLength; lastIndexRemoved = indexToRemove; } - Array.Copy(self.array, copied + removed, newArray, copied, self.Length - (copied + removed)); + Array.Copy(self.array!, copied + removed, newArray, copied, self.Length - (copied + removed)); return new ImmutableArray(newArray); } diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableDictionary.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableDictionary.cs index eb7f117..c6d8239 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableDictionary.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableDictionary.cs @@ -22,7 +22,7 @@ namespace System.Collections.Immutable /// The type of values stored by the dictionary. /// The immutable collection. [Pure] - public static ImmutableDictionary Create() + public static ImmutableDictionary Create() where TKey : notnull { return ImmutableDictionary.Empty; } @@ -37,7 +37,7 @@ namespace System.Collections.Immutable /// The immutable collection. /// [Pure] - public static ImmutableDictionary Create(IEqualityComparer keyComparer) + public static ImmutableDictionary Create(IEqualityComparer? keyComparer) where TKey : notnull { return ImmutableDictionary.Empty.WithComparers(keyComparer); } @@ -53,7 +53,7 @@ namespace System.Collections.Immutable /// The immutable collection. /// [Pure] - public static ImmutableDictionary Create(IEqualityComparer keyComparer, IEqualityComparer valueComparer) + public static ImmutableDictionary Create(IEqualityComparer? keyComparer, IEqualityComparer? valueComparer) where TKey : notnull { return ImmutableDictionary.Empty.WithComparers(keyComparer, valueComparer); } @@ -67,7 +67,7 @@ namespace System.Collections.Immutable /// The new immutable collection. [Pure] [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")] - public static ImmutableDictionary CreateRange(IEnumerable> items) + public static ImmutableDictionary CreateRange(IEnumerable> items) where TKey : notnull { return ImmutableDictionary.Empty.AddRange(items); } @@ -82,7 +82,7 @@ namespace System.Collections.Immutable /// The new immutable collection. [Pure] [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")] - public static ImmutableDictionary CreateRange(IEqualityComparer keyComparer, IEnumerable> items) + public static ImmutableDictionary CreateRange(IEqualityComparer? keyComparer, IEnumerable> items) where TKey : notnull { return ImmutableDictionary.Empty.WithComparers(keyComparer).AddRange(items); } @@ -98,7 +98,7 @@ namespace System.Collections.Immutable /// The new immutable collection. [Pure] [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")] - public static ImmutableDictionary CreateRange(IEqualityComparer keyComparer, IEqualityComparer valueComparer, IEnumerable> items) + public static ImmutableDictionary CreateRange(IEqualityComparer? keyComparer, IEqualityComparer? valueComparer, IEnumerable> items) where TKey : notnull { return ImmutableDictionary.Empty.WithComparers(keyComparer, valueComparer).AddRange(items); } @@ -110,7 +110,7 @@ namespace System.Collections.Immutable /// The type of values stored by the dictionary. /// The new builder. [Pure] - public static ImmutableDictionary.Builder CreateBuilder() + public static ImmutableDictionary.Builder CreateBuilder() where TKey : notnull { return Create().ToBuilder(); } @@ -123,7 +123,7 @@ namespace System.Collections.Immutable /// The key comparer. /// The new builder. [Pure] - public static ImmutableDictionary.Builder CreateBuilder(IEqualityComparer keyComparer) + public static ImmutableDictionary.Builder CreateBuilder(IEqualityComparer? keyComparer) where TKey : notnull { return Create(keyComparer).ToBuilder(); } @@ -137,7 +137,7 @@ namespace System.Collections.Immutable /// The value comparer. /// The new builder. [Pure] - public static ImmutableDictionary.Builder CreateBuilder(IEqualityComparer keyComparer, IEqualityComparer valueComparer) + public static ImmutableDictionary.Builder CreateBuilder(IEqualityComparer? keyComparer, IEqualityComparer? valueComparer) where TKey : notnull { return Create(keyComparer, valueComparer).ToBuilder(); } @@ -155,7 +155,7 @@ namespace System.Collections.Immutable /// The value comparer to use for the map. /// The immutable map. [Pure] - public static ImmutableDictionary ToImmutableDictionary(this IEnumerable source, Func keySelector, Func elementSelector, IEqualityComparer keyComparer, IEqualityComparer valueComparer) + public static ImmutableDictionary ToImmutableDictionary(this IEnumerable source, Func keySelector, Func elementSelector, IEqualityComparer? keyComparer, IEqualityComparer? valueComparer) where TKey : notnull { Requires.NotNull(source, nameof(source)); Requires.NotNull(keySelector, nameof(keySelector)); @@ -171,7 +171,7 @@ namespace System.Collections.Immutable /// The builder to create the immutable dictionary from. /// An immutable dictionary. [Pure] - public static ImmutableDictionary ToImmutableDictionary(this ImmutableDictionary.Builder builder) + public static ImmutableDictionary ToImmutableDictionary(this ImmutableDictionary.Builder builder) where TKey : notnull { Requires.NotNull(builder, nameof(builder)); @@ -190,7 +190,7 @@ namespace System.Collections.Immutable /// The key comparer to use for the map. /// The immutable map. [Pure] - public static ImmutableDictionary ToImmutableDictionary(this IEnumerable source, Func keySelector, Func elementSelector, IEqualityComparer keyComparer) + public static ImmutableDictionary ToImmutableDictionary(this IEnumerable source, Func keySelector, Func elementSelector, IEqualityComparer? keyComparer) where TKey : notnull { return ToImmutableDictionary(source, keySelector, elementSelector, keyComparer, null); } @@ -204,7 +204,7 @@ namespace System.Collections.Immutable /// The function that will produce the key for the map from each sequence element. /// The immutable map. [Pure] - public static ImmutableDictionary ToImmutableDictionary(this IEnumerable source, Func keySelector) + public static ImmutableDictionary ToImmutableDictionary(this IEnumerable source, Func keySelector) where TKey : notnull { return ToImmutableDictionary(source, keySelector, v => v, null, null); } @@ -219,7 +219,7 @@ namespace System.Collections.Immutable /// The key comparer to use for the map. /// The immutable map. [Pure] - public static ImmutableDictionary ToImmutableDictionary(this IEnumerable source, Func keySelector, IEqualityComparer keyComparer) + public static ImmutableDictionary ToImmutableDictionary(this IEnumerable source, Func keySelector, IEqualityComparer? keyComparer) where TKey : notnull { return ToImmutableDictionary(source, keySelector, v => v, keyComparer, null); } @@ -235,7 +235,7 @@ namespace System.Collections.Immutable /// The function that will produce the value for the map from each sequence element. /// The immutable map. [Pure] - public static ImmutableDictionary ToImmutableDictionary(this IEnumerable source, Func keySelector, Func elementSelector) + public static ImmutableDictionary ToImmutableDictionary(this IEnumerable source, Func keySelector, Func elementSelector) where TKey : notnull { return ToImmutableDictionary(source, keySelector, elementSelector, null, null); } @@ -251,7 +251,7 @@ namespace System.Collections.Immutable /// An immutable map. [Pure] [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")] - public static ImmutableDictionary ToImmutableDictionary(this IEnumerable> source, IEqualityComparer keyComparer, IEqualityComparer valueComparer) + public static ImmutableDictionary ToImmutableDictionary(this IEnumerable> source, IEqualityComparer? keyComparer, IEqualityComparer? valueComparer) where TKey : notnull { Requires.NotNull(source, nameof(source)); @@ -274,7 +274,7 @@ namespace System.Collections.Immutable /// An immutable map. [Pure] [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")] - public static ImmutableDictionary ToImmutableDictionary(this IEnumerable> source, IEqualityComparer keyComparer) + public static ImmutableDictionary ToImmutableDictionary(this IEnumerable> source, IEqualityComparer? keyComparer) where TKey : notnull { return ToImmutableDictionary(source, keyComparer, null); } @@ -288,7 +288,7 @@ namespace System.Collections.Immutable /// An immutable map. [Pure] [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")] - public static ImmutableDictionary ToImmutableDictionary(this IEnumerable> source) + public static ImmutableDictionary ToImmutableDictionary(this IEnumerable> source) where TKey : notnull { return ToImmutableDictionary(source, null, null); } @@ -305,7 +305,7 @@ namespace System.Collections.Immutable /// true if this map contains the key-value pair; otherwise, false. /// [Pure] - public static bool Contains(this IImmutableDictionary map, TKey key, TValue value) + public static bool Contains(this IImmutableDictionary map, TKey key, TValue value) where TKey : notnull { Requires.NotNull(map, nameof(map)); Requires.NotNullAllowStructs(key, nameof(key)); @@ -319,9 +319,10 @@ namespace System.Collections.Immutable /// The key to search for. /// The value for the key, or the default value of type if no matching key was found. [Pure] - public static TValue GetValueOrDefault(this IImmutableDictionary dictionary, TKey key) + [return: MaybeNull] + public static TValue GetValueOrDefault(this IImmutableDictionary dictionary, TKey key) where TKey : notnull { - return GetValueOrDefault(dictionary, key, default(TValue)); + return GetValueOrDefault(dictionary, key, default(TValue)!); } /// @@ -336,13 +337,13 @@ namespace System.Collections.Immutable /// The value for the key, or if no matching key was found. /// [Pure] - public static TValue GetValueOrDefault(this IImmutableDictionary dictionary, TKey key, TValue defaultValue) + public static TValue GetValueOrDefault(this IImmutableDictionary dictionary, TKey key, TValue defaultValue) where TKey : notnull { Requires.NotNull(dictionary, nameof(dictionary)); Requires.NotNullAllowStructs(key, nameof(key)); TValue value; - if (dictionary.TryGetValue(key, out value)) + if (dictionary.TryGetValue(key, out value!)) { return value; } diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableDictionary_2.Builder.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableDictionary_2.Builder.cs index 8bdbde1..11717ea 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableDictionary_2.Builder.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableDictionary_2.Builder.cs @@ -54,7 +54,7 @@ namespace System.Collections.Immutable /// Caches an immutable instance that represents the current state of the collection. /// /// Null if no immutable view has been created for the current version. - private ImmutableDictionary _immutable; + private ImmutableDictionary? _immutable; /// /// A number that increments every time the builder changes its contents. @@ -64,7 +64,7 @@ namespace System.Collections.Immutable /// /// The object callers may use to synchronize access to this collection. /// - private object _syncRoot; + private object? _syncRoot; /// /// Initializes a new instance of the class. @@ -262,7 +262,7 @@ namespace System.Collections.Immutable { if (_syncRoot == null) { - Threading.Interlocked.CompareExchange(ref _syncRoot, new object(), null); + Threading.Interlocked.CompareExchange(ref _syncRoot, new object(), null); } return _syncRoot; @@ -288,9 +288,9 @@ namespace System.Collections.Immutable /// /// The to use as the key of the element to add. /// The to use as the value of the element to add. - void IDictionary.Add(object key, object value) + void IDictionary.Add(object key, object? value) { - this.Add((TKey)key, (TValue)value); + this.Add((TKey)key, (TValue)value!); } /// @@ -330,10 +330,10 @@ namespace System.Collections.Immutable /// /// The key. /// - object IDictionary.this[object key] + object? IDictionary.this[object key] { get { return this[(TKey)key]; } - set { this[(TKey)key] = (TValue)value; } + set { this[(TKey)key] = (TValue)value!; } } #endregion @@ -414,7 +414,7 @@ namespace System.Collections.Immutable get { TValue value; - if (this.TryGetValue(key, out value)) + if (this.TryGetValue(key, out value!)) { return value; } @@ -473,9 +473,10 @@ namespace System.Collections.Immutable /// The key to search for. /// The value for the key, or the default value of type if no matching key was found. [Pure] + [return: MaybeNull] public TValue GetValueOrDefault(TKey key) { - return this.GetValueOrDefault(key, default(TValue)); + return this.GetValueOrDefault(key, default(TValue)!); } /// @@ -492,7 +493,7 @@ namespace System.Collections.Immutable Requires.NotNullAllowStructs(key, nameof(key)); TValue value; - if (this.TryGetValue(key, out value)) + if (this.TryGetValue(key, out value!)) { return value; } @@ -602,9 +603,9 @@ namespace System.Collections.Immutable /// true if the object that implements contains an element with the specified key; otherwise, false. /// /// is null. - public bool TryGetValue(TKey key, out TValue value) + public bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value) { - return ImmutableDictionary.TryGetValue(key, this.Origin, out value); + return ImmutableDictionary.TryGetValue(key, this.Origin, out value!); } /// @@ -727,7 +728,7 @@ namespace System.Collections.Immutable /// /// A simple view of the immutable collection that the debugger can show to the developer. /// - internal class ImmutableDictionaryBuilderDebuggerProxy + internal class ImmutableDictionaryBuilderDebuggerProxy where TKey : notnull { /// /// The collection to be enumerated. @@ -737,7 +738,7 @@ namespace System.Collections.Immutable /// /// The simple view of the collection. /// - private KeyValuePair[] _contents; + private KeyValuePair[]? _contents; /// /// Initializes a new instance of the class. diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableDictionary_2.Enumerator.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableDictionary_2.Enumerator.cs index 8ebdcbc..b73d2a8 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableDictionary_2.Enumerator.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableDictionary_2.Enumerator.cs @@ -19,7 +19,7 @@ namespace System.Collections.Immutable /// /// The builder being enumerated, if applicable. /// - private readonly Builder _builder; + private readonly Builder? _builder; /// /// The enumerator over the sorted dictionary whose keys are hash values. @@ -41,7 +41,7 @@ namespace System.Collections.Immutable /// /// The root. /// The builder, if applicable. - internal Enumerator(SortedInt32KeyNode root, Builder builder = null) + internal Enumerator(SortedInt32KeyNode root, Builder? builder = null) { _builder = builder; _mapEnumerator = new SortedInt32KeyNode.Enumerator(root); diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableDictionary_2.HashBucket.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableDictionary_2.HashBucket.cs index e5603ec..9e7a1dc 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableDictionary_2.HashBucket.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableDictionary_2.HashBucket.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Globalization; namespace System.Collections.Immutable @@ -36,7 +37,7 @@ namespace System.Collections.Immutable /// /// The first element. /// The additional elements. - private HashBucket(KeyValuePair firstElement, ImmutableList>.Node additionalElements = null) + private HashBucket(KeyValuePair firstElement, ImmutableList>.Node? additionalElements = null) { _firstValue = firstElement; _additionalElements = additionalElements ?? ImmutableList>.Node.EmptyNode; @@ -110,7 +111,7 @@ namespace System.Collections.Immutable /// /// Throws an exception to catch any errors in comparing instances. /// - public override bool Equals(object obj) + public override bool Equals(object? obj) { // This should never be called, as hash buckets don't know how to equate themselves. throw new NotSupportedException(); @@ -221,7 +222,7 @@ namespace System.Collections.Immutable return this; } - var kv = new KeyValuePair(key, default(TValue)); + var kv = new KeyValuePair(key, default(TValue)!); if (keyOnlyComparer.Equals(_firstValue, kv)) { if (_additionalElements.IsEmpty) @@ -233,7 +234,7 @@ namespace System.Collections.Immutable { // We can promote any element from the list into the first position, but it's most efficient // to remove the root node in the binary tree that implements the list. - int indexOfRootNode = _additionalElements.Left.Count; + int indexOfRootNode = _additionalElements.Left!.Count; result = OperationResult.SizeChanged; return new HashBucket(_additionalElements.Key, _additionalElements.RemoveAt(indexOfRootNode)); } @@ -259,11 +260,11 @@ namespace System.Collections.Immutable /// The comparers. /// The value for the given key. /// A value indicating whether the key was found. - internal bool TryGetValue(TKey key, Comparers comparers, out TValue value) + internal bool TryGetValue(TKey key, Comparers comparers, [MaybeNullWhen(false)] out TValue value) { if (this.IsEmpty) { - value = default(TValue); + value = default(TValue)!; return false; } @@ -273,11 +274,11 @@ namespace System.Collections.Immutable return true; } - var kv = new KeyValuePair(key, default(TValue)); + var kv = new KeyValuePair(key, default(TValue)!); var index = _additionalElements.IndexOf(kv, comparers.KeyOnlyComparer); if (index < 0) { - value = default(TValue); + value = default(TValue)!; return false; } @@ -316,7 +317,7 @@ namespace System.Collections.Immutable return true; } - var kv = new KeyValuePair(equalKey, default(TValue)); + var kv = new KeyValuePair(equalKey, default(TValue)!); var index = _additionalElements.IndexOf(kv, comparers.KeyOnlyComparer); if (index < 0) { diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableDictionary_2.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableDictionary_2.cs index 4432350..d921380 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableDictionary_2.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableDictionary_2.cs @@ -17,7 +17,7 @@ namespace System.Collections.Immutable /// The type of the value. [DebuggerDisplay("Count = {Count}")] [DebuggerTypeProxy(typeof(ImmutableDictionaryDebuggerProxy<,>))] - public sealed partial class ImmutableDictionary : IImmutableDictionary, IImmutableDictionaryInternal, IHashKeyCollection, IDictionary, IDictionary + public sealed partial class ImmutableDictionary : IImmutableDictionary, IImmutableDictionaryInternal, IHashKeyCollection, IDictionary, IDictionary where TKey: notnull { /// /// An empty immutable dictionary with default equality comparers. @@ -65,7 +65,7 @@ namespace System.Collections.Immutable /// Initializes a new instance of the class. /// /// The comparers. - private ImmutableDictionary(Comparers comparers = null) + private ImmutableDictionary(Comparers? comparers = null) { _comparers = comparers ?? Comparers.Get(EqualityComparer.Default, EqualityComparer.Default); _root = SortedInt32KeyNode.EmptyNode; @@ -250,7 +250,7 @@ namespace System.Collections.Immutable Requires.NotNullAllowStructs(key, nameof(key)); TValue value; - if (this.TryGetValue(key, out value)) + if (this.TryGetValue(key, out value!)) { return value; } @@ -417,10 +417,10 @@ namespace System.Collections.Immutable /// /// See the interface. /// - public bool TryGetValue(TKey key, out TValue value) + public bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value) { Requires.NotNullAllowStructs(key, nameof(key)); - return TryGetValue(key, this.Origin, out value); + return TryGetValue(key, this.Origin, out value!); } /// @@ -436,7 +436,7 @@ namespace System.Collections.Immutable /// See the interface. /// [Pure] - public ImmutableDictionary WithComparers(IEqualityComparer keyComparer, IEqualityComparer valueComparer) + public ImmutableDictionary WithComparers(IEqualityComparer? keyComparer, IEqualityComparer? valueComparer) { if (keyComparer == null) { @@ -476,7 +476,7 @@ namespace System.Collections.Immutable /// See the interface. /// [Pure] - public ImmutableDictionary WithComparers(IEqualityComparer keyComparer) + public ImmutableDictionary WithComparers(IEqualityComparer? keyComparer) { return this.WithComparers(keyComparer, _comparers.ValueComparer); } @@ -708,7 +708,7 @@ namespace System.Collections.Immutable /// /// The to use as the key of the element to add. /// The to use as the value of the element to add. - void IDictionary.Add(object key, object value) + void IDictionary.Add(object key, object? value) { throw new NotSupportedException(); } @@ -750,7 +750,7 @@ namespace System.Collections.Immutable /// /// The key. /// - object IDictionary.this[object key] + object? IDictionary.this[object key] { get { return this[(TKey)key]; } set { throw new NotSupportedException(); } @@ -871,7 +871,7 @@ namespace System.Collections.Immutable /// The sequence that may have come from an immutable map. /// Receives the concrete typed value if one can be found. /// true if the cast was successful; false otherwise. - private static bool TryCastToImmutableMap(IEnumerable> sequence, out ImmutableDictionary other) + private static bool TryCastToImmutableMap(IEnumerable> sequence, [NotNullWhen(true)] out ImmutableDictionary? other) { other = sequence as ImmutableDictionary; if (other != null) @@ -879,8 +879,7 @@ namespace System.Collections.Immutable return true; } - var builder = sequence as Builder; - if (builder != null) + if (sequence is Builder builder) { other = builder.ToImmutable(); return true; @@ -901,7 +900,7 @@ namespace System.Collections.Immutable if (origin.Root.TryGetValue(hashCode, out bucket)) { TValue value; - return bucket.TryGetValue(key, origin.Comparers, out value); + return bucket.TryGetValue(key, origin.Comparers, out value!); } return false; @@ -917,7 +916,7 @@ namespace System.Collections.Immutable if (origin.Root.TryGetValue(hashCode, out bucket)) { TValue value; - return bucket.TryGetValue(keyValuePair.Key, origin.Comparers, out value) + return bucket.TryGetValue(keyValuePair.Key, origin.Comparers, out value!) && origin.ValueComparer.Equals(value, keyValuePair.Value); } @@ -927,16 +926,16 @@ namespace System.Collections.Immutable /// /// Performs the operation on a given data structure. /// - private static bool TryGetValue(TKey key, MutationInput origin, out TValue value) + private static bool TryGetValue(TKey key, MutationInput origin, [MaybeNullWhen(false)] out TValue value) { int hashCode = origin.KeyComparer.GetHashCode(key); HashBucket bucket; if (origin.Root.TryGetValue(hashCode, out bucket)) { - return bucket.TryGetValue(key, origin.Comparers, out value); + return bucket.TryGetValue(key, origin.Comparers, out value!); } - value = default(TValue); + value = default(TValue)!; return false; } @@ -1060,7 +1059,7 @@ namespace System.Collections.Immutable /// The root of the data structure. /// The adjusted count if the root has changed. /// The immutable collection. - private ImmutableDictionary Wrap(SortedInt32KeyNode root, int adjustedCountIfDifferentRoot) + private ImmutableDictionary Wrap(SortedInt32KeyNode? root, int adjustedCountIfDifferentRoot) { if (root == null) { @@ -1090,8 +1089,7 @@ namespace System.Collections.Immutable { // If the items being added actually come from an ImmutableDictionary // then there is no value in reconstructing it. - ImmutableDictionary other; - if (TryCastToImmutableMap(pairs, out other)) + if (TryCastToImmutableMap(pairs, out ImmutableDictionary? other)) { return other.WithComparers(this.KeyComparer, this.ValueComparer); } diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableEnumerableDebuggerProxy.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableEnumerableDebuggerProxy.cs index ccfccde..a1eaa52 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableEnumerableDebuggerProxy.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableEnumerableDebuggerProxy.cs @@ -13,7 +13,7 @@ namespace System.Collections.Immutable /// /// The type of the dictionary's keys. /// The type of the dictionary's values. - internal class ImmutableDictionaryDebuggerProxy : ImmutableEnumerableDebuggerProxy> + internal class ImmutableDictionaryDebuggerProxy : ImmutableEnumerableDebuggerProxy> where TKey : notnull { /// /// Initializes a new instance of the class. @@ -43,7 +43,7 @@ namespace System.Collections.Immutable /// /// The contents of the enumerable, cached into an array. /// - private T[] _cachedContents; + private T[]? _cachedContents; /// /// Initializes a new instance of the class. diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableExtensions.Minimal.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableExtensions.Minimal.cs index e20d413..94fbc30 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableExtensions.Minimal.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableExtensions.Minimal.cs @@ -33,22 +33,19 @@ namespace System.Collections.Immutable /// true if the count could be determined; false otherwise. internal static bool TryGetCount(this IEnumerable sequence, out int count) { - var collection = sequence as ICollection; - if (collection != null) + if (sequence is ICollection collection) { count = collection.Count; return true; } - var collectionOfT = sequence as ICollection; - if (collectionOfT != null) + if (sequence is ICollection collectionOfT) { count = collectionOfT.Count; return true; } - var readOnlyCollection = sequence as IReadOnlyCollection; - if (readOnlyCollection != null) + if (sequence is IReadOnlyCollection readOnlyCollection) { count = readOnlyCollection.Count; return true; @@ -107,11 +104,9 @@ namespace System.Collections.Immutable Debug.Assert(arrayIndex >= 0 && arrayIndex <= array.Length); // IList is the GCD of what the following types implement. - var listInterface = sequence as IList; - if (listInterface != null) + if (sequence is IList listInterface) { - var list = sequence as List; - if (list != null) + if (sequence is List list) { list.CopyTo(array, arrayIndex); return true; @@ -127,10 +122,9 @@ namespace System.Collections.Immutable return true; } - if (sequence is ImmutableArray) + if (sequence is ImmutableArray immutable) { - var immutable = (ImmutableArray)sequence; - Array.Copy(immutable.array, 0, array, arrayIndex, immutable.Length); + Array.Copy(immutable.array!, 0, array, arrayIndex, immutable.Length); return true; } } @@ -157,7 +151,7 @@ namespace System.Collections.Immutable if (count == 0) { - return ImmutableArray.Empty.array; + return ImmutableArray.Empty.array!; } T[] array = new T[count]; diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableExtensions.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableExtensions.cs index f448161..c5b0439 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableExtensions.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableExtensions.cs @@ -17,7 +17,7 @@ namespace System.Collections.Immutable { internal static bool IsValueType() { - if (default(T) != null) + if (default(T)! != null) { return true; } @@ -300,7 +300,7 @@ namespace System.Collections.Immutable /// /// The list-ified sequence. /// - private IList _collection; + private IList? _collection; /// /// Initializes a new instance of the class. diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableHashSet.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableHashSet.cs index b8ab82d..565e665 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableHashSet.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableHashSet.cs @@ -32,7 +32,7 @@ namespace System.Collections.Immutable /// The immutable collection. /// [Pure] - public static ImmutableHashSet Create(IEqualityComparer equalityComparer) + public static ImmutableHashSet Create(IEqualityComparer? equalityComparer) { return ImmutableHashSet.Empty.WithComparer(equalityComparer); } @@ -57,7 +57,7 @@ namespace System.Collections.Immutable /// The item to prepopulate. /// The new immutable collection. [Pure] - public static ImmutableHashSet Create(IEqualityComparer equalityComparer, T item) + public static ImmutableHashSet Create(IEqualityComparer? equalityComparer, T item) { return ImmutableHashSet.Empty.WithComparer(equalityComparer).Add(item); } @@ -82,7 +82,7 @@ namespace System.Collections.Immutable /// The items to prepopulate. /// The new immutable collection. [Pure] - public static ImmutableHashSet CreateRange(IEqualityComparer equalityComparer, IEnumerable items) + public static ImmutableHashSet CreateRange(IEqualityComparer? equalityComparer, IEnumerable items) { return ImmutableHashSet.Empty.WithComparer(equalityComparer).Union(items); } @@ -107,7 +107,7 @@ namespace System.Collections.Immutable /// The items to prepopulate. /// The new immutable collection. [Pure] - public static ImmutableHashSet Create(IEqualityComparer equalityComparer, params T[] items) + public static ImmutableHashSet Create(IEqualityComparer? equalityComparer, params T[] items) { return ImmutableHashSet.Empty.WithComparer(equalityComparer).Union(items); } @@ -132,7 +132,7 @@ namespace System.Collections.Immutable /// The immutable collection. /// [Pure] - public static ImmutableHashSet.Builder CreateBuilder(IEqualityComparer equalityComparer) + public static ImmutableHashSet.Builder CreateBuilder(IEqualityComparer? equalityComparer) { return Create(equalityComparer).ToBuilder(); } @@ -145,10 +145,9 @@ namespace System.Collections.Immutable /// The equality comparer to use for initializing and adding members to the hash set. /// An immutable set. [Pure] - public static ImmutableHashSet ToImmutableHashSet(this IEnumerable source, IEqualityComparer equalityComparer) + public static ImmutableHashSet ToImmutableHashSet(this IEnumerable source, IEqualityComparer? equalityComparer) { - var existingSet = source as ImmutableHashSet; - if (existingSet != null) + if (source is ImmutableHashSet existingSet) { return existingSet.WithComparer(equalityComparer); } diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableHashSet_1.Builder.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableHashSet_1.Builder.cs index 7e73f47..589a3e0 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableHashSet_1.Builder.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableHashSet_1.Builder.cs @@ -57,7 +57,7 @@ namespace System.Collections.Immutable /// Caches an immutable instance that represents the current state of the collection. /// /// Null if no immutable view has been created for the current version. - private ImmutableHashSet _immutable; + private ImmutableHashSet? _immutable; /// /// A number that increments every time the builder changes its contents. diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableHashSet_1.Enumerator.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableHashSet_1.Enumerator.cs index 255048b..ecc47ef 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableHashSet_1.Enumerator.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableHashSet_1.Enumerator.cs @@ -19,7 +19,7 @@ namespace System.Collections.Immutable /// /// The builder being enumerated, if applicable. /// - private readonly Builder _builder; + private readonly Builder? _builder; /// /// The enumerator over the sorted dictionary whose keys are hash values. @@ -41,7 +41,7 @@ namespace System.Collections.Immutable /// /// The root. /// The builder, if applicable. - internal Enumerator(SortedInt32KeyNode root, Builder builder = null) + internal Enumerator(SortedInt32KeyNode root, Builder? builder = null) { _builder = builder; _mapEnumerator = new SortedInt32KeyNode.Enumerator(root); @@ -64,7 +64,7 @@ namespace System.Collections.Immutable /// /// Gets the current element. /// - object IEnumerator.Current + object? IEnumerator.Current { get { return this.Current; } } diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableHashSet_1.HashBucket.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableHashSet_1.HashBucket.cs index d4dd830..4c94ed2 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableHashSet_1.HashBucket.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableHashSet_1.HashBucket.cs @@ -52,7 +52,7 @@ namespace System.Collections.Immutable /// /// The first element. /// The additional elements. - private HashBucket(T firstElement, ImmutableList.Node additionalElements = null) + private HashBucket(T firstElement, ImmutableList.Node? additionalElements = null) { _firstValue = firstElement; _additionalElements = additionalElements ?? ImmutableList.Node.EmptyNode; @@ -80,7 +80,7 @@ namespace System.Collections.Immutable /// /// Throws an exception to catch any errors in comparing instances. /// - public override bool Equals(object obj) + public override bool Equals(object? obj) { // This should never be called, as hash buckets don't know how to equate themselves. throw new NotSupportedException(); @@ -221,7 +221,7 @@ namespace System.Collections.Immutable { // We can promote any element from the list into the first position, but it's most efficient // to remove the root node in the binary tree that implements the list. - int indexOfRootNode = _additionalElements.Left.Count; + int indexOfRootNode = _additionalElements.Left!.Count; result = OperationResult.SizeChanged; return new HashBucket(_additionalElements.Key, _additionalElements.RemoveAt(indexOfRootNode)); } @@ -317,7 +317,7 @@ namespace System.Collections.Immutable /// /// Gets the current element. /// - object IEnumerator.Current + object? IEnumerator.Current { get { return this.Current; } } diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableHashSet_1.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableHashSet_1.cs index 2668068..8fc536a 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableHashSet_1.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableHashSet_1.cs @@ -435,7 +435,7 @@ namespace System.Collections.Immutable /// See the interface. /// [Pure] - public ImmutableHashSet WithComparer(IEqualityComparer equalityComparer) + public ImmutableHashSet WithComparer(IEqualityComparer? equalityComparer) { if (equalityComparer == null) { diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableInterlocked.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableInterlocked.cs index d668b29..3697c56 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableInterlocked.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableInterlocked.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Threading; namespace System.Collections.Immutable @@ -121,7 +122,7 @@ namespace System.Collections.Immutable Requires.NotNull(transformer, nameof(transformer)); bool successful; - T[] oldArray = Volatile.Read(ref location.array); + T[]? oldArray = Volatile.Read(ref location.array); do { ImmutableArray newImmutableArray = transformer(new ImmutableArray(oldArray)); @@ -131,7 +132,7 @@ namespace System.Collections.Immutable return false; } - T[] interlockedResult = Interlocked.CompareExchange(ref location.array, newImmutableArray.array, oldArray); + T[]? interlockedResult = Interlocked.CompareExchange(ref location.array, newImmutableArray.array, oldArray); successful = ReferenceEquals(oldArray, interlockedResult); oldArray = interlockedResult; // we already have a volatile read that we can reuse for the next loop } @@ -165,7 +166,7 @@ namespace System.Collections.Immutable Requires.NotNull(transformer, nameof(transformer)); bool successful; - T[] oldArray = Volatile.Read(ref location.array); + T[]? oldArray = Volatile.Read(ref location.array); do { ImmutableArray newImmutableArray = transformer(new ImmutableArray(oldArray), transformerArgument); @@ -175,7 +176,7 @@ namespace System.Collections.Immutable return false; } - T[] interlockedResult = Interlocked.CompareExchange(ref location.array, newImmutableArray.array, oldArray); + T[]? interlockedResult = Interlocked.CompareExchange(ref location.array, newImmutableArray.array, oldArray); successful = ReferenceEquals(oldArray, interlockedResult); oldArray = interlockedResult; // we already have a volatile read that we can reuse for the next loop } @@ -240,7 +241,7 @@ namespace System.Collections.Immutable /// The function to execute to obtain the value to insert into the dictionary if the key is not found. /// The argument to pass to the value factory. /// The value obtained from the dictionary or if it was not present. - public static TValue GetOrAdd(ref ImmutableDictionary location, TKey key, Func valueFactory, TArg factoryArgument) + public static TValue GetOrAdd(ref ImmutableDictionary location, TKey key, Func valueFactory, TArg factoryArgument) where TKey : notnull { Requires.NotNull(valueFactory, nameof(valueFactory)); @@ -248,7 +249,7 @@ namespace System.Collections.Immutable Requires.NotNull(map, nameof(location)); TValue value; - if (map.TryGetValue(key, out value)) + if (map.TryGetValue(key, out value!)) { return value; } @@ -269,7 +270,7 @@ namespace System.Collections.Immutable /// This delegate will not be invoked more than once. /// /// The value obtained from the dictionary or if it was not present. - public static TValue GetOrAdd(ref ImmutableDictionary location, TKey key, Func valueFactory) + public static TValue GetOrAdd(ref ImmutableDictionary location, TKey key, Func valueFactory) where TKey : notnull { Requires.NotNull(valueFactory, nameof(valueFactory)); @@ -277,7 +278,7 @@ namespace System.Collections.Immutable Requires.NotNull(map, nameof(location)); TValue value; - if (map.TryGetValue(key, out value)) + if (map.TryGetValue(key, out value!)) { return value; } @@ -295,7 +296,7 @@ namespace System.Collections.Immutable /// The key for the value to retrieve or add. /// The value to add to the dictionary if one is not already present. /// The value obtained from the dictionary or if it was not present. - public static TValue GetOrAdd(ref ImmutableDictionary location, TKey key, TValue value) + public static TValue GetOrAdd(ref ImmutableDictionary location, TKey key, TValue value) where TKey : notnull { var priorCollection = Volatile.Read(ref location); bool successful; @@ -303,7 +304,7 @@ namespace System.Collections.Immutable { Requires.NotNull(priorCollection, nameof(location)); TValue oldValue; - if (priorCollection.TryGetValue(key, out oldValue)) + if (priorCollection.TryGetValue(key, out oldValue!)) { return oldValue; } @@ -330,7 +331,7 @@ namespace System.Collections.Immutable /// The function that receives the key and returns a new value to add to the dictionary when no value previously exists. /// The function that receives the key and prior value and returns the new value with which to update the dictionary. /// The added or updated value. - public static TValue AddOrUpdate(ref ImmutableDictionary location, TKey key, Func addValueFactory, Func updateValueFactory) + public static TValue AddOrUpdate(ref ImmutableDictionary location, TKey key, Func addValueFactory, Func updateValueFactory) where TKey : notnull { Requires.NotNull(addValueFactory, nameof(addValueFactory)); Requires.NotNull(updateValueFactory, nameof(updateValueFactory)); @@ -343,7 +344,7 @@ namespace System.Collections.Immutable Requires.NotNull(priorCollection, nameof(location)); TValue oldValue; - if (priorCollection.TryGetValue(key, out oldValue)) + if (priorCollection.TryGetValue(key, out oldValue!)) { newValue = updateValueFactory(key, oldValue); } @@ -374,7 +375,7 @@ namespace System.Collections.Immutable /// The value to use if no previous value exists. /// The function that receives the key and prior value and returns the new value with which to update the dictionary. /// The added or updated value. - public static TValue AddOrUpdate(ref ImmutableDictionary location, TKey key, TValue addValue, Func updateValueFactory) + public static TValue AddOrUpdate(ref ImmutableDictionary location, TKey key, TValue addValue, Func updateValueFactory) where TKey : notnull { Requires.NotNull(updateValueFactory, nameof(updateValueFactory)); @@ -386,7 +387,7 @@ namespace System.Collections.Immutable Requires.NotNull(priorCollection, nameof(location)); TValue oldValue; - if (priorCollection.TryGetValue(key, out oldValue)) + if (priorCollection.TryGetValue(key, out oldValue!)) { newValue = updateValueFactory(key, oldValue); } @@ -416,7 +417,7 @@ namespace System.Collections.Immutable /// The key to add, if is not already defined in the dictionary. /// The value to add. /// true if the key was not previously set in the dictionary and the value was set; false otherwise. - public static bool TryAdd(ref ImmutableDictionary location, TKey key, TValue value) + public static bool TryAdd(ref ImmutableDictionary location, TKey key, TValue value) where TKey : notnull { var priorCollection = Volatile.Read(ref location); bool successful; @@ -448,7 +449,7 @@ namespace System.Collections.Immutable /// The new value to set. /// The value that must already be set in the dictionary in order for the update to succeed. /// true if the key and comparison value were present in the dictionary and the update was made; false otherwise. - public static bool TryUpdate(ref ImmutableDictionary location, TKey key, TValue newValue, TValue comparisonValue) + public static bool TryUpdate(ref ImmutableDictionary location, TKey key, TValue newValue, TValue comparisonValue) where TKey : notnull { var valueComparer = EqualityComparer.Default; var priorCollection = Volatile.Read(ref location); @@ -458,7 +459,7 @@ namespace System.Collections.Immutable Requires.NotNull(priorCollection, nameof(location)); TValue priorValue; - if (!priorCollection.TryGetValue(key, out priorValue) || !valueComparer.Equals(priorValue, comparisonValue)) + if (!priorCollection.TryGetValue(key, out priorValue!) || !valueComparer.Equals(priorValue, comparisonValue)) { // The key isn't in the dictionary, or its current value doesn't match what the caller expected. return false; @@ -482,7 +483,7 @@ namespace System.Collections.Immutable /// The key to remove. /// Receives the value from the pre-existing entry, if one exists. /// true if the key was found and removed; false otherwise. - public static bool TryRemove(ref ImmutableDictionary location, TKey key, out TValue value) + public static bool TryRemove(ref ImmutableDictionary location, TKey key, [MaybeNullWhen(false)] out TValue value) where TKey : notnull { var priorCollection = Volatile.Read(ref location); bool successful; @@ -490,7 +491,7 @@ namespace System.Collections.Immutable { Requires.NotNull(priorCollection, nameof(location)); - if (!priorCollection.TryGetValue(key, out value)) + if (!priorCollection.TryGetValue(key, out value!)) { return false; } @@ -515,7 +516,7 @@ namespace System.Collections.Immutable /// The variable or field to atomically update. /// The value popped from the stack, if it was non-empty. /// true if an element was removed from the stack; false otherwise. - public static bool TryPop(ref ImmutableStack location, out T value) + public static bool TryPop(ref ImmutableStack location, [MaybeNullWhen(false)] out T value) { var priorCollection = Volatile.Read(ref location); bool successful; @@ -525,7 +526,7 @@ namespace System.Collections.Immutable if (priorCollection.IsEmpty) { - value = default(T); + value = default(T)!; return false; } @@ -570,7 +571,7 @@ namespace System.Collections.Immutable /// The variable or field to atomically update. /// Receives the value from the head of the queue, if the queue is non-empty. /// true if the queue was not empty and the head element was removed; false otherwise. - public static bool TryDequeue(ref ImmutableQueue location, out T value) + public static bool TryDequeue(ref ImmutableQueue location, [MaybeNullWhen(false)] out T value) { var priorCollection = Volatile.Read(ref location); bool successful; @@ -580,7 +581,7 @@ namespace System.Collections.Immutable if (priorCollection.IsEmpty) { - value = default(T); + value = default(T)!; return false; } diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableList.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableList.cs index 32dfa71..1025f47 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableList.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableList.cs @@ -166,7 +166,7 @@ namespace System.Collections.Immutable /// to the last element, if found; otherwise, -1. /// [Pure] - public static int IndexOf(this IImmutableList list, T item, IEqualityComparer equalityComparer) + public static int IndexOf(this IImmutableList list, T item, IEqualityComparer? equalityComparer) { Requires.NotNull(list, nameof(list)); return list.IndexOf(item, 0, list.Count, equalityComparer); @@ -269,7 +269,7 @@ namespace System.Collections.Immutable /// , if found; otherwise, -1. /// [Pure] - public static int LastIndexOf(this IImmutableList list, T item, IEqualityComparer equalityComparer) + public static int LastIndexOf(this IImmutableList list, T item, IEqualityComparer? equalityComparer) { Requires.NotNull(list, nameof(list)); diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableList_1.Builder.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableList_1.Builder.cs index 460fc0a..bc64eb2 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableList_1.Builder.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableList_1.Builder.cs @@ -44,7 +44,7 @@ namespace System.Collections.Immutable /// Caches an immutable instance that represents the current state of the collection. /// /// Null if no immutable view has been created for the current version. - private ImmutableList _immutable; + private ImmutableList? _immutable; /// /// A number that increments every time the builder changes its contents. @@ -54,7 +54,7 @@ namespace System.Collections.Immutable /// /// The object callers may use to synchronize access to this collection. /// - private object _syncRoot; + private object? _syncRoot; /// /// Initializes a new instance of the class. @@ -401,6 +401,7 @@ namespace System.Collections.Immutable /// The first element that matches the conditions defined by the specified predicate, /// if found; otherwise, the default value for type T. /// + [return: MaybeNull] public T Find(Predicate match) => _root.Find(match); /// @@ -474,6 +475,7 @@ namespace System.Collections.Immutable /// The last element that matches the conditions defined by the specified predicate, /// if found; otherwise, the default value for type T. /// + [return: MaybeNull] public T FindLast(Predicate match) => _root.FindLast(match); /// @@ -597,7 +599,7 @@ namespace System.Collections.Immutable /// contains count number of elements, if found; otherwise, -1. /// [Pure] - public int IndexOf(T item, int index, int count, IEqualityComparer equalityComparer) => + public int IndexOf(T item, int index, int count, IEqualityComparer? equalityComparer) => _root.IndexOf(item, index, count, equalityComparer); /// @@ -693,7 +695,7 @@ namespace System.Collections.Immutable /// and ends at index, if found; otherwise, -1. /// [Pure] - public int LastIndexOf(T item, int startIndex, int count, IEqualityComparer equalityComparer) => + public int LastIndexOf(T item, int startIndex, int count, IEqualityComparer? equalityComparer) => _root.LastIndexOf(item, startIndex, count, equalityComparer); /// @@ -823,7 +825,7 @@ namespace System.Collections.Immutable /// The implementation to use when comparing /// elements, or null to use . /// - public void Sort(IComparer comparer) + public void Sort(IComparer? comparer) { this.Root = this.Root.Sort(comparer); } @@ -842,7 +844,7 @@ namespace System.Collections.Immutable /// The implementation to use when comparing /// elements, or null to use . /// - public void Sort(int index, int count, IComparer comparer) + public void Sort(int index, int count, IComparer? comparer) { Requires.Range(index >= 0, nameof(index)); Requires.Range(count >= 0, nameof(count)); @@ -891,7 +893,7 @@ namespace System.Collections.Immutable /// cannot find an implementation of the System.IComparable<T> generic interface /// or the System.IComparable interface for type T. /// - public int BinarySearch(T item, IComparer comparer) + public int BinarySearch(T item, IComparer? comparer) { return this.BinarySearch(0, this.Count, item, comparer); } @@ -925,7 +927,7 @@ namespace System.Collections.Immutable /// cannot find an implementation of the System.IComparable<T> generic interface /// or the System.IComparable interface for type T. /// - public int BinarySearch(int index, int count, T item, IComparer comparer) + public int BinarySearch(int index, int count, T item, IComparer? comparer) { return this.Root.BinarySearch(index, count, item, comparer); } @@ -962,9 +964,9 @@ namespace System.Collections.Immutable /// /// The position into which the new element was inserted, or -1 to indicate that the item was not inserted into the collection, /// - int IList.Add(object value) + int IList.Add(object? value) { - this.Add((T)value); + this.Add((T)value!); return this.Count - 1; } @@ -983,11 +985,11 @@ namespace System.Collections.Immutable /// /// true if the is found in the ; otherwise, false. /// - bool IList.Contains(object value) + bool IList.Contains(object? value) { if (IsCompatibleObject(value)) { - return this.Contains((T)value); + return this.Contains((T)value!); } return false; @@ -1000,11 +1002,11 @@ namespace System.Collections.Immutable /// /// The index of if found in the list; otherwise, -1. /// - int IList.IndexOf(object value) + int IList.IndexOf(object? value) { if (IsCompatibleObject(value)) { - return this.IndexOf((T)value); + return this.IndexOf((T)value!); } return -1; @@ -1015,9 +1017,9 @@ namespace System.Collections.Immutable /// /// The zero-based index at which should be inserted. /// The object to insert into the . - void IList.Insert(int index, object value) + void IList.Insert(int index, object? value) { - this.Insert(index, (T)value); + this.Insert(index, (T)value!); } /// @@ -1043,11 +1045,11 @@ namespace System.Collections.Immutable /// Removes the first occurrence of a specific object from the . /// /// The object to remove from the . - void IList.Remove(object value) + void IList.Remove(object? value) { if (IsCompatibleObject(value)) { - this.Remove((T)value); + this.Remove((T)value!); } } @@ -1059,10 +1061,10 @@ namespace System.Collections.Immutable /// /// The index. /// - object IList.this[int index] + object? IList.this[int index] { get { return this[index]; } - set { this[index] = (T)value; } + set { this[index] = (T)value!; } } #endregion @@ -1100,7 +1102,7 @@ namespace System.Collections.Immutable { if (_syncRoot == null) { - System.Threading.Interlocked.CompareExchange(ref _syncRoot, new object(), null); + System.Threading.Interlocked.CompareExchange(ref _syncRoot, new object(), null); } return _syncRoot; @@ -1123,7 +1125,7 @@ namespace System.Collections.Immutable /// /// The simple view of the collection. /// - private T[] _cachedContents; + private T[]? _cachedContents; /// /// Initializes a new instance of the class. diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableList_1.Enumerator.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableList_1.Enumerator.cs index 7c817bd..9a24ef1 100755 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableList_1.Enumerator.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableList_1.Enumerator.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.ComponentModel; +using System.Diagnostics; namespace System.Collections.Immutable { @@ -37,7 +38,7 @@ namespace System.Collections.Immutable /// /// The builder being enumerated, if applicable. /// - private readonly Builder _builder; + private readonly Builder? _builder; /// /// A unique ID for this instance of this enumerator. @@ -73,12 +74,12 @@ namespace System.Collections.Immutable /// /// The stack to use for enumerating the binary tree. /// - private SecurePooledObject>> _stack; + private SecurePooledObject>>? _stack; /// /// The node currently selected. /// - private Node _current; + private Node? _current; /// /// The version of the builder (when applicable) that is being enumerated. @@ -93,7 +94,7 @@ namespace System.Collections.Immutable /// The index of the first element to enumerate. /// The number of elements in this collection. /// true if the list should be enumerated in reverse order. - internal Enumerator(Node root, Builder builder = null, int startIndex = -1, int count = -1, bool reversed = false) + internal Enumerator(Node root, Builder? builder = null, int startIndex = -1, int count = -1, bool reversed = false) { Requires.NotNull(root, nameof(root)); Requires.Range(startIndex >= -1, nameof(startIndex)); @@ -145,20 +146,19 @@ namespace System.Collections.Immutable /// /// The current element. /// - object System.Collections.IEnumerator.Current => this.Current; + object? System.Collections.IEnumerator.Current => this.Current; /// /// Disposes of this enumerator and returns the stack reference to the resource pool. /// public void Dispose() { - _root = null; + _root = null!; _current = null; - Stack> stack; - if (_stack != null && _stack.TryUse(ref this, out stack)) + if (_stack != null && _stack.TryUse(ref this, out Stack>? stack)) { stack.ClearFastWhenEmpty(); - s_EnumeratingStacks.TryAdd(this, _stack); + s_EnumeratingStacks.TryAdd(this, _stack!); } _stack = null; @@ -180,7 +180,7 @@ namespace System.Collections.Immutable { Node n = stack.Pop().Value; _current = n; - this.PushNext(this.NextBranch(n)); + this.PushNext(this.NextBranch(n)!); _remainingCount--; return true; } @@ -208,22 +208,23 @@ namespace System.Collections.Immutable /// Resets the stack used for enumeration. private void ResetStack() { + Debug.Assert(_stack != null); var stack = _stack.Use(ref this); stack.ClearFastWhenEmpty(); var node = _root; var skipNodes = _reversed ? _root.Count - _startIndex - 1 : _startIndex; - while (!node.IsEmpty && skipNodes != this.PreviousBranch(node).Count) + while (!node.IsEmpty && skipNodes != this.PreviousBranch(node)!.Count) { - if (skipNodes < this.PreviousBranch(node).Count) + if (skipNodes < this.PreviousBranch(node)!.Count) { stack.Push(new RefAsValueType(node)); - node = this.PreviousBranch(node); + node = this.PreviousBranch(node)!; } else { - skipNodes -= this.PreviousBranch(node).Count + 1; - node = this.NextBranch(node); + skipNodes -= this.PreviousBranch(node)!.Count + 1; + node = this.NextBranch(node)!; } } @@ -236,12 +237,12 @@ namespace System.Collections.Immutable /// /// Obtains the right branch of the given node (or the left, if walking in reverse). /// - private Node NextBranch(Node node) => _reversed ? node.Left : node.Right; + private Node? NextBranch(Node node) => _reversed ? node.Left : node.Right; /// /// Obtains the left branch of the given node (or the right, if walking in reverse). /// - private Node PreviousBranch(Node node) => _reversed ? node.Right : node.Left; + private Node? PreviousBranch(Node node) => _reversed ? node.Right : node.Left; /// /// Throws an if this enumerator has been disposed. @@ -281,11 +282,12 @@ namespace System.Collections.Immutable Requires.NotNull(node, nameof(node)); if (!node.IsEmpty) { + Debug.Assert(_stack != null); var stack = _stack.Use(ref this); while (!node.IsEmpty) { stack.Push(new RefAsValueType(node)); - node = this.PreviousBranch(node); + node = this.PreviousBranch(node)!; } } } diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableList_1.Node.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableList_1.Node.cs index cfde501..211d166 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableList_1.Node.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableList_1.Node.cs @@ -26,7 +26,7 @@ namespace System.Collections.Immutable /// /// The key associated with this node. /// - private T _key; + private T _key = default!; // Defaulting for EmptyNode, shouldn't be null for non empty nodes so didn't annotated with [MaybeNull] /// /// A value indicating whether this node has been frozen (made immutable). @@ -56,12 +56,12 @@ namespace System.Collections.Immutable /// /// The left tree. /// - private Node _left; + private Node? _left; /// /// The right tree. /// - private Node _right; + private Node? _right; /// /// Initializes a new instance of the class @@ -120,32 +120,32 @@ namespace System.Collections.Immutable /// /// Gets the left branch of this node. /// - public Node Left => _left; + public Node? Left => _left; /// /// Gets the left branch of this node. /// - IBinaryTree IBinaryTree.Left => _left; + IBinaryTree? IBinaryTree.Left => _left; /// /// Gets the right branch of this node. /// - public Node Right => _right; + public Node? Right => _right; /// /// Gets the right branch of this node. /// - IBinaryTree IBinaryTree.Right => _right; + IBinaryTree? IBinaryTree.Right => _right; /// /// Gets the left branch of this node. /// - IBinaryTree IBinaryTree.Left => _left; + IBinaryTree? IBinaryTree.Left => _left; /// /// Gets the right branch of this node. /// - IBinaryTree IBinaryTree.Right => _right; + IBinaryTree? IBinaryTree.Right => _right; /// /// Gets the value represented by the current node. @@ -172,6 +172,7 @@ namespace System.Collections.Immutable get { Requires.Range(index >= 0 && index < this.Count, nameof(index)); + Debug.Assert(_left != null && _right != null); if (index < _left._count) { @@ -197,6 +198,7 @@ namespace System.Collections.Immutable { Requires.Range(index >= 0 && index < this.Count, nameof(index)); + Debug.Assert(_left != null && _right != null); if (index < _left._count) { return ref _left.ItemRef(index); @@ -288,7 +290,7 @@ namespace System.Collections.Immutable return CreateLeaf(key); } - Node newRight = _right.Add(key); + Node newRight = _right!.Add(key); Node result = this.MutateRight(newRight); return result.IsBalanced ? result : result.BalanceRight(); } @@ -308,7 +310,7 @@ namespace System.Collections.Immutable return CreateLeaf(key); } - if (index <= _left._count) + if (index <= _left!._count) { Node newLeft = _left.Insert(index, key); Node result = this.MutateLeft(newLeft); @@ -316,7 +318,7 @@ namespace System.Collections.Immutable } else { - Node newRight = _right.Insert(index - _left._count - 1, key); + Node newRight = _right!.Insert(index - _left._count - 1, key); Node result = this.MutateRight(newRight); return result.IsBalanced ? result : result.BalanceRight(); } @@ -336,7 +338,7 @@ namespace System.Collections.Immutable return CreateRange(keys); } - Node newRight = _right.AddRange(keys); + Node newRight = _right!.AddRange(keys); Node result = this.MutateRight(newRight); return result.BalanceMany(); } @@ -358,14 +360,14 @@ namespace System.Collections.Immutable } Node result; - if (index <= _left._count) + if (index <= _left!._count) { Node newLeft = _left.InsertRange(index, keys); result = this.MutateLeft(newLeft); } else { - Node newRight = _right.InsertRange(index - _left._count - 1, keys); + Node newRight = _right!.InsertRange(index - _left._count - 1, keys); result = this.MutateRight(newRight); } @@ -380,6 +382,7 @@ namespace System.Collections.Immutable internal Node RemoveAt(int index) { Requires.Range(index >= 0 && index < this.Count, nameof(index)); + Debug.Assert(_left != null && _right != null); Node result = this; if (index == _left._count) @@ -404,7 +407,7 @@ namespace System.Collections.Immutable // We have two children. Remove the next-highest node and replace // this node with it. var successor = _right; - while (!successor._left.IsEmpty) + while (!successor._left!.IsEmpty) { successor = successor._left; } @@ -482,7 +485,7 @@ namespace System.Collections.Immutable Debug.Assert(!this.IsEmpty); Node result = this; - if (index == _left._count) + if (index == _left!._count) { // We have a match. result = this.MutateKey(value); @@ -494,7 +497,7 @@ namespace System.Collections.Immutable } else { - var newRight = _right.ReplaceAt(index - _left._count - 1, value); + var newRight = _right!.ReplaceAt(index - _left._count - 1, value); result = this.MutateRight(newRight); } @@ -575,7 +578,7 @@ namespace System.Collections.Immutable /// elements, or null to use the default comparer . /// /// The sorted list. - internal Node Sort(IComparer comparer) => this.Sort(0, this.Count, comparer); + internal Node Sort(IComparer? comparer) => this.Sort(0, this.Count, comparer); /// /// Sorts the elements in a range of elements in @@ -592,7 +595,7 @@ namespace System.Collections.Immutable /// elements, or null to use the default comparer . /// /// The sorted list. - internal Node Sort(int index, int count, IComparer comparer) + internal Node Sort(int index, int count, IComparer? comparer) { Requires.Range(index >= 0, nameof(index)); Requires.Range(count >= 0, nameof(count)); @@ -634,7 +637,7 @@ namespace System.Collections.Immutable /// cannot find an implementation of the generic interface /// or the interface for type . /// - internal int BinarySearch(int index, int count, T item, IComparer comparer) + internal int BinarySearch(int index, int count, T item, IComparer? comparer) { Requires.Range(index >= 0, nameof(index)); Requires.Range(count >= 0, nameof(count)); @@ -646,14 +649,14 @@ namespace System.Collections.Immutable } // If this node is not within range, defer to either branch as appropriate. - int thisNodeIndex = _left.Count; // this is only the index within the AVL tree, treating this node as root rather than a member of a larger tree. + int thisNodeIndex = _left!.Count; // this is only the index within the AVL tree, treating this node as root rather than a member of a larger tree. if (index + count <= thisNodeIndex) { return _left.BinarySearch(index, count, item, comparer); } else if (index > thisNodeIndex) { - int result = _right.BinarySearch(index - thisNodeIndex - 1, count, item, comparer); + int result = _right!.BinarySearch(index - thisNodeIndex - 1, count, item, comparer); int offset = thisNodeIndex + 1; return result < 0 ? result - offset : result + offset; } @@ -670,7 +673,7 @@ namespace System.Collections.Immutable else if (compare > 0) { int adjustedCount = count - (thisNodeIndex - index) - 1; - int result = adjustedCount < 0 ? -1 : _right.BinarySearch(0, adjustedCount, item, comparer); + int result = adjustedCount < 0 ? -1 : _right!.BinarySearch(0, adjustedCount, item, comparer); int offset = thisNodeIndex + 1; return result < 0 ? result - offset : result + offset; } @@ -702,7 +705,7 @@ namespace System.Collections.Immutable /// , if found; otherwise, -1. /// [Pure] - internal int IndexOf(T item, IEqualityComparer equalityComparer) => this.IndexOf(item, 0, this.Count, equalityComparer); + internal int IndexOf(T item, IEqualityComparer? equalityComparer) => this.IndexOf(item, 0, this.Count, equalityComparer); /// /// Searches for the specified object and returns true if it is found, false otherwise. @@ -746,7 +749,7 @@ namespace System.Collections.Immutable /// contains number of elements, if found; otherwise, -1. /// [Pure] - internal int IndexOf(T item, int index, int count, IEqualityComparer equalityComparer) + internal int IndexOf(T item, int index, int count, IEqualityComparer? equalityComparer) { Requires.Range(index >= 0, nameof(index)); Requires.Range(count >= 0, nameof(count)); @@ -789,7 +792,7 @@ namespace System.Collections.Immutable /// and ends at , if found; otherwise, -1. /// [Pure] - internal int LastIndexOf(T item, int index, int count, IEqualityComparer equalityComparer) + internal int LastIndexOf(T item, int index, int count, IEqualityComparer? equalityComparer) { Requires.Range(index >= 0, nameof(index)); Requires.Range(count >= 0 && count <= this.Count, nameof(count)); @@ -1002,6 +1005,7 @@ namespace System.Collections.Immutable /// The first element that matches the conditions defined by the specified predicate, /// if found; otherwise, the default value for type . /// + [return: MaybeNull] internal T Find(Predicate match) { Requires.NotNull(match, nameof(match)); @@ -1014,7 +1018,7 @@ namespace System.Collections.Immutable } } - return default(T); + return default(T)!; } /// @@ -1039,7 +1043,7 @@ namespace System.Collections.Immutable return ImmutableList.Empty; } - List list = null; + List? list = null; foreach (var item in this) { if (match(item)) @@ -1147,6 +1151,7 @@ namespace System.Collections.Immutable /// The last element that matches the conditions defined by the specified predicate, /// if found; otherwise, the default value for type . /// + [return: MaybeNull] internal T FindLast(Predicate match) { Requires.NotNull(match, nameof(match)); @@ -1162,7 +1167,7 @@ namespace System.Collections.Immutable } } - return default(T); + return default(T)!; } /// @@ -1255,8 +1260,8 @@ namespace System.Collections.Immutable // If this node is frozen, all its descendants must already be frozen. if (!_frozen) { - _left.Freeze(); - _right.Freeze(); + _left!.Freeze(); + _right!.Freeze(); _frozen = true; } } @@ -1270,9 +1275,9 @@ namespace System.Collections.Immutable private Node RotateLeft() { Debug.Assert(!this.IsEmpty); - Debug.Assert(!_right.IsEmpty); + Debug.Assert(!_right!.IsEmpty); - return _right.MutateLeft(this.MutateRight(_right._left)); + return _right.MutateLeft(this.MutateRight(_right._left!)); } /// @@ -1282,9 +1287,9 @@ namespace System.Collections.Immutable private Node RotateRight() { Debug.Assert(!this.IsEmpty); - Debug.Assert(!_left.IsEmpty); + Debug.Assert(!_left!.IsEmpty); - return _left.MutateRight(this.MutateLeft(_left._right)); + return _left.MutateRight(this.MutateLeft(_left._right!)); } /// @@ -1294,15 +1299,15 @@ namespace System.Collections.Immutable private Node DoubleLeft() { Debug.Assert(!this.IsEmpty); - Debug.Assert(!_right.IsEmpty); - Debug.Assert(!_right._left.IsEmpty); + Debug.Assert(!_right!.IsEmpty); + Debug.Assert(!_right!._left!.IsEmpty); // The following is an optimized version of rotating the right child right, then rotating the parent left. Node right = _right; Node rightLeft = right._left; return rightLeft.MutateBoth( - left: this.MutateRight(rightLeft._left), - right: right.MutateLeft(rightLeft._right)); + left: this.MutateRight(rightLeft._left!), + right: right.MutateLeft(rightLeft._right!)); } /// @@ -1312,15 +1317,15 @@ namespace System.Collections.Immutable private Node DoubleRight() { Debug.Assert(!this.IsEmpty); - Debug.Assert(!_left.IsEmpty); - Debug.Assert(!_left._right.IsEmpty); + Debug.Assert(!_left!.IsEmpty); + Debug.Assert(!_left._right!.IsEmpty); // The following is an optimized version of rotating the left child left, then rotating the parent right. Node left = _left; Node leftRight = left._right; return leftRight.MutateBoth( - left: left.MutateRight(leftRight._left), - right: this.MutateLeft(leftRight._right)); + left: left.MutateRight(leftRight._left!), + right: this.MutateLeft(leftRight._right!)); } /// @@ -1334,7 +1339,7 @@ namespace System.Collections.Immutable get { Debug.Assert(!this.IsEmpty); - return _right._height - _left._height; + return _right!._height - _left!._height; } } @@ -1377,7 +1382,7 @@ namespace System.Collections.Immutable Debug.Assert(!this.IsEmpty); Debug.Assert(this.IsLeftHeavy); - return _left.BalanceFactor > 0 ? this.DoubleRight() : this.RotateRight(); + return _left!.BalanceFactor > 0 ? this.DoubleRight() : this.RotateRight(); } /// @@ -1389,7 +1394,7 @@ namespace System.Collections.Immutable Debug.Assert(!this.IsEmpty); Debug.Assert(this.IsRightHeavy); - return _right.BalanceFactor < 0 ? this.DoubleLeft() : this.RotateLeft(); + return _right!.BalanceFactor < 0 ? this.DoubleLeft() : this.RotateLeft(); } /// @@ -1403,16 +1408,19 @@ namespace System.Collections.Immutable private Node BalanceMany() { Node tree = this; + Debug.Assert(tree._left != null && tree._right != null); while (!tree.IsBalanced) { if (tree.IsRightHeavy) { tree = tree.BalanceRight(); + Debug.Assert(tree._left != null); tree.MutateLeft(tree._left.BalanceMany()); } else { tree = tree.BalanceLeft(); + Debug.Assert(tree._right != null); tree.MutateRight(tree._right.BalanceMany()); } } @@ -1462,13 +1470,13 @@ namespace System.Collections.Immutable if (_frozen) { - return new Node(_key, left, _right); + return new Node(_key, left, _right!); } else { _left = left; - _height = ParentHeight(left, _right); - _count = ParentCount(left, _right); + _height = ParentHeight(left, _right!); + _count = ParentCount(left, _right!); return this; } } @@ -1486,13 +1494,13 @@ namespace System.Collections.Immutable if (_frozen) { - return new Node(_key, _left, right); + return new Node(_key, _left!, right); } else { _right = right; - _height = ParentHeight(_left, right); - _count = ParentCount(_left, right); + _height = ParentHeight(_left!, right); + _count = ParentCount(_left!, right); return this; } } @@ -1526,7 +1534,7 @@ namespace System.Collections.Immutable if (_frozen) { - return new Node(key, _left, _right); + return new Node(key, _left!, _right!); } else { @@ -1542,7 +1550,7 @@ namespace System.Collections.Immutable /// The root of the created node tree. private static Node CreateRange(IEnumerable keys) { - ImmutableList other; + ImmutableList? other; if (TryCastToImmutableList(keys, out other)) { return other._root; @@ -1573,7 +1581,7 @@ namespace System.Collections.Immutable /// The equality comparer to use for testing the node and value. /// /// - private static bool Contains(Node node, T value, IEqualityComparer equalityComparer) => !node.IsEmpty && (equalityComparer.Equals(value, node._key) || Contains(node._left, value, equalityComparer) || Contains(node._right, value, equalityComparer)); + private static bool Contains(Node node, T value, IEqualityComparer equalityComparer) => !node.IsEmpty && (equalityComparer.Equals(value, node._key) || Contains(node._left!, value, equalityComparer) || Contains(node._right!, value, equalityComparer)); } } } diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableList_1.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableList_1.cs index 7ed9ce2..2b4fb31 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableList_1.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableList_1.cs @@ -89,7 +89,7 @@ namespace System.Collections.Immutable /// cannot find an implementation of the generic interface /// or the interface for type . /// - public int BinarySearch(T item, IComparer comparer) => this.BinarySearch(0, this.Count, item, comparer); + public int BinarySearch(T item, IComparer? comparer) => this.BinarySearch(0, this.Count, item, comparer); /// /// Searches a range of elements in the sorted @@ -120,7 +120,7 @@ namespace System.Collections.Immutable /// cannot find an implementation of the generic interface /// or the interface for type . /// - public int BinarySearch(int index, int count, T item, IComparer comparer) => _root.BinarySearch(index, count, item, comparer); + public int BinarySearch(int index, int count, T item, IComparer? comparer) => _root.BinarySearch(index, count, item, comparer); #region IImmutableList Properties @@ -279,7 +279,7 @@ namespace System.Collections.Immutable /// See the interface. /// [Pure] - public ImmutableList Remove(T value, IEqualityComparer equalityComparer) + public ImmutableList Remove(T value, IEqualityComparer? equalityComparer) { int index = this.IndexOf(value, equalityComparer); return index < 0 ? this : this.RemoveAt(index); @@ -329,7 +329,7 @@ namespace System.Collections.Immutable /// A new list with the elements removed. /// [Pure] - public ImmutableList RemoveRange(IEnumerable items, IEqualityComparer equalityComparer) + public ImmutableList RemoveRange(IEnumerable items, IEqualityComparer? equalityComparer) { Requires.NotNull(items, nameof(items)); @@ -400,7 +400,7 @@ namespace System.Collections.Immutable /// See the interface. /// [Pure] - public ImmutableList Replace(T oldValue, T newValue, IEqualityComparer equalityComparer) + public ImmutableList Replace(T oldValue, T newValue, IEqualityComparer? equalityComparer) { int index = this.IndexOf(oldValue, equalityComparer); if (index < 0) @@ -460,7 +460,7 @@ namespace System.Collections.Immutable /// /// The sorted list. [Pure] - public ImmutableList Sort(IComparer comparer) => this.Wrap(_root.Sort(comparer)); + public ImmutableList Sort(IComparer? comparer) => this.Wrap(_root.Sort(comparer)); /// /// Sorts the elements in a range of elements in @@ -478,7 +478,7 @@ namespace System.Collections.Immutable /// /// The sorted list. [Pure] - public ImmutableList Sort(int index, int count, IComparer comparer) + public ImmutableList Sort(int index, int count, IComparer? comparer) { Requires.Range(index >= 0, nameof(index)); Requires.Range(count >= 0, nameof(count)); @@ -617,6 +617,7 @@ namespace System.Collections.Immutable /// The first element that matches the conditions defined by the specified predicate, /// if found; otherwise, the default value for type . /// + [return: MaybeNull] public T Find(Predicate match) => _root.Find(match); /// @@ -690,6 +691,7 @@ namespace System.Collections.Immutable /// The last element that matches the conditions defined by the specified predicate, /// if found; otherwise, the default value for type . /// + [return: MaybeNull] public T FindLast(Predicate match) => _root.FindLast(match); /// @@ -765,7 +767,7 @@ namespace System.Collections.Immutable /// contains number of elements, if found; otherwise, -1. /// [Pure] - public int IndexOf(T item, int index, int count, IEqualityComparer equalityComparer) => _root.IndexOf(item, index, count, equalityComparer); + public int IndexOf(T item, int index, int count, IEqualityComparer? equalityComparer) => _root.IndexOf(item, index, count, equalityComparer); /// /// Searches for the specified object and returns the zero-based index of the @@ -788,7 +790,7 @@ namespace System.Collections.Immutable /// and ends at , if found; otherwise, -1. /// [Pure] - public int LastIndexOf(T item, int index, int count, IEqualityComparer equalityComparer) => _root.LastIndexOf(item, index, count, equalityComparer); + public int LastIndexOf(T item, int index, int count, IEqualityComparer? equalityComparer) => _root.LastIndexOf(item, index, count, equalityComparer); /// /// Determines whether every element in the @@ -856,7 +858,7 @@ namespace System.Collections.Immutable /// See the interface. /// [ExcludeFromCodeCoverage] - IImmutableList IImmutableList.Remove(T value, IEqualityComparer equalityComparer) => this.Remove(value, equalityComparer); + IImmutableList IImmutableList.Remove(T value, IEqualityComparer? equalityComparer) => this.Remove(value, equalityComparer); /// /// See the interface. @@ -868,7 +870,7 @@ namespace System.Collections.Immutable /// See the interface. /// [ExcludeFromCodeCoverage] - IImmutableList IImmutableList.RemoveRange(IEnumerable items, IEqualityComparer equalityComparer) => this.RemoveRange(items, equalityComparer); + IImmutableList IImmutableList.RemoveRange(IEnumerable items, IEqualityComparer? equalityComparer) => this.RemoveRange(items, equalityComparer); /// /// See the interface. @@ -904,7 +906,7 @@ namespace System.Collections.Immutable /// /// The new list. /// Thrown when the old value does not exist in the list. - IImmutableList IImmutableList.Replace(T oldValue, T newValue, IEqualityComparer equalityComparer) => this.Replace(oldValue, newValue, equalityComparer); + IImmutableList IImmutableList.Replace(T oldValue, T newValue, IEqualityComparer? equalityComparer) => this.Replace(oldValue, newValue, equalityComparer); #endregion @@ -1018,7 +1020,7 @@ namespace System.Collections.Immutable /// Nothing. An exception is always thrown. /// /// Always thrown. - int IList.Add(object value) => throw new NotSupportedException(); + int IList.Add(object? value) => throw new NotSupportedException(); /// /// Removes the item at the specified index. @@ -1040,7 +1042,7 @@ namespace System.Collections.Immutable /// /// true if the is found in the ; otherwise, false. /// - bool IList.Contains(object value) => IsCompatibleObject(value) && this.Contains((T)value); + bool IList.Contains(object? value) => IsCompatibleObject(value) && this.Contains((T)value!); /// /// Determines the index of a specific item in the . @@ -1049,7 +1051,7 @@ namespace System.Collections.Immutable /// /// The index of if found in the list; otherwise, -1. /// - int IList.IndexOf(object value) => IsCompatibleObject(value) ? this.IndexOf((T)value) : -1; + int IList.IndexOf(object? value) => IsCompatibleObject(value) ? this.IndexOf((T)value!) : -1; /// /// Inserts an item to the at the specified index. @@ -1057,7 +1059,7 @@ namespace System.Collections.Immutable /// The zero-based index at which should be inserted. /// The object to insert into the . /// Always thrown. - void IList.Insert(int index, object value) => throw new NotSupportedException(); + void IList.Insert(int index, object? value) => throw new NotSupportedException(); /// /// Gets a value indicating whether the has a fixed size. @@ -1077,7 +1079,7 @@ namespace System.Collections.Immutable /// /// The object to remove from the . /// Always thrown. - void IList.Remove(object value) => throw new NotSupportedException(); + void IList.Remove(object? value) => throw new NotSupportedException(); /// /// Gets or sets the at the specified index. @@ -1089,7 +1091,7 @@ namespace System.Collections.Immutable /// The value at the specified index. /// Thrown from getter when is negative or not less than . /// Always thrown from the setter. - object IList.this[int index] + object? IList.this[int index] { get => this[index]; set => throw new NotSupportedException(); @@ -1137,7 +1139,7 @@ namespace System.Collections.Immutable /// The sequence that may have come from an immutable list. /// Receives the concrete typed value if one can be found. /// true if the cast was successful; false otherwise. - private static bool TryCastToImmutableList(IEnumerable sequence, out ImmutableList other) + private static bool TryCastToImmutableList(IEnumerable sequence, [NotNullWhen(true)] out ImmutableList? other) { other = sequence as ImmutableList; if (other != null) @@ -1163,11 +1165,11 @@ namespace System.Collections.Immutable /// /// This implementation comes from . /// - private static bool IsCompatibleObject(object value) + private static bool IsCompatibleObject(object? value) { // Non-null values are fine. Only accept nulls if T is a class or Nullable. // Note that default(T) is not equal to null for value types except when T is Nullable. - return ((value is T) || (value == null && default(T) == null)); + return ((value is T) || (value == null && default(T)! == null)); } /// @@ -1197,7 +1199,7 @@ namespace System.Collections.Immutable { // If the items being added actually come from an ImmutableList // then there is no value in reconstructing it. - if (TryCastToImmutableList(items, out ImmutableList other)) + if (TryCastToImmutableList(items, out ImmutableList? other)) { return other; } diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableQueue_1.Enumerator.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableQueue_1.Enumerator.cs index 1c17181..f0545f2 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableQueue_1.Enumerator.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableQueue_1.Enumerator.cs @@ -23,13 +23,13 @@ namespace System.Collections.Immutable /// /// The remaining forwards stack of the queue being enumerated. /// - private ImmutableStack _remainingForwardsStack; + private ImmutableStack? _remainingForwardsStack; /// /// The remaining backwards stack of the queue being enumerated. /// Its order is reversed when the field is first initialized. /// - private ImmutableStack _remainingBackwardsStack; + private ImmutableStack? _remainingBackwardsStack; /// /// Initializes a new instance of the struct. @@ -61,7 +61,7 @@ namespace System.Collections.Immutable { return _remainingForwardsStack.Peek(); } - else if (!_remainingBackwardsStack.IsEmpty) + else if (!_remainingBackwardsStack!.IsEmpty) { return _remainingBackwardsStack.Peek(); } @@ -90,12 +90,12 @@ namespace System.Collections.Immutable { _remainingForwardsStack = _remainingForwardsStack.Pop(); } - else if (!_remainingBackwardsStack.IsEmpty) + else if (!_remainingBackwardsStack!.IsEmpty) { _remainingBackwardsStack = _remainingBackwardsStack.Pop(); } - return !_remainingForwardsStack.IsEmpty || !_remainingBackwardsStack.IsEmpty; + return !_remainingForwardsStack.IsEmpty || !_remainingBackwardsStack!.IsEmpty; } } @@ -112,13 +112,13 @@ namespace System.Collections.Immutable /// /// The remaining forwards stack of the queue being enumerated. /// - private ImmutableStack _remainingForwardsStack; + private ImmutableStack? _remainingForwardsStack; /// /// The remaining backwards stack of the queue being enumerated. /// Its order is reversed when the field is first initialized. /// - private ImmutableStack _remainingBackwardsStack; + private ImmutableStack? _remainingBackwardsStack; /// /// A value indicating whether this enumerator has been disposed. @@ -152,7 +152,7 @@ namespace System.Collections.Immutable { return _remainingForwardsStack.Peek(); } - else if (!_remainingBackwardsStack.IsEmpty) + else if (!_remainingBackwardsStack!.IsEmpty) { return _remainingBackwardsStack.Peek(); } @@ -169,7 +169,7 @@ namespace System.Collections.Immutable /// object IEnumerator.Current { - get { return this.Current; } + get { return this.Current!; } } /// @@ -190,12 +190,12 @@ namespace System.Collections.Immutable { _remainingForwardsStack = _remainingForwardsStack.Pop(); } - else if (!_remainingBackwardsStack.IsEmpty) + else if (!_remainingBackwardsStack!.IsEmpty) { _remainingBackwardsStack = _remainingBackwardsStack.Pop(); } - return !_remainingForwardsStack.IsEmpty || !_remainingBackwardsStack.IsEmpty; + return !_remainingForwardsStack.IsEmpty || !_remainingBackwardsStack!.IsEmpty; } /// diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableQueue_1.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableQueue_1.cs index 92eb566..2a894af 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableQueue_1.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableQueue_1.cs @@ -42,7 +42,7 @@ namespace System.Collections.Immutable /// /// Backing field for the property. /// - private ImmutableStack _backwardsReversed; + private ImmutableStack? _backwardsReversed; /// /// Initializes a new instance of the class. diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedDictionary.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedDictionary.cs index 6303b91..e5881df 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedDictionary.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedDictionary.cs @@ -22,7 +22,7 @@ namespace System.Collections.Immutable /// The type of values stored by the dictionary. /// The immutable collection. [Pure] - public static ImmutableSortedDictionary Create() + public static ImmutableSortedDictionary Create() where TKey : notnull { return ImmutableSortedDictionary.Empty; } @@ -35,7 +35,7 @@ namespace System.Collections.Immutable /// The key comparer. /// The immutable collection. [Pure] - public static ImmutableSortedDictionary Create(IComparer keyComparer) + public static ImmutableSortedDictionary Create(IComparer? keyComparer) where TKey : notnull { return ImmutableSortedDictionary.Empty.WithComparers(keyComparer); } @@ -49,7 +49,7 @@ namespace System.Collections.Immutable /// The value comparer. /// The immutable collection. [Pure] - public static ImmutableSortedDictionary Create(IComparer keyComparer, IEqualityComparer valueComparer) + public static ImmutableSortedDictionary Create(IComparer? keyComparer, IEqualityComparer? valueComparer) where TKey : notnull { return ImmutableSortedDictionary.Empty.WithComparers(keyComparer, valueComparer); } @@ -63,7 +63,7 @@ namespace System.Collections.Immutable /// The new immutable collection. [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")] [Pure] - public static ImmutableSortedDictionary CreateRange(IEnumerable> items) + public static ImmutableSortedDictionary CreateRange(IEnumerable> items) where TKey : notnull { return ImmutableSortedDictionary.Empty.AddRange(items); } @@ -78,7 +78,7 @@ namespace System.Collections.Immutable /// The new immutable collection. [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")] [Pure] - public static ImmutableSortedDictionary CreateRange(IComparer keyComparer, IEnumerable> items) + public static ImmutableSortedDictionary CreateRange(IComparer? keyComparer, IEnumerable> items) where TKey : notnull { return ImmutableSortedDictionary.Empty.WithComparers(keyComparer).AddRange(items); } @@ -94,7 +94,7 @@ namespace System.Collections.Immutable /// The new immutable collection. [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")] [Pure] - public static ImmutableSortedDictionary CreateRange(IComparer keyComparer, IEqualityComparer valueComparer, IEnumerable> items) + public static ImmutableSortedDictionary CreateRange(IComparer? keyComparer, IEqualityComparer? valueComparer, IEnumerable> items) where TKey : notnull { return ImmutableSortedDictionary.Empty.WithComparers(keyComparer, valueComparer).AddRange(items); } @@ -106,7 +106,7 @@ namespace System.Collections.Immutable /// The type of values stored by the dictionary. /// The immutable collection builder. [Pure] - public static ImmutableSortedDictionary.Builder CreateBuilder() + public static ImmutableSortedDictionary.Builder CreateBuilder() where TKey : notnull { return Create().ToBuilder(); } @@ -119,7 +119,7 @@ namespace System.Collections.Immutable /// The key comparer. /// The immutable collection builder. [Pure] - public static ImmutableSortedDictionary.Builder CreateBuilder(IComparer keyComparer) + public static ImmutableSortedDictionary.Builder CreateBuilder(IComparer? keyComparer) where TKey : notnull { return Create(keyComparer).ToBuilder(); } @@ -133,7 +133,7 @@ namespace System.Collections.Immutable /// The value comparer. /// The immutable collection builder. [Pure] - public static ImmutableSortedDictionary.Builder CreateBuilder(IComparer keyComparer, IEqualityComparer valueComparer) + public static ImmutableSortedDictionary.Builder CreateBuilder(IComparer? keyComparer, IEqualityComparer? valueComparer) where TKey : notnull { return Create(keyComparer, valueComparer).ToBuilder(); } @@ -151,7 +151,7 @@ namespace System.Collections.Immutable /// The value comparer to use for the map. /// The immutable map. [Pure] - public static ImmutableSortedDictionary ToImmutableSortedDictionary(this IEnumerable source, Func keySelector, Func elementSelector, IComparer keyComparer, IEqualityComparer valueComparer) + public static ImmutableSortedDictionary ToImmutableSortedDictionary(this IEnumerable source, Func keySelector, Func elementSelector, IComparer? keyComparer, IEqualityComparer? valueComparer) where TKey : notnull { Requires.NotNull(source, nameof(source)); Requires.NotNull(keySelector, nameof(keySelector)); @@ -167,7 +167,7 @@ namespace System.Collections.Immutable /// The builder to create the immutable map from. /// An immutable map. [Pure] - public static ImmutableSortedDictionary ToImmutableSortedDictionary(this ImmutableSortedDictionary.Builder builder) + public static ImmutableSortedDictionary ToImmutableSortedDictionary(this ImmutableSortedDictionary.Builder builder) where TKey : notnull { Requires.NotNull(builder, nameof(builder)); @@ -186,7 +186,7 @@ namespace System.Collections.Immutable /// The key comparer to use for the map. /// The immutable map. [Pure] - public static ImmutableSortedDictionary ToImmutableSortedDictionary(this IEnumerable source, Func keySelector, Func elementSelector, IComparer keyComparer) + public static ImmutableSortedDictionary ToImmutableSortedDictionary(this IEnumerable source, Func keySelector, Func elementSelector, IComparer? keyComparer) where TKey : notnull { return ToImmutableSortedDictionary(source, keySelector, elementSelector, keyComparer, null); } @@ -202,7 +202,7 @@ namespace System.Collections.Immutable /// The function that will produce the value for the map from each sequence element. /// The immutable map. [Pure] - public static ImmutableSortedDictionary ToImmutableSortedDictionary(this IEnumerable source, Func keySelector, Func elementSelector) + public static ImmutableSortedDictionary ToImmutableSortedDictionary(this IEnumerable source, Func keySelector, Func elementSelector) where TKey : notnull { return ToImmutableSortedDictionary(source, keySelector, elementSelector, null, null); } @@ -218,7 +218,7 @@ namespace System.Collections.Immutable /// An immutable map. [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")] [Pure] - public static ImmutableSortedDictionary ToImmutableSortedDictionary(this IEnumerable> source, IComparer keyComparer, IEqualityComparer valueComparer) + public static ImmutableSortedDictionary ToImmutableSortedDictionary(this IEnumerable> source, IComparer? keyComparer, IEqualityComparer? valueComparer) where TKey : notnull { Requires.NotNull(source, nameof(source)); @@ -241,7 +241,7 @@ namespace System.Collections.Immutable /// An immutable map. [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")] [Pure] - public static ImmutableSortedDictionary ToImmutableSortedDictionary(this IEnumerable> source, IComparer keyComparer) + public static ImmutableSortedDictionary ToImmutableSortedDictionary(this IEnumerable> source, IComparer? keyComparer) where TKey : notnull { return ToImmutableSortedDictionary(source, keyComparer, null); } @@ -255,7 +255,7 @@ namespace System.Collections.Immutable /// An immutable map. [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")] [Pure] - public static ImmutableSortedDictionary ToImmutableSortedDictionary(this IEnumerable> source) + public static ImmutableSortedDictionary ToImmutableSortedDictionary(this IEnumerable> source) where TKey : notnull { return ToImmutableSortedDictionary(source, null, null); } diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedDictionary_2.Builder.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedDictionary_2.Builder.cs index a91ff61..eee40ac 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedDictionary_2.Builder.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedDictionary_2.Builder.cs @@ -13,7 +13,7 @@ namespace System.Collections.Immutable /// Contains the inner class. /// [SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix", Justification = "Ignored")] - public sealed partial class ImmutableSortedDictionary + public sealed partial class ImmutableSortedDictionary where TKey : notnull { /// /// A sorted dictionary that mutates with little or no memory allocations, @@ -57,7 +57,7 @@ namespace System.Collections.Immutable /// Caches an immutable instance that represents the current state of the collection. /// /// Null if no immutable view has been created for the current version. - private ImmutableSortedDictionary _immutable; + private ImmutableSortedDictionary? _immutable; /// /// A number that increments every time the builder changes its contents. @@ -67,7 +67,7 @@ namespace System.Collections.Immutable /// /// The object callers may use to synchronize access to this collection. /// - private object _syncRoot; + private object? _syncRoot; /// /// Initializes a new instance of the class. @@ -183,7 +183,7 @@ namespace System.Collections.Immutable get { TValue value; - if (this.TryGetValue(key, out value)) + if (this.TryGetValue(key, out value!)) { return value; } @@ -275,7 +275,7 @@ namespace System.Collections.Immutable { if (_syncRoot == null) { - Threading.Interlocked.CompareExchange(ref _syncRoot, new object(), null); + Threading.Interlocked.CompareExchange(ref _syncRoot, new object(), null); } return _syncRoot; @@ -365,9 +365,9 @@ namespace System.Collections.Immutable /// /// The to use as the key of the element to add. /// The to use as the value of the element to add. - void IDictionary.Add(object key, object value) + void IDictionary.Add(object key, object? value) { - this.Add((TKey)key, (TValue)value); + this.Add((TKey)key, (TValue)value!); } /// @@ -407,10 +407,10 @@ namespace System.Collections.Immutable /// /// The key. /// - object IDictionary.this[object key] + object? IDictionary.this[object key] { get { return this[(TKey)key]; } - set { this[(TKey)key] = (TValue)value; } + set { this[(TKey)key] = (TValue)value!; } } #endregion @@ -470,9 +470,9 @@ namespace System.Collections.Immutable /// /// See /// - public bool TryGetValue(TKey key, out TValue value) + public bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value) { - return this.Root.TryGetValue(key, _keyComparer, out value); + return this.Root.TryGetValue(key, _keyComparer, out value!); } /// @@ -611,9 +611,10 @@ namespace System.Collections.Immutable /// The key to search for. /// The value for the key, or the default value for type if no matching key was found. [Pure] + [return: MaybeNull] public TValue GetValueOrDefault(TKey key) { - return this.GetValueOrDefault(key, default(TValue)); + return this.GetValueOrDefault(key, default(TValue)!); } /// @@ -630,7 +631,7 @@ namespace System.Collections.Immutable Requires.NotNullAllowStructs(key, nameof(key)); TValue value; - if (this.TryGetValue(key, out value)) + if (this.TryGetValue(key, out value!)) { return value; } @@ -664,7 +665,7 @@ namespace System.Collections.Immutable /// /// A simple view of the immutable collection that the debugger can show to the developer. /// - internal class ImmutableSortedDictionaryBuilderDebuggerProxy + internal class ImmutableSortedDictionaryBuilderDebuggerProxy where TKey : notnull { /// /// The collection to be enumerated. @@ -674,7 +675,7 @@ namespace System.Collections.Immutable /// /// The simple view of the collection. /// - private KeyValuePair[] _contents; + private KeyValuePair[]? _contents; /// /// Initializes a new instance of the class. diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedDictionary_2.Enumerator.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedDictionary_2.Enumerator.cs index 5b9f7f0..4a46a18 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedDictionary_2.Enumerator.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedDictionary_2.Enumerator.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.ComponentModel; +using System.Diagnostics; namespace System.Collections.Immutable { @@ -37,7 +38,7 @@ namespace System.Collections.Immutable /// /// The builder being enumerated, if applicable. /// - private readonly Builder _builder; + private readonly Builder? _builder; /// /// A unique ID for this instance of this enumerator. @@ -53,12 +54,12 @@ namespace System.Collections.Immutable /// /// The stack to use for enumerating the binary tree. /// - private SecurePooledObject>> _stack; + private SecurePooledObject>>? _stack; /// /// The node currently selected. /// - private Node _current; + private Node? _current; /// /// The version of the builder (when applicable) that is being enumerated. @@ -70,7 +71,7 @@ namespace System.Collections.Immutable /// /// The root of the set to be enumerated. /// The builder, if applicable. - internal Enumerator(Node root, Builder builder = null) + internal Enumerator(Node root, Builder? builder = null) { Requires.NotNull(root, nameof(root)); @@ -127,13 +128,12 @@ namespace System.Collections.Immutable /// public void Dispose() { - _root = null; + _root = null!; _current = null; - Stack> stack; - if (_stack != null && _stack.TryUse(ref this, out stack)) + if (_stack != null && _stack.TryUse(ref this, out Stack>? stack)) { stack.ClearFastWhenEmpty(); - s_enumeratingStacks.TryAdd(this, _stack); + s_enumeratingStacks.TryAdd(this, _stack!); } _stack = null; @@ -155,7 +155,7 @@ namespace System.Collections.Immutable { Node n = stack.Pop().Value; _current = n; - this.PushLeft(n.Right); + this.PushLeft(n.Right!); return true; } } @@ -217,11 +217,12 @@ namespace System.Collections.Immutable private void PushLeft(Node node) { Requires.NotNull(node, nameof(node)); + Debug.Assert(_stack != null); var stack = _stack.Use(ref this); while (!node.IsEmpty) { stack.Push(new RefAsValueType(node)); - node = node.Left; + node = node.Left!; } } } diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedDictionary_2.Node.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedDictionary_2.Node.cs index b4e4349..4872384 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedDictionary_2.Node.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedDictionary_2.Node.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.Diagnostics.Contracts; using System.Globalization; @@ -25,12 +26,12 @@ namespace System.Collections.Immutable /// /// The key associated with this node. /// - private readonly TKey _key; + private readonly TKey _key = default!; /// /// The value associated with this node. /// - private readonly TValue _value; + private readonly TValue _value = default!; /// /// A value indicating whether this node has been frozen (made immutable). @@ -49,12 +50,12 @@ namespace System.Collections.Immutable /// /// The left tree. /// - private Node _left; + private Node? _left; /// /// The right tree. /// - private Node _right; + private Node? _right; /// /// Initializes a new instance of the class @@ -109,7 +110,7 @@ namespace System.Collections.Immutable /// /// Gets the left branch of this node. /// - IBinaryTree> IBinaryTree>.Left + IBinaryTree>? IBinaryTree>.Left { get { return _left; } } @@ -117,7 +118,7 @@ namespace System.Collections.Immutable /// /// Gets the right branch of this node. /// - IBinaryTree> IBinaryTree>.Right + IBinaryTree>? IBinaryTree>.Right { get { return _right; } } @@ -130,12 +131,12 @@ namespace System.Collections.Immutable /// /// Gets the left branch of this node. /// - public Node Left { get { return _left; } } + public Node? Left { get { return _left; } } /// /// Gets the left branch of this node. /// - IBinaryTree IBinaryTree.Left + IBinaryTree? IBinaryTree.Left { get { return _left; } } @@ -143,12 +144,12 @@ namespace System.Collections.Immutable /// /// Gets the right branch of this node. /// - public Node Right { get { return _right; } } + public Node? Right { get { return _right; } } /// /// Gets the right branch of this node. /// - IBinaryTree IBinaryTree.Right + IBinaryTree? IBinaryTree.Right { get { return _right; } } @@ -358,7 +359,7 @@ namespace System.Collections.Immutable /// The value. /// True if the key was found. [Pure] - internal bool TryGetValue(TKey key, IComparer keyComparer, out TValue value) + internal bool TryGetValue(TKey key, IComparer keyComparer, [MaybeNullWhen(false)] out TValue value) { Requires.NotNullAllowStructs(key, nameof(key)); Requires.NotNull(keyComparer, nameof(keyComparer)); @@ -366,7 +367,7 @@ namespace System.Collections.Immutable var match = this.Search(key, keyComparer); if (match.IsEmpty) { - value = default(TValue); + value = default(TValue)!; return false; } else @@ -484,8 +485,8 @@ namespace System.Collections.Immutable // If this node is frozen, all its descendants must already be frozen. if (!_frozen) { - _left.Freeze(); - _right.Freeze(); + _left!.Freeze(); + _right!.Freeze(); _frozen = true; } } @@ -502,7 +503,7 @@ namespace System.Collections.Immutable Requires.NotNull(tree, nameof(tree)); Debug.Assert(!tree.IsEmpty); - if (tree._right.IsEmpty) + if (tree._right!.IsEmpty) { return tree; } @@ -521,7 +522,7 @@ namespace System.Collections.Immutable Requires.NotNull(tree, nameof(tree)); Debug.Assert(!tree.IsEmpty); - if (tree._left.IsEmpty) + if (tree._left!.IsEmpty) { return tree; } @@ -540,7 +541,7 @@ namespace System.Collections.Immutable Requires.NotNull(tree, nameof(tree)); Debug.Assert(!tree.IsEmpty); - if (tree._right.IsEmpty) + if (tree._right!.IsEmpty) { return tree; } @@ -559,7 +560,7 @@ namespace System.Collections.Immutable Requires.NotNull(tree, nameof(tree)); Debug.Assert(!tree.IsEmpty); - if (tree._left.IsEmpty) + if (tree._left!.IsEmpty) { return tree; } @@ -579,7 +580,7 @@ namespace System.Collections.Immutable Requires.NotNull(tree, nameof(tree)); Debug.Assert(!tree.IsEmpty); - return tree._right._height - tree._left._height; + return tree._right!._height - tree._left!._height; } /// @@ -621,12 +622,12 @@ namespace System.Collections.Immutable if (IsRightHeavy(tree)) { - return Balance(tree._right) < 0 ? DoubleLeft(tree) : RotateLeft(tree); + return Balance(tree._right!) < 0 ? DoubleLeft(tree) : RotateLeft(tree); } if (IsLeftHeavy(tree)) { - return Balance(tree._left) > 0 ? DoubleRight(tree) : RotateRight(tree); + return Balance(tree._left!) > 0 ? DoubleRight(tree) : RotateRight(tree); } return tree; @@ -689,7 +690,7 @@ namespace System.Collections.Immutable int compareResult = keyComparer.Compare(key, _key); if (compareResult > 0) { - var newRight = _right.SetOrAdd(key, value, keyComparer, valueComparer, overwriteExistingValue, out replacedExistingValue, out mutated); + var newRight = _right!.SetOrAdd(key, value, keyComparer, valueComparer, overwriteExistingValue, out replacedExistingValue, out mutated); if (mutated) { result = this.Mutate(right: newRight); @@ -697,7 +698,7 @@ namespace System.Collections.Immutable } else if (compareResult < 0) { - var newLeft = _left.SetOrAdd(key, value, keyComparer, valueComparer, overwriteExistingValue, out replacedExistingValue, out mutated); + var newLeft = _left!.SetOrAdd(key, value, keyComparer, valueComparer, overwriteExistingValue, out replacedExistingValue, out mutated); if (mutated) { result = this.Mutate(left: newLeft); @@ -714,7 +715,7 @@ namespace System.Collections.Immutable { mutated = true; replacedExistingValue = true; - result = new Node(key, value, _left, _right); + result = new Node(key, value, _left!, _right!); } else { @@ -743,6 +744,7 @@ namespace System.Collections.Immutable } else { + Debug.Assert(_right != null && _left != null); Node result = this; int compare = keyComparer.Compare(key, _key); if (compare == 0) @@ -770,7 +772,7 @@ namespace System.Collections.Immutable // We have two children. Remove the next-highest node and replace // this node with it. var successor = _right; - while (!successor._left.IsEmpty) + while (!successor._left!.IsEmpty) { successor = successor._left; } @@ -808,8 +810,9 @@ namespace System.Collections.Immutable /// The left branch of the mutated node. /// The right branch of the mutated node. /// The mutated (or created) node. - private Node Mutate(Node left = null, Node right = null) + private Node Mutate(Node? left = null, Node? right = null) { + Debug.Assert(_left != null && _right != null); if (_frozen) { return new Node(_key, _value, left ?? _left, right ?? _right); @@ -854,11 +857,11 @@ namespace System.Collections.Immutable } else if (compare > 0) { - return _right.Search(key, keyComparer); + return _right!.Search(key, keyComparer); } else { - return _left.Search(key, keyComparer); + return _left!.Search(key, keyComparer); } } } diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedDictionary_2.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedDictionary_2.cs index f87a5f8..af7c482 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedDictionary_2.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedDictionary_2.cs @@ -51,7 +51,7 @@ namespace System.Collections.Immutable /// /// The key comparer. /// The value comparer. - internal ImmutableSortedDictionary(IComparer keyComparer = null, IEqualityComparer valueComparer = null) + internal ImmutableSortedDictionary(IComparer? keyComparer = null, IEqualityComparer? valueComparer = null) { _keyComparer = keyComparer ?? Comparer.Default; _valueComparer = valueComparer ?? EqualityComparer.Default; @@ -343,7 +343,7 @@ namespace System.Collections.Immutable /// See the interface. /// [Pure] - public ImmutableSortedDictionary WithComparers(IComparer keyComparer, IEqualityComparer valueComparer) + public ImmutableSortedDictionary WithComparers(IComparer? keyComparer, IEqualityComparer? valueComparer) { if (keyComparer == null) { @@ -382,7 +382,7 @@ namespace System.Collections.Immutable /// See the interface. /// [Pure] - public ImmutableSortedDictionary WithComparers(IComparer keyComparer) + public ImmutableSortedDictionary WithComparers(IComparer? keyComparer) { return this.WithComparers(keyComparer, _valueComparer); } @@ -484,10 +484,10 @@ namespace System.Collections.Immutable /// /// See the interface. /// - public bool TryGetValue(TKey key, out TValue value) + public bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value) { Requires.NotNullAllowStructs(key, nameof(key)); - return _root.TryGetValue(key, _keyComparer, out value); + return _root.TryGetValue(key, _keyComparer, out value!); } /// @@ -623,7 +623,7 @@ namespace System.Collections.Immutable /// /// The to use as the key of the element to add. /// The to use as the value of the element to add. - void IDictionary.Add(object key, object value) + void IDictionary.Add(object key, object? value) { throw new NotSupportedException(); } @@ -665,7 +665,7 @@ namespace System.Collections.Immutable /// /// The key. /// - object IDictionary.this[object key] + object? IDictionary.this[object key] { get { return this[(TKey)key]; } set { throw new NotSupportedException(); } @@ -792,7 +792,7 @@ namespace System.Collections.Immutable /// The sequence that may have come from an immutable map. /// Receives the concrete typed value if one can be found. /// true if the cast was successful; false otherwise. - private static bool TryCastToImmutableMap(IEnumerable> sequence, out ImmutableSortedDictionary other) + private static bool TryCastToImmutableMap(IEnumerable> sequence, [NotNullWhen(true)] out ImmutableSortedDictionary? other) { other = sequence as ImmutableSortedDictionary; if (other != null) @@ -800,8 +800,7 @@ namespace System.Collections.Immutable return true; } - var builder = sequence as Builder; - if (builder != null) + if (sequence is Builder builder) { other = builder.ToImmutable(); return true; @@ -881,15 +880,14 @@ namespace System.Collections.Immutable // If the items being added actually come from an ImmutableSortedSet, // and the sort order is equivalent, then there is no value in reconstructing it. - ImmutableSortedDictionary other; + ImmutableSortedDictionary? other; if (TryCastToImmutableMap(items, out other)) { return other.WithComparers(this.KeyComparer, this.ValueComparer); } - var itemsAsDictionary = items as IDictionary; SortedDictionary dictionary; - if (itemsAsDictionary != null) + if (items is IDictionary itemsAsDictionary) { dictionary = new SortedDictionary(itemsAsDictionary, this.KeyComparer); } @@ -905,7 +903,7 @@ namespace System.Collections.Immutable else { TValue value; - if (dictionary.TryGetValue(item.Key, out value)) + if (dictionary.TryGetValue(item.Key, out value!)) { if (!_valueComparer.Equals(value, item.Value)) { diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedSet.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedSet.cs index 000e1ee..7bf8d4f 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedSet.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedSet.cs @@ -32,7 +32,7 @@ namespace System.Collections.Immutable /// The immutable collection. /// [Pure] - public static ImmutableSortedSet Create(IComparer comparer) + public static ImmutableSortedSet Create(IComparer? comparer) { return ImmutableSortedSet.Empty.WithComparer(comparer); } @@ -57,7 +57,7 @@ namespace System.Collections.Immutable /// The item to prepopulate. /// The new immutable collection. [Pure] - public static ImmutableSortedSet Create(IComparer comparer, T item) + public static ImmutableSortedSet Create(IComparer? comparer, T item) { return ImmutableSortedSet.Empty.WithComparer(comparer).Add(item); } @@ -82,7 +82,7 @@ namespace System.Collections.Immutable /// The items to prepopulate. /// The new immutable collection. [Pure] - public static ImmutableSortedSet CreateRange(IComparer comparer, IEnumerable items) + public static ImmutableSortedSet CreateRange(IComparer? comparer, IEnumerable items) { return ImmutableSortedSet.Empty.WithComparer(comparer).Union(items); } @@ -107,7 +107,7 @@ namespace System.Collections.Immutable /// The items to prepopulate. /// The new immutable collection. [Pure] - public static ImmutableSortedSet Create(IComparer comparer, params T[] items) + public static ImmutableSortedSet Create(IComparer? comparer, params T[] items) { return ImmutableSortedSet.Empty.WithComparer(comparer).Union(items); } @@ -132,7 +132,7 @@ namespace System.Collections.Immutable /// The immutable collection. /// [Pure] - public static ImmutableSortedSet.Builder CreateBuilder(IComparer comparer) + public static ImmutableSortedSet.Builder CreateBuilder(IComparer? comparer) { return Create(comparer).ToBuilder(); } @@ -145,10 +145,9 @@ namespace System.Collections.Immutable /// The comparer to use for initializing and adding members to the sorted set. /// An immutable set. [Pure] - public static ImmutableSortedSet ToImmutableSortedSet(this IEnumerable source, IComparer comparer) + public static ImmutableSortedSet ToImmutableSortedSet(this IEnumerable source, IComparer? comparer) { - var existingSet = source as ImmutableSortedSet; - if (existingSet != null) + if (source is ImmutableSortedSet existingSet) { return existingSet.WithComparer(comparer); } diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedSet_1.Builder.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedSet_1.Builder.cs index f392be6..a31de70 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedSet_1.Builder.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedSet_1.Builder.cs @@ -49,7 +49,7 @@ namespace System.Collections.Immutable /// Caches an immutable instance that represents the current state of the collection. /// /// Null if no immutable view has been created for the current version. - private ImmutableSortedSet _immutable; + private ImmutableSortedSet? _immutable; /// /// A number that increments every time the builder changes its contents. @@ -59,7 +59,7 @@ namespace System.Collections.Immutable /// /// The object callers may use to synchronize access to this collection. /// - private object _syncRoot; + private object? _syncRoot; /// /// Initializes a new instance of the class. @@ -128,6 +128,7 @@ namespace System.Collections.Immutable /// Gets the maximum value in the collection, as defined by the comparer. /// /// The maximum value in the set. + [MaybeNull] public T Max { get { return _root.Max; } @@ -137,6 +138,7 @@ namespace System.Collections.Immutable /// Gets the minimum value in the collection, as defined by the comparer. /// /// The minimum value in the set. + [MaybeNull] public T Min { get { return _root.Min; } @@ -492,7 +494,7 @@ namespace System.Collections.Immutable { if (_syncRoot == null) { - Threading.Interlocked.CompareExchange(ref _syncRoot, new object(), null); + Threading.Interlocked.CompareExchange(ref _syncRoot, new object(), null); } return _syncRoot; diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedSet_1.Enumerator.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedSet_1.Enumerator.cs index eced4fe..ae73b59 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedSet_1.Enumerator.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedSet_1.Enumerator.cs @@ -37,7 +37,7 @@ namespace System.Collections.Immutable /// /// The builder being enumerated, if applicable. /// - private readonly Builder _builder; + private readonly Builder? _builder; /// /// A unique ID for this instance of this enumerator. @@ -66,12 +66,12 @@ namespace System.Collections.Immutable /// clr!ArrayStoreCheck /// clr!ObjIsInstanceOf /// - private SecurePooledObject>> _stack; + private SecurePooledObject>>? _stack; /// /// The node currently selected. /// - private Node _current; + private Node? _current; /// /// The version of the builder (when applicable) that is being enumerated. @@ -84,7 +84,7 @@ namespace System.Collections.Immutable /// The root of the set to be enumerated. /// The builder, if applicable. /// true to enumerate the collection in reverse. - internal Enumerator(Node root, Builder builder = null, bool reverse = false) + internal Enumerator(Node root, Builder? builder = null, bool reverse = false) { Requires.NotNull(root, nameof(root)); @@ -129,7 +129,7 @@ namespace System.Collections.Immutable /// /// The current element. /// - object System.Collections.IEnumerator.Current + object? System.Collections.IEnumerator.Current { get { return this.Current; } } @@ -139,13 +139,12 @@ namespace System.Collections.Immutable /// public void Dispose() { - _root = null; + _root = null!; _current = null; - Stack> stack; - if (_stack != null && _stack.TryUse(ref this, out stack)) + if (_stack != null && _stack.TryUse(ref this, out Stack>? stack)) { stack.ClearFastWhenEmpty(); - s_enumeratingStacks.TryAdd(this, _stack); + s_enumeratingStacks.TryAdd(this, _stack!); _stack = null; } } @@ -159,12 +158,12 @@ namespace System.Collections.Immutable this.ThrowIfDisposed(); this.ThrowIfChanged(); - var stack = _stack.Use(ref this); + var stack = _stack!.Use(ref this); if (stack.Count > 0) { Node n = stack.Pop().Value; _current = n; - this.PushNext(_reverse ? n.Left : n.Right); + this.PushNext(_reverse ? n.Left! : n.Right!); return true; } else @@ -183,7 +182,7 @@ namespace System.Collections.Immutable _enumeratingBuilderVersion = _builder != null ? _builder.Version : -1; _current = null; - var stack = _stack.Use(ref this); + var stack = _stack!.Use(ref this); stack.ClearFastWhenEmpty(); this.PushNext(_root); } @@ -224,11 +223,11 @@ namespace System.Collections.Immutable private void PushNext(Node node) { Requires.NotNull(node, nameof(node)); - var stack = _stack.Use(ref this); + var stack = _stack!.Use(ref this); while (!node.IsEmpty) { stack.Push(new RefAsValueType(node)); - node = _reverse ? node.Right : node.Left; + node = _reverse ? node.Right! : node.Left!; } } } diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedSet_1.Node.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedSet_1.Node.cs index ed5a5eb..c08056e 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedSet_1.Node.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedSet_1.Node.cs @@ -25,7 +25,7 @@ namespace System.Collections.Immutable /// /// The key associated with this node. /// - private readonly T _key; + private readonly T _key = default!; /// /// A value indicating whether this node has been frozen (made immutable). @@ -55,12 +55,12 @@ namespace System.Collections.Immutable /// /// The left tree. /// - private Node _left; + private Node? _left; /// /// The right tree. /// - private Node _right; + private Node? _right; /// /// Initializes a new instance of the class @@ -116,7 +116,7 @@ namespace System.Collections.Immutable /// /// Gets the left branch of this node. /// - public Node Left + public Node? Left { get { return _left; } } @@ -124,7 +124,7 @@ namespace System.Collections.Immutable /// /// Gets the left branch of this node. /// - IBinaryTree IBinaryTree.Left + IBinaryTree? IBinaryTree.Left { get { return _left; } } @@ -132,7 +132,7 @@ namespace System.Collections.Immutable /// /// Gets the right branch of this node. /// - public Node Right + public Node? Right { get { return _right; } } @@ -140,7 +140,7 @@ namespace System.Collections.Immutable /// /// Gets the right branch of this node. /// - IBinaryTree IBinaryTree.Right + IBinaryTree? IBinaryTree.Right { get { return _right; } } @@ -148,7 +148,7 @@ namespace System.Collections.Immutable /// /// Gets the left branch of this node. /// - IBinaryTree IBinaryTree.Left + IBinaryTree? IBinaryTree.Left { get { return _left; } } @@ -156,7 +156,7 @@ namespace System.Collections.Immutable /// /// Gets the right branch of this node. /// - IBinaryTree IBinaryTree.Right + IBinaryTree? IBinaryTree.Right { get { return _right; } } @@ -186,17 +186,18 @@ namespace System.Collections.Immutable /// Gets the maximum value in the collection, as defined by the comparer. /// /// The maximum value in the set. + [MaybeNull] internal T Max { get { if (this.IsEmpty) { - return default(T); + return default(T)!; } Node n = this; - while (!n._right.IsEmpty) + while (!n._right!.IsEmpty) { n = n._right; } @@ -209,17 +210,18 @@ namespace System.Collections.Immutable /// Gets the minimum value in the collection, as defined by the comparer. /// /// The minimum value in the set. + [MaybeNull] internal T Min { get { if (this.IsEmpty) { - return default(T); + return default(T)!; } Node n = this; - while (!n._left.IsEmpty) + while (!n._left!.IsEmpty) { n = n._left; } @@ -238,6 +240,7 @@ namespace System.Collections.Immutable get { Requires.Range(index >= 0 && index < this.Count, nameof(index)); + Debug.Assert(_left != null && _right != null); if (index < _left._count) { @@ -262,6 +265,7 @@ namespace System.Collections.Immutable internal ref readonly T ItemRef(int index) { Requires.Range(index >= 0 && index < this.Count, nameof(index)); + Debug.Assert(_left != null && _right != null); if (index < _left._count) { @@ -379,7 +383,7 @@ namespace System.Collections.Immutable int compareResult = comparer.Compare(key, _key); if (compareResult > 0) { - var newRight = _right.Add(key, comparer, out mutated); + var newRight = _right!.Add(key, comparer, out mutated); if (mutated) { result = this.Mutate(right: newRight); @@ -387,7 +391,7 @@ namespace System.Collections.Immutable } else if (compareResult < 0) { - var newLeft = _left.Add(key, comparer, out mutated); + var newLeft = _left!.Add(key, comparer, out mutated); if (mutated) { result = this.Mutate(left: newLeft); @@ -421,6 +425,7 @@ namespace System.Collections.Immutable } else { + Debug.Assert(_left != null && _right != null); Node result = this; int compare = comparer.Compare(key, _key); if (compare == 0) @@ -448,7 +453,7 @@ namespace System.Collections.Immutable // We have two children. Remove the next-highest node and replace // this node with it. var successor = _right; - while (!successor._left.IsEmpty) + while (!successor._left!.IsEmpty) { successor = successor._left; } @@ -502,6 +507,7 @@ namespace System.Collections.Immutable // If this node is frozen, all its descendants must already be frozen. if (!_frozen) { + Debug.Assert(_left != null && _right != null); _left.Freeze(); _right.Freeze(); _frozen = true; @@ -532,11 +538,11 @@ namespace System.Collections.Immutable } else if (compare > 0) { - return _right.Search(key, comparer); + return _right!.Search(key, comparer); } else { - return _left.Search(key, comparer); + return _left!.Search(key, comparer); } } } @@ -558,6 +564,7 @@ namespace System.Collections.Immutable } else { + Debug.Assert(_left != null && _right != null); int compare = comparer.Compare(key, _key); if (compare == 0) { @@ -613,13 +620,13 @@ namespace System.Collections.Immutable Requires.NotNull(tree, nameof(tree)); Debug.Assert(!tree.IsEmpty); - if (tree._right.IsEmpty) + if (tree._right!.IsEmpty) { return tree; } var right = tree._right; - return right.Mutate(left: tree.Mutate(right: right._left)); + return right.Mutate(left: tree.Mutate(right: right._left!)); } /// @@ -632,13 +639,13 @@ namespace System.Collections.Immutable Requires.NotNull(tree, nameof(tree)); Debug.Assert(!tree.IsEmpty); - if (tree._left.IsEmpty) + if (tree._left!.IsEmpty) { return tree; } var left = tree._left; - return left.Mutate(right: tree.Mutate(left: left._right)); + return left.Mutate(right: tree.Mutate(left: left._right!)); } /// @@ -651,7 +658,7 @@ namespace System.Collections.Immutable Requires.NotNull(tree, nameof(tree)); Debug.Assert(!tree.IsEmpty); - if (tree._right.IsEmpty) + if (tree._right!.IsEmpty) { return tree; } @@ -670,7 +677,7 @@ namespace System.Collections.Immutable Requires.NotNull(tree, nameof(tree)); Debug.Assert(!tree.IsEmpty); - if (tree._left.IsEmpty) + if (tree._left!.IsEmpty) { return tree; } @@ -690,7 +697,7 @@ namespace System.Collections.Immutable Requires.NotNull(tree, nameof(tree)); Debug.Assert(!tree.IsEmpty); - return tree._right._height - tree._left._height; + return tree._right!._height - tree._left!._height; } /// @@ -732,12 +739,12 @@ namespace System.Collections.Immutable if (IsRightHeavy(tree)) { - return Balance(tree._right) < 0 ? DoubleLeft(tree) : RotateLeft(tree); + return Balance(tree._right!) < 0 ? DoubleLeft(tree) : RotateLeft(tree); } if (IsLeftHeavy(tree)) { - return Balance(tree._left) > 0 ? DoubleRight(tree) : RotateRight(tree); + return Balance(tree._left!) > 0 ? DoubleRight(tree) : RotateRight(tree); } return tree; @@ -778,8 +785,9 @@ namespace System.Collections.Immutable /// The left branch of the mutated node. /// The right branch of the mutated node. /// The mutated (or created) node. - private Node Mutate(Node left = null, Node right = null) + private Node Mutate(Node? left = null, Node? right = null) { + Debug.Assert(_left != null && _right != null); if (_frozen) { return new Node(_key, left ?? _left, right ?? _right); diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedSet_1.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedSet_1.cs index 6b49ad3..bc1482b 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedSet_1.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedSet_1.cs @@ -50,7 +50,7 @@ namespace System.Collections.Immutable /// Initializes a new instance of the class. /// /// The comparer. - internal ImmutableSortedSet(IComparer comparer = null) + internal ImmutableSortedSet(IComparer? comparer = null) { _root = Node.EmptyNode; _comparer = comparer ?? Comparer.Default; @@ -83,6 +83,7 @@ namespace System.Collections.Immutable /// Gets the maximum value in the collection, as defined by the comparer. /// /// The maximum value in the set. + [MaybeNull] public T Max { get { return _root.Max; } @@ -92,6 +93,7 @@ namespace System.Collections.Immutable /// Gets the minimum value in the collection, as defined by the comparer. /// /// The minimum value in the set. + [MaybeNull] public T Min { get { return _root.Min; } @@ -317,7 +319,7 @@ namespace System.Collections.Immutable { Requires.NotNull(other, nameof(other)); - ImmutableSortedSet immutableSortedSet; + ImmutableSortedSet? immutableSortedSet; if (TryCastToImmutableSortedSet(other, out immutableSortedSet) && immutableSortedSet.KeyComparer == this.KeyComparer) // argument is a compatible immutable sorted set { if (immutableSortedSet.IsEmpty) @@ -354,7 +356,7 @@ namespace System.Collections.Immutable /// See the interface. /// [Pure] - public ImmutableSortedSet WithComparer(IComparer comparer) + public ImmutableSortedSet WithComparer(IComparer? comparer) { if (comparer == null) { @@ -859,7 +861,7 @@ namespace System.Collections.Immutable /// The position into which the new element was inserted, or -1 to indicate that the item was not inserted into the collection, /// /// - int IList.Add(object value) + int IList.Add(object? value) { throw new NotSupportedException(); } @@ -880,9 +882,9 @@ namespace System.Collections.Immutable /// /// true if the is found in the ; otherwise, false. /// - bool IList.Contains(object value) + bool IList.Contains(object? value) { - return this.Contains((T)value); + return this.Contains((T)value!); } /// @@ -892,9 +894,9 @@ namespace System.Collections.Immutable /// /// The index of if found in the list; otherwise, -1. /// - int IList.IndexOf(object value) + int IList.IndexOf(object? value) { - return this.IndexOf((T)value); + return this.IndexOf((T)value!); } /// @@ -903,7 +905,7 @@ namespace System.Collections.Immutable /// The zero-based index at which should be inserted. /// The object to insert into the . /// - void IList.Insert(int index, object value) + void IList.Insert(int index, object? value) { throw new NotSupportedException(); } @@ -913,7 +915,7 @@ namespace System.Collections.Immutable /// /// The object to remove from the . /// - void IList.Remove(object value) + void IList.Remove(object? value) { throw new NotSupportedException(); } @@ -936,7 +938,7 @@ namespace System.Collections.Immutable /// /// The index. /// - object IList.this[int index] + object? IList.this[int index] { get { return this[index]; } set { throw new NotSupportedException(); } @@ -1013,7 +1015,7 @@ namespace System.Collections.Immutable /// /// Discovers an immutable sorted set for a given value, if possible. /// - private static bool TryCastToImmutableSortedSet(IEnumerable sequence, out ImmutableSortedSet other) + private static bool TryCastToImmutableSortedSet(IEnumerable sequence, [NotNullWhen(true)] out ImmutableSortedSet? other) { other = sequence as ImmutableSortedSet; if (other != null) diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableStack_1.Enumerator.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableStack_1.Enumerator.cs index 5e63d90..78f0c53 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableStack_1.Enumerator.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableStack_1.Enumerator.cs @@ -23,7 +23,7 @@ namespace System.Collections.Immutable /// /// The remaining stack not yet enumerated. /// - private ImmutableStack _remainingStack; + private ImmutableStack? _remainingStack; /// /// Initializes a new instance of the struct. @@ -87,7 +87,7 @@ namespace System.Collections.Immutable /// /// The remaining stack not yet enumerated. /// - private ImmutableStack _remainingStack; + private ImmutableStack? _remainingStack; /// /// A flag indicating whether this enumerator has been disposed. @@ -126,7 +126,7 @@ namespace System.Collections.Immutable /// /// Gets the current element. /// - object IEnumerator.Current + object? IEnumerator.Current { get { return this.Current; } } diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableStack_1.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableStack_1.cs index e133cca..55decdb 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableStack_1.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableStack_1.cs @@ -31,12 +31,13 @@ namespace System.Collections.Immutable /// /// The element on the top of the stack. /// - private readonly T _head; + [MaybeNull] + private readonly T _head = default!; /// /// A stack that contains the rest of the elements (under the top element). /// - private readonly ImmutableStack _tail; + private readonly ImmutableStack? _tail; /// /// Initializes a new instance of the class @@ -114,7 +115,7 @@ namespace System.Collections.Immutable throw new InvalidOperationException(SR.InvalidEmptyOperation); } - return _head; + return _head!; } #if !NETSTANDARD1_0 @@ -133,7 +134,7 @@ namespace System.Collections.Immutable throw new InvalidOperationException(SR.InvalidEmptyOperation); } - return ref _head; + return ref _head!; } #endif diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/KeysOrValuesCollectionAccessor.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/KeysOrValuesCollectionAccessor.cs index 93899cb..46c8ab6 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/KeysOrValuesCollectionAccessor.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/KeysOrValuesCollectionAccessor.cs @@ -13,7 +13,7 @@ namespace System.Collections.Immutable /// The type of key in the dictionary. /// The type of value in the dictionary. /// Either TKey or TValue. - internal abstract class KeysOrValuesCollectionAccessor : ICollection, ICollection + internal abstract class KeysOrValuesCollectionAccessor : ICollection, ICollection where TKey : notnull { /// /// The underlying wrapped dictionary. @@ -165,7 +165,7 @@ namespace System.Collections.Immutable /// /// A lightweight collection view over and IEnumerable of keys. /// - internal sealed class KeysCollectionAccessor : KeysOrValuesCollectionAccessor + internal sealed class KeysCollectionAccessor : KeysOrValuesCollectionAccessor where TKey : notnull { /// /// Initializes a new instance of the class. @@ -187,7 +187,7 @@ namespace System.Collections.Immutable /// /// A lightweight collection view over and IEnumerable of values. /// - internal sealed class ValuesCollectionAccessor : KeysOrValuesCollectionAccessor + internal sealed class ValuesCollectionAccessor : KeysOrValuesCollectionAccessor where TKey : notnull { /// /// Initializes a new instance of the class. diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/SecureObjectPool.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/SecureObjectPool.cs index 03fcf1d..68027c2 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/SecureObjectPool.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/SecureObjectPool.cs @@ -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. +using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; using System.Threading; @@ -51,7 +52,7 @@ namespace System.Collections.Immutable } } - public bool TryTake(TCaller caller, out SecurePooledObject item) + public bool TryTake(TCaller caller, out SecurePooledObject? item) { if (caller.PoolUserId != SecureObjectPool.UnassignedId && AllocFreeConcurrentStack>.TryTake(out item)) { @@ -114,7 +115,7 @@ namespace System.Collections.Immutable return _value; } - internal bool TryUse(ref TCaller caller, out T value) + internal bool TryUse(ref TCaller caller, [MaybeNullWhen(false)] out T value) where TCaller : struct, ISecurePooledObjectUser { if (IsOwned(ref caller)) @@ -124,7 +125,7 @@ namespace System.Collections.Immutable } else { - value = default(T); + value = default(T)!; return false; } } diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/SortedInt32KeyNode.Enumerator.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/SortedInt32KeyNode.Enumerator.cs index 7ba0db9..0fbb8bc 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/SortedInt32KeyNode.Enumerator.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/SortedInt32KeyNode.Enumerator.cs @@ -48,12 +48,12 @@ namespace System.Collections.Immutable /// /// The stack to use for enumerating the binary tree. /// - private SecurePooledObject>>> _stack; + private SecurePooledObject>>>? _stack; /// /// The node currently selected. /// - private SortedInt32KeyNode _current; + private SortedInt32KeyNode? _current; /// /// Initializes an structure. @@ -114,13 +114,12 @@ namespace System.Collections.Immutable /// public void Dispose() { - _root = null; + _root = null!; _current = null; - Stack>> stack; - if (_stack != null && _stack.TryUse(ref this, out stack)) + if (_stack != null && _stack.TryUse(ref this, out Stack>>? stack)) { stack.ClearFastWhenEmpty(); - s_enumeratingStacks.TryAdd(this, _stack); + s_enumeratingStacks.TryAdd(this, _stack!); } _stack = null; @@ -141,7 +140,7 @@ namespace System.Collections.Immutable { SortedInt32KeyNode n = stack.Pop().Value; _current = n; - this.PushLeft(n.Right); + this.PushLeft(n.Right!); return true; } } @@ -190,11 +189,11 @@ namespace System.Collections.Immutable private void PushLeft(SortedInt32KeyNode node) { Requires.NotNull(node, nameof(node)); - var stack = _stack.Use(ref this); + var stack = _stack!.Use(ref this); while (!node.IsEmpty) { stack.Push(new RefAsValueType>(node)); - node = node.Left; + node = node.Left!; } } } diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/SortedInt32KeyNode.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/SortedInt32KeyNode.cs index 8d919ef..8161a23 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/SortedInt32KeyNode.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/SortedInt32KeyNode.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.Diagnostics.Contracts; using System.Globalization; @@ -35,7 +36,8 @@ namespace System.Collections.Immutable /// /// The value associated with this node. /// - private readonly TValue _value; + [MaybeNull] + private readonly TValue _value = default!; /// /// A value indicating whether this node has been frozen (made immutable). @@ -54,12 +56,12 @@ namespace System.Collections.Immutable /// /// The left tree. /// - private SortedInt32KeyNode _left; + private SortedInt32KeyNode? _left; /// /// The right tree. /// - private SortedInt32KeyNode _right; + private SortedInt32KeyNode? _right; /// /// Initializes a new instance of the class that is pre-frozen. @@ -108,22 +110,22 @@ namespace System.Collections.Immutable /// /// Gets the left branch of this node. /// - public SortedInt32KeyNode Left { get { return _left; } } + public SortedInt32KeyNode? Left { get { return _left; } } /// /// Gets the right branch of this node. /// - public SortedInt32KeyNode Right { get { return _right; } } + public SortedInt32KeyNode? Right { get { return _right; } } /// /// Gets the left branch of this node. /// - IBinaryTree IBinaryTree.Left { get { return _left; } } + IBinaryTree? IBinaryTree.Left { get { return _left; } } /// /// Gets the right branch of this node. /// - IBinaryTree IBinaryTree.Right { get { return _right; } } + IBinaryTree? IBinaryTree.Right { get { return _right; } } /// /// Gets the number of elements contained by this node and below. @@ -138,7 +140,7 @@ namespace System.Collections.Immutable /// public KeyValuePair Value { - get { return new KeyValuePair(_key, _value); } + get { return new KeyValuePair(_key, _value!); } } /// @@ -198,6 +200,7 @@ namespace System.Collections.Immutable /// The key. /// The value. [Pure] + [return: MaybeNull] internal TValue GetValueOrDefault(int key) { SortedInt32KeyNode node = this; @@ -205,21 +208,21 @@ namespace System.Collections.Immutable { if (node.IsEmpty) { - return default(TValue); + return default(TValue)!; } if (key == node._key) { - return node._value; + return node._value!; } if (key > node._key) { - node = node._right; + node = node._right!; } else { - node = node._left; + node = node._left!; } } } @@ -231,30 +234,30 @@ namespace System.Collections.Immutable /// The value. /// True if the key was found. [Pure] - internal bool TryGetValue(int key, out TValue value) + internal bool TryGetValue(int key, [MaybeNullWhen(false)] out TValue value) { SortedInt32KeyNode node = this; while (true) { if (node.IsEmpty) { - value = default(TValue); + value = default(TValue)!; return false; } if (key == node._key) { - value = node._value; + value = node._value!; return true; } if (key > node._key) { - node = node._right; + node = node._right!; } else { - node = node._left; + node = node._left!; } } } @@ -262,15 +265,15 @@ namespace System.Collections.Immutable /// /// Freezes this node and all descendant nodes so that any mutations require a new instance of the nodes. /// - internal void Freeze(Action> freezeAction = null) + internal void Freeze(Action>? freezeAction = null) { // If this node is frozen, all its descendants must already be frozen. if (!_frozen) { - freezeAction?.Invoke(new KeyValuePair(_key, _value)); + freezeAction?.Invoke(new KeyValuePair(_key, _value!)); - _left.Freeze(freezeAction); - _right.Freeze(freezeAction); + _left!.Freeze(freezeAction); + _right!.Freeze(freezeAction); _frozen = true; } } @@ -285,13 +288,13 @@ namespace System.Collections.Immutable Requires.NotNull(tree, nameof(tree)); Debug.Assert(!tree.IsEmpty); - if (tree._right.IsEmpty) + if (tree._right!.IsEmpty) { return tree; } var right = tree._right; - return right.Mutate(left: tree.Mutate(right: right._left)); + return right.Mutate(left: tree.Mutate(right: right._left!)); } /// @@ -304,13 +307,13 @@ namespace System.Collections.Immutable Requires.NotNull(tree, nameof(tree)); Debug.Assert(!tree.IsEmpty); - if (tree._left.IsEmpty) + if (tree._left!.IsEmpty) { return tree; } var left = tree._left; - return left.Mutate(right: tree.Mutate(left: left._right)); + return left.Mutate(right: tree.Mutate(left: left._right!)); } /// @@ -323,7 +326,7 @@ namespace System.Collections.Immutable Requires.NotNull(tree, nameof(tree)); Debug.Assert(!tree.IsEmpty); - if (tree._right.IsEmpty) + if (tree._right!.IsEmpty) { return tree; } @@ -342,7 +345,7 @@ namespace System.Collections.Immutable Requires.NotNull(tree, nameof(tree)); Debug.Assert(!tree.IsEmpty); - if (tree._left.IsEmpty) + if (tree._left!.IsEmpty) { return tree; } @@ -362,7 +365,7 @@ namespace System.Collections.Immutable Requires.NotNull(tree, nameof(tree)); Debug.Assert(!tree.IsEmpty); - return tree._right._height - tree._left._height; + return tree._right!._height - tree._left!._height; } /// @@ -404,12 +407,12 @@ namespace System.Collections.Immutable if (IsRightHeavy(tree)) { - return Balance(tree._right) < 0 ? DoubleLeft(tree) : RotateLeft(tree); + return Balance(tree._right!) < 0 ? DoubleLeft(tree) : RotateLeft(tree); } if (IsLeftHeavy(tree)) { - return Balance(tree._left) > 0 ? DoubleRight(tree) : RotateRight(tree); + return Balance(tree._left!) > 0 ? DoubleRight(tree) : RotateRight(tree); } return tree; @@ -441,7 +444,7 @@ namespace System.Collections.Immutable SortedInt32KeyNode result = this; if (key > _key) { - var newRight = _right.SetOrAdd(key, value, valueComparer, overwriteExistingValue, out replacedExistingValue, out mutated); + var newRight = _right!.SetOrAdd(key, value, valueComparer, overwriteExistingValue, out replacedExistingValue, out mutated); if (mutated) { result = this.Mutate(right: newRight); @@ -449,7 +452,7 @@ namespace System.Collections.Immutable } else if (key < _key) { - var newLeft = _left.SetOrAdd(key, value, valueComparer, overwriteExistingValue, out replacedExistingValue, out mutated); + var newLeft = _left!.SetOrAdd(key, value, valueComparer, overwriteExistingValue, out replacedExistingValue, out mutated); if (mutated) { result = this.Mutate(left: newLeft); @@ -457,7 +460,7 @@ namespace System.Collections.Immutable } else { - if (valueComparer.Equals(_value, value)) + if (valueComparer.Equals(_value!, value)) { mutated = false; return this; @@ -466,7 +469,7 @@ namespace System.Collections.Immutable { mutated = true; replacedExistingValue = true; - result = new SortedInt32KeyNode(key, value, _left, _right); + result = new SortedInt32KeyNode(key, value, _left!, _right!); } else { @@ -493,6 +496,7 @@ namespace System.Collections.Immutable } else { + Debug.Assert(_right != null && _left != null); SortedInt32KeyNode result = this; if (key == _key) { @@ -519,7 +523,7 @@ namespace System.Collections.Immutable // We have two children. Remove the next-highest node and replace // this node with it. var successor = _right; - while (!successor._left.IsEmpty) + while (!successor._left!.IsEmpty) { successor = successor._left; } @@ -558,11 +562,12 @@ namespace System.Collections.Immutable /// The left branch of the mutated node. /// The right branch of the mutated node. /// The mutated (or created) node. - private SortedInt32KeyNode Mutate(SortedInt32KeyNode left = null, SortedInt32KeyNode right = null) + private SortedInt32KeyNode Mutate(SortedInt32KeyNode? left = null, SortedInt32KeyNode? right = null) { + Debug.Assert(_right != null && _left != null); if (_frozen) { - return new SortedInt32KeyNode(_key, _value, left ?? _left, right ?? _right); + return new SortedInt32KeyNode(_key, _value!, left ?? _left, right ?? _right); } else { diff --git a/src/libraries/System.Collections.Immutable/src/System/Linq/ImmutableArrayExtensions.cs b/src/libraries/System.Collections.Immutable/src/System/Linq/ImmutableArrayExtensions.cs index 8f2f063..b187317 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Linq/ImmutableArrayExtensions.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Linq/ImmutableArrayExtensions.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Collections.Immutable; +using System.Diagnostics.CodeAnalysis; using System.Diagnostics.Contracts; namespace System.Linq @@ -30,7 +31,7 @@ namespace System.Linq // LINQ Select/Where have optimized treatment for arrays. // They also do not modify the source arrays or expose them to modifications. // Therefore we will just apply Select/Where to the underlying this.array array. - return immutableArray.array.Select(selector); + return immutableArray.array!.Select(selector); } /// @@ -60,7 +61,7 @@ namespace System.Linq if (collectionSelector == null || resultSelector == null) { // throw the same exception as would LINQ - return Enumerable.SelectMany(immutableArray, collectionSelector, resultSelector); + return Enumerable.SelectMany(immutableArray, collectionSelector!, resultSelector!); } // This SelectMany overload is used by the C# compiler for a query of the form: @@ -90,7 +91,7 @@ namespace System.Linq // LINQ Select/Where have optimized treatment for arrays. // They also do not modify the source arrays or expose them to modifications. // Therefore we will just apply Select/Where to the underlying this.array array. - return immutableArray.array.Where(predicate); + return immutableArray.array!.Where(predicate); } /// @@ -117,7 +118,7 @@ namespace System.Linq immutableArray.ThrowNullRefIfNotInitialized(); Requires.NotNull(predicate, nameof(predicate)); - foreach (var v in immutableArray.array) + foreach (var v in immutableArray.array!) { if (predicate(v)) { @@ -144,7 +145,7 @@ namespace System.Linq immutableArray.ThrowNullRefIfNotInitialized(); Requires.NotNull(predicate, nameof(predicate)); - foreach (var v in immutableArray.array) + foreach (var v in immutableArray.array!) { if (!predicate(v)) { @@ -161,7 +162,7 @@ namespace System.Linq /// The type of element in the compared array. /// The type of element contained by the collection. [Pure] - public static bool SequenceEqual(this ImmutableArray immutableArray, ImmutableArray items, IEqualityComparer comparer = null) where TDerived : TBase + public static bool SequenceEqual(this ImmutableArray immutableArray, ImmutableArray items, IEqualityComparer? comparer = null) where TDerived : TBase { immutableArray.ThrowNullRefIfNotInitialized(); items.ThrowNullRefIfNotInitialized(); @@ -182,7 +183,7 @@ namespace System.Linq for (int i = 0; i < immutableArray.Length; i++) { - if (!comparer.Equals(immutableArray.array[i], items.array[i])) + if (!comparer.Equals(immutableArray.array![i], items.array![i])) { return false; } @@ -197,7 +198,7 @@ namespace System.Linq /// The type of element in the compared array. /// The type of element contained by the collection. [Pure] - public static bool SequenceEqual(this ImmutableArray immutableArray, IEnumerable items, IEqualityComparer comparer = null) where TDerived : TBase + public static bool SequenceEqual(this ImmutableArray immutableArray, IEnumerable items, IEqualityComparer? comparer = null) where TDerived : TBase { Requires.NotNull(items, nameof(items)); @@ -264,13 +265,14 @@ namespace System.Linq /// /// The type of element contained by the collection. [Pure] + [return: MaybeNull] public static T Aggregate(this ImmutableArray immutableArray, Func func) { Requires.NotNull(func, nameof(func)); if (immutableArray.Length == 0) { - return default(T); + return default(T)!; } var result = immutableArray[0]; @@ -293,7 +295,7 @@ namespace System.Linq Requires.NotNull(func, nameof(func)); var result = seed; - foreach (var v in immutableArray.array) + foreach (var v in immutableArray.array!) { result = func(result, v); } @@ -330,11 +332,12 @@ namespace System.Linq /// /// The type of element contained by the collection. [Pure] + [return: MaybeNull] public static T ElementAtOrDefault(this ImmutableArray immutableArray, int index) { if (index < 0 || index >= immutableArray.Length) { - return default(T); + return default(T)!; } return immutableArray[index]; @@ -349,7 +352,7 @@ namespace System.Linq { Requires.NotNull(predicate, nameof(predicate)); - foreach (var v in immutableArray.array) + foreach (var v in immutableArray.array!) { if (predicate(v)) { @@ -369,11 +372,12 @@ namespace System.Linq [Pure] public static T First(this ImmutableArray immutableArray) { + // In the event of an empty array, generate the same exception // that the linq extension method would. return immutableArray.Length > 0 ? immutableArray[0] - : Enumerable.First(immutableArray.array); + : Enumerable.First(immutableArray.array!); } /// @@ -382,9 +386,10 @@ namespace System.Linq /// The type of element contained by the collection. /// [Pure] + [return: MaybeNull] public static T FirstOrDefault(this ImmutableArray immutableArray) { - return immutableArray.array.Length > 0 ? immutableArray.array[0] : default(T); + return immutableArray.array!.Length > 0 ? immutableArray.array[0] : default(T)!; } /// @@ -392,11 +397,12 @@ namespace System.Linq /// /// The type of element contained by the collection. [Pure] + [return: MaybeNull] public static T FirstOrDefault(this ImmutableArray immutableArray, Func predicate) { Requires.NotNull(predicate, nameof(predicate)); - foreach (var v in immutableArray.array) + foreach (var v in immutableArray.array!) { if (predicate(v)) { @@ -404,7 +410,7 @@ namespace System.Linq } } - return default(T); + return default(T)!; } /// @@ -419,7 +425,7 @@ namespace System.Linq // that the linq extension method would. return immutableArray.Length > 0 ? immutableArray[immutableArray.Length - 1] - : Enumerable.Last(immutableArray.array); + : Enumerable.Last(immutableArray.array!); } /// @@ -449,10 +455,11 @@ namespace System.Linq /// The type of element contained by the collection. /// [Pure] + [return: MaybeNull] public static T LastOrDefault(this ImmutableArray immutableArray) { immutableArray.ThrowNullRefIfNotInitialized(); - return immutableArray.array.LastOrDefault(); + return immutableArray.array!.LastOrDefault()!; } /// @@ -460,6 +467,7 @@ namespace System.Linq /// /// The type of element contained by the collection. [Pure] + [return: MaybeNull] public static T LastOrDefault(this ImmutableArray immutableArray, Func predicate) { Requires.NotNull(predicate, nameof(predicate)); @@ -472,7 +480,7 @@ namespace System.Linq } } - return default(T); + return default(T)!; } /// @@ -484,7 +492,7 @@ namespace System.Linq public static T Single(this ImmutableArray immutableArray) { immutableArray.ThrowNullRefIfNotInitialized(); - return immutableArray.array.Single(); + return immutableArray.array!.Single(); } /// @@ -497,8 +505,8 @@ namespace System.Linq Requires.NotNull(predicate, nameof(predicate)); var first = true; - var result = default(T); - foreach (var v in immutableArray.array) + var result = default(T)!; + foreach (var v in immutableArray.array!) { if (predicate(v)) { @@ -526,10 +534,11 @@ namespace System.Linq /// The type of element contained by the collection. /// [Pure] + [return: MaybeNull] public static T SingleOrDefault(this ImmutableArray immutableArray) { immutableArray.ThrowNullRefIfNotInitialized(); - return immutableArray.array.SingleOrDefault(); + return immutableArray.array!.SingleOrDefault()!; } /// @@ -537,13 +546,14 @@ namespace System.Linq /// /// The type of element contained by the collection. [Pure] + [return: MaybeNull] public static T SingleOrDefault(this ImmutableArray immutableArray, Func predicate) { Requires.NotNull(predicate, nameof(predicate)); var first = true; - var result = default(T); - foreach (var v in immutableArray.array) + var result = default(T)!; + foreach (var v in immutableArray.array!) { if (predicate(v)) { @@ -569,7 +579,7 @@ namespace System.Linq /// The key selector. /// The newly initialized dictionary. [Pure] - public static Dictionary ToDictionary(this ImmutableArray immutableArray, Func keySelector) + public static Dictionary ToDictionary(this ImmutableArray immutableArray, Func keySelector) where TKey : notnull { return ToDictionary(immutableArray, keySelector, EqualityComparer.Default); } @@ -585,7 +595,7 @@ namespace System.Linq /// The element selector. /// The newly initialized dictionary. [Pure] - public static Dictionary ToDictionary(this ImmutableArray immutableArray, Func keySelector, Func elementSelector) + public static Dictionary ToDictionary(this ImmutableArray immutableArray, Func keySelector, Func elementSelector) where TKey : notnull { return ToDictionary(immutableArray, keySelector, elementSelector, EqualityComparer.Default); } @@ -600,7 +610,7 @@ namespace System.Linq /// The comparer to initialize the dictionary with. /// The newly initialized dictionary. [Pure] - public static Dictionary ToDictionary(this ImmutableArray immutableArray, Func keySelector, IEqualityComparer comparer) + public static Dictionary ToDictionary(this ImmutableArray immutableArray, Func keySelector, IEqualityComparer? comparer) where TKey : notnull { Requires.NotNull(keySelector, nameof(keySelector)); @@ -625,13 +635,13 @@ namespace System.Linq /// The comparer to initialize the dictionary with. /// The newly initialized dictionary. [Pure] - public static Dictionary ToDictionary(this ImmutableArray immutableArray, Func keySelector, Func elementSelector, IEqualityComparer comparer) + public static Dictionary ToDictionary(this ImmutableArray immutableArray, Func keySelector, Func elementSelector, IEqualityComparer? comparer) where TKey : notnull { Requires.NotNull(keySelector, nameof(keySelector)); Requires.NotNull(elementSelector, nameof(elementSelector)); var result = new Dictionary(immutableArray.Length, comparer); - foreach (var v in immutableArray.array) + foreach (var v in immutableArray.array!) { result.Add(keySelector(v), elementSelector(v)); } @@ -649,9 +659,9 @@ namespace System.Linq public static T[] ToArray(this ImmutableArray immutableArray) { immutableArray.ThrowNullRefIfNotInitialized(); - if (immutableArray.array.Length == 0) + if (immutableArray.array!.Length == 0) { - return ImmutableArray.Empty.array; + return ImmutableArray.Empty.array!; } return (T[])immutableArray.array.Clone(); @@ -682,11 +692,12 @@ namespace System.Linq /// Returns the first element in the collection, or the default value if the collection is empty. /// [Pure] + [return: MaybeNull] public static T FirstOrDefault(this ImmutableArray.Builder builder) { Requires.NotNull(builder, nameof(builder)); - return builder.Any() ? builder[0] : default(T); + return builder.Any() ? builder[0] : default(T)!; } /// @@ -710,11 +721,12 @@ namespace System.Linq /// Returns the last element in the collection, or the default value if the collection is empty. /// [Pure] + [return: MaybeNull] public static T LastOrDefault(this ImmutableArray.Builder builder) { Requires.NotNull(builder, nameof(builder)); - return builder.Any() ? builder[builder.Count - 1] : default(T); + return builder.Any() ? builder[builder.Count - 1] : default(T)!; } /// @@ -736,7 +748,7 @@ namespace System.Linq Func> collectionSelector, Func resultSelector) { - foreach (TSource item in immutableArray.array) + foreach (TSource item in immutableArray.array!) { foreach (TCollection result in collectionSelector(item)) { diff --git a/src/libraries/System.Collections.Immutable/src/Validation/Requires.cs b/src/libraries/System.Collections.Immutable/src/Validation/Requires.cs index c936f96..06e65df 100644 --- a/src/libraries/System.Collections.Immutable/src/Validation/Requires.cs +++ b/src/libraries/System.Collections.Immutable/src/Validation/Requires.cs @@ -22,7 +22,7 @@ namespace System.Collections.Immutable /// The name of the parameter to include in any thrown exception. /// Thrown if is null [DebuggerStepThrough] - public static void NotNull([ValidatedNotNull]T value, string parameterName) + public static void NotNull([ValidatedNotNull]T value, string? parameterName) where T : class // ensures value-types aren't passed to a null checking method { if (value == null) @@ -40,7 +40,7 @@ namespace System.Collections.Immutable /// The value of the parameter. /// Thrown if is null [DebuggerStepThrough] - public static T NotNullPassthrough([ValidatedNotNull]T value, string parameterName) + public static T NotNullPassthrough([ValidatedNotNull]T value, string? parameterName) where T : class // ensures value-types aren't passed to a null checking method { NotNull(value, parameterName); @@ -59,7 +59,7 @@ namespace System.Collections.Immutable /// may or may not be a class, but certainly cannot be null. /// [DebuggerStepThrough] - public static void NotNullAllowStructs([ValidatedNotNull]T value, string parameterName) + public static void NotNullAllowStructs([ValidatedNotNull]T value, string? parameterName) { if (null == value) { @@ -72,7 +72,7 @@ namespace System.Collections.Immutable /// /// The name of the parameter that was null. [DebuggerStepThrough] - private static void FailArgumentNullException(string parameterName) + private static void FailArgumentNullException(string? parameterName) { // Separating out this throwing operation helps with inlining of the caller throw new ArgumentNullException(parameterName); @@ -82,7 +82,7 @@ namespace System.Collections.Immutable /// Throws an if a condition does not evaluate to true. /// [DebuggerStepThrough] - public static void Range(bool condition, string parameterName, string message = null) + public static void Range(bool condition, string? parameterName, string? message = null) { if (!condition) { @@ -94,7 +94,7 @@ namespace System.Collections.Immutable /// Throws an . /// [DebuggerStepThrough] - public static void FailRange(string parameterName, string message = null) + public static void FailRange(string? parameterName, string? message = null) { if (string.IsNullOrEmpty(message)) { @@ -110,7 +110,7 @@ namespace System.Collections.Immutable /// Throws an if a condition does not evaluate to true. /// [DebuggerStepThrough] - public static void Argument(bool condition, string parameterName, string message) + public static void Argument(bool condition, string? parameterName, string? message) { if (!condition) { @@ -142,7 +142,7 @@ namespace System.Collections.Immutable { // separating out this throwing helps with inlining of the caller, especially // due to the retrieval of the type's name - throw new ObjectDisposedException(disposed.GetType().FullName); + throw new ObjectDisposedException(disposed!.GetType().FullName); } } }