Skip to content

Scalar type hints (version 0.1) #1044

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Zend/tests/return_types/rfc002.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ function answer(): int {
}

answer();

--EXPECTF--
Catchable fatal error: Return value of answer() must be an instance of int, integer returned in %s on line %d
?>
DONE
--EXPECT--
DONE
1 change: 1 addition & 0 deletions Zend/zend_API.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ ZEND_API char *zend_get_type_by_const(int type) /* {{{ */
switch(type) {
case IS_FALSE:
case IS_TRUE:
case _IS_BOOL:
return "boolean";
case IS_LONG:
return "integer";
Expand Down
75 changes: 53 additions & 22 deletions Zend/zend_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -1871,7 +1871,11 @@ static zend_op *zend_delayed_compile_end(uint32_t offset) /* {{{ */
static void zend_emit_return_type_check(znode *expr, zend_arg_info *return_info) /* {{{ */
{
if (return_info->type_hint != IS_UNDEF) {
zend_emit_op(NULL, ZEND_VERIFY_RETURN_TYPE, expr, NULL);
zend_op *opline = zend_emit_op(NULL, ZEND_VERIFY_RETURN_TYPE, expr, NULL);
if (expr && expr->op_type == IS_CONST) {
opline->result_type = expr->op_type = IS_TMP_VAR;
opline->result.var = expr->u.op.var = get_temporary_variable(CG(active_op_array));
}
}
}
/* }}} */
Expand Down Expand Up @@ -3288,10 +3292,7 @@ void zend_compile_return(zend_ast *ast) /* {{{ */
}

if (CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
zend_emit_return_type_check(&expr_node, CG(active_op_array)->arg_info - 1);
if (expr_node.op_type == IS_CONST) {
zval_copy_ctor(&expr_node.u.constant);
}
zend_emit_return_type_check(expr_ast ? &expr_node : NULL, CG(active_op_array)->arg_info - 1);
}
opline = zend_emit_op(NULL, by_ref ? ZEND_RETURN_BY_REF : ZEND_RETURN,
&expr_node, NULL);
Expand Down Expand Up @@ -3927,6 +3928,25 @@ void zend_compile_stmt_list(zend_ast *ast) /* {{{ */
}
/* }}} */

static int zend_scalar_type_hint(zend_string *type_name, zend_arg_info *arg_info) /* {{{ */
{
if (zend_string_equals_literal_ci(type_name, "bool")) {
arg_info->type_hint = _IS_BOOL;
} else if (zend_string_equals_literal_ci(type_name, "int")) {
arg_info->type_hint = IS_LONG;
} else if (zend_string_equals_literal_ci(type_name, "float")) {
arg_info->type_hint = IS_DOUBLE;
} else if (zend_string_equals_literal_ci(type_name, "string")) {
arg_info->type_hint = IS_STRING;
} else if (zend_string_equals_literal_ci(type_name, "resource")) {
arg_info->type_hint = IS_RESOURCE;
} else {
return 0;
}
return 1;
}
/* }}} */

void zend_compile_params(zend_ast *ast, zend_ast *return_type_ast, zend_bool is_method) /* {{{ */
{
zend_ast_list *list = zend_ast_get_list(ast);
Expand All @@ -3949,18 +3969,20 @@ void zend_compile_params(zend_ast *ast, zend_ast *return_type_ast, zend_bool is_
} else {
zend_string *class_name = zend_ast_get_str(return_type_ast);

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;
if (!zend_scalar_type_hint(class_name, arg_infos)) {
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++;
Expand Down Expand Up @@ -4052,6 +4074,7 @@ void zend_compile_params(zend_ast *ast, zend_ast *return_type_ast, zend_bool is_
op_array->fn_flags |= ZEND_ACC_HAS_TYPE_HINTS;
arg_info->allow_null = has_null_default;


if (type_ast->kind == ZEND_AST_TYPE) {
arg_info->type_hint = type_ast->attr;
if (arg_info->type_hint == IS_ARRAY) {
Expand All @@ -4071,18 +4094,26 @@ void zend_compile_params(zend_ast *ast, zend_ast *return_type_ast, zend_bool is_
} else {
zend_string *class_name = zend_ast_get_str(type_ast);

if (zend_is_const_default_class_ref(type_ast)) {
class_name = zend_resolve_class_name_ast(type_ast);
if (zend_scalar_type_hint(class_name, arg_info)) {
if (default_ast && !has_null_default && !Z_CONSTANT(default_node.u.constant) &&
Z_TYPE(default_node.u.constant) != arg_info->type_hint) {
zend_error_noreturn(E_COMPILE_ERROR, "Default value for parameters "
"with a %s type hint can only be %s or NULL", class_name->val, class_name->val);
}
} else {
zend_string_addref(class_name);
}
if (zend_is_const_default_class_ref(type_ast)) {
class_name = zend_resolve_class_name_ast(type_ast);
} else {
zend_string_addref(class_name);
}

arg_info->type_hint = IS_OBJECT;
arg_info->class_name = class_name;
arg_info->type_hint = IS_OBJECT;
arg_info->class_name = class_name;

if (default_ast && !has_null_default && !Z_CONSTANT(default_node.u.constant)) {
if (default_ast && !has_null_default && !Z_CONSTANT(default_node.u.constant)) {
zend_error_noreturn(E_COMPILE_ERROR, "Default value for parameters "
"with a class type hint can only be NULL");
}
}
}
}
Expand Down
Loading