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

checker: stricter []&Type{len:x} check

This commit is contained in:
Alexander Medvednikov 2022-07-05 23:51:29 +03:00
parent 9b4dec7b98
commit 163c7ba2bb
10 changed files with 119 additions and 83 deletions

View File

@ -949,6 +949,7 @@ pub fn open_append(path string) ?File {
// Note: this function will NOT return when successfull, since // Note: this function will NOT return when successfull, since
// the child process will take control over execution. // the child process will take control over execution.
pub fn execvp(cmdpath string, cmdargs []string) ? { pub fn execvp(cmdpath string, cmdargs []string) ? {
unsafe {
mut cargs := []&char{} mut cargs := []&char{}
cargs << &char(cmdpath.str) cargs << &char(cmdpath.str)
for i in 0 .. cmdargs.len { for i in 0 .. cmdargs.len {
@ -964,9 +965,11 @@ pub fn execvp(cmdpath string, cmdargs []string) ? {
if res == -1 { if res == -1 {
return error_with_code(posix_get_error_msg(C.errno), C.errno) return error_with_code(posix_get_error_msg(C.errno), C.errno)
} }
// just in case C._execvp returned ... that happens on windows ... // just in case C._execvp returned ... that happens on windows ...
exit(res) exit(res)
} }
}
// execve - loads and executes a new child process, *in place* of the current process. // execve - loads and executes a new child process, *in place* of the current process.
// The child process executable is located in `cmdpath`. // The child process executable is located in `cmdpath`.
@ -975,6 +978,7 @@ pub fn execvp(cmdpath string, cmdargs []string) ? {
// Note: this function will NOT return when successfull, since // Note: this function will NOT return when successfull, since
// the child process will take control over execution. // the child process will take control over execution.
pub fn execve(cmdpath string, cmdargs []string, envs []string) ? { pub fn execve(cmdpath string, cmdargs []string, envs []string) ? {
unsafe {
mut cargv := []&char{} mut cargv := []&char{}
mut cenvs := []&char{} mut cenvs := []&char{}
cargv << &char(cmdpath.str) cargv << &char(cmdpath.str)
@ -998,6 +1002,7 @@ pub fn execve(cmdpath string, cmdargs []string, envs []string) ? {
return error_with_code(posix_get_error_msg(C.errno), C.errno) return error_with_code(posix_get_error_msg(C.errno), C.errno)
} }
} }
}
// is_atty returns 1 if the `fd` file descriptor is open and refers to a terminal // is_atty returns 1 if the `fd` file descriptor is open and refers to a terminal
pub fn is_atty(fd int) int { pub fn is_atty(fd int) int {

View File

@ -81,6 +81,7 @@ pub fn (mut pool PoolProcessor) work_on_pointers(items []voidptr) {
if pool.njobs > 0 { if pool.njobs > 0 {
njobs = pool.njobs njobs = pool.njobs
} }
unsafe {
pool.thread_contexts = []voidptr{len: items.len} pool.thread_contexts = []voidptr{len: items.len}
pool.results = []voidptr{len: items.len} pool.results = []voidptr{len: items.len}
pool.items = []voidptr{cap: items.len} pool.items = []voidptr{cap: items.len}
@ -94,6 +95,7 @@ pub fn (mut pool PoolProcessor) work_on_pointers(items []voidptr) {
process_in_thread(mut pool, i) process_in_thread(mut pool, i)
} }
} }
}
pool.waitgroup.wait() pool.waitgroup.wait()
} }
@ -136,12 +138,14 @@ pub fn (pool &PoolProcessor) get_results<T>() []T {
// get_results_ref - get a list of type safe results in the main thread. // get_results_ref - get a list of type safe results in the main thread.
pub fn (pool &PoolProcessor) get_results_ref<T>() []&T { pub fn (pool &PoolProcessor) get_results_ref<T>() []&T {
unsafe {
mut res := []&T{cap: pool.results.len} mut res := []&T{cap: pool.results.len}
for i in 0 .. pool.results.len { for i in 0 .. pool.results.len {
res << &T(pool.results[i]) res << &T(pool.results[i])
} }
return res return res
} }
}
// set_shared_context - can be called during the setup so that you can // set_shared_context - can be called during the setup so that you can
// provide a context that is shared between all worker threads, like // provide a context that is shared between all worker threads, like

View File

@ -251,6 +251,7 @@ pub fn (mut b Builder) resolve_deps() {
eprintln(mods.str()) eprintln(mods.str())
eprintln('-------------------------------') eprintln('-------------------------------')
} }
unsafe {
mut reordered_parsed_files := []&ast.File{} mut reordered_parsed_files := []&ast.File{}
for m in mods { for m in mods {
for pf in b.parsed_files { for pf in b.parsed_files {
@ -263,6 +264,7 @@ pub fn (mut b Builder) resolve_deps() {
b.table.modules = mods b.table.modules = mods
b.parsed_files = reordered_parsed_files b.parsed_files = reordered_parsed_files
} }
}
// graph of all imported modules // graph of all imported modules
pub fn (b &Builder) import_graph() &depgraph.DepGraph { pub fn (b &Builder) import_graph() &depgraph.DepGraph {

View File

@ -271,9 +271,10 @@ pub fn (mut c Checker) check_files(ast_files []&ast.File) {
// c.files = ast_files // c.files = ast_files
mut has_main_mod_file := false mut has_main_mod_file := false
mut has_main_fn := false mut has_main_fn := false
unsafe {
mut files_from_main_module := []&ast.File{} mut files_from_main_module := []&ast.File{}
for i in 0 .. ast_files.len { for i in 0 .. ast_files.len {
mut file := unsafe { ast_files[i] } mut file := ast_files[i]
c.timers.start('checker_check $file.path') c.timers.start('checker_check $file.path')
c.check(file) c.check(file)
if file.mod.name == 'main' { if file.mod.name == 'main' {
@ -302,6 +303,7 @@ pub fn (mut c Checker) check_files(ast_files []&ast.File) {
has_main_fn = true has_main_fn = true
} }
} }
}
c.timers.start('checker_post_process_generic_fns') c.timers.start('checker_post_process_generic_fns')
last_file := c.file last_file := c.file
// post process generic functions. must be done after all files have been // post process generic functions. must be done after all files have been

View File

@ -56,6 +56,13 @@ pub fn (mut c Checker) array_init(mut node ast.ArrayInit) ast.Type {
&& c.table.cur_fn.generic_names.len == 0 { && c.table.cur_fn.generic_names.len == 0 {
c.error('generic struct cannot use in non-generic function', node.pos) c.error('generic struct cannot use in non-generic function', node.pos)
} }
// &int{} check
if node.elem_type.is_any_kind_of_pointer() && !c.inside_unsafe
&& (node.has_len || node.has_cap) {
c.warn('arrays of references need to be initialized right away (unless used inside `unsafe`)',
node.pos)
}
return node.typ return node.typ
} }
if node.is_fixed { if node.is_fixed {

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/sum.vv:2:14: warning: arrays of references need to be initialized right away (unless used inside `unsafe`)
1 | fn main() {
2 | println(*[]&int{len: 1}[0])
| ~~~~~~~
3 | }
4 |

View File

@ -0,0 +1,3 @@
fn main() {
println(*[]&int{len: 1}[0])
}

View File

@ -4896,6 +4896,7 @@ fn (mut g Gen) write_sorted_types() {
defer { defer {
g.type_definitions.writeln('// #end sorted_symbols') g.type_definitions.writeln('// #end sorted_symbols')
} }
unsafe {
mut symbols := []&ast.TypeSymbol{cap: g.table.type_symbols.len} // structs that need to be sorted mut symbols := []&ast.TypeSymbol{cap: g.table.type_symbols.len} // structs that need to be sorted
for sym in g.table.type_symbols { for sym in g.table.type_symbols {
if sym.name !in c.builtins { if sym.name !in c.builtins {
@ -4905,6 +4906,7 @@ fn (mut g Gen) write_sorted_types() {
sorted_symbols := g.sort_structs(symbols) sorted_symbols := g.sort_structs(symbols)
g.write_types(sorted_symbols) g.write_types(sorted_symbols)
} }
}
fn (mut g Gen) write_types(symbols []&ast.TypeSymbol) { fn (mut g Gen) write_types(symbols []&ast.TypeSymbol) {
for sym in symbols { for sym in symbols {
@ -5186,12 +5188,14 @@ fn (mut g Gen) sort_structs(typesa []&ast.TypeSymbol) []&ast.TypeSymbol {
'\nif you feel this is an error, please create a new issue here: https://github.com/vlang/v/issues and tag @joe-conigliaro') '\nif you feel this is an error, please create a new issue here: https://github.com/vlang/v/issues and tag @joe-conigliaro')
} }
// sort types // sort types
unsafe {
mut sorted_symbols := []&ast.TypeSymbol{cap: dep_graph_sorted.nodes.len} mut sorted_symbols := []&ast.TypeSymbol{cap: dep_graph_sorted.nodes.len}
for node in dep_graph_sorted.nodes { for node in dep_graph_sorted.nodes {
sorted_symbols << g.table.sym_by_idx(g.table.type_idxs[node.name]) sorted_symbols << g.table.sym_by_idx(g.table.type_idxs[node.name])
} }
return sorted_symbols return sorted_symbols
} }
}
[inline] [inline]
fn (g &Gen) nth_stmt_pos(n int) int { fn (g &Gen) nth_stmt_pos(n int) int {

View File

@ -99,7 +99,7 @@ pub mut:
vet_errors []vet.Error vet_errors []vet.Error
} }
__global codegen_files = []&ast.File{} __global codegen_files = unsafe { []&ast.File{} }
// for tests // for tests
pub fn parse_stmt(text string, table &ast.Table, scope &ast.Scope) ast.Stmt { pub fn parse_stmt(text string, table &ast.Table, scope &ast.Scope) ast.Stmt {
@ -425,6 +425,7 @@ pub fn parse_files(paths []string, table &ast.Table, pref &pref.Preferences) []&
} }
*/ */
} }
unsafe {
mut files := []&ast.File{cap: paths.len} mut files := []&ast.File{cap: paths.len}
for path in paths { for path in paths {
timers.start('parse_file $path') timers.start('parse_file $path')
@ -437,6 +438,7 @@ pub fn parse_files(paths []string, table &ast.Table, pref &pref.Preferences) []&
} }
return files return files
} }
}
// codegen allows you to generate V code, so that it can be parsed, // codegen allows you to generate V code, so that it can be parsed,
// checked, markused, cgen-ed etc further, just like user's V code. // checked, markused, cgen-ed etc further, just like user's V code.