roslyn
roslyn copied to clipboard
LINQ range variable types are forced to be non-nullable no matter what is assigned
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:
Looks like range variables in from
clauses have the same problem:
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:
Non-tuples, too:
More weirdness.