]> granicus.if.org Git - vim/commitdiff
patch 8.2.4572: Vim9: return type "any" is changed to first returned type v8.2.4572
authorBram Moolenaar <Bram@vim.org>
Tue, 15 Mar 2022 12:28:10 +0000 (12:28 +0000)
committerBram Moolenaar <Bram@vim.org>
Tue, 15 Mar 2022 12:28:10 +0000 (12:28 +0000)
Problem:    Vim9: return type "any" is sometimes changed to first returned
            type.  (Virginia Senioria)
Solution:   Do not change the return type if declared as "any". (closes #9949)

src/testdir/test_vim9_func.vim
src/version.c
src/vim9cmds.c

index 8c99ad40097ae865ad6f4527c2eaefbb9b1d1c2f..06f135a459814a48c6c7d4ab35740b5a6ad27bec 100644 (file)
@@ -535,6 +535,30 @@ def Test_return_list_any()
   v9.CheckScriptSuccess(lines)
 enddef
 
+def Test_return_any_two_types()
+  var lines =<< trim END
+      vim9script
+
+      def G(Fn: func(string): any)
+        g:result = Fn("hello")
+      enddef
+
+      def F(a: number, b: string): any
+        echo b
+        if a > 0
+          return 1
+        else
+          return []
+        endif
+      enddef
+
+      G(function(F, [1]))
+  END
+  v9.CheckScriptSuccess(lines)
+  assert_equal(1, g:result)
+  unlet g:result
+enddef
+
 func s:Increment()
   let g:counter += 1
 endfunc
index 777f13116737b80869cbbc7aac694067277c2855..d9752fd101719a509615186125cb3b993ad27402 100644 (file)
@@ -750,6 +750,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    4572,
 /**/
     4571,
 /**/
index b8a511f27913246994cbba918f641cee5afd9f86..cc2c0415837e74e2591773d72f81098f4b11dcb1 100644 (file)
@@ -2258,8 +2258,7 @@ compile_return(char_u *arg, int check_return_type, int legacy, cctx_T *cctx)
            // return type here.
            stack_type = get_type_on_stack(cctx, 0);
            if ((check_return_type && (cctx->ctx_ufunc->uf_ret_type == NULL
-                               || cctx->ctx_ufunc->uf_ret_type == &t_unknown
-                               || cctx->ctx_ufunc->uf_ret_type == &t_any))
+                               || cctx->ctx_ufunc->uf_ret_type == &t_unknown))
                    || (!check_return_type
                                && cctx->ctx_ufunc->uf_ret_type == &t_unknown))
            {