Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display an error for integer overflow at compile-time #200

Merged
merged 4 commits into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Loading