# HG changeset patch # User Bram Moolenaar # Date 1642532403 -3600 # Node ID 47dbeda35910f460cbbe120c33d3f5c7fd027e93 # Parent daeffa5e4c8c92574a19f541719043bbd5a65422 patch 8.2.4138: Vim9: no error for return with argument when invalid Commit: https://github.com/vim/vim/commit/ef7aadbe36ad43a1b909f5f6c7e9b170ad62ef91 Author: Bram Moolenaar Date: Tue Jan 18 18:46:07 2022 +0000 patch 8.2.4138: Vim9: no error for return with argument when invalid Problem: Vim9: no error for return with argument when the function does not return anything. Solution: Give an error for the invalid argument. (issue #9497) diff --git a/src/testdir/test_vim9_func.vim b/src/testdir/test_vim9_func.vim --- a/src/testdir/test_vim9_func.vim +++ b/src/testdir/test_vim9_func.vim @@ -341,6 +341,20 @@ def Test_return_something() ReturnString()->assert_equal('string') ReturnNumber()->assert_equal(123) assert_fails('ReturnGlobal()', 'E1012: Type mismatch; expected number but got string', '', 1, 'ReturnGlobal') + + var lines =<< trim END + vim9script + + def Msg() + echomsg 'in Msg()...' + enddef + + def Func() + return Msg() + enddef + defcompile + END + CheckScriptFailure(lines, 'E1096:') enddef def Test_check_argument_type() diff --git a/src/version.c b/src/version.c --- 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 */ /**/ + 4138, +/**/ 4137, /**/ 4136, diff --git a/src/vim9cmds.c b/src/vim9cmds.c --- a/src/vim9cmds.c +++ b/src/vim9cmds.c @@ -2196,6 +2196,11 @@ compile_return(char_u *arg, int check_re if (*p != NUL && *p != '|' && *p != '\n') { + if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID) + { + emsg(_(e_returning_value_in_function_without_return_type)); + return NULL; + } if (legacy) { int save_flags = cmdmod.cmod_flags; @@ -2231,13 +2236,6 @@ compile_return(char_u *arg, int check_re } else { - if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID - && stack_type->tt_type != VAR_VOID - && stack_type->tt_type != VAR_UNKNOWN) - { - emsg(_(e_returning_value_in_function_without_return_type)); - return NULL; - } if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1, 0, cctx, FALSE, FALSE) == FAIL) return NULL;