Version Used: Visual Studio 16.4 preview 4
Steps to Reproduce:
Compile the following:
using System;
public class C
{
public ReadOnlySpan<char> M(string? s)
{
return s?.AsSpan() ?? throw new ArgumentNullException();
}
}
Expected Behavior:
Compiles without error
Actual Behavior:
Error CS0023 Operator '?' cannot be applied to operand of type 'ReadOnlySpan
@CyrusNajmabadi has very kindly explained to me why this error occurs, but I think the error message is extremely confusing, so I will leave this open so it can hopefully be labelled with diagnostic-clarity.
A better message would be "? operator cannot be used in expression returning type of ReadOnlySpan as it would require creating a temporary Nullable of a ref type".
Seems like when the type ReadOnlySpan<char>? appears in source we use this diagnostic:
<data name="ERR_BadTypeArgument" xml:space="preserve">
<value>The type '{0}' may not be used as a type argument</value>
</data>
Don't know how helpful that is though for providing inspiration for how to phrase a better diagnostic for a?.AsSpan().
Most helpful comment
@CyrusNajmabadi has very kindly explained to me why this error occurs, but I think the error message is extremely confusing, so I will leave this open so it can hopefully be labelled with diagnostic-clarity.
A better message would be "? operator cannot be used in expression returning type of
ReadOnlySpanas it would require creating a temporary Nullable of a ref type".