Bug #80066 [NEW]: List Reference Assignment: Cannot reference an array value by return

From: Date: Sun, 06 Sep 2020 19:40:24 +0000
Subject: Bug #80066 [NEW]: List Reference Assignment: Cannot reference an array value by return
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-228900@lists.php.net to get a copy of this message
From:             php-0919 at jan-runte dot de
Operating system: macOS 10.15.6
PHP version:      7.3.22
Package:          Variables related
Bug Type:         Bug
Bug description:List Reference Assignment: Cannot reference an array value by return

Description:
------------
Feature 7.3 - List Reference Assignment:
- https://wiki.php.net/rfc/list_reference_assignment

When attempting to transfer an array return value via the list reference
assignment to certain keys with by-reference, the error "Attempting to
set reference to non referenceable value" occurs.

But the belonging value of that key is a referenceable value - it is an
array.

7.3.13: failed
7.3.22: failed
7.4.3: failed
7.4.10: failed
8.0.0beta2: failed
8.0.0beta3: failed

Test script:
---------------
function returnsArrayWithReferencedValue(array &$subject) : array {
    return [
        'a' => &$subject,
	'b' => 123,
    ];
}


$source = [
    'c' => [],
];

/*****************
/* PREFERRED WAY *
 *****************/

['a' => &$referencedArray] = returnsArrayWithReferencedValue($source);

// Throws an error: "Attempting to set reference to non referenceable
value in ..."


/**************
/* WORKAROUND *
 **************/

// First set returned array to a variable
$returnedArray = returnsArrayWithReferencedValue($source);

$source['d'] = 456; // manipulate source array
var_dump($returnedArray['a']);


/**********
/* MOCKUP *
 **********/

// Fake returned array as a prepared variable
$fakeReturnedArray = [
    'a' => &$source,
    'b' => 123,	
];

['a' => &$referencedArray] = $fakeReturnedArray;
$source['d'] = 456; // manipulate source array
var_dump($referencedArray);

Expected result:
----------------
array(2) {
  ["c"]=>
  array(0) {
  }
  ["d"]=>
  int(456)
}

Actual result:
--------------
Throws an error: Attempting to set reference to non referenceable value

-- 
Edit bug report at https://bugs.php.net/bug.php?id=80066&edit=1
-- 
Fix committed:                    https://bugs.php.net/fix.php?id=80066&r=fixed
Fixed in release:                 https://bugs.php.net/fix.php?id=80066&r=alreadyfixed
Need backtrace:                   https://bugs.php.net/fix.php?id=80066&r=needtrace
Need Reproduce Script:            https://bugs.php.net/fix.php?id=80066&r=needscript
Try newer version:                https://bugs.php.net/fix.php?id=80066&r=oldversion
Not developer issue:              https://bugs.php.net/fix.php?id=80066&r=support
Expected behavior:                https://bugs.php.net/fix.php?id=80066&r=notwrong
Not enough info:                  https://bugs.php.net/fix.php?id=80066&r=notenoughinfo
Submitted twice:                  https://bugs.php.net/fix.php?id=80066&r=submittedtwice
register_globals:                 https://bugs.php.net/fix.php?id=80066&r=globals
PHP version support discontinued: https://bugs.php.net/fix.php?id=80066&r=phptooold
Daylight Savings:                 https://bugs.php.net/fix.php?id=80066&r=dst
IIS Stability:                    https://bugs.php.net/fix.php?id=80066&r=isapi
Install GNU Sed:                  https://bugs.php.net/fix.php?id=80066&r=gnused
Floating point limitations:       https://bugs.php.net/fix.php?id=80066&r=float
No Zend Extensions:               https://bugs.php.net/fix.php?id=80066&r=nozend
MySQL Configuration Error:        https://bugs.php.net/fix.php?id=80066&r=mysqlcfg


Thread (3 messages)

« previous php.bugs (#228900) next »