Skip to content

Commit 8e02faf

Browse files
committed
patch 8.2.2010: Vim9: compiling fails for unreachable return statement
Problem: Vim9: compiling fails for unreachable return statement. Solution: Fix it. (closes #7319)
1 parent 3823192 commit 8e02faf

3 files changed

Lines changed: 24 additions & 14 deletions

File tree

src/testdir/test_vim9_disassemble.vim

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,9 @@ def Test_disassemble_const_expr()
749749
enddef
750750

751751
def ReturnInIf(): string
752+
if 1 < 0
753+
return "maybe"
754+
endif
752755
if g:cond
753756
return "yes"
754757
else
@@ -759,6 +762,9 @@ enddef
759762
def Test_disassemble_return_in_if()
760763
var instr = execute('disassemble ReturnInIf')
761764
assert_match('ReturnInIf\_s*' ..
765+
'if 1 < 0\_s*' ..
766+
' return "maybe"\_s*' ..
767+
'endif\_s*' ..
762768
'if g:cond\_s*' ..
763769
'0 LOADG g:cond\_s*' ..
764770
'1 COND2BOOL\_s*' ..

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,8 @@ static char *(features[]) =
750750

751751
static int included_patches[] =
752752
{ /* Add new patch number below this line */
753+
/**/
754+
2010,
753755
/**/
754756
2009,
755757
/**/

src/vim9compile.c

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4694,21 +4694,24 @@ compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
46944694
if (compile_expr0(&p, cctx) == FAIL)
46954695
return NULL;
46964696

4697-
stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4698-
if (set_return_type)
4699-
cctx->ctx_ufunc->uf_ret_type = stack_type;
4700-
else
4697+
if (cctx->ctx_skip != SKIP_YES)
47014698
{
4702-
if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4703-
&& stack_type->tt_type != VAR_VOID
4704-
&& stack_type->tt_type != VAR_UNKNOWN)
4699+
stack_type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
4700+
if (set_return_type)
4701+
cctx->ctx_ufunc->uf_ret_type = stack_type;
4702+
else
47054703
{
4706-
emsg(_(e_returning_value_in_function_without_return_type));
4707-
return NULL;
4708-
}
4709-
if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
4704+
if (cctx->ctx_ufunc->uf_ret_type->tt_type == VAR_VOID
4705+
&& stack_type->tt_type != VAR_VOID
4706+
&& stack_type->tt_type != VAR_UNKNOWN)
4707+
{
4708+
emsg(_(e_returning_value_in_function_without_return_type));
4709+
return NULL;
4710+
}
4711+
if (need_type(stack_type, cctx->ctx_ufunc->uf_ret_type, -1,
47104712
cctx, FALSE, FALSE) == FAIL)
4711-
return NULL;
4713+
return NULL;
4714+
}
47124715
}
47134716
}
47144717
else
@@ -4725,8 +4728,7 @@ compile_return(char_u *arg, int set_return_type, cctx_T *cctx)
47254728
// No argument, return zero.
47264729
generate_PUSHNR(cctx, 0);
47274730
}
4728-
4729-
if (generate_instr(cctx, ISN_RETURN) == NULL)
4731+
if (cctx->ctx_skip != SKIP_YES && generate_instr(cctx, ISN_RETURN) == NULL)
47304732
return NULL;
47314733

47324734
// "return val | endif" is possible

0 commit comments

Comments
 (0)