summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBram Moolenaar <Bram@vim.org>2021-07-10 19:42:03 +0200
committerBram Moolenaar <Bram@vim.org>2021-07-10 19:42:03 +0200
commitc323527d67081cfaff22503d1d282495976c7042 (patch)
treec59f38df837b94ee91d62ab3c7d6cdefe32b5fce /src
parentfe3418abe0dac65e42e85b5a91c5d0c975bc65bb (diff)
downloadvim-git-c323527d67081cfaff22503d1d282495976c7042.tar.gz
patch 8.2.3137: Vim9: no error when a line only has a variable namev8.2.3137
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)
Diffstat (limited to 'src')
-rw-r--r--src/errors.h2
-rw-r--r--src/ex_eval.c21
-rw-r--r--src/testdir/test_vim9_expr.vim2
-rw-r--r--src/testdir/test_vim9_func.vim6
-rw-r--r--src/testdir/test_vim9_script.vim19
-rw-r--r--src/version.c2
-rw-r--r--src/vim9compile.c39
7 files changed, 76 insertions, 15 deletions
diff --git a/src/errors.h b/src/errors.h
index 14c5b8def..d4f3179ab 100644
--- a/src/errors.h
+++ b/src/errors.h
@@ -494,3 +494,5 @@ EXTERN char e_no_white_space_allowed_between_option_and[]
INIT(= N_("E1205: No white space allowed between option and"));
EXTERN char e_dict_required_for_argument_nr[]
INIT(= N_("E1206: Dictionary required for argument %d"));
+EXTERN char e_expression_without_effect_str[]
+ INIT(= N_("E1207: Expression without an effect: %s"));
diff --git a/src/ex_eval.c b/src/ex_eval.c
index b504763df..08ba85887 100644
--- a/src/ex_eval.c
+++ b/src/ex_eval.c
@@ -208,7 +208,7 @@ cause_errthrow(
* not skipped. Errors in those commands may affect what of the subsequent
* commands are regarded part of catch and finally clauses. Catching the
* exception would then cause execution of commands not intended by the
- * user, who wouldn't even get aware of the problem. Therefor, discard the
+ * user, who wouldn't even get aware of the problem. Therefore, discard the
* exception currently being thrown to prevent it from being caught. Just
* execute finally clauses and terminate.
*/
@@ -896,11 +896,28 @@ ex_eval(exarg_T *eap)
{
typval_T tv;
evalarg_T evalarg;
+ int name_only = FALSE;
+ char_u *p;
+ long lnum = SOURCING_LNUM;
+
+ if (in_vim9script())
+ {
+ char_u *alias;
+
+ p = eap->arg;
+ get_name_len(&p, &alias, FALSE, FALSE);
+ name_only = ends_excmd2(eap->arg, skipwhite(p));
+ vim_free(alias);
+ }
fill_evalarg_from_eap(&evalarg, eap, eap->skip);
if (eval0(eap->arg, &tv, eap, &evalarg) == OK)
+ {
clear_tv(&tv);
+ if (in_vim9script() && name_only && lnum == SOURCING_LNUM)
+ semsg(_(e_expression_without_effect_str), eap->arg);
+ }
clear_evalarg(&evalarg, eap);
}
@@ -1287,7 +1304,7 @@ ex_continue(exarg_T *eap)
{
// Try to find the matching ":while". This might stop at a try
// conditional not in its finally clause (which is then to be executed
- // next). Therefor, inactivate all conditionals except the ":while"
+ // next). Therefore, inactivate all conditionals except the ":while"
// itself (if reached).
idx = cleanup_conditionals(cstack, CSF_WHILE | CSF_FOR, FALSE);
if (idx >= 0 && (cstack->cs_flags[idx] & (CSF_WHILE | CSF_FOR)))
diff --git a/src/testdir/test_vim9_expr.vim b/src/testdir/test_vim9_expr.vim
index 1e99e9568..79a4ea36e 100644
--- a/src/testdir/test_vim9_expr.vim
+++ b/src/testdir/test_vim9_expr.vim
@@ -647,7 +647,7 @@ def Test_expr4_equal()
CheckDefFailure(["var x = 'a' == "], 'E1097:', 3)
CheckScriptFailure(['vim9script', "var x = 'a' == "], 'E15:', 2)
- CheckDefExecAndScriptFailure2(['var items: any', 'eval 1', 'eval 2', 'if items == []', 'endif'], 'E691:', 'E1072:', 4)
+ CheckDefExecAndScriptFailure2(['var items: any', 'eval 1 + 1', 'eval 2 + 2', 'if items == []', 'endif'], 'E691:', 'E1072:', 4)
CheckDefExecAndScriptFailure(['var x: any = "a"', 'echo x == true'], 'E1072: Cannot compare string with bool', 2)
CheckDefExecAndScriptFailure(["var x: any = true", 'echo x == ""'], 'E1072: Cannot compare bool with string', 2)
diff --git a/src/testdir/test_vim9_func.vim b/src/testdir/test_vim9_func.vim
index 89cdee743..08e68d147 100644
--- a/src/testdir/test_vim9_func.vim
+++ b/src/testdir/test_vim9_func.vim
@@ -2538,7 +2538,7 @@ def Test_restore_modifiers()
set eventignore=
autocmd QuickFixCmdPost * copen
def AutocmdsDisabled()
- eval 0
+ eval 1 + 2
enddef
func Func()
noautocmd call s:AutocmdsDisabled()
@@ -2551,8 +2551,8 @@ def Test_restore_modifiers()
enddef
def StackTop()
- eval 1
- eval 2
+ eval 1 + 2
+ eval 2 + 3
# call not on fourth line
StackBot()
enddef
diff --git a/src/testdir/test_vim9_script.vim b/src/testdir/test_vim9_script.vim
index 66b0c740f..b21b63cbe 100644
--- a/src/testdir/test_vim9_script.vim
+++ b/src/testdir/test_vim9_script.vim
@@ -691,7 +691,7 @@ enddef
def Test_cnext_works_in_catch()
var lines =<< trim END
vim9script
- au BufEnter * eval 0
+ au BufEnter * eval 1 + 2
writefile(['text'], 'Xfile1')
writefile(['text'], 'Xfile2')
var items = [
@@ -1754,6 +1754,21 @@ def Test_script_var_shadows_function()
CheckScriptFailure(lines, 'E1041:', 5)
enddef
+def Test_script_var_shadows_command()
+ var lines =<< trim END
+ var undo = 1
+ undo = 2
+ assert_equal(2, undo)
+ END
+ CheckDefAndScriptSuccess(lines)
+
+ lines =<< trim END
+ var undo = 1
+ undo
+ END
+ CheckDefAndScriptFailure(lines, 'E1207:', 2)
+enddef
+
def s:RetSome(): string
return 'some'
enddef
@@ -2270,7 +2285,7 @@ def Test_if_const_expr()
assert_equal(false, res)
# with constant "false" expression may be invalid so long as the syntax is OK
- if false | eval 0 | endif
+ if false | eval 1 + 2 | endif
if false | eval burp + 234 | endif
if false | echo burp 234 'asd' | endif
if false
diff --git a/src/version.c b/src/version.c
index bcbff0a9c..762f3f809 100644
--- a/src/version.c
+++ b/src/version.c
@@ -756,6 +756,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
+ 3137,
+/**/
3136,
/**/
3135,
diff --git a/src/vim9compile.c b/src/vim9compile.c
index c1fc31ed2..c27a1cc34 100644
--- a/src/vim9compile.c
+++ b/src/vim9compile.c
@@ -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: