Patch 9.0.1558
Problem: Wrong error for unreachable code after :throw.
Solution: Adjust the error message.
Files: src/errors.h, src/vim9.h, src/vim9compile.c,
src/testdir/test_vim9_script.vim
*** ../vim-9.0.1557/src/errors.h 2023-05-06 16:21:59.594537269 +0100
--- src/errors.h 2023-05-14 21:53:53.335171062 +0100
***************
*** 2809,2816 ****
INIT(= N_("E1093: Expected %d items but got %d"));
EXTERN char e_import_can_only_be_used_in_script[]
INIT(= N_("E1094: Import can only be used in a script"));
! EXTERN char e_unreachable_code_after_return[]
! INIT(= N_("E1095: Unreachable code after :return"));
EXTERN char e_returning_value_in_function_without_return_type[]
INIT(= N_("E1096: Returning a value in a function without a return type"));
EXTERN char e_line_incomplete[]
--- 2809,2816 ----
INIT(= N_("E1093: Expected %d items but got %d"));
EXTERN char e_import_can_only_be_used_in_script[]
INIT(= N_("E1094: Import can only be used in a script"));
! EXTERN char e_unreachable_code_after_str[]
! INIT(= N_("E1095: Unreachable code after :%s"));
EXTERN char e_returning_value_in_function_without_return_type[]
INIT(= N_("E1096: Returning a value in a function without a return type"));
EXTERN char e_line_incomplete[]
*** ../vim-9.0.1557/src/vim9.h 2023-02-27 08:07:10.873872843 +0000
--- src/vim9.h 2023-05-14 21:48:33.367054020 +0100
***************
*** 842,847 ****
--- 842,848 ----
skip_T ctx_skip;
scope_T *ctx_scope; // current scope, NULL at toplevel
int ctx_had_return; // last seen statement was "return"
+ int ctx_had_throw; // last seen statement was "throw"
cctx_T *ctx_outer; // outer scope for lambda or nested
// function
*** ../vim-9.0.1557/src/vim9compile.c 2023-05-14 21:37:14.591097337 +0100
--- src/vim9compile.c 2023-05-14 22:04:01.618131724 +0100
***************
*** 3485,3491 ****
}
}
! if (cctx.ctx_had_return
&& ea.cmdidx != CMD_elseif
&& ea.cmdidx != CMD_else
&& ea.cmdidx != CMD_endif
--- 3485,3491 ----
}
}
! if ((cctx.ctx_had_return || cctx.ctx_had_throw)
&& ea.cmdidx != CMD_elseif
&& ea.cmdidx != CMD_else
&& ea.cmdidx != CMD_endif
***************
*** 3496,3504 ****
&& ea.cmdidx != CMD_endtry
&& !ignore_unreachable_code_for_testing)
{
! emsg(_(e_unreachable_code_after_return));
goto erret;
}
p = skipwhite(p);
if (ea.cmdidx != CMD_SIZE
--- 3496,3506 ----
&& ea.cmdidx != CMD_endtry
&& !ignore_unreachable_code_for_testing)
{
! semsg(_(e_unreachable_code_after_str),
! cctx.ctx_had_return ? "return" : "throw");
goto erret;
}
+ cctx.ctx_had_throw = FALSE;
p = skipwhite(p);
if (ea.cmdidx != CMD_SIZE
***************
*** 3612,3618 ****
break;
case CMD_throw:
line = compile_throw(p, &cctx);
! cctx.ctx_had_return = TRUE;
break;
case CMD_eval:
--- 3614,3620 ----
break;
case CMD_throw:
line = compile_throw(p, &cctx);
! cctx.ctx_had_throw = TRUE;
break;
case CMD_eval:
***************
*** 3765,3771 ****
goto erret;
}
! if (!cctx.ctx_had_return)
{
if (ufunc->uf_ret_type->tt_type == VAR_UNKNOWN)
ufunc->uf_ret_type = &t_void;
--- 3767,3775 ----
goto erret;
}
! // TODO: if a function ends in "throw" but there was a return elsewhere we
! // should not assume the return type is "void".
! if (!cctx.ctx_had_return && !cctx.ctx_had_throw)
{
if (ufunc->uf_ret_type->tt_type == VAR_UNKNOWN)
ufunc->uf_ret_type = &t_void;
*** ../vim-9.0.1557/src/testdir/test_vim9_script.vim 2023-05-14 21:37:14.595097337 +0100
--- src/testdir/test_vim9_script.vim 2023-05-14 21:45:13.615011629 +0100
***************
*** 812,817 ****
--- 812,841 ----
v9.CheckDefAndScriptSuccess(lines)
enddef
+ def Test_unreachable_after()
+ var lines =<< trim END
+ try
+ throw 'Error'
+ echo 'not reached'
+ catch /Error/
+ endtry
+ END
+ v9.CheckDefFailure(lines, 'E1095: Unreachable code after :throw')
+
+ lines =<< trim END
+ def SomeFunc(): number
+ try
+ return 3
+ echo 'not reached'
+ catch /Error/
+ endtry
+ return 4
+ enddef
+ defcompile
+ END
+ v9.CheckScriptFailure(lines, 'E1095: Unreachable code after :return')
+ enddef
+
def Test_throw_in_nested_try()
var lines =<< trim END
vim9script
***************
*** 1079,1084 ****
--- 1103,1110 ----
source XthrowSilenced
enddef
+ " g:DeletedFunc() is found when compiling Test_try_catch_throw() and then
+ " deleted, this should give a runtime error.
def DeletedFunc(): list<any>
return ['delete me']
enddef
*** ../vim-9.0.1557/src/version.c 2023-05-14 21:37:14.595097337 +0100
--- src/version.c 2023-05-14 22:04:13.425986919 +0100
***************
*** 697,698 ****
--- 697,700 ----
{ /* Add new patch number below this line */
+ /**/
+ 1558,
/**/
--
Have you heard about the new Beowulf cluster? It's so fast, it executes
an infinite loop in 6 seconds.
/// Bram Moolenaar -- Br...@Moolenaar.net --
http://www.Moolenaar.net \\\
/// \\\
\\\ sponsor Vim, vote for features --
http://www.Vim.org/sponsor/ ///
\\\ help me help AIDS victims --
http://ICCF-Holland.org ///