-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Narrowing occurred during type inference of ZEND_ADD_ARRAY_ELEMENT #10008
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
Comments
I can reproduce this on PHP-8.1 and higher. <?php
function test()
{
$bool_or_int = true;
while (a()) {
if ($bool_or_int !== true) {
// The following line triggers narrowing during type inference of ZEND_ADD_ARRAY_ELEMENT.
$string_key = "a";
$array = ["bool_or_int" => $bool_or_int, $string_key => 123];
}
$bool_or_int = 0;
}
} This triggers narrowing for two ops: first ZEND_ADD_ARRAY_ELEMENT, and then ZEND_ASSIGN. The type inference happens in the following order:
This seems to occur because of the phi node introduced by the while loop. If I remove the loop the problem goes away. |
…_ARRAY_ELEMENT This test triggers narrowing for two ops: first ZEND_ADD_ARRAY_ELEMENT, and then ZEND_ASSIGN. The type inference happens in the following order: 1) The ZEND_ADD_ARRAY_ELEMENT infers type 0x40e04080 (packed flag is set), arr_type=0 at this point because it hasn't been set by ZEND_INIT_ARRAY yet. 2) The ZEND_INIT_ARRAY infers type 0x40804080 3) The ZEND_ADD_ARRAY_ELEMENT infers type 0x40e04080, arr_type=0x40804080, which does not have the packed flag set while the existing result of ZEND_ADD_ARRAY_ELEMENT has the packed flag set. This seems to occur because of the phi node introduced by the while loop. If I remove the loop the problem goes away. As Arnaud noted, this seems to be caused by a too wide type inference for arr_type==0. We should keep the invariant that if x>=y then key_type(x) >= key_type(y). If we write the possible results down in a table we get: ``` arr_type resulting key type --------------- -------------------------------------------------------------------------- HASH_ONLY -> MAY_BE_ARRAY_NUMERIC_HASH PACKED_ONLY -> MAY_BE_ARRAY_NUMERIC_HASH | MAY_BE_ARRAY_PACKED (== MAY_BE_ARRAY_KEY_LONG) HASH || PACKED -> MAY_BE_ARRAY_NUMERIC_HASH | MAY_BE_ARRAY_PACKED (== MAY_BE_ARRAY_KEY_LONG) 0 -> MAY_BE_ARRAY_NUMERIC_HASH | MAY_BE_ARRAY_PACKED (== MAY_BE_ARRAY_KEY_LONG) ``` As we can see, `HASH_ONLY > 0` but `MAY_BE_ARRAY_NUMERIC_HASH < MAY_BE_ARRAY_NUMERIC_HASH | MAY_BE_ARRAY_PACKED`, which violates the invariant. Instead if we modify the zero case to have MAY_BE_ARRAY_NUMERIC_HASH instead, we get the following table which satisfies the invariant. ``` arr_type resulting key type --------------- -------------------------------------------------------------------------- HASH_ONLY -> MAY_BE_ARRAY_NUMERIC_HASH PACKED_ONLY -> MAY_BE_ARRAY_NUMERIC_HASH | MAY_BE_ARRAY_PACKED (== MAY_BE_ARRAY_KEY_LONG) HASH || PACKED -> MAY_BE_ARRAY_NUMERIC_HASH | MAY_BE_ARRAY_PACKED (== MAY_BE_ARRAY_KEY_LONG) 0 -> MAY_BE_ARRAY_NUMERIC_HASH ```
* PHP-8.1: Fix GH-10008: Narrowing occurred during type inference of ZEND_ADD_ARRAY_ELEMENT Fix type error on XSLTProcessor::transformToDoc return value with SimpleXML
* PHP-8.2: Fix GH-10008: Narrowing occurred during type inference of ZEND_ADD_ARRAY_ELEMENT Fix type error on XSLTProcessor::transformToDoc return value with SimpleXML
* PHP-8.3: Fix compile error with -Werror=incompatible-function-pointer-types and old libxml2 Fix GH-10008: Narrowing occurred during type inference of ZEND_ADD_ARRAY_ELEMENT Fix type error on XSLTProcessor::transformToDoc return value with SimpleXML
…D_ADD_ARRAY_ELEMENT" Although it passes CI on 8.1, it causes CI failures in the JIT on 8.2 and higher. See https://github.com/php/php-src/actions/runs/6357716718/job/17269225001 This reverts commit e72fc12.
* PHP-8.1: Revert "Fix GH-10008: Narrowing occurred during type inference of ZEND_ADD_ARRAY_ELEMENT"
* PHP-8.2: Revert "Fix GH-10008: Narrowing occurred during type inference of ZEND_ADD_ARRAY_ELEMENT"
* PHP-8.3: Revert "Fix GH-10008: Narrowing occurred during type inference of ZEND_ADD_ARRAY_ELEMENT"
Reopening because it causes JIT failures in 8.2+, it works in 8.1 though strangely |
* PHP-8.1: Fixed GH-10008: Narrowing occurred during type inference of ZEND_ADD_ARRAY_ELEMENT
* PHP-8.2: Fixed GH-10008: Narrowing occurred during type inference of ZEND_ADD_ARRAY_ELEMENT ext/intl: change when the locale is invalid for the 8.1/8.2 serie.
* PHP-8.3: Fixed GH-10008: Narrowing occurred during type inference of ZEND_ADD_ARRAY_ELEMENT ext/intl: change when the locale is invalid for the 8.1/8.2 serie.
Description
I receive the following warning when running a project of mine:
How to reproduce
The bug occurs in my open-source project Death Notifier. I tried to create a minimal working example by removing parts which I thought were not relevant for the bug, but each time that resulted in the bug not occurring. So instead, I think it's better to share the whole project.
The warning can be reproduced as follows:
README.md
are met on your system. (composer.phar
andnpm
on your path, and some PHP extensions.)npm ci
andcomposer.phar install
to install dependencies.npm run dev
to build the application into thedist/
directory.dist/api.php
in your browser. (You can also opendist/index.html
, but note that this will make requests to my server.)dist/.death-notifier.log
, where you'll find the warning in the log file.Code context
The warning mentions line 95 of
Mediawiki.php
. The full file is hosted in Gitea. Here is an excerpt with the function containing that line; I added a comment to indicate which one is line 95.PHP Version
PHP 8.1.12
Operating System
Debian testing
The text was updated successfully, but these errors were encountered: