-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1660,6 +1660,62 @@ public string RegularProperty2 | |
); | ||
} | ||
|
||
[Fact, WorkItem(50053, "https://github.com/dotnet/roslyn/issues/50053")] | ||
public void PrivatelyImplementingInitOnlyProperty_ReferenceConversion() | ||
{ | ||
string source = @" | ||
var x = new DerivedType() { SomethingElse = 42 }; | ||
System.Console.Write(x.SomethingElse); | ||
|
||
public interface ISomething { int Property { get; init; } } | ||
public record BaseType : ISomething { int ISomething.Property { get; init; } } | ||
|
||
public record DerivedType : BaseType | ||
{ | ||
public int SomethingElse | ||
{ | ||
get => ((ISomething)this).Property; | ||
init => ((ISomething)this).Property = value; | ||
} | ||
} | ||
"; | ||
|
||
var comp = CreateCompilation(new[] { source, IsExternalInitTypeDefinition }, options: TestOptions.DebugExe); | ||
comp.VerifyEmitDiagnostics( | ||
// (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. | ||
// init => ((ISomething)this).Property = value; | ||
Diagnostic(ErrorCode.ERR_AssignmentInitOnly, "((ISomething)this).Property").WithArguments("ISomething.Property").WithLocation(13, 17) | ||
); | ||
} | ||
|
||
[Fact, WorkItem(50053, "https://github.com/dotnet/roslyn/issues/50053")] | ||
public void PrivatelyImplementingInitOnlyProperty_BoxingConversion() | ||
{ | ||
string source = @" | ||
var x = new Type() { SomethingElse = 42 }; | ||
|
||
public interface ISomething { int Property { get; init; } } | ||
|
||
public struct Type : ISomething | ||
{ | ||
int ISomething.Property { get; init; } | ||
|
||
public int SomethingElse | ||
{ | ||
get => throw null; | ||
init => ((ISomething)this).Property = value; | ||
} | ||
} | ||
"; | ||
|
||
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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) |
||
// init => ((ISomething)this).Property = value; | ||
Diagnostic(ErrorCode.ERR_AssignmentInitOnly, "((ISomething)this).Property").WithArguments("ISomething.Property").WithLocation(13, 17) | ||
); | ||
} | ||
|
||
[Fact] | ||
public void OverridingInitOnlyProperty() | ||
{ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider adding a test with
base
. #ClosedThere was a problem hiding this comment.
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 aninit
-only property frombase
.In reply to: 558461131 [](ancestors = 558461131)