v9.CheckSourceFailure(lines, 'E1383: Method "Doit": type mismatch, expected func(object<B>): object<B> but got func(object<B>): object<A>', 20)
enddef
+" Test type checking for class variable in assignments
+func Test_class_variable_complex_type_check()
+ " class variable with a specific type. Try assigning a different type at
+ " script level.
+ let lines =<< trim END
+ vim9script
+ def Foo(l: list<dict<number>>): dict<list<number>>
+ return {}
+ enddef
+ class A
+ public static Fn: func(list<dict<number>>): dict<list<number>> = Foo
+ endclass
+ test_garbagecollect_now()
+ A.Fn = "abc"
+ END
+ call v9.CheckSourceFailure(lines, 'E1012: Type mismatch; expected func(list<dict<number>>): dict<list<number>> but got string', 9)
+
+ " class variable with a specific type. Try assigning a different type at
+ " class def method level.
+ let lines =<< trim END
+ vim9script
+ def Foo(l: list<dict<number>>): dict<list<number>>
+ return {}
+ enddef
+ class A
+ public static Fn: func(list<dict<number>>): dict<list<number>> = Foo
+ def Bar()
+ Fn = "abc"
+ enddef
+ endclass
+ var a = A.new()
+ test_garbagecollect_now()
+ a.Bar()
+ END
+ call v9.CheckSourceFailure(lines, 'E1012: Type mismatch; expected func(list<dict<number>>): dict<list<number>> but got string', 1)
+
+ " class variable with a specific type. Try assigning a different type at
+ " script def method level.
+ let lines =<< trim END
+ vim9script
+ def Foo(l: list<dict<number>>): dict<list<number>>
+ return {}
+ enddef
+ class A
+ public static Fn: func(list<dict<number>>): dict<list<number>> = Foo
+ endclass
+ def Bar()
+ A.Fn = "abc"
+ enddef
+ test_garbagecollect_now()
+ Bar()
+ END
+ call v9.CheckSourceFailure(lines, 'E1012: Type mismatch; expected func(list<dict<number>>): dict<list<number>> but got string', 1)
+
+ " class variable without any type. Should be set to the initialization
+ " expression type. Try assigning a different type from script level.
+ let lines =<< trim END
+ vim9script
+ def Foo(l: list<dict<number>>): dict<list<number>>
+ return {}
+ enddef
+ class A
+ public static Fn = Foo
+ endclass
+ test_garbagecollect_now()
+ A.Fn = "abc"
+ END
+ call v9.CheckSourceFailure(lines, 'E1012: Type mismatch; expected func(list<dict<number>>): dict<list<number>> but got string', 9)
+
+ " class variable without any type. Should be set to the initialization
+ " expression type. Try assigning a different type at class def level.
+ let lines =<< trim END
+ vim9script
+ def Foo(l: list<dict<number>>): dict<list<number>>
+ return {}
+ enddef
+ class A
+ public static Fn = Foo
+ def Bar()
+ Fn = "abc"
+ enddef
+ endclass
+ var a = A.new()
+ test_garbagecollect_now()
+ a.Bar()
+ END
+ call v9.CheckSourceFailure(lines, 'E1012: Type mismatch; expected func(list<dict<number>>): dict<list<number>> but got string', 1)
+
+ " class variable without any type. Should be set to the initialization
+ " expression type. Try assigning a different type at script def level.
+ let lines =<< trim END
+ vim9script
+ def Foo(l: list<dict<number>>): dict<list<number>>
+ return {}
+ enddef
+ class A
+ public static Fn = Foo
+ endclass
+ def Bar()
+ A.Fn = "abc"
+ enddef
+ test_garbagecollect_now()
+ Bar()
+ END
+ call v9.CheckSourceFailure(lines, 'E1012: Type mismatch; expected func(list<dict<number>>): dict<list<number>> but got string', 1)
+
+ " class variable with 'any" type. Can be assigned different types.
+ let lines =<< trim END
+ vim9script
+ def Foo(l: list<dict<number>>): dict<list<number>>
+ return {}
+ enddef
+ class A
+ public static Fn: any = Foo
+ public static Fn2: any
+ endclass
+ test_garbagecollect_now()
+ assert_equal('func(list<dict<number>>): dict<list<number>>', typename(A.Fn))
+ A.Fn = "abc"
+ test_garbagecollect_now()
+ assert_equal('string', typename(A.Fn))
+ A.Fn2 = Foo
+ test_garbagecollect_now()
+ assert_equal('func(list<dict<number>>): dict<list<number>>', typename(A.Fn2))
+ A.Fn2 = "xyz"
+ test_garbagecollect_now()
+ assert_equal('string', typename(A.Fn2))
+ END
+ call v9.CheckSourceSuccess(lines)
+
+ " class variable with 'any" type. Can be assigned different types.
+ let lines =<< trim END
+ vim9script
+ def Foo(l: list<dict<number>>): dict<list<number>>
+ return {}
+ enddef
+ class A
+ public static Fn: any = Foo
+ public static Fn2: any
+
+ def Bar()
+ assert_equal('func(list<dict<number>>): dict<list<number>>', typename(Fn))
+ Fn = "abc"
+ assert_equal('string', typename(Fn))
+ Fn2 = Foo
+ assert_equal('func(list<dict<number>>): dict<list<number>>', typename(Fn2))
+ Fn2 = "xyz"
+ assert_equal('string', typename(Fn2))
+ enddef
+ endclass
+ var a = A.new()
+ test_garbagecollect_now()
+ a.Bar()
+ test_garbagecollect_now()
+ A.Fn = Foo
+ a.Bar()
+ END
+ call v9.CheckSourceSuccess(lines)
+
+ " class variable with 'any" type. Can be assigned different types.
+ let lines =<< trim END
+ vim9script
+ def Foo(l: list<dict<number>>): dict<list<number>>
+ return {}
+ enddef
+ class A
+ public static Fn: any = Foo
+ public static Fn2: any
+ endclass
+
+ def Bar()
+ assert_equal('func(list<dict<number>>): dict<list<number>>', typename(A.Fn))
+ A.Fn = "abc"
+ assert_equal('string', typename(A.Fn))
+ A.Fn2 = Foo
+ assert_equal('func(list<dict<number>>): dict<list<number>>', typename(A.Fn2))
+ A.Fn2 = "xyz"
+ assert_equal('string', typename(A.Fn2))
+ enddef
+ Bar()
+ test_garbagecollect_now()
+ A.Fn = Foo
+ Bar()
+ END
+ call v9.CheckSourceSuccess(lines)
+endfunc
+
+" Test type checking for object variable in assignments
+func Test_object_variable_complex_type_check()
+ " object variable with a specific type. Try assigning a different type at
+ " script level.
+ let lines =<< trim END
+ vim9script
+ def Foo(l: list<dict<number>>): dict<list<number>>
+ return {}
+ enddef
+ class A
+ public this.Fn: func(list<dict<number>>): dict<list<number>> = Foo
+ endclass
+ var a = A.new()
+ test_garbagecollect_now()
+ a.Fn = "abc"
+ END
+ call v9.CheckSourceFailure(lines, 'E1012: Type mismatch; expected func(list<dict<number>>): dict<list<number>> but got string', 10)
+
+ " object variable with a specific type. Try assigning a different type at
+ " object def method level.
+ let lines =<< trim END
+ vim9script
+ def Foo(l: list<dict<number>>): dict<list<number>>
+ return {}
+ enddef
+ class A
+ public this.Fn: func(list<dict<number>>): dict<list<number>> = Foo
+ def Bar()
+ this.Fn = "abc"
+ this.Fn = Foo
+ enddef
+ endclass
+ var a = A.new()
+ test_garbagecollect_now()
+ a.Bar()
+ END
+ call v9.CheckSourceFailure(lines, 'E1012: Type mismatch; expected func(list<dict<number>>): dict<list<number>> but got string', 1)
+
+ " object variable with a specific type. Try assigning a different type at
+ " script def method level.
+ let lines =<< trim END
+ vim9script
+ def Foo(l: list<dict<number>>): dict<list<number>>
+ return {}
+ enddef
+ class A
+ public this.Fn: func(list<dict<number>>): dict<list<number>> = Foo
+ endclass
+ def Bar()
+ var a = A.new()
+ a.Fn = "abc"
+ a.Fn = Foo
+ enddef
+ test_garbagecollect_now()
+ Bar()
+ END
+ call v9.CheckSourceFailure(lines, 'E1012: Type mismatch; expected func(list<dict<number>>): dict<list<number>> but got string', 2)
+
+ " object variable without any type. Should be set to the initialization
+ " expression type. Try assigning a different type from script level.
+ let lines =<< trim END
+ vim9script
+ def Foo(l: list<dict<number>>): dict<list<number>>
+ return {}
+ enddef
+ class A
+ public this.Fn = Foo
+ endclass
+ var a = A.new()
+ test_garbagecollect_now()
+ a.Fn = "abc"
+ END
+ call v9.CheckSourceFailure(lines, 'E1012: Type mismatch; expected func(list<dict<number>>): dict<list<number>> but got string', 10)
+
+ " object variable without any type. Should be set to the initialization
+ " expression type. Try assigning a different type at object def level.
+ let lines =<< trim END
+ vim9script
+ def Foo(l: list<dict<number>>): dict<list<number>>
+ return {}
+ enddef
+ class A
+ public this.Fn = Foo
+ def Bar()
+ this.Fn = "abc"
+ this.Fn = Foo
+ enddef
+ endclass
+ var a = A.new()
+ test_garbagecollect_now()
+ a.Bar()
+ END
+ call v9.CheckSourceFailure(lines, 'E1012: Type mismatch; expected func(list<dict<number>>): dict<list<number>> but got string', 1)
+
+ " object variable without any type. Should be set to the initialization
+ " expression type. Try assigning a different type at script def level.
+ let lines =<< trim END
+ vim9script
+ def Foo(l: list<dict<number>>): dict<list<number>>
+ return {}
+ enddef
+ class A
+ public this.Fn = Foo
+ endclass
+ def Bar()
+ var a = A.new()
+ a.Fn = "abc"
+ a.Fn = Foo
+ enddef
+ test_garbagecollect_now()
+ Bar()
+ END
+ call v9.CheckSourceFailure(lines, 'E1012: Type mismatch; expected func(list<dict<number>>): dict<list<number>> but got string', 2)
+
+ " object variable with 'any" type. Can be assigned different types.
+ let lines =<< trim END
+ vim9script
+ def Foo(l: list<dict<number>>): dict<list<number>>
+ return {}
+ enddef
+ class A
+ public this.Fn: any = Foo
+ public this.Fn2: any
+ endclass
+
+ var a = A.new()
+ test_garbagecollect_now()
+ assert_equal('func(list<dict<number>>): dict<list<number>>', typename(a.Fn))
+ a.Fn = "abc"
+ test_garbagecollect_now()
+ assert_equal('string', typename(a.Fn))
+ a.Fn2 = Foo
+ test_garbagecollect_now()
+ assert_equal('func(list<dict<number>>): dict<list<number>>', typename(a.Fn2))
+ a.Fn2 = "xyz"
+ test_garbagecollect_now()
+ assert_equal('string', typename(a.Fn2))
+ END
+ call v9.CheckSourceSuccess(lines)
+
+ " object variable with 'any" type. Can be assigned different types.
+ let lines =<< trim END
+ vim9script
+ def Foo(l: list<dict<number>>): dict<list<number>>
+ return {}
+ enddef
+ class A
+ public this.Fn: any = Foo
+ public this.Fn2: any
+
+ def Bar()
+ assert_equal('func(list<dict<number>>): dict<list<number>>', typename(this.Fn))
+ this.Fn = "abc"
+ assert_equal('string', typename(this.Fn))
+ this.Fn2 = Foo
+ assert_equal('func(list<dict<number>>): dict<list<number>>', typename(this.Fn2))
+ this.Fn2 = "xyz"
+ assert_equal('string', typename(this.Fn2))
+ enddef
+ endclass
+
+ var a = A.new()
+ test_garbagecollect_now()
+ a.Bar()
+ test_garbagecollect_now()
+ a.Fn = Foo
+ a.Bar()
+ END
+ call v9.CheckSourceSuccess(lines)
+
+ " object variable with 'any" type. Can be assigned different types.
+ let lines =<< trim END
+ vim9script
+ def Foo(l: list<dict<number>>): dict<list<number>>
+ return {}
+ enddef
+ class A
+ public this.Fn: any = Foo
+ public this.Fn2: any
+ endclass
+
+ def Bar()
+ var a = A.new()
+ assert_equal('func(list<dict<number>>): dict<list<number>>', typename(a.Fn))
+ a.Fn = "abc"
+ assert_equal('string', typename(a.Fn))
+ a.Fn2 = Foo
+ assert_equal('func(list<dict<number>>): dict<list<number>>', typename(a.Fn2))
+ a.Fn2 = "xyz"
+ assert_equal('string', typename(a.Fn2))
+ enddef
+ test_garbagecollect_now()
+ Bar()
+ test_garbagecollect_now()
+ Bar()
+ END
+ call v9.CheckSourceSuccess(lines)
+endfunc
+
" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker
char_u *varname,
int has_public, // TRUE if "public" seen before "varname"
char_u **varname_end,
+ int *has_type,
garray_T *type_list,
type_T **type_ret,
char_u **init_expr)
char_u *colon = skipwhite(*varname_end);
char_u *type_arg = colon;
type_T *type = NULL;
+ *has_type = FALSE;
if (*colon == ':')
{
if (VIM_ISWHITE(**varname_end))
type = parse_type(&type_arg, type_list, TRUE);
if (type == NULL)
return FAIL;
+ *has_type = TRUE;
}
char_u *init_arg = skipwhite(type_arg);
char_u *varname,
char_u *varname_end,
int has_public,
+ int has_type,
type_T *type,
char_u *init_expr)
{
m->ocm_name = vim_strnsave(varname, varname_end - varname);
m->ocm_access = has_public ? VIM_ACCESS_ALL
: *varname == '_' ? VIM_ACCESS_PRIVATE : VIM_ACCESS_READ;
+ m->ocm_has_type = has_type;
m->ocm_type = type;
if (init_expr != NULL)
m->ocm_init = init_expr;
static void
add_class_members(class_T *cl, exarg_T *eap)
{
+ garray_T type_list;
+
+ ga_init2(&type_list, sizeof(type_T *), 10);
+
// Allocate a typval for each class member and initialize it.
cl->class_members_tv = ALLOC_CLEAR_MULT(typval_T,
cl->class_class_member_count);
typval_T *etv = eval_expr(m->ocm_init, eap);
if (etv != NULL)
{
+ if (m->ocm_type->tt_type == VAR_ANY
+ && !m->ocm_has_type
+ && etv->v_type != VAR_SPECIAL)
+ // If the member variable type is not yet set, then use
+ // the initialization expression type.
+ m->ocm_type = typval2type(etv, get_copyID(), &type_list,
+ TVTT_DO_MEMBER|TVTT_MORE_SPECIFIC);
*tv = *etv;
vim_free(etv);
}
tv->vval.v_string = NULL;
}
}
+
+ clear_type_list(&type_list);
}
/*
char_u *varname_end = NULL;
type_T *type = NULL;
char_u *init_expr = NULL;
+ int has_type = FALSE;
if (!is_class && *varname == '_')
{
}
if (parse_member(eap, line, varname, has_public,
- &varname_end, &type_list, &type,
+ &varname_end, &has_type, &type_list, &type,
is_class ? &init_expr: NULL) == FAIL)
break;
if (is_reserved_varname(varname, varname_end))
break;
}
if (add_member(&objmembers, varname, varname_end,
- has_public, type, init_expr) == FAIL)
+ has_public, has_type, type, init_expr) == FAIL)
{
vim_free(init_expr);
break;
// "static _varname"
// "static varname"
// "public static varname"
- char_u *varname = p;
- char_u *varname_end = NULL;
- type_T *type = NULL;
- char_u *init_expr = NULL;
+ char_u *varname = p;
+ char_u *varname_end = NULL;
+ int has_type = FALSE;
+ type_T *type = NULL;
+ char_u *init_expr = NULL;
+
if (parse_member(eap, line, varname, has_public,
- &varname_end, &type_list, &type,
+ &varname_end, &has_type, &type_list, &type,
is_class ? &init_expr : NULL) == FAIL)
break;
if (is_reserved_varname(varname, varname_end))
break;
}
if (add_member(&classmembers, varname, varname_end,
- has_public, type, init_expr) == FAIL)
+ has_public, has_type, type, init_expr) == FAIL)
{
vim_free(init_expr);
break;
return m->ocm_type;
}
+/*
+ * Given a class or object variable index, return the variable type
+ */
+ type_T *
+class_member_type_by_idx(
+ class_T *cl,
+ int is_object,
+ int member_idx)
+{
+ ocmember_T *m;
+ int member_count;
+
+ if (is_object)
+ {
+ m = cl->class_obj_members;
+ member_count = cl->class_obj_member_count;
+ }
+ else
+ {
+ m = cl->class_class_members;
+ member_count = cl->class_class_member_count;
+ }
+
+ if (member_idx >= member_count)
+ return NULL;
+
+ return m[member_idx].ocm_type;
+}
+
/*
* Handle ":enum" up to ":endenum".
*/