mirror of
https://github.com/vim/vim.git
synced 2025-09-24 03:44:06 -04:00
patch 9.0.0629: get an error for using const only when executing
Problem: Get an error for using const only when executing. Solution: Check for const at compile time for filter(), map(), remove(), reverse(), sort() and uniq().
This commit is contained in:
@@ -214,13 +214,27 @@ check_arg_type(
|
|||||||
type_T *actual,
|
type_T *actual,
|
||||||
argcontext_T *context)
|
argcontext_T *context)
|
||||||
{
|
{
|
||||||
// TODO: would be useful to know if "actual" is a constant and pass it to
|
|
||||||
// need_type() to get a compile time error if possible.
|
|
||||||
return need_type(actual, expected,
|
return need_type(actual, expected,
|
||||||
context->arg_idx - context->arg_count, context->arg_idx + 1,
|
context->arg_idx - context->arg_count, context->arg_idx + 1,
|
||||||
context->arg_cctx, FALSE, FALSE);
|
context->arg_cctx, FALSE, FALSE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Call need_type() to check an argument type and that it is modifiable
|
||||||
|
*/
|
||||||
|
static int
|
||||||
|
check_arg_type_mod(
|
||||||
|
type_T *expected,
|
||||||
|
type_T *actual,
|
||||||
|
argcontext_T *context)
|
||||||
|
{
|
||||||
|
if (need_type(actual, expected,
|
||||||
|
context->arg_idx - context->arg_count, context->arg_idx + 1,
|
||||||
|
context->arg_cctx, FALSE, FALSE) == FAIL)
|
||||||
|
return FAIL;
|
||||||
|
return arg_type_modifiable(actual, context->arg_idx + 1);
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Give an error if "type" is a constant.
|
* Give an error if "type" is a constant.
|
||||||
*/
|
*/
|
||||||
@@ -279,6 +293,18 @@ arg_list_any(type_T *type, type_T *decl_type UNUSED, argcontext_T *context)
|
|||||||
return check_arg_type(&t_list_any, type, context);
|
return check_arg_type(&t_list_any, type, context);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Check "type" is a list of 'any' and modifiable
|
||||||
|
*/
|
||||||
|
static int
|
||||||
|
arg_list_any_mod(
|
||||||
|
type_T *type,
|
||||||
|
type_T *decl_type UNUSED,
|
||||||
|
argcontext_T *context)
|
||||||
|
{
|
||||||
|
return check_arg_type_mod(&t_list_any, type, context);
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Check "type" is a list of numbers.
|
* Check "type" is a list of numbers.
|
||||||
*/
|
*/
|
||||||
@@ -513,16 +539,20 @@ arg_list_or_dict_mod(
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
* Check "type" is a list of 'any' or a dict of 'any' or a blob.
|
* Check "type" is a list of 'any' or a dict of 'any' or a blob.
|
||||||
|
* Also check if "type" is modifiable.
|
||||||
*/
|
*/
|
||||||
static int
|
static int
|
||||||
arg_list_or_dict_or_blob(type_T *type, type_T *decl_type UNUSED, argcontext_T *context)
|
arg_list_or_dict_or_blob_mod(
|
||||||
|
type_T *type,
|
||||||
|
type_T *decl_type UNUSED,
|
||||||
|
argcontext_T *context)
|
||||||
{
|
{
|
||||||
if (type->tt_type == VAR_ANY
|
if (type->tt_type == VAR_ANY
|
||||||
|| type->tt_type == VAR_UNKNOWN
|
|| type->tt_type == VAR_UNKNOWN
|
||||||
|| type->tt_type == VAR_LIST
|
|| type->tt_type == VAR_LIST
|
||||||
|| type->tt_type == VAR_DICT
|
|| type->tt_type == VAR_DICT
|
||||||
|| type->tt_type == VAR_BLOB)
|
|| type->tt_type == VAR_BLOB)
|
||||||
return OK;
|
return arg_type_modifiable(type, context->arg_idx + 1);
|
||||||
arg_type_mismatch(&t_list_any, type, context->arg_idx + 1);
|
arg_type_mismatch(&t_list_any, type, context->arg_idx + 1);
|
||||||
return FAIL;
|
return FAIL;
|
||||||
}
|
}
|
||||||
@@ -544,6 +574,21 @@ arg_list_or_dict_or_blob_or_string(type_T *type, type_T *decl_type UNUSED, argco
|
|||||||
return FAIL;
|
return FAIL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Check "type" is a list of 'any' or a dict of 'any' or a blob or a string.
|
||||||
|
* Also check the value is modifiable.
|
||||||
|
*/
|
||||||
|
static int
|
||||||
|
arg_list_or_dict_or_blob_or_string_mod(
|
||||||
|
type_T *type,
|
||||||
|
type_T *decl_type,
|
||||||
|
argcontext_T *context)
|
||||||
|
{
|
||||||
|
if (arg_list_or_dict_or_blob_or_string(type, decl_type, context) == FAIL)
|
||||||
|
return FAIL;
|
||||||
|
return arg_type_modifiable(type, context->arg_idx + 1);
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Check second argument of map() or filter().
|
* Check second argument of map() or filter().
|
||||||
*/
|
*/
|
||||||
@@ -994,7 +1039,7 @@ static argcheck_T arg1_float_or_nr[] = {arg_float_or_nr};
|
|||||||
static argcheck_T arg1_job[] = {arg_job};
|
static argcheck_T arg1_job[] = {arg_job};
|
||||||
static argcheck_T arg1_list_any[] = {arg_list_any};
|
static argcheck_T arg1_list_any[] = {arg_list_any};
|
||||||
static argcheck_T arg1_list_number[] = {arg_list_number};
|
static argcheck_T arg1_list_number[] = {arg_list_number};
|
||||||
static argcheck_T arg1_list_or_blob[] = {arg_list_or_blob};
|
static argcheck_T arg1_list_or_blob_mod[] = {arg_list_or_blob_mod};
|
||||||
static argcheck_T arg1_list_or_dict[] = {arg_list_or_dict};
|
static argcheck_T arg1_list_or_dict[] = {arg_list_or_dict};
|
||||||
static argcheck_T arg1_list_string[] = {arg_list_string};
|
static argcheck_T arg1_list_string[] = {arg_list_string};
|
||||||
static argcheck_T arg1_string_or_list_or_dict[] = {arg_string_or_list_or_dict};
|
static argcheck_T arg1_string_or_list_or_dict[] = {arg_string_or_list_or_dict};
|
||||||
@@ -1091,15 +1136,15 @@ static argcheck_T arg23_insert[] = {arg_list_or_blob, arg_item_of_prev, arg_numb
|
|||||||
static argcheck_T arg1_len[] = {arg_len1};
|
static argcheck_T arg1_len[] = {arg_len1};
|
||||||
static argcheck_T arg3_libcall[] = {arg_string, arg_string, arg_string_or_nr};
|
static argcheck_T arg3_libcall[] = {arg_string, arg_string, arg_string_or_nr};
|
||||||
static argcheck_T arg14_maparg[] = {arg_string, arg_string, arg_bool, arg_bool};
|
static argcheck_T arg14_maparg[] = {arg_string, arg_string, arg_bool, arg_bool};
|
||||||
static argcheck_T arg2_filter[] = {arg_list_or_dict_or_blob_or_string, arg_filter_func};
|
static argcheck_T arg2_filter[] = {arg_list_or_dict_or_blob_or_string_mod, arg_filter_func};
|
||||||
static argcheck_T arg2_map[] = {arg_list_or_dict_or_blob_or_string, arg_map_func};
|
static argcheck_T arg2_map[] = {arg_list_or_dict_or_blob_or_string_mod, arg_map_func};
|
||||||
static argcheck_T arg2_mapnew[] = {arg_list_or_dict_or_blob_or_string, NULL};
|
static argcheck_T arg2_mapnew[] = {arg_list_or_dict_or_blob_or_string, NULL};
|
||||||
static argcheck_T arg25_matchadd[] = {arg_string, arg_string, arg_number, arg_number, arg_dict_any};
|
static argcheck_T arg25_matchadd[] = {arg_string, arg_string, arg_number, arg_number, arg_dict_any};
|
||||||
static argcheck_T arg25_matchaddpos[] = {arg_string, arg_list_any, arg_number, arg_number, arg_dict_any};
|
static argcheck_T arg25_matchaddpos[] = {arg_string, arg_list_any, arg_number, arg_number, arg_dict_any};
|
||||||
static argcheck_T arg119_printf[] = {arg_string_or_nr, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
|
static argcheck_T arg119_printf[] = {arg_string_or_nr, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
|
||||||
static argcheck_T arg23_reduce[] = {arg_string_list_or_blob, NULL, NULL};
|
static argcheck_T arg23_reduce[] = {arg_string_list_or_blob, NULL, NULL};
|
||||||
static argcheck_T arg24_remote_expr[] = {arg_string, arg_string, arg_string, arg_number};
|
static argcheck_T arg24_remote_expr[] = {arg_string, arg_string, arg_string, arg_number};
|
||||||
static argcheck_T arg23_remove[] = {arg_list_or_dict_or_blob, arg_remove2, arg_number};
|
static argcheck_T arg23_remove[] = {arg_list_or_dict_or_blob_mod, arg_remove2, arg_number};
|
||||||
static argcheck_T arg2_repeat[] = {arg_repeat1, arg_number};
|
static argcheck_T arg2_repeat[] = {arg_repeat1, arg_number};
|
||||||
static argcheck_T arg15_search[] = {arg_string, arg_string, arg_number, arg_number, arg_string_or_func};
|
static argcheck_T arg15_search[] = {arg_string, arg_string, arg_number, arg_number, arg_string_or_func};
|
||||||
static argcheck_T arg37_searchpair[] = {arg_string, arg_string, arg_string, arg_string, arg_string_or_func, arg_number, arg_number};
|
static argcheck_T arg37_searchpair[] = {arg_string, arg_string, arg_string, arg_string, arg_string_or_func, arg_number, arg_number};
|
||||||
@@ -1111,7 +1156,7 @@ static argcheck_T arg23_settagstack[] = {arg_number, arg_dict_any, arg_string};
|
|||||||
static argcheck_T arg02_sign_getplaced[] = {arg_buffer, arg_dict_any};
|
static argcheck_T arg02_sign_getplaced[] = {arg_buffer, arg_dict_any};
|
||||||
static argcheck_T arg45_sign_place[] = {arg_number, arg_string, arg_string, arg_buffer, arg_dict_any};
|
static argcheck_T arg45_sign_place[] = {arg_number, arg_string, arg_string, arg_buffer, arg_dict_any};
|
||||||
static argcheck_T arg23_slice[] = {arg_slice1, arg_number, arg_number};
|
static argcheck_T arg23_slice[] = {arg_slice1, arg_number, arg_number};
|
||||||
static argcheck_T arg13_sortuniq[] = {arg_list_any, arg_sort_how, arg_dict_any};
|
static argcheck_T arg13_sortuniq[] = {arg_list_any_mod, arg_sort_how, arg_dict_any};
|
||||||
static argcheck_T arg24_strpart[] = {arg_string, arg_number, arg_number, arg_bool};
|
static argcheck_T arg24_strpart[] = {arg_string, arg_number, arg_number, arg_bool};
|
||||||
static argcheck_T arg12_system[] = {arg_string, arg_str_or_nr_or_list};
|
static argcheck_T arg12_system[] = {arg_string, arg_str_or_nr_or_list};
|
||||||
static argcheck_T arg23_win_execute[] = {arg_number, arg_string_or_list_string, arg_string};
|
static argcheck_T arg23_win_execute[] = {arg_number, arg_string_or_list_string, arg_string};
|
||||||
@@ -2362,7 +2407,7 @@ static funcentry_T global_functions[] =
|
|||||||
ret_repeat, f_repeat},
|
ret_repeat, f_repeat},
|
||||||
{"resolve", 1, 1, FEARG_1, arg1_string,
|
{"resolve", 1, 1, FEARG_1, arg1_string,
|
||||||
ret_string, f_resolve},
|
ret_string, f_resolve},
|
||||||
{"reverse", 1, 1, FEARG_1, arg1_list_or_blob,
|
{"reverse", 1, 1, FEARG_1, arg1_list_or_blob_mod,
|
||||||
ret_first_arg, f_reverse},
|
ret_first_arg, f_reverse},
|
||||||
{"round", 1, 1, FEARG_1, arg1_float_or_nr,
|
{"round", 1, 1, FEARG_1, arg1_float_or_nr,
|
||||||
ret_float, f_round},
|
ret_float, f_round},
|
||||||
|
@@ -1528,6 +1528,20 @@ def Test_filter_missing_argument()
|
|||||||
res->assert_equal({aa: [1], ac: [3]})
|
res->assert_equal({aa: [1], ac: [3]})
|
||||||
enddef
|
enddef
|
||||||
|
|
||||||
|
def Test_filter_const()
|
||||||
|
var lines =<< trim END
|
||||||
|
const l = [1, 2, 3]
|
||||||
|
filter(l, 'v:val == 2')
|
||||||
|
END
|
||||||
|
v9.CheckDefFailure(lines, 'E1307: Argument 1: Trying to modify a const list<number>')
|
||||||
|
|
||||||
|
lines =<< trim END
|
||||||
|
const d = {a: 1, b: 2}
|
||||||
|
filter(d, 'v:val == 2')
|
||||||
|
END
|
||||||
|
v9.CheckDefFailure(lines, 'E1307: Argument 1: Trying to modify a const dict<number>')
|
||||||
|
enddef
|
||||||
|
|
||||||
def Test_foldclosed()
|
def Test_foldclosed()
|
||||||
v9.CheckDefAndScriptFailure(['foldclosed(function("min"))'], ['E1013: Argument 1: type mismatch, expected string but got func(...): unknown', 'E1220: String or Number required for argument 1'])
|
v9.CheckDefAndScriptFailure(['foldclosed(function("min"))'], ['E1013: Argument 1: type mismatch, expected string but got func(...): unknown', 'E1220: String or Number required for argument 1'])
|
||||||
v9.CheckDefExecAndScriptFailure(['foldclosed("")'], 'E1209: Invalid value for a line number')
|
v9.CheckDefExecAndScriptFailure(['foldclosed("")'], 'E1209: Invalid value for a line number')
|
||||||
@@ -2547,6 +2561,20 @@ def Test_map_failure()
|
|||||||
v9.CheckDefExecAndScriptFailure(lines, 'E1012: Type mismatch; expected number but got bool')
|
v9.CheckDefExecAndScriptFailure(lines, 'E1012: Type mismatch; expected number but got bool')
|
||||||
enddef
|
enddef
|
||||||
|
|
||||||
|
def Test_map_const()
|
||||||
|
var lines =<< trim END
|
||||||
|
const l = [1, 2, 3]
|
||||||
|
map(l, 'SomeFunc')
|
||||||
|
END
|
||||||
|
v9.CheckDefFailure(lines, 'E1307: Argument 1: Trying to modify a const list<number>')
|
||||||
|
|
||||||
|
lines =<< trim END
|
||||||
|
const d = {a: 1, b: 2}
|
||||||
|
map(d, 'SomeFunc')
|
||||||
|
END
|
||||||
|
v9.CheckDefFailure(lines, 'E1307: Argument 1: Trying to modify a const dict<number>')
|
||||||
|
enddef
|
||||||
|
|
||||||
def Test_map_function_arg()
|
def Test_map_function_arg()
|
||||||
var lines =<< trim END
|
var lines =<< trim END
|
||||||
def MapOne(i: number, v: string): string
|
def MapOne(i: number, v: string): string
|
||||||
@@ -3334,12 +3362,32 @@ def Test_remote_startserver()
|
|||||||
v9.CheckDefAndScriptFailure(['remote_startserver({})'], ['E1013: Argument 1: type mismatch, expected string but got dict<unknown>', 'E1174: String required for argument 1'])
|
v9.CheckDefAndScriptFailure(['remote_startserver({})'], ['E1013: Argument 1: type mismatch, expected string but got dict<unknown>', 'E1174: String required for argument 1'])
|
||||||
enddef
|
enddef
|
||||||
|
|
||||||
def Test_remove_const_list()
|
def Test_remove_literal_list()
|
||||||
var l: list<number> = [1, 2, 3, 4]
|
var l: list<number> = [1, 2, 3, 4]
|
||||||
assert_equal([1, 2], remove(l, 0, 1))
|
assert_equal([1, 2], remove(l, 0, 1))
|
||||||
assert_equal([3, 4], l)
|
assert_equal([3, 4], l)
|
||||||
enddef
|
enddef
|
||||||
|
|
||||||
|
def Test_remove_const()
|
||||||
|
var lines =<< trim END
|
||||||
|
const l = [1, 2, 3, 4]
|
||||||
|
remove(l, 1)
|
||||||
|
END
|
||||||
|
v9.CheckDefFailure(lines, 'E1307: Argument 1: Trying to modify a const list<number>')
|
||||||
|
|
||||||
|
lines =<< trim END
|
||||||
|
const d = {a: 1, b: 2}
|
||||||
|
remove(d, 'a')
|
||||||
|
END
|
||||||
|
v9.CheckDefFailure(lines, 'E1307: Argument 1: Trying to modify a const dict<number>')
|
||||||
|
|
||||||
|
lines =<< trim END
|
||||||
|
const b = 0z010203
|
||||||
|
remove(b, 1)
|
||||||
|
END
|
||||||
|
v9.CheckDefFailure(lines, 'E1307: Argument 1: Trying to modify a const blob')
|
||||||
|
enddef
|
||||||
|
|
||||||
def Test_remove()
|
def Test_remove()
|
||||||
v9.CheckDefAndScriptFailure(['remove("a", 1)'], ['E1013: Argument 1: type mismatch, expected list<any> but got string', 'E1228: List, Dictionary or Blob required for argument 1'])
|
v9.CheckDefAndScriptFailure(['remove("a", 1)'], ['E1013: Argument 1: type mismatch, expected list<any> but got string', 'E1228: List, Dictionary or Blob required for argument 1'])
|
||||||
v9.CheckDefAndScriptFailure(['remove([], "b")'], ['E1013: Argument 2: type mismatch, expected number but got string', 'E1210: Number required for argument 2'])
|
v9.CheckDefAndScriptFailure(['remove([], "b")'], ['E1013: Argument 2: type mismatch, expected number but got string', 'E1210: Number required for argument 2'])
|
||||||
@@ -3419,6 +3467,20 @@ def Test_reverse_return_type()
|
|||||||
res->assert_equal(6)
|
res->assert_equal(6)
|
||||||
enddef
|
enddef
|
||||||
|
|
||||||
|
def Test_reverse_const()
|
||||||
|
var lines =<< trim END
|
||||||
|
const l = [1, 2, 3, 4]
|
||||||
|
reverse(l)
|
||||||
|
END
|
||||||
|
v9.CheckDefFailure(lines, 'E1307: Argument 1: Trying to modify a const list<number>')
|
||||||
|
|
||||||
|
lines =<< trim END
|
||||||
|
const b = 0z010203
|
||||||
|
reverse(b)
|
||||||
|
END
|
||||||
|
v9.CheckDefFailure(lines, 'E1307: Argument 1: Trying to modify a const blob')
|
||||||
|
enddef
|
||||||
|
|
||||||
def Test_rubyeval()
|
def Test_rubyeval()
|
||||||
if !has('ruby')
|
if !has('ruby')
|
||||||
CheckFeature ruby
|
CheckFeature ruby
|
||||||
@@ -4053,6 +4115,14 @@ def Test_sort_argument()
|
|||||||
v9.CheckScriptSuccess(lines)
|
v9.CheckScriptSuccess(lines)
|
||||||
enddef
|
enddef
|
||||||
|
|
||||||
|
def Test_sort_const()
|
||||||
|
var lines =<< trim END
|
||||||
|
const l = [1, 2, 3, 4]
|
||||||
|
sort(l)
|
||||||
|
END
|
||||||
|
v9.CheckDefFailure(lines, 'E1307: Argument 1: Trying to modify a const list<number>')
|
||||||
|
enddef
|
||||||
|
|
||||||
def Test_sort_compare_func_fails()
|
def Test_sort_compare_func_fails()
|
||||||
v9.CheckDefAndScriptFailure(['sort("a")'], ['E1013: Argument 1: type mismatch, expected list<any> but got string', 'E1211: List required for argument 1'])
|
v9.CheckDefAndScriptFailure(['sort("a")'], ['E1013: Argument 1: type mismatch, expected list<any> but got string', 'E1211: List required for argument 1'])
|
||||||
v9.CheckDefAndScriptFailure(['sort([1], "", [1])'], ['E1013: Argument 3: type mismatch, expected dict<any> but got list<number>', 'E1206: Dictionary required for argument 3'])
|
v9.CheckDefAndScriptFailure(['sort([1], "", [1])'], ['E1013: Argument 3: type mismatch, expected dict<any> but got list<number>', 'E1206: Dictionary required for argument 3'])
|
||||||
@@ -4654,6 +4724,14 @@ def Test_uniq()
|
|||||||
v9.CheckDefFailure(['var l: list<number> = uniq(["a", "b"])'], 'E1012: Type mismatch; expected list<number> but got list<string>')
|
v9.CheckDefFailure(['var l: list<number> = uniq(["a", "b"])'], 'E1012: Type mismatch; expected list<number> but got list<string>')
|
||||||
enddef
|
enddef
|
||||||
|
|
||||||
|
def Test_uniq_const()
|
||||||
|
var lines =<< trim END
|
||||||
|
const l = [1, 2, 3, 4]
|
||||||
|
uniq(l)
|
||||||
|
END
|
||||||
|
v9.CheckDefFailure(lines, 'E1307: Argument 1: Trying to modify a const list<number>')
|
||||||
|
enddef
|
||||||
|
|
||||||
def Test_values()
|
def Test_values()
|
||||||
v9.CheckDefAndScriptFailure(['values([])'], ['E1013: Argument 1: type mismatch, expected dict<any> but got list<unknown>', 'E1206: Dictionary required for argument 1'])
|
v9.CheckDefAndScriptFailure(['values([])'], ['E1013: Argument 1: type mismatch, expected dict<any> but got list<unknown>', 'E1206: Dictionary required for argument 1'])
|
||||||
assert_equal([], {}->values())
|
assert_equal([], {}->values())
|
||||||
|
@@ -699,6 +699,8 @@ static char *(features[]) =
|
|||||||
|
|
||||||
static int included_patches[] =
|
static int included_patches[] =
|
||||||
{ /* Add new patch number below this line */
|
{ /* Add new patch number below this line */
|
||||||
|
/**/
|
||||||
|
629,
|
||||||
/**/
|
/**/
|
||||||
628,
|
628,
|
||||||
/**/
|
/**/
|
||||||
|
Reference in New Issue
Block a user