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

FunC codegen error for obvious cases of zero division in statements #716

Open
anton-trunov opened this issue Aug 19, 2024 · 2 comments · May be fixed by #852
Open

FunC codegen error for obvious cases of zero division in statements #716

anton-trunov opened this issue Aug 19, 2024 · 2 comments · May be fixed by #852
Assignees
Labels
bug Something isn't working or isn't right func scope: const-eval The constant and partial evaluation optimization mechanisms
Milestone

Comments

@anton-trunov
Copy link
Member

contract Test {

    a: Int = 0;

    init() {
        self.a / self.a;
        // self. a / 0 would fail too
    }

    get fun foo(): Int {
        let x = 42 - 42;
        return 42 / x;   // this also fails
    }
}

Results in the following error message from FunC:

Func compilation error: cannot generate code for function `$Test$_contract_init`:
/dist/tact_Test.code.fc:15:14: error: division by zero
($self'a / $self'a)

As far as I understand this happens during code generation when going from FunC to Fift-asm.

To resolve this, we need to track variables whose values can be determined at compile-time.

@anton-trunov anton-trunov added bug Something isn't working or isn't right scope: const-eval The constant and partial evaluation optimization mechanisms labels Aug 19, 2024
@jeshecdom
Copy link
Contributor

Mmmm... the main problem I see here is that const-eval (or the interpreter) is only called to simplify expressions. For example, inside function foo, it is called to simplify the argument of the return. const-eval will not execute function foo because there is no expression calling function foo. As such, it will treat variable x in the return statement as uninterpreted.

To solve this issue, we would need some form of code analysis outside of const-eval. Another possibility would be to attempt calling functions that do not have parameters, just to check if its execution generates an error.
Probably, we will need an analysis/optimization pass after the typecheck pass, because currently, optimization and interpretation is only carried out on pieces of the code (in expressions, to be exact).

@anton-trunov
Copy link
Member Author

To solve this issue, we would need some form of code analysis outside of const-eval.

To solve this issue you need to partially evaluate contracts (I almost always mean partial evaluation or constant evaluation by "const-eval", sorry if that's confusing)

@anton-trunov anton-trunov added this to the v1.5.0 milestone Sep 5, 2024
@anton-trunov anton-trunov modified the milestones: v1.5.0, v1.6.0 Sep 15, 2024
@jeshecdom jeshecdom linked a pull request Sep 19, 2024 that will close this issue
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working or isn't right func scope: const-eval The constant and partial evaluation optimization mechanisms
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants