comparison src/vim9compile.c @ 23332:cdb706d5c43d v8.2.2209

patch 8.2.2209: Vim9: return type of => lambda not parsed Commit: https://github.com/vim/vim/commit/9e68c32563d8c9ffe1ac04ecd4ccd730af66b97c Author: Bram Moolenaar <Bram@vim.org> Date: Fri Dec 25 12:38:04 2020 +0100 patch 8.2.2209: Vim9: return type of => lambda not parsed Problem: Vim9: return type of => lambda not parsed. Solution: Parse and use the return type.
author Bram Moolenaar <Bram@vim.org>
date Fri, 25 Dec 2020 12:45:05 +0100
parents e8eb4fd44902
children 4b4f695e9cd1
comparison
equal deleted inserted replaced
23331:c5b1246b9557 23332:cdb706d5c43d
4116 type_T *want_type = NULL; 4116 type_T *want_type = NULL;
4117 4117
4118 // Recognize <type> 4118 // Recognize <type>
4119 if (**arg == '<' && eval_isnamec1((*arg)[1])) 4119 if (**arg == '<' && eval_isnamec1((*arg)[1]))
4120 { 4120 {
4121 int called_emsg_before = called_emsg;
4122
4123 ++*arg; 4121 ++*arg;
4124 want_type = parse_type(arg, cctx->ctx_type_list); 4122 want_type = parse_type(arg, cctx->ctx_type_list, TRUE);
4125 if (called_emsg != called_emsg_before) 4123 if (want_type == NULL)
4126 return FAIL; 4124 return FAIL;
4127 4125
4128 if (**arg != '>') 4126 if (**arg != '>')
4129 { 4127 {
4130 if (*skipwhite(*arg) == '>') 4128 if (*skipwhite(*arg) == '>')
4807 4805
4808 /* 4806 /*
4809 * compile "return [expr]" 4807 * compile "return [expr]"
4810 */ 4808 */
4811 static char_u * 4809 static char_u *
4812 compile_return(char_u *arg, int set_return_type, cctx_T *cctx) 4810 compile_return(char_u *arg, int check_return_type, cctx_T *cctx)
4813 { 4811 {
4814 char_u *p = arg; 4812 char_u *p = arg;
4815 garray_T *stack = &cctx->ctx_type_stack; 4813 garray_T *stack = &cctx->ctx_type_stack;
4816 type_T *stack_type; 4814 type_T *stack_type;
4817 4815
4822 return NULL; 4820 return NULL;
4823 4821
4824 if (cctx->ctx_skip != SKIP_YES) 4822 if (cctx->ctx_skip != SKIP_YES)
4825 { 4823 {
4826 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1]; 4824 stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4827 if (set_return_type) 4825 if (check_return_type && cctx->ctx_ufunc->uf_ret_type == NULL)
4826 {
4828 cctx->ctx_ufunc->uf_ret_type = stack_type; 4827 cctx->ctx_ufunc->uf_ret_type = stack_type;
4828 }
4829 else 4829 else
4830 { 4830 {
4831 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID 4831 if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4832 && stack_type->tt_type != VAR_VOID 4832 && stack_type->tt_type != VAR_VOID
4833 && stack_type->tt_type != VAR_UNKNOWN) 4833 && stack_type->tt_type != VAR_UNKNOWN)
4841 } 4841 }
4842 } 4842 }
4843 } 4843 }
4844 else 4844 else
4845 { 4845 {
4846 // "set_return_type" cannot be TRUE, only used for a lambda which 4846 // "check_return_type" cannot be TRUE, only used for a lambda which
4847 // always has an argument. 4847 // always has an argument.
4848 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID 4848 if (cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_VOID
4849 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN) 4849 && cctx->ctx_ufunc->uf_ret_type->tt_type != VAR_UNKNOWN)
4850 { 4850 {
4851 emsg(_(e_missing_return_value)); 4851 emsg(_(e_missing_return_value));
5634 { 5634 {
5635 semsg(_(e_white_space_required_after_str), ":"); 5635 semsg(_(e_white_space_required_after_str), ":");
5636 goto theend; 5636 goto theend;
5637 } 5637 }
5638 p = skipwhite(var_end + 1); 5638 p = skipwhite(var_end + 1);
5639 type = parse_type(&p, cctx->ctx_type_list); 5639 type = parse_type(&p, cctx->ctx_type_list, TRUE);
5640 if (type == NULL)
5641 goto theend;
5640 has_type = TRUE; 5642 has_type = TRUE;
5641 } 5643 }
5642 else if (lvar != NULL) 5644 else if (lvar != NULL)
5643 type = lvar->lv_type; 5645 type = lvar->lv_type;
5644 } 5646 }
7415 7417
7416 /* 7418 /*
7417 * After ex_function() has collected all the function lines: parse and compile 7419 * After ex_function() has collected all the function lines: parse and compile
7418 * the lines into instructions. 7420 * the lines into instructions.
7419 * Adds the function to "def_functions". 7421 * Adds the function to "def_functions".
7420 * When "set_return_type" is set then set ufunc->uf_ret_type to the type of the 7422 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
7421 * return statement (used for lambda). 7423 * the return statement (used for lambda). When uf_ret_type is already set
7424 * then check that it matches.
7422 * "outer_cctx" is set for a nested function. 7425 * "outer_cctx" is set for a nested function.
7423 * This can be used recursively through compile_lambda(), which may reallocate 7426 * This can be used recursively through compile_lambda(), which may reallocate
7424 * "def_functions". 7427 * "def_functions".
7425 * Returns OK or FAIL. 7428 * Returns OK or FAIL.
7426 */ 7429 */
7427 int 7430 int
7428 compile_def_function(ufunc_T *ufunc, int set_return_type, cctx_T *outer_cctx) 7431 compile_def_function(ufunc_T *ufunc, int check_return_type, cctx_T *outer_cctx)
7429 { 7432 {
7430 char_u *line = NULL; 7433 char_u *line = NULL;
7431 char_u *p; 7434 char_u *p;
7432 char *errormsg = NULL; // error message 7435 char *errormsg = NULL; // error message
7433 cctx_T cctx; 7436 cctx_T cctx;
7795 // function? 7798 // function?
7796 emsg(_(e_cannot_use_function_inside_def)); 7799 emsg(_(e_cannot_use_function_inside_def));
7797 goto erret; 7800 goto erret;
7798 7801
7799 case CMD_return: 7802 case CMD_return:
7800 line = compile_return(p, set_return_type, &cctx); 7803 line = compile_return(p, check_return_type, &cctx);
7801 cctx.ctx_had_return = TRUE; 7804 cctx.ctx_had_return = TRUE;
7802 break; 7805 break;
7803 7806
7804 case CMD_let: 7807 case CMD_let:
7805 emsg(_(e_cannot_use_let_in_vim9_script)); 7808 emsg(_(e_cannot_use_let_in_vim9_script));