{"record":{"id":"ec1c9131c7108b62","repo":"apple/pkl","slug":"propertymustbeconst-ec1c91","errorCode":"propertyMustBeConst","errorMessage":"Cannot reference property `{0}` from here because it is not `const`.","messagePattern":"Cannot reference property `(.+?)` 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/ReadPropertyNode.java","lineNumber":169,"sourceCode":"  private VmException cannotFindProperty(VmObjectLike receiver) {\n    return exceptionBuilder()\n        .cannotFindProperty(\n            receiver, propertyName, true, lookupMode != MemberLookupMode.EXPLICIT_RECEIVER)\n        .build();\n  }\n\n  // Only ever need to check once per node because `needsConst` is only true in the case of:\n  //\n  // * implicit receiver in class (and module) bodies\n  // * `local const` object members\n  //\n  // and the const-ness of a resolved property cannot be changed by subclasses / amending objects.\n  private void checkConst(VmObjectLike receiver) {\n    if (needsConst && !isConstChecked) {\n      CompilerDirectives.transferToInterpreterAndInvalidate();\n      var property = receiver.getVmClass().getProperty(propertyName);\n      if (property != null && !property.isConst()) {\n        throw exceptionBuilder().evalError(\"propertyMustBeConst\", propertyName.toString()).build();\n      }\n      var objectMember = receiver.getMember(propertyName);\n      if (objectMember != null && !objectMember.isConst()) {\n        throw exceptionBuilder().evalError(\"propertyMustBeConst\", propertyName.toString()).build();\n      }\n      if (property == null && objectMember == null) {\n        // fall through; `cannotFindProperty` gets thrown when we attempt to read the property.\n        return;\n      }\n      isConstChecked = true;\n    }\n  }\n}\n","sourceCodeStart":151,"sourceCodeEnd":183,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/ast/expression/member/ReadPropertyNode.java#L151-L183","documentation":"In Pkl, `const` properties can only be read from within const-qualified contexts (e.g. `fixed`/`const` members or definitions). ReadPropertyNode.checkConst runs when a property read site requires const-ness and verifies that the resolved class property (and the object member) actually declares `const`. If the resolved property is not `const`, the evaluator refuses the reference with propertyMustBeConst rather than allowing a value that could differ between amending objects.","triggerScenarios":"Reading a non-const property from a context marked needsConst: e.g. referencing an object property inside a `const` property definition, inside a `fixed` member, or in another location whose read node was constructed with needsConst=true (evalObject path).","commonSituations":"Declaring `const x: Int = foo.bar` where `bar` is a normal (non-const) property; referencing a parent object's mutable property from a `fixed` property; copying an example from `fixed`/`const` docs against a property that was later de-const-ed.","solutions":["Declare the referenced property as `const` (e.g. change `bar: Int` to `const bar: Int`) in the defining class or object","Replace the non-const read with a literal or expression that only involves const/literal values","Change the reading property from `const`/`fixed` to a regular property so const-ness is not required","If the property is conditionally missing, verify the property exists on the receiver (checkConst falls through to cannotFindProperty otherwise)"],"exampleFix":"// before\nclass Server {\n  port: Int = 8080\n  const defaultPort: Int = port // ERROR: port is not const\n}\n// after\nclass Server {\n  const port: Int = 8080\n  const defaultPort: Int = port\n}","handlingStrategy":"validation","validationCode":"// pkl: check the referenced property is declared const before using it in a const context\n// class C { const p: Int = q }  ->  ensure class C declares: const q ...\nfunction isConstProp(cls: Class, name: String): Boolean =\n  cls.getProperty(name).isConst","typeGuard":"null","tryCatchPattern":"null","preventionTips":["Mark values intended for const consumption as `const` at definition time","Avoid reading regular properties from `fixed`/`const` members","Run `pkl eval` in CI to catch const violations before publishing schemas"],"tags":["pkl","const-property","language"],"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"}