diff --git a/Zend/tests/array_map_foreach_optimization_gh25379.phpt b/Zend/tests/array_map_foreach_optimization_gh25379.phpt new file mode 100644 index 000000000000..9469a44d10ab --- /dev/null +++ b/Zend/tests/array_map_foreach_optimization_gh25379.phpt @@ -0,0 +1,334 @@ +--TEST-- +GH-25379: array_map() foreach optimization preserves callee evaluation, by-ref warnings, and dynamic call restrictions +--FILE-- +getMessage(), "\n"; + } + + try { + array_map(getf()(...), 5); + } catch (TypeError $e) { + echo $e->getMessage(), "\n"; + } + + try { + array_map(getc()::m(...), "a" . "b"); + } catch (TypeError $e) { + echo $e->getMessage(), "\n"; + } + + try { + array_map(getc()::{(getm())}(...), 5); + } catch (TypeError $e) { + echo $e->getMessage(), "\n"; + } + + try { + array_map(Foo::{(getm())}(...), 5); + } catch (Error $e) { + echo $e->getMessage(), "\n"; + } + + try { + array_map(getc()::{(throw_getm())}(...), 5); + } catch (RuntimeException $e) { + echo $e->getMessage(), "\n"; + } + + try { + var_dump(array_map(getf()(?, ...), [1])); + } catch (Error $e) { + echo $e->getMessage(), "\n"; + } + + try { + var_dump(array_map(getf()(?, ?), [1])); + } catch (Error $e) { + echo $e->getMessage(), "\n"; + } + + try { + var_dump(array_map(getf_named()(a: 1, ...), [2])); + } catch (Error $e) { + echo $e->getMessage(), "\n"; + } + + /* 2. By-ref callbacks emit warning and wrap instead of throwing Error */ + function inc(&$x) { + $x++; + return $x; + } + + function inc2(&$x, $n) { + $x += $n; + return $x; + } + + function var_ref(&...$a) { + return $a; + } + + class ByRef { + public static function m(&$x) { + $x++; + return $x; + } + } + + var_dump(array_map(inc(...), [1])); + var_dump(array_map(sort(...), [[3, 1]])); + var_dump(array_map(inc2(?, 2), [1])); + var_dump(array_map(var_ref(...), [1])); + var_dump(array_map(ByRef::m(...), [1])); + var_dump(array_map(inc2(n: 3, x: ?), [1])); + + class ByRefChild extends ByRef { + public static function viaStatic() { + return array_map(static::m(...), [1]); + } + public static function viaParent() { + return array_map(parent::m(...), [1]); + } + public static function viaSelf() { + return array_map(self::m(...), [1]); + } + } + + function rec(&$x) { + static $depth = 0; + if ($depth++) { + return ++$x; + } + return array_map(rec(...), [1]); + } + + var_dump(array_map(later(...), [1])); + $f = 'sort'; + var_dump(array_map($f(...), [[3, 1]])); + var_dump(ByRefChild::viaStatic()); + var_dump(ByRefChild::viaParent()); + var_dump(ByRefChild::viaSelf()); + $v = 1; + var_dump(rec($v)); + var_dump(array_map(later2(n: 3, x: ?), [1])); + + function later(&$x) { + return ++$x; + } + + function later2(&$x, $n) { + $x += $n; + return $x; + } + + /* 3. Dynamic-call-forbidden functions throw Error instead of reading caller scope */ + function test_forbid_dyn($a, $b) { + try { + array_map(extract(...), [['zz' => 1]]); + } catch (Error $e) { + echo $e->getMessage(), "\n"; + } + var_dump($zz ?? 'unset'); + + $x = 5; + try { + array_map(compact(...), ['x']); + } catch (Error $e) { + echo $e->getMessage(), "\n"; + } + try { + array_map(func_get_arg(...), [0]); + } catch (Error $e) { + echo $e->getMessage(), "\n"; + } + try { + array_map(func_get_args(...), [[]]); + } catch (Error $e) { + echo $e->getMessage(), "\n"; + } + try { + array_map(func_num_args(...), [[]]); + } catch (Error $e) { + echo $e->getMessage(), "\n"; + } + try { + array_map(get_defined_vars(...), [[]]); + } catch (Error $e) { + echo $e->getMessage(), "\n"; + } + } + test_forbid_dyn('A', 'B'); +} + +namespace NsTest { + function test_ns() { + \var_dump(\array_map(sort(...), [[3, 1]])); + try { + \array_map(extract(...), [['zz' => 1]]); + } catch (\Error $e) { + echo $e->getMessage(), "\n"; + } + } + test_ns(); +} + +?> +--EXPECTF-- +getf +array_map(): Argument #2 ($array) must be of type array, int given +getf +array_map(): Argument #2 ($array) must be of type array, int given +getc +array_map(): Argument #2 ($array) must be of type array, string given +getc +getm +array_map(): Argument #2 ($array) must be of type array, int given +getm +Class "Foo" not found +getc +boom m +getf +array(1) { + [0]=> + string(1) "1" +} +getf +Partial application of strtoupper() expects at most 1 arguments, 2 given +getf_named +array(1) { + [0]=> + int(3) +} + +Warning: inc(): Argument #1 ($x) must be passed by reference, value given in %s on line %d +array(1) { + [0]=> + int(2) +} + +Warning: sort(): Argument #1 ($array) must be passed by reference, value given in %s on line %d +array(1) { + [0]=> + bool(true) +} + +Warning: {closure:pfa:%s}(): Argument #1 ($x) must be passed by reference, value given in %s on line %d +array(1) { + [0]=> + int(3) +} + +Warning: var_ref(): Argument #1 must be passed by reference, value given in %s on line %d +array(1) { + [0]=> + array(1) { + [0]=> + int(1) + } +} + +Warning: ByRef::m(): Argument #1 ($x) must be passed by reference, value given in %s on line %d +array(1) { + [0]=> + int(2) +} + +Warning: {closure:pfa:%s}(): Argument #1 ($x) must be passed by reference, value given in %s on line %d +array(1) { + [0]=> + int(4) +} + +Warning: later(): Argument #1 ($x) must be passed by reference, value given in %s on line %d +array(1) { + [0]=> + int(2) +} + +Warning: sort(): Argument #1 ($array) must be passed by reference, value given in %s on line %d +array(1) { + [0]=> + bool(true) +} + +Warning: ByRef::m(): Argument #1 ($x) must be passed by reference, value given in %s on line %d +array(1) { + [0]=> + int(2) +} + +Warning: ByRef::m(): Argument #1 ($x) must be passed by reference, value given in %s on line %d +array(1) { + [0]=> + int(2) +} + +Warning: ByRef::m(): Argument #1 ($x) must be passed by reference, value given in %s on line %d +array(1) { + [0]=> + int(2) +} + +Warning: rec(): Argument #1 ($x) must be passed by reference, value given in %s on line %d +array(1) { + [0]=> + int(2) +} + +Warning: {closure:pfa:%s}(): Argument #1 ($x) must be passed by reference, value given in %s on line %d +array(1) { + [0]=> + int(4) +} +Cannot call extract() dynamically +string(5) "unset" +Cannot call compact() dynamically +Cannot call func_get_arg() dynamically +func_get_args() expects exactly 0 arguments, 1 given +func_num_args() expects exactly 0 arguments, 1 given +get_defined_vars() expects exactly 0 arguments, 1 given + +Warning: sort(): Argument #1 ($array) must be passed by reference, value given in %s on line %d +array(1) { + [0]=> + bool(true) +} +Cannot call extract() dynamically diff --git a/Zend/tests/array_map_foreach_optimization_gh25379_reference_elements.phpt b/Zend/tests/array_map_foreach_optimization_gh25379_reference_elements.phpt new file mode 100644 index 000000000000..60542c0d9553 --- /dev/null +++ b/Zend/tests/array_map_foreach_optimization_gh25379_reference_elements.phpt @@ -0,0 +1,160 @@ +--TEST-- +GH-25379: array_map() foreach optimization preserves by-ref callbacks with reference elements +--FILE-- +getMessage(), PHP_EOL; + } +} +test('known callee', function () { + $a = [1, 2]; + $r = &$a[0]; + $res = array_map(known(...), $a); + return [$res, $a]; +}); +test('callee declared later', function () { + $a = [1, 2]; + $r = &$a[0]; + $res = array_map(later(...), $a); + return [$res, $a]; +}); +test('static method', function () { + $a = [1, 2]; + $r = &$a[0]; + $res = array_map(ByRefMethod::change(...), $a); + return [$res, $a]; +}); +test('by-value callee', function () { + $a = [1, 2]; + $r = &$a[0]; + $res = array_map(byValue(...), $a); + return [$res, $a]; +}); +test('declared later, positional', DeclaredLater::positional(...)); +test('declared later, named', DeclaredLater::named(...)); +test('declared later, class name', DeclaredLater::byClassName(...)); +test('from trait, variadic', FromTrait::variadic(...)); +test('parent from trait', ChildOfFromTrait::viaParent(...)); +test('prebound by-ref literal on empty array', fn () => array_map(preboundRef(0, ?), [])); +test('non-static method on empty array', fn () => array_map(NonStaticMethod::inst(...), [])); +test('parent in free function', fn () => eval('function _gh25379_free() { return array_map(parent::m(...), [1]); }')); +function later(&$x) { + return ++$x; +} +?> +--EXPECTF-- +known callee: + +Warning: known(): Argument #1 ($x) must be passed by reference, value given in %s on line %d +[[2,3],[2,2]] +callee declared later: + +Warning: later(): Argument #1 ($x) must be passed by reference, value given in %s on line %d +[[2,3],[2,2]] +static method: + +Warning: ByRefMethod::change(): Argument #1 ($x) must be passed by reference, value given in %s on line %d +[[2,3],[2,2]] +by-value callee: +[[2,3],[1,2]] +declared later, positional: + +Warning: DeclaredLater::{closure:pfa:DeclaredLater::positional():%d}(): Argument #1 ($x) must be passed by reference, value given in %s on line %d +[[2,3],[2,2]] +declared later, named: + +Warning: DeclaredLater::{closure:pfa:DeclaredLater::named():%d}(): Argument #1 ($x) must be passed by reference, value given in %s on line %d +[[2,3],[2,2]] +declared later, class name: + +Warning: DeclaredLater::{closure:pfa:DeclaredLater::byClassName():%d}(): Argument #1 ($x) must be passed by reference, value given in %s on line %d +[[2,3],[2,2]] +from trait, variadic: + +Warning: FromTrait::{closure:pfa:FromTrait::variadic():%d}(): Argument #1 ($x0) must be passed by reference, value given in %s on line %d +[[2,3],[2,2]] +parent from trait: + +Warning: FromTrait::{closure:pfa:ChildOfFromTrait::viaParent():%d}(): Argument #1 ($x0) must be passed by reference, value given in %s on line %d +[[2,3],[2,2]] +prebound by-ref literal on empty array: +Error: preboundRef(): Argument #1 ($a) could not be passed by reference +non-static method on empty array: +Error: Non-static method NonStaticMethod::inst() cannot be called statically +parent in free function: + +Fatal error: Cannot use "parent" when no class scope is active in %s(%d) : eval()'d code on line 1 diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c index abed76366229..5aedda83edb2 100644 --- a/Zend/zend_compile.c +++ b/Zend/zend_compile.c @@ -105,7 +105,9 @@ static void zend_compile_expr(znode *result, zend_ast *ast); static void zend_compile_stmt(zend_ast *ast); static void zend_compile_assign(znode *result, zend_ast *ast, bool stmt, uint32_t type); +static bool zend_is_pipe_optimizable_callable_name(zend_ast *ast); static zend_ast *zend_partial_apply(zend_ast *callable_ast, zend_ast *pipe_arg); +static const zend_function *zend_get_compatible_func_or_null(const zend_class_entry *ce, zend_string *lcname); #ifdef ZEND_CHECK_STACK_LIMIT zend_never_inline static void zend_stack_limit_error(void) @@ -5330,103 +5332,191 @@ static zend_result zend_compile_func_clone(znode *result, const zend_ast_list *a return SUCCESS; } -static zend_result zend_compile_func_array_map(znode *result, zend_ast_list *args, zend_string *lcname, uint32_t lineno) /* {{{ */ +static const zend_class_entry *zend_resolve_compile_time_parent(const zend_class_entry *ce) /* {{{ */ { - /* array_map() as an internal function calls the callback as if strict_types=0, - * this optimization is therefore not legal if strict_types=1. */ - if (CG(active_op_array)->fn_flags & ZEND_ACC_STRICT_TYPES) { - return FAILURE; + if (ce->ce_flags & ZEND_ACC_LINKED) { + return ce->parent; + } + if (!ce->parent_name) { + return NULL; } - /* Bail out if we do not have exactly two parameters. */ - if (args->children != 2) { - return FAILURE; + zend_string *parent_lcname = zend_string_tolower(ce->parent_name); + const zend_class_entry *parent_ce = zend_hash_find_ptr(CG(class_table), parent_lcname); + zend_string_release_ex(parent_lcname, 0); + if (!parent_ce + || parent_ce == ce + || zend_compile_ignore_class(parent_ce, CG(active_op_array)->filename)) { + return NULL; } - /* Bail out if callback is not a first class callable */ - zend_ast *callback = args->child[0]; - if (callback->kind != ZEND_AST_CALL && callback->kind != ZEND_AST_STATIC_CALL) { - return FAILURE; + return parent_ce; +} +/* }}} */ + +/* FE_FETCH_R dereferences array elements, while array_map() preserves references + * when passing them to callbacks. Only inline a callback whose target function + * or method is statically known and takes the mapped argument by value. */ +static bool zend_array_map_callback_is_optimizable(zend_ast *callback, const zend_ast_list *fcc_args) /* {{{ */ +{ + zend_ast *name_ast = callback->child[callback->kind == ZEND_AST_CALL ? 0 : 1]; + if (name_ast->kind != ZEND_AST_ZVAL || Z_TYPE_P(zend_ast_get_zval(name_ast)) != IS_STRING) { + return false; } - zend_ast *args_ast = zend_ast_call_get_args(callback); - if (args_ast->kind != ZEND_AST_CALLABLE_CONVERT) { - return FAILURE; + const zend_function *fbc = NULL; + uint32_t fetch_type = ZEND_FETCH_CLASS_DEFAULT; + if (callback->kind == ZEND_AST_CALL) { + bool is_fully_qualified; + zend_string *resolved = zend_resolve_function_name( + zend_ast_get_str(name_ast), name_ast->attr, &is_fully_qualified); + zend_string *lcname = zend_string_tolower(resolved); + fbc = zend_hash_find_ptr(CG(function_table), lcname); + zend_string_release_ex(lcname, 0); + zend_string_release_ex(resolved, 0); + if (fbc && zend_compile_ignore_function(fbc, CG(active_op_array)->filename)) { + return false; + } + } else { + zend_ast *class_ast = callback->child[0]; + if (class_ast->kind != ZEND_AST_ZVAL + || Z_TYPE_P(zend_ast_get_zval(class_ast)) != IS_STRING) { + return false; + } + + const zend_class_entry *ce = NULL; + fetch_type = zend_get_class_fetch_type_ast(class_ast); + if (fetch_type == ZEND_FETCH_CLASS_DEFAULT) { + zend_string *class_name = zend_resolve_class_name_ast(class_ast); + zend_string *lcname = zend_string_tolower(class_name); + ce = zend_hash_find_ptr(CG(class_table), lcname); + zend_string_release_ex(lcname, 0); + if (ce) { + if (zend_compile_ignore_class(ce, CG(active_op_array)->filename)) { + ce = NULL; + } + } else if (CG(active_class_entry) + && zend_string_equals_ci(CG(active_class_entry)->name, class_name)) { + ce = CG(active_class_entry); + } + zend_string_release_ex(class_name, 0); + } else if (fetch_type == ZEND_FETCH_CLASS_SELF && CG(active_class_entry) && zend_is_scope_known()) { + ce = CG(active_class_entry); + } else if (fetch_type == ZEND_FETCH_CLASS_PARENT && CG(active_class_entry) && zend_is_scope_known()) { + ce = zend_resolve_compile_time_parent(CG(active_class_entry)); + } + + if (!ce || (ce->ce_flags & ZEND_ACC_TRAIT)) { + return false; + } + /* Only look up on ce itself: linked classes already include inherited + * and trait methods in function_table, while an unlinked class (such as + * CG(active_class_entry) or one using traits) may override a parent + * method via a trait or a declaration later in the class body. */ + zend_string *lcname = zend_string_tolower(zend_ast_get_str(name_ast)); + fbc = zend_get_compatible_func_or_null(ce, lcname); + zend_string_release_ex(lcname, 0); } - /* PFAs with non-literal pre-bound arguments are not optimizable because we - * can't memoize arguments without breaking pass-by-reference. - * TODO: Support PFAs when the function is known to not receive by ref. */ - zend_ast_fcc *fcc = (zend_ast_fcc*)args_ast; - zend_ast_list *fcc_args = zend_ast_get_list(fcc->args); + if (!fbc + || !fbc_is_finalized(fbc) + || (fbc->common.fn_flags & ZEND_ACC_ABSTRACT) + || (fbc->common.fn_flags2 & ZEND_ACC2_FORBID_DYN_CALLS)) { + return false; + } + + if (callback->kind == ZEND_AST_STATIC_CALL + && !(fbc->common.fn_flags & ZEND_ACC_STATIC) + && (!zend_is_scope_known() || !CG(active_class_entry) + || (CG(active_op_array)->fn_flags & ZEND_ACC_STATIC) + || (fetch_type != ZEND_FETCH_CLASS_SELF && fetch_type != ZEND_FETCH_CLASS_PARENT))) { + return false; + } + + bool uses_named_args = false; + bool has_placeholder = false; + uint32_t positional_count = 0; for (uint32_t i = 0; i < fcc_args->children; i++) { zend_ast *arg = fcc_args->child[i]; + uint32_t arg_num; if (arg->kind == ZEND_AST_NAMED_ARG) { + uses_named_args = true; + arg_num = zend_get_arg_num(fbc, zend_ast_get_str(arg->child[0])); + if (arg_num == (uint32_t) -1 || arg_num <= positional_count) { + return false; + } + for (uint32_t j = positional_count; j < i; j++) { + if (zend_string_equals(zend_ast_get_str(fcc_args->child[j]->child[0]), + zend_ast_get_str(arg->child[0]))) { + return false; + } + } arg = arg->child[1]; + } else { + if (uses_named_args) { + return false; + } + arg_num = ++positional_count; } if (arg->kind == ZEND_AST_PLACEHOLDER_ARG) { + if (has_placeholder) { + return false; + } + if (arg->attr == ZEND_PLACEHOLDER_VARIADIC && uses_named_args) { + return false; + } + if (ARG_SHOULD_BE_SENT_BY_REF(fbc, arg_num)) { + return false; + } + has_placeholder = true; continue; } - if (arg->kind != ZEND_AST_ZVAL) { - return FAILURE; + if (arg->kind != ZEND_AST_ZVAL || ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) { + return false; } } - /* Evaluate class name */ - znode class_node; - if (callback->kind == ZEND_AST_STATIC_CALL - && callback->child[0]->kind == ZEND_AST_ZVAL - && Z_TYPE_P(zend_ast_get_zval(callback->child[0])) == IS_STRING - && zend_get_class_fetch_type(zend_ast_get_str(callback->child[0])) == ZEND_FETCH_CLASS_DEFAULT) { - /* Resolve the name against the namespace and imports, as compiling it - * as an expression would yield the unresolved string. */ - class_node.op_type = IS_CONST; - ZVAL_STR(&class_node.u.constant, zend_resolve_class_name_ast(callback->child[0])); - } else if (callback->kind == ZEND_AST_STATIC_CALL) { - znode result; - zend_compile_expr(&result, callback->child[0]); - if (result.op_type == IS_CONST || result.op_type == IS_TMP_VAR) { - class_node = result; - } else { - class_node = result; - zend_emit_op_tmp(&class_node, ZEND_QM_ASSIGN, &result, NULL); - } - } else { - class_node.op_type = IS_UNUSED; + return has_placeholder; +} +/* }}} */ + +static zend_result zend_compile_func_array_map(znode *result, zend_ast_list *args, zend_string *lcname, uint32_t lineno) /* {{{ */ +{ + /* array_map() as an internal function calls the callback as if strict_types=0, + * this optimization is therefore not legal if strict_types=1. */ + if (CG(active_op_array)->fn_flags & ZEND_ACC_STRICT_TYPES) { + return FAILURE; } - /* Evaluate function name */ - znode func_node; - zend_ast *func_name_ast = callback->child[callback->kind == ZEND_AST_CALL ? 0 : 1]; - if (callback->kind == ZEND_AST_CALL - && func_name_ast->kind == ZEND_AST_ZVAL - && Z_TYPE_P(zend_ast_get_zval(func_name_ast)) == IS_STRING) { - /* A literal function name has no side effects and must not be memoized: - * compiling it as an expression would yield the unresolved string, which - * ignores the current namespace, "use function" imports and the runtime - * fallback to the global function. Compile the call in the loop body with - * the original name instead, like a regular call. */ - func_node.op_type = IS_UNUSED; - } else { - znode result; - zend_compile_expr(&result, func_name_ast); - if (result.op_type == IS_CONST || result.op_type == IS_TMP_VAR) { - func_node = result; - } else { - zend_emit_op_tmp(&func_node, ZEND_QM_ASSIGN, &result, NULL); - } + /* Bail out if we do not have exactly two parameters. */ + if (args->children != 2) { + return FAILURE; } - znode value; - value.op_type = IS_TMP_VAR; - value.u.op.var = get_temporary_variable(); + /* Bail out if callback is not a first class callable */ + zend_ast *callback = args->child[0]; + if (callback->kind != ZEND_AST_CALL && callback->kind != ZEND_AST_STATIC_CALL) { + return FAILURE; + } - zend_ast *call_args = zend_partial_apply(callback, - zend_ast_create_znode(&value)); - if (!call_args) { - goto fail; + zend_ast *args_ast = zend_ast_call_get_args(callback); + if (args_ast->kind != ZEND_AST_CALLABLE_CONVERT) { + return FAILURE; + } + + if (callback->kind == ZEND_AST_CALL + && !zend_is_pipe_optimizable_callable_name(callback->child[0])) { + return FAILURE; + } + + /* PFAs with non-literal pre-bound arguments are not optimizable because we + * can't memoize arguments without breaking pass-by-reference. Also require + * a statically known by-value callee before emitting any bytecode. */ + zend_ast_fcc *fcc = (zend_ast_fcc*)args_ast; + if (!zend_array_map_callback_is_optimizable(callback, zend_ast_get_list(fcc->args))) { + return FAILURE; } zend_op *opline; @@ -5437,11 +5527,19 @@ static zend_result zend_compile_func_array_map(znode *result, zend_ast_list *arg if (array.op_type == IS_CONST) { if (Z_TYPE(array.u.constant) != IS_ARRAY) { zval_ptr_dtor_nogc(&array.u.constant); - goto fail; + return FAILURE; } Z_TRY_ADDREF(array.u.constant); } + znode value; + value.op_type = IS_TMP_VAR; + value.u.op.var = get_temporary_variable(); + + zend_ast *call_args = zend_partial_apply(callback, + zend_ast_create_znode(&value)); + ZEND_ASSERT(call_args); + /* Verify that the input array actually is an array. */ znode name; name.op_type = IS_CONST; @@ -5468,39 +5566,11 @@ static zend_result zend_compile_func_array_map(znode *result, zend_ast_list *arg /* loop body */ znode call_result; - zend_ast *func_ast; - if (func_node.op_type == IS_UNUSED) { - func_ast = func_name_ast; - } else if (func_node.op_type == IS_CONST) { - func_ast = zend_ast_create_znode(&func_node); - } else { - znode copy_node; - zend_emit_op_tmp(©_node, ZEND_COPY_TMP, &func_node, NULL); - func_ast = zend_ast_create_znode(©_node); - } - switch (callback->kind) { - case ZEND_AST_CALL: { - zend_compile_expr(&call_result, zend_ast_create(ZEND_AST_CALL, func_ast, call_args)); - break; - } - case ZEND_AST_STATIC_CALL: { - zend_ast *class_ast; - if (class_node.op_type == IS_CONST) { - class_ast = zend_ast_create_znode(&class_node); - } else { - znode copy_node; - zend_emit_op_tmp(©_node, ZEND_COPY_TMP, &class_node, NULL); - class_ast = zend_ast_create_znode(©_node); - } - - zend_compile_expr(&call_result, zend_ast_create(ZEND_AST_STATIC_CALL, class_ast, func_ast, call_args)); - zend_ast_destroy(class_ast); - break; - } - } - if (func_ast != func_name_ast) { - zend_ast_destroy(func_ast); - } + zend_ast *call_ast = callback->kind == ZEND_AST_CALL + ? zend_ast_create(ZEND_AST_CALL, callback->child[0], call_args) + : zend_ast_create(ZEND_AST_STATIC_CALL, callback->child[0], callback->child[1], call_args); + call_ast->lineno = callback->lineno; + zend_compile_expr(&call_result, call_ast); opline = zend_emit_op(NULL, ZEND_ADD_ARRAY_ELEMENT, &call_result, &key); SET_NODE(opline->result, result); /* end loop body */ @@ -5515,25 +5585,8 @@ static zend_result zend_compile_func_array_map(znode *result, zend_ast_list *arg zend_end_loop(opnum_fetch, &reset_node); zend_emit_op(NULL, ZEND_FE_FREE, &reset_node, NULL); - if (func_node.op_type != IS_UNUSED && func_node.op_type != IS_CONST) { - zend_emit_op(NULL, ZEND_FREE, &func_node, NULL); - } - if (class_node.op_type != IS_UNUSED && class_node.op_type != IS_CONST) { - zend_emit_op(NULL, ZEND_FREE, &class_node, NULL); - } return SUCCESS; - -fail: - CG(active_op_array)->T--; - if (func_node.op_type == IS_CONST) { - zval_ptr_dtor_nogc(&func_node.u.constant); - } - if (class_node.op_type == IS_CONST) { - zval_ptr_dtor_nogc(&class_node.u.constant); - } - - return FAILURE; } static zend_result zend_try_compile_special_func_ex(znode *result, zend_string *lcname, zend_ast_list *args, uint32_t type, uint32_t lineno) /* {{{ */ diff --git a/ext/opcache/tests/array_map_foreach_optimization_006.phpt b/ext/opcache/tests/array_map_foreach_optimization_006.phpt index 4c557ce6802c..bb99b875e8f0 100644 --- a/ext/opcache/tests/array_map_foreach_optimization_006.phpt +++ b/ext/opcache/tests/array_map_foreach_optimization_006.phpt @@ -1,11 +1,10 @@ --TEST-- -array_map(): foreach optimization - dynamic name +array_map(): dynamic callable falls back to native semantics --EXTENSIONS-- opcache --INI-- opcache.enable=1 opcache.enable_cli=1 -opcache.opt_debug_level=0x20000 --FILE-- ---EXPECTF-- -$_main: - ; (lines=%d, args=0, vars=%d, tmps=%d) - ; (after optimizer) - ; %s -0000 INIT_FCALL 2 %d string("range") -0001 SEND_VAL int(1) 1 -0002 SEND_VAL int(10) 2 -0003 T3 = DO_ICALL -0004 ASSIGN CV0($array) T3 -0005 ASSIGN CV1($plus1) string("plus1") -0006 T4 = QM_ASSIGN CV1($plus1) -0007 TYPE_ASSERT 131079 string("array_map") CV0($array) -0008 T3 = INIT_ARRAY 0 (packed) NEXT -0009 V5 = FE_RESET_R CV0($array) 0017 -0010 T7 = FE_FETCH_R V5 T6 0017 -0011 T8 = COPY_TMP T4 -0012 INIT_DYNAMIC_CALL 1 T8 -0013 SEND_VAL_EX T6 1 -0014 T6 = DO_FCALL -0015 T3 = ADD_ARRAY_ELEMENT T6 T7 -0016 JMP 0010 -0017 FE_FREE V5 -0018 FREE T4 -0019 ASSIGN CV2($foo) T3 -0020 INIT_FCALL 1 %d string("var_dump") -0021 SEND_VAR CV2($foo) 1 -0022 DO_ICALL -0023 RETURN int(1) -LIVE RANGES: - 4: 0007 - 0018 (tmp/var) - 3: 0009 - 0019 (tmp/var) - 5: 0010 - 0017 (loop) - 6: 0011 - 0013 (tmp/var) - 7: 0011 - 0015 (tmp/var) - -plus1: - ; (lines=3, args=1, vars=1, tmps=%d) - ; (after optimizer) - ; %s -0000 CV0($x) = RECV 1 -0001 T1 = ADD CV0($x) int(1) -0002 RETURN T1 +--EXPECT-- array(10) { [0]=> int(2) diff --git a/ext/opcache/tests/array_map_foreach_optimization_010.phpt b/ext/opcache/tests/array_map_foreach_optimization_010.phpt index 758c3504a88f..4490b29beead 100644 --- a/ext/opcache/tests/array_map_foreach_optimization_010.phpt +++ b/ext/opcache/tests/array_map_foreach_optimization_010.phpt @@ -82,8 +82,7 @@ array(1) { string(15) "trusted:payload" } empty direct -array(0) { -} +Error: Call to undefined function cand_86013_missing() empty dynamic-call control Error: Call to undefined function cand_86013_missing() direct array_map static call diff --git a/ext/opcache/tests/array_map_foreach_optimization_012.phpt b/ext/opcache/tests/array_map_foreach_optimization_012.phpt index 04b16d2ab6dd..51a5c213dcf5 100644 --- a/ext/opcache/tests/array_map_foreach_optimization_012.phpt +++ b/ext/opcache/tests/array_map_foreach_optimization_012.phpt @@ -1,11 +1,10 @@ --TEST-- -array_map(): foreach optimization - function argument is evaluated once +array_map(): dynamic callee evaluated once on native fallback --EXTENSIONS-- opcache --INI-- opcache.enable=1 opcache.enable_cli=1 -opcache.opt_debug_level=0x20000 --FILE-- ---EXPECTF-- -$_main: - ; (lines=%d, args=0, vars=%d, tmps=%d) - ; (after optimizer) - ; %s -0000 INIT_FCALL 2 %d string("range") -0001 SEND_VAL int(1) 1 -0002 SEND_VAL int(2) 2 -0003 T2 = DO_ICALL -0004 ASSIGN CV0($array) T2 -0005 INIT_FCALL 1 %d string("var_dump") -0006 INIT_FCALL 0 %d string("get_function") -0007 T3 = DO_UCALL -0008 TYPE_ASSERT 131079 string("array_map") CV0($array) -0009 T2 = INIT_ARRAY 0 (packed) NEXT -0010 V4 = FE_RESET_R CV0($array) 0018 -0011 T8 = FE_FETCH_R V4 T7 0018 -0012 T9 = COPY_TMP T3 -0013 INIT_DYNAMIC_CALL 1 T9 -0014 SEND_VAL_EX T7 1 -0015 T7 = DO_FCALL -0016 T2 = ADD_ARRAY_ELEMENT T7 T8 -0017 JMP 0011 -0018 FE_FREE V4 -0019 FREE T3 -0020 SEND_VAL T2 1 -0021 DO_ICALL -0022 INIT_FCALL 1 %d string("var_dump") -0023 T3 = NEW 0 string("C") -0024 DO_FCALL -0025 TYPE_ASSERT 131079 string("array_map") CV0($array) -0026 T2 = INIT_ARRAY 0 (packed) NEXT -0027 V4 = FE_RESET_R CV0($array) 0036 -0028 T7 = FE_FETCH_R V4 T6 0036 -0029 T9 = COPY_TMP T3 -0030 V8 = FETCH_CLASS (exception) T9 -0031 INIT_STATIC_METHOD_CALL 1 V8 string("f") -0032 SEND_VAL_EX T6 1 -0033 T6 = DO_FCALL -0034 T2 = ADD_ARRAY_ELEMENT T6 T7 -0035 JMP 0028 -0036 FE_FREE V4 -0037 FREE T3 -0038 SEND_VAL T2 1 -0039 DO_ICALL -0040 T2 = DECLARE_LAMBDA_FUNCTION 4294967295 0 -0041 BIND_LEXICAL (ref) T2 CV1($f) -0042 ASSIGN CV1($f) T2 -0043 INIT_FCALL 1 %d string("var_dump") -0044 T3 = QM_ASSIGN CV1($f) -0045 TYPE_ASSERT 131079 string("array_map") CV0($array) -0046 T2 = INIT_ARRAY 0 (packed) NEXT -0047 V4 = FE_RESET_R CV0($array) 0055 -0048 T6 = FE_FETCH_R V4 T5 0055 -0049 T7 = COPY_TMP T3 -0050 INIT_DYNAMIC_CALL 1 T7 -0051 SEND_VAL_EX T5 1 -0052 T5 = DO_FCALL -0053 T2 = ADD_ARRAY_ELEMENT T5 T6 -0054 JMP 0048 -0055 FE_FREE V4 -0056 FREE T3 -0057 SEND_VAL T2 1 -0058 DO_ICALL -0059 RETURN int(1) -LIVE RANGES: - 3: 0008 - 0019 (tmp/var) - 2: 0010 - 0020 (tmp/var) - 4: 0011 - 0018 (loop) - 7: 0012 - 0014 (tmp/var) - 8: 0012 - 0016 (tmp/var) - 3: 0024 - 0025 (new) - 3: 0025 - 0037 (tmp/var) - 2: 0027 - 0038 (tmp/var) - 4: 0028 - 0036 (loop) - 6: 0029 - 0032 (tmp/var) - 7: 0029 - 0034 (tmp/var) - 2: 0041 - 0042 (tmp/var) - 3: 0045 - 0056 (tmp/var) - 2: 0047 - 0057 (tmp/var) - 4: 0048 - 0055 (loop) - 5: 0049 - 0051 (tmp/var) - 6: 0049 - 0053 (tmp/var) - -{closure:%s:%d}: - ; (lines=5, args=1, vars=2, tmps=%d) - ; (after optimizer) - ; %s.php:%s -0000 CV0($value) = RECV 1 -0001 BIND_STATIC (ref) CV1($f) -0002 ASSIGN CV1($f) string("dechex") -0003 T2 = ADD CV0($value) int(1) -0004 RETURN T2 - -plusn: - ; (lines=4, args=2, vars=2, tmps=%d) - ; (after optimizer) - ; %s -0000 CV0($x) = RECV 1 -0001 CV1($n) = RECV 2 -0002 T2 = ADD CV0($x) CV1($n) -0003 RETURN T2 - -get_function: - ; (lines=7, args=0, vars=0, tmps=%d) - ; (after optimizer) - ; %s.php:%s -0000 INIT_FCALL 1 %d string("var_dump") -0001 SEND_VAL string("get_function") 1 -0002 DO_ICALL -0003 V1 = DECLARE_ANON_CLASS string("class@anonymous%s") -0004 T0 = NEW 0 V1 -0005 DO_FCALL -0006 RETURN T0 -LIVE RANGES: - 0: 0005 - 0006 (new) - -class@anonymous::__invoke: - ; (lines=3, args=1, vars=1, tmps=%d) - ; (after optimizer) - ; %s.php:%s -0000 CV0($value) = RECV 1 -0001 T1 = ADD CV0($value) int(1) -0002 RETURN T1 - -C::__construct: - ; (lines=4, args=0, vars=0, tmps=%d) - ; (after optimizer) - ; %s.php:%s -0000 INIT_FCALL 1 %d string("var_dump") -0001 SEND_VAL string("C::__construct") 1 -0002 DO_ICALL -0003 RETURN null - -C::f: - ; (lines=3, args=1, vars=1, tmps=%d) - ; (after optimizer) - ; %s.php:%s -0000 CV0($value) = RECV 1 -0001 T1 = ADD CV0($value) int(1) -0002 RETURN T1 +--EXPECT-- string(12) "get_function" array(2) { [0]=> diff --git a/ext/opcache/tests/array_map_foreach_optimization_014.phpt b/ext/opcache/tests/array_map_foreach_optimization_014.phpt index c6c4afc216ae..f1c9a0569526 100644 --- a/ext/opcache/tests/array_map_foreach_optimization_014.phpt +++ b/ext/opcache/tests/array_map_foreach_optimization_014.phpt @@ -1,11 +1,10 @@ --TEST-- -array_map(): foreach optimization - PFAs with literal args are optimizable +array_map(): unresolved PFA callback uses native fallback --EXTENSIONS-- opcache --INI-- opcache.enable=1 opcache.enable_cli=1 -opcache.opt_debug_level=0x20000 --ENV-- A=1 --FILE-- @@ -20,48 +19,7 @@ if (getenv('A')) { var_dump(array_map(plusn(?, 2), range(1, 2))); ?> ---EXPECTF-- -$_main: - ; (lines=%d, args=0, vars=%d, tmps=%d) - ; (after optimizer) - ; %s -0000 INIT_FCALL 1 %d string("getenv") -0001 SEND_VAL string("A") 1 -0002 T0 = DO_ICALL -0003 JMPZ T0 0005 -0004 DECLARE_FUNCTION string("plusn") 0 -0005 INIT_FCALL 1 %d string("var_dump") -0006 INIT_FCALL 2 %d string("range") -0007 SEND_VAL int(1) 1 -0008 SEND_VAL int(2) 2 -0009 T3 = DO_ICALL -0010 TYPE_ASSERT 131079 string("array_map") T3 -0011 T0 = INIT_ARRAY 0 (packed) NEXT -0012 V1 = FE_RESET_R T3 0020 -0013 T3 = FE_FETCH_R V1 T2 0020 -0014 INIT_FCALL_BY_NAME 2 string("plusn") -0015 SEND_VAL_EX T2 1 -0016 SEND_VAL_EX int(2) 2 -0017 T2 = DO_FCALL_BY_NAME -0018 T0 = ADD_ARRAY_ELEMENT T2 T3 -0019 JMP 0013 -0020 FE_FREE V1 -0021 SEND_VAL T0 1 -0022 DO_ICALL -0023 RETURN int(1) -LIVE RANGES: - 3: 0010 - 0012 (tmp/var) - 0: 0012 - 0021 (tmp/var) - 1: 0013 - 0020 (loop) - -plusn: - ; (lines=4, args=2, vars=2, tmps=%d) - ; (after optimizer) - ; %s -0000 CV0($x) = RECV 1 -0001 CV1($n) = RECV 2 -0002 T2 = ADD CV0($x) CV1($n) -0003 RETURN T2 +--EXPECT-- array(2) { [0]=> int(3)