diff src/vim9compile.c @ 27728:fdc841530372 v8.2.4390

patch 8.2.4390: Vim9: list from declaration with inferred type not set Commit: https://github.com/vim/vim/commit/e88c6b7a5d8b24f8aa9a3e976b78654bae293ae4 Author: Bram Moolenaar <Bram@vim.org> Date: Tue Feb 15 15:37:11 2022 +0000 patch 8.2.4390: Vim9: list from declaration with inferred type not set Problem: Vim9: list from declaration with inferred type does not set the type on the value. Solution: When inferring the type in a variable declaration also set the type of the list or dictionary. (closes #9705) Do not set the type when the member is "any".
author Bram Moolenaar <Bram@vim.org>
date Tue, 15 Feb 2022 16:45:03 +0100
parents 4097434c7c67
children d754ac2f5ac5
line wrap: on
line diff
--- a/src/vim9compile.c
+++ b/src/vim9compile.c
@@ -2002,6 +2002,7 @@ compile_assignment(char_u *arg, exarg_T 
 	int	instr_count = -1;
 	int	save_lnum;
 	int	skip_store = FALSE;
+	type_T	*inferred_type = NULL;
 
 	if (var_start[0] == '_' && !eval_isnamec(var_start[1]))
 	{
@@ -2126,7 +2127,10 @@ compile_assignment(char_u *arg, exarg_T 
 			    else if (rhs_type == &t_unknown)
 				lhs.lhs_lvar->lv_type = &t_any;
 			    else
+			    {
 				lhs.lhs_lvar->lv_type = rhs_type;
+				inferred_type = rhs_type;
+			    }
 			}
 		    }
 		    else if (*op == '=')
@@ -2146,7 +2150,7 @@ compile_assignment(char_u *arg, exarg_T 
 									 cctx))
 			    use_type = lhs.lhs_member_type;
 			if (need_type_where(rhs_type, use_type, -1, where,
-				    cctx, FALSE, is_const) == FAIL)
+						cctx, FALSE, is_const) == FAIL)
 			    goto theend;
 		    }
 		}
@@ -2315,10 +2319,20 @@ compile_assignment(char_u *arg, exarg_T 
 	    if ((lhs.lhs_type->tt_type == VAR_DICT
 					  || lhs.lhs_type->tt_type == VAR_LIST)
 		    && lhs.lhs_type->tt_member != NULL
+		    && lhs.lhs_type->tt_member != &t_any
 		    && lhs.lhs_type->tt_member != &t_unknown)
 		// Set the type in the list or dict, so that it can be checked,
 		// also in legacy script.
 		generate_SETTYPE(cctx, lhs.lhs_type);
+	    else if (inferred_type != NULL
+		    && (inferred_type->tt_type == VAR_DICT
+					|| inferred_type->tt_type == VAR_LIST)
+		    && inferred_type->tt_member != NULL
+		    && inferred_type->tt_member != &t_unknown
+		    && inferred_type->tt_member != &t_any)
+		// Set the type in the list or dict, so that it can be checked,
+		// also in legacy script.
+		generate_SETTYPE(cctx, inferred_type);
 
 	    if (!skip_store && generate_store_lhs(cctx, &lhs,
 						 instr_count, is_decl) == FAIL)