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
- 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
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
- Check declared type width before assigning imported values
- Prefer Int for counters and sizes that may grow
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
- type constraint violation: value does not fit in Int8
- type constraint violation: value does not fit in Int32
- type constraint mismatch
- type mismatch: type constraint must be a Boolean or a Functi
- type constraint violation: value must not be null (non-null
AI-assisted analysis of apple/pkl@f3efcbfc9b (2026-09-08).
Data as JSON: /api/errors/0b335e407e556bc0.
Report an issue: GitHub.