Skip to content

Commit 61378a1

Browse files
h-eastbrammool
authored andcommitted
patch 9.0.1466: cannot use an object member name as a method argument
Problem: Cannot use an object member name as a method argument. Solution: Do not give an error for using an object member name for a method argument. (Hirohito Higashi, closes #12241, closes #12225) Fix line number for other argument error.
1 parent 89f9ffb commit 61378a1

3 files changed

Lines changed: 22 additions & 18 deletions

File tree

src/testdir/test_vim9_class.vim

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -995,7 +995,7 @@ def Test_interface_basics()
995995
def Method(count: number)
996996
endinterface
997997
END
998-
v9.CheckScriptFailure(lines, 'E1340: Argument already declared in the class: count')
998+
v9.CheckScriptFailure(lines, 'E1340: Argument already declared in the class: count', 5)
999999

10001000
lines =<< trim END
10011001
vim9script
@@ -1005,7 +1005,9 @@ def Test_interface_basics()
10051005
def Method(value: number)
10061006
endinterface
10071007
END
1008-
v9.CheckScriptFailure(lines, 'E1340: Argument already declared in the class: value')
1008+
# The argument name and the object member name are the same, but this is not a
1009+
# problem because object members are always accessed with the "this." prefix.
1010+
v9.CheckScriptSuccess(lines)
10091011

10101012
lines =<< trim END
10111013
vim9script

src/version.c

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

696696
static int included_patches[] =
697697
{ /* Add new patch number below this line */
698+
/**/
699+
1466,
698700
/**/
699701
1465,
700702
/**/

src/vim9class.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,9 @@ ex_class(exarg_T *eap)
737737

738738
if (success)
739739
{
740-
// Check no function argument name is used as an object/class member.
740+
// Check no function argument name is used as a class member.
741+
// (Object members are always accessed with "this." prefix, so no need
742+
// to check them.)
741743
for (int loop = 1; loop <= 2 && success; ++loop)
742744
{
743745
garray_T *gap = loop == 1 ? &classfunctions : &objmethods;
@@ -749,24 +751,22 @@ ex_class(exarg_T *eap)
749751
for (int i = 0; i < uf->uf_args.ga_len && success; ++i)
750752
{
751753
char_u *aname = ((char_u **)uf->uf_args.ga_data)[i];
752-
for (int il = 1; il <= 2 && success; ++il)
754+
garray_T *mgap = &classmembers;
755+
756+
for (int mi = 0; mi < mgap->ga_len; ++mi)
753757
{
754-
// For a "new()" function "this.member" arguments are
755-
// OK. TODO: check for the "this." prefix.
756-
if (STRNCMP(uf->uf_name, "new", 3) == 0 && il == 2)
757-
continue;
758-
garray_T *mgap = il == 1 ? &classmembers : &objmembers;
759-
for (int mi = 0; mi < mgap->ga_len; ++mi)
758+
char_u *mname = ((ocmember_T *)mgap->ga_data + mi)
759+
->ocm_name;
760+
if (STRCMP(aname, mname) == 0)
760761
{
761-
char_u *mname = ((ocmember_T *)mgap->ga_data
762-
+ mi)->ocm_name;
763-
if (STRCMP(aname, mname) == 0)
764-
{
765-
success = FALSE;
766-
semsg(_(e_argument_already_declared_in_class_str),
762+
success = FALSE;
763+
764+
if (uf->uf_script_ctx.sc_sid > 0)
765+
SOURCING_LNUM = uf->uf_script_ctx.sc_lnum;
766+
767+
semsg(_(e_argument_already_declared_in_class_str),
767768
aname);
768-
break;
769-
}
769+
break;
770770
}
771771
}
772772
}

0 commit comments

Comments
 (0)