summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBram Moolenaar <Bram@vim.org>2021-04-05 19:41:21 +0200
committerBram Moolenaar <Bram@vim.org>2021-04-05 19:41:21 +0200
commit2927c07b0ec627c13e863e1b84bec831743bce12 (patch)
tree3b21fe010166de194f0f42b8b528b270bae5dd91
parent01ac0a1f664c5b1ffd5c9ef196d4b47edf2fd494 (diff)
patch 8.2.2721: Vim9: cannot have a linebreak inside a lambdav8.2.2721
Problem: Vim9: cannot have a linebreak inside a lambda. Solution: Compile the expression before the arguments.
-rw-r--r--src/testdir/test_vim9_expr.vim4
-rw-r--r--src/version.c2
-rw-r--r--src/vim9compile.c63
3 files changed, 49 insertions, 20 deletions
diff --git a/src/testdir/test_vim9_expr.vim b/src/testdir/test_vim9_expr.vim
index 6bee982ea9..4e1e3d7fae 100644
--- a/src/testdir/test_vim9_expr.vim
+++ b/src/testdir/test_vim9_expr.vim
@@ -2075,16 +2075,14 @@ def Test_expr7_new_lambda()
enddef
def Test_expr7_lambda_vim9script()
- # TODO: make this work in a :def function
var lines =<< trim END
- vim9script
var v = 10->((a) =>
a
+ 2
)()
assert_equal(12, v)
END
- CheckScriptSuccess(lines)
+ CheckDefAndScriptSuccess(lines)
# nested lambda with line breaks
lines =<< trim END
diff --git a/src/version.c b/src/version.c
index 358ff07fe8..331ccdbadc 100644
--- a/src/version.c
+++ b/src/version.c
@@ -751,6 +751,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
+ 2721,
+/**/
2720,
/**/
2719,
diff --git a/src/vim9compile.c b/src/vim9compile.c
index 455aa77fa2..678a985288 100644
--- a/src/vim9compile.c
+++ b/src/vim9compile.c
@@ -3975,23 +3975,25 @@ compile_subscript(
if (**arg == '(')
{
int argcount = 1;
- char_u *expr;
- garray_T *stack;
+ garray_T *stack = &cctx->ctx_type_stack;
+ int type_idx_start = stack->ga_len;
type_T *type;
+ int expr_isn_start = cctx->ctx_instr.ga_len;
+ int expr_isn_end;
+ int arg_isn_count;
// Funcref call: list->(Refs[2])(arg)
// or lambda: list->((arg) => expr)(arg)
- // Fist compile the arguments.
- expr = *arg;
- *arg = skipwhite(*arg + 1);
- skip_expr_cctx(arg, cctx);
- *arg = skipwhite(*arg);
- if (**arg != ')')
- {
- semsg(_(e_missing_paren), *arg);
+ //
+ // Fist compile the function expression.
+ if (compile_parenthesis(arg, cctx, ppconst) == FAIL)
return FAIL;
- }
- ++*arg;
+
+ // Remember the next instruction index, where the instructions
+ // for arguments are being written.
+ expr_isn_end = cctx->ctx_instr.ga_len;
+
+ // Compile the arguments.
if (**arg != '(')
{
if (*skipwhite(*arg) == '(')
@@ -4000,16 +4002,43 @@ compile_subscript(
semsg(_(e_missing_paren), *arg);
return FAIL;
}
-
*arg = skipwhite(*arg + 1);
if (compile_arguments(arg, cctx, &argcount) == FAIL)
return FAIL;
- // Compile the function expression.
- if (compile_parenthesis(&expr, cctx, ppconst) == FAIL)
- return FAIL;
-
+ // Move the instructions for the arguments to before the
+ // instructions of the expression and move the type of the
+ // expression after the argument types. This is what ISN_PCALL
+ // expects.
stack = &cctx->ctx_type_stack;
+ arg_isn_count = cctx->ctx_instr.ga_len - expr_isn_end;
+ if (arg_isn_count > 0)
+ {
+ int expr_isn_count = expr_isn_end - expr_isn_start;
+ isn_T *isn = ALLOC_MULT(isn_T, expr_isn_count);
+
+ if (isn == NULL)
+ return FAIL;
+ mch_memmove(isn, ((isn_T *)cctx->ctx_instr.ga_data)
+ + expr_isn_start,
+ sizeof(isn_T) * expr_isn_count);
+ mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
+ + expr_isn_start,
+ ((isn_T *)cctx->ctx_instr.ga_data) + expr_isn_end,
+ sizeof(isn_T) * arg_isn_count);
+ mch_memmove(((isn_T *)cctx->ctx_instr.ga_data)
+ + expr_isn_start + arg_isn_count,
+ isn, sizeof(isn_T) * expr_isn_count);
+ vim_free(isn);
+
+ type = ((type_T **)stack->ga_data)[type_idx_start];
+ mch_memmove(((type_T **)stack->ga_data) + type_idx_start,
+ ((type_T **)stack->ga_data) + type_idx_start + 1,
+ sizeof(type_T *)
+ * (stack->ga_len - type_idx_start - 1));
+ ((type_T **)stack->ga_data)[stack->ga_len - 1] = type;
+ }
+
type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
if (generate_PCALL(cctx, argcount,
(char_u *)"[expression]", type, FALSE) == FAIL)