Skip to content

Commit

Permalink
fix: prevent shadowing of trait consts by contract vars (#480)
Browse files Browse the repository at this point in the history
  • Loading branch information
anton-trunov authored Jun 25, 2024
1 parent b851a35 commit a98d4f4
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Name clashes with FunC keywords in struct constructor function parameters: PR [#467](https://github.com/tact-lang/tact/issues/467)
- Error messages for traversing non-path-expressions in `foreach`-loops : PR [#479](https://github.com/tact-lang/tact/pull/479)
- Shadowing of trait constants by contract storage variables: PR [#480](https://github.com/tact-lang/tact/pull/480)

## [1.4.0] - 2024-06-21

Expand Down
10 changes: 10 additions & 0 deletions src/types/__snapshots__/resolveDescriptors.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,16 @@ Line 9, col 24:
"
`;
exports[`resolveDescriptors should fail descriptors for scope-storage-var-shadows-trait-const 1`] = `
"<unknown>:8:3: Contract Foo inherits constant "storageReserve" from its traits and hence cannot have a storage variable with the same name
Line 8, col 3:
7 | // is a virtual constant defined in BaseTrait
> 8 | storageReserve: Int;
^~~~~~~~~~~~~~~~~~~
9 |
"
`;
exports[`resolveDescriptors should fail descriptors for scope-struct-shadows-contract 1`] = `
"<unknown>:12:1: Type "Main" already exists
Line 12, col 1:
Expand Down
8 changes: 8 additions & 0 deletions src/types/resolveDescriptors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1402,6 +1402,14 @@ export function resolveDescriptors(ctx: CompilerContext) {
t.ast.ref,
);
}
const contractField = t.fields.find((v) => v.name === f.name);
if (contractField) {
// a trait constant has the same name as a contract field
throwCompilationError(
`Contract ${t.name} inherits constant "${f.name}" from its traits and hence cannot have a storage variable with the same name`,
contractField.ref,
);
}

// Register constant
t.constants.push({
Expand Down
13 changes: 13 additions & 0 deletions src/types/test-failed/scope-storage-var-shadows-trait-const.tact
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
primitive Int;

trait BaseTrait { virtual const storageReserve: Int = 0; }

contract Foo {
// this should be a var-scope error because storageReserve
// is a virtual constant defined in BaseTrait
storageReserve: Int;

init() {
self.storageReserve = ton("0.05");
}
}

0 comments on commit a98d4f4

Please sign in to comment.