未验证 提交 d0dfb574 编写于 作者: R Rikki Gibson 提交者: GitHub

Give errors on implicit conversions to 'void' (#40465)

上级 f47c78fa
......@@ -2069,6 +2069,12 @@ internal BoundExpression GenerateConversionForAssignment(TypeSymbol targetType,
return;
}
if (targetType.IsVoidType())
{
Error(diagnostics, ErrorCode.ERR_NoImplicitConv, syntax, operand.Display, targetType);
return;
}
switch (operand.Kind)
{
case BoundKind.BadExpression:
......
......@@ -1486,6 +1486,166 @@ public static void Main()
Diagnostic(ErrorCode.ERR_NoImplicitConv, "m").WithArguments("bool", "int").WithLocation(6, 29));
}
[Fact, WorkItem(40405, "https://github.com/dotnet/roslyn/issues/40405")]
public void ThrowExpression_ImplicitVoidConversion_Return()
{
string text = @"
class C
{
void M1()
{
return true ? throw null : M2();
}
void M2() { }
}
";
CreateCompilation(text).VerifyDiagnostics(
// (6,23): error CS0029: Cannot implicitly convert type '<throw expression>' to 'void'
// return true ? throw null : M2();
Diagnostic(ErrorCode.ERR_NoImplicitConv, "throw null").WithArguments("<throw expression>", "void").WithLocation(6, 23));
}
[Fact, WorkItem(40405, "https://github.com/dotnet/roslyn/issues/40405")]
public void ThrowExpression_ImplicitVoidConversion_Assignment()
{
string text = @"
class C
{
void M1()
{
object obj = true ? throw null : M2();
}
void M2() { }
}
";
CreateCompilation(text).VerifyDiagnostics(
// (6,29): error CS0029: Cannot implicitly convert type '<throw expression>' to 'void'
// object obj = true ? throw null : M2();
Diagnostic(ErrorCode.ERR_NoImplicitConv, "throw null").WithArguments("<throw expression>", "void").WithLocation(6, 29));
}
[Fact, WorkItem(40405, "https://github.com/dotnet/roslyn/issues/40405")]
public void IntLiteral_ImplicitVoidConversion_Assignment()
{
string text = @"
class C
{
void M1()
{
object obj = true ? 0 : M2();
}
void M2() { }
}
";
CreateCompilation(text).VerifyDiagnostics(
// (6,22): error CS0173: Type of conditional expression cannot be determined because there is no implicit conversion between 'int' and 'void'
// object obj = true ? 0 : M2();
Diagnostic(ErrorCode.ERR_InvalidQM, "true ? 0 : M2()").WithArguments("int", "void").WithLocation(6, 22));
}
[Fact, WorkItem(40405, "https://github.com/dotnet/roslyn/issues/40405")]
public void VoidCall_ImplicitVoidConversion_Assignment()
{
string text = @"
class C
{
void M1()
{
object obj = true ? M2() : M2();
}
void M2() { }
}
";
CreateCompilation(text).VerifyDiagnostics(
// (6,22): error CS0029: Cannot implicitly convert type 'void' to 'object'
// object obj = true ? M2() : M2();
Diagnostic(ErrorCode.ERR_NoImplicitConv, "true ? M2() : M2()").WithArguments("void", "object").WithLocation(6, 22));
}
[Fact, WorkItem(40405, "https://github.com/dotnet/roslyn/issues/40405")]
public void VoidCall_ImplicitVoidConversion_DiscardAssignment()
{
string text = @"
class C
{
void M1()
{
_ = true ? M2() : M2();
}
void M2() { }
}
";
CreateCompilation(text).VerifyDiagnostics(
// (6,9): error CS8209: A value of type 'void' may not be assigned.
// _ = true ? M2() : M2();
Diagnostic(ErrorCode.ERR_VoidAssignment, "_").WithLocation(6, 9));
}
[Fact, WorkItem(40405, "https://github.com/dotnet/roslyn/issues/40405")]
public void VoidCall_Assignment()
{
string text = @"
class C
{
void M1()
{
object obj = M2();
}
void M2() { }
}
";
CreateCompilation(text).VerifyDiagnostics(
// (6,22): error CS0029: Cannot implicitly convert type 'void' to 'object'
// object obj = M2();
Diagnostic(ErrorCode.ERR_NoImplicitConv, "M2()").WithArguments("void", "object").WithLocation(6, 22));
}
[Fact, WorkItem(40405, "https://github.com/dotnet/roslyn/issues/40405")]
public void VoidCall_DiscardAssignment()
{
string text = @"
class C
{
void M1()
{
_ = M2();
}
void M2() { }
}
";
CreateCompilation(text).VerifyDiagnostics(
// (6,9): error CS8209: A value of type 'void' may not be assigned.
// _ = M2();
Diagnostic(ErrorCode.ERR_VoidAssignment, "_").WithLocation(6, 9));
}
[Fact, WorkItem(40405, "https://github.com/dotnet/roslyn/issues/40405")]
public void VoidCall_ImplicitVoidConversion_Return()
{
string text = @"
class C
{
void M1()
{
return true ? M2() : M2();
}
void M2() { }
}
";
CreateCompilation(text).VerifyDiagnostics(
// (6,9): error CS0127: Since 'C.M1()' returns void, a return keyword must not be followed by an object expression
// return true ? M2() : M2();
Diagnostic(ErrorCode.ERR_RetNoObjectRequired, "return").WithArguments("C.M1()").WithLocation(6, 9));
}
[Fact]
public void CS0030ERR_NoExplicitConv()
{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册