Skip to content

Commit

Permalink
Disallow local byref void methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
VSadov committed Feb 19, 2016
1 parent 453bdae commit d07de12
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,13 @@ 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]);
}

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.
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 d07de12

Please sign in to comment.