Skip to content

Commit

Permalink
Enable nullable for Microsoft.Bcl.AsyncInterfaces (#70454)
Browse files Browse the repository at this point in the history
* Enable nullable for Microsoft.Bcl.AsyncInterfaces

* Apply suggestions from code review

Co-authored-by: Eric Erhardt <eric.erhardt@microsoft.com>

Co-authored-by: Eric Erhardt <eric.erhardt@microsoft.com>
  • Loading branch information
stephentoub and eerhardt committed Jun 9, 2022
1 parent fcbf91d commit 3d74b00
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public partial struct ManualResetValueTaskSourceCore<TResult>
public short Version { get { throw null; } }
public TResult GetResult(short token) { throw null; }
public System.Threading.Tasks.Sources.ValueTaskSourceStatus GetStatus(short token) { throw null; }
public void OnCompleted(System.Action<object> continuation, object state, short token, System.Threading.Tasks.Sources.ValueTaskSourceOnCompletedFlags flags) { }
public void OnCompleted(System.Action<object?> continuation, object? state, short token, System.Threading.Tasks.Sources.ValueTaskSourceOnCompletedFlags flags) { }
public void Reset() { }
public void SetException(System.Exception error) { }
public void SetResult(TResult result) { }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;$(NetFrameworkMinimum);netstandard2.1</TargetFrameworks>
<Nullable>disable</Nullable>
</PropertyGroup>
<ItemGroup Condition="'$(TargetFramework)' != 'netstandard2.1'">
<Compile Include="Microsoft.Bcl.AsyncInterfaces.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;$(NetFrameworkMinimum);netstandard2.1</TargetFrameworks>
<Nullable>disable</Nullable>
<IsPackable>true</IsPackable>
<!-- This assembly should never be placed inbox as it is only for downlevel compatibility. -->
<PackageDescription>Provides the IAsyncEnumerable&lt;T&gt; and IAsyncDisposable interfaces and helper types for .NET Standard 2.0. This package is not required starting with .NET Standard 2.1 and .NET Core 3.0.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,22 @@ public struct ManualResetValueTaskSourceCore<TResult>
/// or <see cref="ManualResetValueTaskSourceCoreShared.s_sentinel"/> if the operation completed before a callback was supplied,
/// or null if a callback hasn't yet been provided and the operation hasn't yet completed.
/// </summary>
private Action<object> _continuation;
private Action<object?>? _continuation;
/// <summary>State to pass to <see cref="_continuation"/>.</summary>
private object _continuationState;
private object? _continuationState;
/// <summary><see cref="ExecutionContext"/> to flow to the callback, or null if no flowing is required.</summary>
private ExecutionContext _executionContext;
private ExecutionContext? _executionContext;
/// <summary>
/// A "captured" <see cref="SynchronizationContext"/> or <see cref="TaskScheduler"/> with which to invoke the callback,
/// or null if no special context is required.
/// </summary>
private object _capturedContext;
private object? _capturedContext;
/// <summary>Whether the current operation has completed.</summary>
private bool _completed;
/// <summary>The result with which the operation succeeded, or the default value if it hasn't yet completed or failed.</summary>
private TResult _result;
private TResult? _result;
/// <summary>The exception with which the operation failed, or null if it hasn't yet completed or completed successfully.</summary>
private ExceptionDispatchInfo _error;
private ExceptionDispatchInfo? _error;
/// <summary>The current version of this value, used to help prevent misuse.</summary>
private short _version;

Expand Down Expand Up @@ -105,15 +105,15 @@ public TResult GetResult(short token)
}

_error?.Throw();
return _result;
return _result!;
}

/// <summary>Schedules the continuation action for this operation.</summary>
/// <param name="continuation">The continuation to invoke when the operation has completed.</param>
/// <param name="state">The state object to pass to <paramref name="continuation"/> when it's invoked.</param>
/// <param name="token">Opaque value that was provided to the <see cref="ValueTask"/>'s constructor.</param>
/// <param name="flags">The flags describing the behavior of the continuation.</param>
public void OnCompleted(Action<object> continuation, object state, short token, ValueTaskSourceOnCompletedFlags flags)
public void OnCompleted(Action<object?> continuation, object? state, short token, ValueTaskSourceOnCompletedFlags flags)
{
if (continuation is null)
{
Expand All @@ -128,7 +128,7 @@ public void OnCompleted(Action<object> continuation, object state, short token,

if ((flags & ValueTaskSourceOnCompletedFlags.UseSchedulingContext) != 0)
{
SynchronizationContext sc = SynchronizationContext.Current;
SynchronizationContext? sc = SynchronizationContext.Current;
if (sc != null && sc.GetType() != typeof(SynchronizationContext))
{
_capturedContext = sc;
Expand All @@ -151,7 +151,7 @@ public void OnCompleted(Action<object> continuation, object state, short token,
// To minimize the chances of that, we check preemptively whether _continuation
// is already set to something other than the completion sentinel.

object oldContinuation = _continuation;
object? oldContinuation = _continuation;
if (oldContinuation == null)
{
_continuationState = state;
Expand All @@ -175,7 +175,7 @@ public void OnCompleted(Action<object> continuation, object state, short token,
case SynchronizationContext sc:
sc.Post(s =>
{
var tuple = (Tuple<Action<object>, object>)s;
var tuple = (Tuple<Action<object?>, object?>)s!;
tuple.Item1(tuple.Item2);
}, Tuple.Create(continuation, state));
break;
Expand Down Expand Up @@ -212,7 +212,7 @@ private void SignalCompletion()
{
ExecutionContext.Run(
_executionContext,
s => ((ManualResetValueTaskSourceCore<TResult>)s).InvokeContinuation(),
s => ((ManualResetValueTaskSourceCore<TResult>)s!).InvokeContinuation(),
this);
}
else
Expand Down Expand Up @@ -247,7 +247,7 @@ private void InvokeContinuation()
case SynchronizationContext sc:
sc.Post(s =>
{
var state = (Tuple<Action<object>, object>)s;
var state = (Tuple<Action<object?>, object?>)s!;
state.Item1(state.Item2);
}, Tuple.Create(_continuation, _continuationState));
break;
Expand All @@ -261,8 +261,8 @@ private void InvokeContinuation()

internal static class ManualResetValueTaskSourceCoreShared // separated out of generic to avoid unnecessary duplication
{
internal static readonly Action<object> s_sentinel = CompletionSentinel;
private static void CompletionSentinel(object _) // named method to aid debugging
internal static readonly Action<object?> s_sentinel = CompletionSentinel;
private static void CompletionSentinel(object? _) // named method to aid debugging
{
Debug.Fail("The sentinel delegate should never be invoked.");
throw new InvalidOperationException();
Expand Down

0 comments on commit 3d74b00

Please sign in to comment.