Skip to content

Commit 72ec6f0

Browse files
committed
In Lua 5.5, for loop variables are treated as constants
1 parent e84f77c commit 72ec6f0

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

script/parser/compile.lua

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3548,7 +3548,22 @@ local function parseFor()
35483548
LocalCount = LocalCount + forStateVars
35493549
if name then
35503550
---@cast name parser.object
3551-
local loc = createLocal(name)
3551+
-- In Lua 5.5, for loop variables are treated as constants
3552+
local attrs
3553+
if State.version == 'Lua 5.5' then
3554+
attrs = {
3555+
type = 'localattrs',
3556+
[1] = {
3557+
type = 'localattr',
3558+
start = name.start,
3559+
finish = name.finish,
3560+
parent = nil, -- will be set by createLocal
3561+
[1] = 'const',
3562+
}
3563+
}
3564+
attrs[1].parent = attrs
3565+
end
3566+
local loc = createLocal(name, attrs)
35523567
loc.parent = action
35533568
action.finish = name.finish
35543569
action.bstart = action.finish
@@ -3653,7 +3668,22 @@ local function parseFor()
36533668
for i = 1, #list do
36543669
local obj = list[i]
36553670
---@cast obj parser.object
3656-
local loc = createLocal(obj)
3671+
-- In Lua 5.5, for loop variables are treated as constants
3672+
local attrs
3673+
if State.version == 'Lua 5.5' then
3674+
attrs = {
3675+
type = 'localattrs',
3676+
[1] = {
3677+
type = 'localattr',
3678+
start = obj.start,
3679+
finish = obj.finish,
3680+
parent = nil, -- will be set by createLocal
3681+
[1] = 'const',
3682+
}
3683+
}
3684+
attrs[1].parent = attrs
3685+
end
3686+
local loc = createLocal(obj, attrs)
36573687
loc.parent = action
36583688
loc.effect = action.finish
36593689
end

0 commit comments

Comments
 (0)