Skip to content

Commit 1c14312

Browse files
authored
Fix try/catch would not pop excess scopes (#3216)
* Fix try/catch would not pop excess scopes * Set scope directly * Add test
1 parent a14a2c7 commit 1c14312

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
## SHOULD_PASS:EXECUTE
2+
@strict
3+
4+
try {
5+
error("A")
6+
} catch(A:string) {
7+
assert(A == "A")
8+
}
9+
10+
try {
11+
if(1) {
12+
error("B")
13+
}
14+
} catch(B:string) {
15+
assert(B == "B")
16+
}
17+
18+
if(1) {
19+
try {
20+
error("C")
21+
} catch(C:string) {
22+
assert(C == "C")
23+
}
24+
25+
try {
26+
if(1) {
27+
error("D")
28+
}
29+
} catch(D:string) {
30+
assert(D == "D")
31+
}
32+
33+
let Fn = function() { error("F") }
34+
35+
try {
36+
Fn()
37+
} catch(F:string) {
38+
assert(F == "F")
39+
}
40+
}

lua/entities/gmod_wire_expression2/base/compiler.lua

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -621,10 +621,13 @@ local CompileVisitors = {
621621
self.scope.data.ops = self.scope.data.ops + 5
622622

623623
return function(state) ---@param state RuntimeContext
624+
local scope, scope_id = state.Scope, state.ScopeID
624625
state:PushScope()
625626
local ok, err = pcall(try_block, state)
626-
state:PopScope()
627-
if not ok then
627+
if ok then
628+
state:PopScope()
629+
else
630+
state.Scope, state.ScopeID = scope, scope_id -- Skip back any scopes that may have been created in try_block
628631
local catchable, msg = E2Lib.unpackException(err)
629632
if catchable then
630633
state:PushScope()

0 commit comments

Comments
 (0)