Skip to content

Disallow setter-only EqualityContract property#48798

Merged
jcouv merged 5 commits intodotnet:masterfrom
jcouv:equality-contract
Oct 23, 2020
Merged

Disallow setter-only EqualityContract property#48798
jcouv merged 5 commits intodotnet:masterfrom
jcouv:equality-contract

Conversation

@jcouv
Copy link
Member

@jcouv jcouv commented Oct 20, 2020

Fixes #48723

@jcouv jcouv requested a review from a team as a code owner October 20, 2020 21:27
@jcouv jcouv self-assigned this Oct 20, 2020
@RikkiGibson RikkiGibson self-assigned this Oct 20, 2020
{
if (_equalityContract.GetMethod is null)
{
// There the equality contract isn't usable, an error was reported elsewhere
Copy link
Contributor

@cston cston Oct 20, 2020

Choose a reason for hiding this comment

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

There the equality contract [](start = 23, length = 27)

"The equality contract ..."? #Resolved

{
if (_equalityContract.GetMethod is null)
{
// There the equality contract isn't usable, an error was reported elsewhere
Copy link
Contributor

@cston cston Oct 20, 2020

Choose a reason for hiding this comment

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

There the equality contract [](start = 23, length = 27)

"The equality contract ..."? #Resolved

#endregion diagnostics introduced for C# 9.0

ERR_UnexpectedVarianceStaticMember = 9100,
ERR_EqualityContractRequiresGetter = 9101,
Copy link
Contributor

@AlekseyTs AlekseyTs Oct 21, 2020

Choose a reason for hiding this comment

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

ERR_EqualityContractRequiresGetter = 9101, [](start = 8, length = 42)

It looks like this branch is significantly behind. I think in master the last used error number is 8905 #Closed


try
{
if (_equalityContract.GetMethod is null)
Copy link
Contributor

@AlekseyTs AlekseyTs Oct 21, 2020

Choose a reason for hiding this comment

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

if (_equalityContract.GetMethod is null) [](start = 16, length = 40)

Should we limit this check to the code path that actually uses the property? #Closed


try
{
if (_equalityContract.GetMethod is null)
Copy link
Contributor

@AlekseyTs AlekseyTs Oct 21, 2020

Choose a reason for hiding this comment

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

if (_equalityContract.GetMethod is null) [](start = 16, length = 40)

Should we limit this check to the code path that actually uses the property? #Closed

var src = @"
record R
{
protected virtual System.Type EqualityContract { set { } }
Copy link
Contributor

@AlekseyTs AlekseyTs Oct 21, 2020

Choose a reason for hiding this comment

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

set { } [](start = 53, length = 7)

Consider adding a test with a property that has both accessors, unless we already have one. I assume that should be a success scenario? Or should we disallow property like that? #Closed

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, consider adding test(s) when a record class has only setter or both accessors in metadata and we try to derive another record from it with explicit/implicit definition of EqualityContract.


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

Copy link
Member Author

Choose a reason for hiding this comment

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

Or should we disallow property like that [with both accessors]?

I think it's okay to allow, as it meets our requirements and doesn't get in the way of what we need to do.


In reply to: 508941602 [](ancestors = 508941602,508933817)


var comp = CreateCompilation(src);
comp.VerifyEmitDiagnostics(
// (4,35): error CS8906: Record equality contract property 'R.EqualityContract' must have a get accessor.
Copy link
Member

@Youssef1313 Youssef1313 Oct 22, 2020

Choose a reason for hiding this comment

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

nit: "equality contract" seems unnecessarily repetitive in the message.


[Fact]
[WorkItem(48723, "https://github.com/dotnet/roslyn/issues/48723")]
public void EqualityContract_27_GetterAndSetterOnlyProperty_InMetadata()
Copy link
Contributor

@AlekseyTs AlekseyTs Oct 23, 2020

Choose a reason for hiding this comment

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

Only [](start = 55, length = 4)

Is the "Only" part in the name kept to convey something important? #Resolved

}

// Property has a getter and setter
.property instance class [mscorlib]System.Type EqualityContract()
Copy link
Contributor

@AlekseyTs AlekseyTs Oct 23, 2020

Choose a reason for hiding this comment

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

.property instance class [mscorlib]System.Type EqualityContract() [](start = 4, length = 65)

It looks like we decided to allow a declaration like that in source. Is there a particular reason to use IL to define this property for the test? #Resolved

";

var comp = CreateCompilationWithIL(src, il);
comp.VerifyEmitDiagnostics();
Copy link
Contributor

@AlekseyTs AlekseyTs Oct 23, 2020

Choose a reason for hiding this comment

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

comp.VerifyEmitDiagnostics(); [](start = 11, length = 30)

I think that for scenarios tested in this test we would want to test runtime behavior in order to verify that overriding is indeed happening and has expected effect on equality behavior. #Resolved

Copy link
Contributor

@AlekseyTs AlekseyTs left a comment

Choose a reason for hiding this comment

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

LGTM (iteration 3) with some comments for tests.

@jcouv jcouv merged commit 8d981c0 into dotnet:master Oct 23, 2020
@jcouv jcouv deleted the equality-contract branch October 23, 2020 22:24
@ghost ghost added this to the Next milestone Oct 23, 2020
@allisonchou allisonchou modified the milestones: Next, 16.9.P2 Nov 24, 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.

Compiler crash at records lowering

6 participants