Skip to content

Commit b5a0719

Browse files
yegappanchrisbra
authored andcommitted
patch 9.0.1986: Vim9: accepting type-annotations
Problem: Vim9: accepting type-annotations Solution: Reject type annotations outside of declarations. closes: #13267 closes: #13283 Signed-off-by: Christian Brabandt <[email protected]> Co-authored-by: Yegappan Lakshmanan <[email protected]>
1 parent b6d01f1 commit b5a0719

6 files changed

Lines changed: 55 additions & 1 deletion

File tree

src/eval.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,6 +1271,14 @@ get_lval(
12711271
semsg(_(e_using_type_not_in_script_context_str), p);
12721272
return NULL;
12731273
}
1274+
if (vim9script && (flags & GLV_NO_DECL) &&
1275+
!(flags & GLV_FOR_LOOP))
1276+
{
1277+
// Using a type and not in a "var" declaration.
1278+
semsg(_(e_trailing_characters_str), p);
1279+
return NULL;
1280+
}
1281+
12741282

12751283
// parse the type after the name
12761284
lp->ll_type = parse_type(&tp,

src/evalvars.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1858,6 +1858,7 @@ ex_let_one(
18581858
char_u *p;
18591859
int lval_flags = (flags & (ASSIGN_NO_DECL | ASSIGN_DECL))
18601860
? GLV_NO_DECL : 0;
1861+
lval_flags |= (flags & ASSIGN_FOR_LOOP) ? GLV_FOR_LOOP : 0;
18611862
if (op != NULL && *op != '=')
18621863
lval_flags |= GLV_ASSIGN_WITH_OP;
18631864

src/testdir/test_vim9_assign.vim

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2822,6 +2822,45 @@ def Test_using_s_var_in_function()
28222822
v9.CheckScriptSuccess(lines)
28232823
enddef
28242824

2825+
" Test for specifying a type in assignment
2826+
def Test_type_specification_in_assignment()
2827+
# specify type for an existing script local variable without "var"
2828+
var lines =<< trim END
2829+
vim9script
2830+
var n: number = 10
2831+
n: number = 20
2832+
END
2833+
v9.CheckSourceFailure(lines, 'E488: Trailing characters: : number = 20', 3)
2834+
2835+
# specify type for a non-existing script local variable without "var"
2836+
lines =<< trim END
2837+
vim9script
2838+
MyVar: string = 'abc'
2839+
END
2840+
v9.CheckSourceFailure(lines, "E492: Not an editor command: MyVar: string = 'abc'", 2)
2841+
2842+
# specify type for an existing def local variable without "var"
2843+
lines =<< trim END
2844+
vim9script
2845+
def Foo()
2846+
var n: number = 10
2847+
n: number = 20
2848+
enddef
2849+
Foo()
2850+
END
2851+
v9.CheckSourceFailure(lines, 'E488: Trailing characters: : number = 20', 2)
2852+
2853+
# specify type for a non-existing def local variable without "var"
2854+
lines =<< trim END
2855+
vim9script
2856+
def Foo()
2857+
MyVar: string = 'abc'
2858+
enddef
2859+
Foo()
2860+
END
2861+
v9.CheckSourceFailure(lines, "E476: Invalid command: MyVar: string = 'abc'", 1)
2862+
enddef
2863+
28252864
let g:someVar = 'X'
28262865

28272866
" Test for heredoc with Vim expressions.

src/version.c

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

705705
static int included_patches[] =
706706
{ /* Add new patch number below this line */
707+
/**/
708+
1986,
707709
/**/
708710
1985,
709711
/**/

src/vim.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2776,6 +2776,7 @@ typedef int (*opt_expand_cb_T)(optexpand_T *args, int *numMatches, char_u ***mat
27762776
#define GLV_COMPILING TFN_COMPILING // variable may be defined later
27772777
#define GLV_ASSIGN_WITH_OP TFN_ASSIGN_WITH_OP // assignment with operator
27782778
#define GLV_PREFER_FUNC 0x10000 // prefer function above variable
2779+
#define GLV_FOR_LOOP 0x20000 // assigning to a loop variable
27792780

27802781
#define DO_NOT_FREE_CNT 99999 // refcount for dict or list that should not
27812782
// be freed.

src/vim9compile.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1737,11 +1737,14 @@ compile_lhs(
17371737
if (is_decl)
17381738
{
17391739
// if we come here with what looks like an assignment like
1740-
// .= but which has been reject by assignment_len() from
1740+
// .= but which has been rejected by assignment_len() from
17411741
// may_compile_assignment give a better error message
17421742
char_u *p = skipwhite(lhs->lhs_end);
17431743
if (p[0] == '.' && p[1] == '=')
17441744
emsg(_(e_dot_equal_not_supported_with_script_version_two));
1745+
else if (p[0] == ':')
1746+
// type specified in a non-var assignment
1747+
semsg(_(e_trailing_characters_str), p);
17451748
else
17461749
semsg(_(e_variable_already_declared_str), lhs->lhs_name);
17471750
return FAIL;

0 commit comments

Comments
 (0)