Skip to content

Commit 686aa5f

Browse files
CopilotCppCXY
andcommitted
Move readonly-for-loop-vars check from diagnostic to parser stage
Co-authored-by: CppCXY <[email protected]>
1 parent a55f5e7 commit 686aa5f

File tree

10 files changed

+66
-84
lines changed

10 files changed

+66
-84
lines changed

locale/en-us/script.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ DIAG_UNUSED_VARARG =
2424
'Unused vararg.'
2525
DIAG_REDEFINED_LOCAL =
2626
'Redefined local `{}`.'
27-
DIAG_READONLY_FOR_LOOP_VAR =
28-
'Cannot assign to for-loop variable `{}` (read-only in Lua 5.5).'
2927
DIAG_DUPLICATE_INDEX =
3028
'Duplicate index `{}`.'
3129
DIAG_DUPLICATE_METHOD =
@@ -295,6 +293,8 @@ PARSER_MISS_SEP_IN_TABLE =
295293
'Miss symbol `,` or `;` .'
296294
PARSER_SET_CONST =
297295
'Assignment to const variable.'
296+
PARSER_SET_FOR_LOOP_VAR =
297+
'Cannot assign to for-loop variable `{}` (read-only in Lua 5.5).'
298298
PARSER_UNICODE_NAME =
299299
'Contains Unicode characters.'
300300
PARSER_ERR_NONSTANDARD_SYMBOL =

locale/es-419/script.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,8 @@ PARSER_MISS_SEP_IN_TABLE =
291291
'Falta el símbolo `,` ó `;` .'
292292
PARSER_SET_CONST =
293293
'Asignación de valor a una variable constante.'
294+
PARSER_SET_FOR_LOOP_VAR =
295+
'No se puede asignar a la variable de bucle for `{}` (solo lectura en Lua 5.5).'
294296
PARSER_UNICODE_NAME =
295297
'Contiene caracteres Unicode.'
296298
PARSER_ERR_NONSTANDARD_SYMBOL =

locale/ja-jp/script.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,8 @@ PARSER_MISS_SEP_IN_TABLE =
293293
'区切りには `,` または `;` が必要です。'
294294
PARSER_SET_CONST =
295295
'定数に値を代入できません。'
296+
PARSER_SET_FOR_LOOP_VAR =
297+
'forループ変数`{}`に代入できません(Lua 5.5では読み取り専用)。'
296298
PARSER_UNICODE_NAME =
297299
'Unicode 文字が含まれています。'
298300
PARSER_ERR_NONSTANDARD_SYMBOL =

locale/pt-br/script.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,8 @@ PARSER_MISS_SEP_IN_TABLE =
293293
'Falta o símbolo `,` ou `;` .'
294294
PARSER_SET_CONST =
295295
'Atribuição à variável constante.'
296+
PARSER_SET_FOR_LOOP_VAR =
297+
'Não é possível atribuir à variável de loop for `{}` (somente leitura no Lua 5.5).'
296298
PARSER_UNICODE_NAME =
297299
'Contém caracteres Unicode.'
298300
PARSER_ERR_NONSTANDARD_SYMBOL =

locale/zh-cn/script.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,8 @@ PARSER_MISS_SEP_IN_TABLE =
295295
'需要用`,`或`;`进行分割。'
296296
PARSER_SET_CONST =
297297
'不能对常量赋值。'
298+
PARSER_SET_FOR_LOOP_VAR =
299+
'不能对for循环变量`{}`赋值(在Lua 5.5中只读)。'
298300
PARSER_UNICODE_NAME =
299301
'包含了 Unicode 字符。'
300302
PARSER_ERR_NONSTANDARD_SYMBOL =

locale/zh-tw/script.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,8 @@ PARSER_MISS_SEP_IN_TABLE =
293293
'需要用 `,` 或 `;` 進行分割。'
294294
PARSER_SET_CONST =
295295
'不能對常數賦值。'
296+
PARSER_SET_FOR_LOOP_VAR =
297+
'不能對for迴圈變數`{}`賦值(在Lua 5.5中唯讀)。'
296298
PARSER_UNICODE_NAME =
297299
'包含了 Unicode 字元。'
298300
PARSER_ERR_NONSTANDARD_SYMBOL =

script/core/diagnostics/readonly-for-loop-vars.lua

Lines changed: 0 additions & 69 deletions
This file was deleted.

script/parser/compile.lua

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,36 @@ local function addSpecial(name, obj)
263263
obj.special = name
264264
end
265265

266+
---@param local parser.object
267+
---@return boolean
268+
local function isForLoopVariable(local_)
269+
-- Check if this local is a for-loop variable
270+
if not local_ or local_.type ~= 'local' then
271+
return false
272+
end
273+
274+
local parent = local_.parent
275+
if not parent then
276+
return false
277+
end
278+
279+
-- Check if parent is a numeric for-loop
280+
if parent.type == 'loop' and parent.loc == local_ then
281+
return true
282+
end
283+
284+
-- Check if parent is a for-in loop
285+
if parent.type == 'in' and parent.keys then
286+
for i = 1, #parent.keys do
287+
if parent.keys[i] == local_ then
288+
return true
289+
end
290+
end
291+
end
292+
293+
return false
294+
end
295+
266296
---@param offset integer
267297
---@param leftOrRight '"left"'|'"right"'
268298
local function getPosition(offset, leftOrRight)
@@ -2921,6 +2951,15 @@ local function bindValue(n, v, index, lastValue, isLocal, isSet)
29212951
start = n.start,
29222952
finish = n.finish,
29232953
}
2954+
elseif State.version == 'Lua 5.5' and isForLoopVariable(loc) then
2955+
pushError {
2956+
type = 'SET_FOR_LOOP_VAR',
2957+
start = n.start,
2958+
finish = n.finish,
2959+
info = {
2960+
name = loc[1],
2961+
},
2962+
}
29242963
end
29252964
end
29262965
end
@@ -4011,6 +4050,15 @@ function parseAction()
40114050
start = name.start,
40124051
finish = name.finish,
40134052
}
4053+
elseif State.version == 'Lua 5.5' and isForLoopVariable(loc) then
4054+
pushError {
4055+
type = 'SET_FOR_LOOP_VAR',
4056+
start = name.start,
4057+
finish = name.finish,
4058+
info = {
4059+
name = loc[1],
4060+
},
4061+
}
40144062
end
40154063
end
40164064
pushActionIntoCurrentChunk(name)

script/proto/diagnostic.lua

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -174,14 +174,6 @@ m.register {
174174
status = 'Opened',
175175
}
176176

177-
m.register {
178-
'readonly-for-loop-vars',
179-
} {
180-
group = 'readonly',
181-
severity = 'Error',
182-
status = 'Opened',
183-
}
184-
185177
m.register {
186178
'undefined-global',
187179
'global-in-nil-env',

test/diagnostics/readonly-for-loop-vars.lua

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
-- Test for readonly for-loop variables in Lua 5.5
2+
-- Note: These now produce parser errors instead of diagnostics
23

34
TEST [[
45
---@language Lua 5.5
56
for i = 1, 10 do
6-
<!i!> = 5 -- Error: Cannot assign to for-loop variable
7+
i = 5 -- This is now a parser error, so no diagnostic
78
end
89
]]
910

1011
TEST [[
1112
---@language Lua 5.5
1213
for k, v in pairs(t) do
13-
<!k!> = "new" -- Error: Cannot assign to for-loop variable
14-
<!v!> = 123 -- Error: Cannot assign to for-loop variable
14+
k = "new" -- This is now a parser error, so no diagnostic
15+
v = 123 -- This is now a parser error, so no diagnostic
1516
end
1617
]]
1718

1819
TEST [[
1920
---@language Lua 5.5
2021
for i = 1, 10 do
2122
for j = 1, 5 do
22-
<!i!> = j -- Error: Cannot assign to outer for-loop variable
23-
<!j!> = i -- Error: Cannot assign to inner for-loop variable
23+
i = j -- This is now a parser error, so no diagnostic
24+
j = i -- This is now a parser error, so no diagnostic
2425
end
2526
end
2627
]]

0 commit comments

Comments
 (0)