com php-src: Fixed bug #79867: NEWS Zend/tests/ctor_promotion _untyped_default.phpt Zend/zend_compile.c
| From: | Nikita Popov | Date: | Fri, 17 Jul 2020 13:10:45 +0000 |
| Subject: | com php-src: Fixed bug #79867: NEWS Zend/tests/ctor_promotion _untyped_default.phpt Zend/zend_compile.c | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-112207@lists.php.net to get a copy of this message | ||
Commit: 86a62eb1fcf923d7b54d63df2277d73913879d7b
Author: Nikita Popov <nikita.ppv@gmail.com> Fri, 17 Jul 2020 15:09:29 +0200
Parents: 6c8b94eb422049601bab61fa5853869108f084f4
Branches: master
Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=86a62eb1fcf923d7b54d63df2277d73913879d7b
Log:
Fixed bug #79867
In line with usual rules, give untyped properties a null default
value. Otherwise constructor promotion would give you a property
declaration that cannot be achieved through any other means.
Bugs:
https://bugs.php.net/79867
Changed paths:
M NEWS
A Zend/tests/ctor_promotion_untyped_default.phpt
M Zend/zend_compile.c
Diff:
diff --git a/NEWS b/NEWS
index 00100bed80..55bba33d12 100644
--- a/NEWS
+++ b/NEWS
@@ -13,6 +13,8 @@ PHP NEWS
(Nikita)
. Fixed bug #79852 (count(DOMNodeList) doesn't match
count(IteratorIterator(DOMNodeList))). (Nikita)
+ .. Fixed bug #79867 (Promoted untyped properties should get null default
+ value). (Nikita)
09 Jul 2020, PHP 8.0.0alpha2
diff --git a/Zend/tests/ctor_promotion_untyped_default.phpt
b/Zend/tests/ctor_promotion_untyped_default.phpt
new file mode 100644
index 0000000000..50b4962aa4
--- /dev/null
+++ b/Zend/tests/ctor_promotion_untyped_default.phpt
@@ -0,0 +1,29 @@
+--TEST--
+Bug #79867: Promoted untyped properties should get null default value
+--FILE--
+<?php
+
+class A {
+ public function __construct(
+ public $untyped = 1,
+ public int $typed = 2,
+ ) {}
+}
+
+class B extends A {
+ public function __construct() {
+ // Missing parent::__construct() call,
+ // properties will not be initialized.
+ }
+}
+
+var_dump(new B);
+
+?>
+--EXPECT--
+object(B)#1 (1) {
+ ["untyped"]=>
+ NULL
+ ["typed"]=>
+ uninitialized(int)
+}
diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c
index 6725e7ee82..4529aea5ba 100644
--- a/Zend/zend_compile.c
+++ b/Zend/zend_compile.c
@@ -6197,16 +6197,21 @@ void zend_compile_params(zend_ast *ast, zend_ast *return_type_ast, uint32_t
fall
ZSTR_VAL(scope->name), ZSTR_VAL(name), ZSTR_VAL(str));
}
- /* Always use uninitialized as the default. */
- zval default_value;
- ZVAL_UNDEF(&default_value);
-
/* Recompile the type, as it has different memory management requirements. */
zend_type type = ZEND_TYPE_INIT_NONE(0);
if (type_ast) {
type = zend_compile_typename(type_ast, /* force_allow_null */ 0, /* use_arena */ 1);
}
+ /* Don't give the property an explicit default value. For typed properties this means
+ * uninitialized, for untyped properties it means an implicit null default value. */
+ zval default_value;
+ if (ZEND_TYPE_IS_SET(type)) {
+ ZVAL_UNDEF(&default_value);
+ } else {
+ ZVAL_NULL(&default_value);
+ }
+
zend_string *doc_comment =
doc_comment_ast ? zend_string_copy(zend_ast_get_str(doc_comment_ast)) : NULL;
zend_property_info *prop = zend_declare_typed_property(