Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion Zend/Optimizer/block_pass.c
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,14 @@ static void zend_optimize_block(zend_basic_block *block, zend_op_array *op_array
zval c;

ZVAL_COPY(&c, &ZEND_OP1_LITERAL(src));
if (zend_optimizer_update_op2_const(op_array, opline, &c)) {
if (opline->opcode == ZEND_TYPE_ASSERT) {
/* Does not consume its operand, which is used again later. */
if (zend_optimizer_update_op2_const(op_array, opline, &c)) {
++(*opt_count);
} else {
zval_ptr_dtor_nogc(&c);
}
} else if (zend_optimizer_update_op2_const(op_array, opline, &c)) {
VAR_SOURCE(op2) = NULL;
if (!zend_bitset_in(used_ext, VAR_NUM(src->result.var))) {
literal_dtor(&ZEND_OP1_LITERAL(src));
Expand Down
10 changes: 10 additions & 0 deletions Zend/Optimizer/zend_optimizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,16 @@ bool zend_optimizer_replace_by_const(zend_op_array *op_array,

if (opline->op2_type == type &&
opline->op2.var == var) {
if (opline->opcode == ZEND_TYPE_ASSERT) {
/* Does not consume its operand, which is used again later. */
Z_TRY_ADDREF_P(val);
if (!zend_optimizer_update_op2_const(op_array, opline, val)) {
zval_ptr_dtor(val);
return false;
}
opline++;
continue;
}
return zend_optimizer_update_op2_const(op_array, opline, val);
}
opline++;
Expand Down
35 changes: 35 additions & 0 deletions ext/opcache/tests/opt/type_assert_const_operand.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
--TEST--
TYPE_ASSERT does not consume its operand, which may be replaced by a constant
--INI--
opcache.enable=1
opcache.enable_cli=1
opcache.optimization_level=-1
opcache.file_update_protection=0
--EXTENSIONS--
opcache
--FILE--
<?php
/* The array_map() loop uses the array in TYPE_ASSERT and then in FE_RESET_R.
* The array is only known to be constant after optimization. */
function map_array() {
return array_map(strlen(...), constant('PHP_EOL') ? ['a', 'bb'] : 1);
}
function map_int() {
return array_map(strlen(...), constant('PHP_EOL') ? 1 : []);
}

var_dump(map_array());
try {
map_int();
} catch (TypeError $e) {
echo $e->getMessage(), "\n";
}
?>
--EXPECT--
array(2) {
[0]=>
int(1)
[1]=>
int(2)
}
array_map(): Argument #2 ($array) must be of type array, int given
Loading