Skip to content

Commit

Permalink
fix: const eval for Int and Bool expressions (#352)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gusarich authored Jun 7, 2024
1 parent c65ec9b commit 6e1aa01
Show file tree
Hide file tree
Showing 19 changed files with 381 additions and 142 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Typos in ABI generation: : PR [#372](https://github.com/tact-lang/tact/pull/372)
- `__tact_load_address_opt` code generation: PR [#373](https://github.com/tact-lang/tact/pull/373)
- Empty messages are now correctly converted into cells: PR [#380](https://github.com/tact-lang/tact/pull/380)
- All integer and boolean expressions are now being attempted to be evaluated as constants. Additionally, compile-time errors are thrown for errors encountered during the evaluation of actual constants: PR [#352](https://github.com/tact-lang/tact/pull/352)

## [1.3.0] - 2024-05-03

Expand Down
1 change: 1 addition & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
"src/test/feature-strings.spec.ts",
"src/test/features/intrinsics.tact",
"src/test/features/strings.tact",
"src/test/compilation-fail/fail-const-eval.spec.ts",
"stdlib/stdlib.fc"
]
}
38 changes: 38 additions & 0 deletions src/generator/intrinsics/tryExpressionIntrinsics.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { TactSyntaxError } from "../../errors";
import { ASTExpression } from "../../grammar/ast";
import { resolveConstantValue } from "../../types/resolveConstantValue";
import { getExpType } from "../../types/resolveExpression";
Expand Down Expand Up @@ -48,5 +49,42 @@ export function tryExpressionIntrinsics(
}
}

try {
const t = getExpType(ctx.ctx, exp);

if (t.kind === "null") {
const r = resolveConstantValue(t, exp, ctx.ctx);
if (r !== null) {
throw new Error("Expected null");
}
return "null()";
}
if (t.kind === "ref") {
if (t.name === "Int") {
const r = resolveConstantValue(t, exp, ctx.ctx);
if (typeof r !== "bigint") {
throw new Error("Expected bigint");
}
return r.toString(10);
}
if (t.name === "Bool") {
const r = resolveConstantValue(t, exp, ctx.ctx);
if (typeof r !== "boolean") {
throw new Error("Expected boolean");
}
return r ? "true" : "false";
}
}
} catch (e) {
if (e instanceof TactSyntaxError) {
if (
!e.message.includes("Cannot reduce expression to a constant") &&
!e.message.includes("Expected constant value")
) {
throw e;
}
}
}

return null;
}
2 changes: 1 addition & 1 deletion src/generator/writers/writeExpression.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const golden: string[] = [
"($a + ($b / $c))",
"true",
"false",
"( (( (($a > 1)) ? (true) : (( (($b < 2)) ? (($c == 3)) : (false) )) )) ? (true) : ((~ ( (( (($d != 4)) ? (true) : (false) )) ? ((~ false)) : (false) ))) )",
"( (( (($a > 1)) ? (true) : (( (($b < 2)) ? (($c == 3)) : (false) )) )) ? (true) : ((~ ( (( (($d != 4)) ? (true) : (false) )) ? (true) : (false) ))) )",
"$global_f1($a)",
"$A$_constructor_a_b(1, 2)",
`$j'a`,
Expand Down
202 changes: 171 additions & 31 deletions src/test/__snapshots__/feature-constants.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,76 @@ ConstantTester {
"type": "int",
},
},
{
"arguments": [],
"name": "something16",
"returnType": {
"format": 257,
"kind": "simple",
"optional": false,
"type": "int",
},
},
{
"arguments": [],
"name": "something17",
"returnType": {
"format": 257,
"kind": "simple",
"optional": false,
"type": "int",
},
},
{
"arguments": [],
"name": "something18",
"returnType": {
"format": 257,
"kind": "simple",
"optional": false,
"type": "int",
},
},
{
"arguments": [],
"name": "something19",
"returnType": {
"format": 257,
"kind": "simple",
"optional": false,
"type": "int",
},
},
{
"arguments": [],
"name": "minInt1",
"returnType": {
"format": 257,
"kind": "simple",
"optional": false,
"type": "int",
},
},
{
"arguments": [],
"name": "minInt2",
"returnType": {
"format": 257,
"kind": "simple",
"optional": false,
"type": "int",
},
},
{
"arguments": [],
"name": "minInt3",
"returnType": {
"format": 257,
"kind": "simple",
"optional": false,
"type": "int",
},
},
{
"arguments": [],
"name": "globalConst",
Expand Down Expand Up @@ -372,7 +442,7 @@ ConstantTester {
},
],
},
"address": kQDv7yerqTCJwMlp3JugEltyVuclWRTw9BhZNDWZbYViC4MM,
"address": kQBLPCFB6hZTJVoIVoSLp-zwDPnb5aQG3LnI_S3IHwDre99T,
"init": {
"code": x{FF00F4A413F4BCF2C80B}
x{62_}
Expand All @@ -381,26 +451,61 @@ ConstantTester {
x{6D}
x{0192307FE07021D749C21F953020D70B1FDEC00001D749C121B0917FE070}
x{2_}
x{6A_}
x{B22EF6CF36CF0C6_}
x{ED44D0D401F863D20030916DE0F828D70B0A8309BAF2E089DB3C}
x{6D}
x{74}
x{B226B6CF36CF0C6_}
x{ED44D0D401F863D20030916DE0F828D70B0A8309BAF2E089DB3C}
x{6D}
x{8D086002540B846967BD8AD0D9B17DFB2440228EB4C230FA9782F8A2D9B60428BEBB439C}
x{2_}
x{5}
x{2_}
x{2_}
x{B24DF6CF36CF0C6_}
x{AFD96D9E6D9E18C_}
x{ED44D0D401F863D20030916DE0F828D70B0A8309BAF2E089DB3C}
x{6D}
x{85FF}
x{AC5DED9E6D9E18C_}
x{ED44D0D401F863D20030916DE0F828D70B0A8309BAF2E089DB3C}
x{6D}
x{74}
x{2_}
x{AFC9ED9E6D9E18C_}
x{ED44D0D401F863D20030916DE0F828D70B0A8309BAF2E089DB3C}
x{6D}
x{82186A2BB7D000}
x{B245B6CF36CF0C6_}
x{82F70000000000000000000000000000000000000000000000000000000000000001}
x{AC4D6D9E6D9E18C_}
x{ED44D0D401F863D20030916DE0F828D70B0A8309BAF2E089DB3C}
x{6D}
x{8BC48656C6C6F20776F726C64218}
x{8D086002540B846967BD8AD0D9B17DFB2440228EB4C230FA9782F8A2D9B60428BEBB439C}
x{2_}
x{2_}
x{AFF86D9E6D9E18C_}
x{ED44D0D401F863D20030916DE0F828D70B0A8309BAF2E089DB3C}
x{6D}
x{85FF}
x{AF976D9E6D9E18C_}
x{ED44D0D401F863D20030916DE0F828D70B0A8309BAF2E089DB3C}
x{6D}
x{85FF}
x{B3C3F6CF36CF0C6_}
x{ED44D0D401F863D20030916DE0F828D70B0A8309BAF2E089DB3C}
x{6D}
x{82F70000000000000000000000000000000000000000000000000000000000000001}
x{2_}
x{2_}
x{2_}
x{5}
x{A937DB3CDB3C31}
x{ED44D0D401F863D20030916DE0F828D70B0A8309BAF2E089DB3C}
x{6D}
x{82186A2BB7D000}
x{AAE0DB3CDB3C31}
x{ED44D0D401F863D20030916DE0F828D70B0A8309BAF2E089DB3C}
x{6D}
x{84FF}
x{5}
x{A916DB3CDB3C31}
x{ED44D0D401F863D20030916DE0F828D70B0A8309BAF2E089DB3C}
x{6D}
x{8BC48656C6C6F20776F726C64218}
x{AAC1DB3CDB3C31}
x{ED44D0D401F863D20030916DE0F828D70B0A8309BAF2E089DB3C}
x{6D}
x{85FF}
x{2_}
x{5}
x{A975DB3CDB3C31}
Expand Down Expand Up @@ -441,7 +546,7 @@ ConstantTester {
x{ED44D0D401F863D20030916DE0F828D70B0A8309BAF2E089DB3C}
x{6D}
x{800B}
x{AEE3435697066733A2F2F516D6655625A6767516B31555175766B51474D566231474E31597639424B4A46783247787A586F6231397453615382_}
x{AEE3435697066733A2F2F516D5952584D7874336E78397331376A3874474D64315751526D574D6A556764756E75365536685A4C764758627182_}
x{AA45DB3CDB3C31}
x{ED44D0D401F863D20030916DE0F828D70B0A8309BAF2E089DB3C}
x{6D}
Expand Down Expand Up @@ -475,26 +580,61 @@ ConstantTester {
x{6D}
x{0192307FE07021D749C21F953020D70B1FDEC00001D749C121B0917FE070}
x{2_}
x{6A_}
x{B22EF6CF36CF0C6_}
x{ED44D0D401F863D20030916DE0F828D70B0A8309BAF2E089DB3C}
x{6D}
x{74}
x{B226B6CF36CF0C6_}
x{ED44D0D401F863D20030916DE0F828D70B0A8309BAF2E089DB3C}
x{6D}
x{8D086002540B846967BD8AD0D9B17DFB2440228EB4C230FA9782F8A2D9B60428BEBB439C}
x{2_}
x{5}
x{2_}
x{2_}
x{AFD96D9E6D9E18C_}
x{ED44D0D401F863D20030916DE0F828D70B0A8309BAF2E089DB3C}
x{6D}
x{85FF}
x{AC5DED9E6D9E18C_}
x{ED44D0D401F863D20030916DE0F828D70B0A8309BAF2E089DB3C}
x{6D}
x{74}
x{2_}
x{AFC9ED9E6D9E18C_}
x{ED44D0D401F863D20030916DE0F828D70B0A8309BAF2E089DB3C}
x{6D}
x{82F70000000000000000000000000000000000000000000000000000000000000001}
x{AC4D6D9E6D9E18C_}
x{ED44D0D401F863D20030916DE0F828D70B0A8309BAF2E089DB3C}
x{6D}
x{8D086002540B846967BD8AD0D9B17DFB2440228EB4C230FA9782F8A2D9B60428BEBB439C}
x{2_}
x{2_}
x{B24DF6CF36CF0C6_}
x{AFF86D9E6D9E18C_}
x{ED44D0D401F863D20030916DE0F828D70B0A8309BAF2E089DB3C}
x{6D}
x{82186A2BB7D000}
x{B245B6CF36CF0C6_}
x{85FF}
x{AF976D9E6D9E18C_}
x{ED44D0D401F863D20030916DE0F828D70B0A8309BAF2E089DB3C}
x{6D}
x{8BC48656C6C6F20776F726C64218}
x{85FF}
x{B3C3F6CF36CF0C6_}
x{ED44D0D401F863D20030916DE0F828D70B0A8309BAF2E089DB3C}
x{6D}
x{82F70000000000000000000000000000000000000000000000000000000000000001}
x{2_}
x{2_}
x{2_}
x{5}
x{A937DB3CDB3C31}
x{ED44D0D401F863D20030916DE0F828D70B0A8309BAF2E089DB3C}
x{6D}
x{82186A2BB7D000}
x{AAE0DB3CDB3C31}
x{ED44D0D401F863D20030916DE0F828D70B0A8309BAF2E089DB3C}
x{6D}
x{84FF}
x{5}
x{A916DB3CDB3C31}
x{ED44D0D401F863D20030916DE0F828D70B0A8309BAF2E089DB3C}
x{6D}
x{8BC48656C6C6F20776F726C64218}
x{AAC1DB3CDB3C31}
x{ED44D0D401F863D20030916DE0F828D70B0A8309BAF2E089DB3C}
x{6D}
x{85FF}
x{2_}
x{5}
x{A975DB3CDB3C31}
Expand Down Expand Up @@ -535,7 +675,7 @@ ConstantTester {
x{ED44D0D401F863D20030916DE0F828D70B0A8309BAF2E089DB3C}
x{6D}
x{800B}
x{AEE3435697066733A2F2F516D6655625A6767516B31555175766B51474D566231474E31597639424B4A46783247787A586F6231397453615382_}
x{AEE3435697066733A2F2F516D5952584D7874336E78397331376A3874474D64315751526D574D6A556764756E75365536685A4C764758627182_}
x{AA45DB3CDB3C31}
x{ED44D0D401F863D20030916DE0F828D70B0A8309BAF2E089DB3C}
x{6D}
Expand Down
4 changes: 2 additions & 2 deletions src/test/__snapshots__/feature-implicit-init.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`feature-send should deploy 1`] = `
exports[`feature-implicit-init should deploy 1`] = `
[
{
"$seq": 0,
Expand Down Expand Up @@ -53,7 +53,7 @@ exports[`feature-send should deploy 1`] = `
]
`;

exports[`feature-send should increment counter 1`] = `
exports[`feature-implicit-init should increment counter 1`] = `
[
{
"$seq": 1,
Expand Down
47 changes: 1 addition & 46 deletions src/test/__snapshots__/feature-intrinsics.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,52 +7,7 @@ exports[`feature-intrinsics should return correct intrinsic results 1`] = `
"events": [
{
"$type": "storage-charged",
"amount": "0.000000011",
},
{
"$type": "received",
"message": {
"body": {
"text": "emit_1",
"type": "text",
},
"bounce": true,
"from": "@treasure(treasure)",
"to": "@contract",
"type": "internal",
"value": "1",
},
},
{
"$type": "processed",
"gasUsed": 7881n,
},
{
"$type": "sent",
"messages": [
{
"body": {
"text": "Hello world",
"type": "text",
},
"to": null,
"type": "external-out",
},
],
},
],
},
]
`;

exports[`feature-intrinsics should return correct intrinsic results 1`] = `
[
{
"$seq": 1,
"events": [
{
"$type": "storage-charged",
"amount": "0.000000011",
"amount": "0.00000001",
},
{
"$type": "received",
Expand Down
Loading

0 comments on commit 6e1aa01

Please sign in to comment.