diff --git a/src/coreclr/src/System.Private.CoreLib/src/System/IO/FileLoadException.CoreCLR.cs b/src/coreclr/src/System.Private.CoreLib/src/System/IO/FileLoadException.CoreCLR.cs index 2878b89a49cc4..b426926a95e3f 100644 --- a/src/coreclr/src/System.Private.CoreLib/src/System/IO/FileLoadException.CoreCLR.cs +++ b/src/coreclr/src/System.Private.CoreLib/src/System/IO/FileLoadException.CoreCLR.cs @@ -29,7 +29,7 @@ internal static string FormatFileLoadExceptionMessage(string? fileName, int hRes else GetMessageForHR(hResult, new StringHandleOnStack(ref message)); - return string.Format(format, fileName, message); + return string.Format(format!, fileName, message); } [DllImport(RuntimeHelpers.QCall, CharSet = CharSet.Unicode)] diff --git a/src/coreclr/src/System.Private.CoreLib/src/System/Reflection/Assembly.CoreCLR.cs b/src/coreclr/src/System.Private.CoreLib/src/System/Reflection/Assembly.CoreCLR.cs index c227155ef4664..8ef9d01d3ee70 100644 --- a/src/coreclr/src/System.Private.CoreLib/src/System/Reflection/Assembly.CoreCLR.cs +++ b/src/coreclr/src/System.Private.CoreLib/src/System/Reflection/Assembly.CoreCLR.cs @@ -62,7 +62,7 @@ internal static RuntimeAssembly GetExecutingAssembly(ref StackCrawlMark stackMar { RuntimeAssembly? retAssembly = null; GetExecutingAssemblyNative(new StackCrawlMarkHandle(ref stackMark), ObjectHandleOnStack.Create(ref retAssembly)); - return retAssembly; + return retAssembly!; } // Get the assembly that the current code is running from. diff --git a/src/coreclr/src/System.Private.CoreLib/src/System/Reflection/Emit/AssemblyBuilder.cs b/src/coreclr/src/System.Private.CoreLib/src/System/Reflection/Emit/AssemblyBuilder.cs index 2e0acab559016..3b190943be0d4 100644 --- a/src/coreclr/src/System.Private.CoreLib/src/System/Reflection/Emit/AssemblyBuilder.cs +++ b/src/coreclr/src/System.Private.CoreLib/src/System/Reflection/Emit/AssemblyBuilder.cs @@ -191,7 +191,7 @@ internal ModuleBuilder GetModuleBuilder(InternalModuleBuilder module) new StackCrawlMarkHandle(ref stackMark), (int)access, ObjectHandleOnStack.Create(ref retAssembly)); - _internalAssemblyBuilder = (InternalAssemblyBuilder)retAssembly; + _internalAssemblyBuilder = (InternalAssemblyBuilder)retAssembly!; _assemblyData = new AssemblyBuilderData(_internalAssemblyBuilder, access); diff --git a/src/coreclr/src/System.Private.CoreLib/src/System/Reflection/RuntimeAssembly.cs b/src/coreclr/src/System.Private.CoreLib/src/System/Reflection/RuntimeAssembly.cs index 60379a0cf9182..fd6eeb2bc23cb 100644 --- a/src/coreclr/src/System.Private.CoreLib/src/System/Reflection/RuntimeAssembly.cs +++ b/src/coreclr/src/System.Private.CoreLib/src/System/Reflection/RuntimeAssembly.cs @@ -327,7 +327,7 @@ internal static RuntimeAssembly InternalLoad(AssemblyName assemblyName, ref Stac throwOnFileNotFound, ObjectHandleOnStack.Create(ref assemblyLoadContext), ObjectHandleOnStack.Create(ref retAssembly)); - return retAssembly; + return retAssembly!; } [DllImport(RuntimeHelpers.QCall, CharSet = CharSet.Unicode)] diff --git a/src/coreclr/src/System.Private.CoreLib/src/System/Runtime/CompilerServices/QCallHandles.cs b/src/coreclr/src/System.Private.CoreLib/src/System/Runtime/CompilerServices/QCallHandles.cs index 63a8fbf6e9c33..6a3418ade5938 100644 --- a/src/coreclr/src/System.Private.CoreLib/src/System/Runtime/CompilerServices/QCallHandles.cs +++ b/src/coreclr/src/System.Private.CoreLib/src/System/Runtime/CompilerServices/QCallHandles.cs @@ -15,7 +15,7 @@ namespace System.Runtime.CompilerServices { private void* _ptr; - internal StringHandleOnStack([NotNull] ref string? s) + internal StringHandleOnStack(ref string? s) { _ptr = Unsafe.AsPointer(ref s); } @@ -31,7 +31,7 @@ private ObjectHandleOnStack(void* pObject) _ptr = pObject; } - internal static ObjectHandleOnStack Create([NotNull] ref T o) where T : class? + internal static ObjectHandleOnStack Create(ref T o) where T : class? { return new ObjectHandleOnStack(Unsafe.AsPointer(ref o)); } diff --git a/src/coreclr/src/System.Private.CoreLib/src/System/Runtime/InteropServices/WindowsRuntime/ConstantSplittableMap.cs b/src/coreclr/src/System.Private.CoreLib/src/System/Runtime/InteropServices/WindowsRuntime/ConstantSplittableMap.cs index f19d63e403449..d7cd618a07610 100644 --- a/src/coreclr/src/System.Private.CoreLib/src/System/Runtime/InteropServices/WindowsRuntime/ConstantSplittableMap.cs +++ b/src/coreclr/src/System.Private.CoreLib/src/System/Runtime/InteropServices/WindowsRuntime/ConstantSplittableMap.cs @@ -78,9 +78,7 @@ private ConstantSplittableMap(KeyValuePair[] items, int firstItemI public TValue Lookup(TKey key) { - bool found = TryGetValue(key, out TValue value); - - if (!found) + if (!TryGetValue(key, out TValue value)) { Debug.Assert(key != null); Exception e = new KeyNotFoundException(SR.Format(SR.Arg_KeyNotFoundWithKey, key.ToString())); diff --git a/src/coreclr/src/System.Private.CoreLib/src/System/Runtime/InteropServices/WindowsRuntime/DictionaryToMapAdapter.cs b/src/coreclr/src/System.Private.CoreLib/src/System/Runtime/InteropServices/WindowsRuntime/DictionaryToMapAdapter.cs index d24a0c9201d93..bdacad0d07f3b 100644 --- a/src/coreclr/src/System.Private.CoreLib/src/System/Runtime/InteropServices/WindowsRuntime/DictionaryToMapAdapter.cs +++ b/src/coreclr/src/System.Private.CoreLib/src/System/Runtime/InteropServices/WindowsRuntime/DictionaryToMapAdapter.cs @@ -28,9 +28,7 @@ private DictionaryToMapAdapter() internal V Lookup(K key) where K : notnull { IDictionary _this = Unsafe.As>(this); - bool keyFound = _this.TryGetValue(key, out V value); - - if (!keyFound) + if (!_this.TryGetValue(key, out V value)) { Debug.Assert(key != null); Exception e = new KeyNotFoundException(SR.Format(SR.Arg_KeyNotFoundWithKey, key.ToString())); diff --git a/src/coreclr/src/System.Private.CoreLib/src/System/Runtime/InteropServices/WindowsRuntime/IReadOnlyDictionaryToIMapViewAdapter.cs b/src/coreclr/src/System.Private.CoreLib/src/System/Runtime/InteropServices/WindowsRuntime/IReadOnlyDictionaryToIMapViewAdapter.cs index 5ba246996dccb..81c3a453b17a8 100644 --- a/src/coreclr/src/System.Private.CoreLib/src/System/Runtime/InteropServices/WindowsRuntime/IReadOnlyDictionaryToIMapViewAdapter.cs +++ b/src/coreclr/src/System.Private.CoreLib/src/System/Runtime/InteropServices/WindowsRuntime/IReadOnlyDictionaryToIMapViewAdapter.cs @@ -28,9 +28,7 @@ private IReadOnlyDictionaryToIMapViewAdapter() internal V Lookup(K key) where K : notnull { IReadOnlyDictionary _this = Unsafe.As>(this); - bool keyFound = _this.TryGetValue(key, out V value); - - if (!keyFound) + if (!_this.TryGetValue(key, out V value)) { Debug.Assert(key != null); Exception e = new KeyNotFoundException(SR.Format(SR.Arg_KeyNotFoundWithKey, key.ToString())); diff --git a/src/libraries/System.Private.CoreLib/src/System/Array.cs b/src/libraries/System.Private.CoreLib/src/System/Array.cs index fa80d26e412b2..d3753a1b6bf78 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Array.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Array.cs @@ -64,6 +64,8 @@ public static void Resize([NotNull] ref T[]? array, int newSize) Copy(larray, 0, newArray, 0, larray.Length > newSize ? newSize : larray.Length); array = newArray; } + + Debug.Assert(array != null); } public static Array CreateInstance(Type elementType, params long[] lengths) diff --git a/src/libraries/System.Private.CoreLib/src/System/Collections/Concurrent/ConcurrentQueue.cs b/src/libraries/System.Private.CoreLib/src/System/Collections/Concurrent/ConcurrentQueue.cs index 480890eca55b8..b47e97c9d20c8 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Collections/Concurrent/ConcurrentQueue.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Collections/Concurrent/ConcurrentQueue.cs @@ -198,7 +198,7 @@ bool IProducerConsumerCollection.TryAdd(T item) /// For , this operation will attempt to remove the object /// from the beginning of the . /// - bool IProducerConsumerCollection.TryTake(out T item) => TryDequeue(out item); + bool IProducerConsumerCollection.TryTake([MaybeNullWhen(false)] out T item) => TryDequeue(out item); /// /// Gets a value that indicates whether the is empty. diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/AsyncTaskMethodBuilderT.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/AsyncTaskMethodBuilderT.cs index 8aefd8a1773fd..e195976f1f8c5 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/AsyncTaskMethodBuilderT.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/AsyncTaskMethodBuilderT.cs @@ -60,7 +60,7 @@ public struct AsyncTaskMethodBuilder AwaitOnCompleted(ref awaiter, ref stateMachine, ref m_task); internal static void AwaitOnCompleted( - ref TAwaiter awaiter, ref TStateMachine stateMachine, [NotNull] ref Task? taskField) + ref TAwaiter awaiter, ref TStateMachine stateMachine, ref Task? taskField) where TAwaiter : INotifyCompletion where TStateMachine : IAsyncStateMachine { diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/AsyncValueTaskMethodBuilderT.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/AsyncValueTaskMethodBuilderT.cs index d2bd7ec111a4b..c298c0533d57c 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/AsyncValueTaskMethodBuilderT.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/AsyncValueTaskMethodBuilderT.cs @@ -150,7 +150,7 @@ public ValueTask Task } internal static void AwaitOnCompleted( - ref TAwaiter awaiter, ref TStateMachine stateMachine, [NotNull] ref StateMachineBox? box) + ref TAwaiter awaiter, ref TStateMachine stateMachine, ref StateMachineBox? box) where TAwaiter : INotifyCompletion where TStateMachine : IAsyncStateMachine { diff --git a/src/libraries/System.Private.CoreLib/src/System/Threading/LazyInitializer.cs b/src/libraries/System.Private.CoreLib/src/System/Threading/LazyInitializer.cs index 0e9ecac5877fb..8e2e63c2d0670 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Threading/LazyInitializer.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Threading/LazyInitializer.cs @@ -49,7 +49,7 @@ public static class LazyInitializer /// /// public static T EnsureInitialized([NotNull] ref T? target) where T : class => - Volatile.Read(ref target) ?? EnsureInitializedCore(ref target); + Volatile.Read(ref target!) ?? EnsureInitializedCore(ref target); /// /// Initializes a target reference type with the type's default constructor (slow path) @@ -101,7 +101,7 @@ public static class LazyInitializer /// /// public static T EnsureInitialized([NotNull] ref T? target, Func valueFactory) where T : class => - Volatile.Read(ref target) ?? EnsureInitializedCore(ref target, valueFactory); + Volatile.Read(ref target!) ?? EnsureInitializedCore(ref target, valueFactory); /// /// Initialize the target using the given delegate (slow path). @@ -133,9 +133,10 @@ public static class LazyInitializer /// A reference to a boolean that determines whether the target has already /// been initialized. /// A reference to an object used as the mutually exclusive lock for initializing - /// . If is null, a new object will be instantiated. + /// . If is null, and if the target hasn't already + /// been initialized, a new object will be instantiated. /// The initialized value of type . - public static T EnsureInitialized([AllowNull] ref T target, ref bool initialized, [NotNull] ref object? syncLock) + public static T EnsureInitialized([AllowNull] ref T target, ref bool initialized, [NotNullIfNotNull("syncLock")] ref object? syncLock) { // Fast path. if (Volatile.Read(ref initialized)) @@ -190,11 +191,12 @@ private static T EnsureInitializedCore([AllowNull] ref T target, ref bool ini /// A reference to a boolean that determines whether the target has already /// been initialized. /// A reference to an object used as the mutually exclusive lock for initializing - /// . If is null, a new object will be instantiated. + /// . If is null, and if the target hasn't already + /// been initialized, a new object will be instantiated. /// The invoked to initialize the /// reference or value. /// The initialized value of type . - public static T EnsureInitialized([AllowNull] ref T target, ref bool initialized, [NotNull] ref object? syncLock, Func valueFactory) + public static T EnsureInitialized([AllowNull] ref T target, ref bool initialized, [NotNullIfNotNull("syncLock")] ref object? syncLock, Func valueFactory) { // Fast path. if (Volatile.Read(ref initialized)) @@ -239,11 +241,12 @@ private static T EnsureInitializedCore([AllowNull] ref T target, ref bool ini /// The type of the reference to be initialized. Has to be reference type. /// A reference of type to initialize if it has not already been initialized. /// A reference to an object used as the mutually exclusive lock for initializing - /// . If is null, a new object will be instantiated. + /// . If is null, and if the target hasn't already + /// been initialized, a new object will be instantiated. /// The invoked to initialize the reference. /// The initialized value of type . - public static T EnsureInitialized([NotNull] ref T? target, [NotNull] ref object? syncLock, Func valueFactory) where T : class => - Volatile.Read(ref target) ?? EnsureInitializedCore(ref target, ref syncLock, valueFactory); + public static T EnsureInitialized([NotNull] ref T? target, [NotNullIfNotNull("syncLock")] ref object? syncLock, Func valueFactory) where T : class => + Volatile.Read(ref target!) ?? EnsureInitializedCore(ref target, ref syncLock, valueFactory); /// /// Ensure the target is initialized and return the value (slow path). This overload works only for reference type targets. @@ -272,7 +275,8 @@ private static T EnsureInitializedCore([AllowNull] ref T target, ref bool ini } } - return target!; // TODO-NULLABLE: Compiler can't infer target's non-nullness (https://github.com/dotnet/roslyn/issues/37300) + Debug.Assert(target != null); + return target; } /// diff --git a/src/libraries/System.Threading/ref/System.Threading.cs b/src/libraries/System.Threading/ref/System.Threading.cs index 4c8dabcf6c621..719a4c35d1b0a 100644 --- a/src/libraries/System.Threading/ref/System.Threading.cs +++ b/src/libraries/System.Threading/ref/System.Threading.cs @@ -216,10 +216,10 @@ public static partial class Interlocked public static partial class LazyInitializer { public static T EnsureInitialized([System.Diagnostics.CodeAnalysis.NotNullAttribute] ref T? target) where T : class { throw null; } - public static T EnsureInitialized([System.Diagnostics.CodeAnalysis.AllowNullAttribute] ref T target, ref bool initialized, [System.Diagnostics.CodeAnalysis.NotNullAttribute] ref object? syncLock) { throw null; } - public static T EnsureInitialized([System.Diagnostics.CodeAnalysis.AllowNullAttribute] ref T target, ref bool initialized, [System.Diagnostics.CodeAnalysis.NotNullAttribute] ref object? syncLock, System.Func valueFactory) { throw null; } + public static T EnsureInitialized([System.Diagnostics.CodeAnalysis.AllowNullAttribute] ref T target, ref bool initialized, [System.Diagnostics.CodeAnalysis.NotNullIfNotNullAttribute("syncLock")] ref object? syncLock) { throw null; } + public static T EnsureInitialized([System.Diagnostics.CodeAnalysis.AllowNullAttribute] ref T target, ref bool initialized, [System.Diagnostics.CodeAnalysis.NotNullIfNotNullAttribute("syncLock")] ref object? syncLock, System.Func valueFactory) { throw null; } public static T EnsureInitialized([System.Diagnostics.CodeAnalysis.NotNullAttribute] ref T? target, System.Func valueFactory) where T : class { throw null; } - public static T EnsureInitialized([System.Diagnostics.CodeAnalysis.NotNullAttribute] ref T? target, [System.Diagnostics.CodeAnalysis.NotNullAttribute] ref object? syncLock, System.Func valueFactory) where T : class { throw null; } + public static T EnsureInitialized([System.Diagnostics.CodeAnalysis.NotNullAttribute] ref T? target, [System.Diagnostics.CodeAnalysis.NotNullIfNotNullAttribute("syncLock")] ref object? syncLock, System.Func valueFactory) where T : class { throw null; } } public partial struct LockCookie {