diff --git a/CHANGELOG.md b/CHANGELOG.md index 8349fee5c..03298106b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `log2` and `log` math functions in `@stdlib/math`: PR [#166](https://github.com/tact-lang/tact/pull/166) - Reserve mode constants in `@stdlib/reserve`, namely `ReserveExact`, `ReserveAllExcept`, `ReserveAtMost`, `ReserveAddOriginalBalance`, `ReserveInvertSign`, `ReserveBounceIfActionFail`: PR [#173](https://github.com/tact-lang/tact/pull/173) - JSON Schema for `tact.config.json`: PR [#194](https://github.com/tact-lang/tact/pull/194) +- Display an error for integer overflow at compile-time: PR [#200](https://github.com/tact-lang/tact/pull/200) ### Changed - Update the `dump` function to handle addresses: PR [#175](https://github.com/tact-lang/tact/pull/175) diff --git a/src/types/__snapshots__/resolveDescriptors.spec.ts.snap b/src/types/__snapshots__/resolveDescriptors.spec.ts.snap index a2b389670..390c1f3bf 100644 --- a/src/types/__snapshots__/resolveDescriptors.spec.ts.snap +++ b/src/types/__snapshots__/resolveDescriptors.spec.ts.snap @@ -248,6 +248,16 @@ Line 15, col 3: " `; +exports[`resolveDescriptors should fail descriptors for case-25 1`] = ` +":8:21: Cannot evaluate constant expression due to integer overflow +Line 8, col 21: + 7 | +> 8 | const a: Int = 1 + (1 >> -1073741824); + ^~~~~~~~~~~~~~~~ + 9 | +" +`; + exports[`resolveDescriptors should resolve descriptors for case-0 1`] = ` { "BaseTrait": { diff --git a/src/types/resolveConstantValue.ts b/src/types/resolveConstantValue.ts index 5482c0799..19d0bca2a 100644 --- a/src/types/resolveConstantValue.ts +++ b/src/types/resolveConstantValue.ts @@ -5,7 +5,7 @@ import { ASTExpression, throwError } from "../grammar/ast"; import { printTypeRef, TypeRef } from "./types"; import { sha256_sync } from "@ton/crypto"; -function reduceInt(ast: ASTExpression): bigint { +function reduceIntImpl(ast: ASTExpression): bigint { if (ast.kind === 'number') { return ast.value; } else if (ast.kind === 'op_binary') { @@ -59,6 +59,18 @@ function reduceInt(ast: ASTExpression): bigint { throwError('Cannot reduce expression to a constant integer', ast.ref); } +function reduceInt(ast: ASTExpression): bigint { + try { + return reduceIntImpl(ast) + } catch (error) { + if (error instanceof RangeError) { + throwError('Cannot evaluate constant expression due to integer overflow', ast.ref); + } else { + throw error; + } + } +} + function reduceBool(ast: ASTExpression): boolean { if (ast.kind === 'boolean') { return ast.value; diff --git a/src/types/test-failed/case-25.tact b/src/types/test-failed/case-25.tact new file mode 100644 index 000000000..c9dd18dc4 --- /dev/null +++ b/src/types/test-failed/case-25.tact @@ -0,0 +1,8 @@ +primitive Int; +primitive Bool; + +trait BaseTrait { + +} + +const a: Int = 1 + (1 >> -1073741824);