Skip to content

Commit 4cae845

Browse files
committed
patch 9.0.1201: assignment with operator doesn't work in object method
Problem: Assignment with operator doesn't work in object method. Solution: Handle loading the object member. (closes #11820) Add a few more tests.
1 parent 474f226 commit 4cae845

3 files changed

Lines changed: 72 additions & 0 deletions

File tree

src/testdir/test_vim9_class.vim

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,25 @@ def Test_class_member_initializer()
200200
v9.CheckScriptSuccess(lines)
201201
enddef
202202

203+
def Test_assignment_with_operator()
204+
var lines =<< trim END
205+
vim9script
206+
207+
class Foo
208+
this.x: number
209+
210+
def Add(n: number)
211+
this.x += n
212+
enddef
213+
endclass
214+
215+
var f = Foo.new(3)
216+
f.Add(17)
217+
assert_equal(20, f.x)
218+
END
219+
v9.CheckScriptSuccess(lines)
220+
enddef
221+
203222
def Test_class_default_new()
204223
var lines =<< trim END
205224
vim9script
@@ -521,6 +540,25 @@ def Test_class_member()
521540
END
522541
v9.CheckScriptSuccess(lines)
523542

543+
# example in the help
544+
lines =<< trim END
545+
vim9script
546+
class OtherThing
547+
this.size: number
548+
static totalSize: number
549+
550+
def new(this.size)
551+
totalSize += this.size
552+
enddef
553+
endclass
554+
assert_equal(0, OtherThing.totalSize)
555+
var to3 = OtherThing.new(3)
556+
assert_equal(3, OtherThing.totalSize)
557+
var to7 = OtherThing.new(7)
558+
assert_equal(10, OtherThing.totalSize)
559+
END
560+
v9.CheckScriptSuccess(lines)
561+
524562
# check shadowing
525563
lines =<< trim END
526564
vim9script
@@ -986,6 +1024,23 @@ def Test_class_extends()
9861024
assert_equal('Base class: 42', o.ToString())
9871025
END
9881026
v9.CheckScriptSuccess(lines)
1027+
1028+
lines =<< trim END
1029+
vim9script
1030+
class Base
1031+
this.value = 1
1032+
def new(init: number)
1033+
this.value = number + 1
1034+
enddef
1035+
endclass
1036+
class Child extends Base
1037+
def new()
1038+
this.new(3)
1039+
enddef
1040+
endclass
1041+
var c = Child.new()
1042+
END
1043+
v9.CheckScriptFailure(lines, 'E1325: Method not found on class "Child": new(')
9891044
enddef
9901045

9911046
def Test_class_import()

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+
1201,
698700
/**/
699701
1200,
700702
/**/

src/vim9compile.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2044,6 +2044,21 @@ compile_load_lhs(
20442044
int
20452045
compile_load_lhs_with_index(lhs_T *lhs, char_u *var_start, cctx_T *cctx)
20462046
{
2047+
if (lhs->lhs_type->tt_type == VAR_OBJECT)
2048+
{
2049+
// "this.value": load "this" object and get the value at index
2050+
// for an object or class member get the type of the member
2051+
class_T *cl = (class_T *)lhs->lhs_type->tt_member;
2052+
type_T *type = class_member_type(cl, var_start + 5,
2053+
lhs->lhs_end, &lhs->lhs_member_idx);
2054+
if (lhs->lhs_member_idx < 0)
2055+
return FAIL;
2056+
2057+
if (generate_LOAD(cctx, ISN_LOAD, 0, NULL, lhs->lhs_type) == FAIL)
2058+
return FAIL;
2059+
return generate_GET_OBJ_MEMBER(cctx, lhs->lhs_member_idx, type);
2060+
}
2061+
20472062
compile_load_lhs(lhs, var_start, NULL, cctx);
20482063

20492064
if (lhs->lhs_has_index)

0 commit comments

Comments
 (0)