{"record":{"id":"c434359f81fd0148","repo":"apple/pkl","slug":"methodmustbeconst-c43435","errorCode":"methodMustBeConst","errorMessage":"Cannot call method `{0}` from here because it is not `const`.","messagePattern":"Cannot call method `(.+?)` from here because it is not `const`\\.","errorType":"error_code","errorClass":"VmException","httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/ast/expression/member/InvokeSuperMethodNode.java","lineNumber":74,"sourceCode":"  }\n\n  protected ClassMethod findSupermethod(VirtualFrame frame) {\n    var owner = VmUtils.getOwner(frame);\n    while (owner instanceof VmFunction) {\n      owner = owner.getEnclosingOwner();\n    }\n    assert owner != null : \"VmFunction always has a parent\";\n    assert owner.isPrototype();\n\n    var superclass = owner.getVmClass().getSuperclass();\n    assert superclass != null;\n\n    // note the use of getMethod() rather than getDeclaredMethod()\n    var supermethod = superclass.getMethod(methodName);\n    if (supermethod != null) {\n      if (needsConst && !supermethod.isConst()) {\n        CompilerDirectives.transferToInterpreter();\n        throw exceptionBuilder().evalError(\"methodMustBeConst\", methodName.toString()).build();\n      }\n      return supermethod;\n    }\n\n    CompilerDirectives.transferToInterpreter();\n    var parent = owner.getParent();\n    assert parent != null;\n    throw exceptionBuilder()\n        .cannotFindMethod(parent, methodName, argumentNodes.length, false)\n        .build();\n  }\n}\n","sourceCodeStart":56,"sourceCodeEnd":87,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/ast/expression/member/InvokeSuperMethodNode.java#L56-L87","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Add the `const` modifier to the superclass method being called via `super` (if it is pure and safe to do so).","Remove `const` from the property/method in the subclass that performs the `super` call, if const-ness is not required there.","Inline the needed logic directly in the const context instead of delegating to the non-const super method.","If the superclass is third-party, pin a version where the method is const or wrap the computation in a non-const property."],"exampleFix":"// before\nopen class Base {\n  function greet(): String = \"hello \" + compute()\n}\nclass Child extends Base {\n  const greeting: String = super.greet() // error: greet() is not const\n}\n\n// after\nopen class Base {\n  const function greet(): String = \"hello\"\n}\nclass Child extends Base {\n  const greeting: String = super.greet() // OK: greet() is const\n}","handlingStrategy":"validation","validationCode":"// Pkl: before using super in a const context, confirm the super method is const\nfunction isConstSuperMethodAvailable(cls: Class, name: String): Boolean =\n  cls.getSuperclass()?.getMethod(name) is ClassMethod\n// ensure the found method is declared `const` before calling it from a const property","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Mark pure methods `const` in base classes so subclasses can safely use them in const contexts.","Never reference `super` methods from `const` property initializers unless the method is const.","When refactoring a base class, grep subclasses for `super.<method>` before removing or de-consting a method."],"tags":["pkl","const-correctness","super-method","language-runtime"],"backgroundTag":"unsupported-operation","analyzedSha":"f3efcbfc9b60d30053b0536d664948d7aa1b8673","analyzedAt":"2026-09-08T13:10:45.570Z","contentChangedAt":"2026-09-08T13:10:45.570Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}