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

max() function results in compilation failure due to alleged unreachable code #38

Open
m1cm1c opened this issue Oct 26, 2021 · 2 comments

Comments

@m1cm1c
Copy link

m1cm1c commented Oct 26, 2021

Steps to reproduce

Paste the following function into any XJSnark program:

public uint_256 max(uint_256 a, uint_256 b) { 
  if (a > b) { 
    return a; 
  } else { 
    return b; 
  } 
}

The bug occurs regardless of whether this function is ever invoked.

It results in the following compilation problem:

@akosba
Copy link
Owner

akosba commented Oct 26, 2021

This is not related to a specific function. Writing return statements within if statements that rely on a runtime conditional is not supported at this point. This can be avoided by writing a single return at the end.
I pushed an update that generates an error message in the editor directly to indicate that this is not supported.

@m1cm1c
Copy link
Author

m1cm1c commented Oct 27, 2021

Thank you! Storing the result in a variable and simply returning that variable works.

If return statements are difficult, maybe you could instead support the ternary operator for XJSnark types. My initial attempt was:

return a > b ? a : b;

This avoids a return in a conditional block perfectly well and it seems to me that return statements are often written using the ternary operator because a case differentiation involving an if statement for the last statement of a function is unnecessarily cumbersome. However, the problem is that this currently yields the following error message:

Error: Boolean and JBoolean are inconsistent types.

As the error message implies, this is not dependent on the resulting value being used in a return statement.

uint_256 c = a > b ? a : b;

yields the exact same error message.

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

No branches or pull requests

2 participants