aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2024-09-27 10:00:35 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2024-09-27 10:00:35 -0300
commite4f418f07c7349f5ff844fbdc9a3b37b488113a5 (patch)
tree113af8ef98acb336f39ec984fbc9121d234ac2dd
parent20d42ccaaed9a84783d548d76633a5a38f0091f1 (diff)
downloadlua-e4f418f07c7349f5ff844fbdc9a3b37b488113a5.tar.gz
lua-e4f418f07c7349f5ff844fbdc9a3b37b488113a5.tar.bz2
lua-e4f418f07c7349f5ff844fbdc9a3b37b488113a5.zip
Local declaration in the REPL generates a warning
-rw-r--r--lua.c18
-rw-r--r--testes/main.lua9
2 files changed, 25 insertions, 2 deletions
diff --git a/lua.c b/lua.c
index 9693ad68..ea6141bb 100644
--- a/lua.c
+++ b/lua.c
@@ -587,15 +587,28 @@ static int addreturn (lua_State *L) {
587} 587}
588 588
589 589
590static 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
590/* 602/*
591** Read multiple lines until a complete Lua statement or an error not 603** Read multiple lines until a complete Lua statement or an error not
592** for an incomplete statement. Start with first line already read in 604** for an incomplete statement. Start with first line already read in
593** the stack. 605** the stack.
594*/ 606*/
595static int multiline (lua_State *L) { 607static int multiline (lua_State *L) {
608 size_t len;
609 const char *line = lua_tolstring(L, 1, &len); /* get first line */
610 checklocal(line);
596 for (;;) { /* repeat until gets a complete statement */ 611 for (;;) { /* repeat until gets a complete statement */
597 size_t len;
598 const char *line = lua_tolstring(L, 1, &len); /* get what it has */
599 int status = luaL_loadbuffer(L, line, len, "=stdin"); /* try it */ 612 int status = luaL_loadbuffer(L, line, len, "=stdin"); /* try it */
600 if (!incomplete(L, status) || !pushline(L, 0)) 613 if (!incomplete(L, status) || !pushline(L, 0))
601 return status; /* should not or cannot try to add continuation line */ 614 return status; /* should not or cannot try to add continuation line */
@@ -603,6 +616,7 @@ static int multiline (lua_State *L) {
603 lua_pushliteral(L, "\n"); /* add newline... */ 616 lua_pushliteral(L, "\n"); /* add newline... */
604 lua_insert(L, -2); /* ...between the two lines */ 617 lua_insert(L, -2); /* ...between the two lines */
605 lua_concat(L, 3); /* join them */ 618 lua_concat(L, 3); /* join them */
619 line = lua_tolstring(L, 1, &len); /* get what is has */
606 } 620 }
607} 621}
608 622
diff --git a/testes/main.lua b/testes/main.lua
index 7b0f4ee0..1aa7b217 100644
--- a/testes/main.lua
+++ b/testes/main.lua
@@ -263,6 +263,15 @@ assert(string.find(getoutput(), "error calling 'print'"))
263RUN('echo "io.stderr:write(1000)\ncont" | lua -e "require\'debug\'.debug()" 2> %s', out) 263RUN('echo "io.stderr:write(1000)\ncont" | lua -e "require\'debug\'.debug()" 2> %s', out)
264checkout("lua_debug> 1000lua_debug> ") 264checkout("lua_debug> 1000lua_debug> ")
265 265
266do -- 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"))
274end
266 275
267print("testing warnings") 276print("testing warnings")
268 277