提交 c27c7e96 编写于 作者: N Neal Gafter

Merge pull request #1910 from gafter/fix1830

Give an error when a type argument is inferred as void, and when a return statement has a void expression.
......@@ -2908,15 +2908,8 @@ private BoundReturnStatement BindReturn(ReturnStatementSyntax syntax, Diagnostic
}
else
{
// Check that the returned expression is not void. "return void" is not allowed anywhere outside
// expression-lambdas.
//
// Note: in the case of "return void" in non-async lambdas used in return-type-inference,
// simply return "void" as the inferred return type and give an error if that didn't work.
// We can't follow the same route in async methods, since empty-return-operands become a "Task"
// return type.
if ((object)arg?.Type != null && IsInAsyncMethod() && arg.Type.SpecialType == SpecialType.System_Void)
// Check that the returned expression is not void.
if ((object)arg?.Type != null && arg.Type.SpecialType == SpecialType.System_Void)
{
Error(diagnostics, ErrorCode.ERR_CantReturnVoid, expressionSyntax);
}
......
......@@ -405,7 +405,7 @@ private static bool CheckConstraintsSingleType(TypeSymbol type, CheckConstraints
var diagnosticsBuilder = ArrayBuilder<TypeParameterDiagnosticInfo>.GetInstance();
ArrayBuilder<TypeParameterDiagnosticInfo> useSiteDiagnosticsBuilder = null;
var result = CheckTypeConstraints(type, conversions, currentCompilation, diagnosticsBuilder, ref useSiteDiagnosticsBuilder);
var result = !typeSyntax.HasErrors && CheckTypeConstraints(type, conversions, currentCompilation, diagnosticsBuilder, ref useSiteDiagnosticsBuilder);
if (useSiteDiagnosticsBuilder != null)
{
......@@ -647,7 +647,7 @@ private static bool InterfacesAreDistinct(NamedTypeSymbol type, ConsList<Symbol>
return true;
}
if (typeArgument.IsPointerType() || typeArgument.IsRestrictedType())
if (typeArgument.IsPointerType() || typeArgument.IsRestrictedType() || typeArgument.SpecialType == SpecialType.System_Void)
{
// "The type '{0}' may not be used as a type argument"
diagnosticsBuilder.Add(new TypeParameterDiagnosticInfo(typeParameter, new CSDiagnosticInfo(ErrorCode.ERR_BadTypeArgument, typeArgument)));
......
......@@ -1137,5 +1137,27 @@ void M()
// var d = new System.Action(() => (new object()));
Diagnostic(ErrorCode.ERR_IllegalStatement, "(new object())").WithLocation(6, 41));
}
[WorkItem(1830, "https://github.com/dotnet/roslyn/issues/1830")]
[Fact]
public void FuncOfVoid()
{
var comp = CreateCompilationWithMscorlib(@"
using System;
class Program
{
void M1<T>(Func<T> f) {}
void Main(string[] args)
{
M1(() => { return System.Console.Beep(); });
}
}
");
comp.VerifyDiagnostics(
// (8,27): error CS4029: Cannot return an expression of type 'void'
// M1(() => { return System.Console.Beep(); });
Diagnostic(ErrorCode.ERR_CantReturnVoid, "System.Console.Beep()").WithLocation(8, 27)
);
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册