Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix try/catch would not pop excess scopes #3216

Merged
merged 3 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -394,7 +394,7 @@

---@param data { [1]: Token<string>, [2]: Node, [3]: Node, [4]: Node?, [5]: Node } var start stop step block
[NodeVariant.For] = function (self, trace, data)
local var, start, stop, step = data[1], self:CompileExpr(data[2]), self:CompileExpr(data[3]), data[4] and self:CompileExpr(data[4]) or data[4]

Check warning on line 397 in lua/entities/gmod_wire_expression2/base/compiler.lua

View workflow job for this annotation

GitHub Actions / lint

"Unused variable"

Unused variable: step

local block = self:Scope(function(scope)
scope.data.loop = true
Expand Down Expand Up @@ -573,9 +573,9 @@

if state.__break__ then
state.__break__ = false
goto exit

Check warning on line 576 in lua/entities/gmod_wire_expression2/base/compiler.lua

View workflow job for this annotation

GitHub Actions / lint

"Goto"

Don't use labels and gotos unless you're jumping out of multiple loops.
elseif state.__return__ then -- Yes this should only be checked if the switch is inside a function, but I don't care enough about the performance of switch case to add another duplicated 30 lines to the file
goto exit

Check warning on line 578 in lua/entities/gmod_wire_expression2/base/compiler.lua

View workflow job for this annotation

GitHub Actions / lint

"Goto"

Don't use labels and gotos unless you're jumping out of multiple loops.
else -- Fallthrough, run every case until break found.
for j = i + 1, ncases do
cases[j][2](state)
Expand Down Expand Up @@ -621,10 +621,13 @@
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 Expand Up @@ -1031,9 +1034,9 @@
state.GlobalScope[var], state.GlobalScope.vclk[var] = val, true

if state.GlobalScope.lookup[val] then
state.GlobalScope.lookup[val][var] = true

Check warning on line 1037 in lua/entities/gmod_wire_expression2/base/compiler.lua

View workflow job for this annotation

GitHub Actions / lint

"Scope depth"

Are you Egyptian? What's with these fucking scope pyramids!?
else
state.GlobalScope.lookup[val] = { [var] = true }

Check warning on line 1039 in lua/entities/gmod_wire_expression2/base/compiler.lua

View workflow job for this annotation

GitHub Actions / lint

"Scope depth"

Are you Egyptian? What's with these fucking scope pyramids!?
end
end
else
Expand Down Expand Up @@ -1829,7 +1832,7 @@
if fn.attributes.legacy then
local largs = { [1] = {}, [nargs + 2] = arg_types }
for i = 1, nargs do
largs[i + 1] = { [1] = function() return rargs[i] end }

Check warning on line 1835 in lua/entities/gmod_wire_expression2/base/compiler.lua

View workflow job for this annotation

GitHub Actions / lint

"Scope depth"

Are you Egyptian? What's with these fucking scope pyramids!?
end
return fn[3](state, largs, arg_types)
elseif varsig == "array(...)" then -- Need this since can't enforce compile time argument type restrictions on string calls. Woop. Array creation should not be a function..
Expand All @@ -1837,11 +1840,11 @@
while i <= #arg_types do
local ty = arg_types[i]
if BLOCKED_ARRAY_TYPES[ty] then
table.remove(rargs, i)

Check warning on line 1843 in lua/entities/gmod_wire_expression2/base/compiler.lua

View workflow job for this annotation

GitHub Actions / lint

"Scope depth"

Are you Egyptian? What's with these fucking scope pyramids!?
table.remove(arg_types, i)
state:forceThrow("Cannot use type " .. ty .. " for argument #" .. i .. " in stringcall array creation")
else
i = i + 1

Check warning on line 1847 in lua/entities/gmod_wire_expression2/base/compiler.lua

View workflow job for this annotation

GitHub Actions / lint

"Scope depth"

Are you Egyptian? What's with these fucking scope pyramids!?
end
end
end
Expand All @@ -1854,7 +1857,7 @@
if fn then
for _, ty in ipairs(arg_types) do -- Just block them entirely. Current method of finding variadics wouldn't allow a proper solution that works with x<yz> types. Would need to rewrite all of this which I don't think is worth it when already nobody is going to use this functionality.
if BLOCKED_ARRAY_TYPES[ty] then
state:forceThrow("Cannot pass array into variadic array function")

Check warning on line 1860 in lua/entities/gmod_wire_expression2/base/compiler.lua

View workflow job for this annotation

GitHub Actions / lint

"Scope depth"

Are you Egyptian? What's with these fucking scope pyramids!?
end
end

Expand Down
Loading