Compare commits

...

2 Commits

Author SHA1 Message Date
Bram Moolenaar
6914e87d3c patch 8.2.2575: Vim9: a function name with "->" in the next line doesn't work
Problem:    Vim9: a function name with "->" in the next line doesn't work.
Solution:   Recognize a function name by itself. (closes #7770)
2021-03-06 21:01:09 +01:00
Bram Moolenaar
04947cc6ed patch 8.2.2574: Vim9: crash when calling partial with wrong function
Problem:    Vim9: crash when calling partial with wrong function.
Solution:   Check argument types of called function. (closes #7912)
2021-03-06 19:26:46 +01:00
5 changed files with 89 additions and 3 deletions

View File

@@ -355,6 +355,25 @@ def Test_method_call_linebreak()
END
CheckDefAndScriptSuccess(lines)
lines =<< trim END
new
def Foo(): string
return 'the text'
enddef
def Bar(F: func): string
return F()
enddef
def Test()
Foo
->Bar()
->setline(1)
enddef
Test()
assert_equal('the text', getline(1))
bwipe!
END
CheckDefAndScriptSuccess(lines)
lines =<< trim END
new
g:shortlist

View File

@@ -2367,6 +2367,30 @@ def Test_nested_lambda_in_closure()
delete('XnestedDone')
enddef
def Test_check_func_arg_types()
var lines =<< trim END
vim9script
def F1(x: string): string
return x
enddef
def F2(x: number): number
return x + 1
enddef
def G(g: func): dict<func>
return {f: g}
enddef
def H(d: dict<func>): string
return d.f('a')
enddef
END
CheckScriptSuccess(lines + ['echo H(G(F1))'])
CheckScriptFailure(lines + ['echo H(G(F2))'], 'E1013:')
enddef
" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker

View File

@@ -750,6 +750,10 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
2575,
/**/
2574,
/**/
2573,
/**/

View File

@@ -386,6 +386,26 @@ variable_exists(char_u *name, size_t len, cctx_T *cctx)
|| find_imported(name, len, cctx) != NULL;
}
/*
* Return TRUE if "name" is a local variable, argument, script variable,
* imported or function.
*/
static int
item_exists(char_u *name, size_t len, cctx_T *cctx)
{
int is_global;
if (variable_exists(name, len, cctx))
return TRUE;
// Find a function, so that a following "->" works. Skip "g:" before a
// function name.
// Do not check for an internal function, since it might also be a
// valid command, such as ":split" versuse "split()".
is_global = (name[0] == 'g' && name[1] == ':');
return find_func(is_global ? name + 2 : name, is_global, cctx) != NULL;
}
/*
* Check if "p[len]" is already defined, either in script "import_sid" or in
* compilation context "cctx". "cctx" is NULL at the script level.
@@ -728,7 +748,7 @@ get_compare_isn(exprtype_T exprtype, vartype_T type1, vartype_T type2)
}
else if (type1 == VAR_ANY || type2 == VAR_ANY
|| ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
&& (type2 == VAR_NUMBER || type2 ==VAR_FLOAT)))
&& (type2 == VAR_NUMBER || type2 == VAR_FLOAT)))
isntype = ISN_COMPAREANY;
if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
@@ -8399,8 +8419,7 @@ compile_def_function(
}
}
p = find_ex_command(&ea, NULL, starts_with_colon ? NULL
: (int (*)(char_u *, size_t, cctx_T *))variable_exists,
&cctx);
: (int (*)(char_u *, size_t, cctx_T *))item_exists, &cctx);
if (p == NULL)
{

View File

@@ -797,7 +797,27 @@ call_by_name(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
}
if (ufunc != NULL)
{
if (ufunc->uf_arg_types != NULL)
{
int i;
typval_T *argv = STACK_TV_BOT(0) - argcount;
// The function can change at runtime, check that the argument
// types are correct.
for (i = 0; i < argcount; ++i)
{
type_T *type = i < ufunc->uf_args.ga_len
? ufunc->uf_arg_types[i] : ufunc->uf_va_type;
if (type != NULL && check_typval_arg_type(type,
&argv[i], i + 1) == FAIL)
return FAIL;
}
}
return call_ufunc(ufunc, NULL, argcount, ectx, iptr);
}
return FAIL;
}