1
0
forked from aniani/vim

patch 8.2.3137: Vim9: no error when a line only has a variable name

Problem:    Vim9: no error when a line only has a variable name.
Solution:   Give an error when an expression is evaluated without an effect.
            (closes #8538)
This commit is contained in:
Bram Moolenaar
2021-07-10 19:42:03 +02:00
parent fe3418abe0
commit c323527d67
7 changed files with 76 additions and 15 deletions

View File

@@ -8563,6 +8563,37 @@ compile_throw(char_u *arg, cctx_T *cctx UNUSED)
return p;
}
static char_u *
compile_eval(char_u *arg, cctx_T *cctx)
{
char_u *p = arg;
int name_only;
char_u *alias;
long lnum = SOURCING_LNUM;
// find_ex_command() will consider a variable name an expression, assuming
// that something follows on the next line. Check that something actually
// follows, otherwise it's probably a misplaced command.
get_name_len(&p, &alias, FALSE, FALSE);
name_only = ends_excmd2(arg, skipwhite(p));
vim_free(alias);
p = arg;
if (compile_expr0(&p, cctx) == FAIL)
return NULL;
if (name_only && lnum == SOURCING_LNUM)
{
semsg(_(e_expression_without_effect_str), arg);
return NULL;
}
// drop the result
generate_instr_drop(cctx, ISN_DROP, 1);
return skipwhite(p);
}
/*
* compile "echo expr"
* compile "echomsg expr"
@@ -9630,13 +9661,7 @@ compile_def_function(
break;
case CMD_eval:
if (compile_expr0(&p, &cctx) == FAIL)
goto erret;
// drop the result
generate_instr_drop(&cctx, ISN_DROP, 1);
line = skipwhite(p);
line = compile_eval(p, &cctx);
break;
case CMD_echo: