Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: prevent variable declaration from shadowing functions #323

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

vitorpy
Copy link
Contributor

@vitorpy vitorpy commented May 3, 2024

Closes #307

Just checking if this is a sensible approach to handle avoiding variable declarations from shadowing functions.

I'm guessing I should also avoid shadowing other identifiers, such as contract names? Also non-static functions I guess.

  • I have updated CHANGELOG.md
  • I have added tests to demonstrate the contribution is correctly implemented: this usually includes both positive and negative tests, showing the happy path(s) and featuring intentionally broken cases
  • I have run all the tests locally and no test failure was reported
  • I did not do unrelated and/or undiscussed refactorings

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

}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks a bit like an ad hoc approach to me. For instance, would it work for contract functions? This should be solved with a unified variable context if you ask me. So, a bit of refactoring is needed here

@vitorpy vitorpy force-pushed the avoid-shadowing-functions branch from e33a35c to 2d32676 Compare May 13, 2024 07:56
@vitorpy
Copy link
Contributor Author

vitorpy commented May 24, 2024

@anton-trunov Sorry, can you provide some guidance here on which cases this is expected to cover? ie. which test cases are missing?

@@ -510,6 +510,16 @@ Line 8, col 12:
"
`;

exports[`resolveStatements should fail statements for case-51 1`] = `
"<unknown>:4:5: Variable already defined : rec
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message is a bit confusing here: it should say "function 'rec' blah blah"

@vitorpy
Copy link
Contributor Author

vitorpy commented Jun 2, 2024

@anton-trunov @novusnota Hey guys, should this be allowed?

image

@novusnota
Copy link
Member

Hey guys, should this be allowed?

@vitorpy Tact doesn't have first-class functions, so we can allow such declarations, I think. Let's wait for input from @anton-trunov though.

@anton-trunov
Copy link
Member

Tact doesn't have first-class functions, so we can allow such declarations

This is an instance of type-directed disambiguation and it's an opportunity to mess up. I say we go for simplicity and ban this, no shadowing should be allowed at this moment. I mean it's easier to ban some behavior and relax the restrictions in later language versions. And it's backwards compatible!

@anton-trunov
Copy link
Member

@vitorpy Let's disallow let sender: Address = sender()

@anton-trunov anton-trunov added this to the v1.4.0 milestone Jun 12, 2024
@anton-trunov anton-trunov self-assigned this Jun 12, 2024
@anton-trunov anton-trunov modified the milestones: v1.4.0, v1.5.0 Jun 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Function identifiers are allowed to be shadowed in their bodies
3 participants