From: Levi Broderick Date: Tue, 5 May 2020 04:59:46 +0000 (-0700) Subject: Address some new Roslyn nullability warnings (#35782) X-Git-Tag: submit/tizen/20210909.063632~8192 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=c9f917052fc091014ef0f311b6f79a812bbfbd38;p=platform%2Fupstream%2Fdotnet%2Fruntime.git Address some new Roslyn nullability warnings (#35782) --- 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 2878b89..b426926 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 @@ namespace System.IO 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 c227155..8ef9d01 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 @@ namespace System.Reflection { 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 2e0acab..3b19094 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 @@ namespace System.Reflection.Emit 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 60379a0..fd6eeb2 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 @@ namespace System.Reflection 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 63a8fbf..6a3418a 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 @@ namespace System.Runtime.CompilerServices _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 f19d63e..d7cd618 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 @@ namespace System.Runtime.InteropServices.WindowsRuntime 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 d24a0c9..bdacad0 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 @@ namespace System.Runtime.InteropServices.WindowsRuntime 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 5ba2469..81c3a45 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 @@ namespace System.Runtime.InteropServices.WindowsRuntime 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 fa80d26..d3753a1 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 @@ namespace System 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 480890e..b47e97c 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 @@ namespace System.Collections.Concurrent /// 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 8aefd8a..e195976 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 @@ namespace System.Runtime.CompilerServices 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 d2bd7ec..c298c05 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 @@ namespace System.Runtime.CompilerServices } 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 0e9ecac..8e2e63c 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 @@ namespace System.Threading /// /// 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 @@ namespace System.Threading /// /// 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 @@ namespace System.Threading /// 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 @@ namespace System.Threading /// 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 @@ namespace System.Threading /// 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 @@ namespace System.Threading } } - 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 4c8dabc..719a4c3 100644 --- a/src/libraries/System.Threading/ref/System.Threading.cs +++ b/src/libraries/System.Threading/ref/System.Threading.cs @@ -216,10 +216,10 @@ namespace System.Threading 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 {