]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 9.1.1362: Vim9: type ignored when adding tuple to instance list var v9.1.1362
authorYegappan Lakshmanan <yegappan@yahoo.com>
Sat, 3 May 2025 17:11:45 +0000 (19:11 +0200)
committerChristian Brabandt <cb@256bit.org>
Sat, 3 May 2025 17:11:45 +0000 (19:11 +0200)
Problem:  Vim9: type ignored when adding tuple to instance list var
          (Lifepillar)
Solution: When getting the typval of class and object member variables,
          set the variable type (Yegappan Lakshmanan)

fixes: #17236
closes: #17244

Signed-off-by: Yegappan Lakshmanan <yegappan@yahoo.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
src/testdir/test_vim9_class.vim
src/version.c
src/vim9class.c

index e854571e5ffb46d6455f4f614fad858853995bdf..3ba467b238a38b4b9e098f1117bbabda6c6dcbbe 100644 (file)
@@ -13084,4 +13084,73 @@ def Test_object_of_class_type()
         \ 'E1353: Class name not found: <number>'])
 enddef
 
+" Test for the object and class member type
+def Test_obj_class_member_type()
+  var lines =<< trim END
+    vim9script
+    class L
+      var l: list<number>
+    endclass
+    var obj_L = L.new([10, 20])
+    assert_equal('list<number>', typename(obj_L.l))
+    obj_L.l->add('a')
+  END
+  v9.CheckSourceFailure(lines, 'E1012: Type mismatch; expected number but got string', 7)
+
+  lines =<< trim END
+    vim9script
+    class T
+      var t: list<tuple<string>>
+    endclass
+    var obj_T = T.new([('a',), ('b',)])
+    assert_equal('list<tuple<string>>', typename(obj_T.t))
+    obj_T.t->add([('c', 10, true)])
+  END
+  v9.CheckSourceFailure(lines, 'E1012: Type mismatch; expected tuple<string> but got list<tuple<string, number, bool>>', 7)
+
+  lines =<< trim END
+    vim9script
+    class D
+      var d: dict<number>
+    endclass
+    var obj_D = D.new({a: 10, b: 20})
+    assert_equal('dict<number>', typename(obj_D.d))
+    obj_D.d->extend({c: 'C'})
+  END
+  v9.CheckSourceFailure(lines, 'E1013: Argument 2: type mismatch, expected dict<number> but got dict<string> in extend()', 7)
+
+  lines =<< trim END
+    vim9script
+    class L
+      public static var l: list<number>
+    endclass
+    L.l = [10, 20]
+    assert_equal('list<number>', typename(L.l))
+    L.l->add('a')
+  END
+  v9.CheckSourceFailure(lines, 'E1012: Type mismatch; expected number but got string', 7)
+
+  lines =<< trim END
+    vim9script
+    class T
+      public static var t: list<tuple<string>>
+    endclass
+    T.t = [('a',), ('b',)]
+    assert_equal('list<tuple<string>>', typename(T.t))
+    T.t->add([('c', 10, true)])
+  END
+  v9.CheckSourceFailure(lines, 'E1012: Type mismatch; expected tuple<string> but got list<tuple<string, number, bool>>', 7)
+
+  lines =<< trim END
+    vim9script
+    class D
+      public static var d: dict<number>
+    endclass
+    D.d = {a: 10, b: 20}
+    assert_equal('dict<number>', typename(D.d))
+    D.d->extend({c: 'C'})
+  END
+  v9.CheckSourceFailure(lines, 'E1013: Argument 2: type mismatch, expected dict<number> but got dict<string> in extend()', 7)
+enddef
+
 " vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker
index 7d29d7020bc492414b3a1c8dff0e5e660907186c..4dc628f30f7a060015a64818f47b7b0fabdd3f0a 100644 (file)
@@ -704,6 +704,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1362,
 /**/
     1361,
 /**/
index 560c5ae283498d6c8e721c5fdf5708954cfbb99f..a82cc13329b201ae658e5b504453b2b2b2dedf67 100644 (file)
@@ -2928,11 +2928,13 @@ get_member_tv(
        object_T *obj = rettv->vval.v_object;
        typval_T *tv = (typval_T *)(obj + 1) + m_idx;
        copy_tv(tv, rettv);
+       set_tv_type(rettv, m->ocm_type);
        object_unref(obj);
     }
     else
     {
        copy_tv(&cl->class_members_tv[m_idx], rettv);
+       set_tv_type(rettv, m->ocm_type);
        class_unref(cl);
     }