Possible regression with spreading nested arrays into a function call and references
Nobody has claimed this yet.
- Dominant language
- C
- Stars
- 40.4k
- Forks
- 8.1k
- Avg merge
- 2d 13h
- Merged PRs (30d)
- 96
Description
Description
I've noticed the following possible regression:
$args = [1];
$ref = [&$args];
function test(&$v) {
$v = 7;
};
test(...$ref[0]);
var_dump($args[0]);
Starting with PHP 7.3, the output of the above code has changed:
$ docker run --rm php:7.2.34 -r "$args = [1]; $ref = [&$args]; function test(&$v) { $v = 7; }; test(...$ref[0]); var_dump($args[0]);"
int(7)
$ docker run --rm php:7.3 -r "$args = [1]; $ref = [&$args]; function test(&$v) { $v = 7; }; test(...$ref[0]); var_dump($args[0]);"
int(1)
$ docker run --rm php:8.3.7 -r "$args = [1]; $ref = [&$args]; function test(&$v) { $v = 7; }; test(...$ref[0]); var_dump($args[0]);"
int(1)
The only entry I've found within PHP 7.3's release notes that might be related is References returned by Array and Property Accesses are immediately unwrapped. But I'm not sure whether this does actually describe the behavior change in the code snippet above.
I'm also not really sure whether the code is valid or not. In case it is not, it might be helpful to display a message.
There exists a workaround to get the old result by introducing another variable:
$temp = &$ref[0];
test(...$temp);
PHP Version
PHP 7.3.0 - PHP 8.3.7
Operating System
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the nested-array spread and reference example on PHP 7.2, 7.3, and 8.3, then compare it with the PHP 7.3 reference-unwrapping migration note. Determine whether the changed result is valid; done means documenting the expected semantics and adding a correction or diagnostic if the behavior is erroneous.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100