]> granicus.if.org Git - vim/commitdiff
patch 9.0.1207: error when object type is expected but getting "any" v9.0.1207
authorBram Moolenaar <Bram@vim.org>
Mon, 16 Jan 2023 16:39:37 +0000 (16:39 +0000)
committerBram Moolenaar <Bram@vim.org>
Mon, 16 Jan 2023 16:39:37 +0000 (16:39 +0000)
Problem:    Error when object type is expected but getting "any".
Solution:   When actual type is "any" use a runtime type check.
            (closes #11826)

src/testdir/test_vim9_class.vim
src/version.c
src/vim9type.c

index 85f47a88be8416de775655605e8f8f3f7812c89a..c2c4cf30839d6e572464d487b5b6017c3d3b94d1 100644 (file)
@@ -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()
index 78259846bbdd9ac2158b6782eb88b0715527f8db..afc4de87849cd7b40afe72ab0fa67fe85ca734c7 100644 (file)
@@ -695,6 +695,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1207,
 /**/
     1206,
 /**/
index fb32a8628ffc98428e974d1c1e71299af645d412..08dabd1c22f8ee890d63f9bc1689cfd3c41ef63c 100644 (file)
@@ -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;