diff options
author | Bram Moolenaar <Bram@vim.org> | 2021-03-11 20:04:04 +0100 |
---|---|---|
committer | Bram Moolenaar <Bram@vim.org> | 2021-03-11 20:04:04 +0100 |
commit | 12bce95887f7c5f07a7bdb7b4485b53d074ccc0a (patch) | |
tree | 15534bf0eccec02bfde0f92762be82673e2ec351 | |
parent | 776b954622b45125dfdcb4a61243ca90956b0825 (diff) | |
download | vim-git-12bce95887f7c5f07a7bdb7b4485b53d074ccc0a.tar.gz |
patch 8.2.2590: Vim9: default argument value may cause internal errorv8.2.2590
Problem: Vim9: default argument value may cause internal error.
Solution: Hide later function arguments when compiling the expression.
(closes #7948)
-rw-r--r-- | src/testdir/test_vim9_func.vim | 8 | ||||
-rw-r--r-- | src/version.c | 2 | ||||
-rw-r--r-- | src/vim9compile.c | 11 |
3 files changed, 20 insertions, 1 deletions
diff --git a/src/testdir/test_vim9_func.vim b/src/testdir/test_vim9_func.vim index 810b3a3cf..6258b7d5d 100644 --- a/src/testdir/test_vim9_func.vim +++ b/src/testdir/test_vim9_func.vim @@ -311,6 +311,14 @@ def Test_call_default_args() delfunc g:Func CheckScriptFailure(['def Func(arg: number = "text")', 'enddef', 'defcompile'], 'E1013: Argument 1: type mismatch, expected number but got string') delfunc g:Func + + var lines =<< trim END + vim9script + def Func(a = b == 0 ? 1 : 2, b = 0) + enddef + defcompile + END + CheckScriptFailure(lines, 'E1001: Variable not found: b') enddef def FuncWithComment( # comment diff --git a/src/version.c b/src/version.c index 87ac80f5b..1eecf5d98 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 */ /**/ + 2590, +/**/ 2589, /**/ 2588, diff --git a/src/vim9compile.c b/src/vim9compile.c index d29c66b66..c49c49984 100644 --- a/src/vim9compile.c +++ b/src/vim9compile.c @@ -8199,6 +8199,7 @@ compile_def_function( { int count = ufunc->uf_def_args.ga_len; int first_def_arg = ufunc->uf_args.ga_len - count; + int uf_args_len = ufunc->uf_args.ga_len; int i; char_u *arg; int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0); @@ -8211,16 +8212,24 @@ compile_def_function( ufunc->uf_def_arg_idx = ALLOC_CLEAR_MULT(int, count + 1); if (ufunc->uf_def_arg_idx == NULL) goto erret; + SOURCING_LNUM = 0; // line number unknown for (i = 0; i < count; ++i) { garray_T *stack = &cctx.ctx_type_stack; type_T *val_type; int arg_idx = first_def_arg + i; where_T where; + int r; + + // Make sure later arguments are not found. + ufunc->uf_args.ga_len = i; ufunc->uf_def_arg_idx[i] = instr->ga_len; arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i]; - if (compile_expr0(&arg, &cctx) == FAIL) + r = compile_expr0(&arg, &cctx); + + ufunc->uf_args.ga_len = uf_args_len; + if (r == FAIL) goto erret; // If no type specified use the type of the default value. |