diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/PoolingAsyncValueTaskMethodBuilderT.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/PoolingAsyncValueTaskMethodBuilderT.cs index a69dbc041216d..5b89453b2e11c 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/PoolingAsyncValueTaskMethodBuilderT.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/PoolingAsyncValueTaskMethodBuilderT.cs @@ -356,14 +356,18 @@ private static ref StateMachineBox? PerCoreCacheSlot // Get the current processor ID. We need to ensure it fits within s_perCoreCache, so we // could % by its length, but we can do so instead by Environment.ProcessorCount, which will be a const // in tier 1, allowing better code gen, and then further use uints for even better code gen. - Debug.Assert(s_perCoreCache.Length == Environment.ProcessorCount); + Debug.Assert(s_perCoreCache.Length == Environment.ProcessorCount, $"{s_perCoreCache.Length} != {Environment.ProcessorCount}"); int i = (int)((uint)Thread.GetCurrentProcessorId() % (uint)Environment.ProcessorCount); // We want an array of StateMachineBox<> objects, each consuming its own cache line so that // elements don't cause false sharing with each other. But we can't use StructLayout.Explicit // with generics. So we use object fields, but always reinterpret them (for all reads and writes // to avoid any safety issues) as StateMachineBox<> instances. - Debug.Assert(s_perCoreCache[i].Object is null || s_perCoreCache[i].Object is StateMachineBox); +#if DEBUG + object? transientValue = s_perCoreCache[i].Object; + Debug.Assert(transientValue is null || transientValue is StateMachineBox, + $"Expected null or {nameof(StateMachineBox)}, got '{transientValue}'"); +#endif return ref Unsafe.As?>(ref s_perCoreCache[i].Object); } } @@ -385,7 +389,7 @@ public void ClearStateUponCompletion() private static void ExecutionContextCallback(object? s) { // Only used privately to pass directly to EC.Run - Debug.Assert(s is StateMachineBox); + Debug.Assert(s is StateMachineBox, $"Expected {nameof(StateMachineBox)}, got '{s}'"); Unsafe.As>(s).StateMachine!.MoveNext(); } @@ -402,7 +406,7 @@ public void MoveNext() if (context is null) { - Debug.Assert(StateMachine is not null); + Debug.Assert(StateMachine is not null, $"Null {nameof(StateMachine)}"); StateMachine.MoveNext(); } else diff --git a/src/libraries/System.Private.CoreLib/src/System/Threading/Tasks/Sources/ManualResetValueTaskSourceCore.cs b/src/libraries/System.Private.CoreLib/src/System/Threading/Tasks/Sources/ManualResetValueTaskSourceCore.cs index 692f2fa9da824..dc1fa4eb54648 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Threading/Tasks/Sources/ManualResetValueTaskSourceCore.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Threading/Tasks/Sources/ManualResetValueTaskSourceCore.cs @@ -107,7 +107,7 @@ private void ThrowForFailedGetResult(short token) } _error?.Throw(); - Debug.Fail("Should not get here"); + Debug.Fail($"{nameof(ThrowForFailedGetResult)} should never get here"); } /// Schedules the continuation action for this operation. @@ -250,8 +250,8 @@ private void InvokeContinuationWithContext() // for the surrounding code to become less efficent (stack spills etc) // and it is an uncommon path. - Debug.Assert(_continuation != null); - Debug.Assert(_executionContext != null); + Debug.Assert(_continuation != null, $"Null {nameof(_continuation)}"); + Debug.Assert(_executionContext != null, $"Null {nameof(_executionContext)}"); ExecutionContext? currentContext = ExecutionContext.CaptureForRestore(); // Restore the captured ExecutionContext before executing anything. @@ -321,8 +321,8 @@ private void InvokeContinuationWithContext() /// private void InvokeSchedulerContinuation() { - Debug.Assert(_capturedContext != null); - Debug.Assert(_continuation != null); + Debug.Assert(_capturedContext != null, $"Null {nameof(_capturedContext)}"); + Debug.Assert(_continuation != null, $"Null {nameof(_continuation)}"); switch (_capturedContext) {