Promote write "use scalar as array" warning to Error

This commit is contained in:
Nikita Popov 2019-09-27 14:47:23 +02:00
parent 32c20d5670
commit e8b0163e0b
8 changed files with 91 additions and 79 deletions

View file

@ -86,18 +86,16 @@ PHP 8.0 UPGRADE NOTES
function test(?int $arg = CONST_RESOLVING_TO_NULL) {} function test(?int $arg = CONST_RESOLVING_TO_NULL) {}
// Or // Or
function test(int $arg = null) {} function test(int $arg = null) {}
. Attempting to write to a property of a non-object will now result in an . A number of warnings have been converted into Error exceptions:
Error exception. Previously this resulted in a warning and either converted
the value into an object (if it was null, false or an empty string) or * Attempting to write to a property of a non-object. Previously this
ignored the write altogether (in all other cases). implicitly created an stdClass object for null, false and empty strings.
RFC: Part of https://wiki.php.net/rfc/engine_warnings * Attempting to append an element to an array for which the PHP_INT_MAX key
. Attempting to append an element to an array for which the PHP_INT_MAX key is already used.
is already used will now result in an Error exception. Previously this was * Attempting to use an invalid type (array or object) as an array key or
a warning. string offset.
RFC: Part of https://wiki.php.net/rfc/engine_warnings * Attempting to write to an index of a scalar value.
. Attempting to use an invalid type (array or object) as an array key or
string offset will now result in a TypeError exception. Previously this was
a warning.
RFC: Part of https://wiki.php.net/rfc/engine_warnings RFC: Part of https://wiki.php.net/rfc/engine_warnings
- COM: - COM:

View file

@ -24,7 +24,12 @@ function test() {
} catch (Error $e) { } catch (Error $e) {
echo $e->getMessage(), "\n"; echo $e->getMessage(), "\n";
} }
try {
var_dump($true[123] = 456); var_dump($true[123] = 456);
} catch (Error $e) {
echo $e->getMessage(), "\n";
}
try { try {
var_dump($array[] += 123); var_dump($array[] += 123);
@ -43,7 +48,12 @@ function test() {
} catch (Error $e) { } catch (Error $e) {
echo $e->getMessage(), "\n"; echo $e->getMessage(), "\n";
} }
try {
var_dump($true[123] += 456); var_dump($true[123] += 456);
} catch (Error $e) {
echo $e->getMessage(), "\n";
}
try { try {
var_dump($true->foo = 123); var_dump($true->foo = 123);
@ -60,18 +70,14 @@ function test() {
test(); test();
?> ?>
--EXPECTF-- --EXPECT--
Cannot add element to the array as the next element is already occupied Cannot add element to the array as the next element is already occupied
Illegal offset type Illegal offset type
Illegal offset type Illegal offset type
Cannot use a scalar value as an array
Warning: Cannot use a scalar value as an array in %s on line %d
NULL
Cannot add element to the array as the next element is already occupied Cannot add element to the array as the next element is already occupied
Illegal offset type Illegal offset type
Illegal offset type Illegal offset type
Cannot use a scalar value as an array
Warning: Cannot use a scalar value as an array in %s on line %d
NULL
Attempt to assign property 'foo' of non-object Attempt to assign property 'foo' of non-object
Attempt to assign property 'foo' of non-object Attempt to assign property 'foo' of non-object

View file

@ -7,23 +7,35 @@ $array=array(1);
$testvalues=array(null, 0, 1, true, false,'',' ',0.1,array()); $testvalues=array(null, 0, 1, true, false,'',' ',0.1,array());
foreach ($testvalues as $testvalue) { foreach ($testvalues as $testvalue) {
try {
$testvalue['foo']=$array; $testvalue['foo']=$array;
var_dump ($testvalue); } catch (Error $e) {
echo $e->getMessage(), "\n";
}
var_dump($testvalue);
} }
echo "\n*** Indexing - Testing reference assignment with key ***\n"; echo "\n*** Indexing - Testing reference assignment with key ***\n";
$testvalues=array(null, 0, 1, true, false,0.1,array()); $testvalues=array(null, 0, 1, true, false,0.1,array());
foreach ($testvalues as $testvalue) { foreach ($testvalues as $testvalue) {
try {
$testvalue['foo']=&$array; $testvalue['foo']=&$array;
var_dump ($testvalue); } catch (Error $e) {
echo $e->getMessage(), "\n";
}
var_dump($testvalue);
} }
echo "*** Indexing - Testing value assignment no key ***\n"; echo "*** Indexing - Testing value assignment no key ***\n";
$array=array(1); $array=array(1);
$testvalues=array(null, 0, 1, true, false,0.1,array()); $testvalues=array(null, 0, 1, true, false,0.1,array());
foreach ($testvalues as $testvalue) { foreach ($testvalues as $testvalue) {
try {
$testvalue[]=$array; $testvalue[]=$array;
} catch (Error $e) {
echo $e->getMessage(), "\n";
}
var_dump ($testvalue); var_dump ($testvalue);
} }
echo "\n*** Indexing - Testing reference assignment no key ***\n"; echo "\n*** Indexing - Testing reference assignment no key ***\n";
@ -31,7 +43,11 @@ echo "\n*** Indexing - Testing reference assignment no key ***\n";
$testvalues=array(null, 0, 1, true, false,0.1,array()); $testvalues=array(null, 0, 1, true, false,0.1,array());
foreach ($testvalues as $testvalue) { foreach ($testvalues as $testvalue) {
try {
$testvalue[]=&$array; $testvalue[]=&$array;
} catch (Error $e) {
echo $e->getMessage(), "\n";
}
var_dump ($testvalue); var_dump ($testvalue);
} }
@ -47,14 +63,11 @@ array(1) {
int(1) int(1)
} }
} }
Cannot use a scalar value as an array
Warning: Cannot use a scalar value as an array in %s on line %d
int(0) int(0)
Cannot use a scalar value as an array
Warning: Cannot use a scalar value as an array in %s on line %d
int(1) int(1)
Cannot use a scalar value as an array
Warning: Cannot use a scalar value as an array in %s on line %d
bool(true) bool(true)
array(1) { array(1) {
["foo"]=> ["foo"]=>
@ -73,8 +86,7 @@ Warning: Illegal string offset 'foo' in %s on line %d
Notice: Array to string conversion in %s on line %d Notice: Array to string conversion in %s on line %d
string(1) "A" string(1) "A"
Cannot use a scalar value as an array
Warning: Cannot use a scalar value as an array in %s on line %d
float(0.1) float(0.1)
array(1) { array(1) {
["foo"]=> ["foo"]=>
@ -92,14 +104,11 @@ array(1) {
int(1) int(1)
} }
} }
Cannot use a scalar value as an array
Warning: Cannot use a scalar value as an array in %s on line %d
int(0) int(0)
Cannot use a scalar value as an array
Warning: Cannot use a scalar value as an array in %s on line %d
int(1) int(1)
Cannot use a scalar value as an array
Warning: Cannot use a scalar value as an array in %s on line %d
bool(true) bool(true)
array(1) { array(1) {
["foo"]=> ["foo"]=>
@ -108,8 +117,7 @@ array(1) {
int(1) int(1)
} }
} }
Cannot use a scalar value as an array
Warning: Cannot use a scalar value as an array in %s on line %d
float(0.1) float(0.1)
array(1) { array(1) {
["foo"]=> ["foo"]=>
@ -126,14 +134,11 @@ array(1) {
int(1) int(1)
} }
} }
Cannot use a scalar value as an array
Warning: Cannot use a scalar value as an array in %s on line %d
int(0) int(0)
Cannot use a scalar value as an array
Warning: Cannot use a scalar value as an array in %s on line %d
int(1) int(1)
Cannot use a scalar value as an array
Warning: Cannot use a scalar value as an array in %s on line %d
bool(true) bool(true)
array(1) { array(1) {
[0]=> [0]=>
@ -142,8 +147,7 @@ array(1) {
int(1) int(1)
} }
} }
Cannot use a scalar value as an array
Warning: Cannot use a scalar value as an array in %s on line %d
float(0.1) float(0.1)
array(1) { array(1) {
[0]=> [0]=>
@ -161,14 +165,11 @@ array(1) {
int(1) int(1)
} }
} }
Cannot use a scalar value as an array
Warning: Cannot use a scalar value as an array in %s on line %d
int(0) int(0)
Cannot use a scalar value as an array
Warning: Cannot use a scalar value as an array in %s on line %d
int(1) int(1)
Cannot use a scalar value as an array
Warning: Cannot use a scalar value as an array in %s on line %d
bool(true) bool(true)
array(1) { array(1) {
[0]=> [0]=>
@ -177,8 +178,7 @@ array(1) {
int(1) int(1)
} }
} }
Cannot use a scalar value as an array
Warning: Cannot use a scalar value as an array in %s on line %d
float(0.1) float(0.1)
array(1) { array(1) {
[0]=> [0]=>

View file

@ -1887,7 +1887,7 @@ static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_param_must_be_ref(con
static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_use_scalar_as_array(void) static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_use_scalar_as_array(void)
{ {
zend_error(E_WARNING, "Cannot use a scalar value as an array"); zend_throw_error(NULL, "Cannot use a scalar value as an array");
} }
static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_cannot_add_element(void) static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_cannot_add_element(void)
@ -2172,7 +2172,7 @@ return_null:
ZVAL_ERROR(result); ZVAL_ERROR(result);
} else { } else {
if (type == BP_VAR_UNSET) { if (type == BP_VAR_UNSET) {
zend_error(E_WARNING, "Cannot unset offset in a non-array variable"); zend_throw_error(NULL, "Cannot unset offset in a non-array variable");
ZVAL_NULL(result); ZVAL_NULL(result);
} else { } else {
zend_use_scalar_as_array(); zend_use_scalar_as_array();

View file

@ -931,10 +931,10 @@ static void ZEND_FASTCALL zend_jit_assign_dim_helper(zval *object_ptr, zval *dim
} }
} else { } else {
//??? if (OP1_TYPE != IS_VAR || EXPECTED(!Z_ISERROR_P(object_ptr))) { //??? if (OP1_TYPE != IS_VAR || EXPECTED(!Z_ISERROR_P(object_ptr))) {
zend_error(E_WARNING, "Cannot use a scalar value as an array"); zend_throw_error(NULL, "Cannot use a scalar value as an array");
//??? } //??? }
if (result) { if (result) {
ZVAL_NULL(result); ZVAL_UNDEF(result);
} }
} }
} }
@ -978,7 +978,7 @@ static void ZEND_FASTCALL zend_jit_assign_dim_op_helper(zval *container, zval *d
//??? ZEND_VM_C_GOTO(assign_dim_op_convert_to_array); //??? ZEND_VM_C_GOTO(assign_dim_op_convert_to_array);
} else { } else {
//??? if (UNEXPECTED(OP1_TYPE != IS_VAR || EXPECTED(!Z_ISERROR_P(container)))) { //??? if (UNEXPECTED(OP1_TYPE != IS_VAR || EXPECTED(!Z_ISERROR_P(container)))) {
zend_error(E_WARNING, "Cannot use a scalar value as an array"); zend_throw_error(NULL, "Cannot use a scalar value as an array");
//??? } //??? }
//??? if (retval) { //??? if (retval) {
//??? ZVAL_NULL(retval); //??? ZVAL_NULL(retval);

View file

@ -72,7 +72,11 @@ foo4();
function foo5() { function foo5() {
$a = 1; $a = 1;
try {
$a[2] = 1; $a[2] = 1;
} catch (Error $e) {
echo $e->getMessage(), "\n";
}
return $a; return $a;
} }
var_dump(foo5()); var_dump(foo5());
@ -129,6 +133,5 @@ array(1) {
int(1) int(1)
} }
} }
Cannot use a scalar value as an array
Warning: Cannot use a scalar value as an array in %sassign_dim_002.php on line 65
int(1) int(1)

View file

@ -3,9 +3,11 @@ Bug #29893 (segfault when using array as index)
--FILE-- --FILE--
<?php <?php
$base = 50; $base = 50;
$base[$base] -= 0; try {
$base[$base] -= 0;
} catch (Error $e) {
echo $e->getMessage(), "\n";
}
?> ?>
===DONE=== --EXPECT--
--EXPECTF-- Cannot use a scalar value as an array
Warning: Cannot use a scalar value as an array in %sbug29893.php on line %d
===DONE===

View file

@ -10,7 +10,11 @@ echo "A=$a B=$b\n";
// Warning: Cannot use a scalar value as an array in %s on line %d // Warning: Cannot use a scalar value as an array in %s on line %d
$c[$c=1] = 1; try {
$c[$c=1] = 1;
} catch (Error $e) {
echo $e->getMessage(), "\n";
}
// i++ evaluated first, so $d[0] is 10 // i++ evaluated first, so $d[0] is 10
$d = array(0,10); $d = array(0,10);
@ -90,8 +94,7 @@ print_r($ee);
?> ?>
--EXPECTF-- --EXPECTF--
A=hello B=bye A=hello B=bye
Cannot use a scalar value as an array
Warning: Cannot use a scalar value as an array in %s on line %d
array(2) { array(2) {
[0]=> [0]=>
int(10) int(10)