-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Implement GH-18261: Allow cast to be used in constant expressions #18264
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
Changes from all commits
3c616f5
d81b1b8
d264057
d08c584
40d8e9b
93b5145
3a3ba87
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
--TEST-- | ||
Constant expressions with cast | ||
--FILE-- | ||
<?php | ||
class X { | ||
public int $foo = 3; | ||
} | ||
|
||
const T1 = (int) 0.3; | ||
const T2 = (bool) 0.3; | ||
const T3 = (string) []; | ||
const T4 = (object) ["a" => 1]; | ||
const T5 = (float) 5; | ||
const T6 = (array) ""; | ||
const T7 = (array) var_dump(...); | ||
const T8 = (array) new X; | ||
const T9 = (array) new DateTime; | ||
const T10 = (int) new DateTime; | ||
|
||
var_dump(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10); | ||
?> | ||
--EXPECTF-- | ||
Warning: Array to string conversion in %s on line %d | ||
|
||
Warning: Object of class DateTime could not be converted to int in %s on line %d | ||
int(0) | ||
bool(true) | ||
string(5) "Array" | ||
object(stdClass)#%d (1) { | ||
["a"]=> | ||
int(1) | ||
} | ||
float(5) | ||
array(1) { | ||
[0]=> | ||
string(0) "" | ||
} | ||
array(1) { | ||
[0]=> | ||
object(Closure)#%d (2) { | ||
["function"]=> | ||
string(8) "var_dump" | ||
["parameter"]=> | ||
array(2) { | ||
["$value"]=> | ||
string(10) "<required>" | ||
["$values"]=> | ||
string(10) "<optional>" | ||
} | ||
} | ||
} | ||
array(1) { | ||
["foo"]=> | ||
int(3) | ||
} | ||
array(3) { | ||
["date"]=> | ||
string(%d) "%s" | ||
["timezone_type"]=> | ||
int(%d) | ||
["timezone"]=> | ||
string(%d) "%s" | ||
} | ||
int(1) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--TEST-- | ||
Constant expressions with object cast in property | ||
--FILE-- | ||
<?php | ||
class X { | ||
public $foo = (object) []; | ||
} | ||
?> | ||
--EXPECTF-- | ||
Fatal error: Object casts are not supported in this context in %s on line %d |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -702,6 +702,41 @@ ZEND_API zend_result ZEND_FASTCALL zend_ast_evaluate_inner( | |
} | ||
zval_ptr_dtor_nogc(&op1); | ||
break; | ||
case ZEND_AST_CAST: | ||
if (UNEXPECTED(zend_ast_evaluate_ex(&op1, ast->child[0], scope, &short_circuited, ctx) != SUCCESS)) { | ||
iluuu1994 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ret = FAILURE; | ||
break; | ||
} | ||
if (ast->attr == Z_TYPE(op1)) { | ||
ZVAL_COPY_VALUE(result, &op1); | ||
} else { | ||
switch (ast->attr) { | ||
case _IS_BOOL: | ||
ZVAL_BOOL(result, zend_is_true(&op1)); | ||
break; | ||
case IS_LONG: | ||
ZVAL_LONG(result, zval_get_long_func(&op1, false)); | ||
break; | ||
case IS_DOUBLE: | ||
ZVAL_DOUBLE(result, zval_get_double_func(&op1)); | ||
break; | ||
case IS_STRING: | ||
ZVAL_STR(result, zval_get_string_func(&op1)); | ||
break; | ||
case IS_ARRAY: | ||
zend_cast_zval_to_array(result, &op1, IS_VAR); | ||
break; | ||
case IS_OBJECT: | ||
zend_cast_zval_to_object(result, &op1, IS_VAR); | ||
break; | ||
EMPTY_SWITCH_DEFAULT_CASE(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what about the new There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Void casts are not allowed in expression context, only when their return value is not used. This never applies to constant expressions. This is also a different ast type ( |
||
} | ||
zval_ptr_dtor_nogc(&op1); | ||
if (UNEXPECTED(EG(exception))) { | ||
ret = FAILURE; | ||
} | ||
} | ||
break; | ||
case ZEND_AST_OR: | ||
if (UNEXPECTED(zend_ast_evaluate_ex(&op1, ast->child[0], scope, &short_circuited, ctx) != SUCCESS)) { | ||
ret = FAILURE; | ||
|
Uh oh!
There was an error while loading. Please reload this page.