Skip to content

Commit 1e71838

Browse files
authored
fix: handle return statements in init function separately (#794)
1 parent cf57aa0 commit 1e71838

File tree

5 files changed

+59
-1
lines changed

5 files changed

+59
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3636
- Tact AST keeps the original format of integer literals (hex/dec/oct/bin): PR [#771](https://github.com/tact-lang/tact/pull/771)
3737
- Message opcodes are now checked if they fit into 32 bits: PR [#771](https://github.com/tact-lang/tact/pull/771)
3838
- Disallow zero binary message opcodes as those are reserved for text messages: PR [#786](https://github.com/tact-lang/tact/pull/786)
39+
- Return-statements in `init()` function do not cause FunC compilation error anymore: PR [#794](https://github.com/tact-lang/tact/pull/794)
3940

4041
## [1.4.4] - 2024-08-18
4142

src/generator/writers/writeContract.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,11 @@ export function writeInit(
146146
// Generate statements
147147
const returns = resolveFuncTypeUnpack(t, funcIdOf("self"), ctx);
148148
for (const s of init.ast.statements) {
149-
writeStatement(s, returns, null, ctx);
149+
if (s.kind === "statement_return") {
150+
ctx.append(`return ${returns};`);
151+
} else {
152+
writeStatement(s, returns, null, ctx);
153+
}
150154
}
151155

152156
// Return result
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
contract Test {
2+
a: Int;
3+
4+
init() {
5+
self.a = 123;
6+
return;
7+
}
8+
9+
receive () {}
10+
11+
get fun a(): Int {
12+
return self.a;
13+
}
14+
}
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { toNano } from "@ton/core";
2+
import { Blockchain, SandboxContract, TreasuryContract } from "@ton/sandbox";
3+
import { Test } from "./contracts/output/init-return_Test";
4+
import "@ton/test-utils";
5+
6+
describe("init-return", () => {
7+
let blockchain: Blockchain;
8+
let treasure: SandboxContract<TreasuryContract>;
9+
let contract: SandboxContract<Test>;
10+
11+
beforeEach(async () => {
12+
blockchain = await Blockchain.create();
13+
treasure = await blockchain.treasury("treasure");
14+
15+
contract = blockchain.openContract(await Test.fromInit());
16+
17+
const deployResult = await contract.send(
18+
treasure.getSender(),
19+
{ value: toNano("10") },
20+
null,
21+
);
22+
23+
expect(deployResult.transactions).toHaveTransaction({
24+
from: treasure.address,
25+
to: contract.address,
26+
success: true,
27+
deploy: true,
28+
});
29+
});
30+
31+
it("should deploy with return statement in init", async () => {
32+
expect(await contract.getA()).toEqual(123n);
33+
});
34+
});

tact.config.json

+5
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,11 @@
395395
"name": "init-of-message",
396396
"path": "./src/test/e2e-emulated/contracts/init-of-message.tact",
397397
"output": "./src/test/e2e-emulated/contracts/output"
398+
},
399+
{
400+
"name": "init-return",
401+
"path": "./src/test/e2e-emulated/contracts/init-return.tact",
402+
"output": "./src/test/e2e-emulated/contracts/output"
398403
}
399404
]
400405
}

0 commit comments

Comments
 (0)