Skip to content

Add nullable annotation to the parameter types of equality operators#40838

Merged
y87feng merged 8 commits intodotnet:masterfrom
y87feng:40053
Jan 24, 2020
Merged

Add nullable annotation to the parameter types of equality operators#40838
y87feng merged 8 commits intodotnet:masterfrom
y87feng:40053

Conversation

@y87feng
Copy link
Copy Markdown
Contributor

@y87feng y87feng commented Jan 9, 2020

Add nullable annotation to the parameter types of equality operators #40053

@y87feng y87feng requested a review from a team as a code owner January 9, 2020 01:44

public static bool operator ==(C? left, C? right)
{
return global::System.Collections.Generic.EqualityComparer<global::N.C>.Default.Equals(left, right);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

we should not be generated global:: here.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is a separate issue. @y87feng do you mind filing? I can help you file as needed.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

not certain how it's a different issue :) isn't this caused because of the new usage of nullable types heres?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I don't think it is because of the new usage of nullable types:) I can check if there is an issue already

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm curious what woudl cause it then. None of hte other tests exhibit htis issue.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The behavior existed before this change. It might be related to enabling nullable/using csharp 8, but was not directly caused by this change.

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateEqualsAndGetHashCode)]
public async Task TestEqualsNullableAnnotation()
{
await TestWithPickMembersDialogAsync(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

does this need to go through the 'pick members dialog' test path? this doesn't feel dialog specific.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This code indicates that I cannot test "generate operators" without having a dialog, then I think "pick members dialog" is necessary

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

you could definitely update that code :) just add a default generateOperators to http://sourceroslyn.io/#Microsoft.CodeAnalysis.Features/GenerateEqualsAndGetHashCodeFromMembers/GenerateEqualsAndGetHashCodeFromMembersCodeRefactoringProvider.cs,239 and pass that along. No tests need to change, but you can now pass generateOperators: true from your test.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What benefit is there in changing the current behavior? Current code design is that a dialog is required to allow generation of operators. If we change the code, that distinction becomes harder to follow. Testing with a mocked out dialog is not an issue

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@CyrusNajmabadi this seems to be the only outstanding issue. I'm approving as I don't see this blocking, but want to wait for your feedback before merging.

CodeGenerationSymbolFactory.CreateParameterSymbol(_containingType, LeftName),
CodeGenerationSymbolFactory.CreateParameterSymbol(_containingType, RightName));
CodeGenerationSymbolFactory.CreateParameterSymbol(_containingType.WithNullableAnnotation(NullableAnnotation.Annotated), LeftName),
CodeGenerationSymbolFactory.CreateParameterSymbol(_containingType.WithNullableAnnotation(NullableAnnotation.Annotated), RightName));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this warrants a comment here as to waht we're doing.


[WorkItem(40053, "https://github.com/dotnet/roslyn/issues/40053")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateEqualsAndGetHashCode)]
public async Task TestEqualsNullableAnnotation()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

One case we need to make sure is tested is generating operators on value types, which should not annotated the same as reference types.

Copy link
Copy Markdown
Contributor

@CyrusNajmabadi CyrusNajmabadi left a comment

Choose a reason for hiding this comment

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

c

@ryzngard ryzngard self-assigned this Jan 10, 2020
@y87feng y87feng requested a review from ryzngard January 21, 2020 20:55

var generator = _document.GetLanguageService<SyntaxGenerator>();

// add nullable annotation to the parameter reference type, so the value could be null
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This comment is telling us what the code is doing, but not really why.

Needs explanation related to the fact that (in)equality operator implementations for reference types need to allow comparison against null.

Copy link
Copy Markdown
Contributor

@ryzngard ryzngard left a comment

Choose a reason for hiding this comment

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

Small improvement to be made on comment. Otherwise LGTM. Please wait for @CyrusNajmabadi follow up on issue before merging.

@y87feng y87feng merged commit ec6e027 into dotnet:master Jan 24, 2020
@jinujoseph jinujoseph added the Community The pull request was submitted by a contributor who is not a Microsoft employee. label Jan 25, 2020
@jinujoseph jinujoseph added this to the 16.5.P3 milestone Jan 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area-IDE Community The pull request was submitted by a contributor who is not a Microsoft employee. Feature - Nullable Reference Types Nullable Reference Types

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants