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;
+ }
}
}
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