patch 8.2.1435: Vim9: always converting to string for ".." leads to mistakes
Problem: Vim9: always converting to string for ".." leads to mistakes.
Solution: Only automatically convert simple types.
diff --git a/src/eval.c b/src/eval.c
index 99554e7..07b30c7 100644
--- a/src/eval.c
+++ b/src/eval.c
@@ -2712,7 +2712,7 @@
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 @@
{
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);
diff --git a/src/evalfunc.c b/src/evalfunc.c
index bbc3404..d95a1db 100644
--- a/src/evalfunc.c
+++ b/src/evalfunc.c
@@ -1046,7 +1046,7 @@
{"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)},
diff --git a/src/proto/vim9execute.pro b/src/proto/vim9execute.pro
index eac8979..755b7dd 100644
--- a/src/proto/vim9execute.pro
+++ b/src/proto/vim9execute.pro
@@ -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);
diff --git a/src/testdir/test_vim9_disassemble.vim b/src/testdir/test_vim9_disassemble.vim
index 3c7724d..fe8afad 100644
--- a/src/testdir/test_vim9_disassemble.vim
+++ b/src/testdir/test_vim9_disassemble.vim
@@ -708,7 +708,7 @@
'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 @@
'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)
diff --git a/src/testdir/test_vim9_expr.vim b/src/testdir/test_vim9_expr.vim
index 795ea75..81d3cb9 100644
--- a/src/testdir/test_vim9_expr.vim
+++ b/src/testdir/test_vim9_expr.vim
@@ -921,6 +921,14 @@
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 @@
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 @@
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
diff --git a/src/version.c b/src/version.c
index 1100818..f764e98 100644
--- a/src/version.c
+++ b/src/version.c
@@ -755,6 +755,8 @@
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
+ 1435,
+/**/
1434,
/**/
1433,
diff --git a/src/vim9.h b/src/vim9.h
index 77f3427..8789414 100644
--- a/src/vim9.h
+++ b/src/vim9.h
@@ -93,7 +93,7 @@
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 @@
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
diff --git a/src/vim9compile.c b/src/vim9compile.c
index 889a5d5..99f8e22 100644
--- a/src/vim9compile.c
+++ b/src/vim9compile.c
@@ -374,19 +374,49 @@
/*
* 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;
- if ((isn = generate_instr(cctx, ISN_2STRING)) == NULL)
+ // 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;
+ }
+
+ *type = &t_string;
+ if ((isn = generate_instr(cctx, isntype)) == NULL)
return FAIL;
isn->isn_arg.number = offset;
@@ -7005,6 +7035,7 @@
case ISN_2BOOL:
case ISN_2STRING:
+ case ISN_2STRING_ANY:
case ISN_ADDBLOB:
case ISN_ADDLIST:
case ISN_BCALL:
diff --git a/src/vim9execute.c b/src/vim9execute.c
index 00adff3..8575e58 100644
--- a/src/vim9execute.c
+++ b/src/vim9execute.c
@@ -72,6 +72,12 @@
// 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 @@
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 @@
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,