To: vim_dev@googlegroups.com Subject: Patch 8.2.3176 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ Patch 8.2.3176 Problem: Vim9: no type error for comparing number with string. Solution: Add a runtime type check. (closes #8571) Files: src/typval.c, src/errors.h, src/testdir/test_vim9_expr.vim *** ../vim-8.2.3175/src/typval.c 2021-07-17 19:11:03.580709066 +0200 --- src/typval.c 2021-07-18 14:42:01.888065398 +0200 *************** *** 937,943 **** } } else if (in_vim9script() && (typ1->v_type == VAR_BOOL ! || typ2->v_type == VAR_BOOL)) { if (typ1->v_type != typ2->v_type) { --- 937,945 ---- } } else if (in_vim9script() && (typ1->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) { *************** *** 955,967 **** case EXPR_ISNOT: case EXPR_NEQUAL: n1 = (n1 != n2); break; default: ! emsg(_(e_invalid_operation_for_bool)); clear_tv(typ1); return FAIL; } } else { s1 = tv_get_string_buf(typ1, buf1); s2 = tv_get_string_buf(typ2, buf2); if (type != EXPR_MATCH && type != EXPR_NOMATCH) --- 957,979 ---- case EXPR_ISNOT: case EXPR_NEQUAL: n1 = (n1 != n2); break; default: ! semsg(_(e_invalid_operation_for_str), ! vartype_name(typ1->v_type)); clear_tv(typ1); return FAIL; } } 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); s2 = tv_get_string_buf(typ2, buf2); if (type != EXPR_MATCH && type != EXPR_NOMATCH) *** ../vim-8.2.3175/src/errors.h 2021-07-17 19:11:03.576709073 +0200 --- src/errors.h 2021-07-18 14:07:27.746449360 +0200 *************** *** 381,388 **** INIT(= N_("E1151: Mismatched endfunction")); EXTERN char e_mismatched_enddef[] INIT(= N_("E1152: Mismatched enddef")); ! EXTERN char e_invalid_operation_for_bool[] ! INIT(= N_("E1153: Invalid operation for bool")); EXTERN char e_divide_by_zero[] INIT(= N_("E1154: Divide by zero")); EXTERN char e_cannot_define_autocommands_for_all_events[] --- 381,388 ---- INIT(= N_("E1151: Mismatched endfunction")); EXTERN char e_mismatched_enddef[] INIT(= N_("E1152: Mismatched enddef")); ! EXTERN char e_invalid_operation_for_str[] ! INIT(= N_("E1153: Invalid operation for %s")); EXTERN char e_divide_by_zero[] INIT(= N_("E1154: Divide by zero")); EXTERN char e_cannot_define_autocommands_for_all_events[] *** ../vim-8.2.3175/src/testdir/test_vim9_expr.vim 2021-07-15 22:03:46.983932825 +0200 --- src/testdir/test_vim9_expr.vim 2021-07-18 14:18:37.905786077 +0200 *************** *** 660,672 **** --- 660,695 ---- 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 = 'a'", 'echo x == 99'], 'E1030:', 'E1072:', 2) + enddef + def Test_expr4_wrong_type() for op in ['>', '>=', '<', '<=', '=~', '!~'] CheckDefExecAndScriptFailure([ "var a: any = 'a'", 'var b: any = true', 'echo a ' .. op .. ' b'], 'E1072:', 3) 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 " test != comperator *** ../vim-8.2.3175/src/version.c 2021-07-18 13:42:25.903808826 +0200 --- src/version.c 2021-07-18 13:55:16.422943085 +0200 *************** *** 757,758 **** --- 757,760 ---- { /* Add new patch number below this line */ + /**/ + 3176, /**/ -- hundred-and-one symptoms of being an internet addict: 173. You keep tracking down the email addresses of all your friends (even childhood friends). /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\ /// \\\ \\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ /// \\\ help me help AIDS victims -- http://ICCF-Holland.org ///