Skip to content

Commit

Permalink
patch 8.2.4360: Vim9: allowing use of "s:" leads to inconsistencies
Browse files Browse the repository at this point in the history
Problem:    Vim9: allowing use of "s:" leads to inconsistencies.
Solution:   Disallow using "s:" in Vim9 script at the script level.
  • Loading branch information
brammool committed Feb 12, 2022
1 parent 6e28703 commit a749a42
Show file tree
Hide file tree
Showing 15 changed files with 151 additions and 61 deletions.
4 changes: 3 additions & 1 deletion src/errors.h
Expand Up @@ -2828,7 +2828,7 @@ EXTERN char e_unknown_error_while_executing_str[]
INIT(= N_("E1099: Unknown error while executing %s"));
EXTERN char e_command_not_supported_in_vim9_script_missing_var_str[]
INIT(= N_("E1100: Command not supported in Vim9 script (missing :var?): %s"));
EXTERN char e_cannot_declare_script_variable_in_function[]
EXTERN char e_cannot_declare_script_variable_in_function_str[]
INIT(= N_("E1101: Cannot declare a script variable in a function: %s"));
EXTERN char e_lambda_function_not_found_str[]
INIT(= N_("E1102: Lambda function not found: %s"));
Expand Down Expand Up @@ -3232,4 +3232,6 @@ EXTERN char e_critical_error_in_python3_initialization_check_your_installation[]
#ifdef FEAT_EVAL
EXTERN char e_function_name_must_start_with_capital_str[]
INIT(= N_("E1267: Function name must start with a capital: %s"));
EXTERN char e_cannot_use_s_colon_in_vim9_script_str[]
INIT(= N_("E1268: Cannot use s: in Vim9 script: %s"));
#endif
14 changes: 14 additions & 0 deletions src/eval.c
Expand Up @@ -878,6 +878,14 @@ get_lval(
return lp->ll_name_end;
}

// Cannot use "s:var" at the Vim9 script level. "s: type" is OK.
if (in_vim9script() && at_script_level()
&& name[0] == 's' && name[1] == ':' && !VIM_ISWHITE(name[2]))
{
semsg(_(e_cannot_use_s_colon_in_vim9_script_str), name);
return NULL;
}

// Find the end of the name.
p = find_name_end(name, &expr_start, &expr_end, fne_flags);
lp->ll_name_end = p;
Expand Down Expand Up @@ -3732,6 +3740,12 @@ eval7(
emsg(_(e_cannot_use_underscore_here));
ret = FAIL;
}
else if (evaluate && in_vim9script() && len > 2
&& s[0] == 's' && s[1] == ':')
{
semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
ret = FAIL;
}
else if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
{
// "name(..." recursive!
Expand Down
1 change: 1 addition & 0 deletions src/proto/userfunc.pro
Expand Up @@ -24,6 +24,7 @@ int call_user_func_check(ufunc_T *fp, int argcount, typval_T *argvars, typval_T
void save_funccal(funccal_entry_T *entry);
void restore_funccal(void);
funccall_T *get_current_funccal(void);
int at_script_level(void);
void delete_script_functions(int sid);
void free_all_functions(void);
int builtin_function(char_u *name, int len);
Expand Down
59 changes: 30 additions & 29 deletions src/testdir/test_vim9_assign.vim
Expand Up @@ -6,6 +6,7 @@ source term_util.vim

let s:appendToMe = 'xxx'
let s:addToMe = 111
let s:newVar = ''
let g:existing = 'yes'
let g:inc_counter = 1
let $SOME_ENV_VAR = 'some'
Expand Down Expand Up @@ -124,12 +125,12 @@ def Test_assignment()
END
v9.CheckScriptSuccess(lines)

s:appendToMe ..= 'yyy'
assert_equal('xxxyyy', s:appendToMe)
s:addToMe += 222
assert_equal(333, s:addToMe)
s:newVar = 'new'
assert_equal('new', s:newVar)
appendToMe ..= 'yyy'
assert_equal('xxxyyy', appendToMe)
addToMe += 222
assert_equal(333, addToMe)
newVar = 'new'
assert_equal('new', newVar)

set ts=7
var ts: number = &ts
Expand Down Expand Up @@ -1195,7 +1196,7 @@ def Test_assignment_default()
assert_equal(5678, nr)
enddef

let scriptvar = 'init'
let s:scriptvar = 'init'

def Test_assignment_var_list()
var lines =<< trim END
Expand Down Expand Up @@ -1243,17 +1244,17 @@ def Test_assignment_var_list()
END
v9.CheckDefAndScriptSuccess(lines)

[g:globalvar, s:scriptvar, b:bufvar] = ['global', 'script', 'buf']
[g:globalvar, scriptvar, b:bufvar] = ['global', 'script', 'buf']
assert_equal('global', g:globalvar)
assert_equal('script', s:scriptvar)
assert_equal('script', scriptvar)
assert_equal('buf', b:bufvar)

lines =<< trim END
vim9script
var s:scriptvar = 'init'
[g:globalvar, s:scriptvar, w:winvar] = ['global', 'script', 'win']
var scriptvar = 'init'
[g:globalvar, scriptvar, w:winvar] = ['global', 'script', 'win']
assert_equal('global', g:globalvar)
assert_equal('script', s:scriptvar)
assert_equal('script', scriptvar)
assert_equal('win', w:winvar)
END
v9.CheckScriptSuccess(lines)
Expand Down Expand Up @@ -1398,7 +1399,7 @@ def Test_assignment_failure()
v9.CheckDefFailure(["var xnr = xnr + 1"], 'E1001:', 1)
v9.CheckScriptFailure(['vim9script', 'var xnr = xnr + 4'], 'E121:')

v9.CheckScriptFailure(['vim9script', 'def Func()', 'var dummy = s:notfound', 'enddef', 'defcompile'], 'E1108:')
v9.CheckScriptFailure(['vim9script', 'def Func()', 'var dummy = notfound', 'enddef', 'defcompile'], 'E1001:')

v9.CheckDefFailure(['var name: list<string> = [123]'], 'expected list<string> but got list<number>')
v9.CheckDefFailure(['var name: list<number> = ["xx"]'], 'expected list<number> but got list<string>')
Expand Down Expand Up @@ -1719,9 +1720,9 @@ def Test_var_declaration()
g:var_uninit = name
name = 'text'
g:var_test = name
# prefixing s: is optional
s:name = 'prefixed'
g:var_prefixed = s:name
# prefixing s: is not allowed
name = 'prefixed'
g:var_prefixed = name

const FOO: number = 123
assert_equal(123, FOO)
Expand Down Expand Up @@ -1764,9 +1765,9 @@ def Test_var_declaration()
var xyz: string # comment

# type is inferred
var s:dict = {['a']: 222}
var dict = {['a']: 222}
def GetDictVal(key: any)
g:dict_val = s:dict[key]
g:dict_val = dict[key]
enddef
GetDictVal('a')

Expand Down Expand Up @@ -1879,13 +1880,13 @@ def Test_var_declaration_fails()
enddef

def Test_script_local_in_legacy()
# OK to define script-local later when prefixed with s:
# OK to define script-local later but before compiling
var lines =<< trim END
def SetLater()
s:legvar = 'two'
legvar = 'two'
enddef
defcompile
let s:legvar = 'one'
defcompile
call SetLater()
call assert_equal('two', s:legvar)
END
Expand All @@ -1902,7 +1903,7 @@ def Test_script_local_in_legacy()
END
v9.CheckScriptSuccess(lines)

# Not OK to leave out s: prefix when script-local defined later
# Not OK to leave out s: prefix when script-local defined after compiling
lines =<< trim END
def SetLaterNoPrefix()
legvar = 'two'
Expand Down Expand Up @@ -1944,15 +1945,15 @@ def Test_var_type_check()

lines =<< trim END
vim9script
var s:l: list<number>
s:l = []
var l: list<number>
l = []
END
v9.CheckScriptSuccess(lines)

lines =<< trim END
vim9script
var s:d: dict<number>
s:d = {}
var d: dict<number>
d = {}
END
v9.CheckScriptSuccess(lines)

Expand Down Expand Up @@ -2124,7 +2125,7 @@ def Test_unlet()
'vim9script',
'var svar = 123',
'unlet s:svar',
], 'E1081:')
], 'E1268:')
v9.CheckScriptFailure([
'vim9script',
'var svar = 123',
Expand Down Expand Up @@ -2267,14 +2268,14 @@ def Test_script_funcref_case()

lines =<< trim END
vim9script
var s:Len = (s: string): number => len(s) + 2
var Len = (s: string): number => len(s) + 2
assert_equal(6, Len('asdf'))
END
v9.CheckScriptSuccess(lines)

lines =<< trim END
vim9script
var s:len = (s: string): number => len(s) + 1
var len = (s: string): number => len(s) + 1
END
v9.CheckScriptFailure(lines, 'E704:')
enddef
Expand Down
4 changes: 2 additions & 2 deletions src/testdir/test_vim9_builtin.vim
Expand Up @@ -2001,9 +2001,9 @@ def Test_insert()
v9.CheckDefExecAndScriptFailure(lines, 'E1131:', 1)

assert_equal([1, 2, 3], insert([2, 3], 1))
assert_equal([1, 2, 3], insert([2, 3], s:number_one))
assert_equal([1, 2, 3], insert([2, 3], number_one))
assert_equal([1, 2, 3], insert([1, 2], 3, 2))
assert_equal([1, 2, 3], insert([1, 2], 3, s:number_two))
assert_equal([1, 2, 3], insert([1, 2], 3, number_two))
assert_equal(['a', 'b', 'c'], insert(['b', 'c'], 'a'))
assert_equal(0z1234, insert(0z34, 0x12))

Expand Down
7 changes: 6 additions & 1 deletion src/testdir/test_vim9_cmd.vim
Expand Up @@ -1718,7 +1718,12 @@ def Test_var_not_cmd()
lines =<< trim END
s:notexist:repl
END
v9.CheckDefAndScriptFailure(lines, ['E488: Trailing characters: :repl', 'E121: Undefined variable: s:notexist'], 1)
v9.CheckDefAndScriptFailure(lines, ['E488: Trailing characters: :repl', 'E1268:'], 1)

lines =<< trim END
notexist:repl
END
v9.CheckDefAndScriptFailure(lines, ['E476:', 'E492:'], 1)

lines =<< trim END
s-pat-repl
Expand Down
4 changes: 2 additions & 2 deletions src/testdir/test_vim9_disassemble.vim
Expand Up @@ -300,11 +300,11 @@ def Test_disassemble_push()
vim9script
import autoload 'autoscript.vim'

def s:AutoloadFunc()
def AutoloadFunc()
&operatorfunc = autoscript.Opfunc
enddef

var res = execute('disass s:AutoloadFunc')
var res = execute('disass AutoloadFunc')
assert_match('<SNR>\d*_AutoloadFunc.*' ..
'&operatorfunc = autoscript.Opfunc\_s*' ..
'0 AUTOLOAD autoscript#Opfunc\_s*' ..
Expand Down
8 changes: 4 additions & 4 deletions src/testdir/test_vim9_expr.vim
Expand Up @@ -2329,7 +2329,7 @@ def Test_expr8_lambda_vim9script()
v9.CheckDefAndScriptSuccess(lines)
enddef

def Test_expr8_funcref()
def Test_expr8funcref()
var lines =<< trim END
def RetNumber(): number
return 123
Expand All @@ -2344,7 +2344,7 @@ def Test_expr8_funcref()
func g:GlobalFunc()
return 'global'
endfunc
func s:ScriptFunc()
func ScriptFunc()
return 'script'
endfunc
def Test()
Expand All @@ -2353,7 +2353,7 @@ def Test_expr8_funcref()
Ref = g:GlobalFunc
assert_equal('global', Ref())

Ref = s:ScriptFunc
Ref = ScriptFunc
assert_equal('script', Ref())
Ref = ScriptFunc
assert_equal('script', Ref())
Expand Down Expand Up @@ -3347,7 +3347,7 @@ func Test_expr8_fails()
call v9.CheckDefAndScriptFailure(["var x = &notexist"], 'E113:', 1)
call v9.CheckDefAndScriptFailure(["&grepprg = [343]"], ['E1012:', 'E730:'], 1)

call v9.CheckDefExecAndScriptFailure(["echo s:doesnt_exist"], 'E121:', 1)
call v9.CheckDefExecAndScriptFailure(["echo s:doesnt_exist"], ['E121:', 'E1268:'], 1)
call v9.CheckDefExecAndScriptFailure(["echo g:doesnt_exist"], 'E121:', 1)

call v9.CheckDefAndScriptFailure(["echo a:somevar"], ['E1075:', 'E121:'], 1)
Expand Down
12 changes: 6 additions & 6 deletions src/testdir/test_vim9_func.vim
Expand Up @@ -713,7 +713,7 @@ def Test_nested_function()

lines =<< trim END
vim9script
def s:_Func()
def _Func()
echo 'bad'
enddef
END
Expand Down Expand Up @@ -930,7 +930,7 @@ def Test_global_local_function()
def g:Funcy()
echo 'funcy'
enddef
s:Funcy()
Funcy()
END
v9.CheckScriptFailure(lines, 'E117:')
enddef
Expand Down Expand Up @@ -1441,10 +1441,10 @@ enddef
def Test_use_script_func_name_with_prefix()
var lines =<< trim END
vim9script
func s:Getit()
func g:Getit()
return 'it'
endfunc
var Fn = s:Getit
var Fn = g:Getit
assert_equal('it', Fn())
END
v9.CheckScriptSuccess(lines)
Expand Down Expand Up @@ -2849,7 +2849,7 @@ def Test_nested_inline_lambda()
lines =<< trim END
vim9script

def s:Func()
def Func()
range(10)
->mapnew((_, _) => ({
key: range(10)->mapnew((_, _) => {
Expand Down Expand Up @@ -3168,7 +3168,7 @@ def Test_invalid_function_name()
vim9script
def s: list<string>
END
v9.CheckScriptFailure(lines, 'E129:')
v9.CheckScriptFailure(lines, 'E1268:')

lines =<< trim END
vim9script
Expand Down
12 changes: 6 additions & 6 deletions src/testdir/test_vim9_import.vim
Expand Up @@ -1124,7 +1124,7 @@ def Test_vim9_reload_noclear()
lines =<< trim END
vim9script noclear
g:loadCount += 1
var s:reloaded = 'init'
var reloaded = 'init'
import './XExportReload' as exp

def Again(): string
Expand All @@ -1133,13 +1133,13 @@ def Test_vim9_reload_noclear()

exp.TheFunc()

if exists('s:loaded') | finish | endif
var s:loaded = true
if exists('loaded') | finish | endif
var loaded = true

var s:notReloaded = 'yes'
s:reloaded = 'first'
var notReloaded = 'yes'
reloaded = 'first'
def g:Values(): list<string>
return [s:reloaded, s:notReloaded, Again(), Once(), exp.exported]
return [reloaded, notReloaded, Again(), Once(), exp.exported]
enddef

def Once(): string
Expand Down

0 comments on commit a749a42

Please sign in to comment.