diff --git a/src/eval.c b/src/eval.c index 1b522d37c4..481b505a2e 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) { - p++; // skip '.' - p = get_lval_imported(lp, import->imp_sid, p, &v, fne_flags); - if (p == NULL) - return NULL; + 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) + { + 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 191008ecbe..a3bb60ccb1 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 81368c1c9e..d6b798e898 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, /**/