comparison src/testdir/test_vim9_assign.vim @ 26935:ccb9be1cdd71 v8.2.3996

patch 8.2.3996: Vim9: type checking lacks information about declared type Commit: https://github.com/vim/vim/commit/078a46161e8b1b30bf306d6c1f4f0af7c616a989 Author: Bram Moolenaar <Bram@vim.org> Date: Tue Jan 4 15:17:03 2022 +0000 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.
author Bram Moolenaar <Bram@vim.org>
date Tue, 04 Jan 2022 16:30:06 +0100
parents 4e77f9961650
children 11ee2667a09a
comparison
equal deleted inserted replaced
26934:2d3dd8065e25 26935:ccb9be1cdd71
637 lines =<< trim END 637 lines =<< trim END
638 vim9script 638 vim9script
639 extend(test_null_list(), ['x']) 639 extend(test_null_list(), ['x'])
640 END 640 END
641 CheckScriptFailure(lines, 'E1134:', 2) 641 CheckScriptFailure(lines, 'E1134:', 2)
642
643 # using global var has no declared type
644 g:myList = []
645 g:myList->extend([1])
646 g:myList->extend(['x'])
647 assert_equal([1, 'x'], g:myList)
648 unlet g:myList
649
650 # using declared list gives an error
651 lines =<< trim END
652 var l: list<number>
653 g:myList = l
654 g:myList->extend([1])
655 g:myList->extend(['x'])
656 END
657 CheckDefExecAndScriptFailure(lines, 'E1013: Argument 2: type mismatch, expected list<number> but got list<string>', 4)
658 unlet g:myList
642 enddef 659 enddef
643 660
644 def Test_extend_dict() 661 def Test_extend_dict()
645 var lines =<< trim END 662 var lines =<< trim END
646 vim9script 663 vim9script
960 var somedict = rand() > 0 ? {a: 1, b: 2} : {a: 'a', b: 'b'} 977 var somedict = rand() > 0 ? {a: 1, b: 2} : {a: 'a', b: 'b'}
961 978
962 # type is dict<any> even though initializer is dict<number> 979 # type is dict<any> even though initializer is dict<number>
963 var anyDict: dict<any> = {a: 0} 980 var anyDict: dict<any> = {a: 0}
964 assert_equal({a: 0, b: 'x'}, extend(anyDict, {b: 'x'})) 981 assert_equal({a: 0, b: 'x'}, extend(anyDict, {b: 'x'}))
982
983 # using global var, which has no declared type
984 g:myDict = {}
985 g:myDict->extend({a: 1})
986 g:myDict->extend({b: 'x'})
987 assert_equal({a: 1, b: 'x'}, g:myDict)
988 unlet g:myDict
989
990 # using list with declared type gives an error
991 lines =<< trim END
992 var d: dict<number>
993 g:myDict = d
994 g:myDict->extend({a: 1})
995 g:myDict->extend({b: 'x'})
996 END
997 CheckDefExecAndScriptFailure(lines, 'E1013: Argument 2: type mismatch, expected dict<number> but got dict<string>', 4)
998 unlet g:myDict
965 999
966 # assignment to script-local dict 1000 # assignment to script-local dict
967 lines =<< trim END 1001 lines =<< trim END
968 vim9script 1002 vim9script
969 var test: dict<any> = {} 1003 var test: dict<any> = {}