Branch master (20 Sep 2020)
Latest commit d148f06 by Sam Harwell:
Merge pull request #47537 from sharwell/non-null-compilation
Update SyntaxNodeAnalysisContext.Compilation to not return null
Steps to Reproduce:
Compile and run the following code:
#nullable enable
class C
{
public static void Main() => M(false);
public static void M(bool b)
{
(string foo, string bar) = b ? ("foo", "bar") : default;
foo.ToString();
}
}
Expected Behavior:
Warning for assigning non-nullable variable foo with possible null value
Warning for foo.ToString() as the deconstructed tuple can be default and return null values from its deconstruction
Actual Behavior:
No warnings. The code crashes with a NullReferenceException at runtime
Notes
Same problem here, the t ?? default expression can return default tuple with null components but Roslyn doesn't produce any warnings. This code also crashes at runtime with a NullReferenceException
#nullable enable
class C
{
public static void Main() => M(null);
public static void M((string, string)? t)
{
(string foo, string bar) = t ?? default;
foo.ToString();
}
}
Same problem with switch expressions:
#nullable enable
class C
{
public static void Main() => M(null);
public static void M((string, string)? t)
{
(string foo, string bar) = t switch { { } => t.Value, _ => default };
foo.ToString();
}
}
Version Used:
Steps to Reproduce:
Compile and run the following code:
Expected Behavior: Warning for assigning non-nullable variable
foo
with possible null value Warning forfoo.ToString()
as the deconstructed tuple can bedefault
and returnnull
values from its deconstructionActual Behavior: No warnings. The code crashes with a
NullReferenceException
at runtimeNotes Same problem here, the
t ?? default
expression can return default tuple with null components but Roslyn doesn't produce any warnings. This code also crashes at runtime with aNullReferenceException
Same problem with switch expressions: