roslyn icon indicating copy to clipboard operation
roslyn copied to clipboard

LINQ range variable types are forced to be non-nullable no matter what is assigned

Open jnm2 opened this issue 2 years ago • 2 comments

Version Used: Both 17.2.3 and 17.3.0-p1.1

This causes warnings further away in valid code where there is no problem. There should be no warning.

This also repros in a method body. SharpLab repro on main

#nullable enable

using System.Collections.Generic;
using System.Linq;

var enumerable =
    from number in new[] { 1, 2, 3 }
    let nullableThing = (string?)null
    select (number, nullableThing);

// ⚠️ CS8620 Argument of type 'IEnumerable<(int number, string nullableThing)>' cannot be used for parameter
// 'enumerable' of type 'IEnumerable<(int Number, string? NullableThing)>' in 'void M(IEnumerable<(int Number, string?
// NullableThing)> enumerable)' due to differences in the nullability of reference types.
M(enumerable.Select(tuple => (tuple.number, tuple.nullableThing)));

M(enumerable); // Oddly, no warning here

void M(IEnumerable<(int Number, string? NullableThing)> enumerable) { }

Quick info also demonstrates this problem:

image

image

Looks like range variables in from clauses have the same problem:

image

jnm2 avatar Jun 13 '22 21:06 jnm2

It gets weirder. Not sure what to make of this. Looks like tuples in the select clause get all elements forced to be typed as non-nullable too:

image

Non-tuples, too:

image

image

jnm2 avatar Jun 13 '22 21:06 jnm2

More weirdness. image

jnm2 avatar Jun 13 '22 21:06 jnm2