]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 9.2.0543: Vim9: wrong error when redeclaring a typed variable v9.2.0543
authorHirohito Higashi <h.east.727@gmail.com>
Wed, 27 May 2026 19:29:16 +0000 (19:29 +0000)
committerChristian Brabandt <cb@256bit.org>
Wed, 27 May 2026 19:29:16 +0000 (19:29 +0000)
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) <noreply@anthropic.com>
Signed-off-by: Hirohito Higashi <h.east.727@gmail.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
src/testdir/test_vim9_assign.vim
src/version.c
src/vim9compile.c

index 5043fad23f7755250005bc3f87b8047546efeda2..c5f564ec7559559bbab85194a5dd08cdbf354edb 100644 (file)
@@ -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'
index 55f8b8a7ecb1f89cb7b1b4ee3ef497b4675f98b2..de7154c646cc4e4a7ed7f82a5ec5a98d95e43204 100644 (file)
@@ -729,6 +729,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    543,
 /**/
     542,
 /**/
index ea56f4ec5b841a27eacd13b8fd6780aedd2c8114..cbee429763a5f8189eec287ef064a77a003b5d14 100644 (file)
@@ -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;
     }