twigphp/Twig · error · SyntaxError

Cannot assign to " ", only variables can be assigned in…

Error message

Cannot assign to "%s", only variables can be assigned in sequence destructuring.

What it means

Twig's sequence destructuring (`[a, b] = value`) only accepts variables (or EmptyExpression placeholders for skipped elements) on the left side. The constructor throws this SyntaxError when any element of the sequence is a non-variable expression such as a constant or function call.

Solutions

  1. Replace the non-variable element with a plain variable name, or an empty placeholder (`[_, b]`-style skip via EmptyExpression) if the value is unused.
  2. Destructure into variables first, then index the original value for any computed targets.
  3. In PHP-generated templates, emit AssignContextVariable (or EmptyExpression) for every sequence element.

Example fix

// before (template)
{% set [config['key'], b] = pair %}
// after
{% set [cfg, b] = pair %}
{% set keyValue = cfg['key'] %}
Defensive patterns

Strategy: validation

Validate before calling

// Every element of a Twig sequence destructuring pattern must be a bare variable or a skip placeholder.
// When generating nodes in PHP:
foreach ($pairs as $p) {
    $ok = $p['value'] instanceof \Twig\Node\Expression\AssignContextVariable
        || $p['value'] instanceof \Twig\Node\Expression\EmptyExpression;
    if (!$ok) {
        throw new \InvalidArgumentException('Sequence destructuring target must be a variable or empty');
    }
}

Prevention

When it happens

Trigger: Compiling `{ ['literal', b] = arr }` or `{ [first(), b] = arr }`; direct PHP construction of SequenceDestructuringSetBinary whose left ArrayExpression contains a pair value that is neither EmptyExpression nor AssignContextVariable. The message contains the offending node's class name.

Common situations: Trying to destructure into array offsets or function results out of habit from other languages; templates generated by code that didn't normalize targets to variables; typos that turn a variable name into an expression.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of twigphp/Twig@a414c3a491 (2026-09-13). Data as JSON: /api/errors/43dfcfc2888c2301. Report an issue: GitHub.

Appendix: source

Thrown at src/Node/Expression/Binary/SequenceDestructuringSetBinary.php:41

 * @internal
 */
class SequenceDestructuringSetBinary extends AbstractBinary
{
    private array $variables = [];

    /**
     * @param ArrayExpression    $left  The array expression containing variables to assign to
     * @param AbstractExpression $right The expression providing values for assignment
     */
    public function __construct(Node $left, Node $right, int $lineno)
    {
        foreach ($left->getKeyValuePairs() as $pair) {
            if ($pair['value'] instanceof EmptyExpression) {
                $this->variables[] = null;
            } elseif ($pair['value'] instanceof AssignContextVariable) {
                $this->variables[] = $pair['value']->getAttribute('name');
            } else {
                throw new SyntaxError(\sprintf('Cannot assign to "%s", only variables can be assigned in sequence destructuring.', $pair['value']::class), $lineno);
            }
        }

        parent::__construct($left, $right, $lineno);
    }

    public function compile(Compiler $compiler): void
    {
        $compiler->addDebugInfo($this);
        $var = '$'.$compiler->getVarName();
        $compiler
            ->raw('[(('.$var.' = ')
            ->subcompile($this->getNode('right'))
            ->raw(') instanceof \Traversable ? CoreExtension::destructureSequence($context, ')
            ->repr($this->variables)
            ->raw(', '.$var.') : ([')
        ;
        foreach ($this->variables as $i => $name) {

View on GitHub (pinned to a414c3a491)