Skip to content

Commit e798446

Browse files
yegappanchrisbra
authored andcommitted
patch 9.1.0861: Vim9: no runtime check for object member access of any var
Problem: Vim9: no runtime check for object member access of any var (after: 9.1.0850) Solution: Add runtime type compatibility check for object member accessed using a any variable (Yegappan Lakshmanan). closes: #16037 Signed-off-by: Yegappan Lakshmanan <yegappan@yahoo.com> Signed-off-by: Christian Brabandt <cb@256bit.org>
1 parent 210c49b commit e798446

3 files changed

Lines changed: 57 additions & 0 deletions

File tree

src/testdir/test_vim9_class.vim

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11546,6 +11546,54 @@ def Test_any_obj_var_type()
1154611546
Fn(null_object)
1154711547
END
1154811548
v9.CheckScriptFailure(lines, 'E1360: Using a null object', 1)
11549+
11550+
# Try to change a const object variable using a "any" variable
11551+
lines =<< trim END
11552+
vim9script
11553+
class A
11554+
public const v1: number = 123
11555+
endclass
11556+
11557+
def Fn(o: any)
11558+
o.v1 = 321
11559+
enddef
11560+
11561+
var a = A.new()
11562+
Fn(a)
11563+
END
11564+
v9.CheckScriptFailure(lines, 'E1409: Cannot change read-only variable "v1" in class "A"', 1)
11565+
11566+
# Try to change a final object variable using a "any" variable
11567+
lines =<< trim END
11568+
vim9script
11569+
class A
11570+
public final v1: number = 123
11571+
endclass
11572+
11573+
def Fn(o: any)
11574+
o.v1 = 321
11575+
enddef
11576+
11577+
var a = A.new()
11578+
Fn(a)
11579+
END
11580+
v9.CheckScriptFailure(lines, 'E1409: Cannot change read-only variable "v1" in class "A"', 1)
11581+
11582+
# Assign a different type of value to an "any" type object variable
11583+
lines =<< trim END
11584+
vim9script
11585+
class A
11586+
public var v1: list<any> = [1, 2]
11587+
endclass
11588+
11589+
def Fn(o: A)
11590+
o.v1 = 'abc'
11591+
enddef
11592+
11593+
var a = A.new()
11594+
Fn(a)
11595+
END
11596+
v9.CheckScriptFailure(lines, 'E1012: Type mismatch; expected list<any> but got string', 1)
1154911597
enddef
1155011598

1155111599
" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker

src/version.c

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

705705
static int included_patches[] =
706706
{ /* Add new patch number below this line */
707+
/**/
708+
861,
707709
/**/
708710
860,
709711
/**/

src/vim9execute.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2271,6 +2271,7 @@ execute_storeindex(isn_T *iptr, ectx_T *ectx)
22712271
// Get the current function
22722272
ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
22732273
+ ectx->ec_dfunc_idx)->df_ufunc;
2274+
where_T where = WHERE_INIT;
22742275

22752276
// Check whether the member variable is writeable
22762277
if ((m->ocm_access != VIM_ACCESS_ALL) &&
@@ -2283,6 +2284,12 @@ execute_storeindex(isn_T *iptr, ectx_T *ectx)
22832284
emsg_var_cl_define(msg, m->ocm_name, 0, cl);
22842285
status = FAIL;
22852286
}
2287+
// Fail if the variable is a const or final or the type
2288+
// is not compatible
2289+
else if (oc_var_check_ro(cl, m) ||
2290+
check_typval_type(m->ocm_type, tv, where)
2291+
== FAIL)
2292+
status = FAIL;
22862293
else
22872294
lidx = m_idx;
22882295
}

0 commit comments

Comments
 (0)