{"record":{"id":"9d626a221c79a955","repo":"apple/pkl","slug":"cannotinferparent","errorCode":"cannotInferParent","errorMessage":"cannotInferParent","messagePattern":"cannotInferParent","errorType":"error_code","errorClass":"VmException","httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/ast/builder/AstBuilder.java","lineNumber":1118,"sourceCode":"              ReadPropertyNodeGen.create(\n                  createSourceSection(expr.newSpan()),\n                  org.pkl.core.runtime.Identifier.DEFAULT,\n                  new GetReceiverNode()),\n              new GetMemberKeyNode());\n    } else if (parent instanceof ClassMethod || parent instanceof ObjectMethod) {\n      var isObjectMethod =\n          parent instanceof ObjectMethod\n              || parent.parent() instanceof Module && moduleInfo.isAmend();\n      org.pkl.core.runtime.Identifier scopeName = scope.getName();\n      inferredParentNode =\n          isObjectMethod\n              ? InferParentWithinObjectMethodNodeGen.create(\n                  createSourceSection(expr.newSpan()), language, scopeName, new GetOwnerNode())\n              : InferParentWithinMethodNodeGen.create(\n                  createSourceSection(expr.newSpan()), language, scopeName, new GetOwnerNode());\n    } else if (parent instanceof LetExpr letExpr && letExpr.getBindingExpr() == child) {\n      // TODO correctly infer parent, e.g. `let (x: Person = new {}) ...`\n      throw exceptionBuilder()\n          .evalError(\"cannotInferParent\")\n          .withSourceSection(createSourceSection(expr.newSpan()))\n          .build();\n    } else if (parent instanceof ArgumentList argumentList) {\n      // cases we can't cover currently\n      // - FunctionN.apply: the parameter type nodes are not stored\n      // - pkl.base intrinsic constructors: List(), Set(), Map(), Bytes()\n      // - generic methods: pkl.base#Pair(), etc.\n      // these will throw cannotInferParent at runtime\n      var sourceSection = createSourceSection(expr.newSpan());\n      var argIndex = argumentList.getArguments().indexOf(child);\n      inferredParentNode =\n          InferParentWithinMethodArgumentNodeGen.create(sourceSection, language, argIndex);\n    } else {\n      throw exceptionBuilder()\n          .evalError(\"cannotInferParent\")\n          .withSourceSection(createSourceSection(expr.newSpan()))\n          .build();","sourceCodeStart":1100,"sourceCodeEnd":1136,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/ast/builder/AstBuilder.java#L1100-L1136","documentation":"When an object literal (`new ...`) appears in certain positions, Pkl must infer its parent (the class being instantiated) from surrounding context. If the direct parent expression is a `let` binding whose binding expression is the object, the builder cannot determine the parent type and throws cannotInferParent (a known unimplemented case, per the TODO).","triggerScenarios":"Writing `let (x: Person = new {}) { ... }` — an `new {}` object as the binding expression of a let, so `InferParent` has no context node.","commonSituations":"Attempting local scoped object construction inside a let block, a style that works in some dynamic languages but is unsupported here.","solutions":["Give the type explicitly: write `new Person {}` instead of `new {}`.","Assign to a typed property instead of using let, letting the property's type annotate the object.","Introduce an intermediate local via a typed lambda parameter or refactor to a function."],"exampleFix":"// before\nlet (x: Person = new {}) { x.name }\n// after\nlet (x = new Person {}) { x.name }","handlingStrategy":"type-guard","validationCode":"// Prefer explicit types on every `new` inside a let:\nlet (x = new Person {}) { ... }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always annotate `new` expressions in let bindings with their type (`new Person {}`).","Avoid bare `new {}` as a let binding value; it has no inferable parent."],"tags":["pkl","type-inference","let"],"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"}