apple/pkl · error · VmException

methodMustBeConst

methodMustBeConst

Error message

Cannot call method `{0}` from here because it is not `const`.

What it means

This error is thrown by Pkl's super-method invocation node when code calls `super.method()` in a context that requires a `const` value (e.g. a `const` property or `const` method body), but the resolved superclass method is not declared `const`. Pkl enforces const-correctness: const contexts may only invoke methods that are themselves const, so evaluation is aborted. The check happens in `findSupermethod` right after the method is found on the superclass.

Source

Thrown at pkl-core/src/main/java/org/pkl/core/ast/expression/member/InvokeSuperMethodNode.java:74

  }

  protected ClassMethod findSupermethod(VirtualFrame frame) {
    var owner = VmUtils.getOwner(frame);
    while (owner instanceof VmFunction) {
      owner = owner.getEnclosingOwner();
    }
    assert owner != null : "VmFunction always has a parent";
    assert owner.isPrototype();

    var superclass = owner.getVmClass().getSuperclass();
    assert superclass != null;

    // note the use of getMethod() rather than getDeclaredMethod()
    var supermethod = superclass.getMethod(methodName);
    if (supermethod != null) {
      if (needsConst && !supermethod.isConst()) {
        CompilerDirectives.transferToInterpreter();
        throw exceptionBuilder().evalError("methodMustBeConst", methodName.toString()).build();
      }
      return supermethod;
    }

    CompilerDirectives.transferToInterpreter();
    var parent = owner.getParent();
    assert parent != null;
    throw exceptionBuilder()
        .cannotFindMethod(parent, methodName, argumentNodes.length, false)
        .build();
  }
}

View on GitHub (pinned to f3efcbfc9b)

Solutions

  1. Add the `const` modifier to the superclass method being called via `super` (if it is pure and safe to do so).
  2. Remove `const` from the property/method in the subclass that performs the `super` call, if const-ness is not required there.
  3. Inline the needed logic directly in the const context instead of delegating to the non-const super method.
  4. If the superclass is third-party, pin a version where the method is const or wrap the computation in a non-const property.

Example fix

// before
open class Base {
  function greet(): String = "hello " + compute()
}
class Child extends Base {
  const greeting: String = super.greet() // error: greet() is not const
}

// after
open class Base {
  const function greet(): String = "hello"
}
class Child extends Base {
  const greeting: String = super.greet() // OK: greet() is const
}
Defensive patterns

Strategy: validation

Validate before calling

// Pkl: before using super in a const context, confirm the super method is const
function isConstSuperMethodAvailable(cls: Class, name: String): Boolean =
  cls.getSuperclass()?.getMethod(name) is ClassMethod
// ensure the found method is declared `const` before calling it from a const property

Prevention

When it happens

Trigger: Occurs when `super.foo(...)` is evaluated where the invocation site was compiled with needsConst=true (const property initializer, const method, or const default value) and the superclass method `foo` exists but is a non-const method (no `const` modifier in its declaration).

Common situations: Declaring a `const` property in a subclass whose initializer calls a non-const `super` method; a parent class later drops the `const` keyword from a method after a refactor or library upgrade, breaking child classes that call it from const contexts; copying a method body into a const context without checking the super method's const-ness.

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