diff src/evalvars.c @ 28345:fabe722b24e9 v8.2.4698

patch 8.2.4698: Vim9: script variable has no flag that it was set Commit: https://github.com/vim/vim/commit/aa7d0c233532fb9d8c2876ea8e978a82b12c377f Author: Bram Moolenaar <Bram@vim.org> Date: Tue Apr 5 21:40:38 2022 +0100 patch 8.2.4698: Vim9: script variable has no flag that it was set Problem: Vim9: script variable has no flag that it was set. Solution: Add a flag that it was set, to avoid giving it a value when used. (closes #10088)
author Bram Moolenaar <Bram@vim.org>
date Tue, 05 Apr 2022 22:45:03 +0200
parents b418e073b42f
children 4dcccb2673fe
line wrap: on
line diff
--- a/src/evalvars.c
+++ b/src/evalvars.c
@@ -2828,13 +2828,18 @@ eval_variable(
 	}
 	else if (rettv != NULL)
 	{
+	    svar_T  *sv = NULL;
+	    int	    was_assigned = FALSE;
+
 	    if (ht != NULL && ht == get_script_local_ht()
 		    && tv != &SCRIPT_SV(current_sctx.sc_sid)->sv_var.di_tv)
 	    {
-		svar_T *sv = find_typval_in_script(tv, 0, TRUE);
-
+		sv = find_typval_in_script(tv, 0, TRUE);
 		if (sv != NULL)
+		{
 		    type = sv->sv_type;
+		    was_assigned = sv->sv_flags & SVFLAG_ASSIGNED;
+		}
 	    }
 
 	    // If a list or dict variable wasn't initialized and has meaningful
@@ -2843,7 +2848,7 @@ eval_variable(
 	    if (ht != &globvarht)
 	    {
 		if (tv->v_type == VAR_DICT && tv->vval.v_dict == NULL
-			  && ((type != NULL && type != &t_dict_empty)
+					    && ((type != NULL && !was_assigned)
 							  || !in_vim9script()))
 		{
 		    tv->vval.v_dict = dict_alloc();
@@ -2851,10 +2856,12 @@ eval_variable(
 		    {
 			++tv->vval.v_dict->dv_refcount;
 			tv->vval.v_dict->dv_type = alloc_type(type);
+			if (sv != NULL)
+			    sv->sv_flags |= SVFLAG_ASSIGNED;
 		    }
 		}
 		else if (tv->v_type == VAR_LIST && tv->vval.v_list == NULL
-				    && ((type != NULL && type != &t_list_empty)
+					    && ((type != NULL && !was_assigned)
 							  || !in_vim9script()))
 		{
 		    tv->vval.v_list = list_alloc();
@@ -2862,15 +2869,21 @@ eval_variable(
 		    {
 			++tv->vval.v_list->lv_refcount;
 			tv->vval.v_list->lv_type = alloc_type(type);
+			if (sv != NULL)
+			    sv->sv_flags |= SVFLAG_ASSIGNED;
 		    }
 		}
 		else if (tv->v_type == VAR_BLOB && tv->vval.v_blob == NULL
-				    && ((type != NULL && type != &t_blob_null)
+					    && ((type != NULL && !was_assigned)
 							  || !in_vim9script()))
 		{
 		    tv->vval.v_blob = blob_alloc();
 		    if (tv->vval.v_blob != NULL)
+		    {
 			++tv->vval.v_blob->bv_refcount;
+			if (sv != NULL)
+			    sv->sv_flags |= SVFLAG_ASSIGNED;
+		    }
 		}
 	    }
 	    copy_tv(tv, rettv);
@@ -3587,6 +3600,7 @@ set_var_const(
 			goto failed;
 		    if (type == NULL)
 			type = sv->sv_type;
+		    sv->sv_flags |= SVFLAG_ASSIGNED;
 		}
 	    }