patch 9.1.0861: Vim9: no runtime check for object member access of any var
Commit:
https://github.com/vim/vim/commit/e798446362efe4d784a26f51b106280d35c6da82
Author: Yegappan Lakshmanan <[email protected]>
Date: Tue Nov 12 21:03:00 2024 +0100
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 <[email protected]>
Signed-off-by: Christian Brabandt <[email protected]>
diff --git a/src/testdir/test_vim9_class.vim b/src/testdir/test_vim9_class.vim
index 4ce9fcdc4..d8a3fa383 100644
--- a/src/testdir/test_vim9_class.vim
+++ b/src/testdir/test_vim9_class.vim
@@ -11546,6 +11546,54 @@ def Test_any_obj_var_type()
Fn(null_object)
END
v9.CheckScriptFailure(lines, 'E1360: Using a null object', 1)
+
+ # Try to change a const object variable using a "any" variable
+ lines =<< trim END
+ vim9script
+ class A
+ public const v1: number = 123
+ endclass
+
+ def Fn(o: any)
+ o.v1 = 321
+ enddef
+
+ var a = A.new()
+ Fn(a)
+ END
+ v9.CheckScriptFailure(lines, 'E1409: Cannot change read-only variable "v1"
in class "A"', 1)
+
+ # Try to change a final object variable using a "any" variable
+ lines =<< trim END
+ vim9script
+ class A
+ public final v1: number = 123
+ endclass
+
+ def Fn(o: any)
+ o.v1 = 321
+ enddef
+
+ var a = A.new()
+ Fn(a)
+ END
+ v9.CheckScriptFailure(lines, 'E1409: Cannot change read-only variable "v1"
in class "A"', 1)
+
+ # Assign a different type of value to an "any" type object variable
+ lines =<< trim END
+ vim9script
+ class A
+ public var v1: list<any> = [1, 2]
+ endclass
+
+ def Fn(o: A)
+ o.v1 = 'abc'
+ enddef
+
+ var a = A.new()
+ Fn(a)
+ END
+ v9.CheckScriptFailure(lines, 'E1012: Type mismatch; expected list<any> but
got string', 1)
enddef
" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker
diff --git a/src/version.c b/src/version.c
index 798598568..f2e9473f7 100644
--- a/src/version.c
+++ b/src/version.c
@@ -704,6 +704,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
+/**/
+ 861,
/**/
860,
/**/
diff --git a/src/vim9execute.c b/src/vim9execute.c
index da03d5e7d..7ab6813a6 100644
--- a/src/vim9execute.c
+++ b/src/vim9execute.c
@@ -2271,6 +2271,7 @@ execute_storeindex(isn_T *iptr, ectx_T *ectx)
// Get the current function
ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
+ ectx->ec_dfunc_idx)->df_ufunc;
+ where_T where = WHERE_INIT;
// Check whether the member variable is writeable
if ((m->ocm_access != VIM_ACCESS_ALL) &&
@@ -2283,6 +2284,12 @@ execute_storeindex(isn_T *iptr, ectx_T *ectx)
emsg_var_cl_define(msg, m->ocm_name, 0, cl);
status = FAIL;
}
+ // Fail if the variable is a const or final or the type
+ // is not compatible
+ else if (oc_var_check_ro(cl, m) ||
+ check_typval_type(m->ocm_type, tv, where)
+ == FAIL)
+ status = FAIL;
else
lidx = m_idx;
}
--
--
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
---
You received this message because you are subscribed to the Google Groups
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion visit
https://groups.google.com/d/msgid/vim_dev/E1tAxHz-000Aix-8T%40256bit.org.