dotnet / dotnet/roslyn

No Compiler Warning when Nullability mismatches on a Generic struct being passed via an Interface

Open
#79,845 8 comments 3 reactions 1 assignee Claimed by @RikkiGibson View on GitHub
Area-Compilers Feature - Nullable Reference Types
Dominant language
C#
Stars
20.7k
Forks
4.3k
PR merge metrics
PR metrics pending

Description

```
IImmutableList moreNoNulls = new ImmutableArray();
```

This should give a warning (CS8619)... but doesn't.
Appears to be true for any generic struct implementing a generic interface.
Fails for gen. argument of any reference type; works fine if the generic arg is a struct (which makes sense)

**Steps to Reproduce**:

https://lab.razor.fyi/#jVJNbtQwFBaISuAV4gRWNiRomlIW_E2bqs20VVGnHZEWJBASjudNanDsyM-ZIUU9ASsWbNhyDQ7CLbgCSjKTmUxL1WxsP38_730O-blCyMDoxLDU5_jg20qOQiU0KtBC6odaSuBWaIX-PigwgnfJfxEHaZpbFkvoEsIlQ6RTZfKVUEqpUBbMiHGgB_3iYHbY0LmlJ0GXVBi0JueW9ouo2mycBPRlG95Aa4t-EZbrdUC0zApOx1oMaZ8J5XpVuW6q_NrtxJ-A24AyukkVTBqDur4VuF6Xrq3RngakiRgDnTCjykTC6PnT9RfXi8aN6HS-K1TVQ9sWduPcUjzTuRx6ZK5_KNBuTBMOqNJHuZQ41V-8m4lf6rhDY-AsR6jhARVYh-rPTZo3bbul2sBRy7EBbhvDimXvIz0z7TambUZpXj9-ZX5BLsijXyuhViOR-GF0xkw2YAbhOKt-NlfXK90M6HRb8fy3wp4dMpXkLIE3YFBo5S6d_YGBsYCJN2cMDGRGc0DUJirSWEt0nd7uzum-s4DaA2ZzA-hWpfdNSgomrpMjrBqNslCrVn8GJc7BOB3qWJOD43Xa4D0hYYchDKc5LQM_eMTrEtIaP9RpJiQrZ71JCNtS6smpQjYCt5RemKN8tzL4UCsLX-xM7eqyv6vK4gK9vEjFedXJIYxBupcqfg_iPFngRBlwMRK8J1iiNFrBZ65XZRlG688erzsd-hoybeyc5Ed5lhlAXA60Yjy5AaOd7G4qmvGvC7PE9cGyIbPsWMnCHTGJUEm9us3x3b27v__8_fF9dP_Ox1v_AA

```
using System.Collections.Generic;
using System.Collections.Immutable;

class Program
{
interface IMyInterface;

struct MyStruct : IMyInterface;

class MyClass : IMyInterface;

static void Main()
{
IMyInterface a = new MyClass(); // Does give warning CS8619
IMyInterface b = new MyStruct(); // Doesn't give warning (but should)

IList noNulls = new List(); //Does give warning, because List<> is class.
IImmutableList moreNoNulls = new ImmutableArray(); //No warning; because ImmutableArray is struct
}
}
```

See https://github.com/dotnet/csharplang/discussions/9596 and https://github.com/dotnet/runtime/issues/118517

**Expected Behavior**:
Should give warning CS8619;

**Actual Behavior**:
No warning or error; compiles fine;

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.