{"record":{"id":"4ce3dc438158a54e","repo":"apple/pkl","slug":"type-constraint-violation-value-does-not-fit-in-i-4ce3dc","errorCode":null,"errorMessage":"type constraint violation: value does not fit in Int32","messagePattern":"type constraint violation: value does not fit in Int32","errorType":"exception","errorClass":"VmTypeMismatchException","httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/ast/type/TypeNode.java","lineNumber":2701,"sourceCode":"\n    @Override\n    protected boolean acceptTypeNode(boolean visitTypeArguments, TypeNodeConsumer consumer) {\n      return consumer.accept(this);\n    }\n  }\n\n  public static final class Int32TypeAliasTypeNode extends IntSlotTypeNode {\n    public Int32TypeAliasTypeNode() {\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.intValue()) return value;\n\n        CompilerDirectives.transferToInterpreterAndInvalidate();\n        throw constraintException(value, BaseModule.getInt32TypeAlias().getConstraintSection());\n      }\n\n      CompilerDirectives.transferToInterpreterAndInvalidate();\n      throw new VmTypeMismatchException.Simple(\n          BaseModule.getInt32TypeAlias().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.getInt32TypeAlias();\n    }\n\n    @Override","sourceCodeStart":2683,"sourceCodeEnd":2719,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/ast/type/TypeNode.java#L2683-L2719","documentation":"The Int32 variant: a Long is valid for the Int32 alias only if l == l.intValue() (fits signed 32-bit). Otherwise the constraint exception is thrown at the Int32 alias's constraint section; non-Long values raise a VmTypeMismatchException.Simple against Int.","triggerScenarios":"Assigning a value outside -2147483648..2147483647 to an Int32-typed property (e.g. nanosecond timestamps, byte counts > 2 GiB), or a Float/String reaching the check.","commonSituations":"Timestamps or file sizes stored as Int32; arithmetic overflow like `seconds * 1_000_000_000`; deserialized large numbers from JSON imports.","solutions":["Declare the property as Int (Pkl's unbounded machine integer) instead of Int32","Split the computation so intermediate values stay in 32-bit range","Fix a wrong (non-integer) value at the source","Use `v is Int32` to validate before assignment"],"exampleFix":"// before\ndurationNs: Int32 = 5_000_000_000\n// after\ndurationNs: Int = 5_000_000_000","handlingStrategy":"validation","validationCode":"function fitsInt32(v) { return Number.isInteger(v) && v >= -2147483648 && v <= 2147483647; }","typeGuard":"function isInt32(v) { return typeof v === 'number' && Number.isInteger(v) && v >= -2147483648 && v <= 2147483647; }","tryCatchPattern":null,"preventionTips":["Use Int for timestamps, byte counts, and nanosecond math","Audit arithmetic that can overflow 32 bits (seconds*1e9 etc.)"],"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"}