comparison src/evalfunc.c @ 27503:4cea92e99a5a v8.2.4279

patch 8.2.4279: Vim9: cannot change item type with map() after range() Commit: https://github.com/vim/vim/commit/8133018f50bc447570825801e93d5ed67e8dac90 Author: Bram Moolenaar <Bram@vim.org> Date: Tue Feb 1 12:11:58 2022 +0000 patch 8.2.4279: Vim9: cannot change item type with map() after range() Problem: Vim9: cannot change item type with map() after range(). Solution: Split the return type in current type and declared type. (closes #9665)
author Bram Moolenaar <Bram@vim.org>
date Tue, 01 Feb 2022 13:15:03 +0100
parents 7480a2e2ca0f
children f00a7a2bee21
comparison
equal deleted inserted replaced
27502:aad2a09123be 27503:4cea92e99a5a
990 /* 990 /*
991 * Functions that return the return type of a builtin function. 991 * Functions that return the return type of a builtin function.
992 * Note that "argtypes" is NULL if "argcount" is zero. 992 * Note that "argtypes" is NULL if "argcount" is zero.
993 */ 993 */
994 static type_T * 994 static type_T *
995 ret_void(int argcount UNUSED, type2_T *argtypes UNUSED) 995 ret_void(int argcount UNUSED,
996 type2_T *argtypes UNUSED,
997 type_T **decl_type UNUSED)
996 { 998 {
997 return &t_void; 999 return &t_void;
998 } 1000 }
999 static type_T * 1001 static type_T *
1000 ret_any(int argcount UNUSED, type2_T *argtypes UNUSED) 1002 ret_any(int argcount UNUSED,
1003 type2_T *argtypes UNUSED,
1004 type_T **decl_type UNUSED)
1001 { 1005 {
1002 return &t_any; 1006 return &t_any;
1003 } 1007 }
1004 static type_T * 1008 static type_T *
1005 ret_bool(int argcount UNUSED, type2_T *argtypes UNUSED) 1009 ret_bool(int argcount UNUSED,
1010 type2_T *argtypes UNUSED,
1011 type_T **decl_type UNUSED)
1006 { 1012 {
1007 return &t_bool; 1013 return &t_bool;
1008 } 1014 }
1009 static type_T * 1015 static type_T *
1010 ret_number_bool(int argcount UNUSED, type2_T *argtypes UNUSED) 1016 ret_number_bool(int argcount UNUSED,
1017 type2_T *argtypes UNUSED,
1018 type_T **decl_type UNUSED)
1011 { 1019 {
1012 return &t_number_bool; 1020 return &t_number_bool;
1013 } 1021 }
1014 static type_T * 1022 static type_T *
1015 ret_number(int argcount UNUSED, type2_T *argtypes UNUSED) 1023 ret_number(int argcount UNUSED,
1024 type2_T *argtypes UNUSED,
1025 type_T **decl_type UNUSED)
1016 { 1026 {
1017 return &t_number; 1027 return &t_number;
1018 } 1028 }
1019 static type_T * 1029 static type_T *
1020 ret_float(int argcount UNUSED, type2_T *argtypes UNUSED) 1030 ret_float(int argcount UNUSED,
1031 type2_T *argtypes UNUSED,
1032 type_T **decl_type UNUSED)
1021 { 1033 {
1022 return &t_float; 1034 return &t_float;
1023 } 1035 }
1024 static type_T * 1036 static type_T *
1025 ret_string(int argcount UNUSED, type2_T *argtypes UNUSED) 1037 ret_string(int argcount UNUSED,
1038 type2_T *argtypes UNUSED,
1039 type_T **decl_type UNUSED)
1026 { 1040 {
1027 return &t_string; 1041 return &t_string;
1028 } 1042 }
1029 static type_T * 1043 static type_T *
1030 ret_list_any(int argcount UNUSED, type2_T *argtypes UNUSED) 1044 ret_list_any(int argcount UNUSED,
1045 type2_T *argtypes UNUSED,
1046 type_T **decl_type UNUSED)
1031 { 1047 {
1032 return &t_list_any; 1048 return &t_list_any;
1033 } 1049 }
1034 static type_T * 1050 static type_T *
1035 ret_list_number(int argcount UNUSED, type2_T *argtypes UNUSED) 1051 ret_list_number(int argcount UNUSED,
1052 type2_T *argtypes UNUSED,
1053 type_T **decl_type UNUSED)
1036 { 1054 {
1037 return &t_list_number; 1055 return &t_list_number;
1038 } 1056 }
1039 static type_T * 1057 static type_T *
1040 ret_list_string(int argcount UNUSED, type2_T *argtypes UNUSED) 1058 ret_range(int argcount UNUSED,
1059 type2_T *argtypes UNUSED,
1060 type_T **decl_type)
1061 {
1062 // returning a list<number>, but it's not declared as such
1063 *decl_type = &t_list_any;
1064 return &t_list_number;
1065 }
1066 static type_T *
1067 ret_list_string(int argcount UNUSED,
1068 type2_T *argtypes UNUSED,
1069 type_T **decl_type UNUSED)
1041 { 1070 {
1042 return &t_list_string; 1071 return &t_list_string;
1043 } 1072 }
1044 static type_T * 1073 static type_T *
1045 ret_list_dict_any(int argcount UNUSED, type2_T *argtypes UNUSED) 1074 ret_list_dict_any(int argcount UNUSED,
1075 type2_T *argtypes UNUSED,
1076 type_T **decl_type UNUSED)
1046 { 1077 {
1047 return &t_list_dict_any; 1078 return &t_list_dict_any;
1048 } 1079 }
1049 static type_T * 1080 static type_T *
1050 ret_list_items(int argcount UNUSED, type2_T *argtypes UNUSED) 1081 ret_list_items(int argcount UNUSED,
1082 type2_T *argtypes UNUSED,
1083 type_T **decl_type UNUSED)
1051 { 1084 {
1052 return &t_list_list_any; 1085 return &t_list_list_any;
1053 } 1086 }
1054 1087
1055 static type_T * 1088 static type_T *
1056 ret_list_string_items(int argcount UNUSED, type2_T *argtypes UNUSED) 1089 ret_list_string_items(int argcount UNUSED,
1090 type2_T *argtypes UNUSED,
1091 type_T **decl_type UNUSED)
1057 { 1092 {
1058 return &t_list_list_string; 1093 return &t_list_list_string;
1059 } 1094 }
1060 static type_T * 1095 static type_T *
1061 ret_dict_any(int argcount UNUSED, type2_T *argtypes UNUSED) 1096 ret_dict_any(int argcount UNUSED,
1097 type2_T *argtypes UNUSED,
1098 type_T **decl_type UNUSED)
1062 { 1099 {
1063 return &t_dict_any; 1100 return &t_dict_any;
1064 } 1101 }
1065 static type_T * 1102 static type_T *
1066 ret_job_info(int argcount, type2_T *argtypes UNUSED) 1103 ret_job_info(int argcount,
1104 type2_T *argtypes UNUSED,
1105 type_T **decl_type UNUSED)
1067 { 1106 {
1068 if (argcount == 0) 1107 if (argcount == 0)
1069 return &t_list_job; 1108 return &t_list_job;
1070 return &t_dict_any; 1109 return &t_dict_any;
1071 } 1110 }
1072 static type_T * 1111 static type_T *
1073 ret_dict_number(int argcount UNUSED, type2_T *argtypes UNUSED) 1112 ret_dict_number(int argcount UNUSED,
1113 type2_T *argtypes UNUSED,
1114 type_T **decl_type UNUSED)
1074 { 1115 {
1075 return &t_dict_number; 1116 return &t_dict_number;
1076 } 1117 }
1077 static type_T * 1118 static type_T *
1078 ret_dict_string(int argcount UNUSED, type2_T *argtypes UNUSED) 1119 ret_dict_string(int argcount UNUSED,
1120 type2_T *argtypes UNUSED,
1121 type_T **decl_type UNUSED)
1079 { 1122 {
1080 return &t_dict_string; 1123 return &t_dict_string;
1081 } 1124 }
1082 static type_T * 1125 static type_T *
1083 ret_blob(int argcount UNUSED, type2_T *argtypes UNUSED) 1126 ret_blob(int argcount UNUSED,
1127 type2_T *argtypes UNUSED,
1128 type_T **decl_type UNUSED)
1084 { 1129 {
1085 return &t_blob; 1130 return &t_blob;
1086 } 1131 }
1087 static type_T * 1132 static type_T *
1088 ret_func_any(int argcount UNUSED, type2_T *argtypes UNUSED) 1133 ret_func_any(int argcount UNUSED,
1134 type2_T *argtypes UNUSED,
1135 type_T **decl_type UNUSED)
1089 { 1136 {
1090 return &t_func_any; 1137 return &t_func_any;
1091 } 1138 }
1092 static type_T * 1139 static type_T *
1093 ret_func_unknown(int argcount UNUSED, type2_T *argtypes UNUSED) 1140 ret_func_unknown(int argcount UNUSED,
1141 type2_T *argtypes UNUSED,
1142 type_T **decl_type UNUSED)
1094 { 1143 {
1095 return &t_func_unknown; 1144 return &t_func_unknown;
1096 } 1145 }
1097 static type_T * 1146 static type_T *
1098 ret_channel(int argcount UNUSED, type2_T *argtypes UNUSED) 1147 ret_channel(int argcount UNUSED,
1148 type2_T *argtypes UNUSED,
1149 type_T **decl_type UNUSED)
1099 { 1150 {
1100 return &t_channel; 1151 return &t_channel;
1101 } 1152 }
1102 static type_T * 1153 static type_T *
1103 ret_job(int argcount UNUSED, type2_T *argtypes UNUSED) 1154 ret_job(int argcount UNUSED,
1155 type2_T *argtypes UNUSED,
1156 type_T **decl_type UNUSED)
1104 { 1157 {
1105 return &t_job; 1158 return &t_job;
1106 } 1159 }
1107 static type_T * 1160 static type_T *
1108 ret_first_arg(int argcount, type2_T *argtypes) 1161 ret_first_arg(int argcount,
1162 type2_T *argtypes,
1163 type_T **decl_type)
1109 { 1164 {
1110 if (argcount > 0) 1165 if (argcount > 0)
1166 {
1167 *decl_type = argtypes[0].type_decl;
1111 return argtypes[0].type_curr; 1168 return argtypes[0].type_curr;
1169 }
1112 return &t_void; 1170 return &t_void;
1113 } 1171 }
1114 static type_T * 1172 static type_T *
1115 ret_repeat(int argcount, type2_T *argtypes) 1173 ret_extend(int argcount,
1174 type2_T *argtypes,
1175 type_T **decl_type)
1176 {
1177 if (argcount > 0)
1178 {
1179 *decl_type = argtypes[0].type_decl;
1180 // if the second argument has a different current type then the current
1181 // type is "any"
1182 if (argcount > 1 && !equal_type(argtypes[0].type_curr,
1183 argtypes[1].type_curr, 0))
1184 {
1185 if (argtypes[0].type_curr->tt_type == VAR_LIST)
1186 return &t_list_any;
1187 if (argtypes[0].type_curr->tt_type == VAR_DICT)
1188 return &t_dict_any;
1189 }
1190 return argtypes[0].type_curr;
1191 }
1192 return &t_void;
1193 }
1194 static type_T *
1195 ret_repeat(int argcount,
1196 type2_T *argtypes,
1197 type_T **decl_type UNUSED)
1116 { 1198 {
1117 if (argcount == 0) 1199 if (argcount == 0)
1118 return &t_any; 1200 return &t_any;
1119 if (argtypes[0].type_curr == &t_number) 1201 if (argtypes[0].type_curr == &t_number)
1120 return &t_string; 1202 return &t_string;
1121 return argtypes[0].type_curr; 1203 return argtypes[0].type_curr;
1122 } 1204 }
1123 // for map(): returns first argument but item type may differ 1205 // for map(): returns first argument but item type may differ
1124 static type_T * 1206 static type_T *
1125 ret_first_cont(int argcount, type2_T *argtypes) 1207 ret_first_cont(int argcount,
1208 type2_T *argtypes,
1209 type_T **decl_type UNUSED)
1126 { 1210 {
1127 if (argcount > 0) 1211 if (argcount > 0)
1128 { 1212 {
1129 if (argtypes[0].type_curr->tt_type == VAR_LIST) 1213 if (argtypes[0].type_curr->tt_type == VAR_LIST)
1130 return &t_list_any; 1214 return &t_list_any;
1135 } 1219 }
1136 return &t_any; 1220 return &t_any;
1137 } 1221 }
1138 // for getline() 1222 // for getline()
1139 static type_T * 1223 static type_T *
1140 ret_getline(int argcount, type2_T *argtypes UNUSED) 1224 ret_getline(int argcount,
1225 type2_T *argtypes UNUSED,
1226 type_T **decl_type UNUSED)
1141 { 1227 {
1142 return argcount == 1 ? &t_string : &t_list_string; 1228 return argcount == 1 ? &t_string : &t_list_string;
1143 } 1229 }
1144 // for finddir() 1230 // for finddir()
1145 static type_T * 1231 static type_T *
1146 ret_finddir(int argcount, type2_T *argtypes UNUSED) 1232 ret_finddir(int argcount,
1233 type2_T *argtypes UNUSED,
1234 type_T **decl_type UNUSED)
1147 { 1235 {
1148 if (argcount < 3) 1236 if (argcount < 3)
1149 return &t_string; 1237 return &t_string;
1150 // Depending on the count would be a string or a list of strings. 1238 // Depending on the count would be a string or a list of strings.
1151 return &t_any; 1239 return &t_any;
1154 /* 1242 /*
1155 * Used for getqflist(): returns list if there is no argument, dict if there is 1243 * Used for getqflist(): returns list if there is no argument, dict if there is
1156 * one. 1244 * one.
1157 */ 1245 */
1158 static type_T * 1246 static type_T *
1159 ret_list_or_dict_0(int argcount, type2_T *argtypes UNUSED) 1247 ret_list_or_dict_0(int argcount,
1248 type2_T *argtypes UNUSED,
1249 type_T **decl_type UNUSED)
1160 { 1250 {
1161 if (argcount > 0) 1251 if (argcount > 0)
1162 return &t_dict_any; 1252 return &t_dict_any;
1163 return &t_list_dict_any; 1253 return &t_list_dict_any;
1164 } 1254 }
1166 /* 1256 /*
1167 * Used for getloclist(): returns list if there is one argument, dict if there 1257 * Used for getloclist(): returns list if there is one argument, dict if there
1168 * are two. 1258 * are two.
1169 */ 1259 */
1170 static type_T * 1260 static type_T *
1171 ret_list_or_dict_1(int argcount, type2_T *argtypes UNUSED) 1261 ret_list_or_dict_1(int argcount,
1262 type2_T *argtypes UNUSED,
1263 type_T **decl_type UNUSED)
1172 { 1264 {
1173 if (argcount > 1) 1265 if (argcount > 1)
1174 return &t_dict_any; 1266 return &t_dict_any;
1175 return &t_list_dict_any; 1267 return &t_list_dict_any;
1176 } 1268 }
1177 1269
1178 static type_T * 1270 static type_T *
1179 ret_argv(int argcount, type2_T *argtypes UNUSED) 1271 ret_argv(int argcount,
1272 type2_T *argtypes UNUSED,
1273 type_T **decl_type UNUSED)
1180 { 1274 {
1181 // argv() returns list of strings 1275 // argv() returns list of strings
1182 if (argcount == 0) 1276 if (argcount == 0)
1183 return &t_list_string; 1277 return &t_list_string;
1184 1278
1185 // argv(0) returns a string, but argv(-1] returns a list 1279 // argv(0) returns a string, but argv(-1] returns a list
1186 return &t_any; 1280 return &t_any;
1187 } 1281 }
1188 1282
1189 static type_T * 1283 static type_T *
1190 ret_remove(int argcount, type2_T *argtypes) 1284 ret_remove(int argcount,
1285 type2_T *argtypes,
1286 type_T **decl_type UNUSED)
1191 { 1287 {
1192 if (argcount > 0) 1288 if (argcount > 0)
1193 { 1289 {
1194 if (argtypes[0].type_curr->tt_type == VAR_LIST 1290 if (argtypes[0].type_curr->tt_type == VAR_LIST
1195 || argtypes[0].type_curr->tt_type == VAR_DICT) 1291 || argtypes[0].type_curr->tt_type == VAR_DICT)
1292 {
1293 if (argtypes[0].type_curr->tt_type
1294 == argtypes[0].type_decl->tt_type)
1295 *decl_type = argtypes[0].type_decl->tt_member;
1196 return argtypes[0].type_curr->tt_member; 1296 return argtypes[0].type_curr->tt_member;
1297 }
1197 if (argtypes[0].type_curr->tt_type == VAR_BLOB) 1298 if (argtypes[0].type_curr->tt_type == VAR_BLOB)
1198 return &t_number; 1299 return &t_number;
1199 } 1300 }
1200 return &t_any; 1301 return &t_any;
1201 } 1302 }
1202 1303
1203 static type_T * 1304 static type_T *
1204 ret_getreg(int argcount, type2_T *argtypes UNUSED) 1305 ret_getreg(int argcount,
1306 type2_T *argtypes UNUSED,
1307 type_T **decl_type UNUSED)
1205 { 1308 {
1206 // Assume that if the third argument is passed it's non-zero 1309 // Assume that if the third argument is passed it's non-zero
1207 if (argcount == 3) 1310 if (argcount == 3)
1208 return &t_list_string; 1311 return &t_list_string;
1209 return &t_string; 1312 return &t_string;
1210 } 1313 }
1211 1314
1212 static type_T * 1315 static type_T *
1213 ret_maparg(int argcount, type2_T *argtypes UNUSED) 1316 ret_maparg(int argcount,
1317 type2_T *argtypes UNUSED,
1318 type_T **decl_type UNUSED)
1214 { 1319 {
1215 // Assume that if the fourth argument is passed it's non-zero 1320 // Assume that if the fourth argument is passed it's non-zero
1216 if (argcount == 4) 1321 if (argcount == 4)
1217 return &t_dict_any; 1322 return &t_dict_any;
1218 return &t_string; 1323 return &t_string;
1227 char *f_name; // function name 1332 char *f_name; // function name
1228 char f_min_argc; // minimal number of arguments 1333 char f_min_argc; // minimal number of arguments
1229 char f_max_argc; // maximal number of arguments 1334 char f_max_argc; // maximal number of arguments
1230 char f_argtype; // for method: FEARG_ values 1335 char f_argtype; // for method: FEARG_ values
1231 argcheck_T *f_argcheck; // list of functions to check argument types 1336 argcheck_T *f_argcheck; // list of functions to check argument types
1232 type_T *(*f_retfunc)(int argcount, type2_T *argtypes); 1337 type_T *(*f_retfunc)(int argcount, type2_T *argtypes,
1338 type_T **decl_type);
1233 // return type function 1339 // return type function
1234 void (*f_func)(typval_T *args, typval_T *rvar); 1340 void (*f_func)(typval_T *args, typval_T *rvar);
1235 // implementation of function 1341 // implementation of function
1236 } funcentry_T; 1342 } funcentry_T;
1237 1343
1531 {"expand", 1, 3, FEARG_1, arg3_string_bool_bool, 1637 {"expand", 1, 3, FEARG_1, arg3_string_bool_bool,
1532 ret_any, f_expand}, 1638 ret_any, f_expand},
1533 {"expandcmd", 1, 1, FEARG_1, arg1_string, 1639 {"expandcmd", 1, 1, FEARG_1, arg1_string,
1534 ret_string, f_expandcmd}, 1640 ret_string, f_expandcmd},
1535 {"extend", 2, 3, FEARG_1, arg23_extend, 1641 {"extend", 2, 3, FEARG_1, arg23_extend,
1536 ret_first_arg, f_extend}, 1642 ret_extend, f_extend},
1537 {"extendnew", 2, 3, FEARG_1, arg23_extendnew, 1643 {"extendnew", 2, 3, FEARG_1, arg23_extendnew,
1538 ret_first_cont, f_extendnew}, 1644 ret_first_cont, f_extendnew},
1539 {"feedkeys", 1, 2, FEARG_1, arg2_string, 1645 {"feedkeys", 1, 2, FEARG_1, arg2_string,
1540 ret_void, f_feedkeys}, 1646 ret_void, f_feedkeys},
1541 {"file_readable", 1, 1, FEARG_1, arg1_string, // obsolete 1647 {"file_readable", 1, 1, FEARG_1, arg1_string, // obsolete
1989 #endif 2095 #endif
1990 }, 2096 },
1991 {"rand", 0, 1, FEARG_1, arg1_list_number, 2097 {"rand", 0, 1, FEARG_1, arg1_list_number,
1992 ret_number, f_rand}, 2098 ret_number, f_rand},
1993 {"range", 1, 3, FEARG_1, arg3_number, 2099 {"range", 1, 3, FEARG_1, arg3_number,
1994 ret_list_number, f_range}, 2100 ret_range, f_range},
1995 {"readblob", 1, 1, FEARG_1, arg1_string, 2101 {"readblob", 1, 1, FEARG_1, arg1_string,
1996 ret_blob, f_readblob}, 2102 ret_blob, f_readblob},
1997 {"readdir", 1, 3, FEARG_1, arg3_string_any_dict, 2103 {"readdir", 1, 3, FEARG_1, arg3_string_any_dict,
1998 ret_list_string, f_readdir}, 2104 ret_list_string, f_readdir},
1999 {"readdirex", 1, 3, FEARG_1, arg3_string_any_dict, 2105 {"readdirex", 1, 3, FEARG_1, arg3_string_any_dict,
2620 *min_argcount = global_functions[idx].f_min_argc; 2726 *min_argcount = global_functions[idx].f_min_argc;
2621 } 2727 }
2622 2728
2623 /* 2729 /*
2624 * Call the "f_retfunc" function to obtain the return type of function "idx". 2730 * Call the "f_retfunc" function to obtain the return type of function "idx".
2731 * "decl_type" is set to the declared type.
2625 * "argtypes" is the list of argument types or NULL when there are no 2732 * "argtypes" is the list of argument types or NULL when there are no
2626 * arguments. 2733 * arguments.
2627 * "argcount" may be less than the actual count when only getting the type. 2734 * "argcount" may be less than the actual count when only getting the type.
2628 */ 2735 */
2629 type_T * 2736 type_T *
2630 internal_func_ret_type(int idx, int argcount, type2_T *argtypes) 2737 internal_func_ret_type(
2631 { 2738 int idx,
2632 return global_functions[idx].f_retfunc(argcount, argtypes); 2739 int argcount,
2740 type2_T *argtypes,
2741 type_T **decl_type)
2742 {
2743 type_T *ret;
2744
2745 *decl_type = NULL;
2746 ret = global_functions[idx].f_retfunc(argcount, argtypes, decl_type);
2747 if (*decl_type == NULL)
2748 *decl_type = ret;
2749 return ret;
2633 } 2750 }
2634 2751
2635 /* 2752 /*
2636 * Return TRUE if "idx" is for the map() function. 2753 * Return TRUE if "idx" is for the map() function.
2637 */ 2754 */