Skip to content

Commit

Permalink
Report an error for ref-returning auto-properties declared in an inte…
Browse files Browse the repository at this point in the history
…rface. (#49510)

Fixes #49341.
  • Loading branch information
AlekseyTs authored Nov 20, 2020
1 parent 8d278c2 commit 8c47ed0
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ internal override void AfterAddingTypeMembersChecks(ConversionsBase conversions,
Binder.ReportUseSiteDiagnosticForSynthesizedAttribute(DeclaringCompilation,
WellKnownMember.System_Runtime_CompilerServices_CompilerGeneratedAttribute__ctor, diagnostics, location: Location);

if (this.RefKind != RefKind.None && !ContainingType.IsInterface)
if (this.RefKind != RefKind.None)
{
diagnostics.Add(ErrorCode.ERR_AutoPropertyCannotBeRefReturning, Location, this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60024,5 +60024,122 @@ public void IncompletePropertyImplementationSyntax_01()
Diagnostic(ErrorCode.ERR_RbraceExpected, "").WithLocation(3, 16)
);
}

[Fact, WorkItem(49341, "https://github.com/dotnet/roslyn/issues/49341")]
public void RefReturningAutoProperty_01()
{
var source1 =
@"
interface IA
{
static ref int PA { get;}
}

interface IB
{
static ref int PB { get; set;}
}

interface IC
{
static ref int PC { set;}
}
";
var compilation1 = CreateCompilation(source1, options: TestOptions.DebugDll,
parseOptions: TestOptions.Regular,
targetFramework: TargetFramework.NetStandardLatest);
compilation1.VerifyDiagnostics(
// (4,20): error CS8145: Auto-implemented properties cannot return by reference
// static ref int PA { get;}
Diagnostic(ErrorCode.ERR_AutoPropertyCannotBeRefReturning, "PA").WithArguments("IA.PA").WithLocation(4, 20),
// (9,20): error CS8145: Auto-implemented properties cannot return by reference
// static ref int PB { get; set;}
Diagnostic(ErrorCode.ERR_AutoPropertyCannotBeRefReturning, "PB").WithArguments("IB.PB").WithLocation(9, 20),
// (9,30): error CS8147: Properties which return by reference cannot have set accessors
// static ref int PB { get; set;}
Diagnostic(ErrorCode.ERR_RefPropertyCannotHaveSetAccessor, "set").WithArguments("IB.PB.set").WithLocation(9, 30),
// (14,20): error CS8146: Properties which return by reference must have a get accessor
// static ref int PC { set;}
Diagnostic(ErrorCode.ERR_RefPropertyMustHaveGetAccessor, "PC").WithArguments("IC.PC").WithLocation(14, 20)
);
}

[Fact, WorkItem(49341, "https://github.com/dotnet/roslyn/issues/49341")]
public void RefReturningAutoProperty_02()
{
var source1 =
@"
interface IA
{
ref int PA { get;}
}

interface IB
{
ref int PB { get; set;}
}

interface IC
{
ref int PC { set;}
}
";
var compilation1 = CreateCompilation(source1, options: TestOptions.DebugDll,
parseOptions: TestOptions.Regular,
targetFramework: TargetFramework.NetStandardLatest);
compilation1.VerifyDiagnostics(
// (9,23): error CS8147: Properties which return by reference cannot have set accessors
// ref int PB { get; set;}
Diagnostic(ErrorCode.ERR_RefPropertyCannotHaveSetAccessor, "set").WithArguments("IB.PB.set").WithLocation(9, 23),
// (14,13): error CS8146: Properties which return by reference must have a get accessor
// ref int PC { set;}
Diagnostic(ErrorCode.ERR_RefPropertyMustHaveGetAccessor, "PC").WithArguments("IC.PC").WithLocation(14, 13)
);
}

[Fact, WorkItem(49341, "https://github.com/dotnet/roslyn/issues/49341")]
public void RefReturningAutoProperty_03()
{
var source1 =
@"
interface IA
{
sealed ref int PA { get;}
}

interface IB
{
sealed ref int PB { get; set;}
}

interface IC
{
sealed ref int PC { set;}
}
";
var compilation1 = CreateCompilation(source1, options: TestOptions.DebugDll,
parseOptions: TestOptions.Regular,
targetFramework: TargetFramework.NetStandardLatest);
compilation1.VerifyDiagnostics(
// (4,25): error CS0501: 'IA.PA.get' must declare a body because it is not marked abstract, extern, or partial
// sealed ref int PA { get;}
Diagnostic(ErrorCode.ERR_ConcreteMissingBody, "get").WithArguments("IA.PA.get").WithLocation(4, 25),
// (9,25): error CS0501: 'IB.PB.get' must declare a body because it is not marked abstract, extern, or partial
// sealed ref int PB { get; set;}
Diagnostic(ErrorCode.ERR_ConcreteMissingBody, "get").WithArguments("IB.PB.get").WithLocation(9, 25),
// (9,30): error CS8147: Properties which return by reference cannot have set accessors
// sealed ref int PB { get; set;}
Diagnostic(ErrorCode.ERR_RefPropertyCannotHaveSetAccessor, "set").WithArguments("IB.PB.set").WithLocation(9, 30),
// (9,30): error CS0501: 'IB.PB.set' must declare a body because it is not marked abstract, extern, or partial
// sealed ref int PB { get; set;}
Diagnostic(ErrorCode.ERR_ConcreteMissingBody, "set").WithArguments("IB.PB.set").WithLocation(9, 30),
// (14,20): error CS8146: Properties which return by reference must have a get accessor
// sealed ref int PC { set;}
Diagnostic(ErrorCode.ERR_RefPropertyMustHaveGetAccessor, "PC").WithArguments("IC.PC").WithLocation(14, 20),
// (14,25): error CS0501: 'IC.PC.set' must declare a body because it is not marked abstract, extern, or partial
// sealed ref int PC { set;}
Diagnostic(ErrorCode.ERR_ConcreteMissingBody, "set").WithArguments("IC.PC.set").WithLocation(14, 25)
);
}
}
}

0 comments on commit 8c47ed0

Please sign in to comment.