From: Nikita Popov Date: Fri, 17 Jul 2020 13:09:29 +0000 (+0200) Subject: Fixed bug #79867 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=86a62eb1fcf923d7b54d63df2277d73913879d7b;p=php 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. --- 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-- + +--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(