# HG changeset patch # User Bram Moolenaar # Date 1606915803 -3600 # Node ID 2f034cb0a046a009d05a17f87f9ebeba3388a50e # Parent d65e8d80869bc26e34fb755241aafb73b4f5c4c5 patch 8.2.2080: Vim9: no proper error message for using s:var in for loop Commit: https://github.com/vim/vim/commit/ea87069d7814497181483877651aef6d97182120 Author: Bram Moolenaar Date: Wed Dec 2 14:24:30 2020 +0100 patch 8.2.2080: Vim9: no proper error message for using s:var in for loop Problem: Vim9: no proper error message for using s:var in for loop. Solution: Give a specific error. diff --git a/src/testdir/test_vim9_script.vim b/src/testdir/test_vim9_script.vim --- a/src/testdir/test_vim9_script.vim +++ b/src/testdir/test_vim9_script.vim @@ -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 = [] diff --git a/src/version.c b/src/version.c --- a/src/version.c +++ b/src/version.c @@ -751,6 +751,8 @@ static char *(features[]) = static int included_patches[] = { /* Add new patch number below this line */ /**/ + 2080, +/**/ 2079, /**/ 2078, diff --git a/src/vim9compile.c b/src/vim9compile.c --- a/src/vim9compile.c +++ b/src/vim9compile.c @@ -6617,6 +6617,12 @@ compile_for(char_u *arg_start, cctx_T *c 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);