0
0
mirror of https://github.com/vim/vim.git synced 2025-09-24 03:44:06 -04:00

patch 8.2.2212: Vim9: lambda with => does not work at the script level

Problem:    Vim9: lambda with => does not work at the script level.
Solution:   Make it work.
This commit is contained in:
Bram Moolenaar
2020-12-25 15:24:23 +01:00
parent b2f9e0e2c5
commit c754b4cc98
6 changed files with 56 additions and 16 deletions

View File

@@ -3349,8 +3349,13 @@ eval7(
/* /*
* nested expression: (expression). * nested expression: (expression).
* lambda: (arg) => expr
*/ */
case '(': { case '(': ret = NOTDONE;
if (in_vim9script())
ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
if (ret == NOTDONE)
{
*arg = skipwhite_and_linebreak(*arg + 1, evalarg); *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
ret = eval1(arg, rettv, evalarg); // recursive! ret = eval1(arg, rettv, evalarg); // recursive!

View File

@@ -1953,15 +1953,25 @@ def Test_expr7_new_lambda()
# Lambda returning a dict # Lambda returning a dict
var Lmb = () => ({key: 42}) var Lmb = () => ({key: 42})
assert_equal({key: 42}, Lmb()) assert_equal({key: 42}, Lmb())
var RefOne: func(number): string = (a: number): string => 'x'
var RefTwo: func(number): any = (a: number): any => 'x'
var Fx = (a) => ({k1: 0,
k2: 1})
var Fy = (a) => [0,
1]
END END
CheckDefSuccess(lines) CheckDefAndScriptSuccess(lines)
CheckDefFailure(["var Ref = (a)=>a + 1"], 'E1001:') CheckDefFailure(["var Ref = (a)=>a + 1"], 'E1001:')
CheckDefFailure(["var Ref = (a)=> a + 1"], 'E1001:') CheckDefFailure(["var Ref = (a)=> a + 1"], 'E1001:')
CheckDefFailure(["var Ref = (a) =>a + 1"], 'E1001:') CheckDefFailure(["var Ref = (a) =>a + 1"], 'E1001:')
CheckDefSuccess(["var Ref: func(number): string = (a: number): string => 'x'"]) CheckScriptFailure(["vim9script", "var Ref = (a)=>a + 1"], 'E1004:')
CheckDefSuccess(["var Ref: func(number): any = (a: number): any => 'x'"]) CheckScriptFailure(["vim9script", "var Ref = (a)=> a + 1"], 'E1004:')
CheckScriptFailure(["vim9script", "var Ref = (a) =>a + 1"], 'E1004:')
CheckDefFailure(["var Ref: func(number): number = (a: number): string => 'x'"], 'E1012:') CheckDefFailure(["var Ref: func(number): number = (a: number): string => 'x'"], 'E1012:')
CheckDefFailure(["var Ref: func(number): string = (a: number): string => 99"], 'E1012:') CheckDefFailure(["var Ref: func(number): string = (a: number): string => 99"], 'E1012:')
@@ -1978,11 +1988,9 @@ def Test_expr7_new_lambda()
# 'E1106: 2 arguments too many') # 'E1106: 2 arguments too many')
# CheckDefFailure(["echo 'asdf'->{a -> a}(x)"], 'E1001:', 1) # CheckDefFailure(["echo 'asdf'->{a -> a}(x)"], 'E1001:', 1)
CheckDefSuccess(['var Fx = (a) => ({k1: 0,', ' k2: 1})'])
CheckDefFailure(['var Fx = (a) => ({k1: 0', ' k2: 1})'], 'E722:', 2) CheckDefFailure(['var Fx = (a) => ({k1: 0', ' k2: 1})'], 'E722:', 2)
CheckDefFailure(['var Fx = (a) => ({k1: 0,', ' k2 1})'], 'E720:', 2) CheckDefFailure(['var Fx = (a) => ({k1: 0,', ' k2 1})'], 'E720:', 2)
CheckDefSuccess(['var Fx = (a) => [0,', ' 1]'])
CheckDefFailure(['var Fx = (a) => [0', ' 1]'], 'E696:', 2) CheckDefFailure(['var Fx = (a) => [0', ' 1]'], 'E696:', 2)
enddef enddef

View File

@@ -461,21 +461,35 @@ register_cfunc(cfunc_T cb, cfunc_free_T cb_free, void *state)
/* /*
* Skip over "->" or "=>" after the arguments of a lambda. * Skip over "->" or "=>" after the arguments of a lambda.
* If ": type" is found make "ret_type" point to "type". * If ": type" is found make "ret_type" point to "type".
* If "white_error" is not NULL check for correct use of white space and set
* "white_error" to TRUE if there is an error.
* Return NULL if no valid arrow found. * Return NULL if no valid arrow found.
*/ */
static char_u * static char_u *
skip_arrow(char_u *start, int equal_arrow, char_u **ret_type) skip_arrow(
char_u *start,
int equal_arrow,
char_u **ret_type,
int *white_error)
{ {
char_u *s = start; char_u *s = start;
char_u *bef = start - 2; // "start" points to > of ->
if (equal_arrow) if (equal_arrow)
{ {
if (*s == ':') if (*s == ':')
{ {
if (white_error != NULL && !VIM_ISWHITE(s[1]))
{
*white_error = TRUE;
semsg(_(e_white_space_required_after_str), ":");
return NULL;
}
s = skipwhite(s + 1); s = skipwhite(s + 1);
*ret_type = s; *ret_type = s;
s = skip_type(s, TRUE); s = skip_type(s, TRUE);
} }
bef = s;
s = skipwhite(s); s = skipwhite(s);
if (*s != '=') if (*s != '=')
return NULL; return NULL;
@@ -483,6 +497,14 @@ skip_arrow(char_u *start, int equal_arrow, char_u **ret_type)
} }
if (*s != '>') if (*s != '>')
return NULL; return NULL;
if (white_error != NULL && ((!VIM_ISWHITE(*bef) && *bef != '{')
|| !IS_WHITE_OR_NUL(s[1])))
{
*white_error = TRUE;
semsg(_(e_white_space_required_before_and_after_str),
equal_arrow ? "=>" : "->");
return NULL;
}
return skipwhite(s + 1); return skipwhite(s + 1);
} }
@@ -516,6 +538,7 @@ get_lambda_tv(
int eval_lavars = FALSE; int eval_lavars = FALSE;
char_u *tofree = NULL; char_u *tofree = NULL;
int equal_arrow = **arg == '('; int equal_arrow = **arg == '(';
int white_error = FALSE;
if (equal_arrow && !in_vim9script()) if (equal_arrow && !in_vim9script())
return NOTDONE; return NOTDONE;
@@ -529,7 +552,7 @@ get_lambda_tv(
ret = get_function_args(&s, equal_arrow ? ')' : '-', NULL, ret = get_function_args(&s, equal_arrow ? ')' : '-', NULL,
types_optional ? &argtypes : NULL, types_optional, types_optional ? &argtypes : NULL, types_optional,
NULL, NULL, TRUE, NULL, NULL); NULL, NULL, TRUE, NULL, NULL);
if (ret == FAIL || skip_arrow(s, equal_arrow, &ret_type) == NULL) if (ret == FAIL || skip_arrow(s, equal_arrow, &ret_type, NULL) == NULL)
{ {
if (types_optional) if (types_optional)
ga_clear_strings(&argtypes); ga_clear_strings(&argtypes);
@@ -546,12 +569,14 @@ get_lambda_tv(
types_optional ? &argtypes : NULL, types_optional, types_optional ? &argtypes : NULL, types_optional,
&varargs, NULL, FALSE, NULL, NULL); &varargs, NULL, FALSE, NULL, NULL);
if (ret == FAIL if (ret == FAIL
|| (*arg = skip_arrow(*arg, equal_arrow, &ret_type)) == NULL) || (s = skip_arrow(*arg, equal_arrow, &ret_type,
&white_error)) == NULL)
{ {
if (types_optional) if (types_optional)
ga_clear_strings(&argtypes); ga_clear_strings(&argtypes);
return NOTDONE; return white_error ? FAIL : NOTDONE;
} }
*arg = s;
// Set up a flag for checking local variables and arguments. // Set up a flag for checking local variables and arguments.
if (evaluate) if (evaluate)
@@ -647,8 +672,6 @@ get_lambda_tv(
if (register_closure(fp) == FAIL) if (register_closure(fp) == FAIL)
goto errret; goto errret;
} }
else
fp->uf_scoped = NULL;
#ifdef FEAT_PROFILE #ifdef FEAT_PROFILE
if (prof_def_func()) if (prof_def_func())

View File

@@ -750,6 +750,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 */
/**/
2212,
/**/ /**/
2211, 2211,
/**/ /**/

View File

@@ -338,6 +338,8 @@ typval2type_int(typval_T *tv, garray_T *type_gap)
if (ufunc->uf_def_status == UF_TO_BE_COMPILED if (ufunc->uf_def_status == UF_TO_BE_COMPILED
&& compile_def_function(ufunc, TRUE, NULL) == FAIL) && compile_def_function(ufunc, TRUE, NULL) == FAIL)
return NULL; return NULL;
if (ufunc->uf_func_type == NULL)
set_function_type(ufunc);
if (ufunc->uf_func_type != NULL) if (ufunc->uf_func_type != NULL)
return ufunc->uf_func_type; return ufunc->uf_func_type;
} }