Laythe-lang / Laythe

A gradually typed language originally based on the crafting interpreters series
MIT License
65 stars 4 forks source link

Exception Handler Popping issue #133

Closed jonnyboyC closed 1 year ago

jonnyboyC commented 1 year ago

Problem

Right now there is an issue with pushing and popping exception handlers. Right now we only really handle two cases

try {
  // stuff 
  // <- pop here
} catch {
  // <- pop here
  // stuff
}

At first this seems fine we pop the handler at the end of the try catch block and we pop it at the begin of the catch if an exception is handled. The issue is we can exit this block in other ways

try {
  // stuff 
  return 10;
  // <- pop here
} catch { 
  // <- pop here
  // stuff
}

In this case we return prior to popping the exception handler.

Additional we have a similar case with continue and break

while true {
  try {
    // stuff 
    continue; // or break
    // <- pop here
  } catch { 
    // <- pop here
    // stuff
  }
}

Here continue is worse because we can build up a large number of exception handlers that never get popped. This issue materialized because we had a case where an inner exception handler wasn't popped because of a return. Later an exception was through inside the scope of another handler but the first handler was used and attempted to index out of bounds.