From 27b61f20b7ec7ae4eeb6e23e0dee4039370e2484 Mon Sep 17 00:00:00 2001 From: Hirohito Higashi Date: Tue, 5 Aug 2025 20:03:36 +0200 Subject: [PATCH] patch 9.1.1593: Confusing error when compiling incomplete try block Problem: Confusing error when compiling incomplete try block (lacygoill) Solution: Give better error messages (Hirohito Higashi) fixes: #17833 closes: #17853 Signed-off-by: Hirohito Higashi Signed-off-by: Christian Brabandt --- src/testdir/test_vim9_script.vim | 25 +++++++++++++++++++++++ src/version.c | 2 ++ src/vim9compile.c | 35 ++++++++++++++++++++++++-------- 3 files changed, 53 insertions(+), 9 deletions(-) diff --git a/src/testdir/test_vim9_script.vim b/src/testdir/test_vim9_script.vim index 8e968d6efb..76fc36eb1c 100644 --- a/src/testdir/test_vim9_script.vim +++ b/src/testdir/test_vim9_script.vim @@ -1426,6 +1426,31 @@ def Test_try_catch_fails() v9.CheckDefFailure(['for i in range(5)', 'endtry'], 'E170:') v9.CheckDefFailure(['if 1', 'endtry'], 'E171:') v9.CheckDefFailure(['try', 'echo 1', 'endtry'], 'E1032:') + v9.CheckDefFailure(['try'], 'E600:') + v9.CheckDefFailure(['try', 'echo 0'], 'E600:') + v9.CheckDefFailure(['try', 'echo 0', 'catch'], 'E600:') + v9.CheckDefFailure(['try', 'echo 0', 'catch', 'echo 1'], 'E600:') + v9.CheckDefFailure(['try', 'echo 0', 'catch', 'echo 1', 'finally'], 'E600:') + v9.CheckDefFailure(['try', 'echo 0', 'catch', 'echo 1', 'finally', 'echo 2'], 'E600:') + + # Missing :endtry inside a nested :try + var outer1 =<< trim END + try + echo 0 + catch + echo 1 + END + var outer2 =<< trim END + finally + echo 2 + endtry + END + v9.CheckDefFailure(outer1 + ['try'] + outer2, 'E600:') + v9.CheckDefFailure(outer1 + ['try', 'echo 10'] + outer2, 'E600:') + v9.CheckDefFailure(outer1 + ['try', 'echo 10', 'catch'] + outer2, 'E600:') + v9.CheckDefFailure(outer1 + ['try', 'echo 10', 'catch', 'echo 11'] + outer2, 'E600:') + v9.CheckDefFailure(outer1 + ['try', 'echo 10', 'catch', 'echo 11', 'finally'] + outer2, 'E607:') + v9.CheckDefFailure(outer1 + ['try', 'echo 10', 'catch', 'echo 11', 'finally', 'echo 12'] + outer2, 'E607:') v9.CheckDefFailure(['throw'], 'E1143:') v9.CheckDefFailure(['throw xxx'], 'E1001:') diff --git a/src/version.c b/src/version.c index fecf9219f0..ea483004f2 100644 --- a/src/version.c +++ b/src/version.c @@ -719,6 +719,8 @@ static char *(features[]) = static int included_patches[] = { /* Add new patch number below this line */ +/**/ + 1593, /**/ 1592, /**/ diff --git a/src/vim9compile.c b/src/vim9compile.c index f9998ceff2..65e0ff1943 100644 --- a/src/vim9compile.c +++ b/src/vim9compile.c @@ -4800,15 +4800,32 @@ compile_dfunc_scope_end_missing(cctx_T *cctx) if (cctx->ctx_scope == NULL) return FALSE; - if (cctx->ctx_scope->se_type == IF_SCOPE) - emsg(_(e_missing_endif)); - else if (cctx->ctx_scope->se_type == WHILE_SCOPE) - emsg(_(e_missing_endwhile)); - else if (cctx->ctx_scope->se_type == FOR_SCOPE) - emsg(_(e_missing_endfor)); - else - emsg(_(e_missing_rcurly)); - + switch (cctx->ctx_scope->se_type) + { + case IF_SCOPE: + emsg(_(e_missing_endif)); + break; + case WHILE_SCOPE: + emsg(_(e_missing_endwhile)); + break; + case FOR_SCOPE: + emsg(_(e_missing_endfor)); + break; + case TRY_SCOPE: + emsg(_(e_missing_endtry)); + break; + case BLOCK_SCOPE: + // end block scope from :try (maybe) + compile_endblock(cctx); + if (cctx->ctx_scope != NULL + && cctx->ctx_scope->se_type == TRY_SCOPE) + emsg(_(e_missing_endtry)); + else + emsg(_(e_missing_rcurly)); + break; + default: + emsg(_(e_missing_rcurly)); + } return TRUE; }