diff src/vim9type.c @ 24438:5c6ccab68d1e v8.2.2759

patch 8.2.2759: Vim9: for loop infers type of loop variable Commit: https://github.com/vim/vim/commit/f2253963c28e4791092620df6a6bb238c33168df Author: Bram Moolenaar <Bram@vim.org> Date: Tue Apr 13 20:53:13 2021 +0200 patch 8.2.2759: Vim9: for loop infers type of loop variable Problem: Vim9: for loop infers type of loop variable. Solution: Do not get the member type. (closes https://github.com/vim/vim/issues/8102)
author Bram Moolenaar <Bram@vim.org>
date Tue, 13 Apr 2021 21:00:05 +0200
parents e695db9a5465
children a4fda40e0bb9
line wrap: on
line diff
--- a/src/vim9type.c
+++ b/src/vim9type.c
@@ -252,9 +252,10 @@ func_type_add_arg_types(
 /*
  * Get a type_T for a typval_T.
  * "type_gap" is used to temporarily create types in.
+ * When "do_member" is TRUE also get the member type, otherwise use "any".
  */
     static type_T *
-typval2type_int(typval_T *tv, int copyID, garray_T *type_gap)
+typval2type_int(typval_T *tv, int copyID, garray_T *type_gap, int do_member)
 {
     type_T  *type;
     type_T  *member_type = &t_any;
@@ -274,6 +275,8 @@ typval2type_int(typval_T *tv, int copyID
 
 	if (l == NULL || l->lv_first == NULL)
 	    return &t_list_empty;
+	if (!do_member)
+	    return &t_list_any;
 	if (l->lv_first == &range_list_item)
 	    return &t_list_number;
 	if (l->lv_copyID == copyID)
@@ -282,9 +285,9 @@ typval2type_int(typval_T *tv, int copyID
 	l->lv_copyID = copyID;
 
 	// Use the common type of all members.
-	member_type = typval2type(&l->lv_first->li_tv, copyID, type_gap);
+	member_type = typval2type(&l->lv_first->li_tv, copyID, type_gap, TRUE);
 	for (li = l->lv_first->li_next; li != NULL; li = li->li_next)
-	    common_type(typval2type(&li->li_tv, copyID, type_gap),
+	    common_type(typval2type(&li->li_tv, copyID, type_gap, TRUE),
 					  member_type, &member_type, type_gap);
 	return get_list_type(member_type, type_gap);
     }
@@ -297,6 +300,8 @@ typval2type_int(typval_T *tv, int copyID
 
 	if (d == NULL || d->dv_hashtab.ht_used == 0)
 	    return &t_dict_empty;
+	if (!do_member)
+	    return &t_dict_any;
 	if (d->dv_copyID == copyID)
 	    // avoid recursion
 	    return &t_dict_any;
@@ -305,9 +310,9 @@ typval2type_int(typval_T *tv, int copyID
 	// Use the common type of all values.
 	dict_iterate_start(tv, &iter);
 	dict_iterate_next(&iter, &value);
-	member_type = typval2type(value, copyID, type_gap);
+	member_type = typval2type(value, copyID, type_gap, TRUE);
 	while (dict_iterate_next(&iter, &value) != NULL)
-	    common_type(typval2type(value, copyID, type_gap),
+	    common_type(typval2type(value, copyID, type_gap, TRUE),
 					  member_type, &member_type, type_gap);
 	return get_dict_type(member_type, type_gap);
     }
@@ -378,11 +383,12 @@ need_convert_to_bool(type_T *type, typva
 /*
  * Get a type_T for a typval_T.
  * "type_list" is used to temporarily create types in.
+ * When "do_member" is TRUE also get the member type, otherwise use "any".
  */
     type_T *
-typval2type(typval_T *tv, int copyID, garray_T *type_gap)
+typval2type(typval_T *tv, int copyID, garray_T *type_gap, int do_member)
 {
-    type_T *type = typval2type_int(tv, copyID, type_gap);
+    type_T *type = typval2type_int(tv, copyID, type_gap, do_member);
 
     if (type != NULL && type != &t_bool
 	    && (tv->v_type == VAR_NUMBER
@@ -404,7 +410,7 @@ typval2type_vimvar(typval_T *tv, garray_
 	return &t_list_string;
     if (tv->v_type == VAR_DICT)  // e.g. for v:completed_item
 	return &t_dict_any;
-    return typval2type(tv, get_copyID(), type_gap);
+    return typval2type(tv, get_copyID(), type_gap, TRUE);
 }
 
     int
@@ -429,7 +435,7 @@ check_typval_type(type_T *expected, typv
     int		res = FAIL;
 
     ga_init2(&type_list, sizeof(type_T *), 10);
-    actual_type = typval2type(actual_tv, get_copyID(), &type_list);
+    actual_type = typval2type(actual_tv, get_copyID(), &type_list, TRUE);
     if (actual_type != NULL)
 	res = check_type(expected, actual_type, TRUE, where);
     clear_type_list(&type_list);
@@ -1210,7 +1216,7 @@ f_typename(typval_T *argvars, typval_T *
 
     rettv->v_type = VAR_STRING;
     ga_init2(&type_list, sizeof(type_T *), 10);
-    type = typval2type(argvars, get_copyID(), &type_list);
+    type = typval2type(argvars, get_copyID(), &type_list, TRUE);
     name = type_name(type, &tofree);
     if (tofree != NULL)
 	rettv->vval.v_string = (char_u *)tofree;