{"record":{"id":"3dcf5ddb0bdf8689","repo":"twigphp/Twig","slug":"you-cannot-assign-a-value-to-s-tempnameexpression","errorCode":null,"errorMessage":"You cannot assign a value to \"%s\".","messagePattern":"You cannot assign a value to \"(.+?)\"\\.","errorType":"exception","errorClass":"SyntaxError","httpStatus":null,"severity":"error","filePath":"src/Node/Expression/TempNameExpression.php","lineNumber":32,"sourceCode":"use Twig\\Compiler;\nuse Twig\\Error\\SyntaxError;\n\nclass TempNameExpression extends AbstractExpression\n{\n    // 4.0: re-evaluate \"varargs\" here once the implicit macro varargs bucket is removed\n    // (see MacroNode::VARARGS_NAME); the other names map to compiled variables ($context,\n    // $macros, $blocks, $this) and must stay.\n    public const RESERVED_NAMES = ['varargs', 'context', 'macros', 'blocks', 'this'];\n\n    // Prefix applied to reserved names so their compiled PHP variables cannot clash\n    // with the internal ones ($varargs, $context, $macros, $blocks, $this)\n    public const RESERVED_NAME_PREFIX = \"\\u{035C}\";\n\n    public function __construct(string|int|null $name, int $lineno)\n    {\n        // All names supported by ExpressionParser::parsePrimaryExpression() should be excluded\n        if ($name && \\in_array(strtolower($name), ['true', 'false', 'none', 'null'], true)) {\n            throw new SyntaxError(\\sprintf('You cannot assign a value to \"%s\".', $name), $lineno);\n        }\n\n        if (self::class === static::class) {\n            trigger_deprecation('twig/twig', '3.15', 'The \"%s\" class is deprecated.', self::class);\n        }\n\n        if (null !== $name && (\\is_int($name) || ctype_digit($name))) {\n            $name = (int) $name;\n        } elseif (\\in_array($name, self::RESERVED_NAMES, true)) {\n            $name = self::RESERVED_NAME_PREFIX.$name;\n        }\n\n        parent::__construct([], ['name' => $name], $lineno);\n    }\n\n    public function compile(Compiler $compiler): void\n    {\n        if (null === $this->getAttribute('name')) {","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/twigphp/Twig/blob/a414c3a491defb5a60f2fc88ef79ff37c90010cd/src/Node/Expression/TempNameExpression.php#L14-L50","documentation":"TempNameExpression reserves identifiers for Twig's internal temporary variables and rejects using the reserved boolean/null literals (true, false, none, null) as assignment targets. This compile-time check prevents users from shadowing values Twig's expression parser treats specially.","triggerScenarios":"Writing {% set true = x %}, {% set null = 1 %} or capturing a for-loop target named 'none' — any assignment whose name lowercases to true/false/none/null.","commonSituations":"Mistakenly setting a variable named after a literal; template translation or code-generation output that emits reserved words as variable names.","solutions":["Rename the variable to something other than true/false/none/null","If generating templates, sanitize variable names against the reserved list ['true','false','none','null']"],"exampleFix":"// before\n{% set none = value %}\n// after\n{% set noValue = value %}","handlingStrategy":"validation","validationCode":"$reserved = ['true','false','none','null'];\nif (in_array(strtolower($name), $reserved, true)) {\n    throw new InvalidArgumentException(\"Cannot use reserved name '$name' as variable\");\n}","typeGuard":"function isReservedTwigName(string $name): bool {\n    return in_array(strtolower($name), ['true','false','none','null'], true);\n}","tryCatchPattern":"try {\n    $twig->parse($twig->tokenize(new \\Twig\\Source($code, $name)));\n} catch (\\Twig\\Error\\SyntaxError $e) {\n    // handle reserved-name assignment\n}","preventionTips":["Never name template variables after literals","Sanitize identifiers in template-generation code","Lint generated templates before writing them"],"tags":["twig","compile-time","reserved-name","syntax-error"],"backgroundTag":"invalid-identifier","analyzedSha":"a414c3a491defb5a60f2fc88ef79ff37c90010cd","analyzedAt":"2026-09-13T15:10:46.849Z","contentChangedAt":"2026-09-13T15:10:46.849Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}