{"record":{"id":"0b335e407e556bc0","repo":"apple/pkl","slug":"type-constraint-violation-value-does-not-fit-in-i-0b335e","errorCode":null,"errorMessage":"type constraint violation: value does not fit in Int16","messagePattern":"type constraint violation: value does not fit in Int16","errorType":"exception","errorClass":"VmTypeMismatchException","httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/ast/type/TypeNode.java","lineNumber":2657,"sourceCode":"    @Override\n    protected boolean acceptTypeNode(boolean visitTypeArguments, TypeNodeConsumer consumer) {\n      return consumer.accept(this);\n    }\n  }\n\n  public static final class Int16TypeAliasTypeNode extends IntSlotTypeNode {\n    public Int16TypeAliasTypeNode() {\n      super(VmUtils.unavailableSourceSection());\n    }\n\n    @Override\n    protected Object executeLazily(VirtualFrame frame, Object value) {\n      if (value instanceof Long l) {\n        if (l == l.shortValue()) return value;\n\n        CompilerDirectives.transferToInterpreterAndInvalidate();\n        var sourceSection = BaseModule.getInt16TypeAlias().getConstraintSection();\n        throw constraintException(value, sourceSection);\n      }\n\n      throw new VmTypeMismatchException.Simple(\n          BaseModule.getInt16TypeAlias().getBaseTypeSection(), value, BaseModule.getIntClass());\n    }\n\n    @Override\n    public VmClass getVmClass() {\n      return BaseModule.getIntClass();\n    }\n\n    @Override\n    public VmTypeAlias getVmTypeAlias() {\n      return BaseModule.getInt16TypeAlias();\n    }\n\n    @Override\n    public VmTyped getMirror() {","sourceCodeStart":2639,"sourceCodeEnd":2675,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/ast/type/TypeNode.java#L2639-L2675","documentation":"Identical to the Int8 check but for the Int16 type alias: a Long is accepted only when it fits the signed 16-bit range (-32768..32767), checked via l == l.shortValue(). Anything wider triggers a constraint violation; a non-Long value triggers a simple type mismatch against Int.","triggerScenarios":"A value typed Int16 receives a Long outside -32768..32767 (e.g. 70000), or receives a Float/String that is not an Int at all.","commonSituations":"Memory sizes or counters declared Int16 that grow beyond the range; imported data (JSON numbers) exceeding 16 bits; copy-paste of a value intended for Int32.","solutions":["Widen the declaration to Int32 or Int","Compute the value within -32768..32767 and assert the range before assignment","If the value should be a non-integer, fix the source type","Guard with `v is Int16` before assigning"],"exampleFix":"// before\nmaxConnections: Int16 = 100000\n// after\nmaxConnections: Int32 = 100000","handlingStrategy":"validation","validationCode":"function fitsInt16(v) { return Number.isInteger(v) && v >= -32768 && v <= 32767; }","typeGuard":"function isInt16(v) { return typeof v === 'number' && Number.isInteger(v) && v >= -32768 && v <= 32767; }","tryCatchPattern":null,"preventionTips":["Check declared type width before assigning imported values","Prefer Int for counters and sizes that may grow"],"tags":["pkl","integer-range","type-constraint"],"backgroundTag":"value-out-of-range","analyzedSha":"f3efcbfc9b60d30053b0536d664948d7aa1b8673","analyzedAt":"2026-09-08T13:10:45.570Z","contentChangedAt":"2026-09-08T13:10:45.570Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}