Skip to content

Commit

Permalink
fix: check for getter method ids collisions (#875)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gusarich authored Sep 25, 2024
1 parent 213bf42 commit e69c7fc
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- Collisions in getter method ids are now handled and reported properly: PR [#875](https://github.com/tact-lang/tact/pull/875)

### Release contributors

## [1.5.2] - 2024-09-25
Expand Down
33 changes: 33 additions & 0 deletions src/types/__snapshots__/resolveDescriptors.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,39 @@ Line 4, col 17:
"
`;
exports[`resolveDescriptors should fail descriptors for getter-collision 1`] = `
"<unknown>:8:13: Method ID collision: getter 'pko' has the same method ID 103057 as getter 'getter1'
Pick a different getter name to avoid collisions
Line 8, col 13:
7 |
> 8 | get fun pko() {
^~~
9 | return;
"
`;
exports[`resolveDescriptors should fail descriptors for getter-collision-trait 1`] = `
"<unknown>:8:13: Method ID collision: getter 'pko' has the same method ID 103057 as getter 'getter1'
Pick a different getter name to avoid collisions
Line 8, col 13:
7 |
> 8 | get fun pko() {
^~~
9 | return;
"
`;
exports[`resolveDescriptors should fail descriptors for getter-collision-with-trait 1`] = `
"<unknown>:4:13: Method ID collision: getter 'pko' has the same method ID 103057 as getter 'getter1'
Pick a different getter name to avoid collisions
Line 4, col 13:
3 | trait T {
> 4 | get fun pko() {
^~~
5 | return;
"
`;
exports[`resolveDescriptors should fail descriptors for getter-outside-contract 1`] = `
"<unknown>:8:1: Getters must be defined within a contract
Line 8, col 1:
Expand Down
20 changes: 20 additions & 0 deletions src/types/resolveDescriptors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import { ItemOrigin } from "../grammar/grammar";
import { getExpType, resolveExpression } from "./resolveExpression";
import { emptyContext } from "./resolveStatements";
import { isAssignable } from "./subtyping";
import { getMethodId } from "../utils/utils";

const store = createContextStore<TypeDescription>();
const staticFunctionsStore = createContextStore<FunctionDescription>();
Expand Down Expand Up @@ -1916,6 +1917,25 @@ export function resolveDescriptors(ctx: CompilerContext) {
staticConstants.set(idText(a.name), buildConstantDescription(a));
}

// Check for collisions in getter method IDs
for (const t of types.values()) {
const methodIds: Map<number, string> = new Map();
for (const f of t.functions.values()) {
if (f.isGetter) {
const methodId = getMethodId(f.name);
const existing = methodIds.get(methodId);
if (existing) {
throwCompilationError(
`Method ID collision: getter '${f.name}' has the same method ID ${methodId} as getter '${existing}'\nPick a different getter name to avoid collisions`,
f.ast.name.loc,
);
} else {
methodIds.set(methodId, f.name);
}
}
}
}

//
// Register types and functions in context
//
Expand Down
11 changes: 11 additions & 0 deletions src/types/test-failed/getter-collision-trait.tact
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
trait BaseTrait { }

trait T {
get fun getter1() {
return;
}

get fun pko() {
return;
}
}
13 changes: 13 additions & 0 deletions src/types/test-failed/getter-collision-with-trait.tact
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
trait BaseTrait { }

trait T {
get fun pko() {
return;
}
}

contract TestContract with T {
get fun getter1() {
return;
}
}
11 changes: 11 additions & 0 deletions src/types/test-failed/getter-collision.tact
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
trait BaseTrait { }

contract TestContract {
get fun getter1() {
return;
}

get fun pko() {
return;
}
}

0 comments on commit e69c7fc

Please sign in to comment.