]> granicus.if.org Git - php/commitdiff
Allow integer default for float type
authorAaron Piotrowski <aaron@trowski.com>
Thu, 2 Jul 2015 22:46:51 +0000 (17:46 -0500)
committerAaron Piotrowski <aaron@trowski.com>
Thu, 2 Jul 2015 23:07:24 +0000 (18:07 -0500)
Zend/tests/typehints/scalar_float_with_integer_default_strict.phpt [new file with mode: 0644]
Zend/tests/typehints/scalar_float_with_integer_default_weak.phpt [new file with mode: 0644]
Zend/tests/typehints/scalar_float_with_invalid_default.phpt [new file with mode: 0644]
Zend/zend_compile.c

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 (file)
index 0000000..b1aab43
--- /dev/null
@@ -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 (file)
index 0000000..ab32066
--- /dev/null
@@ -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 (file)
index 0000000..6b67985
--- /dev/null
@@ -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
index df19f93be2602555aa51889a0928308a49cfa32c..5a15392fe94747347c900066e8508217698137c2 100644 (file)
@@ -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;
                                        }
                                }
                        }