apple/pkl · error · VmException

cannotAddElementWithEntrySyntax

cannotAddElementWithEntrySyntax

Error message

Cannot use entry syntax to add an element.

What it means

EntriesLiteralNode error 'Cannot use entry syntax to add an element.': entry syntax `[key] = value` is only valid for map-like objects (object/mapping/dynamic); applying it to a Listing parent (which holds plain elements, not keyed entries) is rejected. The input at fault is an entries-literal member added to a Listing.

Solutions

  1. Use plain element syntax (`new { ... }` or positional entries) for Listings.
  2. Change the parent object to a Dynamic or Mapping if keyed entries are intended.

Example fix

// before
new Listing<String> {
  ["a"] = "x"
}
// after
new Mapping<String> {
  ["a"] = "x"
}
Defensive patterns

Strategy: validation

Validate before calling

// Listing bodies must use element syntax only
// wrong: ["k"] = v  |  right: v

Type guard

function isEntryContainer(t) { return t is Dynamic || t is Mapping; }

Prevention

When it happens

Trigger: `Listing { ["key"] = value }` or `[k] = v` inside an object literal amending the Listing class, with the first key node's section reported.

Common situations: Copy-pasting a Mapping/Dynamic literal into a Listing, or adding named entries to a list-shaped config.

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/9b7e7e853652c52e. Report an issue: GitHub.

Appendix: source

Thrown at pkl-core/src/main/java/org/pkl/core/ast/expression/literal/EntriesLiteralNode.java:175

      VirtualFrame frame, @SuppressWarnings("unused") VmClass parent) {
    return new VmMapping(
        frame.materialize(), BaseModule.getMappingClass().getPrototype(), createMapMembers(frame));
  }

  @Specialization(guards = "parent == getDynamicClass()")
  protected VmDynamic evalDynamicClass(
      VirtualFrame frame, @SuppressWarnings("unused") VmClass parent) {
    return new VmDynamic(
        frame.materialize(),
        BaseModule.getDynamicClass().getPrototype(),
        createMapMembers(frame),
        0);
  }

  @Specialization(guards = {"parent == getListingClass()", "checkIsValidListingAmendment()"})
  protected void evalListingClass(@SuppressWarnings("unused") VmClass parent) {
    CompilerDirectives.transferToInterpreter();
    throw exceptionBuilder()
        .evalError("cannotAddElementWithEntrySyntax")
        .withSourceSection(keyNodes[0].getSourceSection())
        .build();
  }

  @Fallback
  @TruffleBoundary
  protected Object fallback(Object parent) {
    var value = parent;
    // blame the non-null type (e.g. blame `Duration` instead of `Null` in the case of `Duration?`)
    if (value instanceof VmNull vmNull) {
      value = getNullDefaultValue(vmNull);
    }
    return elementsEntriesFallback(value, values[0], false);
  }

  @ExplodeLoop
  protected EconomicMap<Object, ObjectMember> createMapMembers(VirtualFrame frame) {

View on GitHub (pinned to f3efcbfc9b)