roslyn: False positive: CS8600 - Converting null literal or possible null value to non-nullable type
Version Used: VS 17.8.7 (Compiler Version 4.8.0-7.23572.1, Language Version 12.0)
Steps to Reproduce:
Try to compile the code below (see also sharplab).
#nullable enable
using System.Diagnostics.CodeAnalysis;
public interface IValueHolder<out T>
{
public T? Value { get; }
public bool HasValue { get; set; }
}
public class ValueHolder<T> : IValueHolder<T>
{
public ValueHolder(T value)
{
this.Value = value;
this.HasValue = true;
}
public ValueHolder()
{
}
public T? Value { get; }
public bool HasValue { get; set; }
}
public static class ValueHolderExtensions
{
public static bool TryGetValue<T>(this IValueHolder<T> holder, [MaybeNullWhen(false)] out T value)
{
value = holder.HasValue ? holder.Value : default;
return holder.HasValue;
}
}
public static class Test
{
public static void Run()
{
var holder = new ValueHolder<string>("foo");
if (holder.TryGetValue(out string? value))
{
string copy = value; // !!!! WARNING !!!!
}
}
}
Diagnostic Id: CS8600
Expected Behavior:
Should compile without warning.
Actual Behavior:
Issues a warning.
Observation:
If you remove the covariance from the interface definition like
public interface IValueHolder<T>
…, the code compiles without warning.
About this issue
- Original URL
- State: open
- Created 3 years ago
- Comments: 16 (9 by maintainers)
To me, this looks correct. The type parameter to
IValueHolder<out T>isoutin variance, so the extension method call toTryGetValue<T>is called as:At this point, the note from a little later in the same bullet point of the linked nullability guidelines comes into play:
As evidence of this, the warning goes away (8.0.203!) if the call is made in one of these ways:
…though IDE0001 kicks in for me for the type argument for the latter. Another option would be to implement
TryGetValueas an instance method:This also produces no warning, but I don’t know what business value
IValueHolder<out T>is providing you, so I couldn’t concretely say that this is a better solution. It couldn’t be added to that interface because of the variance.In general, the nullable annotation of the variable we are assigning to, does not affect the analyzed nullability of the thing we are assigning. e.g. in
string? x = M(...)orstring x = M(...)orvar x = M(...)the nullable flow state ofxis always the same, it is whatever we got out ofM(assuming no weird conversions stuff is in play).I am tempted to think of an out argument as being the same thing. It doesn’t make sense to me that an out argument allowing null or not, affects whether we think null is actually being written to it. The only exception is when nothing else is providing a bounds for a type parameter, and the out argument’s annotation is really the user telling us what nullability is going into the variable.
That said, I am not sure if there is a simple and sensible way to encode this idea into the type argument inference rules, and in a way which justifies the breaking(?) change and time investment.
It does disturb me a bit that the user is sort of in a corner here. I assume a warning will occur if
out stringexplicitly used, and it feels like usingvarhere is somewhat a “trick”, to let the variable be nullable without having to contribute a nullable bounds to the type inference.There is indeed a problem with using
[NotNullWhen(true)] out T? value. IfTis substituted withstring?then the nullability analysis would be wrong. So I think the .NET API guideline is correct after all.I feel there is some bug here because:
<string>type argument toTryGetValuevaras implicitly nullable-annotated, so it doesn’t make sense to me thatvaris not contributing its nullability to type inference whilestring?is contributing its nullability.Curious what you think @jcouv.
I am one of hte lang designers 😄