Skip to content

Commit bf7c88d

Browse files
h-eastchrisbra
authored andcommitted
patch 9.1.1111: Vim9: variable not found in transitive import
Problem: Vim9: variable not found in transitive import (lifepillar) Solution: fix import and class extends (Hirohito Higashi) fixes: #16379 related: #16440 closes: #16602 Signed-off-by: Hirohito Higashi <[email protected]> Signed-off-by: Yegappan Lakshmanan <[email protected]> Signed-off-by: Christian Brabandt <[email protected]>
1 parent e70587d commit bf7c88d

6 files changed

Lines changed: 98 additions & 17 deletions

File tree

src/proto/vim9compile.pro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ int need_type(type_T *actual, type_T *expected, int number_ok, int offset, int a
1212
lvar_T *reserve_local(cctx_T *cctx, char_u *name, size_t len, int assign, type_T *type);
1313
int get_script_item_idx(int sid, char_u *name, int check_writable, cctx_T *cctx, cstack_T *cstack);
1414
imported_T *find_imported(char_u *name, size_t len, int load);
15+
imported_T *find_imported_from_extends(cctx_T *cctx, char_u *name, size_t len, int load);
1516
char_u *may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp);
1617
char_u *peek_next_line_from_context(cctx_T *cctx);
1718
char_u *next_line_from_context(cctx_T *cctx, int skip_comment);

src/proto/vim9expr.pro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
int generate_ppconst(cctx_T *cctx, ppconst_T *ppconst);
33
void clear_ppconst(ppconst_T *ppconst);
44
int compile_member(int is_slice, int *keeping_dict, cctx_T *cctx);
5-
int compile_load_scriptvar(cctx_T *cctx, char_u *name, char_u *start, char_u **end);
5+
int compile_load_scriptvar(cctx_T *cctx, char_u *name, char_u *start, char_u **end, imported_T *import);
66
int compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int is_expr, int error);
77
int compile_arguments(char_u **arg, cctx_T *cctx, int *argcount, ca_special_T special_fn);
88
char_u *to_name_end(char_u *arg, int use_namespace);

src/testdir/test_vim9_import.vim

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3457,4 +3457,41 @@ def Test_vim9_import_and_class_extends()
34573457
v9.CheckScriptFailure(lines, 'E1376: Object variable "value2" accessible only using class "Run" object', 2)
34583458
enddef
34593459

3460+
" Test for import and class extends
3461+
def Test_vim9_import_and_class_extends_2()
3462+
mkdir('import', 'R')
3463+
var save_rtp = &rtp
3464+
&rtp = getcwd()
3465+
3466+
var lines =<< trim END
3467+
vim9script
3468+
export class Property
3469+
public var value: string
3470+
endclass
3471+
END
3472+
writefile(lines, './import/libproperty.vim')
3473+
3474+
lines =<< trim END
3475+
vim9script
3476+
import 'libproperty.vim'
3477+
export class View
3478+
var _content = libproperty.Property.new('')
3479+
endclass
3480+
END
3481+
writefile(lines, './import/libview.vim')
3482+
3483+
lines =<< trim END
3484+
vim9script
3485+
import 'libview.vim'
3486+
class MyView extends libview.View
3487+
def new(value: string)
3488+
this._content.value = value
3489+
enddef
3490+
endclass
3491+
var myView = MyView.new('This should be ok')
3492+
END
3493+
v9.CheckScriptSuccess(lines)
3494+
&rtp = save_rtp
3495+
enddef
3496+
34603497
" 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
@@ -704,6 +704,8 @@ static char *(features[]) =
704704

705705
static int included_patches[] =
706706
{ /* Add new patch number below this line */
707+
/**/
708+
1111,
707709
/**/
708710
1110,
709711
/**/

src/vim9compile.c

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,36 @@ find_imported(char_u *name, size_t len, int load)
836836
return ret;
837837
}
838838

839+
/*
840+
* Find "name" in imported items of extended base class of the class to which
841+
* the context :def function belongs.
842+
*/
843+
imported_T *
844+
find_imported_from_extends(cctx_T *cctx, char_u *name, size_t len, int load)
845+
{
846+
imported_T *ret = NULL;
847+
class_T *cl_extends;
848+
849+
if (cctx == NULL || cctx->ctx_ufunc == NULL
850+
|| cctx->ctx_ufunc->uf_class == NULL)
851+
return NULL;
852+
853+
cl_extends = cctx->ctx_ufunc->uf_class->class_extends;
854+
855+
if (cl_extends == NULL || cl_extends->class_class_function_count_child <= 0)
856+
return NULL;
857+
else
858+
{
859+
sctx_T current_sctx_save = current_sctx;
860+
861+
current_sctx = cl_extends->class_class_functions[0]->uf_script_ctx;
862+
ret = find_imported(name, len, load);
863+
current_sctx = current_sctx_save;
864+
865+
return ret;
866+
}
867+
}
868+
839869
/*
840870
* Called when checking for a following operator at "arg". When the rest of
841871
* the line is empty or only a comment, peek the next line. If there is a next
@@ -1374,7 +1404,7 @@ generate_loadvar(cctx_T *cctx, lhs_T *lhs)
13741404
case dest_script:
13751405
case dest_script_v9:
13761406
res = compile_load_scriptvar(cctx,
1377-
name + (name[1] == ':' ? 2 : 0), NULL, NULL);
1407+
name + (name[1] == ':' ? 2 : 0), NULL, NULL, NULL);
13781408
break;
13791409
case dest_env:
13801410
// Include $ in the name here

src/vim9expr.c

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -538,11 +538,12 @@ compile_load_scriptvar(
538538
cctx_T *cctx,
539539
char_u *name, // variable NUL terminated
540540
char_u *start, // start of variable
541-
char_u **end) // end of variable, may be NULL
541+
char_u **end, // end of variable, may be NULL
542+
imported_T *import) // found imported item, can be NULL
542543
{
543544
scriptitem_T *si;
544545
int idx;
545-
imported_T *import;
546+
imported_T *imp;
546547

547548
if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
548549
return FAIL;
@@ -557,8 +558,14 @@ compile_load_scriptvar(
557558
return OK;
558559
}
559560

560-
import = end == NULL ? NULL : find_imported(name, 0, FALSE);
561-
if (import != NULL)
561+
if (end == NULL)
562+
imp = NULL;
563+
else if (import == NULL)
564+
imp = find_imported(name, 0, FALSE);
565+
else
566+
imp = import;
567+
568+
if (imp != NULL)
562569
{
563570
char_u *p = skipwhite(*end);
564571
char_u *exp_name;
@@ -568,8 +575,8 @@ compile_load_scriptvar(
568575
int done = FALSE;
569576
int res = OK;
570577

571-
check_script_symlink(import->imp_sid);
572-
import_check_sourced_sid(&import->imp_sid);
578+
check_script_symlink(imp->imp_sid);
579+
import_check_sourced_sid(&imp->imp_sid);
573580

574581
// Need to lookup the member.
575582
if (*p != '.')
@@ -591,11 +598,11 @@ compile_load_scriptvar(
591598
cc = *p;
592599
*p = NUL;
593600

594-
si = SCRIPT_ITEM(import->imp_sid);
601+
si = SCRIPT_ITEM(imp->imp_sid);
595602
if (si->sn_import_autoload && si->sn_state == SN_STATE_NOT_LOADED)
596603
// "import autoload './dir/script.vim'" or
597604
// "import autoload './autoload/script.vim'" - load script first
598-
res = generate_SOURCE(cctx, import->imp_sid);
605+
res = generate_SOURCE(cctx, imp->imp_sid);
599606

600607
if (res == OK)
601608
{
@@ -624,17 +631,17 @@ compile_load_scriptvar(
624631
{
625632
char_u sid_name[MAX_FUNC_NAME_LEN];
626633

627-
func_name_with_sid(exp_name, import->imp_sid, sid_name);
634+
func_name_with_sid(exp_name, imp->imp_sid, sid_name);
628635
res = generate_PUSHFUNC(cctx, sid_name, &t_func_any, TRUE);
629636
}
630637
else
631638
res = generate_OLDSCRIPT(cctx, ISN_LOADEXPORT, exp_name,
632-
import->imp_sid, &t_any);
639+
imp->imp_sid, &t_any);
633640
done = TRUE;
634641
}
635642
else
636643
{
637-
idx = find_exported(import->imp_sid, exp_name, &ufunc, &type,
644+
idx = find_exported(imp->imp_sid, exp_name, &ufunc, &type,
638645
cctx, NULL, TRUE);
639646
}
640647
}
@@ -656,7 +663,7 @@ compile_load_scriptvar(
656663
}
657664

658665
generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
659-
import->imp_sid,
666+
imp->imp_sid,
660667
idx,
661668
type);
662669
return OK;
@@ -751,7 +758,7 @@ compile_load(
751758
res = generate_funcref(cctx, name, FALSE);
752759
else
753760
res = compile_load_scriptvar(cctx, name,
754-
NULL, &end);
761+
NULL, &end, NULL);
755762
break;
756763
case 'g': if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
757764
{
@@ -869,11 +876,15 @@ compile_load(
869876
}
870877
else
871878
{
879+
imported_T *imp = NULL;
880+
872881
// "var" can be script-local even without using "s:" if it
873882
// already exists in a Vim9 script or when it's imported.
874883
if (script_var_exists(*arg, len, cctx, NULL) == OK
875-
|| find_imported(name, 0, FALSE) != NULL)
876-
res = compile_load_scriptvar(cctx, name, *arg, &end);
884+
|| (imp = find_imported(name, 0, FALSE)) != NULL
885+
|| (imp = find_imported_from_extends(cctx, name, 0, FALSE))
886+
!= NULL)
887+
res = compile_load_scriptvar(cctx, name, *arg, &end, imp);
877888

878889
// When evaluating an expression and the name starts with an
879890
// uppercase letter it can be a user defined function.

0 commit comments

Comments
 (0)