Skip to content

Synthesize equality operators for records.#46497

Merged
AlekseyTs merged 4 commits intodotnet:masterfrom
AlekseyTs:Records_22
Aug 4, 2020
Merged

Synthesize equality operators for records.#46497
AlekseyTs merged 4 commits intodotnet:masterfrom
AlekseyTs:Records_22

Conversation

@AlekseyTs
Copy link
Contributor

Closes #46381.

@AlekseyTs
Copy link
Contributor Author

AlekseyTs commented Aug 2, 2020

@cston, @jcouv, @333fred, @RikkiGibson, @dotnet/roslyn-compiler Please review. #Closed

1 similar comment
@AlekseyTs
Copy link
Contributor Author

AlekseyTs commented Aug 3, 2020

@cston, @jcouv, @333fred, @RikkiGibson, @dotnet/roslyn-compiler Please review. #Closed


if (IsRecord)
{
// For records the warnings reported below are simply going to eco record specific errors,
Copy link
Contributor

@cston cston Aug 3, 2020

Choose a reason for hiding this comment

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

eco [](start = 79, length = 3)

Typo? #Resolved

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Typo?

Yes, thanks


In reply to: 464556651 [](ancestors = 464556651)

/// The record type includes synthesized '==' and '!=' operators equivalent to operators declared as follows:
///
/// public static bool operator==(R? r1, R? r2)
/// => (object) r1 == r2 || (r1?.Equals(r2) ?? false);
Copy link
Contributor

@cston cston Aug 3, 2020

Choose a reason for hiding this comment

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

(r1?.Equals(r2) ?? false) [](start = 37, length = 25)

Please consider changing this to match the implementation: (object)r1 != null && r1.Equals(r2)

Same comment for other classes. #Resolved


try
{
// => (object)r1 == r2 || (r1?.Equals(r2) ?? false);
Copy link
Contributor

@cston cston Aug 3, 2020

Choose a reason for hiding this comment

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

(r1?.Equals(r2) ?? false) [](start = 42, length = 25)

(object)r1 != null && r1.Equals(r2)

Same comment for InequalityOperator. #Resolved

=> throw null;

System.Boolean System.IEquatable<A>.Equals(A x) => throw null;
}").WithArguments("System.Boolean").WithLocation(2, 1),
Copy link
Contributor

@cston cston Aug 3, 2020

Choose a reason for hiding this comment

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

Can the scope of the error be reduced to the type name rather than the entire declaration? #Resolved

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can the scope of the error be reduced to the type name rather than the entire declaration?

That would be rather difficult to do because I believe that error is coming from bound node factory that is using syntax node as location for the error to report. The name is not a node, but a token, I believe. Given that this is an edge case, if core library is missing, there would be more errors all over the place, I think the current behavior is acceptable.


In reply to: 464575538 [](ancestors = 464575538)

static void Test(A a1, A a2)
{
System.Console.WriteLine(""{0} {1} {2} {3}"", a1 == a2, a2 == a1, a1 != a2, a2 != a1);
}
Copy link
Contributor

@cston cston Aug 3, 2020

Choose a reason for hiding this comment

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

} [](start = 4, length = 1)

Please test a1 == a1, a1 != a1 as well. #Resolved

Test(null, new A(0));
Test(new A(1), new A(1));
Test(new A(2), new A(3));
}
Copy link
Contributor

@cston cston Aug 3, 2020

Choose a reason for hiding this comment

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

} [](start = 4, length = 1)

Consider testing Test(new A(3), new B()); #Resolved

@AlekseyTs
Copy link
Contributor Author

AlekseyTs commented Aug 3, 2020

@jcouv, @333fred, @RikkiGibson, @dotnet/roslyn-compiler Please review, need a second sign-off. #Closed

BoundExpression recordEquals = F.LogicalAnd(F.ObjectNotEqual(r1, F.Null(F.SpecialType(SpecialType.System_Object))),
F.Call(r1, equals, r2));

F.CloseMethod(F.Block(F.Return(F.LogicalOr(objectEqual, recordEquals))));
Copy link
Member

@jcouv jcouv Aug 3, 2020

Choose a reason for hiding this comment

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

nit: we probably don't need F.Block. Also applies in inequality operator #Resolved

Copy link
Contributor Author

Choose a reason for hiding this comment

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

nit: we probably don't need F.Block. Also applies in inequality operator

Why is that? I do not have any appetite to experiment, when there is a wide spread pattern that works.


In reply to: 464596845 [](ancestors = 464596845)

Copy link
Member

@jcouv jcouv left a comment

Choose a reason for hiding this comment

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

LGTM Thanks (iteration 1)

@jcouv jcouv self-assigned this Aug 3, 2020
@AlekseyTs AlekseyTs merged commit 932828f into dotnet:master Aug 4, 2020
@ghost ghost added this to the Next milestone Aug 4, 2020
@RikkiGibson RikkiGibson modified the milestones: Next, 16.8.P2 Aug 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Records should generate operator == and !=

4 participants