mirror of
https://github.com/vim/vim.git
synced 2025-08-23 19:34:27 -04:00
patch 9.0.2155: Vim9: type not kept when assigning vars
Problem: Vim9: type not kept when assigning vars Solution: When assigning a List or a Dict value to a variable of type 'any', keep the type closes: #13639 closes: #13646 Signed-off-by: Yegappan Lakshmanan <yegappan@yahoo.com> Signed-off-by: Christian Brabandt <cb@256bit.org>
This commit is contained in:
parent
f48558e10a
commit
d33518522a
@ -3323,4 +3323,30 @@ def Test_func_rettype_check()
|
|||||||
v9.CheckSourceFailure(lines, 'E1012: Type mismatch; expected object<A> but got number', 1)
|
v9.CheckSourceFailure(lines, 'E1012: Type mismatch; expected object<A> but got number', 1)
|
||||||
enddef
|
enddef
|
||||||
|
|
||||||
|
" Test for assigning different types of value to a variable of type "any"
|
||||||
|
def Test_assign_to_any()
|
||||||
|
for [typestr, val] in [
|
||||||
|
["'bool'", 'true'],
|
||||||
|
["'number'", '100'],
|
||||||
|
["'float'", '1.1'],
|
||||||
|
["'string'", '"abc"'],
|
||||||
|
["'blob'", '0z10'],
|
||||||
|
["'list<number>'", '[1, 2, 3]'],
|
||||||
|
["'dict<number>'", '{a: 1}'],
|
||||||
|
]
|
||||||
|
var lines =<< trim eval END
|
||||||
|
vim9script
|
||||||
|
var x: any = {val}
|
||||||
|
assert_equal({typestr}, typename(x))
|
||||||
|
x = [{{a: 1}}, {{b: 2}}]
|
||||||
|
assert_equal('list<dict<number>>', typename(x))
|
||||||
|
def Foo(xarg: any, s: string)
|
||||||
|
assert_equal(s, typename(xarg))
|
||||||
|
enddef
|
||||||
|
Foo({val}, {typestr})
|
||||||
|
END
|
||||||
|
v9.CheckSourceSuccess(lines)
|
||||||
|
endfor
|
||||||
|
enddef
|
||||||
|
|
||||||
" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker
|
" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker
|
||||||
|
@ -704,6 +704,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 */
|
||||||
|
/**/
|
||||||
|
2155,
|
||||||
/**/
|
/**/
|
||||||
2154,
|
2154,
|
||||||
/**/
|
/**/
|
||||||
|
@ -201,6 +201,10 @@ set_tv_type_recurse(type_T *type)
|
|||||||
void
|
void
|
||||||
set_tv_type(typval_T *tv, type_T *type)
|
set_tv_type(typval_T *tv, type_T *type)
|
||||||
{
|
{
|
||||||
|
if (type->tt_type == VAR_ANY)
|
||||||
|
// If the variable type is "any", then keep the value type.
|
||||||
|
// e.g. var x: any = [1, 2] or var y: any = {v: 1}
|
||||||
|
return;
|
||||||
if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
|
if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
|
||||||
{
|
{
|
||||||
dict_T *d = tv->vval.v_dict;
|
dict_T *d = tv->vval.v_dict;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user