mirror of
https://github.com/vim/vim.git
synced 2025-09-24 03:44:06 -04:00
patch 8.2.0972: Vim9 script variable declarations need a type
Problem: Vim9 script variable declarations need a type. Solution: Make "let var: type" declare a script-local variable.
This commit is contained in:
@@ -728,8 +728,18 @@ ex_let(exarg_T *eap)
|
||||
else if (expr[0] == '.')
|
||||
emsg(_("E985: .= is not supported with script version 2"));
|
||||
else if (!ends_excmd2(eap->cmd, arg))
|
||||
// ":let var1 var2"
|
||||
{
|
||||
if (current_sctx.sc_version == SCRIPT_VERSION_VIM9)
|
||||
{
|
||||
// Vim9 declaration ":let var: type"
|
||||
arg = vim9_declare_scriptvar(eap, arg);
|
||||
}
|
||||
else
|
||||
{
|
||||
// ":let var1 var2" - list values
|
||||
arg = list_arg_vars(eap, arg, &first);
|
||||
}
|
||||
}
|
||||
else if (!eap->skip)
|
||||
{
|
||||
// ":let"
|
||||
|
@@ -1785,6 +1785,8 @@ EXTERN char e_white_after[] INIT(= N_("E1069: white space required after '%s'"))
|
||||
EXTERN char e_no_white_before[] INIT(= N_("E1068: No white space allowed before '%s'"));
|
||||
|
||||
EXTERN char e_lock_unlock[] INIT(= N_("E940: Cannot lock or unlock variable %s"));
|
||||
EXTERN char e_const_req_value[] INIT(= N_("E1021: const requires a value"));
|
||||
EXTERN char e_type_req[] INIT(= N_("E1022: type or initialization required"));
|
||||
#endif
|
||||
#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
|
||||
EXTERN char e_alloc_color[] INIT(= N_("E254: Cannot allocate color %s"));
|
||||
|
@@ -6,4 +6,5 @@ void free_imports(int sid);
|
||||
void ex_import(exarg_T *eap);
|
||||
int find_exported(int sid, char_u **argp, int *name_len, ufunc_T **ufunc, type_T **type);
|
||||
char_u *handle_import(char_u *arg_start, garray_T *gap, int import_sid, void *cctx);
|
||||
char_u *vim9_declare_scriptvar(exarg_T *eap, char_u *arg);
|
||||
/* vim: set ft=c : */
|
||||
|
@@ -1815,6 +1815,22 @@ def Test_let_missing_type()
|
||||
CheckScriptSuccess(lines)
|
||||
enddef
|
||||
|
||||
def Test_let_declaration()
|
||||
let lines =<< trim END
|
||||
vim9script
|
||||
let var: string
|
||||
g:var_uninit = var
|
||||
var = 'text'
|
||||
g:var_test = var
|
||||
END
|
||||
CheckScriptSuccess(lines)
|
||||
assert_equal('', g:var_uninit)
|
||||
assert_equal('text', g:var_test)
|
||||
|
||||
unlet g:var_uninit
|
||||
unlet g:var_test
|
||||
enddef
|
||||
|
||||
def Test_forward_declaration()
|
||||
let lines =<< trim END
|
||||
vim9script
|
||||
|
@@ -754,6 +754,8 @@ static char *(features[]) =
|
||||
|
||||
static int included_patches[] =
|
||||
{ /* Add new patch number below this line */
|
||||
/**/
|
||||
972,
|
||||
/**/
|
||||
971,
|
||||
/**/
|
||||
|
@@ -2255,7 +2255,8 @@ get_script_item_idx(int sid, char_u *name, int check_writable)
|
||||
}
|
||||
|
||||
/*
|
||||
* Find "name" in imported items of the current script/
|
||||
* Find "name" in imported items of the current script or in "cctx" if not
|
||||
* NULL.
|
||||
*/
|
||||
imported_T *
|
||||
find_imported(char_u *name, size_t len, cctx_T *cctx)
|
||||
@@ -5012,12 +5013,12 @@ compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
|
||||
}
|
||||
else if (cmdidx == CMD_const)
|
||||
{
|
||||
emsg(_("E1021: const requires a value"));
|
||||
emsg(_(e_const_req_value));
|
||||
goto theend;
|
||||
}
|
||||
else if (!has_type || dest == dest_option)
|
||||
{
|
||||
emsg(_("E1022: type or initialization required"));
|
||||
emsg(_(e_type_req));
|
||||
goto theend;
|
||||
}
|
||||
else
|
||||
|
@@ -434,4 +434,59 @@ handle_import(char_u *arg_start, garray_T *gap, int import_sid, void *cctx)
|
||||
return cmd_end;
|
||||
}
|
||||
|
||||
/*
|
||||
* Declare a script-local variable without init: "let var: type".
|
||||
* "const" is an error since the value is missing.
|
||||
* Returns a pointer to after the type.
|
||||
*/
|
||||
char_u *
|
||||
vim9_declare_scriptvar(exarg_T *eap, char_u *arg)
|
||||
{
|
||||
char_u *p;
|
||||
char_u *name;
|
||||
scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
|
||||
type_T *type;
|
||||
int called_emsg_before = called_emsg;
|
||||
typval_T init_tv;
|
||||
|
||||
if (eap->cmdidx == CMD_const)
|
||||
{
|
||||
emsg(_(e_const_req_value));
|
||||
return arg + STRLEN(arg);
|
||||
}
|
||||
|
||||
// Check for valid starting character.
|
||||
if (!eval_isnamec1(*arg))
|
||||
{
|
||||
semsg(_(e_invarg2), arg);
|
||||
return arg + STRLEN(arg);
|
||||
}
|
||||
|
||||
for (p = arg + 1; *p != NUL && *p != ':' && eval_isnamec(*p);
|
||||
MB_PTR_ADV(p))
|
||||
;
|
||||
|
||||
if (*p != ':')
|
||||
{
|
||||
emsg(_(e_type_req));
|
||||
return arg + STRLEN(arg);
|
||||
}
|
||||
name = vim_strnsave(arg, p - arg);
|
||||
|
||||
// parse type
|
||||
p = skipwhite(p + 1);
|
||||
type = parse_type(&p, &si->sn_type_list);
|
||||
if (called_emsg != called_emsg_before)
|
||||
return p;
|
||||
|
||||
// Create the variable with 0/NULL value.
|
||||
CLEAR_FIELD(init_tv);
|
||||
init_tv.v_type = type->tt_type;
|
||||
set_var_const(name, type, &init_tv, FALSE, 0);
|
||||
|
||||
vim_free(name);
|
||||
return p;
|
||||
}
|
||||
|
||||
|
||||
#endif // FEAT_EVAL
|
||||
|
Reference in New Issue
Block a user