0
0
mirror of https://github.com/vim/vim.git synced 2025-09-23 03:43:49 -04:00

patch 9.0.1821: Vim9 constructors are always static

Problem:  Vim9 constructors are always static
Solution: make the "static" keyword an error

closes: #12945

Signed-off-by: Christian Brabandt <cb@256bit.org>
Co-authored-by: Gianmaria Bajo <mg1979.git@gmail.com>
This commit is contained in:
Gianmaria Bajo
2023-08-29 22:26:30 +02:00
committed by Christian Brabandt
parent 2ac708b548
commit 4b9777a1df
5 changed files with 52 additions and 16 deletions

View File

@@ -535,6 +535,34 @@ is_duplicate_method(garray_T *fgap, char_u *name)
return FALSE;
}
/*
* Returns TRUE if the constructor is valid.
*/
static int
is_valid_constructor(ufunc_T *uf, int is_abstract, int has_static)
{
// Constructors are not allowed in abstract classes.
if (is_abstract)
{
emsg(_(e_cannot_define_new_function_in_abstract_class));
return FALSE;
}
// A constructor is always static, no need to define it so.
if (has_static)
{
emsg(_(e_cannot_define_new_function_as_static));
return FALSE;
}
// A return type should not be specified for the new()
// constructor method.
if (uf->uf_ret_type->tt_type != VAR_VOID)
{
emsg(_(e_cannot_use_a_return_type_with_new));
return FALSE;
}
return TRUE;
}
/*
* Update the interface class lookup table for the member index on the
* interface to the member index in the class implementing the interface.
@@ -1188,25 +1216,13 @@ early_ret:
{
char_u *name = uf->uf_name;
int is_new = STRNCMP(name, "new", 3) == 0;
if (is_new && is_abstract)
if (is_new && !is_valid_constructor(uf, is_abstract, has_static))
{
emsg(_(e_cannot_define_new_function_in_abstract_class));
success = FALSE;
func_clear_free(uf, FALSE);
break;
}
if (is_new)
{
// A return type should not be specified for the new()
// constructor method.
if (uf->uf_ret_type->tt_type != VAR_VOID)
{
emsg(_(e_cannot_use_a_return_type_with_new));
success = FALSE;
func_clear_free(uf, FALSE);
break;
}
}
garray_T *fgap = has_static || is_new
? &classfunctions : &objmethods;
// Check the name isn't used already.