summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBram Moolenaar <Bram@vim.org>2022-03-15 20:21:33 +0000
committerBram Moolenaar <Bram@vim.org>2022-03-15 20:21:33 +0000
commit056678184f679c2989b73bd48eda112f3c79a62f (patch)
tree6c07b21e0c06adbf8672e3aa25ae912cc82fd2f1
parent139575de6653e7fd5807cb036dfb3684b815c519 (diff)
downloadvim-git-056678184f679c2989b73bd48eda112f3c79a62f.tar.gz
patch 8.2.4576: Vim9: error for comparing with null can be annoyingv8.2.4576
Problem: Vim9: error for comparing with null can be annoying. Solution: Allow comparing anything with null. (closes #9948)
-rw-r--r--src/testdir/test_vim9_expr.vim39
-rw-r--r--src/typval.c9
-rw-r--r--src/version.c2
-rw-r--r--src/vim9instr.c16
4 files changed, 36 insertions, 30 deletions
diff --git a/src/testdir/test_vim9_expr.vim b/src/testdir/test_vim9_expr.vim
index 0d1cbe05f..83cdab134 100644
--- a/src/testdir/test_vim9_expr.vim
+++ b/src/testdir/test_vim9_expr.vim
@@ -716,6 +716,35 @@ def Test_expr4_compare_null()
g:null_dict = test_null_dict()
g:not_null_list = []
var lines =<< trim END
+ assert_false(true == null)
+ assert_false(false == null)
+ assert_false(null == true)
+ assert_false(null == false)
+ assert_true(true != null)
+ assert_true(false != null)
+ assert_true(null != true)
+ assert_true(null != false)
+
+ assert_false(123 == null)
+ assert_false(0 == null)
+ assert_false(null == 123)
+ assert_false(null == 0)
+ assert_true(123 != null)
+ assert_true(0 != null)
+ assert_true(null != 123)
+ assert_true(null != 0)
+
+ if has('float')
+ assert_false(12.3 == null)
+ assert_false(0.0 == null)
+ assert_false(null == 12.3)
+ assert_false(null == 0.0)
+ assert_true(12.3 != null)
+ assert_true(0.0 != null)
+ assert_true(null != 12.3)
+ assert_true(null != 0.0)
+ endif
+
assert_true(test_null_blob() == v:null)
assert_true(null_blob == null)
assert_true(v:null == test_null_blob())
@@ -818,16 +847,6 @@ def Test_expr4_compare_null()
assert_equal(null_function, d.f)
END
v9.CheckDefAndScriptSuccess(lines)
-
- v9.CheckDefAndScriptFailure(['echo 123 == v:null'], 'E1072: Cannot compare number with special')
- v9.CheckDefAndScriptFailure(['echo v:null == 123'], 'E1072: Cannot compare special with number')
- v9.CheckDefAndScriptFailure(['echo 123 != v:null'], 'E1072: Cannot compare number with special')
- v9.CheckDefAndScriptFailure(['echo v:null != 123'], 'E1072: Cannot compare special with number')
- v9.CheckDefAndScriptFailure(['echo true == v:null'], 'E1072: Cannot compare bool with special')
- v9.CheckDefAndScriptFailure(['echo v:null == true'], 'E1072: Cannot compare special with bool')
- v9.CheckDefAndScriptFailure(['echo true != v:null'], 'E1072: Cannot compare bool with special')
- v9.CheckDefAndScriptFailure(['echo v:null != true'], 'E1072: Cannot compare special with bool')
- v9.CheckDefAndScriptFailure(['echo false == v:null'], 'E1072: Cannot compare bool with special')
enddef
def Test_expr4_compare_none()
diff --git a/src/typval.c b/src/typval.c
index e0511be85..d0c937e5d 100644
--- a/src/typval.c
+++ b/src/typval.c
@@ -1417,12 +1417,9 @@ typval_compare_null(typval_T *tv1, typval_T *tv2)
default: break;
}
}
- if (!in_vim9script())
- return FALSE; // backwards compatible
-
- semsg(_(e_cannot_compare_str_with_str),
- vartype_name(tv1->v_type), vartype_name(tv2->v_type));
- return MAYBE;
+ // although comparing null with number, float or bool is not very usefule
+ // we won't give an error
+ return FALSE;
}
/*
diff --git a/src/version.c b/src/version.c
index a68a6628f..c447509c8 100644
--- a/src/version.c
+++ b/src/version.c
@@ -751,6 +751,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
+ 4576,
+/**/
4575,
/**/
4574,
diff --git a/src/vim9instr.c b/src/vim9instr.c
index de144dcc1..48e7edfed 100644
--- a/src/vim9instr.c
+++ b/src/vim9instr.c
@@ -397,20 +397,8 @@ get_compare_isn(
vartype_name(vartype1), vartype_name(vartype2));
return ISN_DROP;
}
- switch (vartype1 == VAR_SPECIAL ? vartype2 : vartype1)
- {
- case VAR_BLOB: break;
- case VAR_CHANNEL: break;
- case VAR_DICT: break;
- case VAR_FUNC: break;
- case VAR_JOB: break;
- case VAR_LIST: break;
- case VAR_PARTIAL: break;
- case VAR_STRING: break;
- default: semsg(_(e_cannot_compare_str_with_str),
- vartype_name(vartype1), vartype_name(vartype2));
- return ISN_DROP;
- }
+ // although comparing null with number, float or bool is not useful, we
+ // allow it
isntype = ISN_COMPARENULL;
}