Skip to content

Commit

Permalink
fix: better error message if constant shadows stdlib identifier
Browse files Browse the repository at this point in the history
  • Loading branch information
anton-trunov committed Aug 15, 2024
1 parent 4ff20ad commit ade351c
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Parsing of optional nested struct fields does not cause the `Not a tuple` error anymore: PR [#692](https://github.com/tact-lang/tact/pull/692)
- Disallow shadowing of recursive function names: PR [#693](https://github.com/tact-lang/tact/pull/693)
- Better error message for the case when a constant shadows an stdlib identifier: PR [#694](https://github.com/tact-lang/tact/pull/694)

## [1.4.2] - 2024-08-13

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const b: Int = 42;
14 changes: 14 additions & 0 deletions src/test/compilation-failed/scope-errors.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { __DANGER_resetNodeId } from "../../grammar/ast";
import { itShouldNotCompile } from "./util";

describe("scope-errors", () => {
beforeEach(() => {
__DANGER_resetNodeId();
});

itShouldNotCompile({
testName: "scope-const-shadows-stdlib-ident",
errorMessage:
'Constant "b" is shadowing an identifier defined in the Tact standard library: pick a different constant name',
});
});
5 changes: 5 additions & 0 deletions src/test/compilation-failed/tact.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@
"name": "func-function-does-not-exist",
"path": "./contracts/func-function-does-not-exist.tact",
"output": "./contracts/output"
},
{
"name": "scope-const-shadows-stdlib-ident",
"path": "./contracts/scope-const-shadows-stdlib-ident.tact",
"output": "./contracts/output"
}
]
}
17 changes: 13 additions & 4 deletions src/types/resolveStatements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
import {
getAllStaticFunctions,
getAllTypes,
getStaticConstant,
getType,
hasStaticConstant,
resolveTypeRef,
Expand Down Expand Up @@ -68,10 +69,18 @@ function checkVariableExists(
);
}
if (hasStaticConstant(ctx, idText(name))) {
throwCompilationError(
`Variable ${idTextErr(name)} is trying to shadow an existing constant with the same name`,
name.loc,
);
if (name.loc.origin === "stdlib") {
const constLoc = getStaticConstant(ctx, idText(name)).loc;
throwCompilationError(
`Constant ${idTextErr(name)} is shadowing an identifier defined in the Tact standard library: pick a different constant name`,
constLoc,
);
} else {
throwCompilationError(
`Variable ${idTextErr(name)} is trying to shadow an existing constant with the same name`,
name.loc,
);
}
}
}

Expand Down

0 comments on commit ade351c

Please sign in to comment.