patch 8.2.1176: Vim9: not enough type checking in Vim9 script
Problem: Vim9: not enough type checking in Vim9 script.
Solution: Use same type checking as in a :def function.
diff --git a/src/eval.c b/src/eval.c
index 6a1bc4c..595cd06 100644
--- a/src/eval.c
+++ b/src/eval.c
@@ -2460,8 +2460,16 @@
}
if (evalarg != NULL && (evalarg->eval_flags & EVAL_EVALUATE))
{
- int ret = typval_compare(rettv, &var2, type, ic);
+ int ret;
+ if (in_vim9script() && check_compare_types(
+ type, rettv, &var2) == FAIL)
+ {
+ ret = FAIL;
+ clear_tv(rettv);
+ }
+ else
+ ret = typval_compare(rettv, &var2, type, ic);
clear_tv(&var2);
return ret;
}
diff --git a/src/proto/vim9compile.pro b/src/proto/vim9compile.pro
index 5486bd9..03862a2 100644
--- a/src/proto/vim9compile.pro
+++ b/src/proto/vim9compile.pro
@@ -3,6 +3,7 @@
void clear_type_list(garray_T *gap);
type_T *typval2type(typval_T *tv);
int check_type(type_T *expected, type_T *actual, int give_msg);
+int check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2);
char_u *skip_type(char_u *start);
type_T *parse_type(char_u **arg, garray_T *type_gap);
char *vartype_name(vartype_T type);
diff --git a/src/testdir/test_vim9_expr.vim b/src/testdir/test_vim9_expr.vim
index 1f4bde9..ef56a67 100644
--- a/src/testdir/test_vim9_expr.vim
+++ b/src/testdir/test_vim9_expr.vim
@@ -557,7 +557,7 @@
enddef
def Test_expr4_vimscript()
- " only checks line continuation
+ " check line continuation
let lines =<< trim END
vim9script
let var = 0
@@ -599,6 +599,25 @@
assert_equal(1, var)
END
CheckScriptSuccess(lines)
+
+ " spot check mismatching types
+ lines =<< trim END
+ vim9script
+ echo '' == 0
+ END
+ CheckScriptFailure(lines, 'E1072:')
+
+ lines =<< trim END
+ vim9script
+ echo v:true > v:false
+ END
+ CheckScriptFailure(lines, 'Cannot compare bool with bool')
+
+ lines =<< trim END
+ vim9script
+ echo 123 is 123
+ END
+ CheckScriptFailure(lines, 'Cannot use "is" with number')
enddef
func Test_expr4_fails()
diff --git a/src/version.c b/src/version.c
index ac5425d..6c6211d 100644
--- a/src/version.c
+++ b/src/version.c
@@ -755,6 +755,8 @@
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
+ 1176,
+/**/
1175,
/**/
1174,
diff --git a/src/vim9compile.c b/src/vim9compile.c
index 3c0336a..e7f90e1 100644
--- a/src/vim9compile.c
+++ b/src/vim9compile.c
@@ -820,6 +820,14 @@
return isntype;
}
+ int
+check_compare_types(exptype_T type, typval_T *tv1, typval_T *tv2)
+{
+ if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
+ return FAIL;
+ return OK;
+}
+
/*
* Generate an ISN_COMPARE* instruction with a boolean result.
*/
@@ -4296,7 +4304,7 @@
// Both sides are a constant, compute the result now.
// First check for a valid combination of types, this is more
// strict than typval_compare().
- if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
+ if (check_compare_types(type, tv1, tv2) == FAIL)
ret = FAIL;
else
{