{"record":{"id":"d11ad3fb0c3c71f3","repo":"apple/pkl","slug":"invalid-for-generator-modifier-dynamic-errormessa","errorCode":null,"errorMessage":"invalid for-generator modifier (dynamic errorMessageKey)","messagePattern":"invalid for-generator modifier \\(dynamic errorMessageKey\\)","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/ast/builder/AstBuilder.java","lineNumber":3185,"sourceCode":"  private @Nullable Pair<@Nullable FrameDescriptorBuilder, FrameSlotVariable[]>\n      createFrameDescriptorBuilderAndSlotVariables(ObjectBody body) {\n    if (body.getParameters().isEmpty()) return null;\n\n    var builder = new FrameDescriptorBuilder(body.getParameters().size());\n    return Pair.of(builder, getSlotVariables(body.getParameters(), builder));\n  }\n\n  private void checkNotInsideForGenerator(Node ctx, String errorMessageKey) {\n    if (!symbolTable.getCurrentScope().isForGeneratorScope()) {\n      return;\n    }\n    var forExprCtx = ctx.parent();\n    while (forExprCtx != null\n        && forExprCtx.getClass() != org.pkl.parser.syntax.ObjectMember.ForGenerator.class) {\n      forExprCtx = forExprCtx.parent();\n    }\n    assert forExprCtx != null;\n    throw exceptionBuilder()\n        .evalError(errorMessageKey)\n        .withSourceSection(\n            createSourceSection(\n                ((org.pkl.parser.syntax.ObjectMember.ForGenerator) forExprCtx).forSpan()))\n        .build();\n  }\n\n  private void checkDuplicateMember(\n      org.pkl.core.runtime.Identifier memberName,\n      SourceSection headerSection,\n      // use Set<String> rather than Set<Identifier>\n      // to detect conflicts between local and non-local identifiers\n      Set<String> visited) {\n\n    if (!visited.add(memberName.toString())) {\n      throw exceptionBuilder()\n          .evalError(\"duplicateDefinition\", memberName)\n          .withSourceSection(headerSection)","sourceCodeStart":3167,"sourceCodeEnd":3203,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/ast/builder/AstBuilder.java#L3167-L3203","documentation":"Pkl's AST builder rejects modifiers such as `...` (spread), `*` (defaults), or `when`/`is` predicates in a `for` generator position where they are not allowed. The builder walks up the parse tree to the enclosing ForGenerator node and attaches the error to the generator's source span. It means the object-spread/amend syntax you wrote cannot be combined with a for-generator in that position.","triggerScenarios":"Writing an object member whose modifier is invalid inside a `for (...) { ... }` generator in a Pkl module, e.g. spreading `...expr` or using an amend modifier on a for-generated member where the grammar disallows it.","commonSituations":"Copy-pasting object-spread patterns into for-generator bodies; assuming `for` generators support the same spread/defaults modifiers as plain object bodies; refactoring amends into generated members.","solutions":["Move the spread/default/conditional modifier out of the for-generator body onto a plain object member or the enclosing object","Iterate and emit plain properties inside the for body instead of using the invalid modifier","Check the Pkl language spec for which modifiers are legal on for-generator members"],"exampleFix":"// before\nfoo {\n  for (item in items) {\n    ...item.props // invalid modifier in for generator\n  }\n}\n// after\nfoo {\n  ...items.first.props\n  for (item in items.drop(1)) {\n    [\"item-\\(item.name)\"] { ...item.props }\n  }\n}","handlingStrategy":"validation","validationCode":"// before relying on modifiers inside a for generator, keep the body to plain members:\nfunction usesOnlyPlainMembers(body: Listing<*>) = body.isEmpty() // review manually: for(...) { ... } must not contain `...`/amend modifiers","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep for-generator bodies limited to plain property/entry members","Apply spread/default modifiers on the enclosing object, not inside for generators","Lint Pkl sources for `...` inside for blocks"],"tags":["pkl","ast-builder","syntax-error","for-generator"],"backgroundTag":"invalid-argument-value","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"}