Skip to content

Commit

Permalink
Local declaration in the REPL generates a warning
Browse files Browse the repository at this point in the history
  • Loading branch information
roberto-ieru committed Sep 27, 2024
1 parent 20d42cc commit e4f418f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
18 changes: 16 additions & 2 deletions lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -587,22 +587,36 @@ static int addreturn (lua_State *L) {
}


static void checklocal (const char *line) {
static const size_t szloc = sizeof("local") - 1;
static const char space[] = " \t";
line += strspn(line, space); /* skip spaces */
if (strncmp(line, "local", szloc) == 0 && /* "local"? */
strchr(space, *(line + szloc)) != NULL) { /* followed by a space? */
lua_writestringerror("%s\n",
"warning: locals do not survive across lines in interactive mode");
}
}


/*
** Read multiple lines until a complete Lua statement or an error not
** for an incomplete statement. Start with first line already read in
** the stack.
*/
static int multiline (lua_State *L) {
size_t len;
const char *line = lua_tolstring(L, 1, &len); /* get first line */
checklocal(line);
for (;;) { /* repeat until gets a complete statement */
size_t len;
const char *line = lua_tolstring(L, 1, &len); /* get what it has */
int status = luaL_loadbuffer(L, line, len, "=stdin"); /* try it */
if (!incomplete(L, status) || !pushline(L, 0))
return status; /* should not or cannot try to add continuation line */
lua_remove(L, -2); /* remove error message (from incomplete line) */
lua_pushliteral(L, "\n"); /* add newline... */
lua_insert(L, -2); /* ...between the two lines */
lua_concat(L, 3); /* join them */
line = lua_tolstring(L, 1, &len); /* get what is has */
}
}

Expand Down
9 changes: 9 additions & 0 deletions testes/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,15 @@ assert(string.find(getoutput(), "error calling 'print'"))
RUN('echo "io.stderr:write(1000)\ncont" | lua -e "require\'debug\'.debug()" 2> %s', out)
checkout("lua_debug> 1000lua_debug> ")

do -- test warning for locals
RUN('echo " local x" | lua -i > %s 2>&1', out)
assert(string.find(getoutput(), "warning: "))

RUN('echo "local1 = 10\nlocal1 + 3" | lua -i > %s 2>&1', out)
local t = getoutput()
assert(not string.find(t, "warning"))
assert(string.find(t, "13"))
end

print("testing warnings")

Expand Down

0 comments on commit e4f418f

Please sign in to comment.