apple/pkl · error · VmTypeMismatchException

type constraint violation: value does not fit in Int16

Error message

type constraint violation: value does not fit in Int16

What it means

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.

Source

Thrown at pkl-core/src/main/java/org/pkl/core/ast/type/TypeNode.java:2657

    @Override
    protected boolean acceptTypeNode(boolean visitTypeArguments, TypeNodeConsumer consumer) {
      return consumer.accept(this);
    }
  }

  public static final class Int16TypeAliasTypeNode extends IntSlotTypeNode {
    public Int16TypeAliasTypeNode() {
      super(VmUtils.unavailableSourceSection());
    }

    @Override
    protected Object executeLazily(VirtualFrame frame, Object value) {
      if (value instanceof Long l) {
        if (l == l.shortValue()) return value;

        CompilerDirectives.transferToInterpreterAndInvalidate();
        var sourceSection = BaseModule.getInt16TypeAlias().getConstraintSection();
        throw constraintException(value, sourceSection);
      }

      throw new VmTypeMismatchException.Simple(
          BaseModule.getInt16TypeAlias().getBaseTypeSection(), value, BaseModule.getIntClass());
    }

    @Override
    public VmClass getVmClass() {
      return BaseModule.getIntClass();
    }

    @Override
    public VmTypeAlias getVmTypeAlias() {
      return BaseModule.getInt16TypeAlias();
    }

    @Override
    public VmTyped getMirror() {

View on GitHub (pinned to f3efcbfc9b)

Solutions

  1. Widen the declaration to Int32 or Int
  2. Compute the value within -32768..32767 and assert the range before assignment
  3. If the value should be a non-integer, fix the source type
  4. Guard with `v is Int16` before assigning

Example fix

// before
maxConnections: Int16 = 100000
// after
maxConnections: Int32 = 100000
Defensive patterns

Strategy: validation

Validate before calling

function fitsInt16(v) { return Number.isInteger(v) && v >= -32768 && v <= 32767; }

Type guard

function isInt16(v) { return typeof v === 'number' && Number.isInteger(v) && v >= -32768 && v <= 32767; }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


AI-assisted analysis of apple/pkl@f3efcbfc9b (2026-09-08). Data as JSON: /api/errors/0b335e407e556bc0. Report an issue: GitHub.