To: vim_dev@googlegroups.com Subject: Patch 8.2.0529 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ Patch 8.2.0529 Problem: Vim9: function argument with default not checked. Solution: Check type of argument with default value. Files: src/vim9compile.c, src/userfunc.c, src/testdir/test_vim9_func.vim *** ../vim-8.2.0528/src/vim9compile.c 2020-04-07 22:05:04.433458738 +0200 --- src/vim9compile.c 2020-04-07 22:44:51.274310804 +0200 *************** *** 5548,5553 **** --- 5548,5554 ---- if (ufunc->uf_def_args.ga_len > 0) { int count = ufunc->uf_def_args.ga_len; + int first_def_arg = ufunc->uf_args.ga_len - count; int i; char_u *arg; int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0); *************** *** 5561,5571 **** goto erret; for (i = 0; i < count; ++i) { ufunc->uf_def_arg_idx[i] = instr->ga_len; arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i]; ! if (compile_expr1(&arg, &cctx) == FAIL ! || generate_STORE(&cctx, ISN_STORE, ! i - count - off, NULL) == FAIL) goto erret; } --- 5562,5591 ---- goto erret; for (i = 0; i < count; ++i) { + garray_T *stack = &cctx.ctx_type_stack; + type_T *val_type; + int arg_idx = first_def_arg + i; + ufunc->uf_def_arg_idx[i] = instr->ga_len; arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i]; ! if (compile_expr1(&arg, &cctx) == FAIL) ! goto erret; ! ! // If no type specified use the type of the default value. ! // Otherwise check that the default value type matches the ! // specified type. ! val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; ! if (ufunc->uf_arg_types[arg_idx] == &t_unknown) ! ufunc->uf_arg_types[arg_idx] = val_type; ! else if (check_type(ufunc->uf_arg_types[i], val_type, FALSE) ! == FAIL) ! { ! arg_type_mismatch(ufunc->uf_arg_types[arg_idx], val_type, ! arg_idx + 1); ! goto erret; ! } ! ! if (generate_STORE(&cctx, ISN_STORE, i - count - off, NULL) == FAIL) goto erret; } *** ../vim-8.2.0528/src/userfunc.c 2020-04-07 22:05:04.433458738 +0200 --- src/userfunc.c 2020-04-07 22:20:40.595304717 +0200 *************** *** 3045,3052 **** { p = ((char_u **)argtypes.ga_data)[i]; if (p == NULL) ! // todo: get type from default value ! type = &t_any; else type = parse_type(&p, &fp->uf_type_list); if (type == NULL) --- 3045,3052 ---- { p = ((char_u **)argtypes.ga_data)[i]; if (p == NULL) ! // will get the type from the default value ! type = &t_unknown; else type = parse_type(&p, &fp->uf_type_list); if (type == NULL) *** ../vim-8.2.0528/src/testdir/test_vim9_func.vim 2020-04-07 22:05:04.433458738 +0200 --- src/testdir/test_vim9_func.vim 2020-04-07 22:11:36.796655537 +0200 *************** *** 128,133 **** --- 128,134 ---- assert_equal('one,foo', MyDefVarargs('one')) assert_equal('one,two', MyDefVarargs('one', 'two')) assert_equal('one,two,three', MyDefVarargs('one', 'two', 'three')) + call CheckDefFailure(['MyDefVarargs("one", 22)'], 'E1013: argument 2: type mismatch, expected string but got number') enddef " Only varargs *** ../vim-8.2.0528/src/version.c 2020-04-07 22:05:04.433458738 +0200 --- src/version.c 2020-04-07 22:42:38.550839186 +0200 *************** *** 740,741 **** --- 740,743 ---- { /* Add new patch number below this line */ + /**/ + 529, /**/ -- Beer & pretzels can't be served at the same time in any bar or restaurant. [real standing law in North Dakota, United States of America] /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\ /// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\ \\\ an exciting new programming language -- http://www.Zimbu.org /// \\\ help me help AIDS victims -- http://ICCF-Holland.org ///