Commit: patch 9.1.1195: inside try-block: fn body executed with default arg undefined

1 view
Skip to first unread message

Christian Brabandt

unread,
Mar 12, 2025, 4:30:13 PM3/12/25
to vim...@googlegroups.com
patch 9.1.1195: inside try-block: fn body executed with default arg undefined

Commit: https://github.com/vim/vim/commit/2d18789aa67cc60072ea0cf21811c403fa0b2c7b
Author: Shane Harper <sh...@shaneharper.net>
Date: Wed Mar 12 21:12:12 2025 +0100

patch 9.1.1195: inside try-block: fn body executed with default arg undefined

Problem: inside try-block: fn body executed when default arg is
undefined
Solution: When inside a try-block do not execute function body after an
error in evaluating a default argument expression
(Shane Harper).

closes: #16865

Signed-off-by: Shane Harper <sh...@shaneharper.net>
Signed-off-by: Christian Brabandt <c...@256bit.org>

diff --git a/src/testdir/test_user_func.vim b/src/testdir/test_user_func.vim
index af7d466f6..b9f6a40e8 100644
--- a/src/testdir/test_user_func.vim
+++ b/src/testdir/test_user_func.vim
@@ -161,14 +161,35 @@ func Test_default_arg()
\ execute('func Args2'))

" Error in default argument expression
- let l =<< trim END
- func F1(x = y)
- return a:x * 2
- endfunc
- echo F1()
- END
- let @a = l->join("
")
- call assert_fails("exe @a", 'E121:')
+ func! s:f(x = s:undefined)
+ return a:x
+ endfunc
+ call assert_fails('echo s:f()', ['E121: Undefined variable: s:undefined',
+ \ 'E121: Undefined variable: a:x'])
+
+ func! s:f(x = s:undefined) abort
+ return a:x
+ endfunc
+ const expected_error = 'E121: Undefined variable: s:undefined'
+ " Only one error should be output; execution of the function should be aborted
+ " after the default argument expression error.
+ call assert_fails('echo s:f()', [expected_error, expected_error])
+endfunc
+
+func Test_default_argument_expression_error_while_inside_of_a_try_block()
+ func! s:f(v = s:undefined_variable)
+ let s:entered_fn_body = 1
+ return a:v
+ endfunc
+
+ unlet! s:entered_fn_body
+ try
+ call s:f()
+ throw "No exception."
+ catch
+ call assert_exception("E121: Undefined variable: s:undefined_variable")
+ endtry
+ call assert_false(exists('s:entered_fn_body'), "exists('s:entered_fn_body')")
endfunc

func s:addFoo(lead)
diff --git a/src/userfunc.c b/src/userfunc.c
index 531b67a55..d8d7014aa 100644
--- a/src/userfunc.c
+++ b/src/userfunc.c
@@ -3275,7 +3275,7 @@ call_user_func(
save_did_emsg = did_emsg;
did_emsg = FALSE;

- if (default_arg_err && (fp->uf_flags & FC_ABORT))
+ if (default_arg_err && (fp->uf_flags & FC_ABORT || trylevel > 0 ))
{
did_emsg = TRUE;
retval = FCERR_FAILED;
diff --git a/src/version.c b/src/version.c
index 33758889d..6297c2b4a 100644
--- a/src/version.c
+++ b/src/version.c
@@ -704,6 +704,8 @@ static char *(features[]) =

static int included_patches[] =
{ /* Add new patch number below this line */
+/**/
+ 1195,
/**/
1194,
/**/
Reply all
Reply to author
Forward
0 new messages