From aa5f3a7962c3b96d1a939a83bf2dacad72d8e898 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Fri, 5 May 2023 20:47:57 +0800 Subject: vim-patch:8.2.2094: when an expression fails getting next command may be wrong Problem: When an expression fails getting the next command may be wrong. Solution: Do not check for a next command after :eval fails. (closes vim/vim#7415) https://github.com/vim/vim/commit/d0fe620cbbf5f5e00446efa89893036265c5c302 Co-authored-by: Bram Moolenaar --- src/nvim/eval.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/nvim/eval.c b/src/nvim/eval.c index 07f65bbf22..c6a6ef6848 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -2367,7 +2367,10 @@ int eval0(char *arg, typval_T *rettv, exarg_T *eap, evalarg_T *const evalarg) semsg(_(e_invexpr2), arg); } } - ret = FAIL; + + // Some of the expression may not have been consumed. Do not check for + // a next command to avoid more errors. + return FAIL; } if (eap != NULL) { -- cgit From ad7f9a701cf25f598cb8ee3d05ad0604fa9a9a65 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Fri, 5 May 2023 20:48:25 +0800 Subject: vim-patch:8.2.2141: a user command with try/catch may not catch an expression error Problem: A user command with try/catch may not catch an expression error. Solution: When an expression fails check for following "|". (closes vim/vim#7469) https://github.com/vim/vim/commit/8143a53c533bc7776c57e5db063d185bdd5750f3 Co-authored-by: Bram Moolenaar --- src/nvim/eval.c | 6 +++++- test/old/testdir/test_trycatch.vim | 25 +++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/nvim/eval.c b/src/nvim/eval.c index c6a6ef6848..f638b90caa 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -2369,7 +2369,11 @@ int eval0(char *arg, typval_T *rettv, exarg_T *eap, evalarg_T *const evalarg) } // Some of the expression may not have been consumed. Do not check for - // a next command to avoid more errors. + // a next command to avoid more errors, unless "|" is following, which + // could only be a command separator. + if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|') { + eap->nextcmd = check_nextcmd(p); + } return FAIL; } diff --git a/test/old/testdir/test_trycatch.vim b/test/old/testdir/test_trycatch.vim index ef20e03126..a87b753a34 100644 --- a/test/old/testdir/test_trycatch.vim +++ b/test/old/testdir/test_trycatch.vim @@ -2220,6 +2220,31 @@ func Test_BufEnter_exception() %bwipe! endfunc +" Test for using try/catch in a user command with a failing expression {{{1 +func Test_user_command_try_catch() + let lines =<< trim END + function s:throw() abort + throw 'error' + endfunction + + command! Execute + \ try + \ | let s:x = s:throw() + \ | catch + \ | let g:caught = 'caught' + \ | endtry + + let g:caught = 'no' + Execute + call assert_equal('caught', g:caught) + END + call writefile(lines, 'XtestTryCatch') + source XtestTryCatch + + call delete('XtestTryCatch') + unlet g:caught +endfunc + " Test for using throw in a called function with following error {{{1 func Test_user_command_throw_in_function_call() let lines =<< trim END -- cgit From 6b912dec8ef06535b164b0f4eb3d55775eb9f6a8 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Sat, 6 May 2023 07:44:34 +0800 Subject: vim-patch:9.0.1508: catch does not work when lines are joined with a newline Problem: Catch does not work when lines are joined with a newline. Solution: Set "nextcmd" appropriately. (closes vim/vim#12348) https://github.com/vim/vim/commit/f2588b6fc90ba85b333ee8f747e8d1ebbc7e6300 --- src/nvim/eval.c | 13 ++++++++----- test/old/testdir/test_trycatch.vim | 30 ++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/src/nvim/eval.c b/src/nvim/eval.c index f638b90caa..42e53cfdfc 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -2368,11 +2368,14 @@ int eval0(char *arg, typval_T *rettv, exarg_T *eap, evalarg_T *const evalarg) } } - // Some of the expression may not have been consumed. Do not check for - // a next command to avoid more errors, unless "|" is following, which - // could only be a command separator. - if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|') { - eap->nextcmd = check_nextcmd(p); + if (eap != NULL && p != NULL) { + // Some of the expression may not have been consumed. + // Only execute a next command if it cannot be a "||" operator. + // The next command may be "catch". + char *nextcmd = check_nextcmd(p); + if (nextcmd != NULL && *nextcmd != '|') { + eap->nextcmd = nextcmd; + } } return FAIL; } diff --git a/test/old/testdir/test_trycatch.vim b/test/old/testdir/test_trycatch.vim index a87b753a34..52a687ab4f 100644 --- a/test/old/testdir/test_trycatch.vim +++ b/test/old/testdir/test_trycatch.vim @@ -2220,6 +2220,36 @@ func Test_BufEnter_exception() %bwipe! endfunc +" Test for using try/catch when lines are joined by "|" or "\n" {{{1 +func Test_try_catch_nextcmd() + func Throw() + throw "Failure" + endfunc + + let lines =<< trim END + try + let s:x = Throw() + catch + let g:caught = 1 + endtry + END + + let g:caught = 0 + call execute(lines) + call assert_equal(1, g:caught) + + let g:caught = 0 + call execute(join(lines, '|')) + call assert_equal(1, g:caught) + + let g:caught = 0 + call execute(join(lines, "\n")) + call assert_equal(1, g:caught) + + unlet g:caught + delfunc Throw +endfunc + " Test for using try/catch in a user command with a failing expression {{{1 func Test_user_command_try_catch() let lines =<< trim END -- cgit