{"record":{"id":"d9c18a20f93d1f98","repo":"apple/pkl","slug":"duplicatedefinition","errorCode":"duplicateDefinition","errorMessage":"duplicateDefinition","messagePattern":"duplicateDefinition","errorType":"error_code","errorClass":"VmException","httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/ast/builder/AstBuilder.java","lineNumber":1587,"sourceCode":"  public GeneratorMemberNode visitForGenerator(ForGenerator ctx) {\n\n    var keyParameter = ctx.getP2() == null ? null : ctx.getP1();\n    var valueParameter = ctx.getP2() == null ? ctx.getP1() : ctx.getP2();\n    var keyBinding = makeBinding(keyParameter);\n    var valueBinding = makeBinding(valueParameter);\n    var keyIdentifier =\n        keyParameter instanceof TypedIdentifier keyTypedIdentifier ? keyTypedIdentifier : null;\n    var valueIdentifier =\n        valueParameter instanceof TypedIdentifier valueTypedIdentifier\n            ? valueTypedIdentifier\n            : null;\n    if (keyIdentifier != null\n        && valueIdentifier != null\n        && keyIdentifier\n            .getIdentifier()\n            .getValue()\n            .equals(valueIdentifier.getIdentifier().getValue())) {\n      throw exceptionBuilder()\n          .evalError(\"duplicateDefinition\", valueIdentifier.getIdentifier().getValue())\n          .withSourceSection(createSourceSection(valueIdentifier))\n          .build();\n    }\n\n    var unresolvedKeyTypeNode =\n        keyIdentifier == null ? null : visitTypeAnnotation(keyIdentifier.getTypeAnnotation());\n    var unresolvedValueTypeNode =\n        valueIdentifier == null ? null : visitTypeAnnotation(valueIdentifier.getTypeAnnotation());\n    // if possible, initialize immediately to avoid later insert\n    var keyTypeNode =\n        unresolvedKeyTypeNode == null && keyBinding != null\n            ? new TypeNode.UnknownTypeNode(VmUtils.unavailableSourceSection())\n                .initWriteSlotNode(keyBinding.slot())\n            : null;\n    // if possible, initialize immediately to avoid later insert\n    var valueTypeNode =\n        unresolvedValueTypeNode == null && valueBinding != null","sourceCodeStart":1569,"sourceCodeEnd":1605,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/ast/builder/AstBuilder.java#L1569-L1605","documentation":"Pkl throws duplicateDefinition when an object member declares identical key and value identifiers (e.g. a for-generator or property binding where the key name and the bound value name are the same string). The AstBuilder detects this while building the AST because such a definition would shadow itself and is meaningless. The error is raised with the value identifier's source section so the offending identifier is highlighted.","triggerScenarios":"Declaring an object member or generator entry where keyIdentifier != null && valueIdentifier != null and both identifiers' text is equal (e.g. `[foo] = foo` style bindings) during AST construction in AstBuilder.visitObjectMember-style code.","commonSituations":"Copy-pasting object entries; writing `for (x in ...) { [x] ... }` where the key equals the loop variable name; hand-editing Pkl config and accidentally repeating the same name on both sides of a definition.","solutions":["Rename the key or the value identifier so they differ.","If the key must match the value's name, drop the explicit key and use plain property syntax (`foo = ...`).","Search the flagged object body for duplicated identifier text and fix the specific entry reported by the source section."],"exampleFix":"// before\nperson {\n  [name] = name\n}\n// after\nperson {\n  name = \"Alice\"\n}","handlingStrategy":"validation","validationCode":"// Before rendering/parsing an object entry, ensure key and value identifiers differ:\nif (key != null && value != null && key.equals(value)) {\n  throw new IllegalArgumentException(\"key and value identifier must differ: \" + key);\n}","typeGuard":"boolean isSelfReferential(String key, String value) { return key != null && key.equals(value); }","tryCatchPattern":null,"preventionTips":["Prefer plain `name = value` property syntax over explicit `[key]` entries unless keys are dynamic.","Lint for entries where key text equals bound identifier text.","After copy-pasting object entries, check identifiers on both sides."],"tags":["pkl","ast","duplicate-definition","static-analysis"],"backgroundTag":"invalid-argument-format","analyzedSha":"f3efcbfc9b60d30053b0536d664948d7aa1b8673","analyzedAt":"2026-09-08T13:10:45.570Z","contentChangedAt":"2026-09-08T13:10:45.570Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}