com php-src: Allow integer default for float type: Zend/tests/t ypehints/scalar_float_with_integer_default_strict.p hpt
Zend/tests/typehints/scalar_float_with_integer_de fault_weak.phpt Zend/tests/typehints/scalar_float_wit h_invalid_default.phpt Zend/zend_compile.c
| From: | Aaron Piotrowski | Date: | Thu, 02 Jul 2015 23:07:24 +0000 |
| Subject: | com php-src: Allow integer default for float type: Zend/tests/t ypehints/scalar_float_with_integer_default_strict.p hpt Zend/tests/typehints/scalar_float_with_integer_de fault_weak.phpt Zend/tests/typehints/scalar_float_wit h_invalid_default.phpt Zend/zend_compile.c |
||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-87717@lists.php.net to get a copy of this message | ||
Commit: 68baa539dc1e42a904617fcb799b58a5705b87ba
Author: Aaron Piotrowski <aaron@trowski.com> Thu, 2 Jul 2015 17:46:51 -0500
Parents: 2768bdbce08646ded80bc472a8b19631a23c2da7
Branches: master
Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=68baa539dc1e42a904617fcb799b58a5705b87ba
Log:
Allow integer default for float type
Changed paths:
A Zend/tests/typehints/scalar_float_with_integer_default_strict.phpt
A Zend/tests/typehints/scalar_float_with_integer_default_weak.phpt
A Zend/tests/typehints/scalar_float_with_invalid_default.phpt
M Zend/zend_compile.c
Diff:
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 0000000..b1aab43
--- /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--
+<?php
+
+declare(strict_types=1);
+
+function test(float $arg = 0)
+{
+ var_dump($arg);
+}
+
+test();
+
+?>
+--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 0000000..ab32066
--- /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--
+<?php
+
+function test(float $arg = 0)
+{
+ var_dump($arg);
+}
+
+test();
+
+?>
+--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 0000000..6b67985
--- /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--
+<?php
+
+function test(float $arg = true)
+{
+ var_dump($arg);
+}
+
+test();
+
+?>
+--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 df19f93..5a15392 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;
}
}
}