dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
18.76k stars 3.99k forks source link

Roslyn fails to verify nullable reference types' safety in collection expressions #72142

Open TessenR opened 5 months ago

TessenR commented 5 months ago

Version Used:

Microsoft Visual Studio Professional 2022
Version 17.10.0 Preview 1.0
VisualStudio.17.Preview/17.10.0-pre.1.0+34607.79
Microsoft .NET Framework
Version 4.8.09037

Steps to Reproduce:

Compile and run the following code:

using System;
using System.Collections.Generic;
#nullable enable

List<string> list = [null];
Console.WriteLine(list[0].Length);

sharplab.io

Expected Behavior: There should be a warning about mismatching nullability somewhere on the line List<string> list = [null];

Actual Behavior: No warnings at all. The program crashes at runtime with a NullReferenceException

jaredpar commented 2 weeks ago

@cston believed this is fixed now. Can we close?

cston commented 2 weeks ago

This is still an issue. The compiler is not checking the nullability for elements of collection expressions for target types that implement IEnumerable and do not use [CollectionBuilder] (see sharplab.io).

See also https://github.com/dotnet/roslyn/issues/68786.