Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AST->string does not reproduce constructor property promotion correctly #17101

Open
DanielEScherzer opened this issue Dec 9, 2024 · 4 comments · May be fixed by #17133
Open

AST->string does not reproduce constructor property promotion correctly #17101

DanielEScherzer opened this issue Dec 9, 2024 · 4 comments · May be fixed by #17133

Comments

@DanielEScherzer
Copy link
Contributor

Description

The following code:

<?php

assert(false && new class (true) {
    public function __construct( public bool $boolVal ) {}
} );

Resulted in this output:

Fatal error: Uncaught AssertionError: assert(false && new class(true) {
    public function __construct(bool $boolVal) {
    }

}) in /in/rM9Hi:3
Stack trace:
#0 /in/rM9Hi(3): assert(false, 'assert(false &&...')
#1 {main}
  thrown in /in/rM9Hi on line 3

But I expected this output instead (note the public in the constructor property)

Fatal error: Uncaught AssertionError: assert(false && new class(true) {
    public function __construct(public bool $boolVal) {
    }

}) in /in/rM9Hi:3
Stack trace:
#0 /in/rM9Hi(3): assert(false, 'assert(false &&...')
#1 {main}
  thrown in /in/rM9Hi on line 3

See https://3v4l.org/rM9Hi

PHP Version

PHP 8.2+

Operating System

No response

@DanielEScherzer
Copy link
Contributor Author

DanielEScherzer commented Dec 9, 2024

Discovered via the zend_test_compile_to_ast() function I'm adding as part of #16952 but exists in the current functionality

Also fails with other promoted modifiers, like asymmetric visibility

@nielsdos
Copy link
Member

nielsdos commented Dec 9, 2024

Yeah this is definitely a bug, likely overlooked because anonymous classes are the only way you can print a function that has a promoted property as argument.

@nielsdos
Copy link
Member

The fix for the visibility is simple, but the hooks are also not outputted so I'll try to tackle that too.

@nielsdos
Copy link
Member

Another bug: the hook type is incorrect in AST printing for the following case:

assert(false && new class {
    public $prop { set => $this->prop = 1; }
});

nielsdos added a commit to nielsdos/php-src that referenced this issue Dec 12, 2024
nielsdos added a commit to nielsdos/php-src that referenced this issue Dec 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants