0
0
mirror of https://github.com/vim/vim.git synced 2025-09-26 04:04:07 -04:00

patch 8.2.3682: Vim9: assigning to a script variable drops the type

Problem:    Vim9: assigning to a script variable drops the required type.
Solution:   Lookup the type of the variable and use it. (closes #9219)
This commit is contained in:
Bram Moolenaar
2021-11-26 17:36:51 +00:00
parent bfc5786a61
commit 7824fc80f6
5 changed files with 40 additions and 28 deletions

View File

@@ -3206,13 +3206,14 @@ set_var(
void void
set_var_const( set_var_const(
char_u *name, char_u *name,
type_T *type, type_T *type_arg,
typval_T *tv_arg, typval_T *tv_arg,
int copy, // make copy of value in "tv" int copy, // make copy of value in "tv"
int flags_arg, // ASSIGN_CONST, ASSIGN_FINAL, etc. int flags_arg, // ASSIGN_CONST, ASSIGN_FINAL, etc.
int var_idx) // index for ":let [a, b] = list" int var_idx) // index for ":let [a, b] = list"
{ {
typval_T *tv = tv_arg; typval_T *tv = tv_arg;
type_T *type = type_arg;
typval_T bool_tv; typval_T bool_tv;
dictitem_T *di; dictitem_T *di;
typval_T *dest_tv = NULL; typval_T *dest_tv = NULL;
@@ -3334,13 +3335,18 @@ set_var_const(
if (var_in_vim9script && (flags & ASSIGN_FOR_LOOP) == 0) if (var_in_vim9script && (flags & ASSIGN_FOR_LOOP) == 0)
{ {
where_T where = WHERE_INIT; where_T where = WHERE_INIT;
svar_T *sv = find_typval_in_script(&di->di_tv);
if (sv != NULL)
{
// check the type and adjust to bool if needed // check the type and adjust to bool if needed
where.wt_index = var_idx; where.wt_index = var_idx;
where.wt_variable = TRUE; where.wt_variable = TRUE;
if (check_script_var_type(&di->di_tv, tv, name, where) if (check_script_var_type(sv, tv, name, where) == FAIL)
== FAIL)
goto failed; goto failed;
if (type == NULL)
type = sv->sv_type;
}
} }
if ((flags & ASSIGN_FOR_LOOP) == 0 if ((flags & ASSIGN_FOR_LOOP) == 0

View File

@@ -16,6 +16,6 @@ char_u *vim9_declare_scriptvar(exarg_T *eap, char_u *arg);
void update_vim9_script_var(int create, dictitem_T *di, int flags, typval_T *tv, type_T **type, int do_member); void update_vim9_script_var(int create, dictitem_T *di, int flags, typval_T *tv, type_T **type, int do_member);
void hide_script_var(scriptitem_T *si, int idx, int func_defined); void hide_script_var(scriptitem_T *si, int idx, int func_defined);
svar_T *find_typval_in_script(typval_T *dest); svar_T *find_typval_in_script(typval_T *dest);
int check_script_var_type(typval_T *dest, typval_T *value, char_u *name, where_T where); int check_script_var_type(svar_T *sv, typval_T *value, char_u *name, where_T where);
int check_reserved_name(char_u *name); int check_reserved_name(char_u *name);
/* vim: set ft=c : */ /* vim: set ft=c : */

View File

@@ -322,6 +322,16 @@ def Test_skipped_assignment()
CheckDefAndScriptSuccess(lines) CheckDefAndScriptSuccess(lines)
enddef enddef
def Test_assign_keep_type()
var lines =<< trim END
vim9script
var l: list<number> = [123]
l = [123]
l->add('string')
END
CheckScriptFailure(lines, 'E1012:', 4)
enddef
def Test_assign_unpack() def Test_assign_unpack()
var lines =<< trim END var lines =<< trim END
var v1: number var v1: number

View File

@@ -757,6 +757,8 @@ static char *(features[]) =
static int included_patches[] = static int included_patches[] =
{ /* Add new patch number below this line */ { /* Add new patch number below this line */
/**/
3682,
/**/ /**/
3681, 3681,
/**/ /**/

View File

@@ -999,16 +999,13 @@ find_typval_in_script(typval_T *dest)
*/ */
int int
check_script_var_type( check_script_var_type(
typval_T *dest, svar_T *sv,
typval_T *value, typval_T *value,
char_u *name, char_u *name,
where_T where) where_T where)
{ {
svar_T *sv = find_typval_in_script(dest);
int ret; int ret;
if (sv != NULL)
{
if (sv->sv_const != 0) if (sv->sv_const != 0)
{ {
semsg(_(e_cannot_change_readonly_variable_str), name); semsg(_(e_cannot_change_readonly_variable_str), name);
@@ -1025,9 +1022,6 @@ check_script_var_type(
value->vval.v_number = val ? VVAL_TRUE : VVAL_FALSE; value->vval.v_number = val ? VVAL_TRUE : VVAL_FALSE;
} }
return ret; return ret;
}
return OK; // not really
} }
// words that cannot be used as a variable // words that cannot be used as a variable