FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Small change in scope of variables in repeat-until · lua/lua@7579fc9 · GitHub

/ lua Public

Commit 7579fc9

Browse files
committed
Small change in scope of variables in repeat-until
A close instruction is still inside the scope of the variables it is closing. The extra close in a repeat-until (to close variables before repeating the loop) was being coded outside that scope.
1 parent d5bbe95 commit 7579fc9

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

‎lparser.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1614,7 +1614,6 @@ static void repeatstat (LexState *ls, int line) {
16141614
statlist(ls);
16151615
check_match(ls, TK_UNTIL, TK_REPEAT, line);
16161616
condexit = cond(ls); /* read condition (inside scope block) */
1617-
leaveblock(fs); /* finish scope */
16181617
if (bl2.upval) { /* upvalues? */
16191618
int exit = luaK_jump(fs); /* normal exit must jump over fix */
16201619
luaK_patchtohere(fs, condexit); /* repetition must close upvalues */
@@ -1623,6 +1622,7 @@ static void repeatstat (LexState *ls, int line) {
16231622
luaK_patchtohere(fs, exit); /* normal exit comes to here */
16241623
}
16251624
luaK_patchlist(fs, condexit, repeat_init); /* close the loop */
1625+
leaveblock(fs); /* finish scope */
16261626
leaveblock(fs); /* finish loop */
16271627
}
16281628

‎testes/locals.lua‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,6 +1179,25 @@ if rawget(_G, "T") then
11791179
end
11801180

11811181

1182+
do
1183+
-- detail in scopes of variables in the loop of 'repeat-until'
1184+
local res = {}
1185+
local function foo (a)
1186+
repeat
1187+
local x
1188+
local i <close> = setmetatable({}, {__close = function ()
1189+
res[#res + 1] = debug.getlocal(2, 2) -- get 'x'
1190+
res[#res + 1] = debug.getlocal(2, 3) -- get 'i'
1191+
a = true
1192+
end})
1193+
until a
1194+
end
1195+
foo(false)
1196+
-- loop variables still in scope when closing 'i', both when 'repeat'
1197+
-- repeats and when 'repeat' stops.
1198+
assert(res[1] == "x" and res[2] == "i" and res[3] == "x" and res[4] == "i")
1199+
end
1200+
11821201

11831202
-- to-be-closed variables in generic for loops
11841203
do

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL