Skip to content

Commit

Permalink
Simplify new_localvar by putting warning logic in its own function
Browse files Browse the repository at this point in the history
  • Loading branch information
well-in-that-case committed Oct 2, 2023
1 parent fa750dd commit 690d10a
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions src/lparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -529,15 +529,7 @@ static LocVar *localdebuginfo (FuncState *fs, int vidx) {
}


/*
** Create a new local variable with the given 'name'. Return its index
** in the function.
*/
static int new_localvar (LexState *ls, TString *name, int line, TypeHint hint = {}) {
lua_State *L = ls->L;
FuncState *fs = ls->fs;
Dyndata *dyd = ls->dyd;
Vardesc *var;
static void checkforshadowing (LexState *ls, FuncState *fs, TString *name, int line) {
int locals = luaY_nvarstack(fs);
for (int i = fs->firstlocal; i < locals; i++) {
Vardesc *desc = getlocalvardesc(fs, i);
Expand All @@ -546,11 +538,24 @@ static int new_localvar (LexState *ls, TString *name, int line, TypeHint hint =
if ((n != "(for state)" && n != "(switch control value)" && n != "(try results)" && n != "(try ok)") && (local && local->varname == name)) { // Got a match.
throw_warn(ls,
"duplicate local declaration",
luaO_fmt(L, "this shadows the initial declaration of '%s' on line %d.", name->contents, desc->vd.line), line, WT_VAR_SHADOW);
L->top.p--; /* pop result of luaO_fmt */
luaO_fmt(ls->L, "this shadows the initial declaration of '%s' on line %d.", name->contents, desc->vd.line), line, WT_VAR_SHADOW);
ls->L->top.p--; /* pop result of luaO_fmt */
break;
}
}
}


/*
** Create a new local variable with the given 'name'. Return its index
** in the function.
*/
static int new_localvar (LexState *ls, TString *name, int line, TypeHint hint = {}) {
lua_State *L = ls->L;
FuncState *fs = ls->fs;
Dyndata *dyd = ls->dyd;
Vardesc *var;
checkforshadowing(ls, fs, name, line);
luaM_growvector(L, dyd->actvar.arr, dyd->actvar.n + 1,
dyd->actvar.size, Vardesc, USHRT_MAX, "local variables");
var = &dyd->actvar.arr[dyd->actvar.n++];
Expand Down

0 comments on commit 690d10a

Please sign in to comment.