]> granicus.if.org Git - vim/commitdiff
patch 8.2.1435: Vim9: always converting to string for ".." leads to mistakes v8.2.1435
authorBram Moolenaar <Bram@vim.org>
Wed, 12 Aug 2020 19:34:49 +0000 (21:34 +0200)
committerBram Moolenaar <Bram@vim.org>
Wed, 12 Aug 2020 19:34:49 +0000 (21:34 +0200)
Problem:    Vim9: always converting to string for ".." leads to mistakes.
Solution:   Only automatically convert simple types.

runtime/doc/vim9.txt
src/eval.c
src/evalfunc.c
src/proto/vim9execute.pro
src/testdir/test_vim9_disassemble.vim
src/testdir/test_vim9_expr.vim
src/version.c
src/vim9.h
src/vim9compile.c
src/vim9execute.c

index 7c4a64f972dc4169162fe44b03ec5863ac53ebe0..f9fd1367ddac5f02a4a37edca0bc0ed33e041d7c 100644 (file)
@@ -426,11 +426,14 @@ The boolean operators "||" and "&&" do not change the value: >
        2 && 0   == 0
        [] && 2  == []
 
-When using `..` for string concatenation the arguments are always converted to
-string. >
+When using `..` for string concatenation arguments of simple types are always
+converted to string. >
        'hello ' .. 123  == 'hello 123'
        'hello ' .. v:true  == 'hello true'
 
+Simple types are string, float, special and bool.  For other types |string()|
+can be used.
+
 In Vim9 script one can use "true" for v:true and "false" for v:false.
 
 
@@ -805,6 +808,9 @@ actually needed.  A recommended mechanism:
           ...
 <   This goes in .../import/someother.vim.
 
+When compiling a `:def` function and a function in an autoload script is
+encountered, the script is not loaded until the `:def` function is called.
+
 
 Import in legacy Vim script ~
 
index 99554e7e263e2268aefd7992e49d1f278b47e451..07b30c723a996059bb26725849270ae196ce1b51 100644 (file)
@@ -2712,7 +2712,7 @@ eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
            return FAIL;
        }
        *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
-       if (eval6(arg, &var2, evalarg, op == '.') == FAIL)
+       if (eval6(arg, &var2, evalarg, !in_vim9script() && op == '.') == FAIL)
        {
            clear_tv(rettv);
            return FAIL;
@@ -2727,8 +2727,22 @@ eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
            {
                char_u  buf1[NUMBUFLEN], buf2[NUMBUFLEN];
                char_u  *s1 = tv_get_string_buf(rettv, buf1);
-               char_u  *s2 = tv_get_string_buf_chk(&var2, buf2);
+               char_u  *s2 = NULL;
 
+               if (in_vim9script() && (var2.v_type == VAR_VOID
+                       || var2.v_type == VAR_CHANNEL
+                       || var2.v_type == VAR_JOB))
+                   emsg(_(e_inval_string));
+#ifdef FEAT_FLOAT
+               else if (var2.v_type == VAR_FLOAT)
+               {
+                   vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
+                                                           var2.vval.v_float);
+                   s2 = buf2;
+               }
+#endif
+               else
+                   s2 = tv_get_string_buf_chk(&var2, buf2);
                if (s2 == NULL)         // type error ?
                {
                    clear_tv(rettv);
index bbc3404e5bbb2551877dc9899da99dd3d62b921d..d95a1db8201b3c958da6667f0569dd498b3d550c 100644 (file)
@@ -1046,7 +1046,7 @@ static funcentry_T global_functions[] =
     {"test_settime",   1, 1, FEARG_1,    ret_void,     f_test_settime},
     {"test_srand_seed",        0, 1, FEARG_1,    ret_void,     f_test_srand_seed},
     {"test_unknown",   0, 0, 0,          ret_any,      f_test_unknown},
-    {"test_void",      0, 0, 0,          ret_any,      f_test_void},
+    {"test_void",      0, 0, 0,          ret_void,     f_test_void},
     {"timer_info",     0, 1, FEARG_1,    ret_list_dict_any, TIMER_FUNC(f_timer_info)},
     {"timer_pause",    2, 2, FEARG_1,    ret_void,     TIMER_FUNC(f_timer_pause)},
     {"timer_start",    2, 3, FEARG_1,    ret_number,   TIMER_FUNC(f_timer_start)},
index eac8979afc523e063e5fd8c8cb263808340e5282..755b7ddd3df2f439c2f051ada906afa358f0acc9 100644 (file)
@@ -1,4 +1,5 @@
 /* vim9execute.c */
+void to_string_error(vartype_T vartype);
 int call_def_function(ufunc_T *ufunc, int argc_arg, typval_T *argv, partial_T *partial, typval_T *rettv);
 void ex_disassemble(exarg_T *eap);
 int tv2bool(typval_T *tv);
index 3c7724df112b1a8b8fc56130e26acfea8bf60de4..fe8afad9a0d4351f8af5e2d9570e8c3aba3bc720 100644 (file)
@@ -708,7 +708,7 @@ def Test_disassemble_lambda()
         'return "X" .. a .. "X"\_s*' ..
         '\d PUSHS "X"\_s*' ..
         '\d LOAD arg\[-1\]\_s*' ..
-        '\d 2STRING stack\[-1\]\_s*' ..
+        '\d 2STRING_ANY stack\[-1\]\_s*' ..
         '\d CONCAT\_s*' ..
         '\d PUSHS "X"\_s*' ..
         '\d CONCAT\_s*' ..
@@ -964,7 +964,7 @@ def Test_disassemble_concat()
         'let res = g:aa .. "bb".*' ..
         '\d LOADG g:aa.*' ..
         '\d PUSHS "bb".*' ..
-        '\d 2STRING stack\[-2].*' ..
+        '\d 2STRING_ANY stack\[-2].*' ..
         '\d CONCAT.*' ..
         '\d STORE $.*',
         instr)
index 795ea752d309ba24e9d78e2e555729ef3030e585..81d3cb9f9325ac1e6742a5e8168cd302a2bd7aaf 100644 (file)
@@ -921,6 +921,14 @@ def Test_expr5()
   assert_equal('123 hello', 123 .. ' hello')
   assert_equal('123456', 123 .. 456)
 
+  assert_equal('av:true', 'a' .. true)
+  assert_equal('av:false', 'a' .. false)
+  assert_equal('av:null', 'a' .. v:null)
+  assert_equal('av:none', 'a' .. v:none)
+  if has('float')
+    assert_equal('a0.123', 'a' .. 0.123)
+  endif
+
   assert_equal([1, 2, 3, 4], [1, 2] + [3, 4])
   assert_equal(0z11223344, 0z1122 + 0z3344)
   assert_equal(0z112201ab, 0z1122
@@ -1013,6 +1021,56 @@ def Test_expr5_vim9script()
       echo 'a'.. 'b'
   END
   CheckScriptFailure(lines, 'E1004:')
+
+  # check valid string concatenation
+  lines =<< trim END
+      vim9script
+      assert_equal('one123', 'one' .. 123)
+      assert_equal('onev:true', 'one' .. true)
+      assert_equal('onev:null', 'one' .. v:null)
+      assert_equal('onev:none', 'one' .. v:none)
+      if has('float')
+        assert_equal('a0.123', 'a' .. 0.123)
+      endif
+  END
+  CheckScriptSuccess(lines)
+
+  # check invalid string concatenation
+  lines =<< trim END
+      vim9script
+      echo 'a' .. [1]
+  END
+  CheckScriptFailure(lines, 'E730:')
+  lines =<< trim END
+      vim9script
+      echo 'a' .. #{a: 1}
+  END
+  CheckScriptFailure(lines, 'E731:')
+  lines =<< trim END
+      vim9script
+      echo 'a' .. test_void()
+  END
+  CheckScriptFailure(lines, 'E908:')
+  lines =<< trim END
+      vim9script
+      echo 'a' .. 0z33
+  END
+  CheckScriptFailure(lines, 'E976:')
+  lines =<< trim END
+      vim9script
+      echo 'a' .. function('len')
+  END
+  CheckScriptFailure(lines, 'E729:')
+  lines =<< trim END
+      vim9script
+      echo 'a' .. test_null_job()
+  END
+  CheckScriptFailure(lines, 'E908:')
+  lines =<< trim END
+      vim9script
+      echo 'a' .. test_null_channel()
+  END
+  CheckScriptFailure(lines, 'E908:')
 enddef
 
 def Test_expr5_float()
@@ -1063,6 +1121,15 @@ func Test_expr5_fails()
   call CheckDefFailure(["let x = [3] + 0z1122"], 'E1051')
   call CheckDefFailure(["let x = 'asdf' + 0z1122"], 'E1051')
   call CheckDefFailure(["let x = 6 + xxx"], 'E1001')
+
+  call CheckDefFailure(["let x = 'a' .. [1]"], 'E1105')
+  call CheckDefFailure(["let x = 'a' .. #{a: 1}"], 'E1105')
+  call CheckDefFailure(["let x = 'a' .. test_void()"], 'E1105')
+  call CheckDefFailure(["let x = 'a' .. 0z32"], 'E1105')
+  call CheckDefFailure(["let x = 'a' .. function('len')"], 'E1105')
+  call CheckDefFailure(["let x = 'a' .. function('len', ['a'])"], 'E1105')
+  call CheckDefFailure(["let x = 'a' .. test_null_job()"], 'E1105')
+  call CheckDefFailure(["let x = 'a' .. test_null_channel()"], 'E1105')
 endfunc
 
 " test multiply, divide, modulo
index 1100818789ea6f54f997159237fb960ebf8e3a9e..f764e9867d7cd49f605fa7787558387ed0103392 100644 (file)
@@ -754,6 +754,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1435,
 /**/
     1434,
 /**/
index 77f34271284ade5da03414062ee7d33f8ea831de..87894144a922e9f3c3cc2551792979e81e4d141d 100644 (file)
@@ -93,7 +93,7 @@ typedef enum {
     ISN_CATCH,     // drop v:exception
     ISN_ENDTRY,            // take entry off from ec_trystack
 
-    // moreexpression operations
+    // more expression operations
     ISN_ADDLIST,
     ISN_ADDBLOB,
 
@@ -124,6 +124,7 @@ typedef enum {
     ISN_STRINGMEMBER, // dict.member using isn_arg.string
     ISN_2BOOL,     // convert value to bool, invert if isn_arg.number != 0
     ISN_2STRING,    // convert value to string at isn_arg.number on stack
+    ISN_2STRING_ANY, // like ISN_2STRING but check type
     ISN_NEGATENR,   // apply "-" to number
 
     ISN_CHECKNR,    // check value can be used as a number
index 889a5d5c9c7ca9294ac2b716c27198ee6a903a4b..99f8e22cfe9d5555f5c50a107b9d8c6b6884d21f 100644 (file)
@@ -374,19 +374,49 @@ generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
 
 /*
  * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
+ * But only for simple types.
  */
     static int
 may_generate_2STRING(int offset, cctx_T *cctx)
 {
     isn_T      *isn;
+    isntype_T  isntype = ISN_2STRING;
     garray_T   *stack = &cctx->ctx_type_stack;
     type_T     **type = ((type_T **)stack->ga_data) + stack->ga_len + offset;
 
-    if ((*type)->tt_type == VAR_STRING)
-       return OK;
-    *type = &t_string;
+    switch ((*type)->tt_type)
+    {
+       // nothing to be done
+       case VAR_STRING: return OK;
+
+       // conversion possible
+       case VAR_SPECIAL:
+       case VAR_BOOL:
+       case VAR_NUMBER:
+       case VAR_FLOAT:
+                        break;
+
+       // conversion possible (with runtime check)
+       case VAR_ANY:
+       case VAR_UNKNOWN:
+                        isntype = ISN_2STRING_ANY;
+                        break;
+
+       // conversion not possible
+       case VAR_VOID:
+       case VAR_BLOB:
+       case VAR_FUNC:
+       case VAR_PARTIAL:
+       case VAR_LIST:
+       case VAR_DICT:
+       case VAR_JOB:
+       case VAR_CHANNEL:
+                        to_string_error((*type)->tt_type);
+                        return FAIL;
+    }
 
-    if ((isn = generate_instr(cctx, ISN_2STRING)) == NULL)
+    *type = &t_string;
+    if ((isn = generate_instr(cctx, isntype)) == NULL)
        return FAIL;
     isn->isn_arg.number = offset;
 
@@ -7005,6 +7035,7 @@ delete_instr(isn_T *isn)
 
        case ISN_2BOOL:
        case ISN_2STRING:
+       case ISN_2STRING_ANY:
        case ISN_ADDBLOB:
        case ISN_ADDLIST:
        case ISN_BCALL:
index 00adff3850d816d60df19c1fdd2e58f98fecc4ca..8575e584a413e5a79bb7b1c01c552a4532591e75 100644 (file)
@@ -72,6 +72,12 @@ typedef struct {
 // Get pointer to item relative to the bottom of the stack, -1 is the last one.
 #define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + idx)
 
+    void
+to_string_error(vartype_T vartype)
+{
+    semsg(_("E1105: Cannot convert %s to string"), vartype_name(vartype));
+}
+
 /*
  * Return the number of arguments, including optional arguments and any vararg.
  */
@@ -2476,12 +2482,26 @@ call_def_function(
                break;
 
            case ISN_2STRING:
+           case ISN_2STRING_ANY:
                {
                    char_u *str;
 
                    tv = STACK_TV_BOT(iptr->isn_arg.number);
                    if (tv->v_type != VAR_STRING)
                    {
+                       if (iptr->isn_type == ISN_2STRING_ANY)
+                       {
+                           switch (tv->v_type)
+                           {
+                               case VAR_SPECIAL:
+                               case VAR_BOOL:
+                               case VAR_NUMBER:
+                               case VAR_FLOAT:
+                               case VAR_BLOB:  break;
+                               default:        to_string_error(tv->v_type);
+                                               goto on_error;
+                           }
+                       }
                        str = typval_tostring(tv);
                        clear_tv(tv);
                        tv->v_type = VAR_STRING;
@@ -3127,6 +3147,9 @@ ex_disassemble(exarg_T *eap)
            case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
                                         (long long)(iptr->isn_arg.number));
                              break;
+           case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current,
+                                        (long long)(iptr->isn_arg.number));
+                             break;
 
            case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
                                         iptr->isn_arg.shuffle.shfl_item,