Commit: patch 9.2.0543: Vim9: wrong error when redeclaring a typed variable

3 views
Skip to first unread message

Christian Brabandt

unread,
May 27, 2026, 3:45:15 PMMay 27
to vim...@googlegroups.com
patch 9.2.0543: Vim9: wrong error when redeclaring a typed variable

Commit: https://github.com/vim/vim/commit/caba9110aa06cfe02a3ad0aff07603b4886f65cd
Author: Hirohito Higashi <h.eas...@gmail.com>
Date: Wed May 27 19:29:16 2026 +0000

patch 9.2.0543: Vim9: wrong error when redeclaring a typed variable

Problem: In a :def function, redeclaring an existing variable with a
type annotation (e.g. "var x: number = 1" used twice) reports
"E488: Trailing characters" instead of the expected
"E1017: Variable already declared".
Solution: Report E1017 when the redeclaration uses a "var", "final" or
"const" command; keep E488 only for a type specified in an
assignment that has no declaration keyword (Hirohito Higashi).

fixes: #20337
closes: #20341

Co-Authored-By: Claude Opus 4.7 (1M context) <nor...@anthropic.com>
Signed-off-by: Hirohito Higashi <h.eas...@gmail.com>
Signed-off-by: Christian Brabandt <c...@256bit.org>

diff --git a/src/testdir/test_vim9_assign.vim b/src/testdir/test_vim9_assign.vim
index 5043fad23..c5f564ec7 100644
--- a/src/testdir/test_vim9_assign.vim
+++ b/src/testdir/test_vim9_assign.vim
@@ -3131,6 +3131,28 @@ def Test_type_specification_in_assignment()
Foo()
END
v9.CheckSourceFailure(lines, "E476: Invalid command: MyVar: string = 'abc'", 1)
+
+ # redeclare an existing def local variable with a type
+ lines =<< trim END
+ vim9script
+ def Foo()
+ var n: number = 10
+ var n: number = 20
+ enddef
+ Foo()
+ END
+ v9.CheckSourceFailure(lines, 'E1017: Variable already declared: n', 2)
+
+ # redeclare an existing def local constant with a type
+ lines =<< trim END
+ vim9script
+ def Foo()
+ const x: number = 1
+ const x: number = 2
+ enddef
+ Foo()
+ END
+ v9.CheckSourceFailure(lines, 'E1017: Variable already declared: x', 2)
enddef

let g:someVar = 'X'
diff --git a/src/version.c b/src/version.c
index 55f8b8a7e..de7154c64 100644
--- a/src/version.c
+++ b/src/version.c
@@ -729,6 +729,8 @@ static char *(features[]) =

static int included_patches[] =
{ /* Add new patch number below this line */
+/**/
+ 543,
/**/
542,
/**/
diff --git a/src/vim9compile.c b/src/vim9compile.c
index ea56f4ec5..cbee42976 100644
--- a/src/vim9compile.c
+++ b/src/vim9compile.c
@@ -1949,7 +1949,8 @@ compile_lhs_var_dest(
int cmdidx,
char_u *var_start,
char_u *var_end,
- int is_decl)
+ int is_decl,
+ int has_cmd) // "var" before "var_start"
{
int declare_error = FALSE;

@@ -2004,8 +2005,8 @@ compile_lhs_var_dest(
char_u *p = skipwhite(lhs->lhs_end);
if (p[0] == '.' && p[1] == '=')
emsg(_(e_dot_equal_not_supported_with_script_version_two));
- else if (p[0] == ':')
- // type specified in a non-var assignment
+ else if (p[0] == ':' && !has_cmd)
+ // type specified in an assignment without "var"
semsg(_(e_trailing_characters_str), p);
else
semsg(_(e_variable_already_declared_str), lhs->lhs_name);
@@ -2309,7 +2310,7 @@ compile_lhs(
{
// compile the LHS destination
if (compile_lhs_var_dest(cctx, lhs, cmdidx, var_start, var_end,
- is_decl) == FAIL)
+ is_decl, has_cmd) == FAIL)
return FAIL;
}

Reply all
Reply to author
Forward
0 new messages