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
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
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)

}
}
";

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.
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)

// init => ((ISomething)this).Property = value;
Diagnostic(ErrorCode.ERR_AssignmentInitOnly, "((ISomething)this).Property").WithArguments("ISomething.Property").WithLocation(13, 17)
);
}

[Fact]
public void OverridingInitOnlyProperty()
{
Expand Down