Skip to content

Commit

Permalink
Merge pull request #8945 from VSadov/fix8441
Browse files Browse the repository at this point in the history
Disallow local byref void methods.
  • Loading branch information
VSadov committed Feb 20, 2016
2 parents 453bdae + d5c1963 commit be01c75
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,12 @@ internal TypeSymbol ComputeReturnType()
// The return type of an async method must be void, Task or Task<T>
diagnostics.Add(ErrorCode.ERR_BadAsyncReturn, this.Locations[0]);
}

if (this.RefKind != RefKind.None && returnType.SpecialType == SpecialType.System_Void)
{
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.
Expand Down
22 changes: 22 additions & 0 deletions src/Compilers/CSharp/Test/Symbol/Symbols/Source/MethodTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down

0 comments on commit be01c75

Please sign in to comment.