Skip to content

Commit 9510d22

Browse files
committed
patch 9.0.0444: trying to declare g:variable gives confusing error
Problem: Trying to declare g:variable gives confusing error. Solution: Give a better error message. (closes #11108)
1 parent cce82a5 commit 9510d22

File tree

9 files changed

+51
-20
lines changed

9 files changed

+51
-20
lines changed

src/errors.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3335,4 +3335,6 @@ EXTERN char e_script_variable_was_deleted[]
33353335
INIT(= N_("E1302: Script variable was deleted"));
33363336
EXTERN char e_custom_list_completion_function_does_not_return_list_but_str[]
33373337
INIT(= N_("E1303: Custom list completion function does not return a List but a %s"));
3338+
EXTERN char e_cannot_use_type_with_this_variable_str[]
3339+
INIT(= N_("E1304: Cannot use type with this variable: %s"));
33383340
#endif

src/eval.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1031,12 +1031,16 @@ get_lval(
10311031
{
10321032
char_u *tp = skipwhite(p + 1);
10331033

1034+
if (is_scoped_variable(name))
1035+
{
1036+
semsg(_(e_cannot_use_type_with_this_variable_str), name);
1037+
return NULL;
1038+
}
10341039
if (tp == p + 1 && !quiet)
10351040
{
10361041
semsg(_(e_white_space_required_after_str_str), ":", p);
10371042
return NULL;
10381043
}
1039-
10401044
if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
10411045
{
10421046
semsg(_(e_using_type_not_in_script_context_str), p);

src/evalvars.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,18 @@ list_script_vars(int *first)
602602
"s:", FALSE, first);
603603
}
604604

605+
/*
606+
* Return TRUE if "name" starts with "g:", "w:", "t:" or "b:".
607+
* But only when an identifier character follows.
608+
*/
609+
int
610+
is_scoped_variable(char_u *name)
611+
{
612+
return vim_strchr((char_u *)"gwbt", name[0]) != NULL
613+
&& name[1] == ':'
614+
&& eval_isnamec(name[2]);
615+
}
616+
605617
/*
606618
* Evaluate one Vim expression {expr} in string "p" and append the
607619
* resulting string to "gap". "p" points to the opening "{".
@@ -3679,8 +3691,7 @@ set_var_const(
36793691
vim9_declare_error(name);
36803692
goto failed;
36813693
}
3682-
if ((flags & ASSIGN_FOR_LOOP) && name[1] == ':'
3683-
&& vim_strchr((char_u *)"gwbt", name[0]) != NULL)
3694+
if ((flags & ASSIGN_FOR_LOOP) && is_scoped_variable(name))
36843695
// Do not make g:var, w:var, b:var or t:var final.
36853696
flags &= ~ASSIGN_FINAL;
36863697

src/ex_docmd.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3761,11 +3761,11 @@ find_ex_command(
37613761
}
37623762
}
37633763

3764-
// Recognize using a type for a w:, b:, t: or g: variable:
3764+
// Recognize trying to use a type for a w:, b:, t: or g: variable:
37653765
// "w:varname: number = 123".
37663766
if (eap->cmd[1] == ':' && *p == ':')
37673767
{
3768-
eap->cmdidx = CMD_eval;
3768+
eap->cmdidx = CMD_var;
37693769
return eap->cmd;
37703770
}
37713771
}

src/proto/evalvars.pro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ list_T *eval_spell_expr(char_u *badword, char_u *expr);
1313
int get_spellword(list_T *list, char_u **pp);
1414
void prepare_vimvar(int idx, typval_T *save_tv);
1515
void restore_vimvar(int idx, typval_T *save_tv);
16+
int is_scoped_variable(char_u *name);
1617
char_u *eval_one_expr_in_str(char_u *p, garray_T *gap, int evaluate);
1718
list_T *heredoc_get(exarg_T *eap, char_u *cmd, int script_get, int vim9compile);
1819
void ex_var(exarg_T *eap);

src/testdir/test_vim9_assign.vim

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1597,13 +1597,31 @@ def Test_assignment_failure()
15971597
v9.CheckDefFailure(['var name: dict<number'], 'E1009:')
15981598

15991599
v9.CheckDefFailure(['w:foo: number = 10'],
1600-
'E488: Trailing characters: : number = 1')
1600+
'E1016: Cannot declare a window variable: w:foo')
16011601
v9.CheckDefFailure(['t:foo: bool = true'],
1602-
'E488: Trailing characters: : bool = true')
1602+
'E1016: Cannot declare a tab variable: t:foo')
16031603
v9.CheckDefFailure(['b:foo: string = "x"'],
1604-
'E488: Trailing characters: : string = "x"')
1604+
'E1016: Cannot declare a buffer variable: b:foo')
16051605
v9.CheckDefFailure(['g:foo: number = 123'],
1606-
'E488: Trailing characters: : number = 123')
1606+
'E1016: Cannot declare a global variable: g:foo')
1607+
1608+
v9.CheckScriptFailure(['vim9script', 'w:foo: number = 123'],
1609+
'E1304: Cannot use type with this variable: w:foo:')
1610+
v9.CheckScriptFailure(['vim9script', 't:foo: number = 123'],
1611+
'E1304: Cannot use type with this variable: t:foo:')
1612+
v9.CheckScriptFailure(['vim9script', 'b:foo: number = 123'],
1613+
'E1304: Cannot use type with this variable: b:foo:')
1614+
v9.CheckScriptFailure(['vim9script', 'g:foo: number = 123'],
1615+
'E1304: Cannot use type with this variable: g:foo:')
1616+
1617+
v9.CheckScriptFailure(['vim9script', 'const w:FOO: number = 123'],
1618+
'E1304: Cannot use type with this variable: w:FOO:')
1619+
v9.CheckScriptFailure(['vim9script', 'const t:FOO: number = 123'],
1620+
'E1304: Cannot use type with this variable: t:FOO:')
1621+
v9.CheckScriptFailure(['vim9script', 'const b:FOO: number = 123'],
1622+
'E1304: Cannot use type with this variable: b:FOO:')
1623+
v9.CheckScriptFailure(['vim9script', 'const g:FOO: number = 123'],
1624+
'E1304: Cannot use type with this variable: g:FOO:')
16071625
enddef
16081626

16091627
def Test_assign_list()
@@ -1959,8 +1977,6 @@ def Test_var_declaration()
19591977
FLIST[0] = 11
19601978
assert_equal([11], FLIST)
19611979

1962-
const g:FOO: number = 321
1963-
assert_equal(321, g:FOO)
19641980
const g:FOOS = 'gfoos'
19651981
assert_equal('gfoos', g:FOOS)
19661982
final g:FLIST = [2]
@@ -1975,8 +1991,6 @@ def Test_var_declaration()
19751991
assert_equal(123, g:globConst)
19761992
assert_true(islocked('g:globConst'))
19771993

1978-
const w:FOO: number = 46
1979-
assert_equal(46, w:FOO)
19801994
const w:FOOS = 'wfoos'
19811995
assert_equal('wfoos', w:FOOS)
19821996
final w:FLIST = [3]
@@ -2015,10 +2029,8 @@ def Test_var_declaration()
20152029
unlet g:var_prefixed
20162030
unlet g:other_var
20172031
unlet g:globConst
2018-
unlet g:FOO
20192032
unlet g:FOOS
20202033
unlet g:FLIST
2021-
unlet w:FOO
20222034
unlet w:FOOS
20232035
unlet w:FLIST
20242036
enddef

src/testdir/test_vim9_cmd.vim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1936,7 +1936,7 @@ def Test_var_not_cmd()
19361936
var lines =<< trim END
19371937
g:notexist:cmd
19381938
END
1939-
v9.CheckDefAndScriptFailure(lines, ['E488: Trailing characters: :cmd', 'E121: Undefined variable: g:notexist'], 1)
1939+
v9.CheckDefAndScriptFailure(lines, ['E1016: Cannot declare a global variable: g:notexist', "E1069: White space required after ':'"], 1)
19401940

19411941
lines =<< trim END
19421942
g-pat-cmd
@@ -1950,7 +1950,7 @@ def Test_var_not_cmd()
19501950
lines =<< trim END
19511951
s:notexist:repl
19521952
END
1953-
v9.CheckDefAndScriptFailure(lines, ['E488: Trailing characters: :repl', 'E1268:'], 1)
1953+
v9.CheckDefAndScriptFailure(lines, ['E1101: Cannot declare a script variable in a function: s:notexist', "E1069: White space required after ':'"], 1)
19541954

19551955
lines =<< trim END
19561956
notexist:repl

src/testdir/test_vim9_script.vim

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4241,13 +4241,12 @@ func Test_misplaced_type()
42414241
endfunc
42424242

42434243
def Run_Test_misplaced_type()
4244-
writefile(['let g:somevar = "asdf"'], 'XTest_misplaced_type')
4244+
writefile(['let g:somevar = "asdf"'], 'XTest_misplaced_type', 'D')
42454245
var buf = g:RunVimInTerminal('-S XTest_misplaced_type', {'rows': 6})
4246-
term_sendkeys(buf, ":vim9cmd echo islocked('g:somevar: string')\<CR>")
4246+
term_sendkeys(buf, ":vim9cmd echo islocked('somevar: string')\<CR>")
42474247
g:VerifyScreenDump(buf, 'Test_misplaced_type', {})
42484248

42494249
g:StopVimInTerminal(buf)
4250-
delete('XTest_misplaced_type')
42514250
enddef
42524251

42534252
" Ensure echo doesn't crash when stringifying empty variables.

src/version.c

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

704704
static int included_patches[] =
705705
{ /* Add new patch number below this line */
706+
/**/
707+
444,
706708
/**/
707709
443,
708710
/**/

0 commit comments

Comments
 (0)