0
0
mirror of https://github.com/vim/vim.git synced 2025-09-24 03:44:06 -04:00

patch 8.2.0368: Vim9: import that redefines local variable does not fail

Problem:    Vim9: import that redefines local variable does not fail.
Solution:   Check for already defined symbols.
This commit is contained in:
Bram Moolenaar
2020-03-09 19:25:27 +01:00
parent 3a2505cc18
commit 5269bd2a72
6 changed files with 63 additions and 7 deletions

View File

@@ -1,4 +1,5 @@
/* vim9compile.c */ /* vim9compile.c */
int check_defined(char_u *p, int len, cctx_T *cctx);
char_u *skip_type(char_u *start); char_u *skip_type(char_u *start);
type_T *parse_type(char_u **arg, garray_T *type_list); type_T *parse_type(char_u **arg, garray_T *type_list);
char *vartype_name(vartype_T type); char *vartype_name(vartype_T type);

View File

@@ -5,5 +5,5 @@ void ex_export(exarg_T *eap);
void free_imports(int sid); void free_imports(int sid);
void ex_import(exarg_T *eap); void ex_import(exarg_T *eap);
int find_exported(int sid, char_u **argp, int *name_len, ufunc_T **ufunc, type_T **type); int find_exported(int sid, char_u **argp, int *name_len, ufunc_T **ufunc, type_T **type);
char_u *handle_import(char_u *arg_start, garray_T *gap, int import_sid); char_u *handle_import(char_u *arg_start, garray_T *gap, int import_sid, void *cctx);
/* vim: set ft=c : */ /* vim: set ft=c : */

View File

@@ -362,7 +362,7 @@ let s:export_script_lines =<< trim END
enddef enddef
END END
def Test_vim9script() def Test_vim9_import_export()
let import_script_lines =<< trim END let import_script_lines =<< trim END
vim9script vim9script
import {exported, Exported} from './Xexport.vim' import {exported, Exported} from './Xexport.vim'
@@ -449,6 +449,33 @@ def Test_vim9script()
writefile(import_not_exported_lines, 'Ximport.vim') writefile(import_not_exported_lines, 'Ximport.vim')
assert_fails('source Ximport.vim', 'E1049:') assert_fails('source Ximport.vim', 'E1049:')
" try to import something that is already defined
let import_already_defined =<< trim END
vim9script
let exported = 'something'
import exported from './Xexport.vim'
END
writefile(import_already_defined, 'Ximport.vim')
assert_fails('source Ximport.vim', 'E1073:')
" try to import something that is already defined
import_already_defined =<< trim END
vim9script
let exported = 'something'
import * as exported from './Xexport.vim'
END
writefile(import_already_defined, 'Ximport.vim')
assert_fails('source Ximport.vim', 'E1073:')
" try to import something that is already defined
import_already_defined =<< trim END
vim9script
let exported = 'something'
import {exported} from './Xexport.vim'
END
writefile(import_already_defined, 'Ximport.vim')
assert_fails('source Ximport.vim', 'E1073:')
" import a very long name, requires making a copy " import a very long name, requires making a copy
let import_long_name_lines =<< trim END let import_long_name_lines =<< trim END
vim9script vim9script
@@ -482,10 +509,11 @@ def Test_vim9script()
vim9script vim9script
import {exported name} from './Xexport.vim' import {exported name} from './Xexport.vim'
END END
writefile(import_missing_comma_lines, 'Ximport.vim') writefile(import_missing_comma_lines, 'Ximport3.vim')
assert_fails('source Ximport.vim', 'E1046:') assert_fails('source Ximport3.vim', 'E1046:')
delete('Ximport.vim') delete('Ximport.vim')
delete('Ximport3.vim')
delete('Xexport.vim') delete('Xexport.vim')
" Check that in a Vim9 script 'cpo' is set to the Vim default. " Check that in a Vim9 script 'cpo' is set to the Vim default.

View File

@@ -738,6 +738,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 */
/**/
368,
/**/ /**/
367, 367,
/**/ /**/

View File

@@ -203,6 +203,25 @@ lookup_script(char_u *name, size_t len)
return di == NULL ? FAIL: OK; return di == NULL ? FAIL: OK;
} }
/*
* Check if "p[len]" is already defined, either in script "import_sid" or in
* compilation context "cctx".
* Return FAIL and give an error if it defined.
*/
int
check_defined(char_u *p, int len, cctx_T *cctx)
{
if (lookup_script(p, len) == OK
|| (cctx != NULL
&& (lookup_local(p, len, cctx) >= 0
|| find_imported(p, len, cctx) != NULL)))
{
semsg("E1073: imported name already defined: %s", p);
return FAIL;
}
return OK;
}
static type_T * static type_T *
get_list_type(type_T *member_type, garray_T *type_list) get_list_type(type_T *member_type, garray_T *type_list)
{ {
@@ -3812,7 +3831,7 @@ theend:
static char_u * static char_u *
compile_import(char_u *arg, cctx_T *cctx) compile_import(char_u *arg, cctx_T *cctx)
{ {
return handle_import(arg, &cctx->ctx_imports, 0); return handle_import(arg, &cctx->ctx_imports, 0, cctx);
} }
/* /*

View File

@@ -143,7 +143,8 @@ ex_import(exarg_T *eap)
emsg(_(e_needs_vim9)); emsg(_(e_needs_vim9));
else else
{ {
char_u *cmd_end = handle_import(eap->arg, NULL, current_sctx.sc_sid); char_u *cmd_end = handle_import(eap->arg, NULL,
current_sctx.sc_sid, NULL);
if (cmd_end != NULL) if (cmd_end != NULL)
eap->nextcmd = check_nextcmd(cmd_end); eap->nextcmd = check_nextcmd(cmd_end);
@@ -238,7 +239,7 @@ find_exported(
* Returns a pointer to after the command or NULL in case of failure * Returns a pointer to after the command or NULL in case of failure
*/ */
char_u * char_u *
handle_import(char_u *arg_start, garray_T *gap, int import_sid) handle_import(char_u *arg_start, garray_T *gap, int import_sid, void *cctx)
{ {
char_u *arg = arg_start; char_u *arg = arg_start;
char_u *cmd_end; char_u *cmd_end;
@@ -278,6 +279,8 @@ handle_import(char_u *arg_start, garray_T *gap, int import_sid)
++arg; ++arg;
as_len = (int)(arg - as_ptr); as_len = (int)(arg - as_ptr);
arg = skipwhite(arg); arg = skipwhite(arg);
if (check_defined(as_ptr, as_len, cctx) == FAIL)
return NULL;
} }
else if (*arg_start == '*') else if (*arg_start == '*')
{ {
@@ -389,6 +392,9 @@ handle_import(char_u *arg_start, garray_T *gap, int import_sid)
if (idx < 0 && ufunc == NULL) if (idx < 0 && ufunc == NULL)
return NULL; return NULL;
if (check_defined(name, name_len, cctx) == FAIL)
return NULL;
imported = new_imported(gap != NULL ? gap imported = new_imported(gap != NULL ? gap
: &SCRIPT_ITEM(import_sid)->sn_imports); : &SCRIPT_ITEM(import_sid)->sn_imports);
if (imported == NULL) if (imported == NULL)