0
0
mirror of https://github.com/vim/vim.git synced 2025-09-25 03:54:15 -04:00

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

@@ -494,3 +494,5 @@ EXTERN char e_no_white_space_allowed_between_option_and[]
INIT(= N_("E1205: 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[] EXTERN char e_dict_required_for_argument_nr[]
INIT(= N_("E1206: Dictionary required for argument %d")); INIT(= N_("E1206: Dictionary required for argument %d"));
EXTERN char e_expression_without_effect_str[]
INIT(= N_("E1207: Expression without an effect: %s"));

View File

@@ -208,7 +208,7 @@ cause_errthrow(
* not skipped. Errors in those commands may affect what of the subsequent * not skipped. Errors in those commands may affect what of the subsequent
* commands are regarded part of catch and finally clauses. Catching the * commands are regarded part of catch and finally clauses. Catching the
* exception would then cause execution of commands not intended by 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 * exception currently being thrown to prevent it from being caught. Just
* execute finally clauses and terminate. * execute finally clauses and terminate.
*/ */
@@ -896,11 +896,28 @@ ex_eval(exarg_T *eap)
{ {
typval_T tv; typval_T tv;
evalarg_T evalarg; 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); fill_evalarg_from_eap(&evalarg, eap, eap->skip);
if (eval0(eap->arg, &tv, eap, &evalarg) == OK) if (eval0(eap->arg, &tv, eap, &evalarg) == OK)
{
clear_tv(&tv); clear_tv(&tv);
if (in_vim9script() && name_only && lnum == SOURCING_LNUM)
semsg(_(e_expression_without_effect_str), eap->arg);
}
clear_evalarg(&evalarg, eap); 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 // Try to find the matching ":while". This might stop at a try
// conditional not in its finally clause (which is then to be executed // 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). // itself (if reached).
idx = cleanup_conditionals(cstack, CSF_WHILE | CSF_FOR, FALSE); idx = cleanup_conditionals(cstack, CSF_WHILE | CSF_FOR, FALSE);
if (idx >= 0 && (cstack->cs_flags[idx] & (CSF_WHILE | CSF_FOR))) if (idx >= 0 && (cstack->cs_flags[idx] & (CSF_WHILE | CSF_FOR)))

View File

@@ -647,7 +647,7 @@ def Test_expr4_equal()
CheckDefFailure(["var x = 'a' == "], 'E1097:', 3) CheckDefFailure(["var x = 'a' == "], 'E1097:', 3)
CheckScriptFailure(['vim9script', "var x = 'a' == "], 'E15:', 2) 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 = "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) CheckDefExecAndScriptFailure(["var x: any = true", 'echo x == ""'], 'E1072: Cannot compare bool with string', 2)

View File

@@ -2538,7 +2538,7 @@ def Test_restore_modifiers()
set eventignore= set eventignore=
autocmd QuickFixCmdPost * copen autocmd QuickFixCmdPost * copen
def AutocmdsDisabled() def AutocmdsDisabled()
eval 0 eval 1 + 2
enddef enddef
func Func() func Func()
noautocmd call s:AutocmdsDisabled() noautocmd call s:AutocmdsDisabled()
@@ -2551,8 +2551,8 @@ def Test_restore_modifiers()
enddef enddef
def StackTop() def StackTop()
eval 1 eval 1 + 2
eval 2 eval 2 + 3
# call not on fourth line # call not on fourth line
StackBot() StackBot()
enddef enddef

View File

@@ -691,7 +691,7 @@ enddef
def Test_cnext_works_in_catch() def Test_cnext_works_in_catch()
var lines =<< trim END var lines =<< trim END
vim9script vim9script
au BufEnter * eval 0 au BufEnter * eval 1 + 2
writefile(['text'], 'Xfile1') writefile(['text'], 'Xfile1')
writefile(['text'], 'Xfile2') writefile(['text'], 'Xfile2')
var items = [ var items = [
@@ -1754,6 +1754,21 @@ def Test_script_var_shadows_function()
CheckScriptFailure(lines, 'E1041:', 5) CheckScriptFailure(lines, 'E1041:', 5)
enddef 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 def s:RetSome(): string
return 'some' return 'some'
enddef enddef
@@ -2270,7 +2285,7 @@ def Test_if_const_expr()
assert_equal(false, res) assert_equal(false, res)
# with constant "false" expression may be invalid so long as the syntax is OK # 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 | eval burp + 234 | endif
if false | echo burp 234 'asd' | endif if false | echo burp 234 'asd' | endif
if false if false

View File

@@ -755,6 +755,8 @@ static char *(features[]) =
static int included_patches[] = static int included_patches[] =
{ /* Add new patch number below this line */ { /* Add new patch number below this line */
/**/
3137,
/**/ /**/
3136, 3136,
/**/ /**/

View File

@@ -8563,6 +8563,37 @@ compile_throw(char_u *arg, cctx_T *cctx UNUSED)
return p; 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 "echo expr"
* compile "echomsg expr" * compile "echomsg expr"
@@ -9630,13 +9661,7 @@ compile_def_function(
break; break;
case CMD_eval: case CMD_eval:
if (compile_expr0(&p, &cctx) == FAIL) line = compile_eval(p, &cctx);
goto erret;
// drop the result
generate_instr_drop(&cctx, ISN_DROP, 1);
line = skipwhite(p);
break; break;
case CMD_echo: case CMD_echo: