Skip to content

Enable nullability in some Control members#7491

Merged
RussKie merged 2 commits intodotnet:mainfrom
gpetrou:ControlMembers
Oct 27, 2022
Merged

Enable nullability in some Control members#7491
RussKie merged 2 commits intodotnet:mainfrom
gpetrou:ControlMembers

Conversation

@gpetrou
Copy link
Contributor

@gpetrou gpetrou commented Jul 25, 2022

Proposed changes

  • Enable nullability in some Control members.
Microsoft Reviewers: Open in CodeFlow

@gpetrou gpetrou requested a review from a team as a code owner July 25, 2022 09:14
@ghost ghost assigned gpetrou Jul 25, 2022
@ghost ghost added the area: NRT label Jul 25, 2022
// but changing the order in Whidbey would be too much of a breaking change
// for this particular class.
base.OnMouseWheel(e);
base.OnMouseWheel(e!);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure what to do about these cases. Should we make the args nullable in all classes that have OnMouseWheel instead?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By convention e can't be null, all event handlers are annotated as (object? sender, EventArgs e).
I've searched the codebase for protected override void On.*(\wEventArgs .*) regex pattern, and we have 322 methods that consume derived eventargs. I haven't checked all 322 instances, but those I checked - don't null-check e (e.g., painting, keyboard or mouse handling).

This said, I'm a bit split about this, as removing the null-check is a technically a breaking change. I'm going to take this to the team discussion.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any more thoughts about this? Perhaps the safest thing to do for now is just keep the null forgiving operators we have in the PR in order to avoid any behavioral changes?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies, it looks like I have failed to respond after this was discussed at a team meeting, and now I'm scratching my head trying to page back the decision.

If I recall correctly, we agreed on the following:

  1. We can't assume something we know may be a null as "not null".
  2. We can't throw a ANE here as it would be a behaviour breaking change.
  3. We should throw a ANE in the base, as it would be within the "acceptable" breaking change rules - i.e., throwing a ANE instead of a NRE:
    protected virtual void OnMouseWheel(MouseEventArgs e)
    {
    ((MouseEventHandler?)Events[s_mouseWheelEvent])?.Invoke(this, e);
    }

Though, testing it locally it doesn't appear OnMouseWheel(null!) throws anything. 🤔

@JeremyKuhne am I misremembering something?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have this right, @RussKie. We generally don't want to introduce ANE in existing APIs with the specific exception of when we know we're about to throw NRE (in the same method).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, so what should be the next step? If I change this to be nullable, I suspect that there will be some places where we should add null checks because we override this in classes that inherit from Control. If I change it to throw an ANE, I suspect that there will be changes in tests and therefore behavior. I still think that using ! is probably the safest option for now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is a headscratcher, isn't it? :)

I think it's probably acceptable to use ! here with an added comment (like we've done in other instances) stating that this has been like this for a very long time, and we believe this should never be null. If/when we get a customer feedback we can review this, if required.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RussKie I have added the comments. Perhaps we could merge this now?

Copy link
Contributor

@RussKie RussKie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These three identified methods that check incoming eventargs look very odd, we don't normally null-check e (e.g., painting, keyboard or mouse handling).

@RussKie RussKie added the waiting-on-team This work item needs to be discussed with team or is waiting on team action in order to proceed label Jul 27, 2022
@Tanya-Solyanik Tanya-Solyanik added the tenet-accessibility MAS violation, UIA issue; problems with accessibility standards label Aug 7, 2022
@Tanya-Solyanik Tanya-Solyanik removed the tenet-accessibility MAS violation, UIA issue; problems with accessibility standards label Aug 19, 2022
@gpetrou
Copy link
Contributor Author

gpetrou commented Oct 2, 2022

@RussKie any more feedback on this?

@RussKie RussKie requested a review from JeremyKuhne October 4, 2022 06:10
@gpetrou gpetrou force-pushed the ControlMembers branch 4 times, most recently from c3f3d01 to d38e3a8 Compare October 24, 2022 09:09
@RussKie RussKie merged commit 32c8792 into dotnet:main Oct 27, 2022
@ghost ghost modified the milestones: .NET 8.0, 8.0 Preview1 Oct 27, 2022
@RussKie
Copy link
Contributor

RussKie commented Oct 27, 2022

Thank you

@RussKie RussKie removed the waiting-on-team This work item needs to be discussed with team or is waiting on team action in order to proceed label Oct 27, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Nov 27, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants