flint icon indicating copy to clipboard operation
flint copied to clipboard

Support Early Returns

Open franklinsch opened this issue 7 years ago • 2 comments

Currently, an if-else block cannot have an early return in either body.

if ... {
  return 
} else {
}
// more code

franklinsch avatar Jan 25 '18 22:01 franklinsch

How can we do this in IULIA?

franklinsch avatar Apr 23 '18 13:04 franklinsch

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:

  1. Support early returns only when there are no local variables
  2. 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.
  3. Don't support early returns in any sense
  4. 🌟 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.

DJRHails avatar Jul 06 '18 14:07 DJRHails