com php-src: Implement per-file strictness for scalar return types: Zend/zend_compile.c Zend/zend_execute.c Zend/zend _vm_def.h Zend/zend_vm_execute.h
| From: | Dmitry Stogov | Date: | Tue, 10 Feb 2015 15:48:09 +0000 |
| Subject: | com php-src: Implement per-file strictness for scalar return types: Zend/zend_compile.c Zend/zend_execute.c Zend/zend _vm_def.h Zend/zend_vm_execute.h | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-85711@lists.php.net to get a copy of this message | ||
Commit: 5a7ac0fadec3e5fcc22c96b0e7c9a73a9de20a32
Author: Andrea Faulds <ajf@ajf.me> Sun, 1 Feb 2015 23:20:39 +0000
Parents: 44ed070a9afed6f45165769f959c1122e765ad35
Branches: master
Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=5a7ac0fadec3e5fcc22c96b0e7c9a73a9de20a32
Log:
Implement per-file strictness for scalar return types
Changed paths:
M Zend/zend_compile.c
M Zend/zend_execute.c
M Zend/zend_vm_def.h
M Zend/zend_vm_execute.h
Diff:
diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c
index b2d4a91...ec41f64 100644
--- a/Zend/zend_compile.c
+++ b/Zend/zend_compile.c
@@ -1902,7 +1902,8 @@ 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);
+ opline->extended_value = (CG(declarables).strict_types ? 1 : 0);
}
}
/* }}} */
@@ -3238,7 +3239,18 @@ 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);
+ zend_arg_info *arg_info = CG(active_op_array)->arg_info - 1;
+
+ /* for scalar, weak return types, the value may be casted
+ * thus, for constants, we need to store them in a tmp var
+ */
+ if (expr_node.op_type == IS_CONST && !CG(declarables).strict_types) {
+ znode expr_node_copy = expr_node;
+
+ zend_emit_op_tmp(&expr_node, ZEND_QM_ASSIGN, &expr_node_copy, NULL);
+ }
+
+ zend_emit_return_type_check(&expr_node, arg_info);
if (expr_node.op_type == IS_CONST) {
zval_copy_ctor(&expr_node.u.constant);
}
diff --git a/Zend/zend_execute.c b/Zend/zend_execute.c
index 1928e9c..0d4634f 100644
--- a/Zend/zend_execute.c
+++ b/Zend/zend_execute.c
@@ -874,7 +874,7 @@ static int zend_verify_internal_return_type(zend_function *zf, zval *ret)
}
#endif
-static void zend_verify_return_type(zend_function *zf, zval *ret)
+static void zend_verify_return_type(zend_function *zf, zval *ret, zend_bool strict)
{
zend_arg_info *ret_info = zf->common.arg_info - 1;
char *need_msg;
@@ -909,7 +909,7 @@ failure:
}
return;
}
- if (!zend_verify_scalar_type_hint(ret_info->type_hint, ret, 1)) {
+ if (!zend_verify_scalar_type_hint(ret_info->type_hint, ret, strict)) {
goto failure;
}
}
diff --git a/Zend/zend_vm_def.h b/Zend/zend_vm_def.h
index 0557ddd..c1cee36 100644
--- a/Zend/zend_vm_def.h
+++ b/Zend/zend_vm_def.h
@@ -2962,7 +2962,8 @@ ZEND_VM_HANDLER(124, ZEND_VERIFY_RETURN_TYPE, CONST|TMP|VAR|UNUSED|CV, UNUSED)
zend_free_op free_op1;
retval_ptr = GET_OP1_ZVAL_PTR_DEREF(BP_VAR_R);
- zend_verify_return_type(EX(func), retval_ptr);
+ /* extended_value stores strictness flag */
+ zend_verify_return_type(EX(func), retval_ptr, opline->extended_value);
}
CHECK_EXCEPTION();
ZEND_VM_NEXT_OPCODE();
diff --git a/Zend/zend_vm_execute.h b/Zend/zend_vm_execute.h
index ab73626..48b2150 100644
--- a/Zend/zend_vm_execute.h
+++ b/Zend/zend_vm_execute.h
@@ -6113,7 +6113,8 @@ static int ZEND_FASTCALL
ZEND_VERIFY_RETURN_TYPE_SPEC_CONST_UNUSED_HANDLER(ZEND
retval_ptr = EX_CONSTANT(opline->op1);
- zend_verify_return_type(EX(func), retval_ptr);
+ /* extended_value stores strictness flag */
+ zend_verify_return_type(EX(func), retval_ptr, opline->extended_value);
}
CHECK_EXCEPTION();
ZEND_VM_NEXT_OPCODE();
@@ -10131,7 +10132,8 @@ static int ZEND_FASTCALL
ZEND_VERIFY_RETURN_TYPE_SPEC_TMP_UNUSED_HANDLER(ZEND_O
zend_free_op free_op1;
retval_ptr = _get_zval_ptr_tmp(opline->op1.var, execute_data, &free_op1);
- zend_verify_return_type(EX(func), retval_ptr);
+ /* extended_value stores strictness flag */
+ zend_verify_return_type(EX(func), retval_ptr, opline->extended_value);
}
CHECK_EXCEPTION();
ZEND_VM_NEXT_OPCODE();
@@ -14972,7 +14974,8 @@ static int ZEND_FASTCALL
ZEND_VERIFY_RETURN_TYPE_SPEC_VAR_UNUSED_HANDLER(ZEND_O
zend_free_op free_op1;
retval_ptr = _get_zval_ptr_var_deref(opline->op1.var, execute_data, &free_op1);
- zend_verify_return_type(EX(func), retval_ptr);
+ /* extended_value stores strictness flag */
+ zend_verify_return_type(EX(func), retval_ptr, opline->extended_value);
}
CHECK_EXCEPTION();
ZEND_VM_NEXT_OPCODE();
@@ -20235,7 +20238,8 @@ static int ZEND_FASTCALL
ZEND_VERIFY_RETURN_TYPE_SPEC_UNUSED_UNUSED_HANDLER(ZEN
retval_ptr = NULL;
- zend_verify_return_type(EX(func), retval_ptr);
+ /* extended_value stores strictness flag */
+ zend_verify_return_type(EX(func), retval_ptr, opline->extended_value);
}
CHECK_EXCEPTION();
ZEND_VM_NEXT_OPCODE();
@@ -28419,7 +28423,8 @@ static int ZEND_FASTCALL
ZEND_VERIFY_RETURN_TYPE_SPEC_CV_UNUSED_HANDLER(ZEND_OP
retval_ptr = _get_zval_ptr_cv_deref_BP_VAR_R(execute_data, opline->op1.var);
- zend_verify_return_type(EX(func), retval_ptr);
+ /* extended_value stores strictness flag */
+ zend_verify_return_type(EX(func), retval_ptr, opline->extended_value);
}
CHECK_EXCEPTION();
ZEND_VM_NEXT_OPCODE();