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

fix: check new variable bindings do not shadow global constants #680

Merged
merged 1 commit into from
Aug 13, 2024

Conversation

anton-trunov
Copy link
Member

Closes #676

  • I have updated CHANGELOG.md
  • [ ] I have documented my contribution in Tact Docs: https://github.com/tact-lang/tact-docs/pull/PR-NUMBER
  • 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 have run the linter, formatter and spellchecker
  • I did not do unrelated and/or undiscussed refactorings

@anton-trunov anton-trunov added this to the v1.4.2 milestone Aug 13, 2024
@anton-trunov anton-trunov self-assigned this Aug 13, 2024
@anton-trunov
Copy link
Member Author

Please let me know if there are any issues in this PR and I'll happily fix those in a follow-up, but now I'm going to merge this one to fix the other issues for v1.4.2

@anton-trunov anton-trunov merged commit 8eb102c into main Aug 13, 2024
3 checks passed
@anton-trunov anton-trunov deleted the fix-const-shadowing branch August 13, 2024 08:51
): StatementContext {
checkVariableExists(src, name); // Should happen earlier
checkVariableExists(ctx, sctx, name); // Should happen earlier
if (isWildcard(name)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Ohhh, interesting. I did not check in the interpreter the use of wildcard identifiers (to be honest, I was not aware of them). Where is the wildcard _ allowed in Tact? Anywhere where an identifier is allowed? I looked in the grammar and it seems that _ is allowed by the id rule. _ allows you to ignore that identifier, right? I am asking this for the interpreter :)

Copy link
Member Author

Choose a reason for hiding this comment

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

Right, underscores (_) means an identifier that cannot be used (so the shadowing rules do not apply to them). You can use it as a function parameter, in catch-blocks, foreach-loops, unused let-bindings.

@@ -197,7 +209,7 @@ function processStatements(
ctx = resolveExpression(s.expression, sctx, ctx);

// Check variable name
checkVariableExists(sctx, s.name);
checkVariableExists(ctx, sctx, s.name);
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there any reason as to why call checkVariableExists at this point if later addVariable will call it in both branches of the conditional? I guess to give the user the variable duplication and shadowing errors as soon as possible.

Copy link
Member Author

Choose a reason for hiding this comment

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

I actually didn't think about this, I just added ctx mechanically here.

@@ -486,11 +498,12 @@ function processStatements(
let catchCtx = sctx;

// Process catchName variable for exit code
checkVariableExists(initialCtx, s.catchName);
checkVariableExists(ctx, initialSctx, s.catchName);
Copy link
Contributor

Choose a reason for hiding this comment

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

Again. Why call checkVariableExists if addVariable is the first thing that it does in the next line?

Copy link
Member Author

Choose a reason for hiding this comment

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

We could try refactor this, although I'm not sure how useful this is going to be since we are going to replace the existing typechecker with a new one

Copy link
Contributor

Choose a reason for hiding this comment

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

Agreed. I think there is no need to refactor.

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.

Constant shadowing does not produce var-scope error
2 participants