0
0
mirror of https://github.com/vim/vim.git synced 2025-09-23 03:43:49 -04:00

patch 8.2.3176: Vim9: no type error for comparing number with string

Problem:    Vim9: no type error for comparing number with string.
Solution:   Add a runtime type check. (closes #8571)
This commit is contained in:
Bram Moolenaar
2021-07-18 14:43:43 +02:00
parent c6ba2f9dde
commit 0c35752d04
4 changed files with 41 additions and 4 deletions

View File

@@ -381,8 +381,8 @@ EXTERN char e_mismatched_endfunction[]
INIT(= N_("E1151: Mismatched endfunction")); INIT(= N_("E1151: Mismatched endfunction"));
EXTERN char e_mismatched_enddef[] EXTERN char e_mismatched_enddef[]
INIT(= N_("E1152: Mismatched enddef")); INIT(= N_("E1152: Mismatched enddef"));
EXTERN char e_invalid_operation_for_bool[] EXTERN char e_invalid_operation_for_str[]
INIT(= N_("E1153: Invalid operation for bool")); INIT(= N_("E1153: Invalid operation for %s"));
EXTERN char e_divide_by_zero[] EXTERN char e_divide_by_zero[]
INIT(= N_("E1154: Divide by zero")); INIT(= N_("E1154: Divide by zero"));
EXTERN char e_cannot_define_autocommands_for_all_events[] EXTERN char e_cannot_define_autocommands_for_all_events[]

View File

@@ -660,13 +660,36 @@ def Test_expr4_equal()
CheckDefExecAndScriptFailure(["var x: any = true", 'echo x == ""'], 'E1072: Cannot compare bool with string', 2) CheckDefExecAndScriptFailure(["var x: any = true", 'echo x == ""'], 'E1072: Cannot compare bool with string', 2)
CheckDefExecAndScriptFailure2(["var x: any = 99", 'echo x == true'], 'E1138', 'E1072:', 2) CheckDefExecAndScriptFailure2(["var x: any = 99", 'echo x == true'], 'E1138', 'E1072:', 2)
CheckDefExecAndScriptFailure2(["var x: any = 'a'", 'echo x == 99'], 'E1030:', 'E1072:', 2) CheckDefExecAndScriptFailure2(["var x: any = 'a'", 'echo x == 99'], 'E1030:', 'E1072:', 2)
enddef
def Test_expr4_wrong_type()
for op in ['>', '>=', '<', '<=', '=~', '!~'] for op in ['>', '>=', '<', '<=', '=~', '!~']
CheckDefExecAndScriptFailure([ CheckDefExecAndScriptFailure([
"var a: any = 'a'", "var a: any = 'a'",
'var b: any = true', 'var b: any = true',
'echo a ' .. op .. ' b'], 'E1072:', 3) 'echo a ' .. op .. ' b'], 'E1072:', 3)
endfor endfor
for op in ['>', '>=', '<', '<=']
CheckDefExecAndScriptFailure2([
"var n: any = 2",
'echo n ' .. op .. ' "3"'], 'E1030:', 'E1072:', 2)
endfor
for op in ['=~', '!~']
CheckDefExecAndScriptFailure([
"var n: any = 2",
'echo n ' .. op .. ' "3"'], 'E1072:', 2)
endfor
CheckDefAndScriptFailure([
'echo v:none == true'], 'E1072:', 1)
CheckDefAndScriptFailure([
'echo false >= true'], 'E1072:', 1)
CheckDefExecAndScriptFailure([
"var n: any = v:none",
'echo n == true'], 'E1072:', 2)
CheckDefExecAndScriptFailure([
"var n: any = v:none",
'echo n < true'], 'E1072:', 2)
enddef enddef
" test != comperator " test != comperator

View File

@@ -937,7 +937,9 @@ typval_compare(
} }
} }
else if (in_vim9script() && (typ1->v_type == VAR_BOOL else if (in_vim9script() && (typ1->v_type == VAR_BOOL
|| typ2->v_type == VAR_BOOL)) || typ2->v_type == VAR_BOOL
|| (typ1->v_type == VAR_SPECIAL
&& typ2->v_type == VAR_SPECIAL)))
{ {
if (typ1->v_type != typ2->v_type) if (typ1->v_type != typ2->v_type)
{ {
@@ -955,13 +957,23 @@ typval_compare(
case EXPR_ISNOT: case EXPR_ISNOT:
case EXPR_NEQUAL: n1 = (n1 != n2); break; case EXPR_NEQUAL: n1 = (n1 != n2); break;
default: default:
emsg(_(e_invalid_operation_for_bool)); semsg(_(e_invalid_operation_for_str),
vartype_name(typ1->v_type));
clear_tv(typ1); clear_tv(typ1);
return FAIL; return FAIL;
} }
} }
else else
{ {
if (in_vim9script()
&& ((typ1->v_type != VAR_STRING && typ1->v_type != VAR_SPECIAL)
|| (typ2->v_type != VAR_STRING && typ2->v_type != VAR_SPECIAL)))
{
semsg(_(e_cannot_compare_str_with_str),
vartype_name(typ1->v_type), vartype_name(typ2->v_type));
clear_tv(typ1);
return FAIL;
}
s1 = tv_get_string_buf(typ1, buf1); s1 = tv_get_string_buf(typ1, buf1);
s2 = tv_get_string_buf(typ2, buf2); s2 = tv_get_string_buf(typ2, buf2);
if (type != EXPR_MATCH && type != EXPR_NOMATCH) if (type != EXPR_MATCH && type != EXPR_NOMATCH)

View File

@@ -755,6 +755,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 */
/**/
3176,
/**/ /**/
3175, 3175,
/**/ /**/