0
0
mirror of https://github.com/vim/vim.git synced 2025-09-25 03:54:15 -04:00

patch 8.2.2264: Vim9: no error for mismatched :endfunc or :enddef

Problem:    Vim9: no error for mismatched :endfunc or :enddef.
Solution:   Check for the mismatch. (issue #7582)
This commit is contained in:
Bram Moolenaar
2021-01-01 18:43:51 +01:00
parent 69f7050ceb
commit 5178b1b02f
4 changed files with 63 additions and 22 deletions

View File

@@ -335,3 +335,7 @@ EXTERN char e_script_variable_invalid_after_reload_in_function_str[]
INIT(= N_("E1149: Script variable is invalid after reload in function %s")); INIT(= N_("E1149: Script variable is invalid after reload in function %s"));
EXTERN char e_script_variable_type_changed[] EXTERN char e_script_variable_type_changed[]
INIT(= N_("E1150: Script variable type changed")); INIT(= N_("E1150: Script variable type changed"));
EXTERN char e_mismatched_endfunction[]
INIT(= N_("E1151: Mismatched endfunction"));
EXTERN char e_mismatched_enddef[]
INIT(= N_("E1152: Mismatched enddef"));

View File

@@ -79,6 +79,25 @@ def Test_funcdepth_error()
set maxfuncdepth& set maxfuncdepth&
enddef enddef
def Test_endfunc_enddef()
var lines =<< trim END
def Test()
echo 'test'
endfunc
enddef
END
CheckScriptFailure(lines, 'E1151:', 3)
lines =<< trim END
def Test()
func Nested()
echo 'test'
enddef
enddef
END
CheckScriptFailure(lines, 'E1152:', 4)
enddef
def ReturnString(): string def ReturnString(): string
return 'string' return 'string'
enddef enddef

View File

@@ -3404,7 +3404,9 @@ define_function(exarg_T *eap, char_u *name_arg)
// Check for "endfunction" or "enddef". // Check for "endfunction" or "enddef".
if (checkforcmd(&p, nesting_def[nesting] if (checkforcmd(&p, nesting_def[nesting]
? "enddef" : "endfunction", 4) && nesting-- == 0) ? "enddef" : "endfunction", 4))
{
if (nesting-- == 0)
{ {
char_u *nextcmd = NULL; char_u *nextcmd = NULL;
@@ -3419,9 +3421,9 @@ define_function(exarg_T *eap, char_u *name_arg)
p, TRUE); p, TRUE);
if (nextcmd != NULL) if (nextcmd != NULL)
{ {
// Another command follows. If the line came from "eap" we // Another command follows. If the line came from "eap"
// can simply point into it, otherwise we need to change // we can simply point into it, otherwise we need to
// "eap->cmdlinep". // change "eap->cmdlinep".
eap->nextcmd = nextcmd; eap->nextcmd = nextcmd;
if (line_to_free != NULL) if (line_to_free != NULL)
{ {
@@ -3432,6 +3434,20 @@ define_function(exarg_T *eap, char_u *name_arg)
} }
break; break;
} }
}
// Check for mismatched "endfunc" or "enddef".
// We don't check for "def" inside "func" thus we also can't check
// for "enddef".
// We continue to find the end of the function, although we might
// not find it.
else if (nesting_def[nesting])
{
if (checkforcmd(&p, "endfunction", 4))
emsg(_(e_mismatched_endfunction));
}
else if (eap->cmdidx == CMD_def && checkforcmd(&p, "enddef", 4))
emsg(_(e_mismatched_enddef));
// Increase indent inside "if", "while", "for" and "try", decrease // Increase indent inside "if", "while", "for" and "try", decrease
// at "end". // at "end".

View File

@@ -750,6 +750,8 @@ static char *(features[]) =
static int included_patches[] = static int included_patches[] =
{ /* Add new patch number below this line */ { /* Add new patch number below this line */
/**/
2264,
/**/ /**/
2263, 2263,
/**/ /**/