Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions lib/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -1158,7 +1158,10 @@ private function isListDestructGroupStart(array $ops, int $index): bool
if ($this->isListSpreadAssignOp($ops[$index])) {
return !$this->isListDestructSpreadTail($ops, $index);
}
if (!$this->isPlainListDestructDimFetch($ops, $index)) {
if (
!$this->isPlainListDestructDimFetch($ops, $index)
&& !$this->isKeyedListDestructDimFetch($ops, $index)
) {
return false;
}
/** @var Op\Expr\ArrayDimFetch $cur */
Expand All @@ -1172,7 +1175,7 @@ private function isListDestructGroupStart(array $ops, int $index): bool
}
if (
$op instanceof Op\Expr\ArrayDimFetch
&& $this->isPlainListDestructDimFetch($ops, $p)
&& ($this->isPlainListDestructDimFetch($ops, $p) || $this->isKeyedListDestructDimFetch($ops, $p))
&& $op->var === $cur->var
) {
return false;
Expand Down Expand Up @@ -1202,7 +1205,8 @@ private function isListDestructSpreadTail(array $ops, int $index): bool
--$p;
}

return $p >= 0 && $this->isPlainListDestructDimFetch($ops, $p);
return $p >= 0
&& ($this->isPlainListDestructDimFetch($ops, $p) || $this->isKeyedListDestructDimFetch($ops, $p));
}

/**
Expand Down Expand Up @@ -1258,7 +1262,10 @@ private function listDestructGroupEndIndex(array $ops, int $start): int
if ($this->isListSpreadAssignOp($ops[$i])) {
return $i;
}
while ($i < count($ops) && $this->isPlainListDestructDimFetch($ops, $i)) {
while (
$i < count($ops)
&& ($this->isPlainListDestructDimFetch($ops, $i) || $this->isKeyedListDestructDimFetch($ops, $i))
) {
$i = $this->listDestructOpEndIndex($ops, $i);
}
if ($i < count($ops) && $this->isListSpreadAssignOp($ops[$i])) {
Expand Down Expand Up @@ -3675,13 +3682,15 @@ protected function compileExpr(Op\Expr $expr, Block $block): array {
}
if (null !== $expr->listSpreadRhs && null !== $expr->listSpreadFromIndex) {
$fromIndex = new Operand\Literal($expr->listSpreadFromIndex);

return [new OpCode(
$spreadOp = new OpCode(
OpCode::TYPE_LIST_SPREAD_ASSIGN,
$this->compileOperand($expr->var, $block, false),
$this->compileOperand($expr->listSpreadRhs, $block, true),
$this->compileOperand($fromIndex, $block, true),
)];
);
$spreadOp->listSpreadExcludedKeys = $expr->listSpreadExcludedKeys ?? [];

return [$spreadOp];
}
$staticPropertyFetch = $this->unwrapStaticPropertyFetch($expr->var);
if (null !== $staticPropertyFetch) {
Expand Down
3 changes: 3 additions & 0 deletions lib/OpCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ class OpCode {
/** `[$a, ...$rest] = $list` tail: arg1=dest, arg2=source array, arg3=from-index constant slot (#4835). */
const TYPE_LIST_SPREAD_ASSIGN = 124;

/** `['k' => $v, ...$tail] = $arr` string keys already assigned; empty = numeric spread only (#4889). */
public array $listSpreadExcludedKeys = [];

public int $type;
public ?int $arg1;
public ?int $arg2;
Expand Down
25 changes: 15 additions & 10 deletions lib/VM.php
Original file line number Diff line number Diff line change
Expand Up @@ -1270,18 +1270,23 @@ private function runFramesInner(): int
}
$offset = $frame->block->constants[$op->arg3]->toInt();
$ht = $src->toArray();
if (!\PHPCompiler\ext\standard\VmArray::isList($ht)) {
$catchFrame = $this->dispatchVmTypeError(
new \TypeError('Cannot unpack array with string keys'),
$frame
);
if (null !== $catchFrame) {
$frame = $catchFrame;
goto restart;
$excludedKeys = $op->listSpreadExcludedKeys;
if ([] !== $excludedKeys) {
$tail = $ht->copyListSpreadTail($offset, $excludedKeys);
} else {
if (!\PHPCompiler\ext\standard\VmArray::isList($ht)) {
$catchFrame = $this->dispatchVmTypeError(
new \TypeError('Cannot unpack array with string keys'),
$frame
);
if (null !== $catchFrame) {
$frame = $catchFrame;
goto restart;
}
break;
}
break;
$tail = $ht->sliceCopy($offset, null);
}
$tail = $ht->sliceCopy($offset, null);
$dest->array($tail);
break;
case OpCode::TYPE_ARRAY_DIM_FETCH:
Expand Down
31 changes: 31 additions & 0 deletions lib/VM/HashTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,37 @@ public function padCopy(int $length, Variable $value): HashTable
return $out;
}

/**
* List spread tail for keyed destructuring: all entries except excluded string keys and int indices below $offset (#4889).
*
* @param list<string> $excludedStringKeys
*/
public function copyListSpreadTail(int $offset, array $excludedStringKeys): self
{
$exclude = array_flip($excludedStringKeys);
$out = new self();
foreach ($this->iterateKeyed(true) as [$key, $value]) {
if (Variable::TYPE_INTEGER === $key->type) {
if ($key->toInt() < $offset) {
continue;
}
$copy = new Variable();
$copy->copyFrom($value);
$out->addIndex($key->toInt(), $copy);
} else {
$k = $key->toString();
if (isset($exclude[$k])) {
continue;
}
$copy = new Variable();
$copy->copyFrom($value);
$out->add($k, $copy);
}
}

return $out;
}

/**
* Copy a sub-range of a packed list array into a new list (non-negative offset).
*/
Expand Down
8 changes: 8 additions & 0 deletions script/apply-patches.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1872,6 +1872,8 @@ if 'listSpreadRhs' not in assign:
" public $listSpreadRhs = null;\n\n"
" /** Zero-based index of first element merged into the spread target (#4835). */\n"
" public $listSpreadFromIndex = null;\n\n"
" /** String literal keys consumed before spread (`['k' => $v, ...$tail]`, #4889). */\n"
" public $listSpreadExcludedKeys = [];\n\n"
" protected $writeVariables"
)
if needle not in assign:
Expand Down Expand Up @@ -1900,6 +1902,7 @@ old = """ $attributes = $this->mapAttributes($expr);

new = """ $attributes = $this->mapAttributes($expr);
$logicalIndex = 0;
$excludedKeys = [];
foreach ($expr->items as $i => $item) {
if (null === $item) {
continue;
Expand All @@ -1917,11 +1920,16 @@ new = """ $attributes = $this->mapAttributes($expr);
$assign = new Op\\Expr\\Assign($target, $rhs, $attributes);
$assign->listSpreadRhs = $rhs;
$assign->listSpreadFromIndex = $logicalIndex;
$assign->listSpreadExcludedKeys = $excludedKeys;
$this->block->children[] = $assign;

continue;
}

if (null !== $item->key && $item->key instanceof Node\\Scalar\\String_) {
$excludedKeys[] = $item->key->value;
}

if ($item->key === null) {
++$logicalIndex;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
list destructuring with keyed spread — ['k' => $v, ...$tail] = $assoc (#4889)
--FILE--
<?php
$src = ['label' => 'L', 'a' => 1, 'b' => 2];
['label' => $label, ...$rest] = $src;
ksort($rest);
echo $label, "\n";
echo json_encode($rest), "\n";
--EXPECT--
L
{"a":1,"b":2}