com php-src: More explicit errors for return; vs return null;: Zend/tests/return_types/void_disallowed1.phpt Zend/te
sts/type_declarations/nullable_typed_return_without_v alue.phpt Zend/zend_compile.c

From: Date: Sat, 04 Jun 2016 11:27:11 +0000
Subject: com php-src: More explicit errors for return; vs return null;: Zend/tests/return_types/void_disallowed1.phpt Zend/te
sts/type_declarations/nullable_typed_return_without_v alue.phpt Zend/zend_compile.c
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-92293@lists.php.net to get a copy of this message
Commit: 73b2f791997a254788b1939ca8cef373694f8461 Author: Nikita Popov <nikic@php.net> Sat, 4 Jun 2016 13:25:52 +0200 Parents: fe907562ada8929863561b27355e802db4f14a79 Branches: master Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=73b2f791997a254788b1939ca8cef373694f8461 Log: More explicit errors for return; vs return null; Both for "return null" in a void function and "return" in a nullable return function. Changed paths: M Zend/tests/return_types/void_disallowed1.phpt A Zend/tests/type_declarations/nullable_typed_return_without_value.phpt M Zend/zend_compile.c Diff: diff --git a/Zend/tests/return_types/void_disallowed1.phpt b/Zend/tests/return_types/void_disallowed1.phpt index b20f129..365e206 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 0000000..b16e7c6 --- /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 4bcec41..2a575cf 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) {

« previous php.cvs (#92293) next »