flint
flint copied to clipboard
Support Early Returns
Currently, an if-else block cannot have an early return in either body.
if ... {
return
} else {
}
// more code
How can we do this in IULIA?
Interesting rabbit hole:
Originally the thought was IULIA/YUL can support this using labels - which are now depreciated. However Jumping doesn't update stack reference, this means that if any local variables are used within an if/else block the jump to the end of the function breaks everything.
We are therefore left with a few options as far as I can see:
- Support early returns only when there are no local variables
- Add a boolean at the start of every function - isReturned - which is set on ret := being updated. Then at the exit of every if/else block it would check isReturned, and jump to end of current scope if set. Recursing up until finally the end of scope is the function end.
- Don't support early returns in any sense
- 🌟 Think of some clever way to track how many local variables have been added, then POP before jumping.
- Update: Probably the best way to "hack" this feature would be to have a function root variable that gets incremented as the stack increases then pop that many times before jumping.