Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Zend/zend_ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,28 @@ ZEND_API void zend_ast_evaluate(zval *result, zend_ast *ast, zend_class_entry *s
}
}

/* addrefs zvals in an AST recursively */
ZEND_API void zend_ast_addref(zend_ast *ast) /* {{{ */
{
if (ast == NULL) {
return;
} else if (ast->kind == ZEND_AST_ZVAL) {
Z_TRY_ADDREF_P(zend_ast_get_zval(ast));
} else if (zend_ast_is_list(ast)) {
zend_ast_list *list = zend_ast_get_list(ast);
uint32_t i;
for (i = 0; i < list->children; i++) {
zend_ast_addref(list->child[i]);
}
} else {
uint32_t i, children = zend_ast_get_num_children(ast);
for (i = 0; i < children; i++) {
zend_ast_addref(ast->child[i]);
}
}
}
/* }}} */

ZEND_API zend_ast *zend_ast_copy(zend_ast *ast)
{
if (ast == NULL) {
Expand Down
1 change: 1 addition & 0 deletions Zend/zend_ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ ZEND_API zend_ast *zend_ast_list_add(zend_ast *list, zend_ast *op);

ZEND_API void zend_ast_evaluate(zval *result, zend_ast *ast, zend_class_entry *scope TSRMLS_DC);

ZEND_API void zend_ast_addref(zend_ast *ast);
ZEND_API zend_ast *zend_ast_copy(zend_ast *ast);
ZEND_API void zend_ast_destroy(zend_ast *ast);
ZEND_API void zend_ast_destroy_and_free(zend_ast *ast);
Expand Down
20 changes: 18 additions & 2 deletions Zend/zend_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -6902,8 +6902,24 @@ void zend_compile_conditional(znode *result, zend_ast *ast TSRMLS_DC) /* {{{ */
uint32_t opnum_jmpz, opnum_jmp, opnum_qm_assign1;

if (!true_ast) {
zend_compile_shorthand_conditional(result, ast TSRMLS_CC);
return;
/* if variable is isset()able, rewrite $a ?: $b to empty($a) ? $b : $a */
if (zend_is_variable(cond_ast) && !zend_is_call(cond_ast))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't that a behavior change? Before, if $a didn't exist, there were notice, but after, there's no notice anymore.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A behaviour change, yes, but it doesn't break backwards-compatibility. I'm writing an RFC for this as we speak, just to clarify.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does it not break BC? You have no idea what people might do with that notice in their custom error handlers. If you remove it, that code won't be triggered and the app will behave differently.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, it won't break BC for most applications, but it will break things if they relied on it throwing an E_NOTICE. I'll update the RFC to note that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I didn't realize that. Within the context of RFC it makes sense. I'd support that for PHP 7.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems a bit inconsistent to me that $a ? $a : $b will throw a notice and $a ?: $b won't. We would need to make that change extremely clear in the documentation highlighting the fact that ?: is now the first operator to suppress these notices and that it is now fundamentally different from its cousin, the ? expr1 : expr2 operator.

{
zend_ast *swap_temp = cond_ast;

cond_ast = ast->child[0] = zend_ast_create(ZEND_AST_EMPTY, cond_ast);

true_ast = ast->child[1] = false_ast;
false_ast = ast->child[2] = swap_temp;

/* avoid any zvals being double-freed due to cond_ast being referenced twice */
zend_ast_addref(swap_temp);
}
else
{
zend_compile_shorthand_conditional(result, ast TSRMLS_CC);
return;
}
}

zend_compile_expr(&cond_node, cond_ast TSRMLS_CC);
Expand Down
58 changes: 58 additions & 0 deletions tests/lang/operators/shortTernaryIsset.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
--TEST--
Test short ternary operator for implicit isset cases
--FILE--
<?php

$var = 7;
$var2 = false;

$obj = new StdClass;
$obj->boo = 7;

$arr = [
2 => 7,
"foo" => "bar",
"foobar" => "",
"qux" => $obj,
"bing" => [
"bang"
]
];

var_dump($nonexistant_variable ?: 3);
echo PHP_EOL;
var_dump($var ?: 3);
var_dump($var2 ?: 3);
echo PHP_EOL;
var_dump($obj->boo ?: 3);
var_dump($obj->bing ?: 3);
var_dump($arr["qux"]->boo ?: 3);
var_dump($arr["qux"]->bing ?: 3);
echo PHP_EOL;
var_dump($arr[2] ?: 3);
var_dump($arr["foo"] ?: 3);
var_dump($arr["foobar"] ?: 3);
var_dump($arr["qux"] ?: 3);
var_dump($arr["bing"][0] ?: 3);
var_dump($arr["bing"][1] ?: 3);
?>
--EXPECTF--
int(3)

int(7)
int(3)

int(7)
int(3)
int(7)
int(3)

int(7)
string(3) "bar"
int(3)
object(stdClass)#1 (%d) {
["boo"]=>
int(7)
}
string(4) "bang"
int(3)