From: Aaron Piotrowski Date: Thu, 2 Jul 2015 22:46:51 +0000 (-0500) Subject: Allow integer default for float type X-Git-Tag: php-7.1.0alpha3~25^2~31 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=68baa539dc1e42a904617fcb799b58a5705b87ba;p=php Allow integer default for float type --- diff --git a/Zend/tests/typehints/scalar_float_with_integer_default_strict.phpt b/Zend/tests/typehints/scalar_float_with_integer_default_strict.phpt new file mode 100644 index 0000000000..b1aab433f9 --- /dev/null +++ b/Zend/tests/typehints/scalar_float_with_integer_default_strict.phpt @@ -0,0 +1,17 @@ +--TEST-- +Float type hint should allow an integer as default even with strict types +--FILE-- + +--EXPECT-- +float(0) diff --git a/Zend/tests/typehints/scalar_float_with_integer_default_weak.phpt b/Zend/tests/typehints/scalar_float_with_integer_default_weak.phpt new file mode 100644 index 0000000000..ab3206691a --- /dev/null +++ b/Zend/tests/typehints/scalar_float_with_integer_default_weak.phpt @@ -0,0 +1,15 @@ +--TEST-- +Float type hint should allow an integer as default +--FILE-- + +--EXPECT-- +float(0) diff --git a/Zend/tests/typehints/scalar_float_with_invalid_default.phpt b/Zend/tests/typehints/scalar_float_with_invalid_default.phpt new file mode 100644 index 0000000000..6b67985fd1 --- /dev/null +++ b/Zend/tests/typehints/scalar_float_with_invalid_default.phpt @@ -0,0 +1,16 @@ +--TEST-- +Float type hint should not allow invalid types as default +--FILE-- + +--EXPECTF-- + +Fatal error: Default value for parameters with a float type hint can only be float, integer, or NULL in %s on line %d diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c index df19f93be2..5a15392fe9 100644 --- a/Zend/zend_compile.c +++ b/Zend/zend_compile.c @@ -4397,10 +4397,21 @@ void zend_compile_params(zend_ast *ast, zend_ast *return_type_ast) /* {{{ */ if (arg_info->class_name) { zend_error_noreturn(E_COMPILE_ERROR, "Default value for parameters " "with a class type hint can only be NULL"); - } else if (!ZEND_SAME_FAKE_TYPE(arg_info->type_hint, Z_TYPE(default_node.u.constant))) { - zend_error_noreturn(E_COMPILE_ERROR, "Default value for parameters " - "with a %s type hint can only be %s or NULL", - zend_get_type_by_const(arg_info->type_hint), zend_get_type_by_const(arg_info->type_hint)); + } else switch (arg_info->type_hint) { + case IS_DOUBLE: + if (Z_TYPE(default_node.u.constant) != IS_DOUBLE && Z_TYPE(default_node.u.constant) != IS_LONG) { + zend_error_noreturn(E_COMPILE_ERROR, "Default value for parameters " + "with a float type hint can only be float, integer, or NULL"); + } + break; + + default: + if (!ZEND_SAME_FAKE_TYPE(arg_info->type_hint, Z_TYPE(default_node.u.constant))) { + zend_error_noreturn(E_COMPILE_ERROR, "Default value for parameters " + "with a %s type hint can only be %s or NULL", + zend_get_type_by_const(arg_info->type_hint), zend_get_type_by_const(arg_info->type_hint)); + } + break; } } }