Skip to content

Commit

Permalink
feat: allow optional types for self in extends mutates
Browse files Browse the repository at this point in the history
  • Loading branch information
Gusarich committed Sep 19, 2024
1 parent b384573 commit 1680829
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 24 deletions.
1 change: 1 addition & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"decompiling",
"dentry",
"Descr",
"DICTUSET",
"disasm",
"divmod",
"dnsresolve",
Expand Down
7 changes: 0 additions & 7 deletions src/generator/writers/writeExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -558,13 +558,6 @@ export function writeExpression(f: AstExpression, wCtx: WriterContext): string {

// Reference type
if (selfTyRef.kind === "ref") {
if (selfTyRef.optional) {
throwCompilationError(
`Cannot call function of non - direct type: "${printTypeRef(selfTyRef)}"`,
f.loc,
);
}

// Render function call
const selfTy = getType(wCtx.ctx, selfTyRef.name);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
asm(value key self keySize) extends mutates fun nativeUdictStoreUint(self: Cell?, keySize: Int, key: Int, value: Slice) { DICTUSET }

extends mutates fun multiply(self: Int, x: Int): Int {
self *= x;
return self;
Expand Down Expand Up @@ -64,4 +66,9 @@ contract Tester {
self.s.loadUint(1);
return self.s.loadUint(3);
}

get fun test10(dict: Cell?): Cell? {
dict.nativeUdictStoreUint(8, 123, rawSlice("456"));
return dict;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { toNano } from "@ton/core";
import { beginCell, BitString, Dictionary, toNano } from "@ton/core";
import { Blockchain, SandboxContract, TreasuryContract } from "@ton/sandbox";
import { Tester } from "./contracts/output/mutating-method-chaining_Tester";
import { Tester } from "./contracts/output/mutating-methods_Tester";
import "@ton/test-utils";

describe("bugs", () => {
Expand Down Expand Up @@ -52,5 +52,38 @@ describe("bugs", () => {
expect(await contract.getTest7()).toBe(42n);
expect(await contract.getTest8()).toBe(5n);
expect(await contract.getTest9()).toBe(5n);

// Test `extends mutates` function with optional self param
{
// Non-empty dictionary
const d = Dictionary.empty(
Dictionary.Keys.Uint(8),
Dictionary.Values.BitString(12),
);
d.set(1, new BitString(Buffer.from("1234", "hex"), 0, 12));
const c = beginCell().storeDictDirect(d).endCell();
const c2 = await contract.getTest10(c);
const d2 = c2
?.beginParse()
.loadDictDirect(
Dictionary.Keys.Uint(8),
Dictionary.Values.BitString(12),
);
expect(d2?.size).toBe(2);
expect(d2?.get(1)?.toString()).toBe("123");
expect(d2?.get(123)?.toString()).toBe("456");
}
{
// Empty dictionary
const c = await contract.getTest10(null);
const d = c
?.beginParse()
.loadDictDirect(
Dictionary.Keys.Uint(8),
Dictionary.Values.BitString(12),
);
expect(d?.size).toBe(1);
expect(d?.get(123)?.toString()).toBe("456");
}
});
});
6 changes: 0 additions & 6 deletions src/types/resolveDescriptors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -817,12 +817,6 @@ export function resolveDescriptors(ctx: CompilerContext) {
firstParam.loc,
);
}
if (firstParam.type.optional) {
throwCompilationError(
"Extend functions must have a non-optional type as the first parameter",
firstParam.loc,
);
}
if (!types.has(firstParam.type.name)) {
throwCompilationError(
"Type " + firstParam.type.name + " not found",
Expand Down
7 changes: 0 additions & 7 deletions src/types/resolveExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -562,13 +562,6 @@ function resolveCall(

// Handle ref
if (src.kind === "ref") {
if (src.optional) {
throwCompilationError(
`Invalid type "${printTypeRef(src)}" for function call`,
exp.loc,
);
}

// Register return type
const srcT = getType(ctx, src.name);

Expand Down
4 changes: 2 additions & 2 deletions tact.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@
"output": "./src/test/e2e-emulated/contracts/output"
},
{
"name": "mutating-method-chaining",
"path": "./src/test/e2e-emulated/contracts/mutating-method-chaining.tact",
"name": "mutating-methods",
"path": "./src/test/e2e-emulated/contracts/mutating-methods.tact",
"output": "./src/test/e2e-emulated/contracts/output"
},
{
Expand Down

0 comments on commit 1680829

Please sign in to comment.