Skip to content

Commit c323527

Browse files
committed
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)
1 parent fe3418a commit c323527

7 files changed

Lines changed: 76 additions & 15 deletions

File tree

src/errors.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,3 +494,5 @@ EXTERN char e_no_white_space_allowed_between_option_and[]
494494
INIT(= N_("E1205: No white space allowed between option and"));
495495
EXTERN char e_dict_required_for_argument_nr[]
496496
INIT(= N_("E1206: Dictionary required for argument %d"));
497+
EXTERN char e_expression_without_effect_str[]
498+
INIT(= N_("E1207: Expression without an effect: %s"));

src/ex_eval.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ cause_errthrow(
208208
* not skipped. Errors in those commands may affect what of the subsequent
209209
* commands are regarded part of catch and finally clauses. Catching the
210210
* exception would then cause execution of commands not intended by the
211-
* user, who wouldn't even get aware of the problem. Therefor, discard the
211+
* user, who wouldn't even get aware of the problem. Therefore, discard the
212212
* exception currently being thrown to prevent it from being caught. Just
213213
* execute finally clauses and terminate.
214214
*/
@@ -896,11 +896,28 @@ ex_eval(exarg_T *eap)
896896
{
897897
typval_T tv;
898898
evalarg_T evalarg;
899+
int name_only = FALSE;
900+
char_u *p;
901+
long lnum = SOURCING_LNUM;
902+
903+
if (in_vim9script())
904+
{
905+
char_u *alias;
906+
907+
p = eap->arg;
908+
get_name_len(&p, &alias, FALSE, FALSE);
909+
name_only = ends_excmd2(eap->arg, skipwhite(p));
910+
vim_free(alias);
911+
}
899912

900913
fill_evalarg_from_eap(&evalarg, eap, eap->skip);
901914

902915
if (eval0(eap->arg, &tv, eap, &evalarg) == OK)
916+
{
903917
clear_tv(&tv);
918+
if (in_vim9script() && name_only && lnum == SOURCING_LNUM)
919+
semsg(_(e_expression_without_effect_str), eap->arg);
920+
}
904921

905922
clear_evalarg(&evalarg, eap);
906923
}
@@ -1287,7 +1304,7 @@ ex_continue(exarg_T *eap)
12871304
{
12881305
// Try to find the matching ":while". This might stop at a try
12891306
// conditional not in its finally clause (which is then to be executed
1290-
// next). Therefor, inactivate all conditionals except the ":while"
1307+
// next). Therefore, inactivate all conditionals except the ":while"
12911308
// itself (if reached).
12921309
idx = cleanup_conditionals(cstack, CSF_WHILE | CSF_FOR, FALSE);
12931310
if (idx >= 0 && (cstack->cs_flags[idx] & (CSF_WHILE | CSF_FOR)))

src/testdir/test_vim9_expr.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@ def Test_expr4_equal()
647647
CheckDefFailure(["var x = 'a' == "], 'E1097:', 3)
648648
CheckScriptFailure(['vim9script', "var x = 'a' == "], 'E15:', 2)
649649

650-
CheckDefExecAndScriptFailure2(['var items: any', 'eval 1', 'eval 2', 'if items == []', 'endif'], 'E691:', 'E1072:', 4)
650+
CheckDefExecAndScriptFailure2(['var items: any', 'eval 1 + 1', 'eval 2 + 2', 'if items == []', 'endif'], 'E691:', 'E1072:', 4)
651651

652652
CheckDefExecAndScriptFailure(['var x: any = "a"', 'echo x == true'], 'E1072: Cannot compare string with bool', 2)
653653
CheckDefExecAndScriptFailure(["var x: any = true", 'echo x == ""'], 'E1072: Cannot compare bool with string', 2)

src/testdir/test_vim9_func.vim

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2538,7 +2538,7 @@ def Test_restore_modifiers()
25382538
set eventignore=
25392539
autocmd QuickFixCmdPost * copen
25402540
def AutocmdsDisabled()
2541-
eval 0
2541+
eval 1 + 2
25422542
enddef
25432543
func Func()
25442544
noautocmd call s:AutocmdsDisabled()
@@ -2551,8 +2551,8 @@ def Test_restore_modifiers()
25512551
enddef
25522552

25532553
def StackTop()
2554-
eval 1
2555-
eval 2
2554+
eval 1 + 2
2555+
eval 2 + 3
25562556
# call not on fourth line
25572557
StackBot()
25582558
enddef

src/testdir/test_vim9_script.vim

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,7 @@ enddef
691691
def Test_cnext_works_in_catch()
692692
var lines =<< trim END
693693
vim9script
694-
au BufEnter * eval 0
694+
au BufEnter * eval 1 + 2
695695
writefile(['text'], 'Xfile1')
696696
writefile(['text'], 'Xfile2')
697697
var items = [
@@ -1754,6 +1754,21 @@ def Test_script_var_shadows_function()
17541754
CheckScriptFailure(lines, 'E1041:', 5)
17551755
enddef
17561756

1757+
def Test_script_var_shadows_command()
1758+
var lines =<< trim END
1759+
var undo = 1
1760+
undo = 2
1761+
assert_equal(2, undo)
1762+
END
1763+
CheckDefAndScriptSuccess(lines)
1764+
1765+
lines =<< trim END
1766+
var undo = 1
1767+
undo
1768+
END
1769+
CheckDefAndScriptFailure(lines, 'E1207:', 2)
1770+
enddef
1771+
17571772
def s:RetSome(): string
17581773
return 'some'
17591774
enddef
@@ -2270,7 +2285,7 @@ def Test_if_const_expr()
22702285
assert_equal(false, res)
22712286

22722287
# with constant "false" expression may be invalid so long as the syntax is OK
2273-
if false | eval 0 | endif
2288+
if false | eval 1 + 2 | endif
22742289
if false | eval burp + 234 | endif
22752290
if false | echo burp 234 'asd' | endif
22762291
if false

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,8 @@ static char *(features[]) =
755755

756756
static int included_patches[] =
757757
{ /* Add new patch number below this line */
758+
/**/
759+
3137,
758760
/**/
759761
3136,
760762
/**/

src/vim9compile.c

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8563,6 +8563,37 @@ compile_throw(char_u *arg, cctx_T *cctx UNUSED)
85638563
return p;
85648564
}
85658565

8566+
static char_u *
8567+
compile_eval(char_u *arg, cctx_T *cctx)
8568+
{
8569+
char_u *p = arg;
8570+
int name_only;
8571+
char_u *alias;
8572+
long lnum = SOURCING_LNUM;
8573+
8574+
// find_ex_command() will consider a variable name an expression, assuming
8575+
// that something follows on the next line. Check that something actually
8576+
// follows, otherwise it's probably a misplaced command.
8577+
get_name_len(&p, &alias, FALSE, FALSE);
8578+
name_only = ends_excmd2(arg, skipwhite(p));
8579+
vim_free(alias);
8580+
8581+
p = arg;
8582+
if (compile_expr0(&p, cctx) == FAIL)
8583+
return NULL;
8584+
8585+
if (name_only && lnum == SOURCING_LNUM)
8586+
{
8587+
semsg(_(e_expression_without_effect_str), arg);
8588+
return NULL;
8589+
}
8590+
8591+
// drop the result
8592+
generate_instr_drop(cctx, ISN_DROP, 1);
8593+
8594+
return skipwhite(p);
8595+
}
8596+
85668597
/*
85678598
* compile "echo expr"
85688599
* compile "echomsg expr"
@@ -9630,13 +9661,7 @@ compile_def_function(
96309661
break;
96319662

96329663
case CMD_eval:
9633-
if (compile_expr0(&p, &cctx) == FAIL)
9634-
goto erret;
9635-
9636-
// drop the result
9637-
generate_instr_drop(&cctx, ISN_DROP, 1);
9638-
9639-
line = skipwhite(p);
9664+
line = compile_eval(p, &cctx);
96409665
break;
96419666

96429667
case CMD_echo:

0 commit comments

Comments
 (0)