Skip to content

Commit e9f09e0

Browse files
JJH090501hishamhm
authored andcommitted
compat: test numeric for control variable shadowing
1 parent 825854e commit e9f09e0

1 file changed

Lines changed: 97 additions & 0 deletions

File tree

spec/lang/code_gen/fornum_spec.lua

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
local util = require("spec.util")
2+
3+
describe("fornum", function()
4+
it("5.3: doesn't generate control variable that is local to the iteration", util.gen([[
5+
local t: {string} = { "a", "b", "c" }
6+
7+
for i = 1, #t do
8+
i = i + 1
9+
print(t[i])
10+
end
11+
]], [[
12+
local t = { "a", "b", "c" }
13+
14+
for i = 1, #t do
15+
i = i + 1
16+
print(t[i])
17+
end
18+
]], "5.3"))
19+
20+
it("5.4: generates control variable that is local to the iteration", util.gen([[
21+
local t: {string} = { "a", "b", "c" }
22+
23+
for i = 1, #t do
24+
i = i + 1
25+
print(t[i])
26+
end
27+
]], [[
28+
local t = { "a", "b", "c" }
29+
30+
for i = 1, #t do local i = i
31+
i = i + 1
32+
print(t[i])
33+
end
34+
]], "5.4"))
35+
36+
it("5.4: does not generate control variable if not assigned to", util.gen([[
37+
local t: {string} = { "a", "b", "c" }
38+
39+
for i = 1, #t do
40+
local j = i + 1
41+
print(t[j])
42+
end
43+
]], [[
44+
local t = { "a", "b", "c" }
45+
46+
for i = 1, #t do
47+
local j = i + 1
48+
print(t[j])
49+
end
50+
]], "5.4"))
51+
52+
it("5.4: generates control variable for a loop with a step", util.gen([[
53+
for i = 10, 1, -2 do
54+
i = i // 2
55+
print(i)
56+
end
57+
]], [[
58+
for i = 10, 1, -2 do local i = i
59+
i = i // 2
60+
print(i)
61+
end
62+
]], "5.4"))
63+
64+
it("5.4: detects an assignment made from a nested function", util.gen([[
65+
for i = 1, 3 do
66+
local function bump()
67+
i = i + 1
68+
end
69+
bump()
70+
print(i)
71+
end
72+
]], [[
73+
for i = 1, 3 do local i = i
74+
local function bump()
75+
i = i + 1
76+
end
77+
bump()
78+
print(i)
79+
end
80+
]], "5.4"))
81+
82+
it("5.4: only shadows the loops that are assigned to", util.gen([[
83+
for i = 1, 3 do
84+
for j = 1, 3 do
85+
j = j + 1
86+
print(i, j)
87+
end
88+
end
89+
]], [[
90+
for i = 1, 3 do
91+
for j = 1, 3 do local j = j
92+
j = j + 1
93+
print(i, j)
94+
end
95+
end
96+
]], "5.4"))
97+
end)

0 commit comments

Comments
 (0)