Skip to content

Commit 2a753b9

Browse files
committed
Throw Error in AST
1 parent e98d0d4 commit 2a753b9

File tree

5 files changed

+34
-32
lines changed

5 files changed

+34
-32
lines changed

Zend/tests/array_literal_next_element_error.phpt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ try {
1111
echo $e->getMessage() . "\n";
1212
}
1313

14-
try {
15-
const FOO = [PHP_INT_MAX => 42, "foo"];
16-
var_dump(FOO);
17-
} catch (\Error $e) {
18-
echo $e->getMessage() . "\n";
19-
}
14+
const FOO = [PHP_INT_MAX => 42, "foo"];
15+
var_dump(FOO);
2016

2117
?>
22-
--EXPECT--
23-
Cannot add element to the array as the next element is already occupied
18+
--EXPECTF--
2419
Cannot add element to the array as the next element is already occupied
2520

21+
Fatal error: Uncaught Error: Cannot add element to the array as the next element is already occupied in %s:11
22+
Stack trace:
23+
#0 {main}
24+
thrown in %s on line 11
25+

Zend/tests/array_unpack/already_occupied.phpt

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,26 @@ Appending to an array via unpack may fail
66
<?php
77

88
$arr = [1, 2, 3];
9-
var_dump([PHP_INT_MAX-1 => 0, ...$arr]);
9+
try {
10+
var_dump([PHP_INT_MAX-1 => 0, ...$arr]);
11+
} catch (\Error $e) {
12+
echo $e->getMessage() . "\n";
13+
}
1014

11-
var_dump([PHP_INT_MAX-1 => 0, ...[1, 2, 3]]);
15+
try {
16+
var_dump([PHP_INT_MAX-1 => 0, ...[1, 2, 3]]);
17+
} catch (\Error $e) {
18+
echo $e->getMessage() . "\n";
19+
}
1220

1321
const ARR = [1, 2, 3];
1422
const ARR2 = [PHP_INT_MAX-1 => 0, ...ARR];
1523
var_dump(ARR2);
1624

1725
?>
1826
--EXPECTF--
19-
Warning: Cannot add element to the array as the next element is already occupied in %s on line %d
20-
array(2) {
21-
[9223372036854775806]=>
22-
int(0)
23-
[9223372036854775807]=>
24-
int(1)
25-
}
26-
27-
Warning: Cannot add element to the array as the next element is already occupied in %s on line %d
28-
array(2) {
29-
[9223372036854775806]=>
30-
int(0)
31-
[9223372036854775807]=>
32-
int(1)
33-
}
27+
Cannot add element to the array as the next element is already occupied
28+
Cannot add element to the array as the next element is already occupied
3429

3530
Warning: Cannot add element to the array as the next element is already occupied in %s on line %d
3631
array(2) {

Zend/tests/assign_dim_obj_null_return.phpt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ function test() {
77
$array = [PHP_INT_MAX => 42];
88
$true = true;
99

10-
var_dump($array[] = 123);
10+
try {
11+
var_dump($array[] = 123);
12+
} catch (\Error $e) {
13+
echo $e->getMessage() . "\n";
14+
}
1115
var_dump($array[[]] = 123);
1216
var_dump($array[new stdClass] = 123);
1317
var_dump($true[123] = 456);
@@ -29,8 +33,7 @@ test();
2933

3034
?>
3135
--EXPECTF--
32-
Warning: Cannot add element to the array as the next element is already occupied in %s on line %d
33-
NULL
36+
Cannot add element to the array as the next element is already occupied
3437

3538
Warning: Illegal offset type in %s on line %d
3639
NULL

Zend/tests/bug69017.phpt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,19 @@ class c1
1616

1717
c1::$a1[] = 1;
1818
c1::$a2[] = 1;
19-
c1::$a3[] = 1;
19+
20+
try {
21+
c1::$a3[] = 1;
22+
} catch (\Error $e) {
23+
echo $e->getMessage() . "\n";
24+
}
2025

2126
var_dump(c1::$a1);
2227
var_dump(c1::$a2);
2328
var_dump(c1::$a3);
2429
?>
2530
--EXPECTF--
26-
Warning: Cannot add element to the array as the next element is already occupied in %sbug69017.php on line %d
31+
Cannot add element to the array as the next element is already occupied
2732
array(2) {
2833
[1]=>
2934
string(3) "one"

Zend/zend_ast.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,8 +411,7 @@ static int zend_ast_add_array_element(zval *result, zval *offset, zval *expr)
411411
switch (Z_TYPE_P(offset)) {
412412
case IS_UNDEF:
413413
if (!zend_hash_next_index_insert(Z_ARRVAL_P(result), expr)) {
414-
zend_error(E_WARNING,
415-
"Cannot add element to the array as the next element is already occupied");
414+
zend_throw_error(NULL, "Cannot add element to the array as the next element is already occupied");
416415
zval_ptr_dtor_nogc(expr);
417416
}
418417
break;

0 commit comments

Comments
 (0)