mirror of
https://github.com/vim/vim.git
synced 2025-10-17 07:44:28 -04:00
patch 8.2.1272: Vim9: type not checked if declaration also assigns value
Problem: Vim9: type not checked if declaration also assigns value. Solution: Check the type. (issue #6507)
This commit is contained in:
@@ -1270,7 +1270,12 @@ set_var_lval(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
{
|
||||||
|
if (lp->ll_type != NULL
|
||||||
|
&& check_typval_type(lp->ll_type, rettv) == FAIL)
|
||||||
|
return;
|
||||||
set_var_const(lp->ll_name, lp->ll_type, rettv, copy, flags);
|
set_var_const(lp->ll_name, lp->ll_type, rettv, copy, flags);
|
||||||
|
}
|
||||||
*endp = cc;
|
*endp = cc;
|
||||||
}
|
}
|
||||||
else if (var_check_lock(lp->ll_newkey == NULL
|
else if (var_check_lock(lp->ll_newkey == NULL
|
||||||
|
@@ -1,9 +1,10 @@
|
|||||||
/* vim9compile.c */
|
/* vim9compile.c */
|
||||||
int check_defined(char_u *p, size_t len, cctx_T *cctx);
|
int check_defined(char_u *p, size_t len, cctx_T *cctx);
|
||||||
void clear_type_list(garray_T *gap);
|
void clear_type_list(garray_T *gap);
|
||||||
type_T *typval2type(typval_T *tv);
|
type_T *typval2type(typval_T *tv, garray_T *type_gap);
|
||||||
|
type_T *typval2type_vimvar(typval_T *tv, garray_T *type_gap);
|
||||||
|
int check_typval_type(type_T *expected, typval_T *actual_tv);
|
||||||
int check_type(type_T *expected, type_T *actual, int give_msg);
|
int check_type(type_T *expected, type_T *actual, int give_msg);
|
||||||
int check_argtype(type_T *expected, typval_T *actual_tv);
|
|
||||||
int check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2);
|
int check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2);
|
||||||
char_u *skip_type(char_u *start);
|
char_u *skip_type(char_u *start);
|
||||||
type_T *parse_type(char_u **arg, garray_T *type_gap);
|
type_T *parse_type(char_u **arg, garray_T *type_gap);
|
||||||
|
@@ -29,6 +29,9 @@ def Test_assignment()
|
|||||||
call CheckDefFailure(['let x:string = "x"'], 'E1069:')
|
call CheckDefFailure(['let x:string = "x"'], 'E1069:')
|
||||||
call CheckDefFailure(['let a:string = "x"'], 'E1069:')
|
call CheckDefFailure(['let a:string = "x"'], 'E1069:')
|
||||||
|
|
||||||
|
let nr: number = 1234
|
||||||
|
call CheckDefFailure(['let nr: number = "asdf"'], 'E1013:')
|
||||||
|
|
||||||
let a: number = 6 #comment
|
let a: number = 6 #comment
|
||||||
assert_equal(6, a)
|
assert_equal(6, a)
|
||||||
|
|
||||||
|
@@ -754,6 +754,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 */
|
||||||
|
/**/
|
||||||
|
1272,
|
||||||
/**/
|
/**/
|
||||||
1271,
|
1271,
|
||||||
/**/
|
/**/
|
||||||
|
@@ -478,22 +478,106 @@ func_type_add_arg_types(
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Return the type_T for a typval. Only for primitive types.
|
* Get a type_T for a typval_T.
|
||||||
|
* "type_list" is used to temporarily create types in.
|
||||||
*/
|
*/
|
||||||
type_T *
|
type_T *
|
||||||
typval2type(typval_T *tv)
|
typval2type(typval_T *tv, garray_T *type_gap)
|
||||||
{
|
{
|
||||||
|
type_T *actual;
|
||||||
|
type_T *member_type;
|
||||||
|
|
||||||
if (tv->v_type == VAR_NUMBER)
|
if (tv->v_type == VAR_NUMBER)
|
||||||
return &t_number;
|
return &t_number;
|
||||||
if (tv->v_type == VAR_BOOL)
|
if (tv->v_type == VAR_BOOL)
|
||||||
return &t_bool; // not used
|
return &t_bool; // not used
|
||||||
if (tv->v_type == VAR_STRING)
|
if (tv->v_type == VAR_STRING)
|
||||||
return &t_string;
|
return &t_string;
|
||||||
|
|
||||||
|
if (tv->v_type == VAR_LIST
|
||||||
|
&& tv->vval.v_list != NULL
|
||||||
|
&& tv->vval.v_list->lv_first != NULL)
|
||||||
|
{
|
||||||
|
// Use the type of the first member, it is the most specific.
|
||||||
|
member_type = typval2type(&tv->vval.v_list->lv_first->li_tv, type_gap);
|
||||||
|
return get_list_type(member_type, type_gap);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (tv->v_type == VAR_DICT
|
||||||
|
&& tv->vval.v_dict != NULL
|
||||||
|
&& tv->vval.v_dict->dv_hashtab.ht_used > 0)
|
||||||
|
{
|
||||||
|
dict_iterator_T iter;
|
||||||
|
typval_T *value;
|
||||||
|
|
||||||
|
// Use the type of the first value, it is the most specific.
|
||||||
|
dict_iterate_start(tv, &iter);
|
||||||
|
dict_iterate_next(&iter, &value);
|
||||||
|
member_type = typval2type(value, type_gap);
|
||||||
|
return get_dict_type(member_type, type_gap);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
|
||||||
|
{
|
||||||
|
char_u *name = NULL;
|
||||||
|
ufunc_T *ufunc = NULL;
|
||||||
|
|
||||||
|
if (tv->v_type == VAR_PARTIAL)
|
||||||
|
{
|
||||||
|
if (tv->vval.v_partial->pt_func != NULL)
|
||||||
|
ufunc = tv->vval.v_partial->pt_func;
|
||||||
|
else
|
||||||
|
name = tv->vval.v_partial->pt_name;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
name = tv->vval.v_string;
|
||||||
|
if (name != NULL)
|
||||||
|
// TODO: how about a builtin function?
|
||||||
|
ufunc = find_func(name, FALSE, NULL);
|
||||||
|
if (ufunc != NULL && ufunc->uf_func_type != NULL)
|
||||||
|
return ufunc->uf_func_type;
|
||||||
|
}
|
||||||
|
|
||||||
|
actual = alloc_type(type_gap);
|
||||||
|
if (actual == NULL)
|
||||||
|
return NULL;
|
||||||
|
actual->tt_type = tv->v_type;
|
||||||
|
actual->tt_member = &t_any;
|
||||||
|
|
||||||
|
return actual;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Get a type_T for a typval_T, used for v: variables.
|
||||||
|
* "type_list" is used to temporarily create types in.
|
||||||
|
*/
|
||||||
|
type_T *
|
||||||
|
typval2type_vimvar(typval_T *tv, garray_T *type_gap)
|
||||||
|
{
|
||||||
if (tv->v_type == VAR_LIST) // e.g. for v:oldfiles
|
if (tv->v_type == VAR_LIST) // e.g. for v:oldfiles
|
||||||
return &t_list_string;
|
return &t_list_string;
|
||||||
if (tv->v_type == VAR_DICT) // e.g. for v:completed_item
|
if (tv->v_type == VAR_DICT) // e.g. for v:completed_item
|
||||||
return &t_dict_any;
|
return &t_dict_any;
|
||||||
return &t_any; // not used
|
return typval2type(tv, type_gap);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Return FAIL if "expected" and "actual" don't match.
|
||||||
|
*/
|
||||||
|
int
|
||||||
|
check_typval_type(type_T *expected, typval_T *actual_tv)
|
||||||
|
{
|
||||||
|
garray_T type_list;
|
||||||
|
type_T *actual_type;
|
||||||
|
int res = FAIL;
|
||||||
|
|
||||||
|
ga_init2(&type_list, sizeof(type_T *), 10);
|
||||||
|
actual_type = typval2type(actual_tv, &type_list);
|
||||||
|
if (actual_type != NULL)
|
||||||
|
res = check_type(expected, actual_type, TRUE);
|
||||||
|
clear_type_list(&type_list);
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@@ -561,71 +645,6 @@ check_type(type_T *expected, type_T *actual, int give_msg)
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* Return FAIl if "expected" and "actual" don't match.
|
|
||||||
* TODO: better type comparison
|
|
||||||
*/
|
|
||||||
int
|
|
||||||
check_argtype(type_T *expected, typval_T *actual_tv)
|
|
||||||
{
|
|
||||||
type_T actual;
|
|
||||||
type_T member;
|
|
||||||
|
|
||||||
// TODO: should should be done with more levels
|
|
||||||
CLEAR_FIELD(actual);
|
|
||||||
actual.tt_type = actual_tv->v_type;
|
|
||||||
if (actual_tv->v_type == VAR_LIST
|
|
||||||
&& actual_tv->vval.v_list != NULL
|
|
||||||
&& actual_tv->vval.v_list->lv_first != NULL)
|
|
||||||
{
|
|
||||||
// Use the type of the first member, it is the most specific.
|
|
||||||
CLEAR_FIELD(member);
|
|
||||||
member.tt_type = actual_tv->vval.v_list->lv_first->li_tv.v_type;
|
|
||||||
member.tt_member = &t_any;
|
|
||||||
actual.tt_member = &member;
|
|
||||||
}
|
|
||||||
else if (actual_tv->v_type == VAR_DICT
|
|
||||||
&& actual_tv->vval.v_dict != NULL
|
|
||||||
&& actual_tv->vval.v_dict->dv_hashtab.ht_used > 0)
|
|
||||||
{
|
|
||||||
dict_iterator_T iter;
|
|
||||||
typval_T *value;
|
|
||||||
|
|
||||||
// Use the type of the first value, it is the most specific.
|
|
||||||
dict_iterate_start(actual_tv, &iter);
|
|
||||||
dict_iterate_next(&iter, &value);
|
|
||||||
CLEAR_FIELD(member);
|
|
||||||
member.tt_type = value->v_type;
|
|
||||||
member.tt_member = &t_any;
|
|
||||||
actual.tt_member = &member;
|
|
||||||
}
|
|
||||||
else if (actual_tv->v_type == VAR_FUNC || actual_tv->v_type == VAR_PARTIAL)
|
|
||||||
{
|
|
||||||
char_u *name = NULL;
|
|
||||||
ufunc_T *ufunc = NULL;
|
|
||||||
|
|
||||||
if (actual_tv->v_type == VAR_PARTIAL)
|
|
||||||
{
|
|
||||||
if (actual_tv->vval.v_partial->pt_func != NULL)
|
|
||||||
ufunc = actual_tv->vval.v_partial->pt_func;
|
|
||||||
else
|
|
||||||
name = actual_tv->vval.v_partial->pt_name;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
name = actual_tv->vval.v_string;
|
|
||||||
if (name != NULL)
|
|
||||||
// TODO: how about a builtin function?
|
|
||||||
ufunc = find_func(name, FALSE, NULL);
|
|
||||||
if (ufunc != NULL && ufunc->uf_func_type != NULL)
|
|
||||||
actual = *ufunc->uf_func_type;
|
|
||||||
else
|
|
||||||
actual.tt_member = &t_any;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
actual.tt_member = &t_any;
|
|
||||||
return check_type(expected, &actual, TRUE);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////
|
||||||
// Following generate_ functions expect the caller to call ga_grow().
|
// Following generate_ functions expect the caller to call ga_grow().
|
||||||
@@ -1314,7 +1333,7 @@ generate_LOADV(
|
|||||||
semsg(_(e_var_notfound), name);
|
semsg(_(e_var_notfound), name);
|
||||||
return FAIL;
|
return FAIL;
|
||||||
}
|
}
|
||||||
type = typval2type(get_vim_var_tv(vidx));
|
type = typval2type_vimvar(get_vim_var_tv(vidx), cctx->ctx_type_list);
|
||||||
|
|
||||||
return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
|
return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
|
||||||
}
|
}
|
||||||
@@ -5235,7 +5254,7 @@ compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
|
|||||||
goto theend;
|
goto theend;
|
||||||
dest = dest_vimvar;
|
dest = dest_vimvar;
|
||||||
vtv = get_vim_var_tv(vimvaridx);
|
vtv = get_vim_var_tv(vimvaridx);
|
||||||
type = typval2type(vtv);
|
type = typval2type_vimvar(vtv, cctx->ctx_type_list);
|
||||||
if (is_decl)
|
if (is_decl)
|
||||||
{
|
{
|
||||||
vim9_declare_error(name);
|
vim9_declare_error(name);
|
||||||
|
@@ -737,7 +737,8 @@ call_def_function(
|
|||||||
for (idx = 0; idx < argc; ++idx)
|
for (idx = 0; idx < argc; ++idx)
|
||||||
{
|
{
|
||||||
if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
|
if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
|
||||||
&& check_argtype(ufunc->uf_arg_types[idx], &argv[idx]) == FAIL)
|
&& check_typval_type(ufunc->uf_arg_types[idx], &argv[idx])
|
||||||
|
== FAIL)
|
||||||
goto failed_early;
|
goto failed_early;
|
||||||
copy_tv(&argv[idx], STACK_TV_BOT(0));
|
copy_tv(&argv[idx], STACK_TV_BOT(0));
|
||||||
++ectx.ec_stack.ga_len;
|
++ectx.ec_stack.ga_len;
|
||||||
|
@@ -557,7 +557,7 @@ check_script_var_type(typval_T *dest, typval_T *value, char_u *name)
|
|||||||
semsg(_(e_readonlyvar), name);
|
semsg(_(e_readonlyvar), name);
|
||||||
return FAIL;
|
return FAIL;
|
||||||
}
|
}
|
||||||
return check_type(sv->sv_type, typval2type(value), TRUE);
|
return check_typval_type(sv->sv_type, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
iemsg("check_script_var_type(): not found");
|
iemsg("check_script_var_type(): not found");
|
||||||
|
Reference in New Issue
Block a user