patch 8.2.3996: Vim9: type checking lacks information about declared type
Problem: Vim9: type checking for list and dict lacks information about
declared type.
Solution: Add dv_decl_type and lv_decl_type. Refactor the type stack to
store two types in each entry.
diff --git a/src/testdir/test_vim9_assign.vim b/src/testdir/test_vim9_assign.vim
index 223a6e4..c91cd65 100644
--- a/src/testdir/test_vim9_assign.vim
+++ b/src/testdir/test_vim9_assign.vim
@@ -639,6 +639,23 @@
extend(test_null_list(), ['x'])
END
CheckScriptFailure(lines, 'E1134:', 2)
+
+ # using global var has no declared type
+ g:myList = []
+ g:myList->extend([1])
+ g:myList->extend(['x'])
+ assert_equal([1, 'x'], g:myList)
+ unlet g:myList
+
+ # using declared list gives an error
+ lines =<< trim END
+ var l: list<number>
+ g:myList = l
+ g:myList->extend([1])
+ g:myList->extend(['x'])
+ END
+ CheckDefExecAndScriptFailure(lines, 'E1013: Argument 2: type mismatch, expected list<number> but got list<string>', 4)
+ unlet g:myList
enddef
def Test_extend_dict()
@@ -963,6 +980,23 @@
var anyDict: dict<any> = {a: 0}
assert_equal({a: 0, b: 'x'}, extend(anyDict, {b: 'x'}))
+ # using global var, which has no declared type
+ g:myDict = {}
+ g:myDict->extend({a: 1})
+ g:myDict->extend({b: 'x'})
+ assert_equal({a: 1, b: 'x'}, g:myDict)
+ unlet g:myDict
+
+ # using list with declared type gives an error
+ lines =<< trim END
+ var d: dict<number>
+ g:myDict = d
+ g:myDict->extend({a: 1})
+ g:myDict->extend({b: 'x'})
+ END
+ CheckDefExecAndScriptFailure(lines, 'E1013: Argument 2: type mismatch, expected dict<number> but got dict<string>', 4)
+ unlet g:myDict
+
# assignment to script-local dict
lines =<< trim END
vim9script
diff --git a/src/testdir/test_vim9_builtin.vim b/src/testdir/test_vim9_builtin.vim
index 7b83510..d957c15 100644
--- a/src/testdir/test_vim9_builtin.vim
+++ b/src/testdir/test_vim9_builtin.vim
@@ -982,26 +982,22 @@
END
CheckDefAndScriptSuccess(lines)
- # FIXME: this should not fail when compiled
lines =<< trim END
- vim9script
assert_equal([1, 2, "x"], extend([1, 2], ["x"]))
assert_equal([1, "b", 1], extend([1], ["b", 1]))
+
+ assert_equal({a: 1, b: "x"}, extend({a: 1}, {b: "x"}))
END
- CheckScriptSuccess(lines)
+ CheckDefAndScriptSuccess(lines)
CheckDefAndScriptFailure(['extend("a", 1)'], ['E1013: Argument 1: type mismatch, expected list<any> but got string', 'E712: Argument of extend() must be a List or Dictionary'])
- CheckDefAndScriptFailure(['extend([1, 2], 3)'], ['E1013: Argument 2: type mismatch, expected list<number> but got number', 'E712: Argument of extend() must be a List or Dictionary'])
+ CheckDefAndScriptFailure(['extend([1, 2], 3)'], ['E1013: Argument 2: type mismatch, expected list<any> but got number', 'E712: Argument of extend() must be a List or Dictionary'])
CheckDefAndScriptFailure(['var ll = [1, 2]', 'extend(ll, ["x"])'], ['E1013: Argument 2: type mismatch, expected list<number> but got list<string>', 'E1013: Argument 2: type mismatch, expected list<number> but got list<string>'])
CheckDefFailure(['extend([1, 2], [3], "x")'], 'E1013: Argument 3: type mismatch, expected number but got string')
- CheckDefFailure(['extend({a: 1}, 42)'], 'E1013: Argument 2: type mismatch, expected dict<number> but got number')
- CheckDefFailure(['extend({a: 1}, {b: "x"})'], 'E1013: Argument 2: type mismatch, expected dict<number> but got dict<string>')
+ CheckDefFailure(['extend({a: 1}, 42)'], 'E1013: Argument 2: type mismatch, expected dict<any> but got number')
CheckDefFailure(['extend({a: 1}, {b: 2}, 1)'], 'E1013: Argument 3: type mismatch, expected string but got number')
- CheckDefFailure(['extend([1], ["b"])'], 'E1013: Argument 2: type mismatch, expected list<number> but got list<string>')
- CheckDefExecFailure(['extend([1], ["b", 1])'], 'E1013: Argument 2: type mismatch, expected list<number> but got list<any>')
-
CheckScriptFailure(['vim9script', 'var l = [1]', 'extend(l, ["b", 1])'], 'E1013: Argument 2: type mismatch, expected list<number> but got list<any> in extend()')
enddef