1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

v: forbid function parameter names, shadowing imported module names (#17210)

This commit is contained in:
ChAoS_UnItY
2023-02-09 02:37:04 +08:00
committed by GitHub
parent c16549b6fd
commit 404a9aa442
45 changed files with 381 additions and 230 deletions

View File

@@ -19,10 +19,10 @@ fn main() {
fn process_files(files []string) ! {
mut table := ast.new_table()
mut pref := pref.new_preferences()
pref.is_fmt = true
pref.skip_warnings = true
pref.output_mode = .silent
mut pref_ := pref.new_preferences()
pref_.is_fmt = true
pref_.skip_warnings = true
pref_.output_mode = .silent
mut sw := time.new_stopwatch()
mut total_us := i64(0)
mut total_bytes := i64(0)
@@ -35,7 +35,7 @@ fn process_files(files []string) ! {
continue
}
// do not measure the scanning, but only the parsing:
mut p := new_parser(f, .skip_comments, table, pref)
mut p := new_parser(f, .skip_comments, table, pref_)
///
sw.restart()
_ := p.parse()
@@ -49,12 +49,12 @@ fn process_files(files []string) ! {
println('${total_us:10}us ${total_tokens:10} ${total_bytes:10} ${(f64(total_tokens) / total_bytes):7.3} | speed: ${(f64(total_bytes) / total_us):2.5f} MB/s')
}
fn new_parser(path string, comments_mode scanner.CommentsMode, table &ast.Table, pref &pref.Preferences) &parser.Parser {
fn new_parser(path string, comments_mode scanner.CommentsMode, table &ast.Table, pref_ &pref.Preferences) &parser.Parser {
mut p := &parser.Parser{
scanner: scanner.new_scanner_file(path, comments_mode, pref) or { panic(err) }
scanner: scanner.new_scanner_file(path, comments_mode, pref_) or { panic(err) }
comments_mode: comments_mode
table: table
pref: pref
pref: pref_
scope: &ast.Scope{
start_pos: 0
parent: table.global_scope

View File

@@ -15,10 +15,10 @@ fn main() {
}
fn process_files(files []string) ! {
mut pref := pref.new_preferences()
pref.is_fmt = true
pref.skip_warnings = true
pref.output_mode = .silent
mut pref_ := pref.new_preferences()
pref_.is_fmt = true
pref_.skip_warnings = true
pref_.output_mode = .silent
mut sw := time.new_stopwatch()
mut total_us := i64(0)
mut total_bytes := i64(0)
@@ -31,7 +31,7 @@ fn process_files(files []string) ! {
continue
}
sw.restart()
s := scanner.new_scanner_file(f, .skip_comments, pref)!
s := scanner.new_scanner_file(f, .skip_comments, pref_)!
f_us := sw.elapsed().microseconds()
total_us += f_us
total_bytes += s.text.len

View File

@@ -108,7 +108,7 @@ fn add_item_to_array(obj &C.cJSON, item &C.cJSON) {
C.cJSON_AddItemToArray(obj, item)
}
fn json_print(json &C.cJSON) string {
s := C.cJSON_Print(json)
fn json_print(json_ &C.cJSON) string {
s := C.cJSON_Print(json_)
return unsafe { tos3(s) }
}

View File

@@ -120,15 +120,15 @@ fn json(file string) string {
// use as permissive preferences as possible, so that `v ast`
// can print the AST of arbitrary V files, even .vsh or ones
// that require globals:
mut pref := &pref.Preferences{}
pref.fill_with_defaults()
pref.enable_globals = true
pref.is_fmt = true
mut pref_ := &pref.Preferences{}
pref_.fill_with_defaults()
pref_.enable_globals = true
pref_.is_fmt = true
//
mut t := Tree{
root: new_object()
table: ast.new_table()
pref: pref
pref: pref_
}
// parse file with comment
ast_file := parser.parse_file(file, t.table, .parse_comments, t.pref)
@@ -359,9 +359,9 @@ fn (t Tree) imports(nodes []ast.Import) &Node {
return import_array
}
fn (t Tree) errors(errors []errors.Error) &Node {
fn (t Tree) errors(errors_ []errors.Error) &Node {
mut errs := new_array()
for e in errors {
for e in errors_ {
obj := new_object()
obj.add_terse('message', t.string_node(e.message))
obj.add_terse('file_path', t.string_node(e.file_path))

View File

@@ -232,9 +232,9 @@ fn (mut foptions FormatOptions) post_process_file(file string, formatted_file_pa
}
diff_cmd := foptions.find_diff_cmd()
foptions.vlog('Using diff command: ${diff_cmd}')
diff := diff.color_compare_files(diff_cmd, file, formatted_file_path)
if diff.len > 0 {
println(diff)
diff_ := diff.color_compare_files(diff_cmd, file, formatted_file_path)
if diff_.len > 0 {
println(diff_)
}
return
}

View File

@@ -301,13 +301,13 @@ fn vpm_install_from_vcs(module_names []string, vcs_key string) {
vmod_path := os.join_path(final_module_path, 'v.mod')
if os.exists(vmod_path) {
data := os.read_file(vmod_path) or { return }
vmod := parse_vmod(data) or {
vmod_ := parse_vmod(data) or {
eprintln(err)
return
}
minfo := mod_name_info(vmod.name)
minfo := mod_name_info(vmod_.name)
if final_module_path != minfo.final_module_path {
println('Relocating module from "${name}" to "${vmod.name}" ( "${minfo.final_module_path}" ) ...')
println('Relocating module from "${name}" to "${vmod_.name}" ( "${minfo.final_module_path}" ) ...')
if os.exists(minfo.final_module_path) {
eprintln('Warning module "${minfo.final_module_path}" already exsits!')
eprintln('Removing module "${minfo.final_module_path}" ...')
@@ -330,10 +330,10 @@ fn vpm_install_from_vcs(module_names []string, vcs_key string) {
}
continue
}
println('Module "${name}" relocated to "${vmod.name}" successfully.')
println('Module "${name}" relocated to "${vmod_.name}" successfully.')
final_module_path = minfo.final_module_path
}
name = vmod.name
name = vmod_.name
}
resolve_dependencies(name, final_module_path, module_names)
}
@@ -646,13 +646,13 @@ fn resolve_dependencies(name string, module_path string, module_names []string)
return
}
data := os.read_file(vmod_path) or { return }
vmod := parse_vmod(data) or {
vmod_ := parse_vmod(data) or {
eprintln(err)
return
}
mut deps := []string{}
// filter out dependencies that were already specified by the user
for d in vmod.deps {
for d in vmod_.deps {
if d !in module_names {
deps << d
}
@@ -666,11 +666,11 @@ fn resolve_dependencies(name string, module_path string, module_names []string)
fn parse_vmod(data string) !Vmod {
manifest := vmod.decode(data) or { return error('Parsing v.mod file failed, ${err}') }
mut vmod := Vmod{}
vmod.name = manifest.name
vmod.version = manifest.version
vmod.deps = manifest.dependencies
return vmod
mut vmod_ := Vmod{}
vmod_.name = manifest.name
vmod_.version = manifest.version
vmod_.deps = manifest.dependencies
return vmod_
}
fn get_working_server_url() string {

View File

@@ -13,13 +13,13 @@ fn main() {
fp.description('\nScan .v source files, and print the V tokens contained in them.')
fp.arguments_description('PATH [PATH]...')
fp.limit_free_args_to_at_least(1)!
pref := pref.new_preferences()
pref_ := pref.new_preferences()
mut all_paths := fp.remaining_parameters()
for path in all_paths {
mut scanner := scanner.new_scanner_file(path, .parse_comments, pref)!
mut scanner_ := scanner.new_scanner_file(path, .parse_comments, pref_)!
mut tok := token.Token{}
for tok.kind != .eof {
tok = scanner.scan()
tok = scanner_.scan()
pos := tok.pos()
location := '${path}:${pos.line_nr + 1}:${pos.col + 1}:'
println('${location:-32} | pos: ${pos.pos:-5} | ${tok.debug()}')