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)
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"));
* 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.
*/
{
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);
}
{
// 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)))
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)
set eventignore=
autocmd QuickFixCmdPost * copen
def AutocmdsDisabled()
- eval 0
+ eval 1 + 2
enddef
func Func()
noautocmd call s:AutocmdsDisabled()
enddef
def StackTop()
- eval 1
- eval 2
+ eval 1 + 2
+ eval 2 + 3
# call not on fourth line
StackBot()
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 = [
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
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
static int included_patches[] =
{ /* Add new patch number below this line */
+/**/
+ 3137,
/**/
3136,
/**/
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"
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: