diff src/evalvars.c @ 25184:e495f40e4b07 v8.2.3128

patch 8.2.3128: Vim9: uninitialzed list does not get type checked Commit: https://github.com/vim/vim/commit/f055d4502365f24de6c0f210a06e2c146a75dc4d Author: Bram Moolenaar <Bram@vim.org> Date: Thu Jul 8 20:57:24 2021 +0200 patch 8.2.3128: Vim9: uninitialzed list does not get type checked Problem: Vim9: uninitialzed list does not get type checked. Solution: Set the type when initializing the variable. (closes https://github.com/vim/vim/issues/8529)
author Bram Moolenaar <Bram@vim.org>
date Thu, 08 Jul 2021 21:00:06 +0200
parents 8f2262c72178
children 0a3b1c66d3f2
line wrap: on
line diff
--- a/src/evalvars.c
+++ b/src/evalvars.c
@@ -2565,6 +2565,7 @@ eval_variable(
     typval_T	*tv = NULL;
     int		found = FALSE;
     dictitem_T	*v;
+    hashtab_T	*ht = NULL;
     int		cc;
 
     // truncate the name, so that we can use strcmp()
@@ -2575,7 +2576,7 @@ eval_variable(
     if ((tv = lookup_debug_var(name)) == NULL)
     {
 	// Check for user-defined variables.
-	v = find_var(name, NULL, flags & EVAL_VAR_NOAUTOLOAD);
+	v = find_var(name, &ht, flags & EVAL_VAR_NOAUTOLOAD);
 	if (v != NULL)
 	{
 	    tv = &v->di_tv;
@@ -2655,18 +2656,35 @@ eval_variable(
 	}
 	else if (rettv != NULL)
 	{
+	    type_T	*type = NULL;
+
+	    if (ht != NULL && ht == get_script_local_ht())
+	    {
+		svar_T *sv = find_typval_in_script(tv, FALSE);
+
+		// TODO: check imported variable
+		if (sv != NULL)
+		    type = sv->sv_type;
+	    }
+
 	    // If a list or dict variable wasn't initialized, do it now.
 	    if (tv->v_type == VAR_DICT && tv->vval.v_dict == NULL)
 	    {
 		tv->vval.v_dict = dict_alloc();
 		if (tv->vval.v_dict != NULL)
+		{
 		    ++tv->vval.v_dict->dv_refcount;
+		    tv->vval.v_dict->dv_type = alloc_type(type);
+		}
 	    }
 	    else if (tv->v_type == VAR_LIST && tv->vval.v_list == NULL)
 	    {
 		tv->vval.v_list = list_alloc();
 		if (tv->vval.v_list != NULL)
+		{
 		    ++tv->vval.v_list->lv_refcount;
+		    tv->vval.v_list->lv_type = alloc_type(type);
+		}
 	    }
 	    else if (tv->v_type == VAR_BLOB && tv->vval.v_blob == NULL)
 	    {