Release destructuring temporaries after assignment

This commit is contained in:
Fabien Potencier
2026-08-27 13:09:56 +02:00
parent 9a8a76c86d
commit c459ef0bdd
4 changed files with 81 additions and 4 deletions
@@ -54,7 +54,7 @@ class ObjectDestructuringSetBinary extends AbstractBinary
{
$compiler->addDebugInfo($this);
$var = '$'.$compiler->getVarName();
$compiler->raw('[');
$compiler->raw('[[');
foreach ($this->mappings as $i => $mapping) {
if ($i) {
$compiler->raw(', ');
@@ -74,7 +74,7 @@ class ObjectDestructuringSetBinary extends AbstractBinary
}
$compiler->raw(', ')->repr($mapping['property'])->raw(', [], \\Twig\\Template::ANY_CALL, false, false, ')->repr($compiler->getEnvironment()->hasExtension(SandboxExtension::class))->raw(', ')->repr($this->getNode('right')->getTemplateLine())->raw(')');
}
$compiler->raw(']');
$compiler->raw('], '.$var.' = null][0]');
}
public function operator(Compiler $compiler): Compiler
@@ -50,7 +50,7 @@ class SequenceDestructuringSetBinary extends AbstractBinary
$compiler->addDebugInfo($this);
$var = '$'.$compiler->getVarName();
$compiler
->raw('(('.$var.' = ')
->raw('[(('.$var.' = ')
->subcompile($this->getNode('right'))
->raw(') instanceof \Traversable ? CoreExtension::destructureSequence($context, ')
->repr($this->variables)
@@ -67,7 +67,7 @@ class SequenceDestructuringSetBinary extends AbstractBinary
$compiler
->raw('] = array_pad('.$var.', ')
->repr(\count($this->variables))
->raw(', null)))')
->raw(', null))), '.$var.' = null][0]')
;
}
@@ -0,0 +1,38 @@
<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Tests\Node\Expression\Binary;
use Twig\Node\Expression\ArrayExpression;
use Twig\Node\Expression\Binary\ObjectDestructuringSetBinary;
use Twig\Node\Expression\ConstantExpression;
use Twig\Node\Expression\Variable\AssignContextVariable;
use Twig\Node\Expression\Variable\ContextVariable;
use Twig\Test\NodeTestCase;
class ObjectDestructuringSetTest extends NodeTestCase
{
public static function provideTests(): iterable
{
$left = new ArrayExpression([], 1);
$left->addElement(new AssignContextVariable('name', 1), new ConstantExpression('name', 1));
$left->addElement(new AssignContextVariable('address', 1), new ConstantExpression('email', 1));
$node = new ObjectDestructuringSetBinary($left, new ContextVariable('user', 1), 1);
return [
[$node, <<<'EOF'
// line 1
[[$context["name"], $context["address"]] = [CoreExtension::getAttribute($this->env, $this->source, ($_v0 = ($context["user"] ?? null)), "name", [], \Twig\Template::ANY_CALL, false, false, false, 1), CoreExtension::getAttribute($this->env, $this->source, $_v0, "email", [], \Twig\Template::ANY_CALL, false, false, false, 1)], $_v0 = null][0]
EOF
],
];
}
}
@@ -0,0 +1,39 @@
<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Tests\Node\Expression\Binary;
use Twig\Node\Expression\ArrayExpression;
use Twig\Node\Expression\Binary\SequenceDestructuringSetBinary;
use Twig\Node\Expression\EmptyExpression;
use Twig\Node\Expression\Variable\AssignContextVariable;
use Twig\Node\Expression\Variable\ContextVariable;
use Twig\Test\NodeTestCase;
class SequenceDestructuringSetTest extends NodeTestCase
{
public static function provideTests(): iterable
{
$left = new ArrayExpression([], 1);
$left->addElement(new AssignContextVariable('first', 1));
$left->addElement(new EmptyExpression(1));
$left->addElement(new AssignContextVariable('third', 1));
$node = new SequenceDestructuringSetBinary($left, new ContextVariable('values', 1), 1);
return [
[$node, <<<'EOF'
// line 1
[(($_v0 = ($context["values"] ?? null)) instanceof \Traversable ? CoreExtension::destructureSequence($context, [0 => "first", 1 => null, 2 => "third"], $_v0) : ([$context["first"], , $context["third"]] = array_pad($_v0, 3, null))), $_v0 = null][0]
EOF
],
];
}
}