Skip to content

Commit 635b9d2

Browse files
committed
zend_compile: Fix handling of constant arrays for array_map()
1 parent 50645e4 commit 635b9d2

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

Zend/zend_compile.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5073,6 +5073,10 @@ static zend_result zend_compile_func_array_map(znode *result, zend_ast_list *arg
50735073

50745074
znode array;
50755075
zend_compile_expr(&array, args->child[1]);
5076+
/* array is an argument to both ZEND_TYPE_ASSERT and to ZEND_FE_RESET_R. */
5077+
if (array.op_type == IS_CONST) {
5078+
Z_TRY_ADDREF(array.u.constant);
5079+
}
50765080

50775081
/* Verify that the input array actually is an array. */
50785082
znode name;
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
--TEST--
2+
array_map(): foreach optimization - const array
3+
--EXTENSIONS--
4+
opcache
5+
--INI--
6+
opcache.enable=1
7+
opcache.enable_cli=1
8+
opcache.opt_debug_level=0x20000
9+
--FILE--
10+
<?php
11+
12+
function plus1($x) {
13+
return $x + 1;
14+
}
15+
16+
$foo = array_map(plus1(...), [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
17+
18+
var_dump($foo);
19+
20+
?>
21+
--EXPECTF--
22+
$_main:
23+
; (lines=%d, args=0, vars=%d, tmps=%d)
24+
; (after optimizer)
25+
; %s
26+
0000 TYPE_ASSERT 131079 string("array_map") array(...)
27+
0001 V1 = INIT_ARRAY 0 (packed) NEXT
28+
0002 V2 = FE_RESET_R array(...) 0009
29+
0003 T4 = FE_FETCH_R V2 T3 0009
30+
0004 INIT_FCALL 1 %d string("plus1")
31+
0005 SEND_VAL T3 1
32+
0006 V3 = DO_UCALL
33+
0007 V1 = ADD_ARRAY_ELEMENT V3 T4
34+
0008 JMP 0003
35+
0009 FE_FREE V2
36+
0010 ASSIGN CV0($foo) V1
37+
0011 INIT_FCALL 1 %d string("var_dump")
38+
0012 SEND_VAR CV0($foo) 1
39+
0013 DO_ICALL
40+
0014 RETURN int(1)
41+
LIVE RANGES:
42+
1: 0002 - 0010 (tmp/var)
43+
2: 0003 - 0009 (loop)
44+
45+
plus1:
46+
; (lines=3, args=1, vars=1, tmps=1)
47+
; (after optimizer)
48+
; %s
49+
0000 CV0($x) = RECV 1
50+
0001 T1 = ADD CV0($x) int(1)
51+
0002 RETURN T1
52+
array(10) {
53+
[0]=>
54+
int(2)
55+
[1]=>
56+
int(3)
57+
[2]=>
58+
int(4)
59+
[3]=>
60+
int(5)
61+
[4]=>
62+
int(6)
63+
[5]=>
64+
int(7)
65+
[6]=>
66+
int(8)
67+
[7]=>
68+
int(9)
69+
[8]=>
70+
int(10)
71+
[9]=>
72+
int(11)
73+
}

0 commit comments

Comments
 (0)