Skip to content

Commit

Permalink
Fixed bug #79867
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nikic committed Jul 17, 2020
1 parent 6c8b94e commit 86a62eb
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
29 changes: 29 additions & 0 deletions Zend/tests/ctor_promotion_untyped_default.phpt
Original file line number Diff line number Diff line change
@@ -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)
}
13 changes: 9 additions & 4 deletions Zend/zend_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 86a62eb

Please sign in to comment.