forked from aniani/vim
patch 9.1.1037: Vim9: confusing error when using abstract method via super
Problem: Vim9: confusing error when using abstract method via super Solution: Display an error when an abstract method is invoked using super (Ernie Rael) fixes: #15514 closes: #16478 Signed-off-by: Ernie Rael <errael@raelity.com> Signed-off-by: Aliaksei Budavei <0x000c70@gmail.com> Signed-off-by: Yegappan Lakshmanan <yegappan@yahoo.com> Signed-off-by: Christian Brabandt <cb@256bit.org>
This commit is contained in:
committed by
Christian Brabandt
parent
5abc44e3c1
commit
bce60c4742
@@ -3619,8 +3619,10 @@ EXTERN char e_class_can_only_be_used_in_script[]
|
||||
INIT(= N_("E1429: Class can only be used in a script"));
|
||||
EXTERN char e_uninitialized_object_var_reference[]
|
||||
INIT(= N_("E1430: Uninitialized object variable '%s' referenced"));
|
||||
EXTERN char e_abstract_method_str_direct[]
|
||||
INIT(= N_("E1431: Abstract method \"%s\" in class \"%s\" cannot be accessed directly"));
|
||||
#endif
|
||||
// E1431 - E1499 unused (reserved for Vim9 class support)
|
||||
// E1432 - E1499 unused (reserved for Vim9 class support)
|
||||
EXTERN char e_cannot_mix_positional_and_non_positional_str[]
|
||||
INIT(= N_("E1500: Cannot mix positional and non-positional arguments: %s"));
|
||||
EXTERN char e_fmt_arg_nr_unused_str[]
|
||||
|
@@ -3268,6 +3268,7 @@ def Test_using_base_class()
|
||||
v9.CheckSourceSuccess(lines)
|
||||
enddef
|
||||
|
||||
" Test for using a method from the super class
|
||||
def Test_super_dispatch()
|
||||
# See #15448 and #15463
|
||||
var lines =<< trim END
|
||||
@@ -3401,6 +3402,160 @@ def Test_super_dispatch()
|
||||
assert_equal(['E.SF()', 'D.SF()', 'B.SF()', 'A._H()', 'A.F()', 'F._G()'], call_chain)
|
||||
END
|
||||
v9.CheckSourceSuccess(lines)
|
||||
|
||||
# problems with method dispatch: super -> abstract
|
||||
# https://github.com/vim/vim/issues/15514
|
||||
lines =<< trim END
|
||||
vim9script
|
||||
abstract class B
|
||||
abstract def ToString(): string
|
||||
endclass
|
||||
|
||||
class C extends B
|
||||
def ToString(): string
|
||||
return super.ToString()
|
||||
enddef
|
||||
endclass
|
||||
|
||||
try
|
||||
defcompile C.ToString
|
||||
call assert_false(1, 'command should have failed')
|
||||
catch
|
||||
call assert_exception('E1431: Abstract method "ToString" in class "B" cannot be accessed directly')
|
||||
endtry
|
||||
END
|
||||
v9.CheckSourceSuccess(lines)
|
||||
|
||||
# problems with method dispatch: super -> abstract -> concrete
|
||||
lines =<< trim END
|
||||
vim9script
|
||||
|
||||
class A
|
||||
def ToString()
|
||||
echo 'A'
|
||||
enddef
|
||||
endclass
|
||||
|
||||
abstract class B extends A
|
||||
abstract def ToString()
|
||||
endclass
|
||||
|
||||
class C extends B
|
||||
def ToString()
|
||||
super.ToString()
|
||||
enddef
|
||||
endclass
|
||||
|
||||
try
|
||||
defcompile C.ToString
|
||||
call assert_false(1, 'command should have failed')
|
||||
catch
|
||||
call assert_exception('E1431: Abstract method "ToString" in class "B" cannot be accessed directly')
|
||||
endtry
|
||||
END
|
||||
v9.CheckSourceSuccess(lines)
|
||||
|
||||
# Invoking a super method and an interface method which have the same name.
|
||||
lines =<< trim END
|
||||
vim9script
|
||||
|
||||
interface I
|
||||
def ToString(): string
|
||||
endinterface
|
||||
|
||||
# Note that A does not implement I.
|
||||
class A
|
||||
def ToString(): string
|
||||
return 'A'
|
||||
enddef
|
||||
endclass
|
||||
|
||||
class B extends A implements I
|
||||
def ToString(): string
|
||||
return super.ToString()
|
||||
enddef
|
||||
endclass
|
||||
|
||||
def TestI(i: I): string
|
||||
return i.ToString()
|
||||
enddef
|
||||
|
||||
assert_equal('A', B.new().ToString())
|
||||
assert_equal('A', TestI(B.new()))
|
||||
END
|
||||
v9.CheckSourceSuccess(lines)
|
||||
|
||||
# super and an abstract class with no abstract methods
|
||||
lines =<< trim END
|
||||
vim9script
|
||||
|
||||
class A
|
||||
def ToString(): string
|
||||
return 'A'
|
||||
enddef
|
||||
endclass
|
||||
|
||||
# An abstract class with no abstract methods.
|
||||
abstract class B extends A
|
||||
endclass
|
||||
|
||||
class C extends B
|
||||
def ToString(): string
|
||||
return super.ToString()
|
||||
enddef
|
||||
endclass
|
||||
|
||||
def TestA(a: A): string
|
||||
return a.ToString()
|
||||
enddef
|
||||
|
||||
def TestB(b: B): string
|
||||
return b.ToString()
|
||||
enddef
|
||||
|
||||
assert_equal('A', C.new().ToString())
|
||||
assert_equal('A', TestA(A.new()))
|
||||
assert_equal('A', TestA(C.new()))
|
||||
assert_equal('A', TestB(C.new()))
|
||||
END
|
||||
v9.CheckSourceSuccess(lines)
|
||||
|
||||
# super and an abstract class with no abstract methods and the initial
|
||||
# implements clause
|
||||
lines =<< trim END
|
||||
vim9script
|
||||
|
||||
interface I
|
||||
def ToString(): string
|
||||
endinterface
|
||||
|
||||
# Note that A does not implement I.
|
||||
class A
|
||||
def ToString(): string
|
||||
return 'A'
|
||||
enddef
|
||||
endclass
|
||||
|
||||
# An abstract class with no abstract methods.
|
||||
abstract class B extends A implements I
|
||||
endclass
|
||||
|
||||
class C extends B implements I
|
||||
def ToString(): string
|
||||
return super.ToString()
|
||||
enddef
|
||||
endclass
|
||||
|
||||
# Note that A.ToString() is different from I.ToString().
|
||||
def TestA(a: A): string
|
||||
return a.ToString()
|
||||
enddef
|
||||
|
||||
assert_equal('A', C.new().ToString())
|
||||
assert_equal('A', TestA(A.new()))
|
||||
assert_equal('A', TestA(C.new()))
|
||||
END
|
||||
v9.CheckSourceSuccess(lines)
|
||||
enddef
|
||||
|
||||
def Test_class_import()
|
||||
|
@@ -704,6 +704,8 @@ static char *(features[]) =
|
||||
|
||||
static int included_patches[] =
|
||||
{ /* Add new patch number below this line */
|
||||
/**/
|
||||
1037,
|
||||
/**/
|
||||
1036,
|
||||
/**/
|
||||
|
@@ -373,6 +373,7 @@ compile_class_object_index(cctx_T *cctx, char_u **arg, type_T *type)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
ocmember_T *ocm = NULL;
|
||||
if (ufunc == NULL)
|
||||
{
|
||||
@@ -405,6 +406,15 @@ compile_class_object_index(cctx_T *cctx, char_u **arg, type_T *type)
|
||||
}
|
||||
}
|
||||
|
||||
if (is_super && IS_ABSTRACT_METHOD(ufunc))
|
||||
{
|
||||
// Trying to invoke an abstract method in a super class is not
|
||||
// allowed.
|
||||
semsg(_(e_abstract_method_str_direct), ufunc->uf_name,
|
||||
ufunc->uf_defclass->class_name);
|
||||
return FAIL;
|
||||
}
|
||||
|
||||
// A private object method can be used only inside the class where it
|
||||
// is defined or in one of the child classes.
|
||||
// A private class method can be used only in the class where it is
|
||||
|
Reference in New Issue
Block a user