]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 9.1.1111: Vim9: variable not found in transitive import v9.1.1111
authorHirohito Higashi <h.east.727@gmail.com>
Thu, 13 Feb 2025 20:04:07 +0000 (21:04 +0100)
committerChristian Brabandt <cb@256bit.org>
Thu, 13 Feb 2025 20:04:07 +0000 (21:04 +0100)
Problem:  Vim9: variable not found in transitive import
          (lifepillar)
Solution: fix import and class extends (Hirohito Higashi)

fixes: #16379
related: #16440
closes: #16602

Signed-off-by: Hirohito Higashi <h.east.727@gmail.com>
Signed-off-by: Yegappan Lakshmanan <yegappan@yahoo.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
src/proto/vim9compile.pro
src/proto/vim9expr.pro
src/testdir/test_vim9_import.vim
src/version.c
src/vim9compile.c
src/vim9expr.c

index 080a2ffaaf015daac880a68098040d8d72cd803b..c918aead254a763033dcbc27001c83c20e6afd22 100644 (file)
@@ -12,6 +12,7 @@ int need_type(type_T *actual, type_T *expected, int number_ok, int offset, int a
 lvar_T *reserve_local(cctx_T *cctx, char_u *name, size_t len, int assign, type_T *type);
 int get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx, cstack_T *cstack);
 imported_T *find_imported(char_u *name, size_t len, int load);
+imported_T *find_imported_from_extends(cctx_T *cctx, char_u *name, size_t len, int load);
 char_u *may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp);
 char_u *peek_next_line_from_context(cctx_T *cctx);
 char_u *next_line_from_context(cctx_T *cctx, int skip_comment);
index f58e3462526e096c1c9580fdaaac275c24696be4..d28908c40019de6c37141c31ba5fbc2a20c81965 100644 (file)
@@ -2,7 +2,7 @@
 int generate_ppconst(cctx_T *cctx, ppconst_T *ppconst);
 void clear_ppconst(ppconst_T *ppconst);
 int compile_member(int is_slice, int *keeping_dict, cctx_T *cctx);
-int compile_load_scriptvar(cctx_T *cctx, char_u *name, char_u *start, char_u **end);
+int compile_load_scriptvar(cctx_T *cctx, char_u *name, char_u *start, char_u **end, imported_T *import);
 int compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int is_expr, int error);
 int compile_arguments(char_u **arg, cctx_T *cctx, int *argcount, ca_special_T special_fn);
 char_u *to_name_end(char_u *arg, int use_namespace);
index 5f5c928b6dbb98cbbc452af95e8185fc483c7d7f..c92aed065b06e819a8df747c60a65b691774328b 100644 (file)
@@ -3457,4 +3457,41 @@ def Test_vim9_import_and_class_extends()
   v9.CheckScriptFailure(lines, 'E1376: Object variable "value2" accessible only using class "Run" object', 2)
 enddef
 
+" Test for import and class extends
+def Test_vim9_import_and_class_extends_2()
+  mkdir('import', 'R')
+  var save_rtp = &rtp
+  &rtp = getcwd()
+
+  var lines =<< trim END
+    vim9script
+    export class Property
+      public var value: string
+    endclass
+  END
+  writefile(lines, './import/libproperty.vim')
+
+  lines =<< trim END
+    vim9script
+    import 'libproperty.vim'
+    export class View
+      var _content = libproperty.Property.new('')
+    endclass
+  END
+  writefile(lines, './import/libview.vim')
+
+  lines =<< trim END
+    vim9script
+    import 'libview.vim'
+    class MyView extends libview.View
+      def new(value: string)
+        this._content.value = value
+      enddef
+    endclass
+    var myView = MyView.new('This should be ok')
+  END
+  v9.CheckScriptSuccess(lines)
+  &rtp = save_rtp
+enddef
+
 " vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker
index 85110f83f03f0e0c50d8ff2feb24ae5c31c583e0..cbd973621ae384f9a78797ee68a812bfa35e9f12 100644 (file)
@@ -704,6 +704,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1111,
 /**/
     1110,
 /**/
index be26c9bad956a602af782ad3a4a8a90bb7020976..42a30b192681dec69a046bba5175dde26aef9e6b 100644 (file)
@@ -836,6 +836,36 @@ find_imported(char_u *name, size_t len, int load)
     return ret;
 }
 
+/*
+ * Find "name" in imported items of extended base class of the class to which
+ * the context :def function belongs.
+ */
+    imported_T *
+find_imported_from_extends(cctx_T *cctx, char_u *name, size_t len, int load)
+{
+    imported_T *ret = NULL;
+    class_T    *cl_extends;
+
+    if (cctx == NULL || cctx->ctx_ufunc == NULL
+                                       || cctx->ctx_ufunc->uf_class == NULL)
+       return NULL;
+
+    cl_extends = cctx->ctx_ufunc->uf_class->class_extends;
+
+    if (cl_extends == NULL || cl_extends->class_class_function_count_child <= 0)
+       return NULL;
+    else
+    {
+       sctx_T current_sctx_save = current_sctx;
+
+       current_sctx = cl_extends->class_class_functions[0]->uf_script_ctx;
+       ret = find_imported(name, len, load);
+       current_sctx = current_sctx_save;
+
+       return ret;
+    }
+}
+
 /*
  * Called when checking for a following operator at "arg".  When the rest of
  * the line is empty or only a comment, peek the next line.  If there is a next
@@ -1374,7 +1404,7 @@ generate_loadvar(cctx_T *cctx, lhs_T *lhs)
        case dest_script:
        case dest_script_v9:
            res = compile_load_scriptvar(cctx,
-                                 name + (name[1] == ':' ? 2 : 0), NULL, NULL);
+                           name + (name[1] == ':' ? 2 : 0), NULL, NULL, NULL);
            break;
        case dest_env:
            // Include $ in the name here
index 58ff395f9e5bf97b3bb9a676e34712e9bafb0a38..fe2be410bad73c972a84c3a07800585d489b3c66 100644 (file)
@@ -538,11 +538,12 @@ compile_load_scriptvar(
        cctx_T *cctx,
        char_u *name,       // variable NUL terminated
        char_u *start,      // start of variable
-       char_u **end)       // end of variable, may be NULL
+       char_u **end,       // end of variable, may be NULL
+       imported_T *import) // found imported item, can be NULL
 {
     scriptitem_T    *si;
     int                    idx;
-    imported_T     *import;
+    imported_T     *imp;
 
     if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
        return FAIL;
@@ -557,8 +558,14 @@ compile_load_scriptvar(
        return OK;
     }
 
-    import = end == NULL ? NULL : find_imported(name, 0, FALSE);
-    if (import != NULL)
+    if (end == NULL)
+       imp = NULL;
+    else if (import == NULL)
+       imp = find_imported(name, 0, FALSE);
+    else
+       imp = import;
+
+    if (imp != NULL)
     {
        char_u  *p = skipwhite(*end);
        char_u  *exp_name;
@@ -568,8 +575,8 @@ compile_load_scriptvar(
        int     done = FALSE;
        int     res = OK;
 
-       check_script_symlink(import->imp_sid);
-       import_check_sourced_sid(&import->imp_sid);
+       check_script_symlink(imp->imp_sid);
+       import_check_sourced_sid(&imp->imp_sid);
 
        // Need to lookup the member.
        if (*p != '.')
@@ -591,11 +598,11 @@ compile_load_scriptvar(
        cc = *p;
        *p = NUL;
 
-       si = SCRIPT_ITEM(import->imp_sid);
+       si = SCRIPT_ITEM(imp->imp_sid);
        if (si->sn_import_autoload && si->sn_state == SN_STATE_NOT_LOADED)
            // "import autoload './dir/script.vim'" or
            // "import autoload './autoload/script.vim'" - load script first
-           res = generate_SOURCE(cctx, import->imp_sid);
+           res = generate_SOURCE(cctx, imp->imp_sid);
 
        if (res == OK)
        {
@@ -624,17 +631,17 @@ compile_load_scriptvar(
                {
                    char_u sid_name[MAX_FUNC_NAME_LEN];
 
-                   func_name_with_sid(exp_name, import->imp_sid, sid_name);
+                   func_name_with_sid(exp_name, imp->imp_sid, sid_name);
                    res = generate_PUSHFUNC(cctx, sid_name, &t_func_any, TRUE);
                }
                else
                    res = generate_OLDSCRIPT(cctx, ISN_LOADEXPORT, exp_name,
-                                                     import->imp_sid, &t_any);
+                                                     imp->imp_sid, &t_any);
                done = TRUE;
            }
            else
            {
-               idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
+               idx = find_exported(imp->imp_sid, exp_name, &ufunc, &type,
                                                             cctx, NULL, TRUE);
            }
        }
@@ -656,7 +663,7 @@ compile_load_scriptvar(
        }
 
        generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
-               import->imp_sid,
+               imp->imp_sid,
                idx,
                type);
        return OK;
@@ -751,7 +758,7 @@ compile_load(
                              res = generate_funcref(cctx, name, FALSE);
                          else
                              res = compile_load_scriptvar(cctx, name,
-                                                                  NULL, &end);
+                                                           NULL, &end, NULL);
                          break;
                case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
                          {
@@ -869,11 +876,15 @@ compile_load(
            }
            else
            {
+               imported_T *imp = NULL;
+
                // "var" can be script-local even without using "s:" if it
                // already exists in a Vim9 script or when it's imported.
                if (script_var_exists(*arg, len, cctx, NULL) == OK
-                                     || find_imported(name, 0, FALSE) != NULL)
-                  res = compile_load_scriptvar(cctx, name, *arg, &end);
+                   || (imp = find_imported(name, 0, FALSE)) != NULL
+                   || (imp = find_imported_from_extends(cctx, name, 0, FALSE))
+                                                                   != NULL)
+                  res = compile_load_scriptvar(cctx, name, *arg, &end, imp);
 
                // When evaluating an expression and the name starts with an
                // uppercase letter it can be a user defined function.