Fix try/catch would not pop excess scopes (#3216)

* Fix try/catch would not pop excess scopes

* Set scope directly

* Add test
This commit is contained in:
Denneisk 2024-12-16 05:10:22 +00:00 committed by GitHub
parent a14a2c7b78
commit 1c14312b14
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 45 additions and 2 deletions

View File

@ -0,0 +1,40 @@
## SHOULD_PASS:EXECUTE
@strict
try {
error("A")
} catch(A:string) {
assert(A == "A")
}
try {
if(1) {
error("B")
}
} catch(B:string) {
assert(B == "B")
}
if(1) {
try {
error("C")
} catch(C:string) {
assert(C == "C")
}
try {
if(1) {
error("D")
}
} catch(D:string) {
assert(D == "D")
}
let Fn = function() { error("F") }
try {
Fn()
} catch(F:string) {
assert(F == "F")
}
}

View File

@ -621,10 +621,13 @@ local CompileVisitors = {
self.scope.data.ops = self.scope.data.ops + 5
return function(state) ---@param state RuntimeContext
local scope, scope_id = state.Scope, state.ScopeID
state:PushScope()
local ok, err = pcall(try_block, state)
state:PopScope()
if not ok then
if ok then
state:PopScope()
else
state.Scope, state.ScopeID = scope, scope_id -- Skip back any scopes that may have been created in try_block
local catchable, msg = E2Lib.unpackException(err)
if catchable then
state:PushScope()