diff src/vim9compile.c @ 33668:fcc8296f36eb v9.0.2072

patch 9.0.2072: Vim9: no nr2str conversion in list-unpack Commit: https://github.com/vim/vim/commit/c229a6ac0775e07dff456ca8832c516e57a74e74 Author: Yegappan Lakshmanan <yegappan@yahoo.com> Date: Thu Oct 26 23:05:07 2023 +0200 patch 9.0.2072: Vim9: no nr2str conversion in list-unpack Problem: Vim9: no nr2str conversion in list-unpack Solution: Generate 2STRING instruction to convert dict index to string Generate instruction to convert dict index to a string fixes: #13417 closes: #13424 Signed-off-by: Christian Brabandt <cb@256bit.org> Co-authored-by: Yegappan Lakshmanan <yegappan@yahoo.com>
author Christian Brabandt <cb@256bit.org>
date Thu, 26 Oct 2023 23:15:06 +0200
parents 86dbcbb94fdb
children 7d9d2404a3d4
line wrap: on
line diff
--- a/src/vim9compile.c
+++ b/src/vim9compile.c
@@ -2040,9 +2040,7 @@ compile_lhs(
 	    lhs->lhs_member_type = m->ocm_type;
 	}
 	else
-	{
 	    lhs->lhs_member_type = lhs->lhs_type->tt_member;
-	}
     }
     return OK;
 }
@@ -2220,16 +2218,27 @@ compile_load_lhs(
 		return FAIL;
 	}
 
+	if (lhs->lhs_type->tt_type == VAR_DICT && var_start[varlen] == '[')
+	{
+	    // If the lhs is a Dict variable and an item is accessed by "[",
+	    // then need to convert the key into a string.  The top item in the
+	    // type stack is the Dict and the second last item is the key.
+	    if (may_generate_2STRING(-2, FALSE, cctx) == FAIL)
+		return FAIL;
+	}
+
 	// Now we can properly check the type.  The variable is indexed, thus
 	// we need the member type.  For a class or object we don't know the
 	// type yet, it depends on what member is used.
+	// The top item in the stack is the Dict, followed by the key and then
+	// the type of the value.
 	vartype_T vartype = lhs->lhs_type->tt_type;
 	type_T *member_type = lhs->lhs_type->tt_member;
 	if (rhs_type != NULL && member_type != NULL
 		&& vartype != VAR_OBJECT && vartype != VAR_CLASS
 		&& rhs_type != &t_void
 		&& need_type(rhs_type, member_type, FALSE,
-					    -2, 0, cctx, FALSE, FALSE) == FAIL)
+					    -3, 0, cctx, FALSE, FALSE) == FAIL)
 	    return FAIL;
     }
     else