Skip to content

Extensions: Confirm whether we should track nullable state for extension properties #81913

@jcouv

Description

@jcouv

We're waiting for additional customer feedback on this, so please do thumbs-up this issue and comment with some motivating usage scenarios.
The current design is that we don't track state for extension properties. Will double check.

    [Fact]
    public void Nullability_Setter_09()
    {
        // property's state
        var src = """
#nullable enable

object o = new object();
o.P = null;
o.P.ToString(); // 1
o.P.ToString(); // 2

o.P = new object();
o.P.ToString(); // 3
o.P.ToString(); // 4

static class E
{
    extension(object o)
    {
        public object? P { get => throw null!; set => throw null!; }
    }
}
""";
        CreateCompilation(src).VerifyEmitDiagnostics(
            // (5,1): warning CS8602: Dereference of a possibly null reference.
            // o.P.ToString(); // 1
            Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "o.P").WithLocation(5, 1),
            // (6,1): warning CS8602: Dereference of a possibly null reference.
            // o.P.ToString(); // 2
            Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "o.P").WithLocation(6, 1),
            // (9,1): warning CS8602: Dereference of a possibly null reference.
            // o.P.ToString(); // 3
            Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "o.P").WithLocation(9, 1),
            // (10,1): warning CS8602: Dereference of a possibly null reference.
            // o.P.ToString(); // 4
            Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "o.P").WithLocation(10, 1));

        src = """
#nullable enable

C o = new C();
o.P = null;
o.P.ToString(); // 1
o.P.ToString();

o.P = new object();
o.P.ToString();

class C
{
    public object? P { get => throw null!; set => throw null!; }
}
""";
        CreateCompilation(src).VerifyEmitDiagnostics(
            // (5,1): warning CS8602: Dereference of a possibly null reference.
            // o.P.ToString(); // 1
            Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "o.P").WithLocation(5, 1));
    }

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions