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

Pulling up an event to an interface should not add/reproduce the nullable annotation #40292

Closed
Tragetaschen opened this issue Dec 11, 2019 · 8 comments
Labels
Area-IDE Bug help wanted The issue is "up for grabs" - add a comment if you are interested in working on it IDE-CodeStyle Built-in analyzers, fixes, and refactorings New Language Feature - Nullable Reference Types Nullable Reference Types
Milestone

Comments

@Tragetaschen
Copy link

Version Used:
16.4.1

Steps to Reproduce:

#nullable enable
using System;

public interface ITest
{
}

public class Test : ITest
{
    public event Action? E; // run "Pull 'E' up to 'ITest'" here
}

Expected Behavior:

 public interface ITest
 {
+    event Action E;
 }

Actual Behavior:

 public interface ITest
 {
+    event Action? E;
 }
@jcouv jcouv added the Area-IDE label Dec 12, 2019
@vatsalyaagrawal vatsalyaagrawal added Bug help wanted The issue is "up for grabs" - add a comment if you are interested in working on it labels Dec 19, 2019
@vatsalyaagrawal vatsalyaagrawal added IDE-CodeStyle Built-in analyzers, fixes, and refactorings New Language Feature - Nullable Reference Types Nullable Reference Types labels Dec 19, 2019
@vatsalyaagrawal vatsalyaagrawal added this to the 16.5 milestone Dec 19, 2019
@jinujoseph jinujoseph modified the milestones: 16.5, Backlog Jan 14, 2020
@stephentoub
Copy link
Member

stephentoub commented Aug 24, 2020

I don't know why the annotation would be dropped here. All events are expected to support adding/removing null delegates, and upon implementing the interface, having the type be non-nullable will lead to warnings (and then if they're suppressed, likely false negatives upon invocation of the event).

@CyrusNajmabadi
Copy link
Member

This is a bit of an interesting case in that i don't think a user externally could ever observe the nullability of these events. i.e. when accessing through the interface, you can't ever refer to the 'Action' value itself (in order to observe it's null/non-null-ness). All you can do is ever do += and -=, and tehse will always be safe.

So i would ask:

  1. should we disallow/warn when using ? on event members in interfaces?
  2. should we make the change the OP asks for.

All events are expected to support adding/removing null delegates, and upon implementing the interface, having the type be non-nullable will lead to warnings

Is this the case? I don't get any warnings with this @stephentoub 👍

    class Program
    {
        public void DoWork(ITest i)
        {
            Action? e = null;
            i.E += e;
        }
    }

    public interface ITest
    {
        event Action E;
    }

@jcouv for thoughts. I'm not sure how much of NRT+events is intentional, or just where we've landed today unintentionally.

@stephentoub
Copy link
Member

stephentoub commented Aug 24, 2020

@CyrusNajmabadi
Copy link
Member

Hrm.. we may want to reconsider that. Esp. since we've now shipped with thsi behavior and likely could not change it. i.e. it seems like we do respect this:

but the += and -= are also nullable

We can't now null check these and introduce new warnigns (esp. on code which seems like it should be totally correct and would not throw).

I guess we should take this to LDM to see waht to do here. @jcouv do you want to drive?

@stephentoub
Copy link
Member

stephentoub commented Aug 24, 2020

Esp. since we've now shipped with thsi behavior and likely could not change it.

Shipped when? We've made plenty of changes for .NET 5 that introduce new nullable warnings. That was part of the deal for rolling out nullable reference types.

@stephentoub
Copy link
Member

esp. on code which seems like it should be totally correct and would not throw

If it's declared to be non-nullable, throwing is perfectly valid behavior if null is passed in. Such non-nullability is simply considered bad form for events.

@jcouv
Copy link
Member

jcouv commented Aug 24, 2020

We currently do very little nullability analysis of events (known gap). It's still possible to fix some of those for .NET 5.
Tracked by #29839

@CyrusNajmabadi
Copy link
Member

Closing out due to lack of feedback here.

@CyrusNajmabadi CyrusNajmabadi closed this as not planned Won't fix, can't repro, duplicate, stale Oct 21, 2024
@dotnet dotnet locked and limited conversation to collaborators Oct 21, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Area-IDE Bug help wanted The issue is "up for grabs" - add a comment if you are interested in working on it IDE-CodeStyle Built-in analyzers, fixes, and refactorings New Language Feature - Nullable Reference Types Nullable Reference Types
Projects
None yet
Development

No branches or pull requests

6 participants