{"record":{"id":"c63ef2515bbfd131","repo":"apple/pkl","slug":"cannotinferparent-c63ef2","errorCode":"cannotInferParent","errorMessage":"Cannot tell which parent to amend.","messagePattern":"Cannot tell which parent to amend\\.","errorType":"error_code","errorClass":"VmException","httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/ast/expression/member/AbstractInferParentNode.java","lineNumber":56,"sourceCode":"\n  protected Object getDefaultValue(\n      VirtualFrame frame,\n      @Nullable TypeNode typeNode,\n      SourceSection headerSection,\n      String qualifiedName) {\n    if (typeNode == null || typeNode instanceof UnknownTypeNode) {\n      return VmDynamic.empty();\n    }\n\n    var defaultValue = typeNode.createDefaultValue(frame, language, headerSection, qualifiedName);\n    if (defaultValue != null) {\n      return defaultValue;\n    }\n\n    CompilerDirectives.transferToInterpreter();\n\n    if (typeNode instanceof TypeVariableNode) {\n      throw exceptionBuilder().evalError(\"cannotInferParent\").build();\n    }\n\n    // try to produce a more specific error message than \"cannotInstantiateType\"\n    var clazz = typeNode.getVmClass();\n    if (clazz != null) {\n      VmUtils.checkIsInstantiable(clazz, typeNode);\n    }\n\n    throw exceptionBuilder()\n        .evalError(\"cannotInstantiateType\", typeNode.getSourceSection().getCharacters())\n        .build();\n  }\n}\n","sourceCodeStart":38,"sourceCodeEnd":70,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/ast/expression/member/AbstractInferParentNode.java#L38-L70","documentation":"Pkl throws `cannotInferParent` when an amending expression uses the implicit-parent (`new`/`...` with inferred receiver) form but the parent cannot be determined. In AbstractInferParentNode.getDefaultValue, if the type to amend is still a type variable at evaluation time, there is no concrete parent to amend.","triggerScenarios":"Using `...` (amend without explicit receiver) or a `new` with inferred type inside a generic/type-parameterized context where the type resolves to a TypeVariableNode (e.g. inside a generic function or type-aliased helper).","commonSituations":"Writing amendment shorthand inside generic utility functions in pkl modules; extending code that relied on concrete types with generics so the implicit parent inference breaks.","solutions":["Make the parent explicit: write `obj { ... }` or `new ConcreteType { ... }` instead of relying on inference.","Move the amendment out of the generic context to where the concrete type is known.","Pass the concrete type as a parameter or specialize the helper per type."],"exampleFix":"// before\nfunction amendIt<T>(x: T): T = x { ... } // cannot infer parent\n// after\nfunction amendIt(x: Server): Server = x {\n  port = 8080\n}","handlingStrategy":"validation","validationCode":"// Pkl: avoid inferred parents in generic contexts\nfunction amendIt(x: Server): Server = x { ... } // concrete type, OK","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never use inferred-parent shorthand inside generic functions.","Pass concrete object references to helpers that amend.","Search code for bare `...` bodies inside type-parameterized helpers."],"tags":["pkl","amendment","type-inference","generics"],"backgroundTag":"unsupported-operation","analyzedSha":"f3efcbfc9b60d30053b0536d664948d7aa1b8673","analyzedAt":"2026-09-08T13:10:45.570Z","contentChangedAt":"2026-09-08T13:10:45.570Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}