apple/pkl · error · VmException

cannotInferParent

cannotInferParent

Error message

Cannot tell which parent to amend.

What it means

Pkl throws `cannotInferParent` when an amend-within-method-argument node looks up the enclosing method's frame auxiliary slot and it is absent. InferParentWithinMethodArgumentNode.getMethodSlot notes this happens in intrinsic constructors such as `pkl.base#List()`, where no method frame slot exists to identify the parent.

Solutions

  1. Make the target explicit instead of relying on inference (e.g. pass the object directly: `List(obj.elements)`).
  2. Move the expression into a proper method context where a method frame exists.
  3. Replace the intrinsic constructor call with an object literal form (`new Listing { ... }`).

Example fix

// before
List(...) // cannot infer parent
// after
List(someListing.elements)
Defensive patterns

Strategy: validation

Validate before calling

// Pkl: pass explicit values to intrinsic constructors
List(someListing.elements) // no inferred parent needed

Prevention

When it happens

Trigger: Using implicit-parent amendment syntax (`...` / inferred receiver) as an argument to an intrinsic constructor like `List(...)`, `Set(...)`, or `Map(...)` where no enclosing Pkl method frame exists.

Common situations: Writing `List(...)` with amendment shorthand at module top level or inside non-method scopes; calling intrinsic collection constructors from contexts Pkl cannot attribute to a method.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of apple/pkl@f3efcbfc9b (2026-09-08). Data as JSON: /api/errors/72ed933ca87f9c30. Report an issue: GitHub.

Appendix: source

Thrown at pkl-core/src/main/java/org/pkl/core/ast/expression/member/InferParentWithinMethodArgumentNode.java:46

import org.pkl.core.runtime.VmLanguage;
import org.pkl.core.runtime.VmUtils;

public abstract class InferParentWithinMethodArgumentNode
    extends AbstractInferParentFromMethodNode {
  private final int argIndex;

  public InferParentWithinMethodArgumentNode(
      SourceSection sourceSection, VmLanguage language, int argIndex) {
    super(sourceSection, language);
    this.argIndex = argIndex;
  }

  @TruffleBoundary
  private int getMethodSlot(FrameDescriptor frameDescriptor) {
    var methodSlot = frameDescriptor.getAuxiliarySlots().get(VmUtils.METHOD_FRAME_SLOT_ID);
    if (methodSlot == null) {
      // used in intrinsic constructor e.g. pkl.base#List()
      throw exceptionBuilder().evalError("cannotInferParent").build();
    }
    return methodSlot;
  }

  @Override
  protected Method getMethod(VirtualFrame frame) {
    var method = (Method) frame.getAuxiliarySlot(getMethodSlot(frame.getFrameDescriptor()));
    if (method == null) {
      // used in FunctionN.apply()
      CompilerDirectives.transferToInterpreter();
      throw exceptionBuilder().evalError("cannotInferParent").build();
    }

    return method;
  }

  @Override
  protected @Nullable TypeNode getTypeNode(VirtualFrame frame, Method method) {

View on GitHub (pinned to f3efcbfc9b)