From d07de1274b96653007800eaa3069c579dbd5d1e5 Mon Sep 17 00:00:00 2001 From: vsadov Date: Fri, 19 Feb 2016 11:42:21 -0800 Subject: [PATCH] Disallow local byref void methods. Fixes #8441 --- .../Symbols/Source/LocalFunctionSymbol.cs | 7 ++++++ .../Test/Symbol/Symbols/Source/MethodTests.cs | 22 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/Compilers/CSharp/Portable/Symbols/Source/LocalFunctionSymbol.cs b/src/Compilers/CSharp/Portable/Symbols/Source/LocalFunctionSymbol.cs index c292462eed906..c592b6667a462 100644 --- a/src/Compilers/CSharp/Portable/Symbols/Source/LocalFunctionSymbol.cs +++ b/src/Compilers/CSharp/Portable/Symbols/Source/LocalFunctionSymbol.cs @@ -168,6 +168,13 @@ internal TypeSymbol ComputeReturnType() // The return type of an async method must be void, Task or Task diagnostics.Add(ErrorCode.ERR_BadAsyncReturn, this.Locations[0]); } + + var returnsVoid = returnType.SpecialType == SpecialType.System_Void; + if (this.RefKind != RefKind.None && returnsVoid) + { + diagnostics.Add(ErrorCode.ERR_VoidReturningMethodCannotReturnByRef, this.Locations[0]); + } + // TODO: note there is a race condition here that will ultimately need to be fixed. // Specifically, the Interlocked.CompareExchange above succeeds, and will be seen by // other threads, before the diagnostics have been recorded in this symbol, below. diff --git a/src/Compilers/CSharp/Test/Symbol/Symbols/Source/MethodTests.cs b/src/Compilers/CSharp/Test/Symbol/Symbols/Source/MethodTests.cs index e3a42c4451fe4..bfd5c85528bca 100644 --- a/src/Compilers/CSharp/Test/Symbol/Symbols/Source/MethodTests.cs +++ b/src/Compilers/CSharp/Test/Symbol/Symbols/Source/MethodTests.cs @@ -2072,6 +2072,28 @@ static ref void M() { } Diagnostic(ErrorCode.ERR_VoidReturningMethodCannotReturnByRef, "ref").WithLocation(4, 12)); } + [Fact] + public void RefReturningVoidMethodNested() + { + var source = @" +static class C +{ + static void Main() + { + ref void M() { } + } +} +"; + + CreateExperimentalCompilationWithMscorlib45(source).VerifyDiagnostics( + // (6,18): error CS8898: Void-returning methods cannot return by reference + // ref void M() { } + Diagnostic(ErrorCode.ERR_VoidReturningMethodCannotReturnByRef, "M").WithLocation(6, 18), + // (6,18): warning CS0168: The variable 'M' is declared but never used + // ref void M() { } + Diagnostic(ErrorCode.WRN_UnreferencedVar, "M").WithArguments("M").WithLocation(6, 18)); + } + [Fact] public void RefReturningAsyncMethod() {