]> granicus.if.org Git - vim/commitdiff
patch 9.0.1138: crash when expecting varargs but it is something else v9.0.1138
authorBram Moolenaar <Bram@vim.org>
Tue, 3 Jan 2023 12:33:26 +0000 (12:33 +0000)
committerBram Moolenaar <Bram@vim.org>
Tue, 3 Jan 2023 12:33:26 +0000 (12:33 +0000)
Problem:    Crash when expecting varargs but it is something else.
Solution:   Only use the member when the type is a list. (closes #11774)

src/testdir/test_vim9_func.vim
src/version.c
src/vim9instr.c
src/vim9type.c

index be07aec04a4b6279b9a113692366306b10e04714..bf8b705ed5c00b91ac53089b0827d2c8323f675d 100644 (file)
@@ -1869,6 +1869,20 @@ def Test_call_varargs_only()
   v9.CheckDefFailure(['g:MyVarargsOnly("one", 2)'], 'E1013: Argument 2: type mismatch, expected string but got number')
 enddef
 
+def Test_varargs_mismatch()
+  var lines =<< trim END
+      vim9script
+
+      def Map(Fn: func(...any): number): number
+        return Fn('12')
+      enddef
+
+      var res = Map((v) => str2nr(v))
+      assert_equal(12, res)
+  END
+  v9.CheckScriptSuccess(lines)
+enddef
+
 def Test_using_var_as_arg()
   var lines =<< trim END
       def Func(x: number)
index 470853ffc1803ce62fc9f4b0ce6eaed23402371e..485512192dd8f6af7e1c52bfb321cc3d4fa497e0 100644 (file)
@@ -695,6 +695,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1138,
 /**/
     1137,
 /**/
index bd2c1b41470d1f6c20fb67adcdc21140b08c23e0..72ecbaa65ed45415fb31177367370fc798440f5b 100644 (file)
@@ -1841,7 +1841,13 @@ check_func_args_from_type(
                type_T  *expected;
 
                if (varargs && i >= type->tt_argcount - 1)
-                   expected = type->tt_args[type->tt_argcount - 1]->tt_member;
+               {
+                   expected = type->tt_args[type->tt_argcount - 1];
+                   if (expected != NULL && expected->tt_type == VAR_LIST)
+                       expected = expected->tt_member;
+                   if (expected == NULL)
+                       expected = &t_any;
+               }
                else if (i >= type->tt_min_argcount
                                             && actual->tt_type == VAR_SPECIAL)
                    expected = &t_any;
index 436a42de51d2dc9b3d45b3819813782f654d3a83..8ce59481638e34268f5c4932c4b7977a47db54aa 100644 (file)
@@ -932,8 +932,10 @@ check_argument_types(
        if (varargs && i >= type->tt_argcount - 1)
        {
            expected = type->tt_args[type->tt_argcount - 1];
-           if (expected != NULL)
+           if (expected != NULL && expected->tt_type == VAR_LIST)
                expected = expected->tt_member;
+           if (expected == NULL)
+               expected = &t_any;
        }
        else
            expected = type->tt_args[i];