Skip to content

Commit

Permalink
fix: allow returning self from getters (#666)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gusarich authored Aug 16, 2024
1 parent 14d3052 commit 2b29403
Show file tree
Hide file tree
Showing 19 changed files with 696 additions and 357 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- Returning `self` from getters is now allowed: PR [#666](https://github.com/tact-lang/tact/pull/666)
- Remainder fields in the middle of a struct are now forbidden: PR [#697](https://github.com/tact-lang/tact/pull/697)
- Defining two native functions from the same FunC function now does not fail compilation: PR [#699](https://github.com/tact-lang/tact/pull/699)

Expand Down
3 changes: 2 additions & 1 deletion cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@
"привет",
"PUSHREF",
"PUSHSLICE",
"SETINDEXVARQ"
"SETINDEXVARQ",
"gettest"
],
"flagWords": [],
"ignorePaths": [
Expand Down
10 changes: 8 additions & 2 deletions src/generator/createABI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ export function createABI(ctx: CompilerContext, name: string): ContractABI {
header: t.header,
fields: t.fields.map((v) => v.abi),
});
} else if (t.kind === "contract") {
types.push({
name: t.name + "$Data",
header: t.header,
fields: t.fields.map((v) => v.abi),
});
}
}

Expand Down Expand Up @@ -119,11 +125,11 @@ export function createABI(ctx: CompilerContext, name: string): ContractABI {
name: f.name,
arguments: f.params.map((v) => ({
name: idText(v.name),
type: createABITypeRefFromTypeRef(v.type, v.loc),
type: createABITypeRefFromTypeRef(ctx, v.type, v.loc),
})),
returnType:
f.returns.kind !== "void"
? createABITypeRefFromTypeRef(f.returns, f.ast.loc)
? createABITypeRefFromTypeRef(ctx, f.returns, f.ast.loc)
: null,
});
}
Expand Down
5 changes: 4 additions & 1 deletion src/generator/writeReport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ export function writeReport(ctx: CompilerContext, pkg: PackageFileFormat) {
w.write("Total Types: " + abi.types!.length);
w.append();
for (const t of abi.types!) {
const tt = getType(ctx, t.name);
const tt = getType(
ctx,
t.name.endsWith("$Data") ? t.name.slice(0, -5) : t.name,
);
w.write(`## ${t.name}`);
w.write(`TLB: \`${tt.tlb!}\``);
w.write(`Signature: \`${tt.signature!}\``);
Expand Down
2 changes: 1 addition & 1 deletion src/generator/writers/resolveFuncTupleType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export function resolveFuncTupleType(
} else {
throw Error("Unknown primitive type: " + descriptor.name);
}
} else if (descriptor.kind === "struct") {
} else if (descriptor.kind === "struct" || descriptor.kind === "contract") {
return "tuple";
}

Expand Down
4 changes: 2 additions & 2 deletions src/generator/writers/writeFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function unwrapExternal(
) {
if (type.kind === "ref") {
const t = getType(ctx.ctx, type.name);
if (t.kind === "struct") {
if (t.kind === "struct" || t.kind === "contract") {
if (type.optional) {
ctx.append(
`${resolveFuncType(type, ctx)} ${targetName} = ${ops.typeFromOptTuple(t.name, ctx)}(${sourceName});`,
Expand Down Expand Up @@ -658,7 +658,7 @@ export function writeGetter(f: FunctionDescription, ctx: WriterContext) {
// Pack if needed
if (f.returns.kind === "ref") {
const t = getType(ctx.ctx, f.returns.name);
if (t.kind === "struct") {
if (t.kind === "struct" || t.kind === "contract") {
if (f.returns.optional) {
ctx.append(
`return ${ops.typeToOptExternal(t.name, ctx)}(res);`,
Expand Down
2 changes: 1 addition & 1 deletion src/pipeline/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ export async function build(args: {
kind: "direct",
args: getType(ctx, contract).init!.params.map((v) => ({
name: idText(v.name),
type: createABITypeRefFromTypeRef(v.type, v.loc),
type: createABITypeRefFromTypeRef(ctx, v.type, v.loc),
})),
prefix: {
bits: 1,
Expand Down
2 changes: 1 addition & 1 deletion src/storage/resolveAllocation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export function resolveAllocations(ctx: CompilerContext) {
// Resolve opts
const ops: AllocationOperation[] = [];
for (const f of s.init.params) {
const abiType = createABITypeRefFromTypeRef(f.type, f.loc);
const abiType = createABITypeRefFromTypeRef(ctx, f.type, f.loc);
ops.push({
name: idText(f.name),
type: abiType,
Expand Down
5 changes: 5 additions & 0 deletions src/test/e2e-emulated/__snapshots__/constants.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,11 @@ ConstantTester {
"header": null,
"name": "T",
},
{
"fields": [],
"header": null,
"name": "ConstantTester$Data",
},
],
},
"address": kQDrQDJFeHEKbkV6TKFO-KhFpeMRJ3cEZgIrvDQYPUyzm4CN,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,21 @@ exports[`strings should implement external fallbacks correctly 1`] = `
"header": 2412401973,
"name": "Add",
},
{
"fields": [
{
"name": "a",
"type": {
"format": 257,
"kind": "simple",
"optional": false,
"type": "int",
},
},
],
"header": null,
"name": "ExternalFallbacksTester$Data",
},
],
}
`;
Loading

0 comments on commit 2b29403

Please sign in to comment.