Skip to content
Open
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
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ PHP NEWS
(Marti2203).
. Fixed bug GH-24989 (Use-after-free in GC when an exception is thrown).
(David Carlier)
. Fixed AST export dropping parentheses around the left operand of property
fetches, method calls and instanceof. (Jorg Sowa)

- CLI:
. Fixed bug GH-24697 (PHP_HTTP_PARSER_STRICT build option doesn't work).
Expand Down
41 changes: 41 additions & 0 deletions Zend/tests/assert/ast_export_deref_parens.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
--TEST--
assert() message keeps parentheses around dereferenced and instanceof operands
--INI--
zend.assertions=1
--FILE--
<?php
class C {
public $x = false;
public function m() { return false; }
}
class D {}

$a = null;
$c = new C;

$tests = [
fn() => assert(($a ?? $c)->x),
fn() => assert(($a ?? $c)?->x),
fn() => assert(($a ?? $c)->m()),
fn() => assert(($a ?? $c)?->m()),
fn() => assert(($a ?? $c) instanceof D),
fn() => assert(-($c instanceof D)),
fn() => assert(!$c instanceof C),
];

foreach ($tests as $test) {
try {
$test();
} catch (AssertionError $e) {
echo $e::class, ': ', $e->getMessage(), "\n";
}
}
?>
--EXPECT--
AssertionError: assert(($a ?? $c)->x)
AssertionError: assert(($a ?? $c)?->x)
AssertionError: assert(($a ?? $c)->m())
AssertionError: assert(($a ?? $c)?->m())
AssertionError: assert(($a ?? $c) instanceof D)
AssertionError: assert(-($c instanceof D))
AssertionError: assert(!($c instanceof C))
8 changes: 5 additions & 3 deletions Zend/zend_ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -2200,7 +2200,7 @@ static ZEND_COLD void zend_ast_export_ex(smart_str *str, zend_ast *ast, int prio
break;
case ZEND_AST_PROP:
case ZEND_AST_NULLSAFE_PROP:
zend_ast_export_ex(str, ast->child[0], 0, indent);
zend_ast_export_ex(str, ast->child[0], 260, indent);
smart_str_appends(str, ast->kind == ZEND_AST_NULLSAFE_PROP ? "?->" : "->");
zend_ast_export_var(str, ast->child[1], 0, indent);
break;
Expand Down Expand Up @@ -2327,9 +2327,11 @@ static ZEND_COLD void zend_ast_export_ex(smart_str *str, zend_ast *ast, int prio
}
break;
case ZEND_AST_INSTANCEOF:
zend_ast_export_ex(str, ast->child[0], 0, indent);
if (priority > 230) smart_str_appendc(str, '(');
zend_ast_export_ex(str, ast->child[0], 231, indent);
smart_str_appends(str, " instanceof ");
zend_ast_export_ns_name(str, ast->child[1], 0, indent);
if (priority > 230) smart_str_appendc(str, ')');
break;
case ZEND_AST_YIELD:
if (priority > 70) smart_str_appendc(str, '(');
Expand Down Expand Up @@ -2539,7 +2541,7 @@ static ZEND_COLD void zend_ast_export_ex(smart_str *str, zend_ast *ast, int prio
/* 3 child nodes */
case ZEND_AST_METHOD_CALL:
case ZEND_AST_NULLSAFE_METHOD_CALL:
zend_ast_export_ex(str, ast->child[0], 0, indent);
zend_ast_export_ex(str, ast->child[0], 260, indent);
smart_str_appends(str, ast->kind == ZEND_AST_NULLSAFE_METHOD_CALL ? "?->" : "->");
zend_ast_export_var(str, ast->child[1], 0, indent);
smart_str_appendc(str, '(');
Expand Down
Loading