dotnet / dotnet/roslyn

Default literals should not contribute their type from initial binding to best-type in nullable analysis

Open
#74,693 0 comments 0 reactions 0 assignees View on GitHub
Area-Compilers Bug Feature - Nullable Reference Types
Dominant language
C#
Stars
20.7k
Forks
4.3k
PR merge metrics
PR metrics pending

Description

[SharpLab](https://sharplab.io/#v2:EYLgtghglgdgPgAQCwAICyAKYB7bAbFYASgFgAoAb3JRpQDcIAnFADxQF5CUB+FAEwCmAMwgBXPABcAhChAoYAgO4BtALooKAIgCMmgL4BuarRbKADOs4xxeAygD09lGCgBnV7ADmKRUxhfyPXIgA===)
```cs
void M(bool b)
{
var x = b ? default! : new[] {"1"};
x[0] = null; // missing warning
}
```

The problem is that the BoundDefaultExpression representing `default!`, which was created by target typing a BoundDefaultLiteral, is contributing its type from initial binding `string~[]` to the best-type analysis of the conditional operator.

We could fix this, but I consider it lower priority than the collection-expr cases and some of the other cases that just fell out from my work in #74490.

I think the way to fix this would be to make a change to treat `BoundDefaultExpression` as a target-typed expression, adding a `bool WasTargetTyped` to it, and fitting it into the target-typed completion mechanism so that the NullableWalker's assumptions are satisfied.

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.