Don't raise "cannot return non-void" on Terror (#21028)

This commit is contained in:
Dennis 2025-03-23 00:40:57 +01:00 committed by GitHub
parent 90e1a584b5
commit 53024b9fc5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 43 additions and 46 deletions

View file

@ -2603,7 +2603,7 @@ Statement statementSemanticVisit(Statement s, Scope* sc)
if (tbret && tbret.ty == Tvoid || convToVoid)
{
if (!convToVoid)
if (!convToVoid && !texp.isTypeError())
{
error(rs.loc, "cannot return non-void from `void` function");
errors = true;

View file

@ -1,11 +0,0 @@
/*
TEST_OUTPUT:
---
fail_compilation/diag10405.d(10): Error: cannot return non-void from `void` function
---
*/
void main()
{
return 10;
}

View file

@ -13,12 +13,12 @@ fail_compilation/diag20888.d(49): Error: return value `callback` of type `int de
fail_compilation/diag20888.d(54): Error: return value `() => 3755` of type `int function() pure nothrow @nogc @safe` does not match return type `int`, and cannot be implicitly converted
fail_compilation/diag20888.d(54): Did you intend to call the function pointer?
fail_compilation/diag20888.d(59): Error: `return` expression expected
fail_compilation/diag20888.d(64): Error: cannot return non-void from `void` function
fail_compilation/diag20888.d(70): Error: return value `() => i` of type `int delegate() pure nothrow @nogc @safe` does not match return type `int`, and cannot be implicitly converted
fail_compilation/diag20888.d(70): Did you intend to call the delegate?
---
*/
int alpha(int function() callback)
{
return callback;
@ -59,10 +59,10 @@ int theta()
return;
}
void iota()
{
return 0xEAB;
}
int kappa()
{

View file

@ -1,11 +0,0 @@
/*
TEST_OUTPUT:
---
fail_compilation/fail305.d(10): Error: cannot return non-void from `void` function
---
*/
void main()
{
return "a";
}

View file

@ -1,18 +0,0 @@
/*
TEST_OUTPUT:
---
fail_compilation/fail41.d(17): Error: cannot return non-void from `void` function
---
*/
class MyClass
{
}
MyClass[char[]] myarray;
void fn()
{
foreach (MyClass mc; myarray)
return mc;
}

View file

@ -0,0 +1,37 @@
/*
TEST_OUTPUT:
---
fail_compilation/nonvoid_return.d(15): Error: cannot return non-void from `void` function
fail_compilation/nonvoid_return.d(18): Error: cannot return non-void from `void` function
fail_compilation/nonvoid_return.d(29): Error: cannot return non-void from `void` function
fail_compilation/nonvoid_return.d(32): Error: undefined identifier `NONEXISTENT`
---
*/
void main()
{
return 10;
}
void fs() => "a";
class MyClass
{
}
MyClass[char[]] myarray;
void fn()
{
foreach (MyClass mc; myarray)
return mc;
}
auto err() { NONEXISTENT++; }
// Because `err` contains an error, it fails to infer void and gets an error return type
// Don't print the 'cannot return non-void' error in this case
void fe() => err;