Skip to content

Commit 3e2b127

Browse files
committed
Merge branch 'PHP-8.0'
* PHP-8.0: Fix SSA integrity violation for type inference in dead code
2 parents fdbfb41 + 5958137 commit 3e2b127

File tree

2 files changed

+37
-15
lines changed

2 files changed

+37
-15
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
Make sure type inference upholds invariants for dead arrays
3+
--FILE--
4+
<?php
5+
6+
function test() {
7+
foreach ($a as $v) {
8+
$b[] = $v;
9+
}
10+
}
11+
12+
test();
13+
14+
?>
15+
--EXPECTF--
16+
Warning: Undefined variable $a in %s on line %d
17+
18+
Warning: foreach() argument must be of type array|object, null given in %s on line %d

ext/opcache/Optimizer/zend_inference.c

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2011,24 +2011,28 @@ static uint32_t assign_dim_result_type(
20112011
tmp |= MAY_BE_RC1 | MAY_BE_RCN;
20122012
}
20132013
if (tmp & MAY_BE_ARRAY) {
2014-
if (value_type & MAY_BE_UNDEF) {
2015-
tmp |= MAY_BE_ARRAY_OF_NULL;
2016-
}
2017-
if (dim_op_type == IS_UNUSED) {
2018-
tmp |= MAY_BE_ARRAY_KEY_LONG;
2019-
} else {
2020-
if (dim_type & (MAY_BE_LONG|MAY_BE_FALSE|MAY_BE_TRUE|MAY_BE_RESOURCE|MAY_BE_DOUBLE)) {
2021-
tmp |= MAY_BE_ARRAY_KEY_LONG;
2014+
/* Only add key type if we have a value type. We want to maintain the invariant that a
2015+
* key type exists iff a value type exists even in dead code that may use empty types. */
2016+
if (value_type & (MAY_BE_ANY|MAY_BE_UNDEF)) {
2017+
if (value_type & MAY_BE_UNDEF) {
2018+
tmp |= MAY_BE_ARRAY_OF_NULL;
20222019
}
2023-
if (dim_type & MAY_BE_STRING) {
2024-
tmp |= MAY_BE_ARRAY_KEY_STRING;
2025-
if (dim_op_type != IS_CONST) {
2026-
// FIXME: numeric string
2020+
if (dim_op_type == IS_UNUSED) {
2021+
tmp |= MAY_BE_ARRAY_KEY_LONG;
2022+
} else {
2023+
if (dim_type & (MAY_BE_LONG|MAY_BE_FALSE|MAY_BE_TRUE|MAY_BE_RESOURCE|MAY_BE_DOUBLE)) {
20272024
tmp |= MAY_BE_ARRAY_KEY_LONG;
20282025
}
2029-
}
2030-
if (dim_type & (MAY_BE_UNDEF|MAY_BE_NULL)) {
2031-
tmp |= MAY_BE_ARRAY_KEY_STRING;
2026+
if (dim_type & MAY_BE_STRING) {
2027+
tmp |= MAY_BE_ARRAY_KEY_STRING;
2028+
if (dim_op_type != IS_CONST) {
2029+
// FIXME: numeric string
2030+
tmp |= MAY_BE_ARRAY_KEY_LONG;
2031+
}
2032+
}
2033+
if (dim_type & (MAY_BE_UNDEF|MAY_BE_NULL)) {
2034+
tmp |= MAY_BE_ARRAY_KEY_STRING;
2035+
}
20322036
}
20332037
}
20342038
/* Only add value type if we have a key type. It might be that the key type is illegal

0 commit comments

Comments
 (0)