{"record":{"id":"648b6ef7789cce5e","repo":"apple/pkl","slug":"cannotapplyconstmodifier","errorCode":"cannotApplyConstModifier","errorMessage":"cannotApplyConstModifier","messagePattern":"cannotApplyConstModifier","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/ast/member/UnresolvedPropertyNode.java","lineNumber":148,"sourceCode":"    var isConst = VmModifier.isConst(modifiers);\n    if (superProperty.isConst() == isConst) {\n      return;\n    }\n    CompilerDirectives.transferToInterpreter();\n\n    if (superProperty.isConst()) {\n      throw exceptionBuilder()\n          .withSourceSection(headerSection)\n          .evalError(\n              \"missingConstModifier\",\n              name,\n              superClass.getQualifiedName(),\n              sourceSection.getCharacters())\n          .build();\n    }\n    var source = headerSection.getCharacters().toString();\n    var constModifierIdx = source.indexOf(\"const\");\n    throw exceptionBuilder()\n        .withSourceSection(\n            headerSection\n                .getSource()\n                .createSection(headerSection.getCharIndex() + constModifierIdx, 5))\n        .evalError(\"cannotApplyConstModifier\", name, superClass.getQualifiedName())\n        .build();\n  }\n\n  @Override\n  public ClassProperty execute(VirtualFrame frame, VmClass clazz) {\n    CompilerDirectives.transferToInterpreter();\n\n    var annotations = VmUtils.evaluateAnnotations(frame, annotationNodes);\n\n    var typeNode =\n        unresolvedTypeNode == null\n            ? null\n            : new PropertyTypeNode(","sourceCodeStart":130,"sourceCodeEnd":166,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/ast/member/UnresolvedPropertyNode.java#L130-L166","documentation":"Pkl throws `cannotApplyConstModifier` when a property is declared with the `const` modifier but cannot legally use it. `checkConst` in UnresolvedPropertyNode rejects the declaration when the property overrides a member of a superclass that does not permit a const override (i.e. the modifier cannot be applied to that inherited property). It highlights the `const` token in the module header area of the source.","triggerScenarios":"Declaring a property as `const` in a class/module that overrides a property inherited from a superclass where applying `const` is disallowed; e.g. overriding an existing property with `const` instead of declaring a fresh one.","commonSituations":"Refactoring a plain property override into a `const` one assuming const-ness can be added freely; copying `const` modifiers from base classes into subclasses; schema authoring where a base class property is overridden in an extending module.","solutions":["Remove the `const` modifier from the overriding property declaration","If constant-ness is required, move the property so it is newly declared rather than an override, or change the superclass property's declaration","Check the superclass (named in the message) to see how the property is declared and align modifiers"],"exampleFix":"// before (MyModule.pkl overriding base class property)\nconst name: String\n// after\nname: String","handlingStrategy":"validation","validationCode":"// before running pkl, scan overrides for const modifier\nconst overridden = /(?m)^\\s*const\\s+\\w+/.test(moduleSource) && moduleSource.includes(\"extends\");\nif (overridden) console.warn(\"const used on override — verify superclass allows it\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Only use const on freshly declared properties, not overrides","Review superclass property modifiers before adding const in subclasses"],"tags":["pkl","typecheck","modifier","inheritance"],"backgroundTag":"invalid-config-value","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"}