Nullable: CancellationToken#23609
Conversation
| if (_kernelEvent != null) | ||
| { | ||
| ManualResetEvent mre = Interlocked.Exchange(ref _kernelEvent, null); | ||
| ManualResetEvent? mre = Interlocked.Exchange<ManualResetEvent?>(ref _kernelEvent!, null); |
There was a problem hiding this comment.
@jaredpar, I'm confused by the changes that were necessary on this line. Here _kernelEvent is declared as a ManualResetEvent?. As written here:
ManualResetEvent? mre = Interlocked.Exchange<ManualResetEvent?>(ref _kernelEvent!, null)it compiles without warning.
However, this:
ManualResetEvent? mre = Interlocked.Exchange(ref _kernelEvent, null);warns on the null argument that Cannot convert null literal to non-nullable reference or unconstrained type parameter and hovering over Exchange to get IntelliSense for it shows that it inferred the type of T to be ManualResetEvent rather than ManualResetEvent?, even though _kernelEvent is ManualResetEvent?. Is it somehow getting messed up by the _kernelEvent != null guard above it?
Similarly, if I change it to be:
ManualResetEvent? mre = Interlocked.Exchange<ManualResetEvent?>(ref _kernelEvent, null);that warns on the _kernelEvent argument "Possible null reference assignment", which should be fine, given that the type of T here is nullable.
Is this by design or a bug?
There was a problem hiding this comment.
Yes, the null check is causing ref _kernelEvent to be inferred as non-nullable.
| /// be used to unregister the callback.</returns> | ||
| /// <exception cref="T:System.ArgumentNullException"><paramref name="callback"/> is null.</exception> | ||
| public CancellationTokenRegistration Register(Action<object> callback, object state) => | ||
| public CancellationTokenRegistration Register(Action<object?> callback, object? state) => |
There was a problem hiding this comment.
@jcouv, there are a bunch of places in the framework where we take an Action<object> and an object state to pass to it. Thus, the object in Action<object> callback will be null if and only if state is null. Do we have any attribution planned that will allow for that relationship to be expressed? I don't yet know if it would actually matter much, so just asking right now.
There was a problem hiding this comment.
We have not discussed any such attribute yet.
Feel free to add a comment with scenario details in dotnet/roslyn#26761
Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
Signed-off-by: dotnet-bot <anirudhagnihotry098@gmail.com>
Signed-off-by: dotnet-bot <anirudhagnihotry098@gmail.com>
Signed-off-by: dotnet-bot <anirudhagnihotry098@gmail.com>
Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
Commit migrated from dotnet/coreclr@0efef3e
No description provided.