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

No nullable warning issued for null event invocation #34982

Closed
stephentoub opened this issue Apr 15, 2019 · 6 comments
Closed

No nullable warning issued for null event invocation #34982

stephentoub opened this issue Apr 15, 2019 · 6 comments

Comments

@stephentoub
Copy link
Member

stephentoub commented Apr 15, 2019

Version Used:
3.1.0-beta1-19172-05+edd2de88fb3e84a097fb30b4070e0f219f624e40

Steps to Reproduce:

#nullable enable
using System;

public class Program
{
    static void Main()
    {
        var p = new Program();
        p.MyEvent(p, EventArgs.Empty);
    }

    public event EventHandler MyEvent;
}

No warning is issued here, but it will null ref.

cc: @dotnet/nullablefc

@HaloFour
Copy link

Why would this warn? The event (and the backing field) are both non-nullable. Is the suggestion that the backing field be annotated as nullable if nullability is enabled?

@stephentoub
Copy link
Member Author

There are two possible warnings here. Either:

  • it should consider MyEvent to be declaring a non-nullable instance field, in which case this should warn "warning CS8618: Non-nullable field 'MyEvent' is uninitialized.", or
  • it should consider MyEvent to be declaring a nullable instance field, in which case this should warn "warning CS8602: Dereference of a possibly null reference."

It's currently doing neither of those.

And it's super rare to initialize an event in a constructor, as 99.9% of the time events are meant to start life without any registrations. So I think declaring an event should map to the latter case, creating the equivalent of a nullable backing field.

But if for some reason we don't want to do that, as noted, this sample should still warn about something.

@HaloFour
Copy link

@stephentoub

Good point about the lack of warning on initialization.

Events seem like an odd case to me. As you mentioned the field, by default, is null, and it's unlikely that the class would initialize its own events to anything else. But it makes little sense for the accessors to be nullable.

So I agree, I think even with nullability enabled and the event accessors marked as non-nullable the backing field should still be nullable. I think that would do the most good.

@krwq
Copy link
Member

krwq commented Apr 17, 2019

should nullability only affect value in add and remove? Backing field will always be nullable and if declared explicitly then it should be taken care of manually

@jcouv
Copy link
Member

jcouv commented Jul 15, 2019

We've not implemented nullability analysis of events yet.
Relates to #29839

@stephentoub
Copy link
Member Author

Closing, as the compiler now treats events like fields, and so warns if you've not made the event nullable or initialized it to non-null, and if you make it nullable, then you get invocation warnings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants