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: type-checking for foreach loops in trait methods #1017

Merged
merged 2 commits into from
Nov 6, 2024

Conversation

anton-trunov
Copy link
Member

@anton-trunov anton-trunov commented Nov 5, 2024

Issue

Closes #971.

Checklist

  • 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 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.6.0 milestone Nov 5, 2024
@anton-trunov anton-trunov requested a review from a team as a code owner November 5, 2024 11:08
@anton-trunov anton-trunov self-assigned this Nov 6, 2024
@anton-trunov anton-trunov merged commit 85416ae into main Nov 6, 2024
17 checks passed
@anton-trunov anton-trunov deleted the foreach-in-trait-bug branch November 6, 2024 08:46
Copy link
Contributor

@jeshecdom jeshecdom left a comment

Choose a reason for hiding this comment

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

Ok, I get it now. resolveDescriptors.ts copies inherited trait functions into the contract. This means that before the fix for the foreach statement in the cloneNode function, resolveDescriptors.ts would duplicate the expression id of the map expression in foreach statements inside functions in the contract. This would later produce the error in resolveExpression.ts as follows (taking the code from the test):

trait TraitWithForeach {
    m: map<Int, Int>;

    fun test() { foreach(_, _ in self.m) { } }
}


contract Test with TraitWithForeach {
    m: map<Int, Int>;
}

The test function in the trait gets copied into contract Test, but with the problem that the expression self.m has the same expression id for the subexpression self in both functions (the one in the trait and the copy in the contract). Hence, when resolveExpression registers the type of self when processing function test in the trait, it does so with type TraitWithForEach; but when it processes the copy of test in the contract, it attempts to register self again (because self has the same expression id), but this time with type Test, producing the error.

This is why changing the code like the following does not produce the error, because let statements correctly clone expressions:

trait TraitWithForeach {
    m: map<Int, Int>;

    fun test() { let a = self.m; foreach(_, _ in a) { } }
}


contract Test with TraitWithForeach {
    m: map<Int, Int>;
}

github-actions bot pushed a commit to TownSquareXYZ/tact that referenced this pull request Nov 7, 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.

Tact internal compile error: expression 2079 already has a type
2 participants