com php-src: Implement scalar return types (strict only for now): Zend/zend_compile.c Zend/zend_execute.c
| From: | Dmitry Stogov | Date: | Tue, 10 Feb 2015 15:48:09 +0000 |
| Subject: | com php-src: Implement scalar return types (strict only for now): Zend/zend_compile.c Zend/zend_execute.c | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-85713@lists.php.net to get a copy of this message | ||
Commit: 6b6b2b4b7cc2f463c7add85fd1ccd1ba89b004cf
Author: Andrea Faulds <ajf@ajf.me> Sun, 1 Feb 2015 16:33:52 +0000
Parents: c7719ab4e5e49cdcef350a873ce4067bab3b7c02
Branches: master
Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=6b6b2b4b7cc2f463c7add85fd1ccd1ba89b004cf
Log:
Implement scalar return types (strict only for now)
Changed paths:
M Zend/zend_compile.c
M Zend/zend_execute.c
Diff:
diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c
index 0f5fb03..b2d4a91 100644
--- a/Zend/zend_compile.c
+++ b/Zend/zend_compile.c
@@ -3909,19 +3909,24 @@ void zend_compile_params(zend_ast *ast, zend_ast *return_type_ast, zend_bool
is_
arg_infos->type_hint = return_type_ast->attr;
} else {
zend_string *class_name = zend_ast_get_str(return_type_ast);
+ zend_uchar type = zend_lookup_scalar_typehint_by_name(class_name);
- if (zend_is_const_default_class_ref(return_type_ast)) {
- class_name = zend_resolve_class_name_ast(return_type_ast);
+ if (type != 0) {
+ arg_infos->type_hint = type;
} else {
- zend_string_addref(class_name);
- if (!is_method) {
- zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare a return type of %s outside of a
class scope", class_name->val);
- return;
+ if (zend_is_const_default_class_ref(return_type_ast)) {
+ class_name = zend_resolve_class_name_ast(return_type_ast);
+ } else {
+ zend_string_addref(class_name);
+ if (!is_method) {
+ zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare a return type of %s outside of a
class scope", class_name->val);
+ return;
+ }
}
- }
- arg_infos->type_hint = IS_OBJECT;
- arg_infos->class_name = class_name;
+ arg_infos->type_hint = IS_OBJECT;
+ arg_infos->class_name = class_name;
+ }
}
arg_infos++;
diff --git a/Zend/zend_execute.c b/Zend/zend_execute.c
index 823412c..1928e9c 100644
--- a/Zend/zend_execute.c
+++ b/Zend/zend_execute.c
@@ -901,10 +901,17 @@ static void zend_verify_return_type(zend_function *zf, zval *ret)
if (!zend_is_callable(ret, IS_CALLABLE_CHECK_SILENT, NULL) && (Z_TYPE_P(ret) != IS_NULL
|| !ret_info->allow_null)) {
zend_verify_return_error(E_RECOVERABLE_ERROR, zf, "be callable", "",
zend_zval_type_name(ret), "");
}
-#if ZEND_DEBUG
- } else {
- zend_error(E_ERROR, "Unknown typehint");
-#endif
+ } else if (UNEXPECTED(!ZEND_SAME_FAKE_TYPE(ret_info->type_hint, Z_TYPE_P(ret)))) {
+ if (Z_TYPE_P(ret) == IS_NULL) {
+ if (!ret_info->allow_null) {
+failure:
+ zend_verify_return_error(E_RECOVERABLE_ERROR, zf, "be of the type ",
zend_get_type_by_const(ret_info->type_hint), zend_zval_type_name(ret), "");
+ }
+ return;
+ }
+ if (!zend_verify_scalar_type_hint(ret_info->type_hint, ret, 1)) {
+ goto failure;
+ }
}
}
}
@@ -926,10 +933,8 @@ static inline int zend_verify_missing_return_type(zend_function *zf)
zend_verify_return_error(E_RECOVERABLE_ERROR, zf, "be of the type array",
"", "none", "");
} else if (ret_info->type_hint == IS_CALLABLE) {
zend_verify_return_error(E_RECOVERABLE_ERROR, zf, "be callable", "",
"none", "");
-#if ZEND_DEBUG
} else {
- zend_error(E_ERROR, "Unknown typehint");
-#endif
+ zend_verify_return_error(E_RECOVERABLE_ERROR, zf, "be of the type ",
zend_get_type_by_const(ret_info->type_hint), "none", "");
}
return 0;
}