{"record":{"id":"2bcf0b1abb7530a8","repo":"apple/pkl","slug":"type-constraint-mismatch","errorCode":null,"errorMessage":"type constraint mismatch","messagePattern":"type constraint mismatch","errorType":"error_code","errorClass":"VmTypeMismatchException.Constraint","httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/ast/type/TypeConstraintNode.java","lineNumber":69,"sourceCode":"\n  @Specialization\n  protected void eval(VirtualFrame frame, boolean result) {\n    initConstraintSlot(frame);\n\n    if (!result) {\n      CompilerDirectives.transferToInterpreterAndInvalidate();\n      var vmContext = VmContext.get(this);\n      var localContext = VmLanguage.get(this).localContext.get();\n      // Use power assertions if enabled and not in type test or already instrumenting.\n      // This prevents `is` checks from triggering instrumentation, but allows them to\n      // participate if instrumentation is already active.\n      var usePowerAssertions =\n          vmContext.getPowerAssertionsEnabled()\n              && (!localContext.isInTypeTest() || localContext.hasActiveTracker());\n      if (usePowerAssertions) {\n        try (var valueTracker = vmContext.getValueTrackerFactory().create()) {\n          getBodyNode().executeGeneric(frame);\n          throw new VmTypeMismatchException.Constraint(\n              sourceSection,\n              frame.getAuxiliarySlot(customThisSlot),\n              sourceSection,\n              valueTracker.values());\n        }\n      } else {\n        throw new VmTypeMismatchException.Constraint(\n            sourceSection, frame.getAuxiliarySlot(customThisSlot), sourceSection, null);\n      }\n    }\n  }\n\n  @Specialization\n  protected void eval(\n      VirtualFrame frame,\n      VmFunction function,\n      @Cached(value = \"createApplyNode()\", neverDefault = true) ApplyVmFunction1Node applyNode) {\n    initConstraintSlot(frame);","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/ast/type/TypeConstraintNode.java#L51-L87","documentation":"A Pkl type constraint (a `is`/typecheck assertion on a property or type) failed while power assertions were enabled (vmContext.getPowerAssertionsEnabled() and not inside a tracker-less type test). After executing the constraint body with value tracking, the evaluator throws VmTypeMismatchException.Constraint, rendered as \"type constraint mismatch\", recording which intermediate values were produced during evaluation to explain the failure.","triggerScenarios":"A value fails a property type constraint or `is` type test at runtime while power assertions are enabled; e.g. assigning a value that does not satisfy `x is Int(isEven)` or a listing element violating its declared element type constraint.","commonSituations":"Config values that don't satisfy declared constraints (wrong numeric ranges, malformed strings, wrong object shape); tests or REPL runs with power assertions enabled surfacing richer mismatch diagnostics than production.","solutions":["Read the mismatch diagnostics: the tracked values show how the constraint evaluation failed (e.g. intermediate results of chained predicates).","Fix the supplied value so it satisfies the declared type constraint, or correct the constraint if it is wrong.","Temporarily disable power assertions if the richer diagnostics are unwanted, but prefer fixing the value/constraint.","Reproduce with a minimal snippet to confirm which constraint (property type vs. element type) rejects the value."],"exampleFix":"// before (Pkl)\nport: Int(this > 1024 && this < 65536) = 80 // fails: 80 <= 1024\n// after\nport: Int(this > 1024 && this < 65536) = 8080","handlingStrategy":"validation","validationCode":"// Pkl: validate before assignment\nassert(cond(value), \"value ${value} must satisfy the declared constraint\")\nx: ConstraintType = value","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Validate config values against declared constraints before feeding them to modules.","Enable power assertions during development for richer mismatch diagnostics.","Keep constraints simple and test type aliases with pkl test."],"tags":["type-constraint","type-mismatch","power-assertions","pkl"],"backgroundTag":"type-mismatch","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"}