Skip to content

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

@RikkiGibson

Description

@RikkiGibson

SharpLab

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions