diff src/evalvars.c @ 33678:7d9d2404a3d4 v9.0.2076

patch 9.0.2076: Vim9: No support for type aliases Commit: https://github.com/vim/vim/commit/ec3cebbd2b6b7583d2f683f5e66345163ec122aa Author: Yegappan Lakshmanan <yegappan@yahoo.com> Date: Fri Oct 27 19:35:26 2023 +0200 patch 9.0.2076: Vim9: No support for type aliases Problem: Vim9: No support for type aliases Solution: Implement :type command A type definition is giving a name to a type specification. This also known type alias. :type ListOfStrings = list<string> The type alias can be used wherever a built-in type can be used. The type alias name must start with an upper case character. closes: #13407 Signed-off-by: Christian Brabandt <cb@256bit.org> Signed-off-by: Yegappan Lakshmanan <yegappan@yahoo.com>
author Christian Brabandt <cb@256bit.org>
date Fri, 27 Oct 2023 19:45:05 +0200
parents 3de472480e91
children f126ffc85f7c
line wrap: on
line diff
--- a/src/evalvars.c
+++ b/src/evalvars.c
@@ -158,6 +158,7 @@ static struct vimvar
     {VV_NAME("sizeofpointer",	 VAR_NUMBER), NULL, VV_RO},
     {VV_NAME("maxcol",		 VAR_NUMBER), NULL, VV_RO},
     {VV_NAME("python3_version",	 VAR_NUMBER), NULL, VV_RO},
+    {VV_NAME("t_typealias",	 VAR_NUMBER), NULL, VV_RO},
 };
 
 // shorthand
@@ -260,6 +261,7 @@ evalvars_init(void)
     set_vim_var_nr(VV_TYPE_BLOB,    VAR_TYPE_BLOB);
     set_vim_var_nr(VV_TYPE_CLASS,   VAR_TYPE_CLASS);
     set_vim_var_nr(VV_TYPE_OBJECT,  VAR_TYPE_OBJECT);
+    set_vim_var_nr(VV_TYPE_TYPEALIAS,  VAR_TYPE_TYPEALIAS);
 
     set_vim_var_nr(VV_ECHOSPACE,    sc_col - 1);
 
@@ -1834,6 +1836,12 @@ ex_let_one(
 	return NULL;
     }
 
+    if (tv->v_type == VAR_TYPEALIAS)
+    {
+	semsg(_(e_using_typealias_as_variable), tv->vval.v_typealias->ta_name);
+	return NULL;
+    }
+
     if (*arg == '$')
     {
 	// ":let $VAR = expr": Set environment variable.
@@ -2331,6 +2339,7 @@ item_lock(typval_T *tv, int deep, int lo
 	case VAR_INSTR:
 	case VAR_CLASS:
 	case VAR_OBJECT:
+	case VAR_TYPEALIAS:
 	    break;
 
 	case VAR_BLOB:
@@ -2998,7 +3007,7 @@ eval_variable(
     }
 
     // Check for local variable when debugging.
-    if ((tv = lookup_debug_var(name)) == NULL)
+    if ((sid == 0) && (tv = lookup_debug_var(name)) == NULL)
     {
 	// Check for user-defined variables.
 	dictitem_T	*v = find_var(name, &ht, flags & EVAL_VAR_NOAUTOLOAD);
@@ -3114,6 +3123,25 @@ eval_variable(
 		}
 	    }
 
+	    if ((tv->v_type == VAR_TYPEALIAS || tv->v_type == VAR_CLASS)
+		    && sid != 0)
+	    {
+		// type alias or class imported from another script.  Check
+		// whether it is exported from the other script.
+		sv = find_typval_in_script(tv, sid, TRUE);
+		if (sv == NULL)
+		{
+		    ret = FAIL;
+		    goto done;
+		}
+		if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
+		{
+		    semsg(_(e_item_not_exported_in_script_str), name);
+		    ret = FAIL;
+		    goto done;
+		}
+	    }
+
 	    // If a list or dict variable wasn't initialized and has meaningful
 	    // type, do it now.  Not for global variables, they are not
 	    // declared.
@@ -3162,6 +3190,7 @@ eval_variable(
 	}
     }
 
+done:
     if (len > 0)
 	name[len] = cc;
 
@@ -3948,6 +3977,14 @@ set_var_const(
 		goto failed;
 	    }
 
+	    if (di->di_tv.v_type == VAR_TYPEALIAS)
+	    {
+		semsg(_(e_using_typealias_as_variable),
+					    di->di_tv.vval.v_typealias->ta_name);
+		clear_tv(&di->di_tv);
+		goto failed;
+	    }
+
 	    if (var_in_vim9script && (flags & ASSIGN_FOR_LOOP) == 0)
 	    {
 		where_T where = WHERE_INIT;