Skip to content

Commit

Permalink
Reference conversion on this in init-only phase (#50424)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcouv authored Jan 28, 2021
1 parent f67e419 commit c6b76a6
Showing 1 changed file with 56 additions and 0 deletions.
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;
}
}
";

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.
// init => ((ISomething)this).Property = value;
Diagnostic(ErrorCode.ERR_AssignmentInitOnly, "((ISomething)this).Property").WithArguments("ISomething.Property").WithLocation(13, 17)
);
}

[Fact]
public void OverridingInitOnlyProperty()
{
Expand Down

0 comments on commit c6b76a6

Please sign in to comment.