comparison src/vim9compile.c @ 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 7045e9308ca3
comparison
equal deleted inserted replaced
26934:2d3dd8065e25 26935:ccb9be1cdd71
1637 { 1637 {
1638 size_t varlen = lhs->lhs_varlen; 1638 size_t varlen = lhs->lhs_varlen;
1639 int c = var_start[varlen]; 1639 int c = var_start[varlen];
1640 int lines_len = cctx->ctx_ufunc->uf_lines.ga_len; 1640 int lines_len = cctx->ctx_ufunc->uf_lines.ga_len;
1641 char_u *p = var_start; 1641 char_u *p = var_start;
1642 garray_T *stack = &cctx->ctx_type_stack;
1643 int res; 1642 int res;
1644 1643
1645 // Evaluate "ll[expr]" of "ll[expr][idx]". End the line with a NUL and 1644 // Evaluate "ll[expr]" of "ll[expr][idx]". End the line with a NUL and
1646 // limit the lines array length to avoid skipping to a following line. 1645 // limit the lines array length to avoid skipping to a following line.
1647 var_start[varlen] = NUL; 1646 var_start[varlen] = NUL;
1655 if (res != FAIL) 1654 if (res != FAIL)
1656 emsg(_(e_missing_closing_square_brace)); 1655 emsg(_(e_missing_closing_square_brace));
1657 return FAIL; 1656 return FAIL;
1658 } 1657 }
1659 1658
1660 lhs->lhs_type = stack->ga_len == 0 ? &t_void 1659 lhs->lhs_type = cctx->ctx_type_stack.ga_len == 0 ? &t_void
1661 : ((type_T **)stack->ga_data)[stack->ga_len - 1]; 1660 : get_type_on_stack(cctx, 0);
1662 // now we can properly check the type 1661 // now we can properly check the type
1663 if (rhs_type != NULL && lhs->lhs_type->tt_member != NULL 1662 if (rhs_type != NULL && lhs->lhs_type->tt_member != NULL
1664 && rhs_type != &t_void 1663 && rhs_type != &t_void
1665 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx, 1664 && need_type(rhs_type, lhs->lhs_type->tt_member, -2, 0, cctx,
1666 FALSE, FALSE) == FAIL) 1665 FALSE, FALSE) == FAIL)
1715 int is_assign, 1714 int is_assign,
1716 type_T *rhs_type, 1715 type_T *rhs_type,
1717 cctx_T *cctx) 1716 cctx_T *cctx)
1718 { 1717 {
1719 vartype_T dest_type; 1718 vartype_T dest_type;
1720 garray_T *stack = &cctx->ctx_type_stack;
1721 int range = FALSE; 1719 int range = FALSE;
1722 1720
1723 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL) 1721 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
1724 return FAIL; 1722 return FAIL;
1725 if (is_assign && range 1723 if (is_assign && range
1751 { 1749 {
1752 type_T *type; 1750 type_T *type;
1753 1751
1754 if (range) 1752 if (range)
1755 { 1753 {
1756 type = ((type_T **)stack->ga_data)[stack->ga_len - 2]; 1754 type = get_type_on_stack(cctx, 1);
1757 if (need_type(type, &t_number, 1755 if (need_type(type, &t_number,
1758 -1, 0, cctx, FALSE, FALSE) == FAIL) 1756 -1, 0, cctx, FALSE, FALSE) == FAIL)
1759 return FAIL; 1757 return FAIL;
1760 } 1758 }
1761 type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; 1759 type = get_type_on_stack(cctx, 0);
1762 if ((dest_type != VAR_BLOB && type != &t_special) 1760 if ((dest_type != VAR_BLOB && type != &t_special)
1763 && need_type(type, &t_number, 1761 && need_type(type, &t_number,
1764 -1, 0, cctx, FALSE, FALSE) == FAIL) 1762 -1, 0, cctx, FALSE, FALSE) == FAIL)
1765 return FAIL; 1763 return FAIL;
1766 } 1764 }
1835 int var_count = 0; 1833 int var_count = 0;
1836 int var_idx; 1834 int var_idx;
1837 int semicolon = 0; 1835 int semicolon = 0;
1838 int did_generate_slice = FALSE; 1836 int did_generate_slice = FALSE;
1839 garray_T *instr = &cctx->ctx_instr; 1837 garray_T *instr = &cctx->ctx_instr;
1840 garray_T *stack = &cctx->ctx_type_stack;
1841 char_u *op; 1838 char_u *op;
1842 int oplen = 0; 1839 int oplen = 0;
1843 int heredoc = FALSE; 1840 int heredoc = FALSE;
1844 int incdec = FALSE; 1841 int incdec = FALSE;
1845 type_T *rhs_type = &t_any; 1842 type_T *rhs_type = &t_any;
1927 { 1924 {
1928 type_T *stacktype; 1925 type_T *stacktype;
1929 int needed_list_len; 1926 int needed_list_len;
1930 int did_check = FALSE; 1927 int did_check = FALSE;
1931 1928
1932 stacktype = stack->ga_len == 0 ? &t_void 1929 stacktype = cctx->ctx_type_stack.ga_len == 0 ? &t_void
1933 : ((type_T **)stack->ga_data)[stack->ga_len - 1]; 1930 : get_type_on_stack(cctx, 0);
1934 if (stacktype->tt_type == VAR_VOID) 1931 if (stacktype->tt_type == VAR_VOID)
1935 { 1932 {
1936 emsg(_(e_cannot_use_void_value)); 1933 emsg(_(e_cannot_use_void_value));
1937 goto theend; 1934 goto theend;
1938 } 1935 }
2071 // list. 2068 // list.
2072 if (generate_GETITEM(cctx, var_idx, *op != '=') == FAIL) 2069 if (generate_GETITEM(cctx, var_idx, *op != '=') == FAIL)
2073 goto theend; 2070 goto theend;
2074 } 2071 }
2075 2072
2076 rhs_type = stack->ga_len == 0 ? &t_void 2073 rhs_type = cctx->ctx_type_stack.ga_len == 0 ? &t_void
2077 : ((type_T **)stack->ga_data)[stack->ga_len - 1]; 2074 : get_type_on_stack(cctx, 0);
2078 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type)) 2075 if (lhs.lhs_lvar != NULL && (is_decl || !lhs.lhs_has_type))
2079 { 2076 {
2080 if ((rhs_type->tt_type == VAR_FUNC 2077 if ((rhs_type->tt_type == VAR_FUNC
2081 || rhs_type->tt_type == VAR_PARTIAL) 2078 || rhs_type->tt_type == VAR_PARTIAL)
2082 && !lhs.lhs_has_index 2079 && !lhs.lhs_has_index
2228 goto theend; 2225 goto theend;
2229 } 2226 }
2230 else 2227 else
2231 { 2228 {
2232 expected = lhs.lhs_member_type; 2229 expected = lhs.lhs_member_type;
2233 stacktype = ((type_T **)stack->ga_data)[stack->ga_len - 1]; 2230 stacktype = get_type_on_stack(cctx, 0);
2234 if ( 2231 if (
2235 #ifdef FEAT_FLOAT 2232 #ifdef FEAT_FLOAT
2236 // If variable is float operation with number is OK. 2233 // If variable is float operation with number is OK.
2237 !(expected == &t_float && (stacktype == &t_number 2234 !(expected == &t_float && (stacktype == &t_number
2238 || stacktype == &t_number_bool)) && 2235 || stacktype == &t_number_bool)) &&
2525 cctx.ctx_compile_type = compile_type; 2522 cctx.ctx_compile_type = compile_type;
2526 cctx.ctx_ufunc = ufunc; 2523 cctx.ctx_ufunc = ufunc;
2527 cctx.ctx_lnum = -1; 2524 cctx.ctx_lnum = -1;
2528 cctx.ctx_outer = outer_cctx; 2525 cctx.ctx_outer = outer_cctx;
2529 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10); 2526 ga_init2(&cctx.ctx_locals, sizeof(lvar_T), 10);
2530 ga_init2(&cctx.ctx_type_stack, sizeof(type_T *), 50); 2527 // Each entry on the type stack consists of two type pointers.
2528 ga_init2(&cctx.ctx_type_stack, sizeof(type2_T), 50);
2531 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10); 2529 ga_init2(&cctx.ctx_imports, sizeof(imported_T), 10);
2532 cctx.ctx_type_list = &ufunc->uf_type_list; 2530 cctx.ctx_type_list = &ufunc->uf_type_list;
2533 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50); 2531 ga_init2(&cctx.ctx_instr, sizeof(isn_T), 50);
2534 instr = &cctx.ctx_instr; 2532 instr = &cctx.ctx_instr;
2535 2533
2562 2560
2563 // Produce instructions for the default values of optional arguments. 2561 // Produce instructions for the default values of optional arguments.
2564 SOURCING_LNUM = 0; // line number unknown 2562 SOURCING_LNUM = 0; // line number unknown
2565 for (i = 0; i < count; ++i) 2563 for (i = 0; i < count; ++i)
2566 { 2564 {
2567 garray_T *stack = &cctx.ctx_type_stack;
2568 type_T *val_type; 2565 type_T *val_type;
2569 int arg_idx = first_def_arg + i; 2566 int arg_idx = first_def_arg + i;
2570 where_T where = WHERE_INIT; 2567 where_T where = WHERE_INIT;
2571 int r; 2568 int r;
2572 int jump_instr_idx = instr->ga_len; 2569 int jump_instr_idx = instr->ga_len;
2586 goto erret; 2583 goto erret;
2587 2584
2588 // If no type specified use the type of the default value. 2585 // If no type specified use the type of the default value.
2589 // Otherwise check that the default value type matches the 2586 // Otherwise check that the default value type matches the
2590 // specified type. 2587 // specified type.
2591 val_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; 2588 val_type = get_type_on_stack(&cctx, 0);
2592 where.wt_index = arg_idx + 1; 2589 where.wt_index = arg_idx + 1;
2593 if (ufunc->uf_arg_types[arg_idx] == &t_unknown) 2590 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
2594 { 2591 {
2595 did_set_arg_type = TRUE; 2592 did_set_arg_type = TRUE;
2596 ufunc->uf_arg_types[arg_idx] = val_type; 2593 ufunc->uf_arg_types[arg_idx] = val_type;