[php-src] PR #25546: Fix GH-25379: Preserve array_map() semantics in foreach optimization
| From: | prateekbhujel | Date: | Wed, 18 Nov 2026 04:15:36 +0000 |
| Subject: | [php-src] PR #25546: Fix GH-25379: Preserve array_map() semantics in foreach optimization | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-40763@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/25546
Author: prateekbhujel
Fixes the compiler regressions reported in #25379 inside
zend_compile_func_array_map():
1. **Single evaluation and no TMP leak on bailout:**
- Move zend_partial_apply() / zend_is_pipe_optimizable_callable_name()
checks (e.g. assert, multiple placeholders, named args before a variadic placeholder)
before compiling class_node and func_node.
- Defer allocating the loop value temporary until after checking whether the compiled
array argument is an IS_CONST non-array. When class_node or
func_node already emitted runtime opcodes into IS_TMP_VAR, fall back in
place to a regular array_map() call reusing those already-evaluated nodes instead of
emitting orphaned opcodes/TMPs and compiling the callee twice.
2. **By-reference callbacks:**
- Bail out when the statically known target function or method has
ARG_MUST_BE_SENT_BY_REF for the placeholder parameter (including variadic by-ref
parameters and named placeholders), so calls like inc(...), sort(...), and
inc2(?, 2) continue to emit a warning and wrap the value instead of throwing
Error: Argument could not be passed by reference.
3. **Functions forbidding dynamic calls (ZEND_ACC2_FORBID_DYN_CALLS):**
- Bail out when the resolved function or static method carries
ZEND_ACC2_FORBID_DYN_CALLS (such as extract, compact,
func_get_arg, func_get_args, func_num_args, and
get_defined_vars, including unqualified calls in namespaces that fall back to global
internal functions), preserving Cannot call X() dynamically instead of running in the
caller scope.
Note on #25379 part 3 (late/empty-array callback validation such as nope(...) on
[]): intentionally left untouched here, as pre-validating unknown callbacks before the
loop would break the closure-elision opcodes tested by
array_map_foreach_optimization_006.phpt / 012.phpt, and bailing out on
unknown classes/functions at compile time would disable the optimization for autoloaded static
methods and functions declared later in a file.
Refs GH-25379.
Local debug build checks:
- Zend/tests/array_map_foreach_optimization_gh25379.phpt passes with both
opcache.enable_cli=0 and opcache.enable_cli=1 (0 memory leaks).
- ext/opcache/tests/ and ext/standard/tests/array/: 1,845 passed, 0 failed
(58 skipped, 2 expected fail).
- Zend/tests/: 5,562 passed, 178 skipped, 1 expected fail (only
Zend/tests/new_oom.phpt failed locally due to an unquoted space in the local checkout
path).