Better error messages for invalid operands in numeric 'for' · lua/lua@e073cbc · GitHub
Skip to content

Commit e073cbc

Browse files
committed
Better error messages for invalid operands in numeric 'for'
"Better" and similar to error messages for invalid function arguments. *old message: 'for' limit must be a number *new message: bad 'for' limit (number expected, got table)
1 parent 2316ec4 commit e073cbc

4 files changed

Lines changed: 22 additions & 4 deletions

File tree

‎ldebug.c‎

Lines changed: 6 additions & 0 deletions

‎ldebug.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
LUAI_FUNC int luaG_getfuncline (const Proto *f, int pc);
2525
LUAI_FUNC l_noret luaG_typeerror (lua_State *L, const TValue *o,
2626
const char *opname);
27+
LUAI_FUNC l_noret luaG_forerror (lua_State *L, const TValue *o,
28+
const char *what);
2729
LUAI_FUNC l_noret luaG_concaterror (lua_State *L, const TValue *p1,
2830
const TValue *p2);
2931
LUAI_FUNC l_noret luaG_opinterror (lua_State *L, const TValue *p1,

‎lvm.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1681,7 +1681,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
16811681
int stopnow;
16821682
if (unlikely(!forlimit(plimit, &ilimit, 1, &stopnow))) {
16831683
savestate(L, ci); /* for the error message */
1684-
luaG_runerror(L, "'for' limit must be a number");
1684+
luaG_forerror(L, plimit, "limit");
16851685
}
16861686
initv = (stopnow ? 0 : ivalue(init));
16871687
setivalue(plimit, ilimit);
@@ -1732,13 +1732,13 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
17321732
lua_Number ninit; lua_Number nlimit; lua_Number nstep;
17331733
savestate(L, ci); /* in case of errors */
17341734
if (unlikely(!tonumber(plimit, &nlimit)))
1735-
luaG_runerror(L, "'for' limit must be a number");
1735+
luaG_forerror(L, plimit, "limit");
17361736
setfltvalue(plimit, nlimit);
17371737
if (unlikely(!tonumber(pstep, &nstep)))
1738-
luaG_runerror(L, "'for' step must be a number");
1738+
luaG_forerror(L, pstep, "step");
17391739
setfltvalue(pstep, nstep);
17401740
if (unlikely(!tonumber(init, &ninit)))
1741-
luaG_runerror(L, "'for' initial value must be a number");
1741+
luaG_forerror(L, init, "initial value");
17421742
setfltvalue(init, luai_numsub(L, ninit, nstep));
17431743
}
17441744
pc += GETARG_Bx(i);

‎testes/errors.lua‎

Lines changed: 10 additions & 0 deletions

0 commit comments

Comments
 (0)