summaryrefslogtreecommitdiffstats
path: root/src/vim9type.c
diff options
context:
space:
mode:
authorBram Moolenaar <Bram@vim.org>2022-12-13 21:14:28 +0000
committerBram Moolenaar <Bram@vim.org>2022-12-13 21:14:28 +0000
commit74e1274edf632b83d2948a2a2d519589eff52997 (patch)
tree3513a53175d76a294e5d189525480f276591a8e3 /src/vim9type.c
parent65b0d1676814ee08fb58ef8d64dd342d1d883192 (diff)
patch 9.0.1054: object member can't get type from initializerv9.0.1054
Problem: Object member can't get type from initializer. Solution: If there is no type specified try to use the type of the initializer. Check for a valid type.
Diffstat (limited to 'src/vim9type.c')
-rw-r--r--src/vim9type.c30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/vim9type.c b/src/vim9type.c
index 2d55cf2242..0709ce0437 100644
--- a/src/vim9type.c
+++ b/src/vim9type.c
@@ -425,6 +425,17 @@ typval2type_int(typval_T *tv, int copyID, garray_T *type_gap, int flags)
return &t_number;
if (tv->v_type == VAR_BOOL)
return &t_bool;
+ if (tv->v_type == VAR_SPECIAL)
+ {
+ if (tv->vval.v_number == VVAL_NULL)
+ return &t_null;
+ if (tv->vval.v_number == VVAL_NONE)
+ return &t_none;
+ if (tv->vval.v_number == VVAL_TRUE
+ || tv->vval.v_number == VVAL_TRUE)
+ return &t_bool;
+ return &t_unknown;
+ }
if (tv->v_type == VAR_STRING)
return &t_string;
if (tv->v_type == VAR_BLOB)
@@ -620,6 +631,25 @@ typval2type(typval_T *tv, int copyID, garray_T *type_gap, int flags)
}
/*
+ * Return TRUE if "type" can be used for a variable declaration.
+ * Give an error and return FALSE if not.
+ */
+ int
+valid_declaration_type(type_T *type)
+{
+ if (type->tt_type == VAR_SPECIAL // null, none
+ || type->tt_type == VAR_VOID)
+ {
+ char *tofree = NULL;
+ char *name = type_name(type, &tofree);
+ semsg(_(e_invalid_type_for_object_member_str), name);
+ vim_free(tofree);
+ return FALSE;
+ }
+ return TRUE;
+}
+
+/*
* Get a type_T for a typval_T, used for v: variables.
* "type_list" is used to temporarily create types in.
*/