changeset 31750:13cecb002fe6 v9.0.1207

patch 9.0.1207: error when object type is expected but getting "any" Commit: https://github.com/vim/vim/commit/450c7a97d1a28f715acaf562298112b9b932adc3 Author: Bram Moolenaar <Bram@vim.org> Date: Mon Jan 16 16:39:37 2023 +0000 patch 9.0.1207: error when object type is expected but getting "any" Problem: Error when object type is expected but getting "any". Solution: When actual type is "any" use a runtime type check. (closes #11826)
author Bram Moolenaar <Bram@vim.org>
date Mon, 16 Jan 2023 17:45:02 +0100
parents e496296f7aaa
children 997105f91449
files src/testdir/test_vim9_class.vim src/version.c src/vim9type.c
diffstat 3 files changed, 27 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/testdir/test_vim9_class.vim
+++ b/src/testdir/test_vim9_class.vim
@@ -537,6 +537,26 @@ def Test_object_type()
       assert_equal(5, o.GetMember())
   END
   v9.CheckScriptSuccess(lines)
+
+  lines =<< trim END
+      vim9script
+
+      class Num
+        this.n: number = 0
+      endclass
+
+      def Ref(name: string): func(Num): Num
+        return (arg: Num): Num => {
+          return eval(name)(arg)
+        }
+      enddef
+
+      const Fn = Ref('Double')
+      var Double = (m: Num): Num => Num.new(m.n * 2)
+
+      echo Fn(Num.new(4))
+  END
+  v9.CheckScriptSuccess(lines)
 enddef
 
 def Test_class_member()
--- a/src/version.c
+++ b/src/version.c
@@ -696,6 +696,8 @@ static char *(features[]) =
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    1207,
+/**/
     1206,
 /**/
     1205,
--- a/src/vim9type.c
+++ b/src/vim9type.c
@@ -878,6 +878,11 @@ check_type_maybe(
 	}
 	else if (expected->tt_type == VAR_OBJECT)
 	{
+	    if (actual->tt_type == VAR_ANY)
+		return MAYBE;	// use runtime type check
+	    if (actual->tt_type != VAR_OBJECT)
+		return FAIL;	// don't use tt_member
+
 	    // check the class, base class or an implemented interface matches
 	    class_T *cl;
 	    for (cl = (class_T *)actual->tt_member; cl != NULL;