To: vim_dev@googlegroups.com Subject: Patch 9.0.1181 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ Patch 9.0.1181 Problem: Class inheritance and typing insufficiently tested. Solution: Add more tests. Implement missing behavior. Files: src/vim9type.c, src/testdir/test_vim9_class.vim *** ../vim-9.0.1180/src/vim9type.c 2023-01-09 14:18:09.235577964 +0000 --- src/vim9type.c 2023-01-11 21:06:20.876226855 +0000 *************** *** 874,879 **** --- 874,890 ---- // check the argument count at runtime ret = MAYBE; } + else if (expected->tt_type == VAR_OBJECT) + { + class_T *cl; + for (cl = (class_T *)actual->tt_member; cl != NULL; + cl = cl->class_extends) + if ((class_T *)expected->tt_member == cl) + break; + if (cl == NULL) + ret = FAIL; + } + if (ret == FAIL && give_msg) type_mismatch_where(expected, actual, where); } *************** *** 1601,1613 **** if (type == NULL) return "[unknown]"; name = vartype_name(type->tt_type); if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT) { char *member_free; char *member_name = type_name(type->tt_member, &member_free); ! size_t len; ! ! len = STRLEN(name) + STRLEN(member_name) + 3; *tofree = alloc(len); if (*tofree != NULL) { --- 1612,1623 ---- if (type == NULL) return "[unknown]"; name = vartype_name(type->tt_type); + if (type->tt_type == VAR_LIST || type->tt_type == VAR_DICT) { char *member_free; char *member_name = type_name(type->tt_member, &member_free); ! size_t len = STRLEN(name) + STRLEN(member_name) + 3; *tofree = alloc(len); if (*tofree != NULL) { *************** *** 1616,1621 **** --- 1626,1644 ---- return *tofree; } } + + if (type->tt_type == VAR_OBJECT || type->tt_type == VAR_CLASS) + { + char_u *class_name = ((class_T *)type->tt_member)->class_name; + size_t len = STRLEN(name) + STRLEN(class_name) + 3; + *tofree = alloc(len); + if (*tofree != NULL) + { + vim_snprintf(*tofree, len, "%s<%s>", name, class_name); + return *tofree; + } + } + if (type->tt_type == VAR_FUNC) { garray_T ga; *** ../vim-9.0.1180/src/testdir/test_vim9_class.vim 2023-01-11 17:59:35.114319167 +0000 --- src/testdir/test_vim9_class.vim 2023-01-11 21:06:48.928219836 +0000 *************** *** 419,424 **** --- 419,462 ---- endfor enddef + def Test_object_type() + var lines =<< trim END + vim9script + + class One + this.one = 1 + endclass + class Two + this.two = 2 + endclass + class TwoMore extends Two + this.more = 9 + endclass + + var o: One = One.new() + var t: Two = Two.new() + var m: TwoMore = TwoMore.new() + var tm: Two = TwoMore.new() + + t = m + END + v9.CheckScriptSuccess(lines) + + lines =<< trim END + vim9script + + class One + this.one = 1 + endclass + class Two + this.two = 2 + endclass + + var o: One = Two.new() + END + v9.CheckScriptFailure(lines, 'E1012: Type mismatch; expected object but got object') + enddef + def Test_class_member() # check access rules var lines =<< trim END *************** *** 750,756 **** var p: Point p = 'text' END ! v9.CheckScriptFailure(lines, 'E1012: Type mismatch; expected object but got string') enddef def Test_class_extends() --- 788,794 ---- var p: Point p = 'text' END ! v9.CheckScriptFailure(lines, 'E1012: Type mismatch; expected object but got string') enddef def Test_class_extends() *************** *** 895,900 **** --- 933,959 ---- echo o.ToString() END v9.CheckScriptFailure(lines, 'E1358:') + + lines =<< trim END + vim9script + class Base + this.name: string + static def ToString(): string + return 'Base class' + enddef + endclass + + class Child extends Base + this.age: number + def ToString(): string + return Base.ToString() .. ': ' .. this.age + enddef + endclass + + var o = Child.new('John', 42) + assert_equal('Base class: 42', o.ToString()) + END + v9.CheckScriptSuccess(lines) enddef *** ../vim-9.0.1180/src/version.c 2023-01-11 19:11:09.468757611 +0000 --- src/version.c 2023-01-11 20:35:18.656552352 +0000 *************** *** 697,698 **** --- 697,700 ---- { /* Add new patch number below this line */ + /**/ + 1181, /**/ -- Every engineer dreams about saving the universe and having sex with aliens. This is much more glamorous than the real life of an engineer, which consists of hiding from the universe and having sex without the participation of other life forms. (Scott Adams - The Dilbert principle) /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\ /// \\\ \\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ /// \\\ help me help AIDS victims -- http://ICCF-Holland.org ///