patch 8.2.4873: Vim9: using "else" differs from using "endif/if !cond"
Problem: Vim9: using "else" differs from using "endif/if !cond".
Solution: Leave the block and enter another one. (closes #10320)
diff --git a/src/ex_eval.c b/src/ex_eval.c
index e9dbd10..7b4ac17 100644
--- a/src/ex_eval.c
+++ b/src/ex_eval.c
@@ -1124,6 +1124,11 @@
skip = TRUE;
}
+ // Variables declared in the previous block can no longer be
+ // used. Needs to be done before setting "cs_flags".
+ leave_block(cstack);
+ enter_block(cstack);
+
// if skipping or the ":if" was TRUE, reset ACTIVE, otherwise set it
if (skip || cstack->cs_flags[cstack->cs_idx] & CSF_TRUE)
{
diff --git a/src/testdir/test_vim9_script.vim b/src/testdir/test_vim9_script.vim
index 4fa6904..8811382 100644
--- a/src/testdir/test_vim9_script.vim
+++ b/src/testdir/test_vim9_script.vim
@@ -1640,6 +1640,50 @@
v9.CheckDefAndScriptFailure(lines, ['E1143:', 'E15:'], 4)
enddef
+def Test_if_else_func_using_var()
+ var lines =<< trim END
+ vim9script
+
+ const debug = true
+ if debug
+ var mode_chars = 'something'
+ def Bits2Ascii()
+ var x = mode_chars
+ g:where = 'in true'
+ enddef
+ else
+ def Bits2Ascii()
+ g:where = 'in false'
+ enddef
+ endif
+
+ Bits2Ascii()
+ END
+ v9.CheckScriptSuccess(lines)
+ assert_equal('in true', g:where)
+ unlet g:where
+
+ lines =<< trim END
+ vim9script
+
+ const debug = false
+ if debug
+ var mode_chars = 'something'
+ def Bits2Ascii()
+ g:where = 'in true'
+ enddef
+ else
+ def Bits2Ascii()
+ var x = mode_chars
+ g:where = 'in false'
+ enddef
+ endif
+
+ Bits2Ascii()
+ END
+ v9.CheckScriptFailure(lines, 'E1001: Variable not found: mode_chars')
+enddef
+
let g:bool_true = v:true
let g:bool_false = v:false
diff --git a/src/version.c b/src/version.c
index 5ad7551..3242c14 100644
--- a/src/version.c
+++ b/src/version.c
@@ -747,6 +747,8 @@
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
+ 4873,
+/**/
4872,
/**/
4871,