From 1c0896e47cf1f9ea2f5e18bb680b201dc91894c1 Mon Sep 17 00:00:00 2001 From: Denneisk <20892685+Denneisk@users.noreply.github.com> Date: Thu, 12 Dec 2024 16:37:35 -0500 Subject: [PATCH 1/3] Fix try/catch would not pop excess scopes --- lua/entities/gmod_wire_expression2/base/compiler.lua | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lua/entities/gmod_wire_expression2/base/compiler.lua b/lua/entities/gmod_wire_expression2/base/compiler.lua index 202faba047..93d7cba0f6 100644 --- a/lua/entities/gmod_wire_expression2/base/compiler.lua +++ b/lua/entities/gmod_wire_expression2/base/compiler.lua @@ -621,9 +621,12 @@ local CompileVisitors = { self.scope.data.ops = self.scope.data.ops + 5 return function(state) ---@param state RuntimeContext + local depth = state.ScopeID state:PushScope() local ok, err = pcall(try_block, state) - state:PopScope() + while state.ScopeID > depth do -- Dump all scopes that may have been created in between + state:PopScope() + end if not ok then local catchable, msg = E2Lib.unpackException(err) if catchable then From 65440ca920484587758cda6eb3c0069cdb00aaa3 Mon Sep 17 00:00:00 2001 From: Denneisk <20892685+Denneisk@users.noreply.github.com> Date: Thu, 12 Dec 2024 18:02:45 -0500 Subject: [PATCH 2/3] Set scope directly --- lua/entities/gmod_wire_expression2/base/compiler.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lua/entities/gmod_wire_expression2/base/compiler.lua b/lua/entities/gmod_wire_expression2/base/compiler.lua index 93d7cba0f6..8b9ddef424 100644 --- a/lua/entities/gmod_wire_expression2/base/compiler.lua +++ b/lua/entities/gmod_wire_expression2/base/compiler.lua @@ -621,13 +621,13 @@ local CompileVisitors = { self.scope.data.ops = self.scope.data.ops + 5 return function(state) ---@param state RuntimeContext - local depth = state.ScopeID + local scope, scope_id = state.Scope, state.ScopeID state:PushScope() local ok, err = pcall(try_block, state) - while state.ScopeID > depth do -- Dump all scopes that may have been created in between + if ok then state:PopScope() - end - if not ok then + 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() From efcbc0decc3063dcd6c09cd94aa7386baac0d5a5 Mon Sep 17 00:00:00 2001 From: Denneisk <20892685+Denneisk@users.noreply.github.com> Date: Thu, 12 Dec 2024 18:02:50 -0500 Subject: [PATCH 3/3] Add test --- data/expression2/tests/regressions/3080.txt | 40 +++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 data/expression2/tests/regressions/3080.txt diff --git a/data/expression2/tests/regressions/3080.txt b/data/expression2/tests/regressions/3080.txt new file mode 100644 index 0000000000..e53317eff2 --- /dev/null +++ b/data/expression2/tests/regressions/3080.txt @@ -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") + } +}