Skip to content

Fix array_map optimization with non-literal function or non-literal args - #23254

Draft
arnaud-lb wants to merge 1 commit into
php:masterfrom
arnaud-lb:pfa-bug-3
Draft

Fix array_map optimization with non-literal function or non-literal args#23254
arnaud-lb wants to merge 1 commit into
php:masterfrom
arnaud-lb:pfa-bug-3

Conversation

@arnaud-lb

Copy link
Copy Markdown
Member

Non-literal expressions must be evaluated once and memoized to maintain semantics:

$callback = function ($value) {
    global $callback;
    $callback = function () { return 'changed'; };
    return $value + 1;
};

array_map($callback, [1,2]);

// Expected result: [2,3]
// Actual result: [2,'changed']

Unfortunately, we can't memoize pre-bound PFA arguments without breaking pass-by-reference, so we disable the optimization if the callback is a PFA with non-literal arguments. We could improve this when the function is known and we can determine that the argument is not passed by ref.

Bug found by Ryan @ Calif.io.

Non-literal expressions must be evaluated once and memoized to maintain
semantics.
Comment thread Zend/zend_compile.c
return FAILURE;
}

/* Bail out if callback is not an FCC */

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment might be confused between "First Class Callable" and "fcall_info_cache" which we usually refer as FCCs too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants