-
Notifications
You must be signed in to change notification settings - Fork 7.8k
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
base: master
Are you sure you want to change the base?
Conversation
} | ||
break; | ||
case IS_OBJECT: | ||
/* Adapted from VM */ |
There was a problem hiding this comment.
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);
There was a problem hiding this comment.
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 (
Line 5728 in ac9392b
/* we keep in cache only not refcounted values */ |
The code your provided doesn't execute though. I presume you meant (object) []
, in which case there is indeed a problem.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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(...); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
No description provided.