Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nullable state of field-like events is not tracked in structs #29901

Open
AlekseyTs opened this issue Sep 14, 2018 · 2 comments
Open

Nullable state of field-like events is not tracked in structs #29901

AlekseyTs opened this issue Sep 14, 2018 · 2 comments

Comments

@AlekseyTs
Copy link
Contributor

        // PROTOTYPE(NullableReferenceTypes): Events are not tracked for structs.
        // (This should be fixed if/when struct member state is populated lazily.)
        [Fact(Skip = "TODO")]
        public void Events_02()
        {
            CSharpCompilation c = CreateCompilation(new[] { @"
class Test
{
    static void Main()
    {
    }
}

struct TS1
{
    event System.Action? E1;

    TS1(System.Action x1) 
    {
        E1 = x1;
        System.Action y1 = E1 ?? x1;

        E1 = x1;
        TS1 z1 = this;
        y1 = z1.E1 ?? x1;
    }

    void Test3(System.Action x3)
    {
        TS1 s3;
        s3.E1 = x3;
        System.Action y3 = s3.E1 ?? x3;

        s3.E1 = x3;
        TS1 z3 = s3;
        y3 = z3.E1 ?? x3;
    }
}
", NonNullTypesTrue, NonNullTypesAttributesDefinition });

            c.VerifyDiagnostics(
                 // (16,28): hidden CS8607: Expression is probably never null.
                 //         System.Action y1 = E1 ?? x1;
                 Diagnostic(ErrorCode.HDN_ExpressionIsProbablyNeverNull, "E1").WithLocation(16, 28),
                 // (20,14): hidden CS8607: Expression is probably never null.
                 //         y1 = z1.E1 ?? x1;
                 Diagnostic(ErrorCode.HDN_ExpressionIsProbablyNeverNull, "z1.E1").WithLocation(20, 14),
                 // (27,28): hidden CS8607: Expression is probably never null.
                 //         System.Action y3 = s3.E1 ?? x3;
                 Diagnostic(ErrorCode.HDN_ExpressionIsProbablyNeverNull, "s3.E1").WithLocation(27, 28),
                 // (31,14): hidden CS8607: Expression is probably never null.
                 //         y3 = z3.E1 ?? x3;
                 Diagnostic(ErrorCode.HDN_ExpressionIsProbablyNeverNull, "z3.E1").WithLocation(31, 14)
                );
        }

        // PROTOTYPE(NullableReferenceTypes): Events are not tracked for structs.
        // (This should be fixed if/when struct member state is populated lazily.)
        [Fact(Skip = "TODO")]
        public void Events_03()
        {
            CSharpCompilation c = CreateCompilation(new[] { @"
class Test
{
    static void Main()
    {
    }
}

struct TS2
{
    event System.Action? E2;

    TS2(System.Action x2) 
    {
        this = new TS2();
        System.Action z2 = E2;
        System.Action y2 = E2 ?? x2;
    }
}

", NonNullTypesTrue, NonNullTypesAttributesDefinition });

            c.VerifyDiagnostics(
                 // (16,28): warning CS8600: Converting null literal or possible null value to non-nullable type.
                 //         System.Action z2 = E2;
                 Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "E2").WithLocation(16, 28)
                );
        }
@jcouv
Copy link
Member

jcouv commented Sep 17, 2018

I'm adding a reference to this issue in NullableWalker as well. I think it likely can be addressed at the same time. If not, we'll spawn a new issue.

        protected override bool TryGetReceiverAndMember(BoundExpression expr, out BoundExpression receiver, out Symbol member)
        {
            receiver = null;
            member = null;

            switch (expr.Kind)
            {
                case BoundKind.FieldAccess:
                    {
                        var fieldAccess = (BoundFieldAccess)expr;
                        var fieldSymbol = fieldAccess.FieldSymbol;
                        if (fieldSymbol.IsStatic || fieldSymbol.IsFixed)
                        {
                            return false;
                        }
                        member = fieldSymbol;
                        receiver = fieldAccess.ReceiverOpt;
                        break;
                    }
                case BoundKind.EventAccess:
                    {
                        var eventAccess = (BoundEventAccess)expr;
                        var eventSymbol = eventAccess.EventSymbol;
                        if (eventSymbol.IsStatic)
                        {
                            return false;
                        }
                        // PROTOTYPE(NullableReferenceTypes): Use AssociatedField for field-like events?
                        member = eventSymbol;
                        receiver = eventAccess.ReceiverOpt;
                        break;
                    }
                case BoundKind.PropertyAccess:
                    {
                        var propAccess = (BoundPropertyAccess)expr;
                        var propSymbol = propAccess.PropertySymbol;
                        if (propSymbol.IsStatic)
                        {
                            return false;
                        }
                        member = GetBackingFieldIfStructProperty(propSymbol);
                        receiver = propAccess.ReceiverOpt;
                        break;
                    }
            }

            return (object)member != null &&
                (object)receiver != null &&
                receiver.Kind != BoundKind.TypeExpression &&
                (object)receiver.Type != null;
        }

@jaredpar
Copy link
Member

Related to #29839

@jaredpar jaredpar modified the milestones: 16.0, 16.1.P1 Jan 27, 2019
@jaredpar jaredpar added this to Event in Nullable Board Jan 28, 2019
@gafter gafter modified the milestones: 16.1.P1, 16.1.P3 Apr 9, 2019
@jcouv jcouv modified the milestones: 16.1.P3, 16.1, 16.2 Apr 18, 2019
@jcouv jcouv modified the milestones: 16.2, Compiler.Next Jun 18, 2019
@jaredpar jaredpar modified the milestones: Compiler.Next, Backlog Sep 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

No branches or pull requests

4 participants