From e8f7e35fafcb3628a9bd02d7f9db701a235f4ad7 Mon Sep 17 00:00:00 2001 From: Santiago Fernandez Madero Date: Thu, 27 Jun 2019 10:39:13 -0700 Subject: [PATCH] React to new compiler nullability warnings Commit migrated from https://github.com/dotnet/coreclr/commit/b5ccdc3d9b9c8f6020e9c458e19a8504eb845910 --- .../Runtime/CompilerServices/AsyncMethodBuilder.cs | 11 ++++++----- .../src/System/Threading/Tasks/FutureFactory.cs | 3 ++- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/AsyncMethodBuilder.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/AsyncMethodBuilder.cs index d08753a088c688..fd66c1891227e3 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/AsyncMethodBuilder.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/AsyncMethodBuilder.cs @@ -570,13 +570,13 @@ private static void ExecutionContextCallback(object? s) { Debug.Assert(s is AsyncStateMachineBox); // Only used privately to pass directly to EC.Run - Unsafe.As>(s).StateMachine.MoveNext(); + Unsafe.As>(s).StateMachine!.MoveNext(); } /// A delegate to the method. private Action? _moveNextAction; /// The state machine itself. - [AllowNull, MaybeNull] public TStateMachine StateMachine = default!; // mutable struct; do not make this readonly. SOS DumpAsync command depends on this name. // TODO-NULLABLE: Remove ! when nullable attributes are respected + [AllowNull, MaybeNull] public TStateMachine StateMachine = default; // mutable struct; do not make this readonly. SOS DumpAsync command depends on this name. /// Captured ExecutionContext with which to invoke ; may be null. public ExecutionContext? Context; @@ -601,7 +601,8 @@ private void MoveNext(Thread? threadPoolThread) ExecutionContext? context = Context; if (context == null) { - StateMachine.MoveNext(); + Debug.Assert(StateMachine != null); + StateMachine!.MoveNext(); // TODO-NULLABLE: remove ! when Debug.Assert on fields is respected (https://github.com/dotnet/roslyn/issues/36830) } else { @@ -620,7 +621,7 @@ private void MoveNext(Thread? threadPoolThread) // Clear out state now that the async method has completed. // This avoids keeping arbitrary state referenced by lifted locals // if this Task / state machine box is held onto. - StateMachine = default!; // TODO-NULLABLE: Remove ! when nullable attributes are respected + StateMachine = default; Context = default; #if !CORERT @@ -641,7 +642,7 @@ private void MoveNext(Thread? threadPoolThread) } /// Gets the state machine as a boxed object. This should only be used for debugging purposes. - IAsyncStateMachine IAsyncStateMachineBox.GetStateMachineObject() => StateMachine; // likely boxes, only use for debugging + IAsyncStateMachine IAsyncStateMachineBox.GetStateMachineObject() => StateMachine!; // likely boxes, only use for debugging } /// Gets the for this builder. diff --git a/src/libraries/System.Private.CoreLib/src/System/Threading/Tasks/FutureFactory.cs b/src/libraries/System.Private.CoreLib/src/System/Threading/Tasks/FutureFactory.cs index a7de15a0d50f90..6464676b1be8e3 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Threading/Tasks/FutureFactory.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Threading/Tasks/FutureFactory.cs @@ -1260,7 +1260,7 @@ internal static void CompleteFromAsyncResult(IAsyncResult asyncResult) // Grab the relevant state and then null it out so that the task doesn't hold onto the state unnecessarily var thisRef = promise!.m_thisRef; // TODO-NULLABLE: Remove ! when [DoesNotReturn] respected var endMethod = promise.m_endMethod; - promise.m_thisRef = default!; // TODO-NULLABLE: Remove ! when nullable attributes are respected + promise.m_thisRef = default; promise.m_endMethod = null; if (endMethod == null) ThrowHelper.ThrowArgumentException(ExceptionResource.InvalidOperation_WrongAsyncResultOrEndCalledMultiple, ExceptionArgument.asyncResult); @@ -1268,6 +1268,7 @@ internal static void CompleteFromAsyncResult(IAsyncResult asyncResult) // we'll instead complete the promise at the call site. if (!asyncResult.CompletedSynchronously) { + Debug.Assert(thisRef != null); promise.Complete(thisRef, endMethod!, asyncResult, requiresSynchronization: true); // TODO-NULLABLE: Remove ! when [DoesNotReturn] respected } }