From 6d53701c7a0dc4736d824fd891ee6f22265d0d68 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Sun, 18 May 2025 12:03:54 -0300 Subject: Proper error message when jumping into 'global *' A goto cannot jump into the scope of any variable declaration, including 'global *'. To report the error, it needs a "name" for the scope it is entering. --- lparser.c | 6 +++--- manual/manual.of | 2 +- testes/goto.lua | 13 ++++++++----- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/lparser.c b/lparser.c index bad3592a..c134b7a8 100644 --- a/lparser.c +++ b/lparser.c @@ -542,13 +542,13 @@ static void adjust_assign (LexState *ls, int nvars, int nexps, expdesc *e) { /* ** Generates an error that a goto jumps into the scope of some -** local variable. +** variable declaration. */ static l_noret jumpscopeerror (LexState *ls, Labeldesc *gt) { TString *tsname = getlocalvardesc(ls->fs, gt->nactvar)->vd.name; - const char *varname = getstr(tsname); + const char *varname = (tsname != NULL) ? getstr(tsname) : "*"; luaK_semerror(ls, - " at line %d jumps into the scope of local '%s'", + " at line %d jumps into the scope of '%s'", getstr(gt->name), gt->line, varname); /* raise the error */ } diff --git a/manual/manual.of b/manual/manual.of index eb97e853..a6361fa2 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -1504,7 +1504,7 @@ labels in Lua are considered statements too: A label is visible in the entire block where it is defined, except inside nested functions. A goto can jump to any visible label as long as it does not -enter into the scope of a local variable. +enter into the scope of a variable declaration. A label should not be declared where a previous label with the same name is visible, even if this other label has been declared in an enclosing block. diff --git a/testes/goto.lua b/testes/goto.lua index 44486e20..d7730061 100644 --- a/testes/goto.lua +++ b/testes/goto.lua @@ -23,15 +23,18 @@ errmsg([[ ::l1:: ::l1:: ]], "label 'l1'") errmsg([[ ::l1:: do ::l1:: end]], "label 'l1'") --- undefined label -errmsg([[ goto l1; local aa ::l1:: ::l2:: print(3) ]], "local 'aa'") --- jumping over variable definition +-- jumping over variable declaration +errmsg([[ goto l1; local aa ::l1:: ::l2:: print(3) ]], "scope of 'aa'") + +errmsg([[ goto l2; global *; ::l1:: ::l2:: print(3) ]], "scope of '*'") + errmsg([[ do local bb, cc; goto l1; end local aa ::l1:: print(3) -]], "local 'aa'") +]], "scope of 'aa'") + -- jumping into a block errmsg([[ do ::l1:: end goto l1 ]], "label 'l1'") @@ -44,7 +47,7 @@ errmsg([[ local xuxu = 10 ::cont:: until xuxu < x -]], "local 'xuxu'") +]], "scope of 'xuxu'") -- simple gotos local x -- cgit v1.2.3-55-g6feb