0
0
mirror of https://github.com/vim/vim.git synced 2025-09-25 03:54:15 -04:00

patch 8.2.3129: Vim9: imported uninitialized list does not get type checked

Problem:    Vim9: imported uninitialized list does not get type checked.
Solution:   Get type from imported variable.
This commit is contained in:
Bram Moolenaar
2021-07-08 21:38:50 +02:00
parent f055d45023
commit c967d57aa9
7 changed files with 29 additions and 20 deletions

View File

@@ -959,7 +959,7 @@ get_lval(
&& lp->ll_tv == &v->di_tv && lp->ll_tv == &v->di_tv
&& ht != NULL && ht == get_script_local_ht()) && ht != NULL && ht == get_script_local_ht())
{ {
svar_T *sv = find_typval_in_script(lp->ll_tv, TRUE); svar_T *sv = find_typval_in_script(lp->ll_tv);
// Vim9 script local variable: get the type // Vim9 script local variable: get the type
if (sv != NULL) if (sv != NULL)

View File

@@ -2564,9 +2564,9 @@ eval_variable(
int ret = OK; int ret = OK;
typval_T *tv = NULL; typval_T *tv = NULL;
int found = FALSE; int found = FALSE;
dictitem_T *v;
hashtab_T *ht = NULL; hashtab_T *ht = NULL;
int cc; int cc;
type_T *type = NULL;
// truncate the name, so that we can use strcmp() // truncate the name, so that we can use strcmp()
cc = name[len]; cc = name[len];
@@ -2576,13 +2576,16 @@ eval_variable(
if ((tv = lookup_debug_var(name)) == NULL) if ((tv = lookup_debug_var(name)) == NULL)
{ {
// Check for user-defined variables. // Check for user-defined variables.
v = find_var(name, &ht, flags & EVAL_VAR_NOAUTOLOAD); dictitem_T *v = find_var(name, &ht, flags & EVAL_VAR_NOAUTOLOAD);
if (v != NULL) if (v != NULL)
{ {
tv = &v->di_tv; tv = &v->di_tv;
if (dip != NULL) if (dip != NULL)
*dip = v; *dip = v;
} }
else
ht = NULL;
} }
if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0)) if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
@@ -2628,6 +2631,7 @@ eval_variable(
svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
+ import->imp_var_vals_idx; + import->imp_var_vals_idx;
tv = sv->sv_tv; tv = sv->sv_tv;
type = sv->sv_type;
} }
} }
else if (in_vim9script()) else if (in_vim9script())
@@ -2656,13 +2660,10 @@ eval_variable(
} }
else if (rettv != NULL) else if (rettv != NULL)
{ {
type_T *type = NULL;
if (ht != NULL && ht == get_script_local_ht()) if (ht != NULL && ht == get_script_local_ht())
{ {
svar_T *sv = find_typval_in_script(tv, FALSE); svar_T *sv = find_typval_in_script(tv);
// TODO: check imported variable
if (sv != NULL) if (sv != NULL)
type = sv->sv_type; type = sv->sv_type;
} }

View File

@@ -16,7 +16,7 @@ char_u *vim9_declare_scriptvar(exarg_T *eap, char_u *arg);
void update_vim9_script_var(int create, dictitem_T *di, int flags, typval_T *tv, type_T **type, int do_member); void update_vim9_script_var(int create, dictitem_T *di, int flags, typval_T *tv, type_T **type, int do_member);
void hide_script_var(scriptitem_T *si, int idx, int func_defined); void hide_script_var(scriptitem_T *si, int idx, int func_defined);
void free_all_script_vars(scriptitem_T *si); void free_all_script_vars(scriptitem_T *si);
svar_T *find_typval_in_script(typval_T *dest, int give_error); svar_T *find_typval_in_script(typval_T *dest);
int check_script_var_type(typval_T *dest, typval_T *value, char_u *name, where_T where); int check_script_var_type(typval_T *dest, typval_T *value, char_u *name, where_T where);
int check_reserved_name(char_u *name); int check_reserved_name(char_u *name);
/* vim: set ft=c : */ /* vim: set ft=c : */

View File

@@ -1391,6 +1391,7 @@ def Test_import_as()
vim9script vim9script
export var one = 1 export var one = 1
export var yes = 'yes' export var yes = 'yes'
export var slist: list<string>
END END
writefile(export_lines, 'XexportAs') writefile(export_lines, 'XexportAs')
@@ -1415,6 +1416,13 @@ def Test_import_as()
END END
CheckScriptSuccess(import_lines) CheckScriptSuccess(import_lines)
import_lines =<< trim END
vim9script
import {slist as impSlist} from './XexportAs'
impSlist->add(123)
END
CheckScriptFailure(import_lines, 'E1012: Type mismatch; expected string but got number')
delete('XexportAs') delete('XexportAs')
enddef enddef
@@ -1947,8 +1955,8 @@ def Test_import_rtp()
'g:imported_rtp = exported', 'g:imported_rtp = exported',
] ]
writefile(import_lines, 'Ximport_rtp.vim') writefile(import_lines, 'Ximport_rtp.vim')
mkdir('import') mkdir('Ximport')
writefile(s:export_script_lines, 'import/Xexport_rtp.vim') writefile(s:export_script_lines, 'Ximport/Xexport_rtp.vim')
var save_rtp = &rtp var save_rtp = &rtp
&rtp = getcwd() &rtp = getcwd()
@@ -1960,7 +1968,7 @@ def Test_import_rtp()
Undo_export_script_lines() Undo_export_script_lines()
unlet g:imported_rtp unlet g:imported_rtp
delete('Ximport_rtp.vim') delete('Ximport_rtp.vim')
delete('import', 'rf') delete('Ximport', 'rf')
enddef enddef
def Test_import_compile_error() def Test_import_compile_error()

View File

@@ -1512,7 +1512,7 @@ deref_func_name(
{ {
if (type != NULL && ht == get_script_local_ht()) if (type != NULL && ht == get_script_local_ht())
{ {
svar_T *sv = find_typval_in_script(&v->di_tv, TRUE); svar_T *sv = find_typval_in_script(&v->di_tv);
if (sv != NULL) if (sv != NULL)
*type = sv->sv_type; *type = sv->sv_type;

View File

@@ -755,6 +755,8 @@ static char *(features[]) =
static int included_patches[] = static int included_patches[] =
{ /* Add new patch number below this line */ { /* Add new patch number below this line */
/**/
3129,
/**/ /**/
3128, 3128,
/**/ /**/

View File

@@ -616,7 +616,7 @@ handle_import(
if (idx < 0 && ufunc == NULL) if (idx < 0 && ufunc == NULL)
goto erret; goto erret;
// If already imported with the same propertis and the // If already imported with the same properties and the
// IMP_FLAGS_RELOAD set then we keep that entry. Otherwise create // IMP_FLAGS_RELOAD set then we keep that entry. Otherwise create
// a new one (and give an error for an existing import). // a new one (and give an error for an existing import).
imported = find_imported(name, len, cctx); imported = find_imported(name, len, cctx);
@@ -806,7 +806,7 @@ update_vim9_script_var(
} }
else else
{ {
sv = find_typval_in_script(&di->di_tv, TRUE); sv = find_typval_in_script(&di->di_tv);
} }
if (sv != NULL) if (sv != NULL)
{ {
@@ -922,11 +922,10 @@ free_all_script_vars(scriptitem_T *si)
/* /*
* Find the script-local variable that links to "dest". * Find the script-local variable that links to "dest".
* Returns NULL if not found and when "give_error" is TRUE this is considered * Returns NULL if not found and give an internal error.
* an internal error.
*/ */
svar_T * svar_T *
find_typval_in_script(typval_T *dest, int give_error) find_typval_in_script(typval_T *dest)
{ {
scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid); scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
int idx; int idx;
@@ -945,7 +944,6 @@ find_typval_in_script(typval_T *dest, int give_error)
if (sv->sv_name != NULL && sv->sv_tv == dest) if (sv->sv_name != NULL && sv->sv_tv == dest)
return sv; return sv;
} }
if (give_error)
iemsg("find_typval_in_script(): not found"); iemsg("find_typval_in_script(): not found");
return NULL; return NULL;
} }
@@ -961,7 +959,7 @@ check_script_var_type(
char_u *name, char_u *name,
where_T where) where_T where)
{ {
svar_T *sv = find_typval_in_script(dest, TRUE); svar_T *sv = find_typval_in_script(dest);
int ret; int ret;
if (sv != NULL) if (sv != NULL)