Skip to content

Commit

Permalink
Add a warning for switch cases jumping into variable scopes
Browse files Browse the repository at this point in the history
  • Loading branch information
Sainan committed Feb 28, 2024
1 parent 333134c commit a9381a9
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/lparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2972,6 +2972,8 @@ static void switchimpl (LexState *ls, int tk, void(*caselist)(LexState*,void*),
}
}

const auto nactvar = fs->nactvar;

std::vector<int> first{};
TString* const begin_switch = luaS_newliteral(ls->L, "pluto_begin_switch");
TString* default_case = nullptr;
Expand All @@ -2991,6 +2993,13 @@ static void switchimpl (LexState *ls, int tk, void(*caselist)(LexState*,void*),

while (gett(ls) != TK_END) {
auto case_line = ls->getLineNumber();
if (fs->nactvar != nactvar) {
const char *varname = getstr(getlocalvardesc(ls->fs, nactvar)->vd.name);
const char *msg = "switch case on line %d jumps into the scope of local '%s'. this will be a fatal error in 0.9.0.";
msg = luaO_pushfstring(ls->L, msg, case_line, varname);
throw_warn(ls, msg, WT_DEPRECATED);
lua_pop(ls->L, 1); /* pop result of luaO_pushfstring */
}
if (gett(ls) == TK_DEFAULT) {
luaX_next(ls); /* Skip 'default' */
checknext(ls, tk);
Expand Down

0 comments on commit a9381a9

Please sign in to comment.