dotnet / dotnet/roslyn

Implicitly typed lambdas returning arrays can have unexpected oblivious element nullability

Open
#74,609 0 comments 0 reactions 0 assignees 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

Found during #74490

[SharpLab](https://sharplab.io/#v2:EYLgtghglgdgNAFxBAzggPgYhgVwDZ4TB4CmABCTEaQLABQ9AAgMxmMBMZAwvQN71lBbVowCMABgDaAXTIBZUQApgAexV4ywAJQCh/OkMNkAbhABOZQmDIBeMoq22AfLqNl9bt1ABm97a883RgB2MkkAIlFw6QBuegDAkLIYEgB3GXcycPZwsgBfOINPAviit1MLcws7KwdCwKrJcVk7XAIYsgB6TrJRBKEkqvqhPNLDFjYJDLl2ZTUNfzL3fsEKywhrOwdnZLSM3iyc/OHy8zIq23WwOpXzs2rNMgB+MMjoshA7sxOjRubLtp4DrdMjsW6De4/UZ0PJAA==)

```cs
#nullable enable

public class C
{
public string[] M1(bool b)
{
var lam = () =>
{
if (b)
return ["1"];

return new[] { "2" };
};

var arr = lam();
arr[0] = null; // 1
return arr;
}

public string[] M2(bool b)
{
var lam = () => new[] { "2" };
var arr = lam();
arr = b ? ["1"] : arr;
arr[0] = null; // 2
return arr;
}
}
```

**Expected behavior**: Nullable warnings are reported at both locations that null is assigned to the resulting array
**Actual behavior**: No nullable warnings in the sample

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the nullable-analysis sample in the linked SharpLab snippet, focusing on the two implicitly typed lambda return cases and the assignments marked 1 and 2. Trace how the resulting array element nullability is inferred; done means nullable warnings are reported at both null assignments.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.