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

Implement GH-18261: Allow cast to be used in constant expressions #18264

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

nielsdos
Copy link
Member

@nielsdos nielsdos commented Apr 6, 2025

No description provided.

@nielsdos nielsdos linked an issue Apr 6, 2025 that may be closed by this pull request
}
break;
case IS_OBJECT:
/* Adapted from VM */
Copy link
Member

@iluuu1994 iluuu1994 Apr 6, 2025

Choose a reason for hiding this comment

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

I think the object cast is problematic. This will lead to an incorrect result.

class C {
    public $prop = (object) [];
}

$c = new C;
$c->prop->x = 42;
var_dump(new C);

Copy link
Member Author

Choose a reason for hiding this comment

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

I remember from the RECV_INIT case that we always re-evaluate refcounted values (

/* we keep in cache only not refcounted values */
), of course this particular situation is different but I assumed this held in general.

The code your provided doesn't execute though. I presume you meant (object) [], in which case there is indeed a problem.

Copy link
Member Author

Choose a reason for hiding this comment

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

We could ban the object cast from constant evaluation (which should be rare anyway), or find a workaround. I think the former (banning it) seems more reasonable. WDYT?

Copy link
Member

Choose a reason for hiding this comment

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

I agree that object cast should simply be forbidden. This feature is not worth changing how we evaluate property constant expressions.

Copy link
Member

@bwoebi bwoebi Apr 6, 2025

Choose a reason for hiding this comment

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

@iluuu1994 Should it not be treated the same than https://wiki.php.net/rfc/new_in_initializers simply? At least that's what I'd expect.
Doing new stdClass or (object) [] is in the exact same vein.

Copy link
Member

@iluuu1994 iluuu1994 Apr 6, 2025

Choose a reason for hiding this comment

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

@bwoebi If @nielsdos wants to add this support, I'm fine with it. Importantly, zend_ast_evaluate_ctx.had_side_effects still needs to be set to false so that we don't have erroneous caching for RECV_INIT. In this case, static properties and class constants would technically be fine, since the cast does not have any side-effects.

const T4 = (object) ["a" => 1];
const T5 = (float) 5;
const T6 = (array) "";
const T7 = (array) var_dump(...);
Copy link
Contributor

Choose a reason for hiding this comment

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

Test with warning like https://3v4l.org/6ugOa should be added.

IDK if exception is possible, if so, it should be tested as well.

Copy link
Member

Choose a reason for hiding this comment

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

Exceptions are at least possible with (string) $nonStringable, there might be others.

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.

Allow cast to be used in constant expressions
4 participants