Skip to content

Commit e4f418f

Browse files
committed
Local declaration in the REPL generates a warning
1 parent 20d42cc commit e4f418f

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

lua.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -587,22 +587,36 @@ static int addreturn (lua_State *L) {
587587
}
588588

589589

590+
static void checklocal (const char *line) {
591+
static const size_t szloc = sizeof("local") - 1;
592+
static const char space[] = " \t";
593+
line += strspn(line, space); /* skip spaces */
594+
if (strncmp(line, "local", szloc) == 0 && /* "local"? */
595+
strchr(space, *(line + szloc)) != NULL) { /* followed by a space? */
596+
lua_writestringerror("%s\n",
597+
"warning: locals do not survive across lines in interactive mode");
598+
}
599+
}
600+
601+
590602
/*
591603
** Read multiple lines until a complete Lua statement or an error not
592604
** for an incomplete statement. Start with first line already read in
593605
** the stack.
594606
*/
595607
static int multiline (lua_State *L) {
608+
size_t len;
609+
const char *line = lua_tolstring(L, 1, &len); /* get first line */
610+
checklocal(line);
596611
for (;;) { /* repeat until gets a complete statement */
597-
size_t len;
598-
const char *line = lua_tolstring(L, 1, &len); /* get what it has */
599612
int status = luaL_loadbuffer(L, line, len, "=stdin"); /* try it */
600613
if (!incomplete(L, status) || !pushline(L, 0))
601614
return status; /* should not or cannot try to add continuation line */
602615
lua_remove(L, -2); /* remove error message (from incomplete line) */
603616
lua_pushliteral(L, "\n"); /* add newline... */
604617
lua_insert(L, -2); /* ...between the two lines */
605618
lua_concat(L, 3); /* join them */
619+
line = lua_tolstring(L, 1, &len); /* get what is has */
606620
}
607621
}
608622

testes/main.lua

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,15 @@ assert(string.find(getoutput(), "error calling 'print'"))
263263
RUN('echo "io.stderr:write(1000)\ncont" | lua -e "require\'debug\'.debug()" 2> %s', out)
264264
checkout("lua_debug> 1000lua_debug> ")
265265

266+
do -- test warning for locals
267+
RUN('echo " local x" | lua -i > %s 2>&1', out)
268+
assert(string.find(getoutput(), "warning: "))
269+
270+
RUN('echo "local1 = 10\nlocal1 + 3" | lua -i > %s 2>&1', out)
271+
local t = getoutput()
272+
assert(not string.find(t, "warning"))
273+
assert(string.find(t, "13"))
274+
end
266275

267276
print("testing warnings")
268277

0 commit comments

Comments
 (0)