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:
parent
9b4dec7b98
commit
163c7ba2bb
@ -949,6 +949,7 @@ pub fn open_append(path string) ?File {
|
||||
// Note: this function will NOT return when successfull, since
|
||||
// the child process will take control over execution.
|
||||
pub fn execvp(cmdpath string, cmdargs []string) ? {
|
||||
unsafe {
|
||||
mut cargs := []&char{}
|
||||
cargs << &char(cmdpath.str)
|
||||
for i in 0 .. cmdargs.len {
|
||||
@ -964,9 +965,11 @@ pub fn execvp(cmdpath string, cmdargs []string) ? {
|
||||
if res == -1 {
|
||||
return error_with_code(posix_get_error_msg(C.errno), C.errno)
|
||||
}
|
||||
|
||||
// just in case C._execvp returned ... that happens on windows ...
|
||||
exit(res)
|
||||
}
|
||||
}
|
||||
|
||||
// execve - loads and executes a new child process, *in place* of the current process.
|
||||
// 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
|
||||
// the child process will take control over execution.
|
||||
pub fn execve(cmdpath string, cmdargs []string, envs []string) ? {
|
||||
unsafe {
|
||||
mut cargv := []&char{}
|
||||
mut cenvs := []&char{}
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// is_atty returns 1 if the `fd` file descriptor is open and refers to a terminal
|
||||
pub fn is_atty(fd int) int {
|
||||
|
@ -81,6 +81,7 @@ pub fn (mut pool PoolProcessor) work_on_pointers(items []voidptr) {
|
||||
if pool.njobs > 0 {
|
||||
njobs = pool.njobs
|
||||
}
|
||||
unsafe {
|
||||
pool.thread_contexts = []voidptr{len: items.len}
|
||||
pool.results = []voidptr{len: 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
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.
|
||||
pub fn (pool &PoolProcessor) get_results_ref<T>() []&T {
|
||||
unsafe {
|
||||
mut res := []&T{cap: pool.results.len}
|
||||
for i in 0 .. pool.results.len {
|
||||
res << &T(pool.results[i])
|
||||
}
|
||||
return res
|
||||
}
|
||||
}
|
||||
|
||||
// set_shared_context - can be called during the setup so that you can
|
||||
// provide a context that is shared between all worker threads, like
|
||||
|
@ -251,6 +251,7 @@ pub fn (mut b Builder) resolve_deps() {
|
||||
eprintln(mods.str())
|
||||
eprintln('-------------------------------')
|
||||
}
|
||||
unsafe {
|
||||
mut reordered_parsed_files := []&ast.File{}
|
||||
for m in mods {
|
||||
for pf in b.parsed_files {
|
||||
@ -263,6 +264,7 @@ pub fn (mut b Builder) resolve_deps() {
|
||||
b.table.modules = mods
|
||||
b.parsed_files = reordered_parsed_files
|
||||
}
|
||||
}
|
||||
|
||||
// graph of all imported modules
|
||||
pub fn (b &Builder) import_graph() &depgraph.DepGraph {
|
||||
|
@ -271,9 +271,10 @@ pub fn (mut c Checker) check_files(ast_files []&ast.File) {
|
||||
// c.files = ast_files
|
||||
mut has_main_mod_file := false
|
||||
mut has_main_fn := false
|
||||
unsafe {
|
||||
mut files_from_main_module := []&ast.File{}
|
||||
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.check(file)
|
||||
if file.mod.name == 'main' {
|
||||
@ -302,6 +303,7 @@ pub fn (mut c Checker) check_files(ast_files []&ast.File) {
|
||||
has_main_fn = true
|
||||
}
|
||||
}
|
||||
}
|
||||
c.timers.start('checker_post_process_generic_fns')
|
||||
last_file := c.file
|
||||
// post process generic functions. must be done after all files have been
|
||||
|
@ -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.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
|
||||
}
|
||||
if node.is_fixed {
|
||||
|
7
vlib/v/checker/tests/ptr_array_init.out
Normal file
7
vlib/v/checker/tests/ptr_array_init.out
Normal 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 |
|
||||
|
3
vlib/v/checker/tests/ptr_array_init.vv
Normal file
3
vlib/v/checker/tests/ptr_array_init.vv
Normal file
@ -0,0 +1,3 @@
|
||||
fn main() {
|
||||
println(*[]&int{len: 1}[0])
|
||||
}
|
@ -4896,6 +4896,7 @@ fn (mut g Gen) write_sorted_types() {
|
||||
defer {
|
||||
g.type_definitions.writeln('// #end sorted_symbols')
|
||||
}
|
||||
unsafe {
|
||||
mut symbols := []&ast.TypeSymbol{cap: g.table.type_symbols.len} // structs that need to be sorted
|
||||
for sym in g.table.type_symbols {
|
||||
if sym.name !in c.builtins {
|
||||
@ -4905,6 +4906,7 @@ fn (mut g Gen) write_sorted_types() {
|
||||
sorted_symbols := g.sort_structs(symbols)
|
||||
g.write_types(sorted_symbols)
|
||||
}
|
||||
}
|
||||
|
||||
fn (mut g Gen) write_types(symbols []&ast.TypeSymbol) {
|
||||
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')
|
||||
}
|
||||
// sort types
|
||||
unsafe {
|
||||
mut sorted_symbols := []&ast.TypeSymbol{cap: dep_graph_sorted.nodes.len}
|
||||
for node in dep_graph_sorted.nodes {
|
||||
sorted_symbols << g.table.sym_by_idx(g.table.type_idxs[node.name])
|
||||
}
|
||||
return sorted_symbols
|
||||
}
|
||||
}
|
||||
|
||||
[inline]
|
||||
fn (g &Gen) nth_stmt_pos(n int) int {
|
||||
|
@ -99,7 +99,7 @@ pub mut:
|
||||
vet_errors []vet.Error
|
||||
}
|
||||
|
||||
__global codegen_files = []&ast.File{}
|
||||
__global codegen_files = unsafe { []&ast.File{} }
|
||||
|
||||
// for tests
|
||||
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}
|
||||
for path in paths {
|
||||
timers.start('parse_file $path')
|
||||
@ -437,6 +438,7 @@ pub fn parse_files(paths []string, table &ast.Table, pref &pref.Preferences) []&
|
||||
}
|
||||
return files
|
||||
}
|
||||
}
|
||||
|
||||
// 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.
|
||||
|
Loading…
Reference in New Issue
Block a user