]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 8.2.0973: Vim9: type is not checked when assigning to a script variable v8.2.0973
authorBram Moolenaar <Bram@vim.org>
Sat, 13 Jun 2020 17:00:10 +0000 (19:00 +0200)
committerBram Moolenaar <Bram@vim.org>
Sat, 13 Jun 2020 17:00:10 +0000 (19:00 +0200)
Problem:    Vim9: type is not checked when assigning to a script variable.
Solution:   Check the type.

src/evalvars.c
src/proto/vim9compile.pro
src/proto/vim9script.pro
src/testdir/test_vim9_script.vim
src/version.c
src/vim9compile.c
src/vim9script.c

index 8a7162c99cd580ac74b8e005ca9e2d3f8bf88c0b..31cd01d25bab3d1d8d9fa5a844a649182618d272 100644 (file)
@@ -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
index 0f3ebc394a60fcb367f9d02ed31ffba7ed760b26..41c688721189b6293b1ad8c8183ed6770458aa34 100644 (file)
@@ -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);
index 45deed69a813dd6735bcc049b5fc17cfaa6c2307..3a7b6e1719ac04fb3eae554d7f904982d111611c 100644 (file)
@@ -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 : */
index 7674cc01789de8b66f718b53991813e4bad9ad49..2fea0de92015d8b0bbfc83473b0e540f54e4e8b9 100644 (file)
@@ -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
index 2996d3b8995f0849b9ccebfe8b8126bccc57d13b..d2f442c89a279a86ad1c9c74c5aca445cd9be5ce 100644 (file)
@@ -754,6 +754,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    973,
 /**/
     972,
 /**/
index 9029a48a4bb7e264eb624790500a276924d3dbb8..01af95e74426ee4d4df407ff34b77d555c0b1c50 100644 (file)
@@ -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;
index 51afa19a3930916aaf70bb618157b777f582fc45..13a29ec3a92a2312b8cf302be88ecc41320e33e9 100644 (file)
@@ -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