patch 9.1.1828: local variables shadowed by import names
Commit:
https://github.com/vim/vim/commit/68585877610ac46ac27a911165d0ecb4fe181a04
Author: thinca <[email protected]>
Date: Sun Oct 5 13:27:26 2025 +0000
patch 9.1.1828: local variables shadowed by import names
Problem: local variables shadowed by import names
Solution: Check if a local variable exists before handling imports
(thinca)
closes: #18480
Signed-off-by: thinca <[email protected]>
Signed-off-by: Christian Brabandt <[email protected]>
diff --git a/src/eval.c b/src/eval.c
index 1b522d37c..481b505a2 100644
--- a/src/eval.c
+++ b/src/eval.c
@@ -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;
+ }
}
}
diff --git a/src/testdir/test_vim9_import.vim b/src/testdir/test_vim9_import.vim
index 191008ecb..a3bb60ccb 100644
--- a/src/testdir/test_vim9_import.vim
+++ b/src/testdir/test_vim9_import.vim
@@ -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
diff --git a/src/version.c b/src/version.c
index 81368c1c9..d6b798e89 100644
--- a/src/version.c
+++ b/src/version.c
@@ -729,6 +729,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
+/**/
+ 1828,
/**/
1827,
/**/
--
--
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
---
You received this message because you are subscribed to the Google Groups
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion visit
https://groups.google.com/d/msgid/vim_dev/E1v5P2v-00DR71-40%40256bit.org.