Skip to content

Incorrect state for some structs after being created with arguments #81892

@jcouv

Description

@jcouv

While working on nullability analysis of extension properties in object initializers, I noticed that we use the visit results from arguments to infer the initial state of the created struct (see VisitObjectCreationExpressionBase usage of inferInitialObjectState).
But the visit results are pre-conversion. Some conversions can change the nullable state of an expression. I think we should be using the post-conversion visit results. Those are made available conveniently as part of PR #81871

    [Fact]
    public void TODO2()
    {
        var src = """
#nullable enable

new System.ValueTuple<C?, object>(new D(), new object()).Item1.ToString(); // missing

new System.ValueTuple<C?, object>(null, new object()).Item1.ToString(); // 1

class D { }

class C
{
    public static implicit operator C?(D d) => null;
}
""";
        CreateCompilation(src).VerifyEmitDiagnostics(
            // (5,1): warning CS8602: Dereference of a possibly null reference.
            // new System.ValueTuple<C?, object>(null, new object()).Item1.ToString(); // 1
            Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "new System.ValueTuple<C?, object>(null, new object()).Item1").WithLocation(5, 1));
    }

Metadata

Metadata

Assignees

Type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions