Roslyn: C# compiler should treat return value of SingleOrDefault as nullable for enumerables with reference types

Created on 28 Sep 2019  路  4Comments  路  Source: dotnet/roslyn

Version Used: .NET Core SDK 3.0.100

Steps to Reproduce:

#nullable enable

public class DemoClass
{
    public object DemoMethod() => new object[0].SingleOrDefault();
}

Expected Behavior:

C# compiler detects the return value of SingleOrDefault as nullable and therefore emits warning CS8603.

Actual Behavior:

No warning is emitted.

Area-Compilers Bug New Language Feature - Nullable Reference Types Resolution-By Design

Most helpful comment

Is this planned to be fixed for the 5.0 release? Having one of the cornerstone libraries in .NET not be aware of nullability is not great.

Given that it was apparently already in master in September, I don't understand why this wasn't included with 3.1.

All 4 comments

System.Linq is nullable annotated in master branch of corefx, but not in release/3.0. So there's nothing unexpected in compiler.
The compiler should provide some diagnostics showing that you are using nullable-oblivious stuff.

Running into this myself. I understand that we're going to have to wait for libraries (I wouldn't normally lump System.Linq under "libraries" but that applies here, I guess) to support the annotations. In the meantime, is there something one can do to generate warnings on nullable-oblivious dependencies? (Other than wrapping every one.)

Is this planned to be fixed for the 5.0 release? Having one of the cornerstone libraries in .NET not be aware of nullability is not great.

Given that it was apparently already in master in September, I don't understand why this wasn't included with 3.1.

@danstur,

TLDR;
Yes, it'ill be part of .net 5 release.

Long story:
I think the PR merged into master that @huoyaoyuan is mentionning is this one: https://github.com/dotnet/corefx/pull/40651

In the mean time the repository dotnet/corefx has been de-comitionned and the code can be found it dotnet/runtime.

In https://github.com/dotnet/runtime/blob/master/src/libraries/System.Linq/src/System/Linq/Single.cs#L86 for instance you can see that SingleOrDefault has been annotated. Thus, this will be part of .net 5 release.

Was this page helpful?
0 / 5 - 0 ratings