From ae5f6831125e177003ecc07762c87405e6fafb70 Mon Sep 17 00:00:00 2001 From: Julien Couvreur Date: Sat, 8 Feb 2020 13:02:15 -0800 Subject: [PATCH 01/12] Help compiler enforce nullability annotations --- NuGet.config | 2 ++ eng/Versions.props | 1 + .../src/System/Environment.CoreCLR.cs | 1 + .../Collections/Generic/ReferenceEqualityComparer.cs | 3 ++- .../Collections/Concurrent/ConcurrentDictionary.cs | 2 ++ .../Collections/Immutable/IImmutableListQueries.cs | 3 +++ .../Collections/Immutable/ImmutableHashSet_1.cs | 12 ++++++------ .../Collections/Generic/HashSetEqualityComparer.cs | 2 +- .../Collections/Generic/SortedSet.TreeSubSet.cs | 2 +- .../src/System/Collections/Generic/SortedSet.cs | 2 +- .../Collections/Generic/SortedSetEqualityComparer.cs | 4 ++-- .../Primitives/ComposablePartDefinition.cs | 4 +++- .../Composition/ReflectionModel/ImportType.cs | 6 +++++- .../ReflectionComposablePartDefinition.cs | 4 ++-- .../src/System/Dynamic/Utils/CollectionExtensions.cs | 2 +- .../Linq/Expressions/Interpreter/LightCompiler.cs | 6 +++--- .../Parallel/Partitioning/HashRepartitionStream.cs | 4 ++-- .../Parallel/QueryOperators/Unary/ForAllOperator.cs | 5 ++--- .../src/System/Linq/Parallel/Utils/HashLookup.cs | 2 +- .../System/Linq/Parallel/Utils/ReverseComparer.cs | 3 ++- .../Linq/Parallel/Utils/WrapperEqualityComparer.cs | 2 +- .../src/System/Linq/Partition.SpeedOpt.cs | 1 - .../System.Private.CoreLib/src/System/Action.cs | 4 +++- .../src/System/Collections/Generic/Comparer.cs | 2 +- .../System/Collections/Generic/IEqualityComparer.cs | 2 +- .../src/System/Diagnostics/DebugProvider.cs | 5 +++++ .../System.Private.CoreLib/src/System/HashCode.cs | 3 ++- .../System/Runtime/InteropServices/MemoryMarshal.cs | 4 ++-- .../src/System/SpanHelpers.BinarySearch.cs | 3 ++- .../Internal/Utilities/ByteSequenceComparer.cs | 5 +++-- .../Reflection/TypeLoading/General/RoAssemblyName.cs | 5 +++-- .../src/System/Resources/__FastResourceComparer.cs | 7 +++---- .../ref/System.Runtime.Extensions.cs | 6 +++--- .../Formatters/Binary/ObjectProgress.cs | 4 ++-- src/libraries/System.Runtime/ref/System.Runtime.cs | 8 ++++---- .../Serialization/ReferenceEqualsEqualityComparer.cs | 3 ++- .../System/Text/Json/ThrowHelper.Serialization.cs | 1 - .../src/System/TypeIdentifier.cs | 4 +++- 38 files changed, 83 insertions(+), 56 deletions(-) diff --git a/NuGet.config b/NuGet.config index b656e4d9162cef..61f74a4f9f56ba 100644 --- a/NuGet.config +++ b/NuGet.config @@ -17,6 +17,8 @@ + + diff --git a/eng/Versions.props b/eng/Versions.props index 131c3ff9ab7b26..e7e9c1d44d2fb3 100644 --- a/eng/Versions.props +++ b/eng/Versions.props @@ -57,6 +57,7 @@ + 3.6.0-dev-20110-01 5.0.0-beta.20105.2 5.0.0-beta.20105.2 5.0.0-beta.20105.2 diff --git a/src/coreclr/src/System.Private.CoreLib/src/System/Environment.CoreCLR.cs b/src/coreclr/src/System.Private.CoreLib/src/System/Environment.CoreCLR.cs index 59b2bd9d79040b..f141b1e9cfeb73 100644 --- a/src/coreclr/src/System.Private.CoreLib/src/System/Environment.CoreCLR.cs +++ b/src/coreclr/src/System.Private.CoreLib/src/System/Environment.CoreCLR.cs @@ -16,6 +16,7 @@ public static partial class Environment // Terminates this process with the given exit code. [DllImport(RuntimeHelpers.QCall, CharSet = CharSet.Unicode)] + [DoesNotReturn] private static extern void _Exit(int exitCode); [DoesNotReturn] diff --git a/src/libraries/Common/src/System/Collections/Generic/ReferenceEqualityComparer.cs b/src/libraries/Common/src/System/Collections/Generic/ReferenceEqualityComparer.cs index a21c8d894b12d5..1e88596d17f776 100644 --- a/src/libraries/Common/src/System/Collections/Generic/ReferenceEqualityComparer.cs +++ b/src/libraries/Common/src/System/Collections/Generic/ReferenceEqualityComparer.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; namespace System.Collections.Generic @@ -15,7 +16,7 @@ private ReferenceEqualityComparer() { } - public bool Equals(T x, T y) + public bool Equals([AllowNull] T x, [AllowNull] T y) { return ReferenceEquals(x, y); } diff --git a/src/libraries/System.Collections.Concurrent/src/System/Collections/Concurrent/ConcurrentDictionary.cs b/src/libraries/System.Collections.Concurrent/src/System/Collections/Concurrent/ConcurrentDictionary.cs index ff155065feee20..4ef5ecdf2927a3 100644 --- a/src/libraries/System.Collections.Concurrent/src/System/Collections/Concurrent/ConcurrentDictionary.cs +++ b/src/libraries/System.Collections.Concurrent/src/System/Collections/Concurrent/ConcurrentDictionary.cs @@ -918,11 +918,13 @@ public TValue this[TKey key] // as these are uncommonly needed and when inlined are observed to prevent the inlining // of important methods like TryGetValue and ContainsKey. + [DoesNotReturn] private static void ThrowKeyNotFoundException(object key) { throw new KeyNotFoundException(SR.Format(SR.Arg_KeyNotFoundWithKey, key.ToString())); } + [DoesNotReturn] private static void ThrowKeyNullException() { throw new ArgumentNullException("key"); 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 36bcbe5e0e0021..0a6e1f904d34ad 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 @@ -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 { @@ -120,6 +121,7 @@ internal interface IImmutableListQueries : IReadOnlyList /// The first element that matches the conditions defined by the specified predicate, /// if found; otherwise, the default value for type . /// + [return: MaybeNull] T Find(Predicate match); /// @@ -193,6 +195,7 @@ internal interface IImmutableListQueries : IReadOnlyList /// The last element that matches the conditions defined by the specified predicate, /// if found; otherwise, the default value for type . /// + [return: MaybeNull] T FindLast(Predicate match); /// 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 8fc536af27da4a..fee4c0563059de 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 @@ -221,7 +221,7 @@ public ImmutableHashSet Remove(T item) [Pure] public bool TryGetValue(T equalValue, out T actualValue) { - int hashCode = _equalityComparer.GetHashCode(equalValue); + int hashCode = _equalityComparer.GetHashCode(equalValue!); HashBucket bucket; if (_root.TryGetValue(hashCode, out bucket)) { @@ -639,7 +639,7 @@ private static bool IsSupersetOf(IEnumerable other, MutationInput origin) private static MutationResult Add(T item, MutationInput origin) { OperationResult result; - int hashCode = origin.EqualityComparer.GetHashCode(item); + int hashCode = origin.EqualityComparer.GetHashCode(item!); HashBucket bucket = origin.Root.GetValueOrDefault(hashCode); var newBucket = bucket.Add(item, origin.EqualityComparer, out result); if (result == OperationResult.NoChangeRequired) @@ -658,7 +658,7 @@ private static MutationResult Add(T item, MutationInput origin) private static MutationResult Remove(T item, MutationInput origin) { var result = OperationResult.NoChangeRequired; - int hashCode = origin.EqualityComparer.GetHashCode(item); + int hashCode = origin.EqualityComparer.GetHashCode(item!); HashBucket bucket; var newRoot = origin.Root; if (origin.Root.TryGetValue(hashCode, out bucket)) @@ -680,7 +680,7 @@ private static MutationResult Remove(T item, MutationInput origin) /// private static bool Contains(T item, MutationInput origin) { - int hashCode = origin.EqualityComparer.GetHashCode(item); + int hashCode = origin.EqualityComparer.GetHashCode(item!); HashBucket bucket; if (origin.Root.TryGetValue(hashCode, out bucket)) { @@ -701,7 +701,7 @@ private static MutationResult Union(IEnumerable other, MutationInput origin) var newRoot = origin.Root; foreach (var item in other.GetEnumerableDisposable()) { - int hashCode = origin.EqualityComparer.GetHashCode(item); + int hashCode = origin.EqualityComparer.GetHashCode(item!); HashBucket bucket = newRoot.GetValueOrDefault(hashCode); OperationResult result; var newBucket = bucket.Add(item, origin.EqualityComparer, out result); @@ -812,7 +812,7 @@ private static MutationResult Except(IEnumerable other, IEqualityComparer var newRoot = root; foreach (var item in other.GetEnumerableDisposable()) { - int hashCode = equalityComparer.GetHashCode(item); + int hashCode = equalityComparer.GetHashCode(item!); HashBucket bucket; if (newRoot.TryGetValue(hashCode, out bucket)) { diff --git a/src/libraries/System.Collections/src/System/Collections/Generic/HashSetEqualityComparer.cs b/src/libraries/System.Collections/src/System/Collections/Generic/HashSetEqualityComparer.cs index 5cfae13f3f31ec..5976e9025ca805 100644 --- a/src/libraries/System.Collections/src/System/Collections/Generic/HashSetEqualityComparer.cs +++ b/src/libraries/System.Collections/src/System/Collections/Generic/HashSetEqualityComparer.cs @@ -30,7 +30,7 @@ public int GetHashCode(HashSet? obj) { foreach (T t in obj) { - hashCode = hashCode ^ (_comparer.GetHashCode(t) & 0x7FFFFFFF); + hashCode = hashCode ^ (_comparer.GetHashCode(t!) & 0x7FFFFFFF); // TODO2 } } // else returns hashcode of 0 for null hashsets return hashCode; diff --git a/src/libraries/System.Collections/src/System/Collections/Generic/SortedSet.TreeSubSet.cs b/src/libraries/System.Collections/src/System/Collections/Generic/SortedSet.TreeSubSet.cs index bcbc46e8b7042b..4611785eb9ed80 100644 --- a/src/libraries/System.Collections/src/System/Collections/Generic/SortedSet.TreeSubSet.cs +++ b/src/libraries/System.Collections/src/System/Collections/Generic/SortedSet.TreeSubSet.cs @@ -341,7 +341,7 @@ internal override int TotalCount() // This passes functionality down to the underlying tree, clipping edges if necessary // There's nothing gained by having a nested subset. May as well draw it from the base // Cannot increase the bounds of the subset, can only decrease it - public override SortedSet GetViewBetween(T lowerValue, T upperValue) + public override SortedSet GetViewBetween([AllowNull] T lowerValue, [AllowNull] T upperValue) { if (_lBoundActive && Comparer.Compare(_min, lowerValue) > 0) { diff --git a/src/libraries/System.Collections/src/System/Collections/Generic/SortedSet.cs b/src/libraries/System.Collections/src/System/Collections/Generic/SortedSet.cs index d82c4052aa86ec..c896834c997103 100644 --- a/src/libraries/System.Collections/src/System/Collections/Generic/SortedSet.cs +++ b/src/libraries/System.Collections/src/System/Collections/Generic/SortedSet.cs @@ -806,7 +806,7 @@ public static IEqualityComparer> CreateSetComparer(IEqualityCompare /// The second set. /// The fallback comparer to use if the sets do not have equal comparers. /// true if the sets have equal contents; otherwise, false. - internal static bool SortedSetEquals(SortedSet set1, SortedSet set2, IComparer comparer) + internal static bool SortedSetEquals(SortedSet? set1, SortedSet? set2, IComparer comparer) { if (set1 == null) { diff --git a/src/libraries/System.Collections/src/System/Collections/Generic/SortedSetEqualityComparer.cs b/src/libraries/System.Collections/src/System/Collections/Generic/SortedSetEqualityComparer.cs index e0e59ff8704c95..8de2cd33b15b3f 100644 --- a/src/libraries/System.Collections/src/System/Collections/Generic/SortedSetEqualityComparer.cs +++ b/src/libraries/System.Collections/src/System/Collections/Generic/SortedSetEqualityComparer.cs @@ -27,7 +27,7 @@ private SortedSetEqualityComparer(IComparer? comparer, IEqualityComparer? } // Use _comparer to keep equals properties intact; don't want to choose one of the comparers. - public bool Equals(SortedSet x, SortedSet y) => SortedSet.SortedSetEquals(x, y, _comparer); + public bool Equals(SortedSet? x, SortedSet? y) => SortedSet.SortedSetEquals(x, y, _comparer); // IMPORTANT: this part uses the fact that GetHashCode() is consistent with the notion of equality in the set. public int GetHashCode(SortedSet obj) @@ -37,7 +37,7 @@ public int GetHashCode(SortedSet obj) { foreach (T t in obj) { - hashCode = hashCode ^ (_memberEqualityComparer.GetHashCode(t) & 0x7FFFFFFF); + hashCode = hashCode ^ (_memberEqualityComparer.GetHashCode(t!) & 0x7FFFFFFF); // TODO2 } } // Returns 0 for null sets. diff --git a/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/Primitives/ComposablePartDefinition.cs b/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/Primitives/ComposablePartDefinition.cs index 039e4642888196..395c19d1c5ee92 100644 --- a/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/Primitives/ComposablePartDefinition.cs +++ b/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/Primitives/ComposablePartDefinition.cs @@ -140,10 +140,12 @@ internal virtual bool TryGetExports(ImportDefinition definition, [NotNullWhen(tr if (multipleExports != null) { multipleMatches = multipleExports; + // TODO2 singleMatch dould be null when returning true + singleMatch = null!; } else { - singleMatch = singleExport; + singleMatch = singleExport!; } return true; } diff --git a/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/ReflectionModel/ImportType.cs b/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/ReflectionModel/ImportType.cs index ebc0705dd8cde5..b88ed6b6b476ed 100644 --- a/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/ReflectionModel/ImportType.cs +++ b/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/ReflectionModel/ImportType.cs @@ -172,7 +172,7 @@ private static bool IsLazyGenericType(Type genericType) private static bool TryGetCastFunction(Type genericType, bool isOpenGeneric, Type[] arguments, [NotNullWhen(true)] out Func? castFunction) { - castFunction = null; + castFunction = null!; // TODO2 if (genericType == LazyOfTType) { @@ -180,6 +180,7 @@ private static bool TryGetCastFunction(Type genericType, bool isOpenGeneric, Typ { castFunction = ExportServices.CreateStronglyTypedLazyFactory(arguments[0].UnderlyingSystemType, null); } + // castFunction return true; } @@ -189,6 +190,7 @@ private static bool TryGetCastFunction(Type genericType, bool isOpenGeneric, Typ { castFunction = ExportServices.CreateStronglyTypedLazyFactory(arguments[0].UnderlyingSystemType, arguments[1].UnderlyingSystemType); } + // castFunction return true; } @@ -200,6 +202,7 @@ private static bool TryGetCastFunction(Type genericType, bool isOpenGeneric, Typ { castFunction = new ExportFactoryCreator(genericType).CreateStronglyTypedExportFactoryFactory(arguments[0].UnderlyingSystemType, null); } + // castFunction return true; } else if (arguments.Length == 2) @@ -208,6 +211,7 @@ private static bool TryGetCastFunction(Type genericType, bool isOpenGeneric, Typ { castFunction = new ExportFactoryCreator(genericType).CreateStronglyTypedExportFactoryFactory(arguments[0].UnderlyingSystemType, arguments[1].UnderlyingSystemType); } + // castFunction return true; } else diff --git a/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/ReflectionModel/ReflectionComposablePartDefinition.cs b/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/ReflectionModel/ReflectionComposablePartDefinition.cs index 5cce60871db19b..f353480946ca05 100644 --- a/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/ReflectionModel/ReflectionComposablePartDefinition.cs +++ b/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/ReflectionModel/ReflectionComposablePartDefinition.cs @@ -154,11 +154,11 @@ public override ComposablePart CreatePart() return null; } - internal override bool TryGetExports(ImportDefinition definition, out Tuple? singleMatch, out IEnumerable>? multipleMatches) + internal override bool TryGetExports(ImportDefinition definition, [NotNullWhen(true)] out Tuple? singleMatch, out IEnumerable>? multipleMatches) { if (this.IsGeneric()) { - singleMatch = null; + singleMatch = null!; // TODO2 multipleMatches = null; List>? exports = null; diff --git a/src/libraries/System.Linq.Expressions/src/System/Dynamic/Utils/CollectionExtensions.cs b/src/libraries/System.Linq.Expressions/src/System/Dynamic/Utils/CollectionExtensions.cs index cd655d9f0366cc..9462c74680e8e7 100644 --- a/src/libraries/System.Linq.Expressions/src/System/Dynamic/Utils/CollectionExtensions.cs +++ b/src/libraries/System.Linq.Expressions/src/System/Dynamic/Utils/CollectionExtensions.cs @@ -86,7 +86,7 @@ public static int ListHashCode(this ReadOnlyCollection list) int h = 6551; foreach (T t in list) { - h ^= (h << 5) ^ cmp.GetHashCode(t); + h ^= (h << 5) ^ cmp.GetHashCode(t!); } return h; } diff --git a/src/libraries/System.Linq.Expressions/src/System/Linq/Expressions/Interpreter/LightCompiler.cs b/src/libraries/System.Linq.Expressions/src/System/Linq/Expressions/Interpreter/LightCompiler.cs index 61520cc3599251..d518fd9209d234 100644 --- a/src/libraries/System.Linq.Expressions/src/System/Linq/Expressions/Interpreter/LightCompiler.cs +++ b/src/libraries/System.Linq.Expressions/src/System/Linq/Expressions/Interpreter/LightCompiler.cs @@ -108,7 +108,7 @@ internal bool HasHandler(InterpretedFrame frame, Exception exception, [NotNullWh // Unreachable. // Want to assert that this case isn't hit, but an assertion failure here will be eaten because // we are in an exception filter. Therefore return true here and assert in the catch block. - handler = null; + handler = null!; unwrappedException = exception; return true; } @@ -215,9 +215,9 @@ internal sealed class DebugInfo private class DebugInfoComparer : IComparer { //We allow comparison between int and DebugInfo here - int IComparer.Compare(DebugInfo d1, DebugInfo d2) + int IComparer.Compare([AllowNull] DebugInfo d1, [AllowNull] DebugInfo d2) { - if (d1.Index > d2.Index) return 1; + if (d1!.Index > d2!.Index) return 1; // TODO2 else if (d1.Index == d2.Index) return 0; else return -1; } diff --git a/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/Partitioning/HashRepartitionStream.cs b/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/Partitioning/HashRepartitionStream.cs index 0314d2bac60742..729b125b650467 100644 --- a/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/Partitioning/HashRepartitionStream.cs +++ b/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/Partitioning/HashRepartitionStream.cs @@ -93,7 +93,7 @@ internal int GetHashCode(TInputOutput element) (HashCodeMask & (_elementComparer == null ? (element == null ? NULL_ELEMENT_HASH_CODE : element.GetHashCode()) : - _elementComparer.GetHashCode(element))) + _elementComparer.GetHashCode(element!))) % _distributionMod; } @@ -103,7 +103,7 @@ internal int GetHashCode(THashKey key) (HashCodeMask & (_keyComparer == null ? (key == null ? NULL_ELEMENT_HASH_CODE : key.GetHashCode()) : - _keyComparer.GetHashCode(key))) % _distributionMod; + _keyComparer.GetHashCode(key!))) % _distributionMod; } } } diff --git a/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Unary/ForAllOperator.cs b/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Unary/ForAllOperator.cs index 116170fd1d9de1..c50b7fc481c033 100644 --- a/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Unary/ForAllOperator.cs +++ b/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/QueryOperators/Unary/ForAllOperator.cs @@ -147,7 +147,7 @@ internal ForAllEnumerator(QueryOperatorEnumerator source, Action comparer) _comparer = comparer; } - public int Compare(T x, T y) + public int Compare([AllowNull] T x, [AllowNull] T y) { return _comparer.Compare(y, x); } diff --git a/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/Utils/WrapperEqualityComparer.cs b/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/Utils/WrapperEqualityComparer.cs index 4f447720e260c6..f7f4f047f25638 100644 --- a/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/Utils/WrapperEqualityComparer.cs +++ b/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/Utils/WrapperEqualityComparer.cs @@ -43,7 +43,7 @@ public bool Equals(Wrapper x, Wrapper y) public int GetHashCode(Wrapper x) { Debug.Assert(_comparer != null); - return _comparer.GetHashCode(x.Value); + return _comparer.GetHashCode(x.Value!); } } } diff --git a/src/libraries/System.Linq/src/System/Linq/Partition.SpeedOpt.cs b/src/libraries/System.Linq/src/System/Linq/Partition.SpeedOpt.cs index 4c59749a7dea15..2f61a5d61670d9 100644 --- a/src/libraries/System.Linq/src/System/Linq/Partition.SpeedOpt.cs +++ b/src/libraries/System.Linq/src/System/Linq/Partition.SpeedOpt.cs @@ -36,7 +36,6 @@ private EmptyPartition() public bool MoveNext() => false; [ExcludeFromCodeCoverage] // Shouldn't be called, and as undefined can return or throw anything anyway. - [MaybeNull] public TElement Current => default!; [ExcludeFromCodeCoverage] // Shouldn't be called, and as undefined can return or throw anything anyway. diff --git a/src/libraries/System.Private.CoreLib/src/System/Action.cs b/src/libraries/System.Private.CoreLib/src/System/Action.cs index 54ca7aaf5323d5..1c4771a1b64ff7 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Action.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Action.cs @@ -2,6 +2,8 @@ // 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; + namespace System { public delegate void Action(); @@ -24,7 +26,7 @@ namespace System public delegate TResult Func(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7); public delegate TResult Func(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8); - public delegate int Comparison(T x, T y); + public delegate int Comparison([AllowNull] T x, [AllowNull] T y); public delegate TOutput Converter(TInput input); diff --git a/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/Comparer.cs b/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/Comparer.cs index ec2535e2010ec7..31cc4ece91b1d1 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/Comparer.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/Comparer.cs @@ -43,7 +43,7 @@ public ComparisonComparer(Comparison comparison) _comparison = comparison; } - public override int Compare(T x, T y) + public override int Compare([AllowNull] T x, [AllowNull] T y) { return _comparison(x, y); } diff --git a/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/IEqualityComparer.cs b/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/IEqualityComparer.cs index 61a6bd1e7422f8..29fde08191e2ed 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/IEqualityComparer.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/IEqualityComparer.cs @@ -8,7 +8,7 @@ namespace System.Collections.Generic { // The generic IEqualityComparer interface implements methods to if check two objects are equal // and generate Hashcode for an object. - // It is use in Dictionary class. + // It is used in Dictionary class. public interface IEqualityComparer { bool Equals([AllowNull] T x, [AllowNull] T y); diff --git a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/DebugProvider.cs b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/DebugProvider.cs index cf4dfe1effdb81..6a695a0fd93869 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/DebugProvider.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/DebugProvider.cs @@ -5,6 +5,8 @@ // Do not remove this, it is needed to retain calls to these conditional methods in release builds #define DEBUG +using System.Diagnostics.CodeAnalysis; + namespace System.Diagnostics { /// @@ -12,6 +14,7 @@ namespace System.Diagnostics /// public partial class DebugProvider { + [DoesNotReturn] public virtual void Fail(string? message, string? detailMessage) { string stackTrace; @@ -25,7 +28,9 @@ public virtual void Fail(string? message, string? detailMessage) } WriteAssert(stackTrace, message, detailMessage); FailCore(stackTrace, message, detailMessage, "Assertion failed."); +#nullable disable } +#nullable restore internal void WriteAssert(string stackTrace, string? message, string? detailMessage) { diff --git a/src/libraries/System.Private.CoreLib/src/System/HashCode.cs b/src/libraries/System.Private.CoreLib/src/System/HashCode.cs index 0ff94adf17b981..f0116ee129d9c1 100644 --- a/src/libraries/System.Private.CoreLib/src/System/HashCode.cs +++ b/src/libraries/System.Private.CoreLib/src/System/HashCode.cs @@ -303,7 +303,8 @@ public void Add(T value) public void Add(T value, IEqualityComparer? comparer) { - Add(comparer != null ? comparer.GetHashCode(value) : (value?.GetHashCode() ?? 0)); + // TODO2 + Add(comparer != null ? comparer.GetHashCode(value!) : (value?.GetHashCode() ?? 0)); } private void Add(int value) diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/MemoryMarshal.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/MemoryMarshal.cs index 16d431cc2d2fd7..caf089b23f1416 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/MemoryMarshal.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/MemoryMarshal.cs @@ -307,7 +307,7 @@ public static bool TryGetMemoryManager(ReadOnlyMemory memory, [N { TManager? localManager; // Use register for null comparison rather than byref manager = localManager = memory.GetObjectStartLength(out _, out _) as TManager; - return localManager != null; + return manager != null; } /// @@ -329,7 +329,7 @@ public static bool TryGetMemoryManager(ReadOnlyMemory memory, [N Debug.Assert(length >= 0); - if (localManager == null) + if (manager == null) { start = default; length = default; diff --git a/src/libraries/System.Private.CoreLib/src/System/SpanHelpers.BinarySearch.cs b/src/libraries/System.Private.CoreLib/src/System/SpanHelpers.BinarySearch.cs index 385992fe45aeeb..b50bed9581f993 100644 --- a/src/libraries/System.Private.CoreLib/src/System/SpanHelpers.BinarySearch.cs +++ b/src/libraries/System.Private.CoreLib/src/System/SpanHelpers.BinarySearch.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.Runtime.CompilerServices; using System.Runtime.InteropServices; @@ -75,7 +76,7 @@ public ComparerComparable(T value, TComparer comparer) } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public int CompareTo(T other) => _comparer.Compare(_value, other); + public int CompareTo([AllowNull] T other) => _comparer.Compare(_value, other); } } } diff --git a/src/libraries/System.Reflection.Metadata/src/System/Reflection/Internal/Utilities/ByteSequenceComparer.cs b/src/libraries/System.Reflection.Metadata/src/System/Reflection/Internal/Utilities/ByteSequenceComparer.cs index cd89135d49b20e..b97d68de6960ef 100644 --- a/src/libraries/System.Reflection.Metadata/src/System/Reflection/Internal/Utilities/ByteSequenceComparer.cs +++ b/src/libraries/System.Reflection.Metadata/src/System/Reflection/Internal/Utilities/ByteSequenceComparer.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.Collections.Immutable; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; namespace System.Reflection.Internal { @@ -62,7 +63,7 @@ internal static bool Equals(byte[] left, int leftStart, byte[] right, int rightS return true; } - internal static bool Equals(byte[] left, byte[] right) + internal static bool Equals(byte[]? left, byte[]? right) { if (ReferenceEquals(left, right)) { @@ -99,7 +100,7 @@ internal static int GetHashCode(ImmutableArray x) return Hash.GetFNVHashCode(x); } - bool IEqualityComparer.Equals(byte[] x, byte[] y) + bool IEqualityComparer.Equals(byte[]? x, byte[]? y) { return Equals(x, y); } diff --git a/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/General/RoAssemblyName.cs b/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/General/RoAssemblyName.cs index 293fb2aee61fb1..60ffff0e43ab82 100644 --- a/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/General/RoAssemblyName.cs +++ b/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/General/RoAssemblyName.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; namespace System.Reflection.TypeLoading { @@ -49,9 +50,9 @@ public RoAssemblyName(string? name, Version? version, string? cultureName, byte[ // Equality - this compares every bit of data in the RuntimeAssemblyName which is acceptable for use as keys in a cache // where semantic duplication is permissible. This method is *not* meant to define ref->def binding rules or // assembly binding unification rules. - public bool Equals(RoAssemblyName other) + public bool Equals([AllowNull] RoAssemblyName other) { - if (Name != other.Name) + if (Name != other!.Name) // TODO2 return false; if (Version != other.Version) return false; diff --git a/src/libraries/System.Resources.Writer/src/System/Resources/__FastResourceComparer.cs b/src/libraries/System.Resources.Writer/src/System/Resources/__FastResourceComparer.cs index d884dec2a1e8b5..598b50f7a09cf7 100644 --- a/src/libraries/System.Resources.Writer/src/System/Resources/__FastResourceComparer.cs +++ b/src/libraries/System.Resources.Writer/src/System/Resources/__FastResourceComparer.cs @@ -6,6 +6,7 @@ using System.Collections; using System.Collections.Generic; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; namespace System.Resources { @@ -33,16 +34,14 @@ internal static int HashFunction(string key) return (int)hash; } - public int Compare(string a, string b) + public int Compare([AllowNull] string a, [AllowNull] string b) { return string.CompareOrdinal(a, b); } - public bool Equals(string a, string b) + public bool Equals([AllowNull] string a, [AllowNull] string b) { return string.Equals(a, b); } - - } } diff --git a/src/libraries/System.Runtime.Extensions/ref/System.Runtime.Extensions.cs b/src/libraries/System.Runtime.Extensions/ref/System.Runtime.Extensions.cs index a06a6075660e67..1414b85ce3145d 100644 --- a/src/libraries/System.Runtime.Extensions/ref/System.Runtime.Extensions.cs +++ b/src/libraries/System.Runtime.Extensions/ref/System.Runtime.Extensions.cs @@ -683,12 +683,12 @@ public static partial class Environment public static System.Version Version { get { throw null; } } public static long WorkingSet { get { throw null; } } [System.Diagnostics.CodeAnalysis.DoesNotReturnAttribute] - public static void Exit(int exitCode) { } + public static void Exit(int exitCode) => throw null; public static string ExpandEnvironmentVariables(string name) { throw null; } [System.Diagnostics.CodeAnalysis.DoesNotReturnAttribute] - public static void FailFast(string? message) { } + public static void FailFast(string? message) => throw null; [System.Diagnostics.CodeAnalysis.DoesNotReturnAttribute] - public static void FailFast(string? message, System.Exception? exception) { } + public static void FailFast(string? message, System.Exception? exception) => throw null; public static string[] GetCommandLineArgs() { throw null; } public static string? GetEnvironmentVariable(string variable) { throw null; } public static string? GetEnvironmentVariable(string variable, System.EnvironmentVariableTarget target) { throw null; } diff --git a/src/libraries/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/Formatters/Binary/ObjectProgress.cs b/src/libraries/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/Formatters/Binary/ObjectProgress.cs index 64c0a193330f26..51490e712ad1e9 100644 --- a/src/libraries/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/Formatters/Binary/ObjectProgress.cs +++ b/src/libraries/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/Formatters/Binary/ObjectProgress.cs @@ -92,7 +92,7 @@ internal bool GetNext(out BinaryTypeEnum outBinaryTypeEnum, [NotNullWhen(true)] else { outBinaryTypeEnum = _binaryTypeEnum; - outTypeInformation = _typeInformation; + outTypeInformation = _typeInformation!; // TODO2 if (_count == 0) _isInitial = false; _count++; @@ -110,7 +110,7 @@ internal bool GetNext(out BinaryTypeEnum outBinaryTypeEnum, [NotNullWhen(true)] { Debug.Assert(_binaryTypeEnumA != null && _typeInformationA != null && _memberNames != null && _memberTypes != null); outBinaryTypeEnum = _binaryTypeEnumA[_count]; - outTypeInformation = _typeInformationA[_count]; + outTypeInformation = _typeInformationA[_count]!; // TODO2 if (_count == 0) { _isInitial = false; diff --git a/src/libraries/System.Runtime/ref/System.Runtime.cs b/src/libraries/System.Runtime/ref/System.Runtime.cs index 122e0037232134..3eaa73d317e01a 100644 --- a/src/libraries/System.Runtime/ref/System.Runtime.cs +++ b/src/libraries/System.Runtime/ref/System.Runtime.cs @@ -4213,10 +4213,10 @@ public static void Assert([System.Diagnostics.CodeAnalysis.DoesNotReturnIfAttrib public static void Close() { } [System.Diagnostics.CodeAnalysis.DoesNotReturnAttribute] [System.Diagnostics.ConditionalAttribute("DEBUG")] - public static void Fail(string? message) { } + public static void Fail(string? message) => throw null; [System.Diagnostics.CodeAnalysis.DoesNotReturnAttribute] [System.Diagnostics.ConditionalAttribute("DEBUG")] - public static void Fail(string? message, string? detailMessage) { } + public static void Fail(string? message, string? detailMessage) => throw null; [System.Diagnostics.ConditionalAttribute("DEBUG")] public static void Flush() { } [System.Diagnostics.ConditionalAttribute("DEBUG")] @@ -7447,9 +7447,9 @@ internal ExceptionDispatchInfo() { } public static System.Runtime.ExceptionServices.ExceptionDispatchInfo Capture(System.Exception source) { throw null; } public static System.Exception SetCurrentStackTrace(System.Exception source) { throw null; } [System.Diagnostics.CodeAnalysis.DoesNotReturnAttribute] - public void Throw() { } + public void Throw() => throw null; [System.Diagnostics.CodeAnalysis.DoesNotReturnAttribute] - public static void Throw(System.Exception source) { } + public static void Throw(System.Exception source) => throw null; } public partial class FirstChanceExceptionEventArgs : System.EventArgs { diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/ReferenceEqualsEqualityComparer.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/ReferenceEqualsEqualityComparer.cs index 6f3667c2380657..495f4c0d75f4c6 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/ReferenceEqualsEqualityComparer.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/ReferenceEqualsEqualityComparer.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Runtime.CompilerServices; +using System.Diagnostics.CodeAnalysis; namespace System.Text.Json.Serialization { @@ -15,7 +16,7 @@ internal sealed class ReferenceEqualsEqualityComparer : IEqualityComparer { public static ReferenceEqualsEqualityComparer Comparer = new ReferenceEqualsEqualityComparer(); - bool IEqualityComparer.Equals(T x, T y) + bool IEqualityComparer.Equals([AllowNull] T x, [AllowNull] T y) { return ReferenceEquals(x, y); } diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/ThrowHelper.Serialization.cs b/src/libraries/System.Text.Json/src/System/Text/Json/ThrowHelper.Serialization.cs index a03e0c3e233d31..18723094128d26 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/ThrowHelper.Serialization.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/ThrowHelper.Serialization.cs @@ -19,7 +19,6 @@ public static void ThrowArgumentException_DeserializeWrongType(Type type, object throw new ArgumentException(SR.Format(SR.DeserializeWrongType, type, value.GetType())); } - [DoesNotReturn] [MethodImpl(MethodImplOptions.NoInlining)] public static NotSupportedException GetNotSupportedException_SerializationNotSupportedCollection(Type propertyType, Type? parentType, MemberInfo? memberInfo) { diff --git a/src/mono/netcore/System.Private.CoreLib/src/System/TypeIdentifier.cs b/src/mono/netcore/System.Private.CoreLib/src/System/TypeIdentifier.cs index 0460777d058ff1..24e07936bebda9 100644 --- a/src/mono/netcore/System.Private.CoreLib/src/System/TypeIdentifier.cs +++ b/src/mono/netcore/System.Private.CoreLib/src/System/TypeIdentifier.cs @@ -21,6 +21,8 @@ // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // +using System.Diagnostics.CodeAnalysis; + namespace System { // A TypeName is wrapper around type names in display form @@ -68,7 +70,7 @@ internal abstract class ATypeName : TypeName public abstract TypeName NestedName (TypeIdentifier innerName); - public bool Equals (TypeName other) + public bool Equals ([AllowNull] TypeName other) { return other != null && DisplayName == other.DisplayName; } From cf34100bcc2959bb2949a6a3af04e332a64df8da Mon Sep 17 00:00:00 2001 From: Julien Couvreur Date: Mon, 10 Feb 2020 19:16:23 -0800 Subject: [PATCH 02/12] Revert repro changes --- NuGet.config | 2 -- eng/Versions.props | 1 - 2 files changed, 3 deletions(-) diff --git a/NuGet.config b/NuGet.config index 61f74a4f9f56ba..b656e4d9162cef 100644 --- a/NuGet.config +++ b/NuGet.config @@ -17,8 +17,6 @@ - - diff --git a/eng/Versions.props b/eng/Versions.props index e7e9c1d44d2fb3..131c3ff9ab7b26 100644 --- a/eng/Versions.props +++ b/eng/Versions.props @@ -57,7 +57,6 @@ - 3.6.0-dev-20110-01 5.0.0-beta.20105.2 5.0.0-beta.20105.2 5.0.0-beta.20105.2 From 1282234f82ea077d973907b76516ca9d275b2926 Mon Sep 17 00:00:00 2001 From: Julien Couvreur Date: Mon, 10 Feb 2020 20:42:32 -0800 Subject: [PATCH 03/12] Simplifications --- .../src/System/Linq/Expressions/Interpreter/LightCompiler.cs | 2 +- .../src/System/Runtime/InteropServices/MemoryMarshal.cs | 4 ++-- .../System/Reflection/TypeLoading/General/RoAssemblyName.cs | 2 +- .../src/System/Resources/__FastResourceComparer.cs | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/libraries/System.Linq.Expressions/src/System/Linq/Expressions/Interpreter/LightCompiler.cs b/src/libraries/System.Linq.Expressions/src/System/Linq/Expressions/Interpreter/LightCompiler.cs index d518fd9209d234..6039ecb9acb627 100644 --- a/src/libraries/System.Linq.Expressions/src/System/Linq/Expressions/Interpreter/LightCompiler.cs +++ b/src/libraries/System.Linq.Expressions/src/System/Linq/Expressions/Interpreter/LightCompiler.cs @@ -215,7 +215,7 @@ internal sealed class DebugInfo private class DebugInfoComparer : IComparer { //We allow comparison between int and DebugInfo here - int IComparer.Compare([AllowNull] DebugInfo d1, [AllowNull] DebugInfo d2) + int IComparer.Compare(DebugInfo? d1, DebugInfo? d2) { if (d1!.Index > d2!.Index) return 1; // TODO2 else if (d1.Index == d2.Index) return 0; diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/MemoryMarshal.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/MemoryMarshal.cs index caf089b23f1416..035892909e9a83 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/MemoryMarshal.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/MemoryMarshal.cs @@ -307,7 +307,7 @@ public static bool TryGetMemoryManager(ReadOnlyMemory memory, [N { TManager? localManager; // Use register for null comparison rather than byref manager = localManager = memory.GetObjectStartLength(out _, out _) as TManager; - return manager != null; + return localManager != null; // TODO2 } /// @@ -329,7 +329,7 @@ public static bool TryGetMemoryManager(ReadOnlyMemory memory, [N Debug.Assert(length >= 0); - if (manager == null) + if (localManager == null) // TODO2 { start = default; length = default; diff --git a/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/General/RoAssemblyName.cs b/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/General/RoAssemblyName.cs index 60ffff0e43ab82..a99bd9a2fc925f 100644 --- a/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/General/RoAssemblyName.cs +++ b/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/General/RoAssemblyName.cs @@ -50,7 +50,7 @@ public RoAssemblyName(string? name, Version? version, string? cultureName, byte[ // Equality - this compares every bit of data in the RuntimeAssemblyName which is acceptable for use as keys in a cache // where semantic duplication is permissible. This method is *not* meant to define ref->def binding rules or // assembly binding unification rules. - public bool Equals([AllowNull] RoAssemblyName other) + public bool Equals(RoAssemblyName? other) { if (Name != other!.Name) // TODO2 return false; diff --git a/src/libraries/System.Resources.Writer/src/System/Resources/__FastResourceComparer.cs b/src/libraries/System.Resources.Writer/src/System/Resources/__FastResourceComparer.cs index 598b50f7a09cf7..628e2e4b7e2c4c 100644 --- a/src/libraries/System.Resources.Writer/src/System/Resources/__FastResourceComparer.cs +++ b/src/libraries/System.Resources.Writer/src/System/Resources/__FastResourceComparer.cs @@ -34,12 +34,12 @@ internal static int HashFunction(string key) return (int)hash; } - public int Compare([AllowNull] string a, [AllowNull] string b) + public int Compare(string? a, string? b) { return string.CompareOrdinal(a, b); } - public bool Equals([AllowNull] string a, [AllowNull] string b) + public bool Equals(string? a, string? b) { return string.Equals(a, b); } From d65f2d7e3f2c4c7903a74be66e821ad2afbd5a46 Mon Sep 17 00:00:00 2001 From: Julien Couvreur Date: Tue, 11 Feb 2020 10:10:20 -0800 Subject: [PATCH 04/12] Address feedback --- .../Collections/Generic/ReferenceEqualityComparer.cs | 2 +- .../Collections/Immutable/ImmutableHashSet_1.cs | 12 ++++++------ .../Collections/Generic/HashSetEqualityComparer.cs | 5 ++++- .../Collections/Generic/SortedSetEqualityComparer.cs | 5 ++++- .../Internal/Collections/CollectionServices.cs | 5 ++++- .../Composition/Primitives/ComposablePartCatalog.cs | 2 +- .../Primitives/ComposablePartDefinition.cs | 7 +++---- .../Composition/ReflectionModel/ImportType.cs | 8 ++------ .../ReflectionComposablePartDefinition.cs | 4 ++-- .../src/System/Dynamic/Utils/CollectionExtensions.cs | 5 ++++- .../Linq/Expressions/Interpreter/LightCompiler.cs | 3 ++- .../Parallel/Partitioning/HashRepartitionStream.cs | 11 ++++------- .../src/System/Linq/Parallel/Utils/HashLookup.cs | 4 +--- .../Linq/Parallel/Utils/WrapperEqualityComparer.cs | 3 ++- .../src/System/Diagnostics/DebugProvider.cs | 4 ++-- .../System.Private.CoreLib/src/System/HashCode.cs | 3 +-- .../System/Runtime/InteropServices/MemoryMarshal.cs | 8 ++++++-- .../Reflection/TypeLoading/General/RoAssemblyName.cs | 3 ++- .../Formatters/Binary/ObjectProgress.cs | 6 +++--- 19 files changed, 54 insertions(+), 46 deletions(-) diff --git a/src/libraries/Common/src/System/Collections/Generic/ReferenceEqualityComparer.cs b/src/libraries/Common/src/System/Collections/Generic/ReferenceEqualityComparer.cs index 1e88596d17f776..b581a45923a18d 100644 --- a/src/libraries/Common/src/System/Collections/Generic/ReferenceEqualityComparer.cs +++ b/src/libraries/Common/src/System/Collections/Generic/ReferenceEqualityComparer.cs @@ -16,7 +16,7 @@ private ReferenceEqualityComparer() { } - public bool Equals([AllowNull] T x, [AllowNull] T y) + public bool Equals(T? x, T? y) { return ReferenceEquals(x, y); } 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 fee4c0563059de..ba754a8d61a3a6 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 @@ -221,7 +221,7 @@ public ImmutableHashSet Remove(T item) [Pure] public bool TryGetValue(T equalValue, out T actualValue) { - int hashCode = _equalityComparer.GetHashCode(equalValue!); + int hashCode = equalValue is object ? _equalityComparer.GetHashCode(equalValue) : 0; HashBucket bucket; if (_root.TryGetValue(hashCode, out bucket)) { @@ -639,7 +639,7 @@ private static bool IsSupersetOf(IEnumerable other, MutationInput origin) private static MutationResult Add(T item, MutationInput origin) { OperationResult result; - int hashCode = origin.EqualityComparer.GetHashCode(item!); + int hashCode = item is object ? origin.EqualityComparer.GetHashCode(item) : 0; HashBucket bucket = origin.Root.GetValueOrDefault(hashCode); var newBucket = bucket.Add(item, origin.EqualityComparer, out result); if (result == OperationResult.NoChangeRequired) @@ -658,7 +658,7 @@ private static MutationResult Add(T item, MutationInput origin) private static MutationResult Remove(T item, MutationInput origin) { var result = OperationResult.NoChangeRequired; - int hashCode = origin.EqualityComparer.GetHashCode(item!); + int hashCode = item is object ? origin.EqualityComparer.GetHashCode(item) : 0; HashBucket bucket; var newRoot = origin.Root; if (origin.Root.TryGetValue(hashCode, out bucket)) @@ -680,7 +680,7 @@ private static MutationResult Remove(T item, MutationInput origin) /// private static bool Contains(T item, MutationInput origin) { - int hashCode = origin.EqualityComparer.GetHashCode(item!); + int hashCode = item is object ? origin.EqualityComparer.GetHashCode(item) : 0; HashBucket bucket; if (origin.Root.TryGetValue(hashCode, out bucket)) { @@ -701,7 +701,7 @@ private static MutationResult Union(IEnumerable other, MutationInput origin) var newRoot = origin.Root; foreach (var item in other.GetEnumerableDisposable()) { - int hashCode = origin.EqualityComparer.GetHashCode(item!); + int hashCode = item is object ? origin.EqualityComparer.GetHashCode(item) : 0; HashBucket bucket = newRoot.GetValueOrDefault(hashCode); OperationResult result; var newBucket = bucket.Add(item, origin.EqualityComparer, out result); @@ -812,7 +812,7 @@ private static MutationResult Except(IEnumerable other, IEqualityComparer var newRoot = root; foreach (var item in other.GetEnumerableDisposable()) { - int hashCode = equalityComparer.GetHashCode(item!); + int hashCode = item is object ? equalityComparer.GetHashCode(item) : 0; HashBucket bucket; if (newRoot.TryGetValue(hashCode, out bucket)) { diff --git a/src/libraries/System.Collections/src/System/Collections/Generic/HashSetEqualityComparer.cs b/src/libraries/System.Collections/src/System/Collections/Generic/HashSetEqualityComparer.cs index 5976e9025ca805..369b4ce28db0e2 100644 --- a/src/libraries/System.Collections/src/System/Collections/Generic/HashSetEqualityComparer.cs +++ b/src/libraries/System.Collections/src/System/Collections/Generic/HashSetEqualityComparer.cs @@ -30,7 +30,10 @@ public int GetHashCode(HashSet? obj) { foreach (T t in obj) { - hashCode = hashCode ^ (_comparer.GetHashCode(t!) & 0x7FFFFFFF); // TODO2 + if (t is object) + { + hashCode = hashCode ^ (_comparer.GetHashCode(t) & 0x7FFFFFFF); + } } } // else returns hashcode of 0 for null hashsets return hashCode; diff --git a/src/libraries/System.Collections/src/System/Collections/Generic/SortedSetEqualityComparer.cs b/src/libraries/System.Collections/src/System/Collections/Generic/SortedSetEqualityComparer.cs index 8de2cd33b15b3f..e3da1fb01233ad 100644 --- a/src/libraries/System.Collections/src/System/Collections/Generic/SortedSetEqualityComparer.cs +++ b/src/libraries/System.Collections/src/System/Collections/Generic/SortedSetEqualityComparer.cs @@ -37,7 +37,10 @@ public int GetHashCode(SortedSet obj) { foreach (T t in obj) { - hashCode = hashCode ^ (_memberEqualityComparer.GetHashCode(t!) & 0x7FFFFFFF); // TODO2 + if (t is object) + { + hashCode = hashCode ^ (_memberEqualityComparer.GetHashCode(t) & 0x7FFFFFFF); + } } } // Returns 0 for null sets. diff --git a/src/libraries/System.ComponentModel.Composition/src/Microsoft/Internal/Collections/CollectionServices.cs b/src/libraries/System.ComponentModel.Composition/src/Microsoft/Internal/Collections/CollectionServices.cs index 71a388e0e25e3c..84b30f98e5e3ec 100644 --- a/src/libraries/System.ComponentModel.Composition/src/Microsoft/Internal/Collections/CollectionServices.cs +++ b/src/libraries/System.ComponentModel.Composition/src/Microsoft/Internal/Collections/CollectionServices.cs @@ -6,6 +6,7 @@ using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; +using System.Diagnostics; using System.Linq; namespace Microsoft.Internal.Collections @@ -144,11 +145,13 @@ private static List FastAppendToListAllowNulls(this List? source, T val } public static List? FastAppendToListAllowNulls( - this List? source, T value, + this List? source, T? value, IEnumerable? second) + where T : class { if (second == null) { + Debug.Assert(value is object); source = source.FastAppendToListAllowNulls(value); } else diff --git a/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/Primitives/ComposablePartCatalog.cs b/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/Primitives/ComposablePartCatalog.cs index 2e3a3c7c129595..1b6f617b0a1fc1 100644 --- a/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/Primitives/ComposablePartCatalog.cs +++ b/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/Primitives/ComposablePartCatalog.cs @@ -96,7 +96,7 @@ public virtual IQueryable Parts /// /// [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")] - public virtual IEnumerable> GetExports(ImportDefinition definition) + public virtual IEnumerable?> GetExports(ImportDefinition definition) { ThrowIfDisposed(); diff --git a/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/Primitives/ComposablePartDefinition.cs b/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/Primitives/ComposablePartDefinition.cs index 395c19d1c5ee92..c93e66147f9e94 100644 --- a/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/Primitives/ComposablePartDefinition.cs +++ b/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/Primitives/ComposablePartDefinition.cs @@ -103,7 +103,7 @@ protected ComposablePartDefinition() /// public abstract ComposablePart CreatePart(); - internal virtual bool TryGetExports(ImportDefinition definition, [NotNullWhen(true)] out Tuple? singleMatch, out IEnumerable>? multipleMatches) + internal virtual bool TryGetExports(ImportDefinition definition, out Tuple? singleMatch, out IEnumerable>? multipleMatches) { singleMatch = null; multipleMatches = null; @@ -140,12 +140,11 @@ internal virtual bool TryGetExports(ImportDefinition definition, [NotNullWhen(tr if (multipleExports != null) { multipleMatches = multipleExports; - // TODO2 singleMatch dould be null when returning true - singleMatch = null!; + singleMatch = null; } else { - singleMatch = singleExport!; + singleMatch = singleExport; } return true; } diff --git a/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/ReflectionModel/ImportType.cs b/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/ReflectionModel/ImportType.cs index b88ed6b6b476ed..5cd82d7ab90c14 100644 --- a/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/ReflectionModel/ImportType.cs +++ b/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/ReflectionModel/ImportType.cs @@ -170,9 +170,9 @@ private static bool IsLazyGenericType(Type genericType) return (genericType == LazyOfTType) || (genericType == LazyOfTMType); } - private static bool TryGetCastFunction(Type genericType, bool isOpenGeneric, Type[] arguments, [NotNullWhen(true)] out Func? castFunction) + private static bool TryGetCastFunction(Type genericType, bool isOpenGeneric, Type[] arguments, out Func? castFunction) { - castFunction = null!; // TODO2 + castFunction = null; if (genericType == LazyOfTType) { @@ -180,7 +180,6 @@ private static bool TryGetCastFunction(Type genericType, bool isOpenGeneric, Typ { castFunction = ExportServices.CreateStronglyTypedLazyFactory(arguments[0].UnderlyingSystemType, null); } - // castFunction return true; } @@ -190,7 +189,6 @@ private static bool TryGetCastFunction(Type genericType, bool isOpenGeneric, Typ { castFunction = ExportServices.CreateStronglyTypedLazyFactory(arguments[0].UnderlyingSystemType, arguments[1].UnderlyingSystemType); } - // castFunction return true; } @@ -202,7 +200,6 @@ private static bool TryGetCastFunction(Type genericType, bool isOpenGeneric, Typ { castFunction = new ExportFactoryCreator(genericType).CreateStronglyTypedExportFactoryFactory(arguments[0].UnderlyingSystemType, null); } - // castFunction return true; } else if (arguments.Length == 2) @@ -211,7 +208,6 @@ private static bool TryGetCastFunction(Type genericType, bool isOpenGeneric, Typ { castFunction = new ExportFactoryCreator(genericType).CreateStronglyTypedExportFactoryFactory(arguments[0].UnderlyingSystemType, arguments[1].UnderlyingSystemType); } - // castFunction return true; } else diff --git a/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/ReflectionModel/ReflectionComposablePartDefinition.cs b/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/ReflectionModel/ReflectionComposablePartDefinition.cs index f353480946ca05..5cce60871db19b 100644 --- a/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/ReflectionModel/ReflectionComposablePartDefinition.cs +++ b/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/ReflectionModel/ReflectionComposablePartDefinition.cs @@ -154,11 +154,11 @@ public override ComposablePart CreatePart() return null; } - internal override bool TryGetExports(ImportDefinition definition, [NotNullWhen(true)] out Tuple? singleMatch, out IEnumerable>? multipleMatches) + internal override bool TryGetExports(ImportDefinition definition, out Tuple? singleMatch, out IEnumerable>? multipleMatches) { if (this.IsGeneric()) { - singleMatch = null!; // TODO2 + singleMatch = null; multipleMatches = null; List>? exports = null; diff --git a/src/libraries/System.Linq.Expressions/src/System/Dynamic/Utils/CollectionExtensions.cs b/src/libraries/System.Linq.Expressions/src/System/Dynamic/Utils/CollectionExtensions.cs index 9462c74680e8e7..9b3f7789f34ab1 100644 --- a/src/libraries/System.Linq.Expressions/src/System/Dynamic/Utils/CollectionExtensions.cs +++ b/src/libraries/System.Linq.Expressions/src/System/Dynamic/Utils/CollectionExtensions.cs @@ -86,7 +86,10 @@ public static int ListHashCode(this ReadOnlyCollection list) int h = 6551; foreach (T t in list) { - h ^= (h << 5) ^ cmp.GetHashCode(t!); + if (t is object) + { + h ^= (h << 5) ^ cmp.GetHashCode(t); + } } return h; } diff --git a/src/libraries/System.Linq.Expressions/src/System/Linq/Expressions/Interpreter/LightCompiler.cs b/src/libraries/System.Linq.Expressions/src/System/Linq/Expressions/Interpreter/LightCompiler.cs index 6039ecb9acb627..a348dc11c1dc8e 100644 --- a/src/libraries/System.Linq.Expressions/src/System/Linq/Expressions/Interpreter/LightCompiler.cs +++ b/src/libraries/System.Linq.Expressions/src/System/Linq/Expressions/Interpreter/LightCompiler.cs @@ -217,7 +217,8 @@ private class DebugInfoComparer : IComparer //We allow comparison between int and DebugInfo here int IComparer.Compare(DebugInfo? d1, DebugInfo? d2) { - if (d1!.Index > d2!.Index) return 1; // TODO2 + Debug.Assert(d1 is object && d2 is object); + if (d1.Index > d2.Index) return 1; else if (d1.Index == d2.Index) return 0; else return -1; } diff --git a/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/Partitioning/HashRepartitionStream.cs b/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/Partitioning/HashRepartitionStream.cs index 729b125b650467..e90a0a9002585b 100644 --- a/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/Partitioning/HashRepartitionStream.cs +++ b/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/Partitioning/HashRepartitionStream.cs @@ -91,19 +91,16 @@ internal int GetHashCode(TInputOutput element) { return (HashCodeMask & - (_elementComparer == null ? - (element == null ? NULL_ELEMENT_HASH_CODE : element.GetHashCode()) : - _elementComparer.GetHashCode(element!))) - % _distributionMod; + (element == null ? NULL_ELEMENT_HASH_CODE : (_elementComparer?.GetHashCode(element) ?? element.GetHashCode()))) + % _distributionMod; } internal int GetHashCode(THashKey key) { return (HashCodeMask & - (_keyComparer == null ? - (key == null ? NULL_ELEMENT_HASH_CODE : key.GetHashCode()) : - _keyComparer.GetHashCode(key!))) % _distributionMod; + (key == null ? NULL_ELEMENT_HASH_CODE : (_keyComparer?.GetHashCode(key) ?? key.GetHashCode()))) + % _distributionMod; } } } diff --git a/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/Utils/HashLookup.cs b/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/Utils/HashLookup.cs index 131bcd4e30fb38..2bfa32b04bd3f3 100644 --- a/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/Utils/HashLookup.cs +++ b/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/Utils/HashLookup.cs @@ -64,9 +64,7 @@ internal TValue this[TKey key] private int GetKeyHashCode(TKey key) { return HashCodeMask & - (comparer == null ? - (key == null ? 0 : key.GetHashCode()) : - comparer.GetHashCode(key!)); + (key == null ? 0 : (comparer?.GetHashCode(key) ?? key.GetHashCode())); } private bool AreKeysEqual(TKey key1, TKey key2) diff --git a/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/Utils/WrapperEqualityComparer.cs b/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/Utils/WrapperEqualityComparer.cs index f7f4f047f25638..1ccfde59a44d2b 100644 --- a/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/Utils/WrapperEqualityComparer.cs +++ b/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/Utils/WrapperEqualityComparer.cs @@ -43,7 +43,8 @@ public bool Equals(Wrapper x, Wrapper y) public int GetHashCode(Wrapper x) { Debug.Assert(_comparer != null); - return _comparer.GetHashCode(x.Value!); + T value = x.Value; + return value is null ? 0 : _comparer.GetHashCode(value); } } } diff --git a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/DebugProvider.cs b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/DebugProvider.cs index 6a695a0fd93869..6d199d7f786d9a 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/DebugProvider.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/DebugProvider.cs @@ -28,9 +28,9 @@ public virtual void Fail(string? message, string? detailMessage) } WriteAssert(stackTrace, message, detailMessage); FailCore(stackTrace, message, detailMessage, "Assertion failed."); -#nullable disable +#pragma warning disable 8763 // "A method marked [DoesNotReturn] should not return." } -#nullable restore +#pragma warning restore 8763 internal void WriteAssert(string stackTrace, string? message, string? detailMessage) { diff --git a/src/libraries/System.Private.CoreLib/src/System/HashCode.cs b/src/libraries/System.Private.CoreLib/src/System/HashCode.cs index f0116ee129d9c1..6ea2c67d0e1eb9 100644 --- a/src/libraries/System.Private.CoreLib/src/System/HashCode.cs +++ b/src/libraries/System.Private.CoreLib/src/System/HashCode.cs @@ -303,8 +303,7 @@ public void Add(T value) public void Add(T value, IEqualityComparer? comparer) { - // TODO2 - Add(comparer != null ? comparer.GetHashCode(value!) : (value?.GetHashCode() ?? 0)); + Add(value is null ? 0 : (comparer?.GetHashCode(value) ?? value.GetHashCode())); } private void Add(int value) diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/MemoryMarshal.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/MemoryMarshal.cs index 035892909e9a83..6fbb2f8cb9791e 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/MemoryMarshal.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/MemoryMarshal.cs @@ -307,7 +307,9 @@ public static bool TryGetMemoryManager(ReadOnlyMemory memory, [N { TManager? localManager; // Use register for null comparison rather than byref manager = localManager = memory.GetObjectStartLength(out _, out _) as TManager; - return localManager != null; // TODO2 +#pragma warning disable 8762 // "Parameter 'manager' may not have a null value when exiting with 'true'." + return localManager != null; +#pragma warning restore 8762 } /// @@ -329,13 +331,15 @@ public static bool TryGetMemoryManager(ReadOnlyMemory memory, [N Debug.Assert(length >= 0); - if (localManager == null) // TODO2 + if (localManager == null) { start = default; length = default; return false; } +#pragma warning disable 8762 // "Parameter 'manager' may not have a null value when exiting with 'true'." return true; +#pragma warning restore 8762 } /// diff --git a/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/General/RoAssemblyName.cs b/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/General/RoAssemblyName.cs index a99bd9a2fc925f..904d3cd67ee878 100644 --- a/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/General/RoAssemblyName.cs +++ b/src/libraries/System.Reflection.MetadataLoadContext/src/System/Reflection/TypeLoading/General/RoAssemblyName.cs @@ -52,7 +52,8 @@ public RoAssemblyName(string? name, Version? version, string? cultureName, byte[ // assembly binding unification rules. public bool Equals(RoAssemblyName? other) { - if (Name != other!.Name) // TODO2 + Debug.Assert(other is object); + if (Name != other.Name) return false; if (Version != other.Version) return false; diff --git a/src/libraries/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/Formatters/Binary/ObjectProgress.cs b/src/libraries/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/Formatters/Binary/ObjectProgress.cs index 51490e712ad1e9..94febe485f0733 100644 --- a/src/libraries/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/Formatters/Binary/ObjectProgress.cs +++ b/src/libraries/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/Formatters/Binary/ObjectProgress.cs @@ -76,7 +76,7 @@ internal void Init() internal void ArrayCountIncrement(int value) => _count += value; // Specifies what is to parsed next from the wire. - internal bool GetNext(out BinaryTypeEnum outBinaryTypeEnum, [NotNullWhen(true)] out object? outTypeInformation) + internal bool GetNext(out BinaryTypeEnum outBinaryTypeEnum, out object? outTypeInformation) { //Initialize the out params up here. outBinaryTypeEnum = BinaryTypeEnum.Primitive; @@ -92,7 +92,7 @@ internal bool GetNext(out BinaryTypeEnum outBinaryTypeEnum, [NotNullWhen(true)] else { outBinaryTypeEnum = _binaryTypeEnum; - outTypeInformation = _typeInformation!; // TODO2 + outTypeInformation = _typeInformation; if (_count == 0) _isInitial = false; _count++; @@ -110,7 +110,7 @@ internal bool GetNext(out BinaryTypeEnum outBinaryTypeEnum, [NotNullWhen(true)] { Debug.Assert(_binaryTypeEnumA != null && _typeInformationA != null && _memberNames != null && _memberTypes != null); outBinaryTypeEnum = _binaryTypeEnumA[_count]; - outTypeInformation = _typeInformationA[_count]!; // TODO2 + outTypeInformation = _typeInformationA[_count]; if (_count == 0) { _isInitial = false; From 3e5405f78e7432bc2ee67ac37f4b30399446b7d9 Mon Sep 17 00:00:00 2001 From: Julien Couvreur Date: Tue, 11 Feb 2020 11:52:03 -0800 Subject: [PATCH 05/12] Tweak --- .../Composition/Primitives/ComposablePartCatalog.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/Primitives/ComposablePartCatalog.cs b/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/Primitives/ComposablePartCatalog.cs index 1b6f617b0a1fc1..2e3a3c7c129595 100644 --- a/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/Primitives/ComposablePartCatalog.cs +++ b/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/Primitives/ComposablePartCatalog.cs @@ -96,7 +96,7 @@ public virtual IQueryable Parts /// /// [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")] - public virtual IEnumerable?> GetExports(ImportDefinition definition) + public virtual IEnumerable> GetExports(ImportDefinition definition) { ThrowIfDisposed(); From 4ee4cea2e59689405053c6b25456939c7dd2977a Mon Sep 17 00:00:00 2001 From: Julien Couvreur Date: Tue, 11 Feb 2020 11:53:45 -0800 Subject: [PATCH 06/12] Another cleanup --- .../Composition/Primitives/ComposablePartDefinition.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/Primitives/ComposablePartDefinition.cs b/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/Primitives/ComposablePartDefinition.cs index c93e66147f9e94..31d7d84f6ced12 100644 --- a/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/Primitives/ComposablePartDefinition.cs +++ b/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/Primitives/ComposablePartDefinition.cs @@ -140,7 +140,6 @@ internal virtual bool TryGetExports(ImportDefinition definition, out Tuple Date: Tue, 11 Feb 2020 12:55:10 -0800 Subject: [PATCH 07/12] Fixup on ImportTypes --- .../Composition/ReflectionModel/ImportType.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/ReflectionModel/ImportType.cs b/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/ReflectionModel/ImportType.cs index 5cd82d7ab90c14..0f02be5bb1d2e3 100644 --- a/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/ReflectionModel/ImportType.cs +++ b/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/ReflectionModel/ImportType.cs @@ -23,13 +23,13 @@ internal class ImportType private readonly bool _isOpenGeneric = false; [ThreadStatic] - internal static Dictionary>? _castSingleValueCache; + internal static Dictionary?>? _castSingleValueCache; - private static Dictionary> CastSingleValueCache + private static Dictionary?> CastSingleValueCache { get { - return _castSingleValueCache = _castSingleValueCache ?? new Dictionary>(); + return _castSingleValueCache = _castSingleValueCache ?? new Dictionary?>(); } } From 3e6f259352c2fcb0f7f4044d50fae65d39020f07 Mon Sep 17 00:00:00 2001 From: Julien Couvreur Date: Tue, 11 Feb 2020 18:54:06 -0800 Subject: [PATCH 08/12] Revert from T? to [AllowNull]T --- .../src/System/Collections/Generic/ReferenceEqualityComparer.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/Common/src/System/Collections/Generic/ReferenceEqualityComparer.cs b/src/libraries/Common/src/System/Collections/Generic/ReferenceEqualityComparer.cs index b581a45923a18d..1e88596d17f776 100644 --- a/src/libraries/Common/src/System/Collections/Generic/ReferenceEqualityComparer.cs +++ b/src/libraries/Common/src/System/Collections/Generic/ReferenceEqualityComparer.cs @@ -16,7 +16,7 @@ private ReferenceEqualityComparer() { } - public bool Equals(T? x, T? y) + public bool Equals([AllowNull] T x, [AllowNull] T y) { return ReferenceEquals(x, y); } From ca6adaac796a19d70e4361a9b2a8d090c2f27637 Mon Sep 17 00:00:00 2001 From: Julien Couvreur Date: Tue, 11 Feb 2020 19:06:16 -0800 Subject: [PATCH 09/12] Enable nullability on one file --- .../System/Collections/Generic/ReferenceEqualityComparer.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libraries/Common/src/System/Collections/Generic/ReferenceEqualityComparer.cs b/src/libraries/Common/src/System/Collections/Generic/ReferenceEqualityComparer.cs index 1e88596d17f776..820a8de16d8f64 100644 --- a/src/libraries/Common/src/System/Collections/Generic/ReferenceEqualityComparer.cs +++ b/src/libraries/Common/src/System/Collections/Generic/ReferenceEqualityComparer.cs @@ -4,6 +4,7 @@ using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; +#nullable enable namespace System.Collections.Generic { @@ -16,7 +17,7 @@ private ReferenceEqualityComparer() { } - public bool Equals([AllowNull] T x, [AllowNull] T y) + public bool Equals(T? x, T? y) { return ReferenceEquals(x, y); } From b25040f001c3de4f6d0546cfbe20c548dc73899b Mon Sep 17 00:00:00 2001 From: Julien Couvreur Date: Thu, 13 Feb 2020 15:32:26 -0800 Subject: [PATCH 10/12] Addressing PR feedback from Stephen --- .../Collections/Generic/ReferenceEqualityComparer.cs | 2 +- .../Collections/Immutable/ImmutableHashSet_1.cs | 12 ++++++------ .../Collections/Generic/HashSetEqualityComparer.cs | 4 ++-- .../Collections/Generic/SortedSetEqualityComparer.cs | 4 ++-- .../Internal/Collections/CollectionServices.cs | 2 +- .../src/System/Dynamic/Utils/CollectionExtensions.cs | 2 +- .../Linq/Expressions/Interpreter/LightCompiler.cs | 2 +- .../Linq/Parallel/Utils/WrapperEqualityComparer.cs | 2 +- src/libraries/System.Runtime/ref/System.Runtime.cs | 2 +- 9 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/libraries/Common/src/System/Collections/Generic/ReferenceEqualityComparer.cs b/src/libraries/Common/src/System/Collections/Generic/ReferenceEqualityComparer.cs index 820a8de16d8f64..7a9b6c0f1d50b8 100644 --- a/src/libraries/Common/src/System/Collections/Generic/ReferenceEqualityComparer.cs +++ b/src/libraries/Common/src/System/Collections/Generic/ReferenceEqualityComparer.cs @@ -2,9 +2,9 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +#nullable enable using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; -#nullable enable namespace System.Collections.Generic { 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 ba754a8d61a3a6..e740e1afc442aa 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 @@ -221,7 +221,7 @@ public ImmutableHashSet Remove(T item) [Pure] public bool TryGetValue(T equalValue, out T actualValue) { - int hashCode = equalValue is object ? _equalityComparer.GetHashCode(equalValue) : 0; + int hashCode = equalValue != null ? _equalityComparer.GetHashCode(equalValue) : 0; HashBucket bucket; if (_root.TryGetValue(hashCode, out bucket)) { @@ -639,7 +639,7 @@ private static bool IsSupersetOf(IEnumerable other, MutationInput origin) private static MutationResult Add(T item, MutationInput origin) { OperationResult result; - int hashCode = item is object ? origin.EqualityComparer.GetHashCode(item) : 0; + int hashCode = item != null ? origin.EqualityComparer.GetHashCode(item) : 0; HashBucket bucket = origin.Root.GetValueOrDefault(hashCode); var newBucket = bucket.Add(item, origin.EqualityComparer, out result); if (result == OperationResult.NoChangeRequired) @@ -658,7 +658,7 @@ private static MutationResult Add(T item, MutationInput origin) private static MutationResult Remove(T item, MutationInput origin) { var result = OperationResult.NoChangeRequired; - int hashCode = item is object ? origin.EqualityComparer.GetHashCode(item) : 0; + int hashCode = item != null ? origin.EqualityComparer.GetHashCode(item) : 0; HashBucket bucket; var newRoot = origin.Root; if (origin.Root.TryGetValue(hashCode, out bucket)) @@ -680,7 +680,7 @@ private static MutationResult Remove(T item, MutationInput origin) /// private static bool Contains(T item, MutationInput origin) { - int hashCode = item is object ? origin.EqualityComparer.GetHashCode(item) : 0; + int hashCode = item != null ? origin.EqualityComparer.GetHashCode(item) : 0; HashBucket bucket; if (origin.Root.TryGetValue(hashCode, out bucket)) { @@ -701,7 +701,7 @@ private static MutationResult Union(IEnumerable other, MutationInput origin) var newRoot = origin.Root; foreach (var item in other.GetEnumerableDisposable()) { - int hashCode = item is object ? origin.EqualityComparer.GetHashCode(item) : 0; + int hashCode = item != null ? origin.EqualityComparer.GetHashCode(item) : 0; HashBucket bucket = newRoot.GetValueOrDefault(hashCode); OperationResult result; var newBucket = bucket.Add(item, origin.EqualityComparer, out result); @@ -812,7 +812,7 @@ private static MutationResult Except(IEnumerable other, IEqualityComparer var newRoot = root; foreach (var item in other.GetEnumerableDisposable()) { - int hashCode = item is object ? equalityComparer.GetHashCode(item) : 0; + int hashCode = item != null ? equalityComparer.GetHashCode(item) : 0; HashBucket bucket; if (newRoot.TryGetValue(hashCode, out bucket)) { diff --git a/src/libraries/System.Collections/src/System/Collections/Generic/HashSetEqualityComparer.cs b/src/libraries/System.Collections/src/System/Collections/Generic/HashSetEqualityComparer.cs index 369b4ce28db0e2..8415e534569903 100644 --- a/src/libraries/System.Collections/src/System/Collections/Generic/HashSetEqualityComparer.cs +++ b/src/libraries/System.Collections/src/System/Collections/Generic/HashSetEqualityComparer.cs @@ -30,9 +30,9 @@ public int GetHashCode(HashSet? obj) { foreach (T t in obj) { - if (t is object) + if (t != null) { - hashCode = hashCode ^ (_comparer.GetHashCode(t) & 0x7FFFFFFF); + hashCode ^= (_comparer.GetHashCode(t) & 0x7FFFFFFF); } } } // else returns hashcode of 0 for null hashsets diff --git a/src/libraries/System.Collections/src/System/Collections/Generic/SortedSetEqualityComparer.cs b/src/libraries/System.Collections/src/System/Collections/Generic/SortedSetEqualityComparer.cs index e3da1fb01233ad..dc262258981d24 100644 --- a/src/libraries/System.Collections/src/System/Collections/Generic/SortedSetEqualityComparer.cs +++ b/src/libraries/System.Collections/src/System/Collections/Generic/SortedSetEqualityComparer.cs @@ -37,9 +37,9 @@ public int GetHashCode(SortedSet obj) { foreach (T t in obj) { - if (t is object) + if (t != null) { - hashCode = hashCode ^ (_memberEqualityComparer.GetHashCode(t) & 0x7FFFFFFF); + hashCode ^= (_memberEqualityComparer.GetHashCode(t) & 0x7FFFFFFF); } } } diff --git a/src/libraries/System.ComponentModel.Composition/src/Microsoft/Internal/Collections/CollectionServices.cs b/src/libraries/System.ComponentModel.Composition/src/Microsoft/Internal/Collections/CollectionServices.cs index 84b30f98e5e3ec..6a1af13a3d4ae2 100644 --- a/src/libraries/System.ComponentModel.Composition/src/Microsoft/Internal/Collections/CollectionServices.cs +++ b/src/libraries/System.ComponentModel.Composition/src/Microsoft/Internal/Collections/CollectionServices.cs @@ -151,7 +151,7 @@ private static List FastAppendToListAllowNulls(this List? source, T val { if (second == null) { - Debug.Assert(value is object); + Debug.Assert(value != null); source = source.FastAppendToListAllowNulls(value); } else diff --git a/src/libraries/System.Linq.Expressions/src/System/Dynamic/Utils/CollectionExtensions.cs b/src/libraries/System.Linq.Expressions/src/System/Dynamic/Utils/CollectionExtensions.cs index 9b3f7789f34ab1..329e585226469f 100644 --- a/src/libraries/System.Linq.Expressions/src/System/Dynamic/Utils/CollectionExtensions.cs +++ b/src/libraries/System.Linq.Expressions/src/System/Dynamic/Utils/CollectionExtensions.cs @@ -86,7 +86,7 @@ public static int ListHashCode(this ReadOnlyCollection list) int h = 6551; foreach (T t in list) { - if (t is object) + if (t != null) { h ^= (h << 5) ^ cmp.GetHashCode(t); } diff --git a/src/libraries/System.Linq.Expressions/src/System/Linq/Expressions/Interpreter/LightCompiler.cs b/src/libraries/System.Linq.Expressions/src/System/Linq/Expressions/Interpreter/LightCompiler.cs index a348dc11c1dc8e..4959f96de8267c 100644 --- a/src/libraries/System.Linq.Expressions/src/System/Linq/Expressions/Interpreter/LightCompiler.cs +++ b/src/libraries/System.Linq.Expressions/src/System/Linq/Expressions/Interpreter/LightCompiler.cs @@ -217,7 +217,7 @@ private class DebugInfoComparer : IComparer //We allow comparison between int and DebugInfo here int IComparer.Compare(DebugInfo? d1, DebugInfo? d2) { - Debug.Assert(d1 is object && d2 is object); + Debug.Assert(d1 != null && d2 != null); if (d1.Index > d2.Index) return 1; else if (d1.Index == d2.Index) return 0; else return -1; diff --git a/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/Utils/WrapperEqualityComparer.cs b/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/Utils/WrapperEqualityComparer.cs index 1ccfde59a44d2b..b203042f5791ad 100644 --- a/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/Utils/WrapperEqualityComparer.cs +++ b/src/libraries/System.Linq.Parallel/src/System/Linq/Parallel/Utils/WrapperEqualityComparer.cs @@ -44,7 +44,7 @@ public int GetHashCode(Wrapper x) { Debug.Assert(_comparer != null); T value = x.Value; - return value is null ? 0 : _comparer.GetHashCode(value); + return value == null ? 0 : _comparer.GetHashCode(value); } } } diff --git a/src/libraries/System.Runtime/ref/System.Runtime.cs b/src/libraries/System.Runtime/ref/System.Runtime.cs index 3eaa73d317e01a..5ac59a7713aa42 100644 --- a/src/libraries/System.Runtime/ref/System.Runtime.cs +++ b/src/libraries/System.Runtime/ref/System.Runtime.cs @@ -608,7 +608,7 @@ public sealed partial class CLSCompliantAttribute : System.Attribute public CLSCompliantAttribute(bool isCompliant) { } public bool IsCompliant { get { throw null; } } } - public delegate int Comparison(T x, T y); + public delegate int Comparison([System.Diagnostics.CodeAnalysis.AllowNullAttribute] T x, [System.Diagnostics.CodeAnalysis.AllowNullAttribute] T y); public delegate TOutput Converter(TInput input); public readonly partial struct DateTime : System.IComparable, System.IComparable, System.IConvertible, System.IEquatable, System.IFormattable, System.Runtime.Serialization.ISerializable { From f7175ba5427729ce60c898d5e7f00d9a4550e12d Mon Sep 17 00:00:00 2001 From: Julien Couvreur Date: Thu, 13 Feb 2020 15:34:24 -0800 Subject: [PATCH 11/12] Use Debug.Assert instead of pragma --- .../System/Runtime/InteropServices/MemoryMarshal.cs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/MemoryMarshal.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/MemoryMarshal.cs index 6fbb2f8cb9791e..932e3bfe46ab55 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/MemoryMarshal.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/MemoryMarshal.cs @@ -307,9 +307,12 @@ public static bool TryGetMemoryManager(ReadOnlyMemory memory, [N { TManager? localManager; // Use register for null comparison rather than byref manager = localManager = memory.GetObjectStartLength(out _, out _) as TManager; -#pragma warning disable 8762 // "Parameter 'manager' may not have a null value when exiting with 'true'." - return localManager != null; -#pragma warning restore 8762 + if (localManager != null) + { + Debug.Assert(manager != null); + return true; + } + return false; } /// @@ -337,9 +340,8 @@ public static bool TryGetMemoryManager(ReadOnlyMemory memory, [N length = default; return false; } -#pragma warning disable 8762 // "Parameter 'manager' may not have a null value when exiting with 'true'." + Debug.Assert(manager != null); return true; -#pragma warning restore 8762 } /// From 1feaf72d337d85f230fef91dccc5ac1a0bd7f786 Mon Sep 17 00:00:00 2001 From: Julien Couvreur Date: Thu, 13 Feb 2020 15:47:05 -0800 Subject: [PATCH 12/12] Revert "Use Debug.Assert instead of pragma" This reverts commit f7175ba5427729ce60c898d5e7f00d9a4550e12d. --- .../System/Runtime/InteropServices/MemoryMarshal.cs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/MemoryMarshal.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/MemoryMarshal.cs index 932e3bfe46ab55..6fbb2f8cb9791e 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/MemoryMarshal.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/MemoryMarshal.cs @@ -307,12 +307,9 @@ public static bool TryGetMemoryManager(ReadOnlyMemory memory, [N { TManager? localManager; // Use register for null comparison rather than byref manager = localManager = memory.GetObjectStartLength(out _, out _) as TManager; - if (localManager != null) - { - Debug.Assert(manager != null); - return true; - } - return false; +#pragma warning disable 8762 // "Parameter 'manager' may not have a null value when exiting with 'true'." + return localManager != null; +#pragma warning restore 8762 } /// @@ -340,8 +337,9 @@ public static bool TryGetMemoryManager(ReadOnlyMemory memory, [N length = default; return false; } - Debug.Assert(manager != null); +#pragma warning disable 8762 // "Parameter 'manager' may not have a null value when exiting with 'true'." return true; +#pragma warning restore 8762 } ///