Skip to content

Commit

Permalink
fix: handle return statements in init function separately (#794)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gusarich authored Sep 5, 2024
1 parent cf57aa0 commit 1e71838
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Tact AST keeps the original format of integer literals (hex/dec/oct/bin): PR [#771](https://github.com/tact-lang/tact/pull/771)
- Message opcodes are now checked if they fit into 32 bits: PR [#771](https://github.com/tact-lang/tact/pull/771)
- Disallow zero binary message opcodes as those are reserved for text messages: PR [#786](https://github.com/tact-lang/tact/pull/786)
- Return-statements in `init()` function do not cause FunC compilation error anymore: PR [#794](https://github.com/tact-lang/tact/pull/794)

## [1.4.4] - 2024-08-18

Expand Down
6 changes: 5 additions & 1 deletion src/generator/writers/writeContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,11 @@ export function writeInit(
// Generate statements
const returns = resolveFuncTypeUnpack(t, funcIdOf("self"), ctx);
for (const s of init.ast.statements) {
writeStatement(s, returns, null, ctx);
if (s.kind === "statement_return") {
ctx.append(`return ${returns};`);
} else {
writeStatement(s, returns, null, ctx);
}
}

// Return result
Expand Down
14 changes: 14 additions & 0 deletions src/test/e2e-emulated/contracts/init-return.tact
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
contract Test {
a: Int;

init() {
self.a = 123;
return;
}

receive () {}

get fun a(): Int {
return self.a;
}
}
34 changes: 34 additions & 0 deletions src/test/e2e-emulated/init-return.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { toNano } from "@ton/core";
import { Blockchain, SandboxContract, TreasuryContract } from "@ton/sandbox";
import { Test } from "./contracts/output/init-return_Test";
import "@ton/test-utils";

describe("init-return", () => {
let blockchain: Blockchain;
let treasure: SandboxContract<TreasuryContract>;
let contract: SandboxContract<Test>;

beforeEach(async () => {
blockchain = await Blockchain.create();
treasure = await blockchain.treasury("treasure");

contract = blockchain.openContract(await Test.fromInit());

const deployResult = await contract.send(
treasure.getSender(),
{ value: toNano("10") },
null,
);

expect(deployResult.transactions).toHaveTransaction({
from: treasure.address,
to: contract.address,
success: true,
deploy: true,
});
});

it("should deploy with return statement in init", async () => {
expect(await contract.getA()).toEqual(123n);
});
});
5 changes: 5 additions & 0 deletions tact.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,11 @@
"name": "init-of-message",
"path": "./src/test/e2e-emulated/contracts/init-of-message.tact",
"output": "./src/test/e2e-emulated/contracts/output"
},
{
"name": "init-return",
"path": "./src/test/e2e-emulated/contracts/init-return.tact",
"output": "./src/test/e2e-emulated/contracts/output"
}
]
}

0 comments on commit 1e71838

Please sign in to comment.