]> granicus.if.org Git - vim/commitdiff
patch 8.2.0346: Vim9: finding common list type not tested v8.2.0346
authorBram Moolenaar <Bram@vim.org>
Sun, 1 Mar 2020 22:32:25 +0000 (23:32 +0100)
committerBram Moolenaar <Bram@vim.org>
Sun, 1 Mar 2020 22:32:25 +0000 (23:32 +0100)
Problem:    Vim9: finding common list type not tested.
Solution:   Add more tests.  Fix listing function.  Fix overwriting type.

src/testdir/runtest.vim
src/testdir/test_vim9_disassemble.vim
src/testdir/test_vim9_script.vim
src/userfunc.c
src/version.c
src/vim9compile.c

index f0cc1abff32ca96577639c22f5a617d92c34f856..d8a1cf730ab204ef76d3e7a4c3a1b154eb41a20a 100644 (file)
@@ -386,7 +386,7 @@ let s:flaky_errors_re = 'StopVimInTerminal\|VerifyScreenDump'
 redir @q
 silent function /^Test_
 redir END
-let s:tests = split(substitute(@q, 'function \(\k*()\)', '\1', 'g'))
+let s:tests = split(substitute(@q, '\(function\|def\) \(\k*()\)', '\2', 'g'))
 
 " If there is an extra argument filter the function names against it.
 if argc() > 1
index 7875e847e7b4fb6e21bafe7fe7afbb1445e0cf86..5ba18dd5c3a3be00dc00c2a33926cb587e634e99 100644 (file)
@@ -814,4 +814,36 @@ def Test_disassemble_execute()
         \, res)
 enddef
 
+def SomeStringArg(arg: string)
+  echo arg
+enddef
+
+def SomeAnyArg(arg: any)
+  echo arg
+enddef
+
+def SomeStringArgAndReturn(arg: string): string
+  return arg
+enddef
+
+def Test_display_func()
+  let res1 = execute('function SomeStringArg')
+  assert_match('.* def SomeStringArg(arg: string).*'
+        \ .. '  echo arg.*'
+        \ .. '  enddef'
+        \, res1)
+
+  let res2 = execute('function SomeAnyArg')
+  assert_match('.* def SomeAnyArg(arg: any).*'
+        \ .. '  echo arg.*'
+        \ .. '  enddef'
+        \, res2)
+
+  let res3 = execute('function SomeStringArgAndReturn')
+  assert_match('.* def SomeStringArgAndReturn(arg: string): string.*'
+        \ .. '  return arg.*'
+        \ .. '  enddef'
+        \, res3)
+enddef
+
 " vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker
index c01ab8316427406eff35b18940cf2d6960ef16ab..d5e42e8dc5b0ade31b2822924210e925c559d8a7 100644 (file)
@@ -66,6 +66,9 @@ def Test_assignment()
   let party1: partial
   let party2: partial = funcref('Test_syntax')
 
+  " type becomes list<any>
+  let somelist = rand() > 0 ? [1, 2, 3] : ['a', 'b', 'c']
+
   g:newvar = 'new'
   assert_equal('new', g:newvar)
 
index 9ff78c29b5418508bb3bd37a4b49d8b0f2412d2b..c6c6cec3cb955928758370573f4541d5f13a4dd8 100644 (file)
@@ -1902,7 +1902,7 @@ printable_func_name(ufunc_T *fp)
 }
 
 /*
- * List the head of the function: "name(arg1, arg2)".
+ * List the head of the function: "function name(arg1, arg2)".
  */
     static void
 list_func_head(ufunc_T *fp, int indent)
@@ -1912,7 +1912,10 @@ list_func_head(ufunc_T *fp, int indent)
     msg_start();
     if (indent)
        msg_puts("   ");
-    msg_puts("function ");
+    if (fp->uf_dfunc_idx >= 0)
+       msg_puts("def ");
+    else
+       msg_puts("function ");
     msg_puts((char *)printable_func_name(fp));
     msg_putchar('(');
     for (j = 0; j < fp->uf_args.ga_len; ++j)
@@ -1957,7 +1960,19 @@ list_func_head(ufunc_T *fp, int indent)
        }
     }
     msg_putchar(')');
-    if (fp->uf_flags & FC_ABORT)
+
+    if (fp->uf_dfunc_idx >= 0)
+    {
+       if (fp->uf_ret_type != &t_void)
+       {
+           char *tofree;
+
+           msg_puts(": ");
+           msg_puts(type_name(fp->uf_ret_type, &tofree));
+           vim_free(tofree);
+       }
+    }
+    else if (fp->uf_flags & FC_ABORT)
        msg_puts(" abort");
     if (fp->uf_flags & FC_RANGE)
        msg_puts(" range");
index 6205a564900af5eb0dd821f9fec4b2f43420aaa6..f2f3cd7dfef6a54fd16149a91f6d2bcce7763e37 100644 (file)
@@ -738,6 +738,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    346,
 /**/
     345,
 /**/
index 158770c3a4255c261ab7ad8acb95ed191d878dd7..5d8dc90025ee07ea55e1735cf1dd71cd2347c06c 100644 (file)
@@ -1443,7 +1443,7 @@ equal_type(type_T *type1, type_T *type2)
        case VAR_BLOB:
        case VAR_JOB:
        case VAR_CHANNEL:
-           return TRUE;  // not composite is always OK
+           break;  // not composite is always OK
        case VAR_LIST:
        case VAR_DICT:
            return equal_type(type1->tt_member, type2->tt_member);
@@ -1461,27 +1461,32 @@ equal_type(type_T *type1, type_T *type2)
  * "type2" and "dest" may be the same.
  */
     static void
-common_type(type_T *type1, type_T *type2, type_T *dest)
+common_type(type_T *type1, type_T *type2, type_T **dest, garray_T *type_list)
 {
     if (equal_type(type1, type2))
     {
-       if (dest != type2)
-           *dest = *type2;
+       *dest = type1;
        return;
     }
 
     if (type1->tt_type == type2->tt_type)
     {
-       dest->tt_type = type1->tt_type;
        if (type1->tt_type == VAR_LIST || type2->tt_type == VAR_DICT)
        {
-           common_type(type1->tt_member, type2->tt_member, dest->tt_member);
+           type_T *common;
+
+           common_type(type1->tt_member, type2->tt_member, &common, type_list);
+           if (type1->tt_type == VAR_LIST)
+               *dest = get_list_type(common, type_list);
+           else
+               *dest = get_dict_type(common, type_list);
            return;
        }
        // TODO: VAR_FUNC and VAR_PARTIAL
+       *dest = type1;
     }
 
-    dest->tt_type = VAR_UNKNOWN;  // "any"
+    *dest = &t_any;
 }
 
     char *
@@ -1501,7 +1506,7 @@ vartype_name(vartype_T type)
        case VAR_CHANNEL: return "channel";
        case VAR_LIST: return "list";
        case VAR_DICT: return "dict";
-       case VAR_FUNC: return "function";
+       case VAR_FUNC: return "func";
        case VAR_PARTIAL: return "partial";
     }
     return "???";
@@ -3160,7 +3165,7 @@ compile_expr1(char_u **arg,  cctx_T *cctx)
 
        // If the types differ, the result has a more generic type.
        type2 = ((type_T **)stack->ga_data)[stack->ga_len - 1];
-       common_type(type1, type2, type2);
+       common_type(type1, type2, &type2, cctx->ctx_type_list);
 
        // jump here from JUMP_ALWAYS
        isn = ((isn_T *)instr->ga_data) + end_idx;