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

Nested calls to the same function produces incorrect code #3

Open
GearsDatapacks opened this issue Nov 5, 2024 · 3 comments
Open
Labels
bug Something isn't working

Comments

@GearsDatapacks
Copy link
Member

This code:

namespace example

fn add(a, b) {
  return a + b
}

fn load() {
  add(1, add(2, 3))
}

Generates:

data modify storage example:add a set value 1
data modify storage example:add a set value 2
data modify storage example:add b set value 3
data modify storage example:add return set value false
function example:add
data modify storage example:add b set from storage example:add return
function example:add

Which causes the outer function's a parameter to be set to 2, resulting in an incorrect result.
We should probably handle inner function calls first, but that's more complex than it might sound, because it also needs to take into account calls in other expressions:

add(1, 2 + add(3, 4))
@GearsDatapacks GearsDatapacks added the bug Something isn't working label Nov 5, 2024
@GearsDatapacks
Copy link
Member Author

Another thing to consider:

add(add(1, 2), add(3, 4))

No matter which order we call the inner functions in, one of the arguments to the outer function will be incorrect. We need to store some intermediate variable

@moxvallix
Copy link
Contributor

Yeah I guess for functions called within a function argument they need to store their result in to temp variables.

@GearsDatapacks
Copy link
Member Author

For now we could just use a naive approach, where any nested calls to the same function would be stored in a temp variable, and then optimise it later. There probably are not many cases we can optimise anyway

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants