]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 8.2.2320: Vim9: no error for comparing bool with string v8.2.2320
authorBram Moolenaar <Bram@vim.org>
Sat, 9 Jan 2021 15:21:37 +0000 (16:21 +0100)
committerBram Moolenaar <Bram@vim.org>
Sat, 9 Jan 2021 15:21:37 +0000 (16:21 +0100)
Problem:    Vim9: no error for comparing bool with string.
Solution:   Check for wrong types when comparing. (closes #7639)

src/errors.h
src/testdir/test_vim9_expr.vim
src/typval.c
src/version.c

index e71b706004156f81bc0b69d3b275729d38c83957..47e9876f68e7e27525b52d3b377b32aa7f3dfe55 100644 (file)
@@ -341,3 +341,5 @@ EXTERN char e_mismatched_endfunction[]
        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"));
index 6d82e5e27df26257bb1de4c5facfafcb0b507fd3..fc42088d81ce20251c38d46ac20eac8f269b7dd0 100644 (file)
@@ -597,6 +597,18 @@ def Test_expr4_equal()
   CheckDefFailure(["var x = 'a' == "], 'E1097:', 3)
 
   CheckDefExecFailure(['var items: any', 'eval 1', 'eval 2', 'if items == []', 'endif'], 'E691:', 4)
+
+  CheckDefExecFailure(['var x: any = "a"', 'echo x == true'], 'E1072: Cannot compare string with bool', 2)
+  CheckDefExecFailure(["var x: any = true", 'echo x == ""'], 'E1072: Cannot compare bool with string', 2)
+  CheckDefExecFailure(["var x: any = 99", 'echo x == true'], 'E1138', 2)
+  CheckDefExecFailure(["var x: any = 'a'", 'echo x == 99'], 'E1030:', 2)
+
+  for op in ['>', '>=', '<', '<=', '=~', '!~']
+    CheckDefExecFailure([
+        "var a: any = 'a'",
+        'var b: any = true',
+        'echo a ' .. op .. ' b'], 'E1072:', 3)
+  endfor
 enddef
 
 " test != comperator
index e620bedd742aec814c54498255afd73f921ee79a..06276b473d96526e22155d07fd6156d36d88d2b5 100644 (file)
@@ -834,6 +834,30 @@ typval_compare(
            default:  break;  // avoid gcc warning
        }
     }
+    else if (in_vim9script() && (typ1->v_type == VAR_BOOL
+                                                || typ2->v_type == VAR_BOOL))
+    {
+       if (typ1->v_type != typ2->v_type)
+       {
+           semsg(_(e_cannot_compare_str_with_str),
+                      vartype_name(typ1->v_type), vartype_name(typ2->v_type));
+           clear_tv(typ1);
+           return FAIL;
+       }
+       n1 = typ1->vval.v_number;
+       n2 = typ2->vval.v_number;
+       switch (type)
+       {
+           case EXPR_IS:
+           case EXPR_EQUAL:    n1 = (n1 == n2); break;
+           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);
index 0b174f5319fb32324a216a2feb5f6db7e49723d2..2ef443b239b3eaccec346b3d1a047bcc74673d66 100644 (file)
@@ -750,6 +750,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    2320,
 /**/
     2319,
 /**/