Skip to content

Commit

Permalink
fix(const-eval): display an error for integer overflow at compile-time (
Browse files Browse the repository at this point in the history
#200)

Closes #189
  • Loading branch information
byakuren-hijiri authored Apr 1, 2024
1 parent c0ec546 commit ea5191a
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions src/types/__snapshots__/resolveDescriptors.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,16 @@ Line 15, col 3:
"
`;
exports[`resolveDescriptors should fail descriptors for case-25 1`] = `
"<unknown>: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": {
Expand Down
14 changes: 13 additions & 1 deletion src/types/resolveConstantValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions src/types/test-failed/case-25.tact
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
primitive Int;
primitive Bool;

trait BaseTrait {

}

const a: Int = 1 + (1 >> -1073741824);

0 comments on commit ea5191a

Please sign in to comment.