From 8c47ed09b371b49ab63e92655a940b21650baf1e Mon Sep 17 00:00:00 2001 From: AlekseyTs Date: Fri, 20 Nov 2020 07:38:44 -0800 Subject: [PATCH] Report an error for ref-returning auto-properties declared in an interface. (#49510) Fixes #49341. --- .../Source/SourcePropertySymbolBase.cs | 2 +- .../DefaultInterfaceImplementationTests.cs | 117 ++++++++++++++++++ 2 files changed, 118 insertions(+), 1 deletion(-) diff --git a/src/Compilers/CSharp/Portable/Symbols/Source/SourcePropertySymbolBase.cs b/src/Compilers/CSharp/Portable/Symbols/Source/SourcePropertySymbolBase.cs index 0b2a75c8661a5..562d625644a5a 100644 --- a/src/Compilers/CSharp/Portable/Symbols/Source/SourcePropertySymbolBase.cs +++ b/src/Compilers/CSharp/Portable/Symbols/Source/SourcePropertySymbolBase.cs @@ -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); } diff --git a/src/Compilers/CSharp/Test/Symbol/Symbols/DefaultInterfaceImplementationTests.cs b/src/Compilers/CSharp/Test/Symbol/Symbols/DefaultInterfaceImplementationTests.cs index ff711d95762fe..a6434f9c9b5eb 100644 --- a/src/Compilers/CSharp/Test/Symbol/Symbols/DefaultInterfaceImplementationTests.cs +++ b/src/Compilers/CSharp/Test/Symbol/Symbols/DefaultInterfaceImplementationTests.cs @@ -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) + ); + } } }