Patch 8.2.0519
Problem: Vim9: return type not properly checked.
Solution: Check type properly, also at runtime.
Files: src/vim9compile.c, src/testdir/test_vim9_func.vim
*** ../vim-8.2.0518/src/vim9compile.c 2020-04-05 21:38:11.637962358 +0200
--- src/vim9compile.c 2020-04-05 22:09:53.096004830 +0200
***************
*** 2432,2438 ****
if (ret == FAIL && give_msg)
type_mismatch(expected, actual);
}
! return OK;
}
/*
--- 2432,2438 ----
if (ret == FAIL && give_msg)
type_mismatch(expected, actual);
}
! return ret;
}
/*
***************
*** 2444,2450 ****
static int
need_type(type_T *actual, type_T *expected, int offset, cctx_T *cctx)
{
! if (check_type(expected, actual, FALSE))
return OK;
if (actual->tt_type != VAR_ANY && actual->tt_type != VAR_UNKNOWN)
{
--- 2444,2450 ----
static int
need_type(type_T *actual, type_T *expected, int offset, cctx_T *cctx)
{
! if (check_type(expected, actual, FALSE) == OK)
return OK;
if (actual->tt_type != VAR_ANY && actual->tt_type != VAR_UNKNOWN)
{
***************
*** 4069,4075 ****
lvar->lv_type = stacktype;
}
}
! else if (check_type(lvar->lv_type, stacktype, TRUE) == FAIL)
goto theend;
}
else if (*p != '=' && check_type(type, stacktype, TRUE) == FAIL)
--- 4069,4075 ----
lvar->lv_type = stacktype;
}
}
! else if (need_type(stacktype, lvar->lv_type, -1, cctx) == FAIL)
goto theend;
}
else if (*p != '=' && check_type(type, stacktype, TRUE) == FAIL)
*** ../vim-8.2.0518/src/testdir/test_vim9_func.vim 2020-04-05 21:38:11.637962358 +0200
--- src/testdir/test_vim9_func.vim 2020-04-05 21:50:38.267671511 +0200
***************
*** 380,385 ****
--- 380,389 ----
return arg
enddef
+ def FuncOneArgRetAny(arg: any): any
+ return arg
+ enddef
+
def Test_func_type()
let Ref1: func()
funcResult = 0
***************
*** 417,421 ****
--- 421,440 ----
CheckDefFailure(['let Ref1: func()', 'Ref1 = FuncOneArgRetNumber'], 'E1013: type mismatch, expected func() but got func(number): number')
enddef
+ def Test_func_return_type()
+ let nr: number
+ nr = FuncNoArgRetNumber()
+ assert_equal(1234, nr)
+
+ nr = FuncOneArgRetAny(122)
+ assert_equal(122, nr)
+
+ let str: string
+ str = FuncOneArgRetAny('yes')
+ assert_equal('yes', str)
+
+ CheckDefFailure(['let str: string', 'str = FuncNoArgRetNumber()'], 'E1013: type mismatch, expected string but got number')
+ enddef
+
" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker
*** ../vim-8.2.0518/src/version.c 2020-04-05 21:42:09.285006966 +0200
--- src/version.c 2020-04-05 22:14:32.543016480 +0200
***************
*** 740,741 ****
--- 740,743 ----
{ /* Add new patch number below this line */
+ /**/
+ 519,
/**/
--
It is illegal for anyone to give lighted cigars to dogs, cats, and other
domesticated animal kept as pets.
[real standing law in Illinois, United States of America]
/// Bram Moolenaar -- Br...@Moolenaar.net --
http://www.Moolenaar.net \\\
/// sponsor Vim, vote for features --
http://www.Vim.org/sponsor/ \\\
\\\ an exciting new programming language --
http://www.Zimbu.org ///
\\\ help me help AIDS victims --
http://ICCF-Holland.org ///