Skip to content

Commit

Permalink
Restrict when reserved identifiers are okay to match documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Sainan authored and well-in-that-case committed Aug 17, 2024
1 parent 6d6ced0 commit 1e5adfa
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
14 changes: 7 additions & 7 deletions src/lparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ enum NameFlags {
N_OVERRIDABLE = (1 << 2),
};

[[nodiscard]] static bool isnametkn (LexState *ls, int flags = N_RESERVED_NON_VALUE) {
[[nodiscard]] static bool isnametkn (LexState *ls, int flags = 0) {
return ls->t.token == TK_NAME || ls->t.IsNarrow()
|| ((flags & N_RESERVED_NON_VALUE) && ls->t.IsReservedNonValue())
|| ((flags & N_RESERVED) && ls->t.IsReserved())
Expand All @@ -389,7 +389,7 @@ static void disablekeyword (LexState *ls, int token) {
i->token = TK_NAME;
}

static TString *str_checkname (LexState *ls, int flags = N_RESERVED_NON_VALUE) {
static TString *str_checkname (LexState *ls, int flags = 0) {
#ifdef PLUTO_PARSER_SUGGESTIONS
if (ls->shouldSuggest()) {
SuggestionsState ss(ls);
Expand Down Expand Up @@ -445,7 +445,7 @@ static void codestring (expdesc *e, TString *s) {
}


static void codename (LexState *ls, expdesc *e, int flags = N_RESERVED_NON_VALUE) {
static void codename (LexState *ls, expdesc *e, int flags = 0) {
codestring(e, str_checkname(ls, flags));
}

Expand Down Expand Up @@ -485,7 +485,7 @@ static int registerlocalvar (LexState *ls, FuncState *fs, TString *varname) {
if (testnext(ls, '?'))
th.emplaceTypeDesc(VT_NIL);
do {
TString *ts = str_checkname(ls);
TString *ts = str_checkname(ls, N_RESERVED);
const char *tname = getstr(ts);
if (strcmp(tname, "number") == 0)
th.emplaceTypeDesc(VT_NUMBER);
Expand Down Expand Up @@ -2898,9 +2898,9 @@ static void parentexp (LexState *ls, expdesc *v) {

static void primaryexp (LexState *ls, expdesc *v, int flags = 0) {
/* primaryexp -> NAME | '(' expr ')' */
if (isnametkn(ls, N_RESERVED_NON_VALUE | N_OVERRIDABLE)) {
if (isnametkn(ls, N_OVERRIDABLE)) {
const bool is_overridable = ls->t.IsOverridable();
TString *varname = str_checkname(ls, N_RESERVED_NON_VALUE | N_OVERRIDABLE);
TString *varname = str_checkname(ls, N_OVERRIDABLE);
if (gett(ls) == TK_WALRUS) {
if (flags & E_OR_KILLED_WALRUS)
throwerr(ls, "':=' is not allowed in this context", "due to the 'or', it is no longer guaranteed that the local will be initialized by the time it's in scope.");
Expand Down Expand Up @@ -4884,7 +4884,7 @@ static void retstat (LexState *ls, TypeHint *prop) {
int startpc = fs->pc;
int endpc = -1;
if (block_follow(ls, 1) || ls->t.token == ';'
|| (!ls->switchstates.empty() && (ls->t.token == TK_CASE || ls->t.token == TK_DEFAULT))
|| (!ls->switchstates.empty() && (ls->t.token == TK_CASE || ls->t.token == TK_DEFAULT || ls->t.token == TK_BREAK))
) {
nret = 0; /* return no values */
if (prop) prop->emplaceTypeDesc(VT_VOID);
Expand Down
23 changes: 22 additions & 1 deletion testes/pluto/basic.pluto
Original file line number Diff line number Diff line change
Expand Up @@ -1123,7 +1123,10 @@ do
return
case 2:
return 2
case 3: -- return before defualt
case 3: -- return before case, with break
return
break
case 4: -- return before defualt
return
default:
return
Expand All @@ -1133,6 +1136,7 @@ do
assert(switchfunc(1) == nil)
assert(switchfunc(2) == 2)
assert(switchfunc(3) == nil)
assert(switchfunc(4) == nil)
end
do
switch true do
Expand Down Expand Up @@ -3149,6 +3153,23 @@ do
::if_then_goto_test::
end
end
do -- Reserved Identifiers
local t = {
class = "key"
}
assert(t.class == "key")

for i = 1, 10 do
if i == 5 then
goto continue
end
::continue::
end

-- This should still be an error
assert(load[[return break]] == nil)
assert(load[[return while]] == nil)
end
do
local function compat_names(default, parent)
assert(default == 1)
Expand Down

0 comments on commit 1e5adfa

Please sign in to comment.