Skip to content

Commit

Permalink
Fix try/catch would not pop excess scopes (#3216)
Browse files Browse the repository at this point in the history
* Fix try/catch would not pop excess scopes

* Set scope directly

* Add test
  • Loading branch information
Denneisk authored Dec 16, 2024
1 parent a14a2c7 commit 1c14312
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
40 changes: 40 additions & 0 deletions data/expression2/tests/regressions/3080.txt
Original file line number Diff line number Diff line change
@@ -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")
}
}
7 changes: 5 additions & 2 deletions lua/entities/gmod_wire_expression2/base/compiler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 1c14312

Please sign in to comment.