summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBram Moolenaar <Bram@vim.org>2020-06-13 19:00:10 +0200
committerBram Moolenaar <Bram@vim.org>2020-06-13 19:00:10 +0200
commit34db91f7a47b7bd4aabf1e1dfbaa8a08278bf78d (patch)
tree116cb78d7fe2603f6213f355ad35c7ccbd3ebcd6
parentc82a5b5da5eab15bc35115545b639fb590272ad7 (diff)
downloadvim-git-8.2.0973.tar.gz
patch 8.2.0973: Vim9: type is not checked when assigning to a script variablev8.2.0973
Problem: Vim9: type is not checked when assigning to a script variable. Solution: Check the type.
-rw-r--r--src/evalvars.c15
-rw-r--r--src/proto/vim9compile.pro2
-rw-r--r--src/proto/vim9script.pro1
-rw-r--r--src/testdir/test_vim9_script.vim9
-rw-r--r--src/version.c2
-rw-r--r--src/vim9compile.c5
-rw-r--r--src/vim9script.c25
7 files changed, 51 insertions, 8 deletions
diff --git a/src/evalvars.c b/src/evalvars.c
index 8a7162c99..31cd01d25 100644
--- a/src/evalvars.c
+++ b/src/evalvars.c
@@ -2883,12 +2883,17 @@ set_var_const(
|| var_check_lock(di->di_tv.v_lock, name, FALSE))
return;
- if ((flags & LET_NO_COMMAND) == 0
- && is_script_local
- && current_sctx.sc_version == SCRIPT_VERSION_VIM9)
+ if (is_script_local
+ && current_sctx.sc_version == SCRIPT_VERSION_VIM9)
{
- semsg(_("E1041: Redefining script item %s"), name);
- return;
+ if ((flags & LET_NO_COMMAND) == 0)
+ {
+ semsg(_("E1041: Redefining script item %s"), name);
+ return;
+ }
+
+ // check the type
+ check_script_var_type(&di->di_tv, tv, name);
}
}
else
diff --git a/src/proto/vim9compile.pro b/src/proto/vim9compile.pro
index 0f3ebc394..41c688721 100644
--- a/src/proto/vim9compile.pro
+++ b/src/proto/vim9compile.pro
@@ -1,5 +1,7 @@
/* vim9compile.c */
int check_defined(char_u *p, int len, cctx_T *cctx);
+type_T *typval2type(typval_T *tv);
+int check_type(type_T *expected, type_T *actual, int give_msg);
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/proto/vim9script.pro b/src/proto/vim9script.pro
index 45deed69a..3a7b6e171 100644
--- a/src/proto/vim9script.pro
+++ b/src/proto/vim9script.pro
@@ -7,4 +7,5 @@ void ex_import(exarg_T *eap);
int find_exported(int sid, char_u **argp, int *name_len, ufunc_T **ufunc, type_T **type);
char_u *handle_import(char_u *arg_start, garray_T *gap, int import_sid, void *cctx);
char_u *vim9_declare_scriptvar(exarg_T *eap, char_u *arg);
+void check_script_var_type(typval_T *dest, typval_T *value, char_u *name);
/* vim: set ft=c : */
diff --git a/src/testdir/test_vim9_script.vim b/src/testdir/test_vim9_script.vim
index 7674cc017..2fea0de92 100644
--- a/src/testdir/test_vim9_script.vim
+++ b/src/testdir/test_vim9_script.vim
@@ -1831,6 +1831,15 @@ def Test_let_declaration()
unlet g:var_test
enddef
+def Test_let_type_check()
+ let lines =<< trim END
+ vim9script
+ let var: string
+ var = 1234
+ END
+ CheckScriptFailure(lines, 'E1013:')
+enddef
+
def Test_forward_declaration()
let lines =<< trim END
vim9script
diff --git a/src/version.c b/src/version.c
index 2996d3b89..d2f442c89 100644
--- a/src/version.c
+++ b/src/version.c
@@ -755,6 +755,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
+ 973,
+/**/
972,
/**/
971,
diff --git a/src/vim9compile.c b/src/vim9compile.c
index 9029a48a4..01af95e74 100644
--- a/src/vim9compile.c
+++ b/src/vim9compile.c
@@ -139,7 +139,6 @@ static char e_used_as_arg[] = N_("E1006: %s is used as an argument");
static void delete_def_function_contents(dfunc_T *dfunc);
static void arg_type_mismatch(type_T *expected, type_T *actual, int argidx);
-static int check_type(type_T *expected, type_T *actual, int give_msg);
/*
* Lookup variable "name" in the local scope and return it.
@@ -461,7 +460,7 @@ func_type_add_arg_types(
/*
* Return the type_T for a typval. Only for primitive types.
*/
- static type_T *
+ type_T *
typval2type(typval_T *tv)
{
if (tv->v_type == VAR_NUMBER)
@@ -504,7 +503,7 @@ arg_type_mismatch(type_T *expected, type_T *actual, int argidx)
* Check if the expected and actual types match.
* Does not allow for assigning "any" to a specific type.
*/
- static int
+ int
check_type(type_T *expected, type_T *actual, int give_msg)
{
int ret = OK;
diff --git a/src/vim9script.c b/src/vim9script.c
index 51afa19a3..13a29ec3a 100644
--- a/src/vim9script.c
+++ b/src/vim9script.c
@@ -488,5 +488,30 @@ vim9_declare_scriptvar(exarg_T *eap, char_u *arg)
return p;
}
+/*
+ * Check if the type of script variable "dest" allows assigning "value".
+ */
+ void
+check_script_var_type(typval_T *dest, typval_T *value, char_u *name)
+{
+ scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
+ int idx;
+
+ // Find the svar_T in sn_var_vals.
+ for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
+ {
+ svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
+
+ if (sv->sv_tv == dest)
+ {
+ if (sv->sv_const)
+ semsg(_(e_readonlyvar), name);
+ else
+ check_type(sv->sv_type, typval2type(value), TRUE);
+ return;
+ }
+ }
+ iemsg("check_script_var_type(): not found");
+}
#endif // FEAT_EVAL