Skip to content

Commit

Permalink
Added test case for function parameter shadowing a constant.
Browse files Browse the repository at this point in the history
  • Loading branch information
jeshecdom committed Aug 12, 2024
1 parent c7c435a commit 03bee2e
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1066,6 +1066,17 @@ export class Interpreter {
const paramNames = functionCode.params.map((param) =>
idText(param.name),
);
// Check parameter names do not shadow constants
if (
paramNames.some((paramName) =>
hasStaticConstant(this.context, paramName),
)
) {
throwInternalCompilerError(
`some parameter of function ${idText(functionCode.name)} shadows a constant with the same name`,
functionCode.loc,
);
}
// Call function inside a new environment
return this.envStack.executeInNewEnvironment(
() => {
Expand Down
5 changes: 5 additions & 0 deletions src/test/compilation-failed/const-eval-failed.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,9 @@ describe("fail-const-eval", () => {
errorMessage:
"[INTERNAL COMPILER ERROR]: declaration of C shadows a constant with the same name",
});
itShouldNotCompile({
testName: "const-eval-constant-shadowing-in-fun-argument",
errorMessage:
"[INTERNAL COMPILER ERROR]: some parameter of function shadowingFunction shadows a constant with the same name",
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const C: Int = 0;

fun shadowingFunction(C: Int): Int {
return C;
}

contract ConstEvalNegativeTest {
get fun something(): Int {
return shadowingFunction(2);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const C: Int = 0;

fun shadowingFunction(): Int {
let C: Int = 3;
return C;
}

contract ConstEvalNegativeTest {
get fun something(): Int {
return shadowingFunction();
}
}
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 @@ -191,6 +191,11 @@
"name": "const-eval-constant-shadowing-in-fun",
"path": "./contracts/const-eval-constant-shadowing-in-fun.tact",
"output": "./contracts/output"
},
{
"name": "const-eval-constant-shadowing-in-fun-argument",
"path": "./contracts/const-eval-constant-shadowing-in-fun-argument.tact",
"output": "./contracts/output"
}
]
}

0 comments on commit 03bee2e

Please sign in to comment.