Skip to content

Commit bb1b5e2

Browse files
committed
patch 8.2.1365: Vim9: no error for missing white space around operator
Problem: Vim9: no error for missing white space around operator. Solution: Check for white space. (closes #6618)
1 parent 282f9c6 commit bb1b5e2

7 files changed

Lines changed: 80 additions & 26 deletions

File tree

src/eval.c

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2574,6 +2574,7 @@ eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
25742574
int getnext;
25752575
char_u *p;
25762576
int op;
2577+
int oplen;
25772578
int concat;
25782579
typval_T var2;
25792580

@@ -2584,11 +2585,19 @@ eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
25842585
if (op != '+' && op != '-' && !concat)
25852586
break;
25862587

2588+
evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
25872589
if (getnext)
25882590
*arg = eval_next_line(evalarg);
25892591
else
2592+
{
2593+
if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
2594+
{
2595+
error_white_both(p, 1);
2596+
clear_tv(rettv);
2597+
return FAIL;
2598+
}
25902599
*arg = p;
2591-
evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
2600+
}
25922601
if ((op != '+' || (rettv->v_type != VAR_LIST
25932602
&& rettv->v_type != VAR_BLOB))
25942603
#ifdef FEAT_FLOAT
@@ -2613,9 +2622,14 @@ eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
26132622
/*
26142623
* Get the second variable.
26152624
*/
2616-
if (op == '.' && *(*arg + 1) == '.') // .. string concatenation
2617-
++*arg;
2618-
*arg = skipwhite_and_linebreak(*arg + 1, evalarg);
2625+
oplen = (op == '.' && *(*arg + 1) == '.') ? 2 : 1;
2626+
if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[oplen]))
2627+
{
2628+
error_white_both(p, oplen);
2629+
clear_tv(rettv);
2630+
return FAIL;
2631+
}
2632+
*arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
26192633
if (eval6(arg, &var2, evalarg, op == '.') == FAIL)
26202634
{
26212635
clear_tv(rettv);
@@ -3358,6 +3372,7 @@ eval_method(
33583372
}
33593373
else
33603374
{
3375+
*arg = skipwhite(*arg);
33613376
if (**arg != '(')
33623377
{
33633378
if (verbose)
@@ -4841,7 +4856,7 @@ get_env_len(char_u **arg)
48414856

48424857
/*
48434858
* Get the length of the name of a function or internal variable.
4844-
* "arg" is advanced to the first non-white character after the name.
4859+
* "arg" is advanced to after the name.
48454860
* Return 0 if something is wrong.
48464861
*/
48474862
int
@@ -4867,7 +4882,7 @@ get_id_len(char_u **arg)
48674882
return 0;
48684883

48694884
len = (int)(p - *arg);
4870-
*arg = skipwhite(p);
4885+
*arg = p;
48714886

48724887
return len;
48734888
}

src/evalvars.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,6 +1137,7 @@ list_arg_vars(exarg_T *eap, char_u *arg, int *first)
11371137
}
11381138
else
11391139
{
1140+
arg = skipwhite(arg);
11401141
if (tofree != NULL)
11411142
name = tofree;
11421143
if (eval_variable(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
@@ -3358,6 +3359,7 @@ assert_error(garray_T *gap)
33583359
int
33593360
var_exists(char_u *var)
33603361
{
3362+
char_u *arg = var;
33613363
char_u *name;
33623364
char_u *tofree;
33633365
typval_T tv;
@@ -3366,7 +3368,7 @@ var_exists(char_u *var)
33663368

33673369
// get_name_len() takes care of expanding curly braces
33683370
name = var;
3369-
len = get_name_len(&var, &tofree, TRUE, FALSE);
3371+
len = get_name_len(&arg, &tofree, TRUE, FALSE);
33703372
if (len > 0)
33713373
{
33723374
if (tofree != NULL)
@@ -3375,12 +3377,13 @@ var_exists(char_u *var)
33753377
if (n)
33763378
{
33773379
// handle d.key, l[idx], f(expr)
3378-
n = (handle_subscript(&var, &tv, &EVALARG_EVALUATE, FALSE) == OK);
3380+
arg = skipwhite(arg);
3381+
n = (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, FALSE) == OK);
33793382
if (n)
33803383
clear_tv(&tv);
33813384
}
33823385
}
3383-
if (*var != NUL)
3386+
if (*arg != NUL)
33843387
n = FALSE;
33853388

33863389
vim_free(tofree);

src/proto/vim9compile.pro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ char_u *peek_next_line_from_context(cctx_T *cctx);
1717
char_u *next_line_from_context(cctx_T *cctx, int skip_comment);
1818
char_u *to_name_const_end(char_u *arg);
1919
exptype_T get_compare_type(char_u *p, int *len, int *type_is);
20+
void error_white_both(char_u *op, int len);
2021
int assignment_len(char_u *p, int *heredoc);
2122
void vim9_declare_error(char_u *name);
2223
int check_vim9_unlet(char_u *name);

src/testdir/test_vim9_expr.vim

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,39 @@ def Test_expr5_vim9script()
872872
echo 'abc' isnot? 'abc'
873873
END
874874
CheckScriptFailure(lines, 'E15:')
875+
876+
# check white space
877+
lines =<< trim END
878+
vim9script
879+
echo 5+6
880+
END
881+
CheckScriptFailure(lines, 'E1004:')
882+
lines =<< trim END
883+
vim9script
884+
echo 5 +6
885+
END
886+
CheckScriptFailure(lines, 'E1004:')
887+
lines =<< trim END
888+
vim9script
889+
echo 5+ 6
890+
END
891+
CheckScriptFailure(lines, 'E1004:')
892+
893+
lines =<< trim END
894+
vim9script
895+
echo 'a'..'b'
896+
END
897+
CheckScriptFailure(lines, 'E1004:')
898+
lines =<< trim END
899+
vim9script
900+
echo 'a' ..'b'
901+
END
902+
CheckScriptFailure(lines, 'E1004:')
903+
lines =<< trim END
904+
vim9script
905+
echo 'a'.. 'b'
906+
END
907+
CheckScriptFailure(lines, 'E1004:')
875908
enddef
876909

877910
def Test_expr5_float()

src/testdir/test_vim9_func.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1270,7 +1270,7 @@ enddef
12701270
def TreeWalk(dir: string): list<any>
12711271
return readdir(dir)->map({_, val ->
12721272
fnamemodify(dir .. '/' .. val, ':p')->isdirectory()
1273-
? {val : TreeWalk(dir .. '/' .. val)}
1273+
? {val: TreeWalk(dir .. '/' .. val)}
12741274
: val
12751275
})
12761276
enddef

src/version.c

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

755755
static int included_patches[] =
756756
{ /* Add new patch number below this line */
757+
/**/
758+
1365,
757759
/**/
758760
1364,
759761
/**/

src/vim9compile.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4243,6 +4243,18 @@ compile_expr7(
42434243
return OK;
42444244
}
42454245

4246+
/*
4247+
* Give the "white on both sides" error, taking the operator from "p[len]".
4248+
*/
4249+
void
4250+
error_white_both(char_u *op, int len)
4251+
{
4252+
char_u buf[10];
4253+
4254+
vim_strncpy(buf, op, len);
4255+
semsg(_(e_white_both), buf);
4256+
}
4257+
42464258
/*
42474259
* * number multiplication
42484260
* / number division
@@ -4275,10 +4287,7 @@ compile_expr6(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
42754287

42764288
if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[1]))
42774289
{
4278-
char_u buf[3];
4279-
4280-
vim_strncpy(buf, op, 1);
4281-
semsg(_(e_white_both), buf);
4290+
error_white_both(op, 1);
42824291
return FAIL;
42834292
}
42844293
*arg = skipwhite(op + 1);
@@ -4354,10 +4363,7 @@ compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
43544363

43554364
if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(op[oplen]))
43564365
{
4357-
char_u buf[3];
4358-
4359-
vim_strncpy(buf, op, oplen);
4360-
semsg(_(e_white_both), buf);
4366+
error_white_both(op, oplen);
43614367
return FAIL;
43624368
}
43634369

@@ -4486,10 +4492,7 @@ compile_expr4(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
44864492

44874493
if (!IS_WHITE_OR_NUL(**arg) || !IS_WHITE_OR_NUL(p[len]))
44884494
{
4489-
char_u buf[7];
4490-
4491-
vim_strncpy(buf, p, len);
4492-
semsg(_(e_white_both), buf);
4495+
error_white_both(p, len);
44934496
return FAIL;
44944497
}
44954498

@@ -5132,10 +5135,7 @@ compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
51325135

51335136
if (oplen > 0 && (!VIM_ISWHITE(*sp) || !VIM_ISWHITE(op[oplen])))
51345137
{
5135-
char_u buf[4];
5136-
5137-
vim_strncpy(buf, op, oplen);
5138-
semsg(_(e_white_both), buf);
5138+
error_white_both(op, oplen);
51395139
return NULL;
51405140
}
51415141

0 commit comments

Comments
 (0)