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

Reference conversion on this in init-only phase #50424

Merged
merged 2 commits into from
Jan 28, 2021

Conversation

jcouv
Copy link
Member

@jcouv jcouv commented Jan 13, 2021

Fixes #50053

From discussion with Jared and Mads, the set of conversions that we should consider for this scenario is identity and reference conversions.

@jcouv jcouv self-assigned this Jan 13, 2021
@jcouv jcouv added this to the 16.9 milestone Jan 13, 2021
@jcouv jcouv marked this pull request as ready for review January 14, 2021 10:09
@jcouv jcouv requested a review from a team as a code owner January 14, 2021 10:09
@AlekseyTs
Copy link
Contributor

AlekseyTs commented Jan 14, 2021

Is there a matching spec update? #Closed

@jaredpar
Copy link
Member

Is there a matching spec update?

Think this is a change where we should ensure there is a spec update. The concept here I believe is easy to understand from a compiler developer perspective: conversions on this which are identity preserving. Can likely just go through ConversionKind and identify the ones that match. At the same time I'm not sure that type of concept exists in the language spec (perhaps @MadsTorgersen was able to point one out). Want to make sure this is explainable in the language spec in conjunction with this change.

@jcouv
Copy link
Member Author

jcouv commented Jan 14, 2021

I'll update the spec.
I talked to Mads already and we settled on identity+reference conversions. We don't seem to have a concept of identity/representation-preserving conversions.


receiver = boundConversion.Operand;
}

// bad: other.InitOnlyProperty = ...
if (!(receiver is BoundThisReference || receiver is BoundBaseReference))
Copy link
Contributor

@AlekseyTs AlekseyTs Jan 15, 2021

Choose a reason for hiding this comment

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

receiver is BoundBaseReference [](start = 56, length = 30)

Should we disallow conversions on top of base? It is illegal to convert base in general. #Closed

Copy link
Member

Choose a reason for hiding this comment

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

I think it's likely fine to handle it here. We'll report an error on the conversion itself.


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

";

var comp = CreateCompilation(new[] { source, IsExternalInitTypeDefinition }, options: TestOptions.DebugExe);
comp.VerifyDiagnostics();
Copy link
Contributor

@AlekseyTs AlekseyTs Jan 15, 2021

Choose a reason for hiding this comment

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

.VerifyDiagnostics() [](start = 16, length = 20)

Consider calling this on CompileAndVerify instead. #Closed

Copy link
Member

Choose a reason for hiding this comment

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

Is there even any need to call VerifyDiagnostics?


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

public int SomethingElse
{
get => ((ISomething)this).Property;
init => ((ISomething)this).Property = value;
Copy link
Contributor

@AlekseyTs AlekseyTs Jan 15, 2021

Choose a reason for hiding this comment

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

this [](start = 29, length = 4)

Consider adding a test with base. #Closed

Copy link
Member

Choose a reason for hiding this comment

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

Also a test where this hides an init-only property from base.


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

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 (commit 1)


var comp = CreateCompilation(new[] { source, IsExternalInitTypeDefinition }, options: TestOptions.DebugExe);
comp.VerifyDiagnostics(
// (13,17): error CS8852: Init-only property or indexer 'ISomething.Property' can only be assigned in an object initializer, or on 'this' or 'base' in an instance constructor or an 'init' accessor.
Copy link
Member

Choose a reason for hiding this comment

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

Would it be reasonable to add a more specific error for this, or do you think that would be more effort than it's worth? The error as it exists is misleading.

Copy link
Member

Choose a reason for hiding this comment

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

Looking at the code I don't think it would be a ton of work to add, and I really don't like the error as it exists because it's just wrong.


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

@333fred
Copy link
Member

333fred commented Jan 15, 2021

Done review pass (commit 1)

@@ -1196,6 +1196,17 @@ bool isAllowedInitOnlySet(BoundExpression receiver)
return true;
}

while (receiver is BoundConversion boundConversion)
Copy link
Contributor

@AlekseyTs AlekseyTs Jan 20, 2021

Choose a reason for hiding this comment

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

receiver is BoundConversion boundConversion [](start = 23, length = 43)

Should we also handle BoundAsOperator? #Closed

@jcouv jcouv marked this pull request as ready for review January 28, 2021 02:02
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 (commit 2)

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 (commit 2)

@AlekseyTs
Copy link
Contributor

Should this PR target 16.9 branch at this point?

@jcouv jcouv changed the base branch from master to release/dev16.9 January 28, 2021 17:37
@jcouv jcouv added the Test Test failures in roslyn-CI label Jan 28, 2021
@jcouv
Copy link
Member Author

jcouv commented Jan 28, 2021

Retargeted PR to 16.9 release branch

@jcouv jcouv merged commit c6b76a6 into dotnet:release/dev16.9 Jan 28, 2021
@jcouv jcouv deleted the init-conversion branch January 28, 2021 23:20
@jcouv
Copy link
Member Author

jcouv commented Jan 28, 2021

Merged into release branch with single review since this is a test-only change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
4 participants