]> granicus.if.org Git - vim/commitdiff
patch 8.2.2080: Vim9: no proper error message for using s:var in for loop v8.2.2080
authorBram Moolenaar <Bram@vim.org>
Wed, 2 Dec 2020 13:24:30 +0000 (14:24 +0100)
committerBram Moolenaar <Bram@vim.org>
Wed, 2 Dec 2020 13:24:30 +0000 (14:24 +0100)
Problem:    Vim9: no proper error message for using s:var in for loop.
Solution:   Give a specific error.

src/testdir/test_vim9_script.vim
src/version.c
src/vim9compile.c

index 01396967fd5cf2068c6bc021b1089b64336968f6..e8778c8d4c3bce6c522f57efc9421273ce85c268 100644 (file)
@@ -1884,6 +1884,27 @@ def Test_for_loop_fails()
   CheckDefFailure(['for i in range(3)', 'echo 3'], 'E170:')
 enddef
 
+def Test_for_loop_script_var()
+  # cannot use s:var in a :def function
+  CheckDefFailure(['for s:var in range(3)', 'echo 3'], 'E1101:')
+
+  # can use s:var in Vim9 script, with or without s:
+  var lines =<< trim END
+    vim9script
+    var total = 0
+    for s:var in [1, 2, 3]
+      total += s:var
+    endfor
+    assert_equal(6, total)
+
+    total = 0
+    for var in [1, 2, 3]
+      total += var
+    endfor
+    assert_equal(6, total)
+  END
+enddef
+
 def Test_for_loop_unpack()
   var lines =<< trim END
       var result = []
index 3a205811249c29a5e383b5477169625f2e1aa417..e3d5b6eb938c3c4da57bd431ef0ce3b0a25832cb 100644 (file)
@@ -750,6 +750,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    2080,
 /**/
     2079,
 /**/
index 07a22daa088d2b125f05d7aaff60b425f5300ed8..5b5f8f1af3ac5c4361b3b2736d659efe72e92236 100644 (file)
@@ -6617,6 +6617,12 @@ compile_for(char_u *arg_start, cctx_T *cctx)
                goto failed;
            }
 
+           if (STRNCMP(name, "s:", 2) == 0)
+           {
+               semsg(_(e_cannot_declare_script_variable_in_function), name);
+               goto failed;
+           }
+
            // Reserve a variable to store "var".
            // TODO: check for type
            var_lvar = reserve_local(cctx, arg, varlen, FALSE, &t_any);