Skip to content

Commit

Permalink
feat: prevent variable declaration from shadowing functions
Browse files Browse the repository at this point in the history
  • Loading branch information
vitorpy committed May 3, 2024
1 parent 6d9dbc1 commit e33a35c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/types/__snapshots__/resolveStatements.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,16 @@ Line 4, col 1:
"
`;
exports[`resolveStatements should fail statements for case-47 1`] = `
"<unknown>:4:5: Variable shadows existing function: rec
Line 4, col 5:
3 | fun rec(): Int {
> 4 | let rec: Int = 42; // shadowing error
^~~~~~~~~~~~~~~~~~
5 | return rec;
"
`;
exports[`resolveStatements should resolve statements for case-0 1`] = `
[
[
Expand Down
5 changes: 5 additions & 0 deletions src/types/resolveStatements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,11 @@ function processStatements(
);
}

const staticFunction = getAllStaticFunctions(ctx);
if (Object.values(staticFunction).some((f) => f.name === s.name)) {
throwError(`Variable shadows existing function: ${s.name}`, s.ref);

}
// Add variable to statement context
if (sctx.vars.has(s.name)) {
throwError(`Variable already exists: ${s.name}`, s.ref);
Expand Down
6 changes: 6 additions & 0 deletions src/types/stmts-failed/case-47.tact
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
primitive Int;

fun rec(): Int {
let rec: Int = 42; // shadowing error
return rec;
}

0 comments on commit e33a35c

Please sign in to comment.