Skip to content

Commit 6858587

Browse files
thincachrisbra
authored andcommitted
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]>
1 parent f3d0d08 commit 6858587

File tree

3 files changed

+53
-6
lines changed

3 files changed

+53
-6
lines changed

src/eval.c

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2236,13 +2236,33 @@ get_lval(
22362236

22372237
if (*p == '.')
22382238
{
2239-
imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, TRUE);
2240-
if (import != NULL)
2239+
// In legacy script, when a local variable and import exists with this name,
2240+
// prioritize local variable over imports to avoid conflicts.
2241+
int var_exists = FALSE;
2242+
if (!vim9script)
2243+
{
2244+
cc = *p;
2245+
*p = NUL;
2246+
hashtab_T *local_ht = get_funccal_local_ht();
2247+
if (local_ht != NULL)
2248+
{
2249+
hashitem_T *hi = hash_find(local_ht, lp->ll_name);
2250+
if (!HASHITEM_EMPTY(hi))
2251+
var_exists = TRUE;
2252+
}
2253+
*p = cc;
2254+
}
2255+
2256+
if (!var_exists)
22412257
{
2242-
p++; // skip '.'
2243-
p = get_lval_imported(lp, import->imp_sid, p, &v, fne_flags);
2244-
if (p == NULL)
2245-
return NULL;
2258+
imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, TRUE);
2259+
if (import != NULL)
2260+
{
2261+
p++; // skip '.'
2262+
p = get_lval_imported(lp, import->imp_sid, p, &v, fne_flags);
2263+
if (p == NULL)
2264+
return NULL;
2265+
}
22462266
}
22472267
}
22482268

src/testdir/test_vim9_import.vim

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3694,4 +3694,29 @@ def Test_import_member_initializer()
36943694
v9.CheckScriptSuccess(lines)
36953695
enddef
36963696

3697+
def Test_import_name_conflict_with_local_variable()
3698+
var lines =<< trim END
3699+
vim9script
3700+
3701+
export class Foo
3702+
def Method(): string
3703+
return 'Method'
3704+
enddef
3705+
endclass
3706+
END
3707+
writefile(lines, 'Xvim9.vim', 'D')
3708+
3709+
lines =<< trim END
3710+
import './Xvim9.vim'
3711+
3712+
function! s:Main() abort
3713+
let Xvim9 = s:Xvim9.Foo.new()
3714+
call assert_equal('Method', Xvim9.Method())
3715+
endfunction
3716+
3717+
call s:Main()
3718+
END
3719+
v9.CheckScriptSuccess(lines)
3720+
enddef
3721+
36973722
" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,8 @@ static char *(features[]) =
729729

730730
static int included_patches[] =
731731
{ /* Add new patch number below this line */
732+
/**/
733+
1828,
732734
/**/
733735
1827,
734736
/**/

0 commit comments

Comments
 (0)