summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Zend/tests/return_types/void_disallowed1.phpt2
-rw-r--r--Zend/tests/type_declarations/nullable_typed_return_without_value.phpt14
-rw-r--r--Zend/zend_compile.c18
3 files changed, 30 insertions, 4 deletions
diff --git a/Zend/tests/return_types/void_disallowed1.phpt b/Zend/tests/return_types/void_disallowed1.phpt
index b20f129a58..365e2060bf 100644
--- a/Zend/tests/return_types/void_disallowed1.phpt
+++ b/Zend/tests/return_types/void_disallowed1.phpt
@@ -9,4 +9,4 @@ function foo(): void {
// Note the lack of function call: function validated at compile-time
--EXPECTF--
-Fatal error: A void function must not return a value in %s on line %d
+Fatal error: A void function must not return a value (did you mean "return;" instead of "return null;"?) in %s on line %d
diff --git a/Zend/tests/type_declarations/nullable_typed_return_without_value.phpt b/Zend/tests/type_declarations/nullable_typed_return_without_value.phpt
new file mode 100644
index 0000000000..b16e7c6434
--- /dev/null
+++ b/Zend/tests/type_declarations/nullable_typed_return_without_value.phpt
@@ -0,0 +1,14 @@
+--TEST--
+Nullable typed return without value generates friendlier error message
+--FILE--
+<?php
+
+function test() : ?int {
+ return;
+}
+
+test();
+
+?>
+--EXPECTF--
+Fatal error: A function with return type must return a value (did you mean "return null;" instead of "return;"?) in %s on line %d
diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c
index 4bcec416a7..2a575cff62 100644
--- a/Zend/zend_compile.c
+++ b/Zend/zend_compile.c
@@ -2270,7 +2270,13 @@ static void zend_emit_return_type_check(
/* `return ...;` is illegal in a void function (but `return;` isn't) */
if (return_info->type_hint == IS_VOID) {
if (expr) {
- zend_error_noreturn(E_COMPILE_ERROR, "A void function must not return a value");
+ if (expr->op_type == IS_CONST && Z_TYPE(expr->u.constant) == IS_NULL) {
+ zend_error_noreturn(E_COMPILE_ERROR,
+ "A void function must not return a value "
+ "(did you mean \"return;\" instead of \"return null;\"?)");
+ } else {
+ zend_error_noreturn(E_COMPILE_ERROR, "A void function must not return a value");
+ }
}
/* we don't need run-time check */
return;
@@ -2280,8 +2286,14 @@ static void zend_emit_return_type_check(
zend_op *opline;
if (!expr && !implicit) {
- zend_error_noreturn(E_COMPILE_ERROR,
- "A function with return type must return a value");
+ if (return_info->allow_null) {
+ zend_error_noreturn(E_COMPILE_ERROR,
+ "A function with return type must return a value "
+ "(did you mean \"return null;\" instead of \"return;\"?)");
+ } else {
+ zend_error_noreturn(E_COMPILE_ERROR,
+ "A function with return type must return a value");
+ }
}
if (expr && expr->op_type == IS_CONST) {