To: vim_dev@googlegroups.com Subject: Patch 8.2.4390 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ Patch 8.2.4390 Problem: Vim9: list from declaration with inferred type does not set the type on the value. Solution: When inferring the type in a variable declaration also set the type of the list or dictionary. (closes #9705) Do not set the type when the member is "any". Files: src/vim9compile.c, src/testdir/test_vim9_assign.vim, src/testdir/test_vim9_builtin.vim, src/testdir/test_vim9_disassemble.vim *** ../vim-8.2.4389/src/vim9compile.c 2022-02-14 21:19:01.130589032 +0000 --- src/vim9compile.c 2022-02-15 15:15:52.193910550 +0000 *************** *** 2002,2007 **** --- 2002,2008 ---- int instr_count = -1; int save_lnum; int skip_store = FALSE; + type_T *inferred_type = NULL; if (var_start[0] == '_' && !eval_isnamec(var_start[1])) { *************** *** 2126,2132 **** --- 2127,2136 ---- else if (rhs_type == &t_unknown) lhs.lhs_lvar->lv_type = &t_any; else + { lhs.lhs_lvar->lv_type = rhs_type; + inferred_type = rhs_type; + } } } else if (*op == '=') *************** *** 2146,2152 **** cctx)) use_type = lhs.lhs_member_type; if (need_type_where(rhs_type, use_type, -1, where, ! cctx, FALSE, is_const) == FAIL) goto theend; } } --- 2150,2156 ---- cctx)) use_type = lhs.lhs_member_type; if (need_type_where(rhs_type, use_type, -1, where, ! cctx, FALSE, is_const) == FAIL) goto theend; } } *************** *** 2315,2324 **** --- 2319,2338 ---- if ((lhs.lhs_type->tt_type == VAR_DICT || lhs.lhs_type->tt_type == VAR_LIST) && lhs.lhs_type->tt_member != NULL + && lhs.lhs_type->tt_member != &t_any && lhs.lhs_type->tt_member != &t_unknown) // Set the type in the list or dict, so that it can be checked, // also in legacy script. generate_SETTYPE(cctx, lhs.lhs_type); + else if (inferred_type != NULL + && (inferred_type->tt_type == VAR_DICT + || inferred_type->tt_type == VAR_LIST) + && inferred_type->tt_member != NULL + && inferred_type->tt_member != &t_unknown + && inferred_type->tt_member != &t_any) + // Set the type in the list or dict, so that it can be checked, + // also in legacy script. + generate_SETTYPE(cctx, inferred_type); if (!skip_store && generate_store_lhs(cctx, &lhs, instr_count, is_decl) == FAIL) *** ../vim-8.2.4389/src/testdir/test_vim9_assign.vim 2022-02-14 21:19:01.134589027 +0000 --- src/testdir/test_vim9_assign.vim 2022-02-15 15:01:56.663599801 +0000 *************** *** 1885,1890 **** --- 1885,1903 ---- v9.CheckDefFailure(['const foo: number'], 'E1021:') enddef + def Test_var_declaration_inferred() + # check that type is set on the list so that extend() fails + var lines =<< trim END + vim9script + def GetList(): list + var l = [1, 2, 3] + return l + enddef + echo GetList()->extend(['x']) + END + v9.CheckScriptFailure(lines, 'E1013:', 6) + enddef + def Test_script_local_in_legacy() # OK to define script-local later but before compiling var lines =<< trim END *** ../vim-8.2.4389/src/testdir/test_vim9_builtin.vim 2022-02-12 19:52:22.024702251 +0000 --- src/testdir/test_vim9_builtin.vim 2022-02-15 15:33:10.516214732 +0000 *************** *** 1129,1134 **** --- 1129,1135 ---- def Test() var d: dict = {} d->extend({A: 10, Func: function('F', [])}) + d.Func() enddef Test() *** ../vim-8.2.4389/src/testdir/test_vim9_disassemble.vim 2022-02-12 19:52:22.028702244 +0000 --- src/testdir/test_vim9_disassemble.vim 2022-02-15 15:11:30.126406583 +0000 *************** *** 427,432 **** --- 427,433 ---- '\d PUSHS "dd"\_s*' .. '\d NEWDICT size 0\_s*' .. '\d NEWDICT size 1\_s*' .. + '\d SETTYPE dict>\_s*' .. '\d STORE $0\_s*' .. 'd.dd\[0\] = 0\_s*' .. '\d PUSHNR 0\_s*' .. *************** *** 457,463 **** '\d STORE $1\_s*' .. 'var l: list\_s*' .. '\d NEWLIST size 0\_s*' .. - '\d SETTYPE list\_s*' .. '\d STORE $2\_s*' .. '\[x, y; l\] = g:stringlist\_s*' .. '\d LOADG g:stringlist\_s*' .. --- 458,463 ---- *************** *** 470,476 **** '\d\+ CHECKTYPE string stack\[-1\] arg 2\_s*' .. '\d\+ STORE $1\_s*' .. '\d\+ SLICE 2\_s*' .. - '\d\+ SETTYPE list\_s*' .. '\d\+ STORE $2\_s*' .. '\d\+ RETURN void', res) --- 470,475 ---- *************** *** 615,627 **** lockvar d.a enddef ! def Test_disassemble_locl_local() var res = execute('disass s:LockLocal') assert_match('\d*_LockLocal\_s*' .. 'var d = {a: 1}\_s*' .. '\d PUSHS "a"\_s*' .. '\d PUSHNR 1\_s*' .. '\d NEWDICT size 1\_s*' .. '\d STORE $0\_s*' .. 'lockvar d.a\_s*' .. '\d LOAD $0\_s*' .. --- 614,627 ---- lockvar d.a enddef ! def Test_disassemble_lock_local() var res = execute('disass s:LockLocal') assert_match('\d*_LockLocal\_s*' .. 'var d = {a: 1}\_s*' .. '\d PUSHS "a"\_s*' .. '\d PUSHNR 1\_s*' .. '\d NEWDICT size 1\_s*' .. + '\d SETTYPE dict\_s*' .. '\d STORE $0\_s*' .. 'lockvar d.a\_s*' .. '\d LOAD $0\_s*' .. *************** *** 1626,1631 **** --- 1626,1632 ---- '\d PUSHNR 2\_s*' .. '\d PUSHNR 3\_s*' .. '\d NEWLIST size 3\_s*' .. + '\d SETTYPE list\_s*' .. '\d STORE $0\_s*' .. 'var res = l\[1]\_s*' .. '\d LOAD $0\_s*' .. *************** *** 1650,1662 **** '\d PUSHNR 2\_s*' .. '\d PUSHNR 3\_s*' .. '\d NEWLIST size 3\_s*' .. '\d STORE $0\_s*' .. 'var res = l\[1 : 8]\_s*' .. '\d LOAD $0\_s*' .. '\d PUSHNR 1\_s*' .. '\d PUSHNR 8\_s*' .. ! '\d LISTSLICE\_s*' .. ! '\d STORE $1\_s*', instr) assert_equal([2, 3], ListSlice()) enddef --- 1651,1665 ---- '\d PUSHNR 2\_s*' .. '\d PUSHNR 3\_s*' .. '\d NEWLIST size 3\_s*' .. + '\d SETTYPE list\_s*' .. '\d STORE $0\_s*' .. 'var res = l\[1 : 8]\_s*' .. '\d LOAD $0\_s*' .. '\d PUSHNR 1\_s*' .. '\d PUSHNR 8\_s*' .. ! '\d\+ LISTSLICE\_s*' .. ! '\d\+ SETTYPE list\_s*' .. ! '\d\+ STORE $1\_s*', instr) assert_equal([2, 3], ListSlice()) enddef *************** *** 1675,1680 **** --- 1678,1684 ---- '\d PUSHS "item"\_s*' .. '\d PUSHNR 1\_s*' .. '\d NEWDICT size 1\_s*' .. + '\d SETTYPE dict\_s*' .. '\d STORE $0\_s*' .. 'var res = d.item\_s*' .. '\d\+ LOAD $0\_s*' .. *************** *** 2541,2546 **** --- 2545,2551 ---- '\d PUSHS "func"\_s*' .. '\d PUSHFUNC "<80>R\d\+_Legacy"\_s*' .. '\d NEWDICT size 1\_s*' .. + '\d SETTYPE dict\_s*' .. '\d STORE $0\_s*' .. 'var v = d.func()\_s*' .. *** ../vim-8.2.4389/src/version.c 2022-02-15 13:40:13.508939677 +0000 --- src/version.c 2022-02-15 14:55:03.360670793 +0000 *************** *** 752,753 **** --- 752,755 ---- { /* Add new patch number below this line */ + /**/ + 4390, /**/ -- hundred-and-one symptoms of being an internet addict: 42. Your virtual girlfriend finds a new net sweetheart with a larger bandwidth. /// 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 ///