Skip to content

zend_ast: Wrap class names in parens during export when they are an expression#22389

Merged
TimWolla merged 2 commits into
php:masterfrom
TimWolla:zend-ast-export-class-name-expression
Jul 6, 2026
Merged

zend_ast: Wrap class names in parens during export when they are an expression#22389
TimWolla merged 2 commits into
php:masterfrom
TimWolla:zend-ast-export-class-name-expression

Conversation

@TimWolla

Copy link
Copy Markdown
Member

Fixes #22387.

@TimWolla
TimWolla requested a review from iliaal June 21, 2026 20:07
@TimWolla
TimWolla requested a review from bukka as a code owner June 21, 2026 20:07
@TimWolla
TimWolla force-pushed the zend-ast-export-class-name-expression branch from add205b to 9c7592b Compare June 21, 2026 20:34
@TimWolla
TimWolla requested a review from iluuu1994 June 21, 2026 20:35
@iliaal

iliaal commented Jun 21, 2026

Copy link
Copy Markdown
Contributor

X::class (ZEND_AST_CLASS_NAME) still goes through zend_ast_export_ns_name(), so (bar)::class exports as bar::class, which re-parses to the string "bar" rather than (bar)::class. Same case as the (bar)::m() / (bar)::$p / (bar)::C ones already covered. Switching that branch to zend_ast_export_ns_name_or_expression() fixes it, and it's worth a (bar)::class line in the test.

@TimWolla
TimWolla force-pushed the zend-ast-export-class-name-expression branch from 9c7592b to 6d478dd Compare June 21, 2026 21:41
@TimWolla

Copy link
Copy Markdown
Member Author

I had seen that and then during testing it complained about ::class not being legal on a string (since I was testing with a string constant) and then disregard that as a case that cannot happen in practice. Of course AST printing also needs to work with runtime errors, and constants can contain objects.

Now fixed, thanks.

@iliaal iliaal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

Comment thread Zend/zend_ast.c Outdated
@TimWolla
TimWolla force-pushed the zend-ast-export-class-name-expression branch from 6d478dd to b597b35 Compare June 23, 2026 17:37
@TimWolla
TimWolla requested a review from iluuu1994 June 23, 2026 17:38

@iluuu1994 iluuu1994 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Only nits. Thanks!

Comment thread Zend/zend_ast.c Outdated
@@ -0,0 +1,62 @@
--TEST--
GH-22387: AST pretty-printing drops meaningful parentheses around RHS of instanceof

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This test does more than check instanceof.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, this is borrowing the title from the associated issue (and then testing some related logic).

Comment thread Zend/zend_ast.c
break;
case ZEND_AST_STATIC_CALL:
zend_ast_export_ns_name(str, ast->child[0], 0, indent);
if (zend_ast_is_parent_hook_call(ast)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I assume this is already tested elsewhere?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, Zend/tests/property_hooks/ast_printing.phpt fails without this check:

TEST 11/211 [Zend/tests/property_hooks/ast_printing.phpt]
========DIFF========
--
         }
         public $prop2 {
             get {
008-             return parent::$prop1::get();
008+             return (parent::$prop1)::get();
             }
             final set {
                 echo 'Foo';
--
========DONE========
FAIL Hook AST printing [Zend/tests/property_hooks/ast_printing.phpt] 

@TimWolla
TimWolla force-pushed the zend-ast-export-class-name-expression branch from b597b35 to 34502c1 Compare July 6, 2026 19:08
@TimWolla
TimWolla merged commit 7330366 into php:master Jul 6, 2026
18 checks passed
@TimWolla
TimWolla deleted the zend-ast-export-class-name-expression branch July 7, 2026 11:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AST pretty-printing drops meaningful parentheses around RHS of instanceof

3 participants