]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 9.0.1986: Vim9: accepting type-annotations v9.0.1986
authorYegappan Lakshmanan <yegappan@yahoo.com>
Thu, 5 Oct 2023 18:14:43 +0000 (20:14 +0200)
committerChristian Brabandt <cb@256bit.org>
Thu, 5 Oct 2023 18:16:06 +0000 (20:16 +0200)
Problem:  Vim9: accepting type-annotations
Solution: Reject type annotations outside of declarations.

closes: #13267
closes: #13283

Signed-off-by: Christian Brabandt <cb@256bit.org>
Co-authored-by: Yegappan Lakshmanan <yegappan@yahoo.com>
src/eval.c
src/evalvars.c
src/testdir/test_vim9_assign.vim
src/version.c
src/vim.h
src/vim9compile.c

index 0a335df071c888dd2c9daf74f7184b05f4149104..19ab01561de45d5f05d98cc20e458b22df79c952 100644 (file)
@@ -1271,6 +1271,14 @@ get_lval(
                    semsg(_(e_using_type_not_in_script_context_str), p);
                    return NULL;
                }
+               if (vim9script && (flags & GLV_NO_DECL) &&
+                       !(flags & GLV_FOR_LOOP))
+               {
+                   // Using a type and not in a "var" declaration.
+                   semsg(_(e_trailing_characters_str), p);
+                   return NULL;
+               }
+
 
                // parse the type after the name
                lp->ll_type = parse_type(&tp,
index 14b7dc39a4e9b248ee38d12466c58bc15ab4b85b..d7a1a96a541e07a55ee60f34c8f80412b96365bd 100644 (file)
@@ -1858,6 +1858,7 @@ ex_let_one(
        char_u  *p;
        int     lval_flags = (flags & (ASSIGN_NO_DECL | ASSIGN_DECL))
                                                             ? GLV_NO_DECL : 0;
+       lval_flags |= (flags & ASSIGN_FOR_LOOP) ? GLV_FOR_LOOP : 0;
        if (op != NULL && *op != '=')
            lval_flags |= GLV_ASSIGN_WITH_OP;
 
index ce728230b38e91723adf412b1586a63ae2965818..07afadb0c81fe2bddb83201224dfd63ed185a0f0 100644 (file)
@@ -2822,6 +2822,45 @@ def Test_using_s_var_in_function()
   v9.CheckScriptSuccess(lines)
 enddef
 
+" Test for specifying a type in assignment
+def Test_type_specification_in_assignment()
+  # specify type for an existing script local variable without "var"
+  var lines =<< trim END
+    vim9script
+    var n: number = 10
+    n: number = 20
+  END
+  v9.CheckSourceFailure(lines, 'E488: Trailing characters: : number = 20', 3)
+
+  # specify type for a non-existing script local variable without "var"
+  lines =<< trim END
+    vim9script
+    MyVar: string = 'abc'
+  END
+  v9.CheckSourceFailure(lines, "E492: Not an editor command: MyVar: string = 'abc'", 2)
+
+  # specify type for an existing def local variable without "var"
+  lines =<< trim END
+    vim9script
+    def Foo()
+      var n: number = 10
+      n: number = 20
+    enddef
+    Foo()
+  END
+  v9.CheckSourceFailure(lines, 'E488: Trailing characters: : number = 20', 2)
+
+  # specify type for a non-existing def local variable without "var"
+  lines =<< trim END
+    vim9script
+    def Foo()
+      MyVar: string = 'abc'
+    enddef
+    Foo()
+  END
+  v9.CheckSourceFailure(lines, "E476: Invalid command: MyVar: string = 'abc'", 1)
+enddef
+
 let g:someVar = 'X'
 
 " Test for heredoc with Vim expressions.
index eb517ec37e97dd2bb7cc0747366c41327bb9abd1..c93b3e582d9778fcd09e720e2332eb160d8be9ce 100644 (file)
@@ -704,6 +704,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1986,
 /**/
     1985,
 /**/
index 0a0b2f862ae1da219b2867a84bc4fc8b1f3cce42..2bafda4249f3eb3d83215bd403594f10146936c0 100644 (file)
--- a/src/vim.h
+++ b/src/vim.h
@@ -2776,6 +2776,7 @@ typedef int (*opt_expand_cb_T)(optexpand_T *args, int *numMatches, char_u ***mat
 #define GLV_COMPILING  TFN_COMPILING   // variable may be defined later
 #define GLV_ASSIGN_WITH_OP TFN_ASSIGN_WITH_OP // assignment with operator
 #define GLV_PREFER_FUNC        0x10000         // prefer function above variable
+#define GLV_FOR_LOOP   0x20000         // assigning to a loop variable
 
 #define DO_NOT_FREE_CNT 99999  // refcount for dict or list that should not
                                // be freed.
index abe1e2fb5013e278652c6c7f3ab85199c971a3e5..136bea49d3cc878242876e248a458ca78783178d 100644 (file)
@@ -1737,11 +1737,14 @@ compile_lhs(
                if (is_decl)
                {
                    // if we come here with what looks like an assignment like
-                   // .= but which has been reject by assignment_len() from
+                   // .= but which has been rejected by assignment_len() from
                    // may_compile_assignment give a better error message
                    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
+                       semsg(_(e_trailing_characters_str), p);
                    else
                        semsg(_(e_variable_already_declared_str), lhs->lhs_name);
                    return FAIL;