]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 9.1.1828: local variables shadowed by import names v9.1.1828
authorthinca <thinca@gmail.com>
Sun, 5 Oct 2025 13:27:26 +0000 (13:27 +0000)
committerChristian Brabandt <cb@256bit.org>
Sun, 5 Oct 2025 13:27:26 +0000 (13:27 +0000)
Problem:  local variables shadowed by import names
Solution: Check if a local variable exists before handling imports
          (thinca)

closes: #18480

Signed-off-by: thinca <thinca@gmail.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
src/eval.c
src/testdir/test_vim9_import.vim
src/version.c

index 1b522d37c4463f8766cbbcca8e44f5c934a93c8f..481b505a2ed13212244a482bb50fe117d3398ef3 100644 (file)
@@ -2236,13 +2236,33 @@ get_lval(
 
     if (*p == '.')
     {
-       imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, TRUE);
-       if (import != NULL)
+       // In legacy script, when a local variable and import exists with this name,
+       // prioritize local variable over imports to avoid conflicts.
+       int var_exists = FALSE;
+       if (!vim9script)
+       {
+           cc = *p;
+           *p = NUL;
+           hashtab_T *local_ht = get_funccal_local_ht();
+           if (local_ht != NULL)
+           {
+               hashitem_T *hi = hash_find(local_ht, lp->ll_name);
+               if (!HASHITEM_EMPTY(hi))
+                   var_exists = TRUE;
+           }
+           *p = cc;
+       }
+
+       if (!var_exists)
        {
-           p++;        // skip '.'
-           p = get_lval_imported(lp, import->imp_sid, p, &v, fne_flags);
-           if (p == NULL)
-               return NULL;
+           imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, TRUE);
+           if (import != NULL)
+           {
+               p++;    // skip '.'
+               p = get_lval_imported(lp, import->imp_sid, p, &v, fne_flags);
+               if (p == NULL)
+                   return NULL;
+           }
        }
     }
 
index 191008ecbe3b4e329f225e516966475f8c94a8ff..a3bb60ccb184f501a0cad56c12c0909e5c1bfe2f 100644 (file)
@@ -3694,4 +3694,29 @@ def Test_import_member_initializer()
   v9.CheckScriptSuccess(lines)
 enddef
 
+def Test_import_name_conflict_with_local_variable()
+  var lines =<< trim END
+    vim9script
+
+    export class Foo
+      def Method(): string
+        return 'Method'
+      enddef
+    endclass
+  END
+  writefile(lines, 'Xvim9.vim', 'D')
+
+  lines =<< trim END
+    import './Xvim9.vim'
+
+    function! s:Main() abort
+      let Xvim9 = s:Xvim9.Foo.new()
+      call assert_equal('Method', Xvim9.Method())
+    endfunction
+
+    call s:Main()
+  END
+  v9.CheckScriptSuccess(lines)
+enddef
+
 " vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker
index 81368c1c9e93b9509c8543c20033cb5296cc6384..d6b798e898054fd84aa8d3d4cf43b3a008333ceb 100644 (file)
@@ -729,6 +729,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1828,
 /**/
     1827,
 /**/