tunnelvisionlabs / ReferenceAssemblyAnnotator

IL weaver to add nullability annotations to .NET reference assemblies
MIT License
71 stars 8 forks source link

Incorrect warning about StringComparer differing in nullability from IEqualityComparer<string?> #28

Closed jnm2 closed 5 years ago

jnm2 commented 5 years ago

I'm not sure of the cause, but I'm seeing this on net472 using alpha 53 and Microsoft.NETCore.App.Ref 3.0.0:

void M(IEnumerable<string?> values)
{
    // CS8620 Argument of type 'StringComparer' cannot be used for parameter 'comparer' of type
    // 'IEqualityComparer<string?>' in 'IEnumerable<string?> Enumerable.Distinct<string?>(
    // IEnumerable<string?> source, IEqualityComparer<string?> comparer)' due to differences in
    // the nullability of reference types.
    //              ↓
    values.Distinct(StringComparer.CurrentCultureIgnoreCase);
}

StringComparer does in fact implement IEqualityComparer<string?>, not IEqualityComparer<string>. If I target netcoreapp3.0, the warning goes away.

jnm2 commented 5 years ago

This didn't go away in alpha.69 and I'm just now realizing that it's not an incorrect warning when IEnumerable<> is invariant, as it is in net35.

For future readers, this is the link @sharwell gave when I was asking about ICollection<string> implicitly converting to IEnumerable<string?>: https://github.com/dotnet/roslyn/issues/38261