-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Add (void)
cast
#18115
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
Add (void)
cast
#18115
Changes from all commits
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,47 @@ | ||
--TEST-- | ||
casting different variables to void | ||
--FILE-- | ||
<?php | ||
|
||
$r = fopen(__FILE__, "r"); | ||
|
||
class test { | ||
private $var1 = 1; | ||
public $var2 = 2; | ||
protected $var3 = 3; | ||
|
||
function __toString() { | ||
return "10"; | ||
} | ||
} | ||
|
||
$o = new test; | ||
|
||
$vars = array( | ||
"string", | ||
"", | ||
"\0", | ||
"8754456", | ||
9876545, | ||
0.10, | ||
array(), | ||
array(1,2,3), | ||
false, | ||
true, | ||
NULL, | ||
$r, | ||
$o | ||
); | ||
|
||
foreach ($vars as $var) { | ||
(void)$var; | ||
} | ||
|
||
// Cast literals to verify behavior for optimized const zvals | ||
(void)"foo"; | ||
(void)["foo", "bar"]; | ||
|
||
echo "Done\n"; | ||
?> | ||
--EXPECTF-- | ||
Done |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
--TEST-- | ||
(void) is included in AST printing | ||
--FILE-- | ||
<?php | ||
|
||
try { | ||
assert(false && function () { | ||
(void) somefunc(); | ||
}); | ||
} catch (Error $e) { | ||
echo $e->getMessage(), "\n"; | ||
} | ||
|
||
?> | ||
--EXPECT-- | ||
assert(false && function () { | ||
(void)somefunc(); | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
--TEST-- | ||
casting to void destroys the value. | ||
--FILE-- | ||
<?php | ||
|
||
class WithDestructor { | ||
public function __destruct() { | ||
echo __METHOD__, PHP_EOL; | ||
} | ||
} | ||
|
||
function test(): WithDestructor { | ||
return new WithDestructor(); | ||
} | ||
|
||
function do_it(): void { | ||
echo "Before", PHP_EOL; | ||
|
||
(void)test(); | ||
|
||
echo "After", PHP_EOL; | ||
} | ||
|
||
do_it(); | ||
|
||
?> | ||
--EXPECT-- | ||
Before | ||
WithDestructor::__destruct | ||
After |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
--TEST-- | ||
casting to void is a statement | ||
--FILE-- | ||
<?php | ||
|
||
$tmp = (void)$dummy; | ||
|
||
echo "Done\n"; | ||
?> | ||
--EXPECTF-- | ||
Parse error: syntax error, unexpected token "(void)" in %s on line %d | ||
TimWolla marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10589,6 +10589,26 @@ static void zend_compile_include_or_eval(znode *result, zend_ast *ast) /* {{{ */ | |
} | ||
/* }}} */ | ||
|
||
static void zend_compile_void_cast(znode *result, zend_ast *ast) | ||
{ | ||
zend_ast *expr_ast = ast->child[0]; | ||
znode expr_node; | ||
zend_op *opline; | ||
|
||
zend_compile_expr(&expr_node, expr_ast); | ||
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. I guess you could optimize it a bit by not even handling IS_CONSTANT_AST in the first place, i.e. early returning. 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. As with the |
||
|
||
switch (expr_node.op_type) { | ||
case IS_TMP_VAR: | ||
case IS_VAR: | ||
opline = zend_emit_op(NULL, ZEND_FREE, &expr_node, NULL); | ||
opline->extended_value = ZEND_FREE_VOID_CAST; | ||
break; | ||
case IS_CONST: | ||
zend_do_free(&expr_node); | ||
break; | ||
} | ||
} | ||
|
||
static void zend_compile_isset_or_empty(znode *result, zend_ast *ast) /* {{{ */ | ||
{ | ||
zend_ast *var_ast = ast->child[0]; | ||
|
@@ -11547,6 +11567,9 @@ static void zend_compile_stmt(zend_ast *ast) /* {{{ */ | |
case ZEND_AST_THROW: | ||
zend_compile_expr(NULL, ast); | ||
break; | ||
case ZEND_AST_CAST_VOID: | ||
zend_compile_void_cast(NULL, ast); | ||
break; | ||
default: | ||
{ | ||
znode result; | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.