Skip to content

Commit 2b71df7

Browse files
committed
Merge branch 'PHP-8.0' into PHP-8.1
* PHP-8.0: Fix type inference and SCCP with typed references
2 parents b88c678 + d8c2ff6 commit 2b71df7

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

Zend/Optimizer/sccp.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -992,14 +992,15 @@ static void sccp_visit_instr(scdf_ctx *scdf, zend_op *opline, zend_ssa_op *ssa_o
992992
switch (opline->opcode) {
993993
case ZEND_ASSIGN:
994994
/* The value of op1 is irrelevant here, because we are overwriting it
995-
* -- unless it can be a reference, in which case we propagate a BOT. */
995+
* -- unless it can be a reference, in which case we propagate a BOT.
996+
* The result is also BOT in this case, because it might be a typed reference. */
996997
if (IS_BOT(op1) && (ctx->scdf.ssa->var_info[ssa_op->op1_use].type & MAY_BE_REF)) {
997998
SET_RESULT_BOT(op1);
999+
SET_RESULT_BOT(result);
9981000
} else {
9991001
SET_RESULT(op1, op2);
1002+
SET_RESULT(result, op2);
10001003
}
1001-
1002-
SET_RESULT(result, op2);
10031004
return;
10041005
case ZEND_TYPE_CHECK:
10051006
/* We may be able to evaluate TYPE_CHECK based on type inference info,

Zend/Optimizer/zend_inference.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2916,7 +2916,12 @@ static zend_always_inline int _zend_update_type_info(
29162916
COPY_SSA_OBJ_TYPE(ssa_op->op2_use, ssa_op->op1_def);
29172917
}
29182918
if (ssa_op->result_def >= 0) {
2919-
UPDATE_SSA_TYPE(tmp & ~MAY_BE_REF, ssa_op->result_def);
2919+
if (tmp & MAY_BE_REF) {
2920+
/* Assignment to typed reference may change type.
2921+
* Be conservative and don't assume anything. */
2922+
tmp = MAY_BE_RC1|MAY_BE_RCN|MAY_BE_ANY|MAY_BE_ARRAY_KEY_ANY|MAY_BE_ARRAY_OF_ANY|MAY_BE_ARRAY_OF_REF;
2923+
}
2924+
UPDATE_SSA_TYPE(tmp, ssa_op->result_def);
29202925
COPY_SSA_OBJ_TYPE(ssa_op->op2_use, ssa_op->result_def);
29212926
}
29222927
break;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
Result of assigning to typed reference
3+
--FILE--
4+
<?php
5+
6+
class Test {
7+
public ?string $prop;
8+
}
9+
function test() {
10+
$obj = new Test;
11+
$ref =& $obj->prop;
12+
var_dump($ref = 0);
13+
}
14+
test();
15+
16+
?>
17+
--EXPECT--
18+
string(1) "0"

0 commit comments

Comments
 (0)