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(typechecker): less permissive equality for optional types #650

Merged
merged 3 commits into from
Aug 2, 2024

Conversation

anton-trunov
Copy link
Member

Closes #649

  • 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 1, 2024
@anton-trunov anton-trunov self-assigned this Aug 1, 2024
@anton-trunov anton-trunov requested a review from Gusarich August 1, 2024 10:51
Copy link
Member

@Gusarich Gusarich left a comment

Choose a reason for hiding this comment

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

Why do we forbid comparing X and X?? It makes sense to allow comparing these two. Generated code would look like this:

  1. First we check whether X? is null and return false in this case because null cannot be equal to X
  2. If X? is not null, we can just compare them as X and X

Same for comparison of X? and X?: First we check if either both of these are null or both of these are not null and compare them accordingly, and in other cases (first is null and second is not or vice versa) we just return false

@anton-trunov
Copy link
Member Author

First we check whether X? is null and return false in this case because null cannot be equal to X

In general, equality comparisons only make sense for terms of the same type. The check you describe should be done by the user, e.g. if (x != null) { return x!! == y } where, for instance, x: Int? and y: Int.

Same for comparison of X? and X?: First we check if either both of these are null or both of these are not null and compare them accordingly, and in other cases (first is null and second is not or vice versa) we just return false

Look at the tests: we don't forbid equality comparisons for X? and X? in general, just for non-equality types like Builder or structs.

@anton-trunov
Copy link
Member Author

Why do we forbid comparing X and X?? It makes sense to allow comparing these two

wait, we don't actually forbid that -- we check assignability of types

so, my example above is not correct, because Int and Int? are both equality types

@anton-trunov
Copy link
Member Author

@Gusarich Added a test for that: https://github.com/tact-lang/tact/pull/650/files#diff-570728cab178f5e1fa5f1885b32ee9826e95de3a357d341adbc331578591ab1a

@anton-trunov anton-trunov merged commit bff157e into main Aug 2, 2024
3 checks passed
@anton-trunov anton-trunov deleted the fix-equality-for-optionals-typechecking branch August 2, 2024 08:25
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.

Optional types are always equality types even when the argument type is not
2 participants